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

taosdata / TDengine / #4348

21 Jun 2025 07:48AM UTC coverage: 62.366% (+1.8%) from 60.571%
#4348

push

travis-ci

web-flow
docs: add OpenMetrics support and configuration details to taosAdapter documentation (#31427)

* docs: add OpenMetrics support and configuration details to taosAdapter documentation

* docs: enhance OpenMetrics section in taosAdapter documentation

156282 of 319947 branches covered (48.85%)

Branch coverage included in aggregate %.

242147 of 318911 relevant lines covered (75.93%)

6151642.33 hits per line

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

60.5
/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) {
124,820✔
64
  int32_t code = 0;
124,820✔
65
  int32_t lino = 0;
124,820✔
66

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

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

78
  // name
79
  char *name = NULL;
124,866✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
124,846!
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);
124,846✔
87
  if (uid == 0) {
124,824✔
88
    uid = tGenIdPI64();
123,331✔
89
  }
90
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
124,867✔
91

92
  // btime
93
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
124,867✔
94

95
  tEndDecode(pCoder);
124,867✔
96

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

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
65,222!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
143,658✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
78,441✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
78,436!
127
  }
128

129
  tEndDecode(&dc);
65,217✔
130

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

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

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

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

157
  code = 0;
5,485✔
158

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

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

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

177
  SArray *tbUids = NULL;
18,972✔
178
  int64_t timestampMs = 0;
18,972✔
179

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

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

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
18,960✔
201
    ttlReq.pTbUids = tbUids;
18,954✔
202
  }
203

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

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

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

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

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

230
  code = 0;
30✔
231

232
_exit:
18,956✔
233
  taosArrayDestroy(tbUids);
18,956✔
234

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

242
  return code;
18,957✔
243
}
244

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

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

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
2,774,117✔
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
46,407✔
267
    TSDB_CHECK_CODE(code, lino, _exit);
46,401!
268
  }
269

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

276
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
2,774,069✔
277
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
46,400✔
278
    pCoder->pos += sizeof(int64_t);
46,400✔
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
2,727,688!
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

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

291
  // scan and check
292
  TSKEY now = btimeMs;
2,774,070✔
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
2,774,070✔
294
    now *= 1000;
15,910✔
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
2,758,160✔
296
    now *= 1000000;
3,809✔
297
  }
298

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

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

327
    if (colData.flag != HAS_VALUE) {
10,278!
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++) {
9,977,836✔
333
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
9,967,559!
334
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
48✔
335
        TSDB_CHECK_CODE(code, lino, _exit);
48✔
336
      }
337
    }
338

339
    for (uint64_t i = 1; i < nColData; i++) {
31,187✔
340
      code = tDecodeColData(version, pCoder, &colData);
20,912✔
341
      if (code) {
20,910!
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) {
2,763,757!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      TSDB_CHECK_CODE(code, lino, _exit);
95!
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
99,140,191✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
96,376,529✔
355
      pCoder->pos += pRow->len;
96,376,529✔
356
#ifndef NO_UNALIGNED_ACCESS
357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
96,376,529✔
358
#else
359
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
360
      if (ts < minKey || ts > maxKey) {
361
#endif
362
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
3,336✔
363
        TSDB_CHECK_CODE(code, lino, _exit);
3,336✔
364
      }
365

366
      // Check pRow->sver
367
      if (pRow->sver != submitTbData.sver) {
96,376,434!
368
        code = TSDB_CODE_INVALID_DATA_FMT;
×
369
        TSDB_CHECK_CODE(code, lino, _exit);
×
370
      }
371
    }
372
  }
373

374
  if (!tDecodeIsEnd(pCoder)) {
2,773,937!
375
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
2,773,994✔
376
    pCoder->pos += sizeof(int64_t);
2,773,994✔
377
  }
378

379
  tEndDecode(pCoder);
2,773,937✔
380

381
_exit:
2,774,045✔
382
  if (code) {
2,774,045✔
383
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
96!
384
  }
385
  return code;
2,774,068✔
386
}
387
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,077,885✔
388
  int32_t code = 0;
2,077,885✔
389
  int32_t lino = 0;
2,077,885✔
390

391
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
2,077,885!
392
    return TSDB_CODE_MSG_PREPROCESSED;
×
393
  }
394

395
  SDecoder *pCoder = &(SDecoder){0};
2,077,885✔
396

397
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
2,077,885!
398
    code = TSDB_CODE_INVALID_MSG;
×
399
    TSDB_CHECK_CODE(code, lino, _exit);
×
400
  }
401

402
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
2,077,963✔
403

404
  if (tStartDecode(pCoder) < 0) {
2,077,907!
405
    code = TSDB_CODE_INVALID_MSG;
×
406
    TSDB_CHECK_CODE(code, lino, _exit);
×
407
  }
408

409
  uint64_t nSubmitTbData;
410
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
2,077,905!
411
    code = TSDB_CODE_INVALID_MSG;
×
412
    TSDB_CHECK_CODE(code, lino, _exit);
×
413
  }
414

415
  int64_t btimeMs = taosGetTimestampMs();
2,077,995✔
416
  int64_t ctimeMs = btimeMs;
2,077,995✔
417
  for (int32_t i = 0; i < nSubmitTbData; i++) {
4,851,967✔
418
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
2,774,065✔
419
    TSDB_CHECK_CODE(code, lino, _exit);
2,774,068✔
420
  }
421

422
  tEndDecode(pCoder);
2,077,902✔
423

424
_exit:
2,077,974✔
425
  tDecoderClear(pCoder);
2,077,974✔
426
  if (code) {
2,077,947✔
427
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
96!
428
           tstrerror(code), TMSG_INFO(pMsg->msgType));
429
  }
430
  return code;
2,077,915✔
431
}
432

433
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
7,147✔
434
  int32_t code = 0;
7,147✔
435

436
  int32_t    size;
437
  int32_t    ret;
438
  uint8_t   *pCont;
439
  SEncoder  *pCoder = &(SEncoder){0};
7,147✔
440
  SDeleteRes res = {0};
7,147✔
441

442
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
7,147✔
443
  initStorageAPI(&handle.api);
7,147✔
444

445
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
7,147✔
446
  if (code) goto _exit;
7,147!
447

448
  res.ctimeMs = taosGetTimestampMs();
7,147✔
449
  // malloc and encode
450
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
7,147!
451
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
7,147✔
452

453
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
7,147✔
454
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
7,147✔
455

456
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
7,147✔
457
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
7,147!
458
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
459
  }
460
  tEncoderClear(pCoder);
7,147✔
461

462
  rpcFreeCont(pMsg->pCont);
7,146✔
463
  pMsg->pCont = pCont;
7,147✔
464
  pMsg->contLen = size + sizeof(SMsgHead);
7,147✔
465

466
  taosArrayDestroy(res.uidList);
7,147✔
467

468
_exit:
7,147✔
469
  return code;
7,147✔
470
}
471

472
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
930✔
473
  int32_t code = 0;
930✔
474
  int32_t lino = 0;
930✔
475

476
  int64_t         ctimeMs = taosGetTimestampMs();
930✔
477
  SBatchDeleteReq pReq = {0};
930✔
478
  SDecoder       *pCoder = &(SDecoder){0};
930✔
479

480
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
930✔
481

482
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
930!
483
    code = TSDB_CODE_INVALID_MSG;
×
484
  }
485

486
  tDecoderClear(pCoder);
930✔
487
  taosArrayDestroy(pReq.deleteReqs);
930✔
488

489
  if (code) {
930!
490
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
491
  } else {
492
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
930✔
493
  }
494
  return code;
930✔
495
}
496

497
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
22✔
498
  SVArbCheckSyncReq syncReq = {0};
22✔
499

500
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
22!
501
                                    &syncReq) != 0) {
502
    return TSDB_CODE_INVALID_MSG;
×
503
  }
504

505
  int32_t ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
22✔
506
  if (ret != 0) {
22✔
507
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
15!
508
  }
509

510
  int32_t code = terrno;
22✔
511
  tFreeSVArbCheckSyncReq(&syncReq);
22✔
512

513
  return code;
22✔
514
}
515

516
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
15,499✔
517
  int32_t          code = TSDB_CODE_SUCCESS;
15,499✔
518
  int32_t          lino = 0;
15,499✔
519
  int32_t          size = 0;
15,499✔
520
  SDecoder         dc = {0};
15,499✔
521
  SEncoder         ec = {0};
15,499✔
522
  SVDropTbBatchReq receivedBatchReqs = {0};
15,499✔
523
  SVDropTbBatchReq sentBatchReqs = {0};
15,499✔
524

525
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
15,499✔
526

527
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
15,499✔
528
  if (code < 0) {
15,499!
529
    terrno = code;
×
530
    TSDB_CHECK_CODE(code, lino, _exit);
×
531
  }
532
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
15,499✔
533
  if (!sentBatchReqs.pArray) {
15,499!
534
    code = terrno;
×
535
    goto _exit;
×
536
  }
537

538
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
31,018✔
539
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
15,519✔
540
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
15,519✔
541
    if (uid == 0) {
15,519!
542
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
543
      continue;
×
544
    }
545
    pReq->uid = uid;
15,519✔
546
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
15,519✔
547
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
31,038!
548
      code = terrno;
×
549
      goto _exit;
×
550
    }
551
  }
552
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
15,499✔
553

554
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
15,499!
555
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
15,499✔
556
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
15,499✔
557
  tEncoderClear(&ec);
15,499✔
558
  if (code != TSDB_CODE_SUCCESS) {
15,499!
559
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
560
    TSDB_CHECK_CODE(code, lino, _exit);
×
561
  }
562

563
_exit:
15,499✔
564
  tDecoderClear(&dc);
15,499✔
565
  if (sentBatchReqs.pArray) {
15,499!
566
    taosArrayDestroy(sentBatchReqs.pArray);
15,499✔
567
  }
568
  return code;
15,499✔
569
}
570

571
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,274,582✔
572
  int32_t code = 0;
2,274,582✔
573

574
  switch (pMsg->msgType) {
2,274,582✔
575
    case TDMT_VND_CREATE_TABLE: {
65,232✔
576
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
65,232✔
577
    } break;
65,242✔
578
    case TDMT_VND_ALTER_TABLE: {
5,485✔
579
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
5,485✔
580
    } break;
5,485✔
581
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
18,974✔
582
    case TDMT_VND_DROP_TTL_TABLE: {
583
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
18,974✔
584
    } break;
18,952✔
585
    case TDMT_VND_SUBMIT: {
2,077,965✔
586
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
2,077,965✔
587
    } break;
2,077,910✔
588
    case TDMT_VND_DELETE: {
7,147✔
589
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
7,147✔
590
    } break;
7,147✔
591
    case TDMT_VND_BATCH_DEL: {
930✔
592
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
930✔
593
    } break;
930✔
594
    case TDMT_VND_ARB_CHECK_SYNC: {
22✔
595
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
22✔
596
    } break;
22✔
597
    case TDMT_VND_DROP_TABLE: {
15,499✔
598
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,499✔
599
    } break;
15,499✔
600
    default:
83,328✔
601
      break;
83,328✔
602
  }
603

604
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
2,274,515✔
605
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
111!
606
           TMSG_INFO(pMsg->msgType));
607
  }
608
  return code;
2,274,533✔
609
}
610

611
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
3,041,799✔
612
  int32_t code = 0;
3,041,799✔
613
  void   *ptr = NULL;
3,041,799✔
614
  void   *pReq;
615
  int32_t len;
616

617
  (void)taosThreadMutexLock(&pVnode->mutex);
3,041,799✔
618
  if (pVnode->disableWrite) {
3,042,125!
619
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
620
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
621
    return TSDB_CODE_VND_WRITE_DISABLED;
×
622
  }
623
  (void)taosThreadMutexUnlock(&pVnode->mutex);
3,042,125✔
624

625
  if (ver <= pVnode->state.applied) {
3,042,131!
626
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
627
           pVnode->state.applied);
628
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
629
  }
630

631
  vGDebug(&pMsg->info.traceId,
3,042,131!
632
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
633
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
634
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
635
          pMsg->info.conn.applyTerm, pMsg->contLen);
636

637
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
3,041,830!
638
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
639
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
640
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
641
  }
642

643
  if (!(pVnode->state.applied + 1 == ver)) {
3,041,830!
644
    vError("vgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
645
           pVnode->state.applied + 1, ver);
646
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
647
  }
648

649
  atomic_store_64(&pVnode->state.applied, ver);
3,041,830✔
650
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
3,042,089✔
651

652
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
3,042,109✔
653

654
  // skip header
655
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
3,009,328✔
656
  len = pMsg->contLen - sizeof(SMsgHead);
3,009,328✔
657
  bool needCommit = false;
3,009,328✔
658

659
  switch (pMsg->msgType) {
3,009,328!
660
    /* META */
661
    case TDMT_VND_CREATE_STB:
26,814✔
662
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
26,814✔
663
      break;
26,869✔
664
    case TDMT_VND_ALTER_STB:
10,905✔
665
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
10,905!
666
      break;
10,919✔
667
    case TDMT_VND_DROP_STB:
1,854✔
668
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
1,854!
669
      break;
1,859✔
670
    case TDMT_VND_CREATE_TABLE:
74,724✔
671
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
74,724!
672
      break;
74,793✔
673
    case TDMT_VND_ALTER_TABLE:
5,491✔
674
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
5,491!
675
      break;
5,491✔
676
    case TDMT_VND_DROP_TABLE:
15,541✔
677
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
15,541!
678
      break;
15,541✔
679
    case TDMT_VND_DROP_TTL_TABLE:
×
680
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
681
      break;
×
682
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
30✔
683
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
30!
684
      break;
30✔
685
    case TDMT_VND_TRIM:
17✔
686
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
17!
687
      break;
17✔
688
    case TDMT_VND_S3MIGRATE:
×
689
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
690
      break;
×
691
    case TDMT_VND_CREATE_SMA:
×
692
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
693
      break;
×
694
    /* TSDB */
695
    case TDMT_VND_SUBMIT:
2,795,586✔
696
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
2,795,586✔
697
      break;
2,795,619✔
698
    case TDMT_VND_DELETE:
8,347✔
699
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
8,347!
700
      break;
8,347✔
701
    case TDMT_VND_BATCH_DEL:
930✔
702
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
930!
703
      break;
930✔
704
    /* TQ */
705
#if defined(USE_TQ) || defined(USE_STREAM)
706
    case TDMT_VND_TMQ_SUBSCRIBE:
3,162✔
707
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
3,162!
708
        goto _err;
×
709
      }
710
      break;
3,168✔
711
    case TDMT_VND_TMQ_DELETE_SUB:
921✔
712
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
921!
713
        goto _err;
×
714
      }
715
      break;
924✔
716
    case TDMT_VND_TMQ_COMMIT_OFFSET:
13,509✔
717
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
13,509!
718
        goto _err;
×
719
      }
720
      break;
13,515✔
721
    case TDMT_VND_TMQ_ADD_CHECKINFO:
121✔
722
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
121!
723
        goto _err;
×
724
      }
725
      break;
121✔
726
    case TDMT_VND_TMQ_DEL_CHECKINFO:
10✔
727
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
10!
728
        goto _err;
×
729
      }
730
      break;
10✔
731
    case TDMT_STREAM_TASK_DEPLOY: {
6,565✔
732
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
6,565!
733
        goto _err;
×
734
      }
735
    } break;
6,565✔
736
    case TDMT_STREAM_TASK_DROP: {
2,633✔
737
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
2,633!
738
        goto _err;
×
739
      }
740
    } break;
2,633✔
741
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
1,312✔
742
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
1,312!
743
        goto _err;
×
744
      }
745
    } break;
1,312✔
746
    case TDMT_STREAM_CONSEN_CHKPT: {
26✔
747
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
26!
748
        goto _err;
×
749
      }
750

751
    } break;
26✔
752
    case TDMT_STREAM_TASK_PAUSE: {
231✔
753
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
372!
754
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
141✔
755
        goto _err;
×
756
      }
757
    } break;
231✔
758
    case TDMT_STREAM_TASK_RESUME: {
224✔
759
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
358!
760
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
134✔
761
        goto _err;
×
762
      }
763
    } break;
224✔
764
    case TDMT_VND_STREAM_TASK_RESET: {
×
765
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
766
        goto _err;
×
767
      }
768

769
    } break;
×
770
    case TDMT_VND_STREAM_ALL_STOP: {
3,613✔
771
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessAllTaskStopReq(pVnode->pTq, pMsg)) < 0) {
3,613!
772
        goto _err;
×
773
      }
774

775
    } break;
3,616✔
776
#endif
777
    case TDMT_VND_ALTER_CONFIRM:
11,530✔
778
      needCommit = pVnode->config.hashChange;
11,530✔
779
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
11,530!
780
        goto _err;
×
781
      }
782
      break;
11,530✔
783
    case TDMT_VND_ALTER_CONFIG:
586✔
784
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
586✔
785
      break;
586✔
786
    case TDMT_VND_COMMIT:
23,537✔
787
      needCommit = true;
23,537✔
788
      break;
23,537✔
789
    case TDMT_VND_CREATE_INDEX:
17✔
790
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
17✔
791
      break;
17✔
792
    case TDMT_VND_DROP_INDEX:
14✔
793
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
14✔
794
      break;
14✔
795
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
842✔
796
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
842✔
797
      break;
842✔
798
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
28✔
799
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
28✔
800
      break;
28✔
801
    case TDMT_VND_COMPACT:
194✔
802
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
194✔
803
      goto _exit;
194✔
804
    case TDMT_SYNC_CONFIG_CHANGE:
×
805
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
806
      break;
×
807
#ifdef TD_ENTERPRISE
808
    case TDMT_VND_KILL_COMPACT:
×
809
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
810
      break;
×
811
#endif
812
    /* ARB */
813
    case TDMT_VND_ARB_CHECK_SYNC:
14✔
814
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
14✔
815
      break;
14✔
816
    default:
×
817
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
818
      return TSDB_CODE_INVALID_MSG;
×
819
  }
820

821
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
3,009,328!
822

823
  walApplyVer(pVnode->pWal, ver);
3,009,371✔
824

825
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
3,009,338✔
826
  if (code) {
3,009,356!
827
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
828
    return code;
×
829
  }
830

831
  // commit if need
832
  if (needCommit) {
3,009,356✔
833
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
23,552✔
834
    code = vnodeAsyncCommit(pVnode);
23,614✔
835
    if (code) {
23,613✔
836
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
5!
837
      goto _err;
5✔
838
    }
839

840
    // start a new one
841
    code = vnodeBegin(pVnode);
23,608✔
842
    if (code) {
23,599✔
843
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
6!
844
      goto _err;
×
845
    }
846
  }
847

848
_exit:
3,009,397✔
849
  return 0;
3,041,848✔
850

851
_err:
16✔
852
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
16!
853
         tstrerror(terrno), ver);
854
  return code;
16✔
855
}
856

857
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,666,391✔
858
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
1,666,391✔
859
    return 0;
82,610✔
860
  }
861

862
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
1,583,781✔
863
}
864

865
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,668,896✔
866
  vTrace("message in vnode query queue is processing");
1,668,896✔
867
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
1,668,896✔
868
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,293✔
869
    return 0;
1,293✔
870
  }
871

872
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
1,667,596!
873
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
874
    return 0;
×
875
  }
876

877
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
1,667,596✔
878
  initStorageAPI(&handle.api);
1,667,596✔
879
  int32_t code = TSDB_CODE_SUCCESS;
1,667,488✔
880
  bool    redirected = false;
1,667,488✔
881

882
  switch (pMsg->msgType) {
1,667,488✔
883
    case TDMT_SCH_QUERY:
1,419,440✔
884
      if (!syncIsReadyForRead(pVnode->sync)) {
1,419,440✔
885
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
3,400!
886
        redirected = true;
3,400✔
887
      }
888
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,419,486✔
889
      if (redirected) {
1,418,182✔
890
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,821✔
891
        return 0;
3,400✔
892
      }
893
      return code;
1,415,361✔
894
    case TDMT_SCH_MERGE_QUERY:
166,703✔
895
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
166,703✔
896
    case TDMT_SCH_QUERY_CONTINUE:
12,934✔
897
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
12,934✔
898
    case TDMT_VND_TMQ_CONSUME:
53,886✔
899
      return tqProcessPollReq(pVnode->pTq, pMsg);
53,886✔
900
    case TDMT_VND_TMQ_CONSUME_PUSH:
14,490✔
901
      return tqProcessPollPush(pVnode->pTq);
14,490✔
902
    default:
35✔
903
      vError("unknown msg type:%d in query queue", pMsg->msgType);
35!
904
      return TSDB_CODE_APP_ERROR;
×
905
  }
906
}
907

908
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
4,330,288✔
909
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
4,330,288✔
910
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
4,330,288✔
911
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
2,904,844!
912
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
2,813,659!
913
      !syncIsReadyForRead(pVnode->sync)) {
1,518,343✔
914
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,567✔
915
    return 0;
1,567✔
916
  }
917

918
  switch (pMsg->msgType) {
4,328,719!
919
    case TDMT_SCH_FETCH:
1,656,832✔
920
    case TDMT_SCH_MERGE_FETCH:
921
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,656,832✔
922
    case TDMT_SCH_FETCH_RSP:
×
923
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
924
    // case TDMT_SCH_CANCEL_TASK:
925
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
926
    case TDMT_SCH_DROP_TASK:
1,584,490✔
927
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,584,490✔
928
    case TDMT_SCH_TASK_NOTIFY:
39✔
929
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
39✔
930
    case TDMT_SCH_QUERY_HEARTBEAT:
995,827✔
931
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
995,827✔
932
    case TDMT_VND_TABLE_META:
1,768✔
933
    case TDMT_VND_TABLE_NAME:
934
      return vnodeGetTableMeta(pVnode, pMsg, true);
1,768✔
935
    case TDMT_VND_TABLE_CFG:
×
936
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
937
    case TDMT_VND_BATCH_META:
89,688✔
938
      return vnodeGetBatchMeta(pVnode, pMsg);
89,688✔
939
    case TDMT_VND_VSUBTABLES_META:
×
940
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
941
    case TDMT_VND_VSTB_REF_DBS:
×
942
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
943
#ifdef TD_ENTERPRISE
944
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
38✔
945
      return vnodeQueryCompactProgress(pVnode, pMsg);
38✔
946
#endif
947
      //    case TDMT_VND_TMQ_CONSUME:
948
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
949
#ifdef USE_TQ
950
    case TDMT_VND_TMQ_VG_WALINFO:
11✔
951
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
11✔
952
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2✔
953
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2✔
954
    case TDMT_VND_TMQ_SEEK:
24✔
955
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
956
#endif
957
    default:
×
958
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
959
      return TSDB_CODE_APP_ERROR;
×
960
  }
961
}
962
#ifdef USE_STREAM
963
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
102,098✔
964
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
102,098✔
965

966
  // todo: NOTE: some command needs to run on follower, such as, stop_all_tasks
967
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
102,098!
968
       pMsg->msgType == TDMT_VND_BATCH_META) &&
102,142✔
969
      !syncIsReadyForRead(pVnode->sync)) {
×
970
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
971
    return 0;
×
972
  }
973

974
  switch (pMsg->msgType) {
102,141!
975
    case TDMT_STREAM_TASK_RUN:
101,299✔
976
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
101,299✔
977
    case TDMT_STREAM_RETRIEVE:
424✔
978
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
424✔
979
    case TDMT_STREAM_RETRIEVE_RSP:
418✔
980
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
418✔
981
    case TDMT_VND_GET_STREAM_PROGRESS:
×
982
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
983
    default:
×
984
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
985
      return TSDB_CODE_APP_ERROR;
×
986
  }
987
}
988

989
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
58,565✔
990
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
58,565✔
991
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
58,565!
992
       pMsg->msgType == TDMT_VND_BATCH_META) &&
58,568✔
993
      !syncIsReadyForRead(pVnode->sync)) {
×
994
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
995
    return 0;
×
996
  }
997

998
  switch (pMsg->msgType) {
58,565!
999
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
5,246✔
1000
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
5,246✔
1001
    case TDMT_STREAM_TASK_DISPATCH:
11,970✔
1002
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
11,970✔
1003
    case TDMT_STREAM_TASK_DISPATCH_RSP:
12,290✔
1004
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
12,290✔
1005
    case TDMT_VND_STREAM_TASK_CHECK:
11,321✔
1006
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
11,321✔
1007
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
9,686✔
1008
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
9,686✔
1009
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
2,382✔
1010
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
2,382✔
1011
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
2,382✔
1012
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
2,382✔
1013
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
1014
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
1015
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
1016
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
1017
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
1,663✔
1018
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
1,663✔
1019
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
1,625✔
1020
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
1,625✔
1021
    default:
×
1022
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
1023
      return TSDB_CODE_APP_ERROR;
×
1024
  }
1025
}
1026

1027
int32_t vnodeProcessStreamLongExecMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,023✔
1028
  vTrace("vgId:%d, msg:%p in stream long exec queue is processing", pVnode->config.vgId, pMsg);
1,023✔
1029
  if (!syncIsReadyForRead(pVnode->sync)) {
1,023!
1030
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1031
    return 0;
×
1032
  }
1033

1034
  switch (pMsg->msgType) {
1,023!
1035
    case TDMT_VND_STREAM_SCAN_HISTORY:
1,023✔
1036
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
1,023✔
1037
    default:
×
1038
      vError("unknown msg type:%d in stream long exec queue", pMsg->msgType);
×
1039
      return TSDB_CODE_APP_ERROR;
×
1040
  }
1041
}
1042

1043
int32_t vnodeProcessStreamChkptMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
3,140✔
1044
  vTrace("vgId:%d, msg:%p in stream chkpt queue is processing", pVnode->config.vgId, pMsg);
3,140✔
1045
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
3,140!
1046
       pMsg->msgType == TDMT_VND_BATCH_META) &&
3,140!
1047
      !syncIsReadyForRead(pVnode->sync)) {
×
1048
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1049
    return 0;
×
1050
  }
1051

1052
  switch (pMsg->msgType) {
3,140!
1053
    case TDMT_STREAM_CHKPT_EXEC:
3,140✔
1054
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
3,140✔
1055
    default:
×
1056
      vError("unknown msg type:%d in stream chkpt queue", pMsg->msgType);
×
1057
      return TSDB_CODE_APP_ERROR;
×
1058
  }
1059
}
1060
#endif
1061

1062
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1063
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1064
  if (code) {
×
1065
    vError("failed to process sma result since %s", tstrerror(code));
×
1066
  }
1067
}
×
1068

1069
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
149,833✔
1070
  if (NULL == pMetaRsp) {
149,833!
1071
    return;
×
1072
  }
1073

1074
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
149,833✔
1075
  pMetaRsp->dbId = pVnode->config.dbId;
149,833✔
1076
  pMetaRsp->vgId = TD_VID(pVnode);
149,833✔
1077
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
149,833✔
1078
}
1079

1080
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1081

1082
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
17✔
1083
  if (!pVnode->restored) {
17✔
1084
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
6!
1085
    return 0;
6✔
1086
  }
1087

1088
  int32_t     code = 0;
11✔
1089
  SVTrimDbReq trimReq = {0};
11✔
1090

1091
  // decode
1092
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
11!
1093
    code = TSDB_CODE_INVALID_MSG;
×
1094
    goto _exit;
×
1095
  }
1096

1097
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
11!
1098

1099
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
11✔
1100

1101
_exit:
11✔
1102
  return code;
11✔
1103
}
1104

1105
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1106

1107
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1108
  int32_t          code = 0;
×
1109
  SVS3MigrateDbReq s3migrateReq = {0};
×
1110

1111
  // decode
1112
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1113
    code = TSDB_CODE_INVALID_MSG;
×
1114
    goto _exit;
×
1115
  }
1116

1117
  vInfo("vgId:%d, process s3migrate vnode request, time:%d", pVnode->config.vgId, s3migrateReq.timestamp);
×
1118

1119
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1120

1121
_exit:
×
1122
  return code;
×
1123
}
1124

1125
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1126
  int               ret = 0;
×
1127
  SVDropTtlTableReq ttlReq = {0};
×
1128
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1129
    ret = TSDB_CODE_INVALID_MSG;
×
1130
    goto end;
×
1131
  }
1132

1133
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1134
    ret = TSDB_CODE_INVALID_MSG;
×
1135
    goto end;
×
1136
  }
1137

1138
  if (ttlReq.nUids != 0) {
×
1139
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1140
          ttlReq.timestampSec, ttlReq.nUids);
1141
  }
1142

1143
  if (ttlReq.nUids > 0) {
×
1144
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1145
    if (code) return code;
×
1146

1147
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1148
    if (code) {
×
1149
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1150
    }
1151
  }
1152

1153
end:
×
1154
  taosArrayDestroy(ttlReq.pTbUids);
×
1155
  return ret;
×
1156
}
1157

1158
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
30✔
1159
  int32_t                 code = -1;
30✔
1160
  SMetaReader             mr = {0};
30✔
1161
  SVDropTtlTableReq       ttlReq = {0};
30✔
1162
  SVFetchTtlExpiredTbsRsp rsp = {0};
30✔
1163
  SEncoder                encoder = {0};
30✔
1164
  SArray                 *pNames = NULL;
30✔
1165
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
30✔
1166
  pRsp->code = TSDB_CODE_SUCCESS;
30✔
1167
  pRsp->pCont = NULL;
30✔
1168
  pRsp->contLen = 0;
30✔
1169

1170
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
30!
1171
    terrno = TSDB_CODE_INVALID_MSG;
×
1172
    goto _end;
×
1173
  }
1174

1175
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
30!
1176
    terrno = TSDB_CODE_INVALID_MSG;
×
1177
    goto _end;
×
1178
  }
1179

1180
  tb_uid_t    suid;
1181
  char        ctbName[TSDB_TABLE_NAME_LEN];
1182
  SVDropTbReq expiredTb = {.igNotExists = true};
30✔
1183
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
30✔
1184
  rsp.vgId = TD_VID(pVnode);
30✔
1185
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
30✔
1186
  if (!rsp.pExpiredTbs) goto _end;
30!
1187

1188
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
30✔
1189
  if (!pNames) {
30!
1190
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1191
    goto _end;
×
1192
  }
1193
  char buf[TSDB_TABLE_NAME_LEN];
1194
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
151✔
1195
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
121✔
1196
    expiredTb.suid = *uid;
121✔
1197
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
121✔
1198
    if (terrno < 0) goto _end;
118!
1199
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
118✔
1200
    void *p = taosArrayPush(pNames, buf);
120✔
1201
    if (p == NULL) {
120!
1202
      goto _end;
×
1203
    }
1204

1205
    expiredTb.name = p;
120✔
1206
    if (mr.me.type == TSDB_CHILD_TABLE) {
120✔
1207
      expiredTb.suid = mr.me.ctbEntry.suid;
75✔
1208
    }
1209

1210
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
241!
1211
      goto _end;
×
1212
    }
1213
  }
1214

1215
  int32_t ret = 0;
30✔
1216
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
30!
1217
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
30✔
1218
  if (pRsp->pCont == NULL) {
30!
1219
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1220
    code = -1;
×
1221
    goto _end;
×
1222
  }
1223
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
30✔
1224
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
30✔
1225
  tEncoderClear(&encoder);
30✔
1226

1227
  if (terrno == 0) code = 0;
30!
1228
_end:
×
1229
  metaReaderClear(&mr);
30✔
1230
  tFreeFetchTtlExpiredTbsRsp(&rsp);
30✔
1231
  taosArrayDestroy(ttlReq.pTbUids);
30✔
1232
  if (pNames) taosArrayDestroy(pNames);
30!
1233
  pRsp->code = terrno;
30✔
1234
  return code;
30✔
1235
}
1236

1237
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
26,810✔
1238
  int32_t        code = 0;
26,810✔
1239
  SVCreateStbReq req = {0};
26,810✔
1240
  SDecoder       coder;
1241

1242
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
26,810✔
1243
  pRsp->code = TSDB_CODE_SUCCESS;
26,810✔
1244
  pRsp->pCont = NULL;
26,810✔
1245
  pRsp->contLen = 0;
26,810✔
1246

1247
  // decode and process req
1248
  tDecoderInit(&coder, pReq, len);
26,810✔
1249

1250
  code = tDecodeSVCreateStbReq(&coder, &req);
26,816✔
1251
  if (code) {
26,785!
1252
    pRsp->code = code;
×
1253
    goto _err;
×
1254
  }
1255

1256
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
26,785✔
1257
  if (code) {
26,903✔
1258
    pRsp->code = code;
3✔
1259
    goto _err;
3✔
1260
  }
1261

1262
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
26,900!
1263
    pRsp->code = code;
×
1264
    goto _err;
×
1265
  }
1266

1267
  tDecoderClear(&coder);
26,885✔
1268
  return 0;
26,858✔
1269

1270
_err:
3✔
1271
  tDecoderClear(&coder);
3✔
1272
  return code;
3✔
1273
}
1274

1275
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
74,713✔
1276
                                       SRpcMsg *pOriginRpc) {
1277
  SDecoder           decoder = {0};
74,713✔
1278
  SEncoder           encoder = {0};
74,713✔
1279
  int32_t            rcode = 0;
74,713✔
1280
  SVCreateTbBatchReq req = {0};
74,713✔
1281
  SVCreateTbReq     *pCreateReq;
1282
  SVCreateTbBatchRsp rsp = {0};
74,713✔
1283
  SVCreateTbRsp      cRsp = {0};
74,713✔
1284
  char               tbName[TSDB_TABLE_FNAME_LEN];
1285
  STbUidStore       *pStore = NULL;
74,713✔
1286
  SArray            *tbUids = NULL;
74,713✔
1287
  SArray            *tbNames = NULL;
74,713✔
1288
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
74,713✔
1289
  pRsp->code = TSDB_CODE_SUCCESS;
74,713✔
1290
  pRsp->pCont = NULL;
74,713✔
1291
  pRsp->contLen = 0;
74,713✔
1292

1293
  // decode
1294
  tDecoderInit(&decoder, pReq, len);
74,713✔
1295
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
74,716!
1296
    rcode = -1;
×
1297
    terrno = TSDB_CODE_INVALID_MSG;
×
1298
    goto _exit;
×
1299
  }
1300

1301
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
74,779✔
1302
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
74,787✔
1303
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
74,787✔
1304
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
74,791!
1305
    rcode = -1;
4✔
1306
    terrno = TSDB_CODE_OUT_OF_MEMORY;
4✔
1307
    goto _exit;
×
1308
  }
1309

1310
  // loop to create table
1311
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
177,176✔
1312
    pCreateReq = req.pReqs + iReq;
102,378✔
1313
    memset(&cRsp, 0, sizeof(cRsp));
102,378✔
1314

1315
    if (tsEnableAudit && tsEnableAuditCreateTable) {
102,378!
1316
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
102,379!
1317
      if (str == NULL) {
102,381!
1318
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1319
        rcode = -1;
×
1320
        goto _exit;
×
1321
      }
1322
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
102,381✔
1323
      if (taosArrayPush(tbNames, &str) == NULL) {
102,386!
1324
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1325
        rcode = -1;
×
1326
        goto _exit;
×
1327
      }
1328
    }
1329

1330
    // validate hash
1331
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
102,385✔
1332
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
102,379!
1333
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1334
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1335
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1336
        rcode = -1;
×
1337
        goto _exit;
×
1338
      }
1339
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1340
      continue;
×
1341
    }
1342

1343
    // do create table
1344
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
102,358✔
1345
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
747!
1346
        cRsp.code = TSDB_CODE_SUCCESS;
113✔
1347
      } else {
1348
        cRsp.code = terrno;
634✔
1349
      }
1350
    } else {
1351
      cRsp.code = TSDB_CODE_SUCCESS;
101,644✔
1352
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
101,644!
1353
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1354
      }
1355
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
203,287!
1356
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1357
        rcode = -1;
×
1358
        goto _exit;
×
1359
      }
1360
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
101,643✔
1361
    }
1362

1363
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
204,780!
1364
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1365
      rcode = -1;
×
1366
      goto _exit;
×
1367
    }
1368
  }
1369

1370
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
74,798✔
1371
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
74,798!
1372
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1373
  }
1374
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
74,792!
1375
    goto _exit;
×
1376
  }
1377
  pStore = tdUidStoreFree(pStore);
74,793✔
1378

1379
  // prepare rsp
1380
  int32_t ret = 0;
74,791✔
1381
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
74,791!
1382
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
74,792✔
1383
  if (pRsp->pCont == NULL) {
74,792!
1384
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1385
    rcode = -1;
×
1386
    goto _exit;
×
1387
  }
1388
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
74,792✔
1389
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
74,793!
1390
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1391
  }
1392

1393
  if (tsEnableAudit && tsEnableAuditCreateTable) {
74,789!
1394
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
74,787✔
1395

1396
    SName name = {0};
74,787✔
1397
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
74,787!
1398
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1399
    }
1400

1401
    SStringBuilder sb = {0};
74,792✔
1402
    for (int32_t i = 0; i < tbNames->size; i++) {
177,176✔
1403
      char **key = (char **)taosArrayGet(tbNames, i);
102,387✔
1404
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
102,384✔
1405
      if (i < tbNames->size - 1) {
102,386✔
1406
        taosStringBuilderAppendChar(&sb, ',');
27,598✔
1407
      }
1408
      // taosMemoryFreeClear(*key);
1409
    }
1410

1411
    size_t len = 0;
74,789✔
1412
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
74,789✔
1413

1414
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
74,791!
1415
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
62,764✔
1416
    }
1417

1418
    taosStringBuilderDestroy(&sb);
74,790✔
1419
  }
1420

1421
_exit:
2✔
1422
  tDeleteSVCreateTbBatchReq(&req);
74,790✔
1423
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
74,790✔
1424
  taosArrayDestroy(tbUids);
74,794✔
1425
  tDecoderClear(&decoder);
74,794✔
1426
  tEncoderClear(&encoder);
74,792✔
1427
  taosArrayDestroyP(tbNames, NULL);
74,794✔
1428
  return rcode;
74,794✔
1429
}
1430

1431
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
10,905✔
1432
  int32_t        code = 0;
10,905✔
1433
  SVCreateStbReq req = {0};
10,905✔
1434
  SDecoder       dc = {0};
10,905✔
1435

1436
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
10,905✔
1437
  pRsp->code = TSDB_CODE_SUCCESS;
10,905✔
1438
  pRsp->pCont = NULL;
10,905✔
1439
  pRsp->contLen = 0;
10,905✔
1440

1441
  tDecoderInit(&dc, pReq, len);
10,905✔
1442

1443
  // decode req
1444
  code = tDecodeSVCreateStbReq(&dc, &req);
10,905✔
1445
  if (code) {
10,911!
1446
    tDecoderClear(&dc);
×
1447
    return code;
×
1448
  }
1449

1450
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
10,911✔
1451
  if (code) {
10,919!
1452
    pRsp->code = code;
×
1453
    tDecoderClear(&dc);
×
1454
    return code;
×
1455
  }
1456

1457
  tDecoderClear(&dc);
10,919✔
1458

1459
  return 0;
10,919✔
1460
}
1461

1462
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,851✔
1463
  SVDropStbReq req = {0};
1,851✔
1464
  int32_t      rcode = TSDB_CODE_SUCCESS;
1,851✔
1465
  SDecoder     decoder = {0};
1,851✔
1466
  SArray      *tbUidList = NULL;
1,851✔
1467

1468
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
1,851✔
1469
  pRsp->pCont = NULL;
1,851✔
1470
  pRsp->contLen = 0;
1,851✔
1471

1472
  // decode request
1473
  tDecoderInit(&decoder, pReq, len);
1,851✔
1474
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
1,852!
1475
    rcode = TSDB_CODE_INVALID_MSG;
×
1476
    goto _exit;
×
1477
  }
1478

1479
  // process request
1480
  tbUidList = taosArrayInit(8, sizeof(int64_t));
1,856✔
1481
  if (tbUidList == NULL) goto _exit;
1,860!
1482
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
1,860✔
1483
    rcode = terrno;
2✔
1484
    goto _exit;
2✔
1485
  }
1486

1487
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
1,858!
1488
    rcode = terrno;
×
1489
    goto _exit;
×
1490
  }
1491

1492
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
1,858!
1493
    rcode = terrno;
×
1494
    goto _exit;
×
1495
  }
1496

1497
  // return rsp
1498
_exit:
1,858✔
1499
  if (tbUidList) taosArrayDestroy(tbUidList);
1,860✔
1500
  pRsp->code = rcode;
1,861✔
1501
  tDecoderClear(&decoder);
1,861✔
1502
  return 0;
1,860✔
1503
}
1504

1505
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,491✔
1506
  SVAlterTbReq  vAlterTbReq = {0};
5,491✔
1507
  SVAlterTbRsp  vAlterTbRsp = {0};
5,491✔
1508
  SDecoder      dc = {0};
5,491✔
1509
  int32_t       code = 0;
5,491✔
1510
  int32_t       lino = 0;
5,491✔
1511
  int32_t       ret;
1512
  SEncoder      ec = {0};
5,491✔
1513
  STableMetaRsp vMetaRsp = {0};
5,491✔
1514

1515
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
5,491✔
1516
  pRsp->pCont = NULL;
5,491✔
1517
  pRsp->contLen = 0;
5,491✔
1518
  pRsp->code = TSDB_CODE_SUCCESS;
5,491✔
1519

1520
  tDecoderInit(&dc, pReq, len);
5,491✔
1521

1522
  // decode
1523
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
5,491!
1524
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1525
    tDecoderClear(&dc);
×
1526
    goto _exit;
×
1527
  }
1528

1529
  // process
1530
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
5,491✔
1531
    vAlterTbRsp.code = terrno;
919✔
1532
    tDecoderClear(&dc);
919✔
1533
    goto _exit;
919✔
1534
  }
1535
  tDecoderClear(&dc);
4,572✔
1536

1537
  if (NULL != vMetaRsp.pSchemas) {
4,572✔
1538
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
1,280✔
1539
    vAlterTbRsp.pMeta = &vMetaRsp;
1,280✔
1540
  }
1541

1542
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
4,572✔
1543
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
3,173✔
1544
    if (uid == 0) {
3,173!
1545
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1546
             vAlterTbReq.tbName);
1547
      goto _exit;
×
1548
    }
1549

1550
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
3,173✔
1551
    void* p = taosArrayPush(tbUids, &uid);
3,173✔
1552
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
3,173!
1553

1554
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,173✔
1555
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
3,173!
1556
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1557
    }
1558

1559
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,173✔
1560
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
3,173!
1561
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1562
    }
1563

1564
    taosArrayDestroy(tbUids);
3,173✔
1565
  }
1566

1567
_exit:
1,399✔
1568
  taosArrayDestroy(vAlterTbReq.pMultiTag);
5,491✔
1569
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
5,491!
1570
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
5,491✔
1571
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
5,491✔
1572
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
5,491!
1573
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1574
  }
1575

1576
  tEncoderClear(&ec);
5,491✔
1577
  if (vMetaRsp.pSchemas) {
5,491✔
1578
    taosMemoryFree(vMetaRsp.pSchemas);
1,280!
1579
    taosMemoryFree(vMetaRsp.pSchemaExt);
1,280!
1580
  }
1581
  if (vMetaRsp.pColRefs) {
5,491✔
1582
    taosMemoryFree(vMetaRsp.pColRefs);
6!
1583
  }
1584
  return 0;
5,491✔
1585
}
1586

1587
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
15,541✔
1588
                                     SRpcMsg *pOriginRpc) {
1589
  SVDropTbBatchReq req = {0};
15,541✔
1590
  SVDropTbBatchRsp rsp = {0};
15,541✔
1591
  SDecoder         decoder = {0};
15,541✔
1592
  SEncoder         encoder = {0};
15,541✔
1593
  int32_t          ret;
1594
  SArray          *tbUids = NULL;
15,541✔
1595
  STbUidStore     *pStore = NULL;
15,541✔
1596
  SArray          *tbNames = NULL;
15,541✔
1597

1598
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
15,541✔
1599
  pRsp->pCont = NULL;
15,541✔
1600
  pRsp->contLen = 0;
15,541✔
1601
  pRsp->code = TSDB_CODE_SUCCESS;
15,541✔
1602

1603
  // decode req
1604
  tDecoderInit(&decoder, pReq, len);
15,541✔
1605
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
15,541✔
1606
  if (ret < 0) {
15,541!
1607
    terrno = TSDB_CODE_INVALID_MSG;
×
1608
    pRsp->code = terrno;
×
1609
    goto _exit;
×
1610
  }
1611

1612
  // process req
1613
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
15,541✔
1614
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
15,541✔
1615
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
15,541✔
1616
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
15,541!
1617

1618
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,102✔
1619
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
15,561✔
1620
    SVDropTbRsp  dropTbRsp = {0};
15,561✔
1621
    tb_uid_t     tbUid = 0;
15,561✔
1622

1623
    /* code */
1624
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
15,561✔
1625
    if (ret < 0) {
15,561!
1626
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1627
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1628
      } else {
1629
        dropTbRsp.code = terrno;
×
1630
      }
1631
    } else {
1632
      dropTbRsp.code = TSDB_CODE_SUCCESS;
15,561✔
1633
      if (tbUid > 0) {
15,561!
1634
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1635
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1636
        }
1637
      }
1638
    }
1639

1640
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
31,122!
1641
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1642
      pRsp->code = terrno;
×
1643
      goto _exit;
×
1644
    }
1645

1646
    if (tsEnableAuditCreateTable) {
15,561!
1647
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
15,561!
1648
      if (str == NULL) {
15,561!
1649
        pRsp->code = terrno;
×
1650
        goto _exit;
×
1651
      }
1652
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
15,561✔
1653
      if (taosArrayPush(tbNames, &str) == NULL) {
15,561!
1654
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1655
        pRsp->code = terrno;
×
1656
        goto _exit;
×
1657
      }
1658
    }
1659
  }
1660

1661
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
15,541!
1662
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1663
  }
1664

1665
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
15,541!
1666
    goto _exit;
×
1667
  }
1668

1669
  if (tsEnableAuditCreateTable) {
15,541!
1670
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
15,541✔
1671

1672
    SName name = {0};
15,541✔
1673
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
15,541!
1674
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1675
    }
1676

1677
    SStringBuilder sb = {0};
15,541✔
1678
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,102✔
1679
      char **key = (char **)taosArrayGet(tbNames, iReq);
15,561✔
1680
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
15,561✔
1681
      if (iReq < req.nReqs - 1) {
15,561✔
1682
        taosStringBuilderAppendChar(&sb, ',');
20✔
1683
      }
1684
      taosMemoryFreeClear(*key);
15,561!
1685
    }
1686

1687
    size_t len = 0;
15,541✔
1688
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,541✔
1689

1690
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
15,541!
1691
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
15,499✔
1692
    }
1693

1694
    taosStringBuilderDestroy(&sb);
15,541✔
1695
  }
1696

1697
_exit:
×
1698
  taosArrayDestroy(tbUids);
15,541✔
1699
  pStore = tdUidStoreFree(pStore);
15,541✔
1700
  tDecoderClear(&decoder);
15,541✔
1701
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
15,541!
1702
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
15,541✔
1703
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
15,541✔
1704
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
15,541!
1705
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1706
  }
1707
  tEncoderClear(&encoder);
15,541✔
1708
  taosArrayDestroy(rsp.pArray);
15,541✔
1709
  taosArrayDestroy(tbNames);
15,541✔
1710
  return 0;
15,541✔
1711
}
1712

1713
#ifdef BUILD_NO_CALL
1714
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1715
                                              const char *tags) {
1716
  SSubmitBlkIter blkIter = {0};
1717
  STSchema      *pSchema = NULL;
1718
  tb_uid_t       suid = 0;
1719
  STSRow        *row = NULL;
1720
  int32_t        rv = -1;
1721

1722
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1723
  if (blkIter.row == NULL) return 0;
1724

1725
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1726
                                         &pSchema);  // TODO: use the real schema
1727
  if (TSDB_CODE_SUCCESS != code) {
1728
    printf("%s:%d no valid schema\n", tags, __LINE__);
1729
    return code;
1730
  }
1731

1732
  suid = msgIter->suid;
1733
  rv = TD_ROW_SVER(blkIter.row);
1734

1735
  char __tags[128] = {0};
1736
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1737
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1738
    tdSRowPrint(row, pSchema, __tags);
1739
  }
1740

1741
  taosMemoryFreeClear(pSchema);
1742

1743
  return TSDB_CODE_SUCCESS;
1744
}
1745
#endif
1746
typedef struct SSubmitReqConvertCxt {
1747
  SSubmitMsgIter msgIter;
1748
  SSubmitBlk    *pBlock;
1749
  SSubmitBlkIter blkIter;
1750
  STSRow        *pRow;
1751
  STSRowIter     rowIter;
1752
  SSubmitTbData *pTbData;
1753
  STSchema      *pTbSchema;
1754
  SArray        *pColValues;
1755
} SSubmitReqConvertCxt;
1756

1757
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1758
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1759
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1760
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1761
  if (TSDB_CODE_SUCCESS != code) {
×
1762
    return code;
×
1763
  }
1764
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1765

1766
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1767
  if (NULL == pCxt->pTbData) {
×
1768
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1769
    if (NULL == pCxt->pTbData) {
×
1770
      return terrno;
×
1771
    }
1772
  }
1773
  pCxt->pTbData->flags = 0;
×
1774
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1775
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1776
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1777
  pCxt->pTbData->pCreateTbReq = NULL;
×
1778
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1779
  if (NULL == pCxt->pTbData->aRowP) {
×
1780
    return terrno;
×
1781
  }
1782

1783
  taosArrayDestroy(pCxt->pColValues);
×
1784
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1785
  if (NULL == pCxt->pColValues) {
×
1786
    return terrno;
×
1787
  }
1788
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1789
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1790
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1791
      return terrno;
×
1792
    }
1793
  }
1794

1795
  return TSDB_CODE_SUCCESS;
×
1796
}
1797

1798
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1799
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1800
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1801
  taosMemoryFreeClear(pCxt->pTbData);
×
1802
  taosArrayDestroy(pCxt->pColValues);
×
1803
}
×
1804

1805
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1806
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1807
    pColVal->flag = CV_FLAG_NONE;
×
1808
    return TSDB_CODE_SUCCESS;
×
1809
  }
1810

1811
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1812
    pColVal->flag = CV_FLAG_NULL;
×
1813
    return TSDB_CODE_SUCCESS;
×
1814
  }
1815

1816
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1817
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1818
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1819
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1820
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1821
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1822
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1823
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1824
  } else {
1825
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1826
  }
1827

1828
  pColVal->flag = CV_FLAG_VALUE;
×
1829
  return TSDB_CODE_SUCCESS;
×
1830
}
1831

1832
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1833
  int32_t code = TSDB_CODE_SUCCESS;
×
1834
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1835
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1836
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1837
    SCellVal  cellVal = {0};
×
1838
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1839
      break;
×
1840
    }
1841
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1842
  }
1843
  return code;
×
1844
}
1845

1846
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1847
  if (pCxt->msgIter.schemaLen <= 0) {
×
1848
    return TSDB_CODE_SUCCESS;
×
1849
  }
1850

1851
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1852
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1853
    return terrno;
×
1854
  }
1855

1856
  SDecoder decoder = {0};
×
1857
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1858
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1859
  tDecoderClear(&decoder);
×
1860

1861
  return code;
×
1862
}
1863

1864
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1865
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1866
  if (NULL == pReq2->aSubmitTbData) {
×
1867
    return terrno;
×
1868
  }
1869

1870
  SSubmitReqConvertCxt cxt = {0};
×
1871

1872
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1873
  while (TSDB_CODE_SUCCESS == code) {
×
1874
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1875
    if (TSDB_CODE_SUCCESS == code) {
×
1876
      if (NULL == cxt.pBlock) {
×
1877
        break;
×
1878
      }
1879
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1880
    }
1881
    if (TSDB_CODE_SUCCESS == code) {
×
1882
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1883
    }
1884
    if (TSDB_CODE_SUCCESS == code) {
×
1885
      code = vnodeDecodeCreateTbReq(&cxt);
×
1886
    }
1887
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1888
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1889
      if (TSDB_CODE_SUCCESS == code) {
×
1890
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1891
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1892
      }
1893
    }
1894
    if (TSDB_CODE_SUCCESS == code) {
×
1895
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1896
    }
1897
    if (TSDB_CODE_SUCCESS == code) {
×
1898
      taosMemoryFreeClear(cxt.pTbData);
×
1899
    }
1900
  }
1901

1902
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1903
  return code;
×
1904
}
1905

1906
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1907
  int32_t  code = TSDB_CODE_SUCCESS;
×
1908
  char    *pMsg = NULL;
×
1909
  uint32_t msglen = 0;
×
1910
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1911
  if (TSDB_CODE_SUCCESS == code) {
×
1912
    pMsg = taosMemoryMalloc(msglen);
×
1913
    if (NULL == pMsg) {
×
1914
      code = terrno;
×
1915
    }
1916
  }
1917
  if (TSDB_CODE_SUCCESS == code) {
×
1918
    SEncoder encoder;
1919
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1920
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1921
    tEncoderClear(&encoder);
×
1922
  }
1923
  if (TSDB_CODE_SUCCESS == code) {
×
1924
    *ppMsg = pMsg;
×
1925
  }
1926
  return code;
×
1927
}
1928

1929
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,795,584✔
1930
                                     SRpcMsg *pOriginalMsg) {
1931
  int32_t code = 0;
2,795,584✔
1932
  terrno = 0;
2,795,584✔
1933

1934
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
2,795,822✔
1935
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
2,795,822✔
1936
  SArray      *newTbUids = NULL;
2,795,822✔
1937
  int32_t      ret;
1938
  SEncoder     ec = {0};
2,795,822✔
1939

1940
  pRsp->code = TSDB_CODE_SUCCESS;
2,795,822✔
1941

1942
  void           *pAllocMsg = NULL;
2,795,822✔
1943
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
2,795,822✔
1944
  if (0 == pMsg->version) {
2,795,822!
1945
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1946
    if (TSDB_CODE_SUCCESS == code) {
×
1947
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1948
    }
1949
    if (TSDB_CODE_SUCCESS == code) {
×
1950
      pAllocMsg = pReq;
×
1951
    }
1952
    if (TSDB_CODE_SUCCESS != code) {
×
1953
      goto _exit;
×
1954
    }
1955
  } else {
1956
    // decode
1957
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
2,795,822✔
1958
    len -= sizeof(SSubmitReq2Msg);
2,795,822✔
1959
    SDecoder dc = {0};
2,795,822✔
1960
    tDecoderInit(&dc, pReq, len);
2,795,822✔
1961
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
2,795,715!
1962
      code = TSDB_CODE_INVALID_MSG;
×
1963
      goto _exit;
×
1964
    }
1965
    tDecoderClear(&dc);
2,795,808✔
1966
  }
1967

1968
  // scan
1969
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
2,795,775✔
1970
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
2,795,857✔
1971
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
2,795,857✔
1972
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
6,285,744✔
1973
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
3,491,792✔
1974

1975
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
3,491,859!
1976
      code = TSDB_CODE_INVALID_MSG;
×
1977
      goto _exit;
×
1978
    }
1979

1980
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
3,491,859✔
1981
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
13,879!
1982
        code = TSDB_CODE_INVALID_MSG;
×
1983
        goto _exit;
×
1984
      }
1985

1986
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
13,879✔
1987
      SRowKey   lastKey;
1988
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
13,879✔
1989
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
21,581,186✔
1990
        SRowKey key;
1991
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
21,567,990✔
1992
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
21,564,118!
1993
          code = TSDB_CODE_INVALID_MSG;
×
1994
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1995
          goto _exit;
×
1996
        }
1997
      }
1998
    } else {
1999
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
3,477,980✔
2000
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
3,477,980✔
2001
      SRowKey lastRowKey;
2002
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
137,988,792✔
2003
#ifndef NO_UNALIGNED_ACCESS
2004
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
134,512,101!
2005
#else
2006
        TSKEY ts = taosGetInt64Aligned(&(aRow[iRow]->ts));
2007
        if (ts < minKey || ts > maxKey) {
2008
#endif
2009
          code = TSDB_CODE_INVALID_MSG;
31,931✔
2010
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
31,931!
2011
          goto _exit;
×
2012
        }
2013
        if (iRow == 0) {
134,480,170✔
2014
          tRowGetKey(aRow[iRow], &lastRowKey);
6,955,894✔
2015
        } else {
2016
          SRowKey rowKey;
2017
          tRowGetKey(aRow[iRow], &rowKey);
262,004,446✔
2018

2019
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
131,002,223!
2020
            code = TSDB_CODE_INVALID_MSG;
×
2021
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
2022
            goto _exit;
×
2023
          }
2024
          lastRowKey = rowKey;
131,024,343✔
2025
        }
2026
      }
2027
    }
2028
  }
2029

2030
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
6,285,917✔
2031
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
3,491,840✔
2032

2033
    if (pSubmitTbData->pCreateTbReq) {
3,491,921✔
2034
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
50,059✔
2035
    } else {
2036
      SMetaInfo info = {0};
3,441,862✔
2037

2038
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
3,441,862✔
2039
      if (code) {
3,441,883✔
2040
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
31✔
2041
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
31!
2042
        goto _exit;
×
2043
      }
2044

2045
      if (info.suid != pSubmitTbData->suid) {
3,441,852!
2046
        code = TSDB_CODE_INVALID_MSG;
×
2047
        goto _exit;
×
2048
      }
2049

2050
      if (info.suid) {
3,441,852✔
2051
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
2,458,084!
2052
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2053
        }
2054
      }
2055

2056
      if (pSubmitTbData->sver != info.skmVer) {
3,441,906!
2057
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
2058
        goto _exit;
×
2059
      }
2060
    }
2061

2062
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
3,491,965✔
2063
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
13,878✔
2064
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
13,878✔
2065

2066
      if (nColData <= 0) {
13,878!
2067
        code = TSDB_CODE_INVALID_MSG;
×
2068
        goto _exit;
×
2069
      }
2070

2071
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
13,878!
2072
          aColData[0].nVal <= 0) {
13,878!
2073
        code = TSDB_CODE_INVALID_MSG;
×
2074
        goto _exit;
×
2075
      }
2076

2077
      for (int32_t j = 1; j < nColData; j++) {
41,987✔
2078
        if (aColData[j].nVal != aColData[0].nVal) {
28,109!
2079
          code = TSDB_CODE_INVALID_MSG;
×
2080
          goto _exit;
×
2081
        }
2082
      }
2083
    }
2084
  }
2085

2086
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
2,794,077!
2087
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2088

2089
  // loop to handle
2090
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
6,287,643✔
2091
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
3,491,775✔
2092

2093
    // create table
2094
    if (pSubmitTbData->pCreateTbReq) {
3,491,860✔
2095
      // alloc if need
2096
      if (pSubmitRsp->aCreateTbRsp == NULL &&
50,055✔
2097
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
49,170!
2098
              NULL) {
2099
        code = terrno;
×
2100
        goto _exit;
8✔
2101
      }
2102

2103
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
50,056✔
2104

2105
      // create table
2106
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
50,056✔
2107
        // create table success
2108

2109
        if (newTbUids == NULL &&
92,987!
2110
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
46,078✔
2111
          code = terrno;
×
2112
          goto _exit;
×
2113
        }
2114

2115
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
93,818!
2116
          code = terrno;
×
2117
          goto _exit;
×
2118
        }
2119

2120
        if (pCreateTbRsp->pMeta) {
46,909!
2121
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
46,909✔
2122
        }
2123
      } else {  // create table failed
2124
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
3,147✔
2125
          code = terrno;
8✔
2126
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2127
                 tstrerror(terrno));
2128
          goto _exit;
8✔
2129
        }
2130
        terrno = 0;
3,139✔
2131
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
3,139✔
2132
      }
2133
    }
2134

2135
    // insert data
2136
    int32_t affectedRows;
2137
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
3,491,853✔
2138
    if (code) goto _exit;
3,491,835!
2139

2140
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
3,491,835✔
2141
    if (code) goto _exit;
3,491,902!
2142

2143
    pSubmitRsp->affectedRows += affectedRows;
3,491,902✔
2144
  }
2145

2146
  // update the affected table uid list
2147
  if (taosArrayGetSize(newTbUids) > 0) {
2,795,868✔
2148
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
46,076✔
2149
           (int32_t)taosArrayGetSize(newTbUids));
2150
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
46,076!
2151
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2152
    }
2153
  }
2154

2155
_exit:
2,795,832✔
2156
  // message
2157
  pRsp->code = code;
2,795,840✔
2158
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
2,795,840!
2159
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,795,735✔
2160
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,795,807✔
2161
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
2,795,790!
2162
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2163
  }
2164
  tEncoderClear(&ec);
2,795,838✔
2165

2166
  // update statistics
2167
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
2,795,864✔
2168
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
2,795,886✔
2169
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
2,795,884✔
2170

2171
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
2,795,898!
2172
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
100!
2173
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
100✔
2174
                                   pVnode->monitor.strClusterId,
100✔
2175
                                   pVnode->monitor.strDnodeId,
100✔
2176
                                   tsLocalEp,
2177
                                   pVnode->monitor.strVgId,
100✔
2178
                                   pOriginalMsg->info.conn.user,
100✔
2179
                                   "Success"};
2180
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
100✔
2181
  }
2182

2183
  if (code == 0) {
2,795,898✔
2184
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2,795,853✔
2185
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2,795,890✔
2186
  }
2187
  /*
2188
  if (code == 0) {
2189
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2190
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2191

2192
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2193
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2194
                                          pOriginalMsg->info.conn.user, "Success"};
2195
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2196
  }
2197
  else{
2198
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2199
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2200
                                        pOriginalMsg->info.conn.user, "Failed"};
2201
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2202
  }
2203
  */
2204

2205
  // clear
2206
  taosArrayDestroy(newTbUids);
2,795,900✔
2207
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
2,795,745!
2208
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
2,795,833✔
2209

2210
  if (code) terrno = code;
2,795,656✔
2211

2212
  taosMemoryFree(pAllocMsg);
2,795,656!
2213

2214
  return code;
2,795,793✔
2215
}
2216

2217
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2218
#ifdef USE_TSMA
2219
  SVCreateTSmaReq req = {0};
×
2220
  SDecoder        coder = {0};
×
2221

2222
  if (pRsp) {
×
2223
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
×
2224
    pRsp->code = TSDB_CODE_SUCCESS;
×
2225
    pRsp->pCont = NULL;
×
2226
    pRsp->contLen = 0;
×
2227
  }
2228

2229
  // decode and process req
2230
  tDecoderInit(&coder, pReq, len);
×
2231

2232
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
×
2233
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2234
    if (pRsp) pRsp->code = terrno;
×
2235
    goto _err;
×
2236
  }
2237

2238
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
×
2239
    if (pRsp) pRsp->code = terrno;
×
2240
    goto _err;
×
2241
  }
2242

2243
  tDecoderClear(&coder);
×
2244
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
×
2245
         req.indexName, req.indexUid, ver, req.tableUid);
2246
  return 0;
×
2247

2248
_err:
×
2249
  tDecoderClear(&coder);
×
2250
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2251
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2252
  return terrno;
×
2253
#else
2254
  return TSDB_CODE_INTERNAL_ERROR;
2255
#endif
2256
}
2257

2258
/**
2259
 * @brief specific for smaDstVnode
2260
 *
2261
 * @param pVnode
2262
 * @param pCont
2263
 * @param contLen
2264
 * @return int32_t
2265
 */
2266
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2267
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2268
}
2269

2270
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
30✔
2271
  int32_t code = TSDB_CODE_SUCCESS;
30✔
2272

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

2276
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2277
  code = metaTrimTables(pVnode->pMeta, ver);
30✔
2278

2279
  return code;
30✔
2280
}
2281

2282
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,530✔
2283
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
11,530!
2284
  int32_t code = TSDB_CODE_SUCCESS;
11,530✔
2285
  if (!pVnode->config.hashChange) {
11,530✔
2286
    goto _exit;
11,500✔
2287
  }
2288

2289
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
30✔
2290
  if (code < 0) {
30!
2291
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2292
    goto _exit;
×
2293
  }
2294
  pVnode->config.hashChange = false;
30✔
2295

2296
_exit:
11,530✔
2297
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
11,530✔
2298
  pRsp->code = code;
11,530✔
2299
  pRsp->pCont = NULL;
11,530✔
2300
  pRsp->contLen = 0;
11,530✔
2301

2302
  return code;
11,530✔
2303
}
2304

2305
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2306
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2307

2308
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
586✔
2309
  bool walChanged = false;
586✔
2310
  bool tsdbChanged = false;
586✔
2311

2312
  SAlterVnodeConfigReq req = {0};
586✔
2313
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
586!
2314
    terrno = TSDB_CODE_INVALID_MSG;
×
2315
    return TSDB_CODE_INVALID_MSG;
×
2316
  }
2317

2318
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
586!
2319
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2320
        "s3Compact:%d fsync:%d level:%d "
2321
        "walRetentionPeriod:%d walRetentionSize:%d",
2322
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2323
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2324
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2325
        req.walRetentionSize);
2326

2327
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
586✔
2328
    pVnode->config.cacheLastSize = req.cacheLastSize;
119✔
2329
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
119✔
2330
  }
2331

2332
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
586!
2333
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2334
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2335
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2336
  }
2337

2338
  if (pVnode->config.szCache != req.pages) {
586!
2339
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
×
2340
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2341
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2342
      return terrno;
×
2343
    } else {
2344
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2345
      pVnode->config.szCache = req.pages;
×
2346
    }
2347
  }
2348

2349
  if (pVnode->config.cacheLast != req.cacheLast) {
586✔
2350
    pVnode->config.cacheLast = req.cacheLast;
312✔
2351
  }
2352

2353
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
586✔
2354
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
54✔
2355
    walChanged = true;
54✔
2356
  }
2357

2358
  if (pVnode->config.walCfg.level != req.walLevel) {
586✔
2359
    if (pVnode->config.walCfg.level == 0) {
36!
2360
      pVnode->config.walCfg.clearFiles = 1;
×
2361
    }
2362
    pVnode->config.walCfg.level = req.walLevel;
36✔
2363
    walChanged = true;
36✔
2364
  }
2365

2366
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
586✔
2367
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2368
    walChanged = true;
132✔
2369
  }
2370

2371
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
586!
2372
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2373
    walChanged = true;
×
2374
  }
2375

2376
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
586✔
2377
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
46✔
2378
    if (!VND_IS_RSMA(pVnode)) {
46!
2379
      tsdbChanged = true;
46✔
2380
    }
2381
  }
2382

2383
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
586✔
2384
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
46✔
2385
    if (!VND_IS_RSMA(pVnode)) {
46!
2386
      tsdbChanged = true;
46✔
2387
    }
2388
  }
2389

2390
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
586✔
2391
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
48✔
2392
    if (!VND_IS_RSMA(pVnode)) {
48!
2393
      tsdbChanged = true;
48✔
2394
    }
2395
  }
2396

2397
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
586✔
2398
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
2✔
2399
    if (!VND_IS_RSMA(pVnode)) {
2!
2400
      tsdbChanged = true;
2✔
2401
    }
2402
  }
2403

2404
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
586!
2405
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2406
      pVnode->config.sttTrigger = req.sttTrigger;
×
2407
    } else {
2408
      vnodeAWait(&pVnode->commitTask);
×
2409

2410
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2411
      if (ret != 0) {
×
2412
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2413
      }
2414

2415
      pVnode->config.sttTrigger = req.sttTrigger;
×
2416
      tsdbEnableBgTask(pVnode->pTsdb);
×
2417
    }
2418
  }
2419

2420
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
586!
2421
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2422
  }
2423

2424
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
586!
2425
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
×
2426
  }
2427
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
586!
2428
    pVnode->config.s3Compact = req.s3Compact;
×
2429
  }
2430

2431
  if (walChanged) {
586✔
2432
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
222!
2433
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2434
    }
2435
  }
2436

2437
  if (tsdbChanged) {
586✔
2438
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
50✔
2439
  }
2440

2441
  return 0;
586✔
2442
}
2443

2444
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
930✔
2445
  SBatchDeleteReq deleteReq;
2446
  SDecoder        decoder;
2447
  tDecoderInit(&decoder, pReq, len);
930✔
2448
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
930!
2449
    tDecoderClear(&decoder);
×
2450
    return terrno = TSDB_CODE_INVALID_MSG;
×
2451
  }
2452

2453
  SMetaReader mr = {0};
930✔
2454
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
930✔
2455
  STsdb *pTsdb = pVnode->pTsdb;
930✔
2456

2457
  if (deleteReq.level) {
930✔
2458
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
16✔
2459
  }
2460

2461
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
930✔
2462
  for (int32_t i = 0; i < sz; i++) {
2,675✔
2463
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
1,745✔
2464
    char             *name = pOneReq->tbname;
1,745✔
2465
    if (metaGetTableEntryByName(&mr, name) < 0) {
1,745✔
2466
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
614✔
2467
      continue;
614✔
2468
    }
2469

2470
    int64_t uid = mr.me.uid;
1,131✔
2471

2472
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
1,131✔
2473
    if (code < 0) {
1,131!
2474
      terrno = code;
×
2475
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2476
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2477
    }
2478

2479
    if (deleteReq.level == 0) {
1,131✔
2480
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
515✔
2481
      if (code < 0) {
515!
2482
        terrno = code;
×
2483
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2484
               ", end ts:%" PRId64,
2485
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2486
      }
2487
    }
2488
    tDecoderClear(&mr.coder);
1,131✔
2489
  }
2490
  metaReaderClear(&mr);
930✔
2491
  taosArrayDestroy(deleteReq.deleteReqs);
930✔
2492
  return 0;
930✔
2493
}
2494

2495
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
8,347✔
2496
                                     SRpcMsg *pOriginalMsg) {
2497
  int32_t     code = 0;
8,347✔
2498
  SDecoder   *pCoder = &(SDecoder){0};
8,347✔
2499
  SDeleteRes *pRes = &(SDeleteRes){0};
8,347✔
2500

2501
  pRsp->msgType = TDMT_VND_DELETE_RSP;
8,347✔
2502
  pRsp->pCont = NULL;
8,347✔
2503
  pRsp->contLen = 0;
8,347✔
2504
  pRsp->code = TSDB_CODE_SUCCESS;
8,347✔
2505

2506
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
8,347✔
2507
  if (pRes->uidList == NULL) {
8,347!
2508
    code = terrno;
×
2509
    goto _err;
×
2510
  }
2511

2512
  tDecoderInit(pCoder, pReq, len);
8,347✔
2513
  code = tDecodeDeleteRes(pCoder, pRes);
8,347✔
2514
  if (code) goto _err;
8,347!
2515

2516
  if (pRes->affectedRows > 0) {
8,347✔
2517
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
14,444✔
2518
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
7,717✔
2519
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
7,717✔
2520
      if (code) goto _err;
7,717!
2521
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
7,717✔
2522
      if (code) goto _err;
7,717!
2523
    }
2524
  }
2525

2526
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
8,347✔
2527

2528
  tDecoderClear(pCoder);
8,347✔
2529
  taosArrayDestroy(pRes->uidList);
8,347✔
2530

2531
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
8,347✔
2532
  int32_t     ret = 0;
8,347✔
2533
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
8,347!
2534
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
8,347✔
2535
  SEncoder ec = {0};
8,347✔
2536
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
8,347✔
2537
  code = tEncodeSVDeleteRsp(&ec, &rsp);
8,347✔
2538
  if (code) goto _err;
8,347!
2539
  tEncoderClear(&ec);
8,347✔
2540
  return code;
8,347✔
2541

2542
_err:
×
2543
  /*
2544
  if(code == TSDB_CODE_SUCCESS){
2545
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2546
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2547
                                        pOriginalMsg->info.conn.user, "Success"};
2548
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2549
  }
2550
  else{
2551
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2552
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2553
                                        pOriginalMsg->info.conn.user, "Failed"};
2554
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2555
  }
2556
  */
2557

2558
  return code;
×
2559
}
2560
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
17✔
2561
  SVCreateStbReq req = {0};
17✔
2562
  SDecoder       dc = {0};
17✔
2563
  int32_t        code = 0;
17✔
2564

2565
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
17✔
2566
  pRsp->code = TSDB_CODE_SUCCESS;
17✔
2567
  pRsp->pCont = NULL;
17✔
2568
  pRsp->contLen = 0;
17✔
2569

2570
  tDecoderInit(&dc, pReq, len);
17✔
2571
  // decode req
2572
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
17!
2573
    tDecoderClear(&dc);
×
2574
    return terrno = TSDB_CODE_INVALID_MSG;
×
2575
  }
2576

2577
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
17✔
2578
  if (code) {
17!
2579
    pRsp->code = code;
×
2580
    goto _err;
×
2581
  }
2582
  tDecoderClear(&dc);
17✔
2583
  return 0;
17✔
2584

2585
_err:
×
2586
  tDecoderClear(&dc);
×
2587
  return code;
×
2588
}
2589
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
14✔
2590
  SDropIndexReq req = {0};
14✔
2591
  int32_t       code = 0;
14✔
2592
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
14✔
2593
  pRsp->code = TSDB_CODE_SUCCESS;
14✔
2594
  pRsp->pCont = NULL;
14✔
2595
  pRsp->contLen = 0;
14✔
2596

2597
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
14!
2598
    pRsp->code = code;
×
2599
    return code;
×
2600
  }
2601

2602
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
14✔
2603
  if (code) {
14!
2604
    pRsp->code = code;
×
2605
    return code;
×
2606
  }
2607
  return TSDB_CODE_SUCCESS;
14✔
2608
}
2609

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

2612
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
194✔
2613
  if (!pVnode->restored) {
194✔
2614
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
174!
2615
    return 0;
174✔
2616
  }
2617
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
20✔
2618
}
2619

2620
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2621
  if (syncCheckMember(pVnode->sync) != 0) {
×
2622
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2623
  }
2624

2625
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2626
  pRsp->code = TSDB_CODE_SUCCESS;
×
2627
  pRsp->pCont = NULL;
×
2628
  pRsp->contLen = 0;
×
2629

2630
  return 0;
×
2631
}
2632

2633
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
36✔
2634
  SSyncState syncState = syncGetState(pVnode->sync);
36✔
2635
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
36✔
2636
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
9✔
2637
  }
2638

2639
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
27✔
2640
  if (vnodeGetArbToken(pVnode, token) != 0) {
27!
2641
    return terrno = TSDB_CODE_NOT_FOUND;
×
2642
  }
2643

2644
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
27✔
2645
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
23✔
2646
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
13✔
2647
  }
2648

2649
  terrno = TSDB_CODE_SUCCESS;
14✔
2650
  return 0;
14✔
2651
}
2652

2653
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
14✔
2654
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
14✔
2655
  if (code != 0) {
14✔
2656
    return code;
7✔
2657
  }
2658

2659
  return syncCheckSynced(pVnode->sync);
7✔
2660
}
2661

2662
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
14✔
2663
  int32_t code = 0;
14✔
2664

2665
  SVArbCheckSyncReq syncReq = {0};
14✔
2666

2667
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
14✔
2668
  if (code) {
14!
2669
    return terrno = code;
×
2670
  }
2671

2672
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
14✔
2673
  pRsp->code = TSDB_CODE_SUCCESS;
14✔
2674
  pRsp->pCont = NULL;
14✔
2675
  pRsp->contLen = 0;
14✔
2676

2677
  SVArbCheckSyncRsp syncRsp = {0};
14✔
2678
  syncRsp.arbToken = syncReq.arbToken;
14✔
2679
  syncRsp.member0Token = syncReq.member0Token;
14✔
2680
  syncRsp.member1Token = syncReq.member1Token;
14✔
2681
  syncRsp.vgId = TD_VID(pVnode);
14✔
2682

2683
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
14✔
2684
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
7!
2685
  }
2686
  syncRsp.errCode = terrno;
14✔
2687

2688
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
14!
2689
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2690
    code = -1;
×
2691
    goto _OVER;
×
2692
  }
2693

2694
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
14✔
2695
  if (contLen <= 0) {
14!
2696
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2697
    code = -1;
×
2698
    goto _OVER;
×
2699
  }
2700
  void *pHead = rpcMallocCont(contLen);
14✔
2701
  if (!pHead) {
14!
2702
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2703
    code = -1;
×
2704
    goto _OVER;
×
2705
  }
2706

2707
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
14!
2708
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2709
    rpcFreeCont(pHead);
×
2710
    code = -1;
×
2711
    goto _OVER;
×
2712
  }
2713

2714
  pRsp->pCont = pHead;
14✔
2715
  pRsp->contLen = contLen;
14✔
2716

2717
  terrno = TSDB_CODE_SUCCESS;
14✔
2718

2719
_OVER:
14✔
2720
  tFreeSVArbCheckSyncReq(&syncReq);
14✔
2721
  return code;
14✔
2722
}
2723

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