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

taosdata / TDengine / #4756

25 Sep 2025 05:58AM UTC coverage: 58.829% (+0.2%) from 58.63%
#4756

push

travis-ci

web-flow
enh: taos command line support '-uroot' on windows (#33055)

135574 of 293169 branches covered (46.24%)

Branch coverage included in aggregate %.

204395 of 284720 relevant lines covered (71.79%)

18747092.16 hits per line

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

53.07
/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 <stdint.h>
17
#include "audit.h"
18
#include "cos.h"
19
#include "libs/new-stream/stream.h"
20
#include "monitor.h"
21
#include "taoserror.h"
22
#include "tencode.h"
23
#include "tglobal.h"
24
#include "tmsg.h"
25
#include "tmsgcb.h"
26
#include "tstrbuild.h"
27
#include "tutil.h"
28
#include "vnd.h"
29
#include "vnode.h"
30
#include "vnodeInt.h"
31

32
extern taos_counter_t *tsInsertCounter;
33

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

59
static int32_t vnodeCheckState(SVnode *pVnode);
60
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
61
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
62
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
63

64
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
65
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
66

67
extern int32_t vnodeListSsMigrateFileSets(SVnode *pVnode, SRpcMsg *pMsg);
68
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
69
extern int32_t vnodeQuerySsMigrateProgress(SVnode *pVnode, SRpcMsg *pMsg);
70
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
71

72
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
160,790✔
73
  int32_t code = 0;
160,790✔
74
  int32_t lino = 0;
160,790✔
75

76
  if (tStartDecode(pCoder) < 0) {
160,790!
77
    code = TSDB_CODE_INVALID_MSG;
×
78
    TSDB_CHECK_CODE(code, lino, _exit);
×
79
  }
80

81
  // flags
82
  if (tDecodeI32v(pCoder, NULL) < 0) {
160,929!
83
    code = TSDB_CODE_INVALID_MSG;
×
84
    TSDB_CHECK_CODE(code, lino, _exit);
×
85
  }
86

87
  // name
88
  char *name = NULL;
160,929✔
89
  if (tDecodeCStr(pCoder, &name) < 0) {
160,798!
90
    code = TSDB_CODE_INVALID_MSG;
×
91
    TSDB_CHECK_CODE(code, lino, _exit);
×
92
  }
93

94
  // uid
95
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
160,798✔
96
  if (uid == 0) {
161,144✔
97
    uid = tGenIdPI64();
151,366✔
98
  }
99
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
160,975✔
100

101
  // btime
102
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
160,975✔
103

104
  tEndDecode(pCoder);
160,975✔
105

106
_exit:
160,943✔
107
  if (code) {
160,943!
108
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
109
  } else {
110
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
160,943✔
111
    if (pUid) *pUid = uid;
160,943✔
112
  }
113
  return code;
160,946✔
114
}
115
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
77,562✔
116
  int32_t code = 0;
77,562✔
117
  int32_t lino = 0;
77,562✔
118

119
  int64_t  btime = taosGetTimestampMs();
77,566✔
120
  SDecoder dc = {0};
77,566✔
121
  int32_t  nReqs;
122

123
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
77,566✔
124
  if (tStartDecode(&dc) < 0) {
77,563!
125
    code = TSDB_CODE_INVALID_MSG;
×
126
    return code;
×
127
  }
128

129
  if (tDecodeI32v(&dc, &nReqs) < 0) {
77,563!
130
    code = TSDB_CODE_INVALID_MSG;
×
131
    TSDB_CHECK_CODE(code, lino, _exit);
×
132
  }
133
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
183,369✔
134
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
105,830✔
135
    TSDB_CHECK_CODE(code, lino, _exit);
105,806!
136
  }
137

138
  tEndDecode(&dc);
77,539✔
139

140
_exit:
77,562✔
141
  tDecoderClear(&dc);
77,562✔
142
  if (code) {
77,568!
143
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
144
           tstrerror(code), TMSG_INFO(pMsg->msgType));
145
  }
146
  return code;
77,563✔
147
}
148

149
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
15,576✔
150
  int32_t code = TSDB_CODE_INVALID_MSG;
15,576✔
151
  int32_t lino = 0;
15,576✔
152

153
  SDecoder dc = {0};
15,576✔
154
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
15,576✔
155

156
  SVAlterTbReq vAlterTbReq = {0};
15,576✔
157
  int64_t      ctimeMs = taosGetTimestampMs();
15,576✔
158
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
15,576!
159
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
160
    vAlterTbReq.pMultiTag = NULL;
×
161
    goto _exit;
×
162
  }
163
  taosArrayDestroy(vAlterTbReq.pMultiTag);
15,576✔
164
  vAlterTbReq.pMultiTag = NULL;
15,576✔
165

166
  code = 0;
15,576✔
167

168
_exit:
15,576✔
169
  tDecoderClear(&dc);
15,576✔
170
  if (code) {
15,576!
171
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
172
  } else {
173
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
15,576!
174
           ctimeMs);
175
  }
176
  return code;
15,576✔
177
}
178

179
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
66,554✔
180
  int32_t code = TSDB_CODE_INVALID_MSG;
66,554✔
181
  int32_t lino = 0;
66,554✔
182

183
  SMsgHead *pContOld = pMsg->pCont;
66,554✔
184
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
66,554✔
185

186
  SArray *tbUids = NULL;
66,554✔
187
  int64_t timestampMs = 0;
66,554✔
188

189
  SVDropTtlTableReq ttlReq = {0};
66,554✔
190
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
66,554!
191
    code = TSDB_CODE_INVALID_MSG;
×
192
    TSDB_CHECK_CODE(code, lino, _exit);
×
193
  }
194

195
  {  // find expired uids
196
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
66,714✔
197
    if (tbUids == NULL) {
66,706!
198
      code = terrno;
×
199
      TSDB_CHECK_CODE(code, lino, _exit);
×
200
    }
201

202
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
66,706✔
203
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
66,706✔
204
    if (code != 0) {
66,606!
205
      code = TSDB_CODE_INVALID_MSG;
×
206
      TSDB_CHECK_CODE(code, lino, _exit);
×
207
    }
208

209
    ttlReq.nUids = taosArrayGetSize(tbUids);
66,606✔
210
    ttlReq.pTbUids = tbUids;
66,469✔
211
  }
212

213
  if (ttlReq.nUids == 0) {
66,469✔
214
    code = TSDB_CODE_MSG_PREPROCESSED;
66,411✔
215
    TSDB_CHECK_CODE(code, lino, _exit);
66,411!
216
  }
217

218
  {  // prepare new content
219
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
28✔
220
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
31✔
221

222
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
31✔
223
    if (pContNew == NULL) {
31!
224
      code = terrno;
×
225
      TSDB_CHECK_CODE(code, lino, _exit);
×
226
    }
227

228
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
31!
229
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
31!
230
    }
231
    pContNew->contLen = htonl(reqLenNew);
31✔
232
    pContNew->vgId = pContOld->vgId;
31✔
233

234
    rpcFreeCont(pContOld);
31✔
235
    pMsg->pCont = pContNew;
31✔
236
    pMsg->contLen = contLenNew;
31✔
237
  }
238

239
  code = 0;
31✔
240

241
_exit:
66,472✔
242
  taosArrayDestroy(tbUids);
66,472✔
243

244
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
66,621!
245
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
246
           tstrerror(code), TMSG_INFO(pMsg->msgType));
247
  } else {
248
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
66,621✔
249
  }
250

251
  return code;
66,489✔
252
}
253

254
extern int64_t tsMaxKeyByPrecision[];
255
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
10,072,234✔
256
  int32_t code = 0;
10,072,234✔
257
  int32_t lino = 0;
10,072,234✔
258

259
  if (tStartDecode(pCoder) < 0) {
10,072,234!
260
    code = TSDB_CODE_INVALID_MSG;
×
261
    TSDB_CHECK_CODE(code, lino, _exit);
×
262
  }
263

264
  SSubmitTbData submitTbData;
265
  uint8_t       version;
266
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
10,072,780!
267
    code = TSDB_CODE_INVALID_MSG;
×
268
    TSDB_CHECK_CODE(code, lino, _exit);
×
269
  }
270
  version = (submitTbData.flags >> 8) & 0xff;
10,072,780✔
271
  submitTbData.flags = submitTbData.flags & 0xff;
10,072,780✔
272

273
  int64_t uid;
274
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,072,780✔
275
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
54,973✔
276
    TSDB_CHECK_CODE(code, lino, _exit);
55,135!
277
  }
278

279
  // submit data
280
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
10,072,881!
281
    code = TSDB_CODE_INVALID_MSG;
×
282
    TSDB_CHECK_CODE(code, lino, _exit);
×
283
  }
284

285
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
10,072,881✔
286
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
55,126✔
287
    pCoder->pos += sizeof(int64_t);
55,126✔
288
  } else {
289
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
10,017,745!
290
      code = TSDB_CODE_INVALID_MSG;
×
291
      TSDB_CHECK_CODE(code, lino, _exit);
×
292
    }
293
  }
294

295
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
10,072,618!
296
    code = TSDB_CODE_INVALID_MSG;
×
297
    TSDB_CHECK_CODE(code, lino, _exit);
×
298
  }
299

300
  // scan and check
301
  TSKEY now = btimeMs;
10,072,618✔
302
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
10,072,618✔
303
    now *= 1000;
22,206✔
304
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
10,050,412✔
305
    now *= 1000000;
2,930✔
306
  }
307

308
  int32_t keep = pVnode->config.tsdbCfg.keep2;
10,072,618✔
309

310
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
10,072,618✔
311
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
10,072,618✔
312
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
10,072,618✔
313
    uint64_t nColData;
314
    if (tDecodeU64v(pCoder, &nColData) < 0) {
822!
315
      code = TSDB_CODE_INVALID_MSG;
×
316
      TSDB_CHECK_CODE(code, lino, _exit);
1!
317
    }
318

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

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

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

338
    for (uint64_t i = 1; i < nColData; i++) {
4,693✔
339
      code = tDecodeColData(version, pCoder, &colData);
3,872✔
340
      if (code) {
3,871!
341
        code = TSDB_CODE_INVALID_MSG;
×
342
        TSDB_CHECK_CODE(code, lino, _exit);
×
343
      }
344
    }
345
  } else {
346
    uint64_t nRow;
347
    if (tDecodeU64v(pCoder, &nRow) < 0) {
10,071,721!
348
      code = TSDB_CODE_INVALID_MSG;
×
349
      TSDB_CHECK_CODE(code, lino, _exit);
500!
350
    }
351

352
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
798,554,664✔
353
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
788,483,443✔
354
      pCoder->pos += pRow->len;
788,483,443✔
355
#ifndef NO_UNALIGNED_ACCESS
356
      if (pRow->ts < minKey || pRow->ts > maxKey) {
788,483,443!
357
#else
358
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
359
      if (ts < minKey || ts > maxKey) {
360
#endif
361
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
362
        TSDB_CHECK_CODE(code, lino, _exit);
×
363
      }
364

365
      // Check pRow->sver
366
      if (pRow->sver != submitTbData.sver) {
788,482,943!
367
        code = TSDB_CODE_INVALID_DATA_FMT;
×
368
        TSDB_CHECK_CODE(code, lino, _exit);
×
369
      }
370
    }
371
  }
372

373
  if (!tDecodeIsEnd(pCoder)) {
10,072,042!
374
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
10,072,218✔
375
    pCoder->pos += sizeof(int64_t);
10,072,218✔
376
  }
377

378
  tEndDecode(pCoder);
10,072,042✔
379

380
_exit:
10,072,248✔
381
  if (code) {
10,072,248✔
382
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
501!
383
           lino, tstrerror(code));
384
  }
385
  return code;
10,072,248✔
386
}
387
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,997,683✔
388
  int32_t code = 0;
9,997,683✔
389
  int32_t lino = 0;
9,997,683✔
390

391
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
9,997,683✔
392
    return TSDB_CODE_MSG_PREPROCESSED;
1✔
393
  }
394

395
  SDecoder *pCoder = &(SDecoder){0};
9,997,682✔
396

397
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
9,997,682!
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));
9,997,744✔
403

404
  if (tStartDecode(pCoder) < 0) {
9,997,670!
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) {
9,997,703!
411
    code = TSDB_CODE_INVALID_MSG;
×
412
    TSDB_CHECK_CODE(code, lino, _exit);
×
413
  }
414

415
  int64_t btimeMs = taosGetTimestampMs();
9,997,761✔
416
  int64_t ctimeMs = btimeMs;
9,997,761✔
417
  for (int32_t i = 0; i < nSubmitTbData; i++) {
20,069,515✔
418
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
10,072,303✔
419
    TSDB_CHECK_CODE(code, lino, _exit);
10,072,255✔
420
  }
421

422
  tEndDecode(pCoder);
9,997,212✔
423

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

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

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

442
  SReadHandle handle = {0};
62,545✔
443
  handle.vnode = pVnode;
62,545✔
444
  handle.pMsgCb = &pVnode->msgCb;
62,545✔
445
  handle.skipRollup = 1;
62,545✔
446
  initStorageAPI(&handle.api);
62,545✔
447

448
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
62,546✔
449
  if (code) goto _exit;
62,547!
450

451
  res.ctimeMs = taosGetTimestampMs();
62,548✔
452
  // malloc and encode
453
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
62,548!
454
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
62,540✔
455

456
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
62,545✔
457
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
62,545✔
458

459
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
62,545✔
460
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
62,548!
461
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
462
  }
463
  tEncoderClear(pCoder);
62,542✔
464

465
  rpcFreeCont(pMsg->pCont);
62,546✔
466
  pMsg->pCont = pCont;
62,547✔
467
  pMsg->contLen = size + sizeof(SMsgHead);
62,547✔
468

469
  taosArrayDestroy(res.uidList);
62,547✔
470

471
_exit:
62,546✔
472
  return code;
62,546✔
473
}
474

475
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
476
  int32_t code = 0;
×
477
  int32_t lino = 0;
×
478

479
  int64_t         ctimeMs = taosGetTimestampMs();
×
480
  SBatchDeleteReq pReq = {0};
×
481
  SDecoder       *pCoder = &(SDecoder){0};
×
482

483
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
×
484

485
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
486
    code = TSDB_CODE_INVALID_MSG;
×
487
  }
488

489
  tDecoderClear(pCoder);
×
490
  taosArrayDestroy(pReq.deleteReqs);
×
491

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

500
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
44✔
501
  int32_t ret = 0;
44✔
502
  if ((ret = vnodeCheckState(pVnode)) != 0) {
44✔
503
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
23!
504
    return 0;
23✔
505
  }
506

507
  SVArbCheckSyncReq syncReq = {0};
21✔
508

509
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
21!
510
                                    &syncReq) != 0) {
511
    return TSDB_CODE_INVALID_MSG;
×
512
  }
513

514
  ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
21✔
515
  if (ret != 0) {
21✔
516
    vError("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
16!
517
  }
518

519
  int32_t code = terrno;
21✔
520
  tFreeSVArbCheckSyncReq(&syncReq);
21✔
521

522
  return code;
21✔
523
}
524

525
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
15,724✔
526
  int32_t          code = TSDB_CODE_SUCCESS;
15,724✔
527
  int32_t          lino = 0;
15,724✔
528
  int32_t          size = 0;
15,724✔
529
  SDecoder         dc = {0};
15,724✔
530
  SEncoder         ec = {0};
15,724✔
531
  SVDropTbBatchReq receivedBatchReqs = {0};
15,724✔
532
  SVDropTbBatchReq sentBatchReqs = {0};
15,724✔
533

534
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
15,724✔
535

536
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
15,724✔
537
  if (code < 0) {
15,724!
538
    terrno = code;
×
539
    TSDB_CHECK_CODE(code, lino, _exit);
×
540
  }
541
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
15,724✔
542
  if (!sentBatchReqs.pArray) {
15,724!
543
    code = terrno;
×
544
    goto _exit;
×
545
  }
546

547
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
31,851✔
548
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
16,127✔
549
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
16,127✔
550
    if (uid == 0) {
16,127!
551
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
552
      continue;
×
553
    }
554
    pReq->uid = uid;
16,127✔
555
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
16,127✔
556
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
32,254!
557
      code = terrno;
×
558
      goto _exit;
×
559
    }
560
  }
561
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
15,724✔
562

563
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
15,724!
564
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
15,724✔
565
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
15,724✔
566
  tEncoderClear(&ec);
15,724✔
567
  if (code != TSDB_CODE_SUCCESS) {
15,724!
568
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
569
    TSDB_CHECK_CODE(code, lino, _exit);
×
570
  }
571

572
_exit:
15,724✔
573
  tDecoderClear(&dc);
15,724✔
574
  if (sentBatchReqs.pArray) {
15,724!
575
    taosArrayDestroy(sentBatchReqs.pArray);
15,724✔
576
  }
577
  return code;
15,724✔
578
}
579

580

581
int32_t vnodePreProcessSsMigrateFileSetReq(SVnode* pVnode, SRpcMsg* pMsg) {
×
582
  int32_t          code = TSDB_CODE_SUCCESS, lino = 0;
×
583
  SSsMigrateFileSetReq req = {0};
×
584

585
  code = tDeserializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead), &req);
×
586
  if (code < 0) {
×
587
    terrno = code;
×
588
    TSDB_CHECK_CODE(code, lino, _exit);
×
589
  }
590

591
  req.nodeId = vnodeNodeId(pVnode);
×
592
  TAOS_UNUSED(tSerializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead), &req));
×
593

594
_exit:
×
595
  return code;
×
596
}
597

598
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,314,420✔
599
  int32_t code = 0;
10,314,420✔
600

601
  if (pVnode->mounted) {
10,314,420✔
602
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
10!
603
           TMSG_INFO(pMsg->msgType));
604
    return TSDB_CODE_OPS_NOT_SUPPORT;
10✔
605
  }
606

607
  switch (pMsg->msgType) {
10,314,410!
608
    case TDMT_VND_CREATE_TABLE: {
77,566✔
609
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
77,566✔
610
    } break;
77,562✔
611
    case TDMT_VND_ALTER_TABLE: {
15,576✔
612
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
15,576✔
613
    } break;
15,576✔
614
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
66,652✔
615
    case TDMT_VND_DROP_TTL_TABLE: {
616
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
66,652✔
617
    } break;
66,483✔
618
    case TDMT_VND_SUBMIT: {
9,997,718✔
619
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
9,997,718!
620
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
621
    } break;
9,997,675✔
622
    case TDMT_VND_DELETE: {
62,544✔
623
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
62,544✔
624
    } break;
62,545✔
625
    case TDMT_VND_BATCH_DEL: {
×
626
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
627
    } break;
×
628
    case TDMT_VND_ARB_CHECK_SYNC: {
44✔
629
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
44✔
630
    } break;
44✔
631
    case TDMT_VND_DROP_TABLE: {
15,724✔
632
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
15,724✔
633
    } break;
15,724✔
634
#ifdef TD_ENTERPRISE
635
    case TDMT_VND_SSMIGRATE_FILESET: {
×
636
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
×
637
    } break;
×
638
#endif
639
    default:
78,586✔
640
      break;
78,586✔
641
  }
642

643
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
10,314,195✔
644
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
517!
645
           TMSG_INFO(pMsg->msgType));
646
  }
647
  return code;
10,314,047✔
648
}
649

650
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
651
  int32_t code = 0;
×
652
  int32_t lino = 0;
×
653

654
  int64_t   st = taosGetTimestampUs();
×
655
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
656
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
657

658
  SBseBatch *pBatch = NULL;
×
659

660
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
661
  TSDB_CHECK_CODE(code, lino, _exit);
×
662

663
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
664
  int32_t rowIdx = -1;
×
665
  for (int32_t i = 0; i < sz; i++) {
×
666
    int64_t     seq = 0;
×
667
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
668
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
669
      // skip empty or null blob
670
      continue;
×
671
    }
672

673
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
674
    TSDB_CHECK_CODE(code, lino, _exit);
×
675

676
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
×
677
    if (row == NULL) {
×
678
      int32_t tlen = taosArrayGetSize(pBlobSet->pSeqTable);
×
679
      uTrace("blob invalid row index:%d, sz:%d, pBlobSet size:%d", rowIdx, sz, tlen);
×
680
      break;
×
681
    }
682

683
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
×
684
      code = TSDB_CODE_INVALID_MSG;
×
685
      TSDB_CHECK_CODE(code, lino, _exit);
×
686
    }
687
  }
688

689
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
690
  TSDB_CHECK_CODE(code, lino, _exit);
×
691

692
  int64_t cost = taosGetTimestampUs() - st;
×
693
  if (cost >= 500) {
×
694
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
695
           pBlobSet->len);
696
  }
697
_exit:
×
698
  if (code != 0) {
×
699
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
700
  }
701
  return code;
×
702
}
703

704
static int32_t inline vnodeSubmitSubColBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
705
  int32_t code = 0;
×
706
  int32_t lino = 0;
×
707

708
  int32_t   blobColIdx = 0;
×
709
  SColData *pBlobCol = NULL;
×
710
  int64_t   st = taosGetTimestampUs();
×
711
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
712
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
713

714
  SBseBatch *pBatch = NULL;
×
715

716
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
717
  TSDB_CHECK_CODE(code, lino, _exit);
×
718

719
  SColData *p = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
×
720
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitTbData->aCol); i++) {
×
721
    if (IS_STR_DATA_BLOB(p[i].type)) {
×
722
      pBlobCol = &p[i];
×
723
      break;
×
724
    }
725
  }
726
  if (pBlobCol == NULL) {
×
727
    vError("vgId:%d %s failed to find blob column in submit data", TD_VID(pVnode), __func__);
×
728
    code = TSDB_CODE_INVALID_MSG;
×
729
    goto _exit;
×
730
  }
731

732
  int32_t offset = 0;
×
733
  // int32_t   rowIdx = -1;
734
  for (int32_t i = 0; i < sz; i++) {
×
735
    int64_t     seq = 0;
×
736
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
737
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
738
      // skip empty or null blob
739
      continue;
×
740
    }
741
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
742
    TSDB_CHECK_CODE(code, lino, _exit);
×
743

744
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
×
745
    offset += BSE_SEQUECE_SIZE;
×
746
  }
747

748
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
749
  TSDB_CHECK_CODE(code, lino, _exit);
×
750

751
  int64_t cost = taosGetTimestampUs() - st;
×
752
  if (cost >= 500) {
×
753
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
754
           pBlobSet->len);
755
  }
756
_exit:
×
757
  if (code != 0) {
×
758
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
759
  }
760
  return code;
×
761
}
762
static int32_t inline vnodeSubmitBlobData(SVnode *pVnode, SSubmitTbData *pData) {
×
763
  int32_t code = 0;
×
764
  if (pData->flags & SUBMIT_REQ_WITH_BLOB) {
×
765
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
766
      code = vnodeSubmitSubColBlobData(pVnode, pData);
×
767
    } else {
768
      code = vnodeSubmitSubRowBlobData(pVnode, pData);
×
769
    }
770
  }
771

772
  return code;
×
773
}
774

775
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
13,721,309✔
776
  int32_t code = 0;
13,721,309✔
777
  int32_t lino = 0;
13,721,309✔
778
  void   *ptr = NULL;
13,721,309✔
779
  void   *pReq;
780
  int32_t len;
781

782
  (void)taosThreadMutexLock(&pVnode->mutex);
13,721,309✔
783
  if (pVnode->disableWrite) {
13,721,573!
784
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
785
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
786
    return TSDB_CODE_VND_WRITE_DISABLED;
×
787
  }
788
  (void)taosThreadMutexUnlock(&pVnode->mutex);
13,721,573✔
789

790
  if (ver <= pVnode->state.applied) {
13,721,523!
791
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
792
           pVnode->state.applied);
793
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
794
  }
795

796
  vGDebug(&pMsg->info.traceId,
13,721,523!
797
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
798
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
799
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
800
          pMsg->info.conn.applyTerm, pMsg->contLen);
801

802
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
13,721,433!
803
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
804
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
805
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
806
  }
807

808
  if (!(pVnode->state.applied + 1 == ver)) {
13,721,433!
809
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
810
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
811
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
812
  }
813

814
  atomic_store_64(&pVnode->state.applied, ver);
13,721,433✔
815
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
13,721,566✔
816

817
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
13,721,579✔
818

819
  // skip header
820
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
13,694,216✔
821
  len = pMsg->contLen - sizeof(SMsgHead);
13,694,216✔
822
  bool needCommit = false;
13,694,216✔
823

824
  switch (pMsg->msgType) {
13,694,216!
825
    /* META */
826
    case TDMT_VND_CREATE_STB:
28,283✔
827
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
28,283✔
828
      TSDB_CHECK_CODE(code, lino, _err);
28,276✔
829
      break;
28,272✔
830
    case TDMT_VND_ALTER_STB:
17,192✔
831
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
17,192✔
832
      TSDB_CHECK_CODE(code, lino, _err);
17,193!
833
      break;
17,193✔
834
    case TDMT_VND_DROP_STB:
3,057✔
835
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
3,057✔
836
      TSDB_CHECK_CODE(code, lino, _err);
3,066!
837
      break;
3,066✔
838
    case TDMT_VND_CREATE_TABLE:
85,694✔
839
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
85,694✔
840
      TSDB_CHECK_CODE(code, lino, _err);
85,701!
841
      break;
85,701✔
842
    case TDMT_VND_ALTER_TABLE:
15,583✔
843
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
15,583✔
844
      TSDB_CHECK_CODE(code, lino, _err);
15,583!
845
      break;
15,583✔
846
    case TDMT_VND_DROP_TABLE:
15,725✔
847
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
15,725✔
848
      TSDB_CHECK_CODE(code, lino, _err);
15,725!
849
      break;
15,725✔
850
    case TDMT_VND_DROP_TTL_TABLE:
×
851
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
852
      TSDB_CHECK_CODE(code, lino, _err);
×
853
      break;
×
854
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
31✔
855
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
31✔
856
      TSDB_CHECK_CODE(code, lino, _err);
31!
857
      break;
31✔
858
    case TDMT_VND_TRIM:
177✔
859
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
177✔
860
      TSDB_CHECK_CODE(code, lino, _err);
177!
861
      break;
177✔
862
#ifdef TD_ENTERPRISE
863
    case TDMT_VND_SSMIGRATE_FILESET:
×
864
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
865
      break;
×
866
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
867
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
868
      break;
×
869
    case TDMT_VND_KILL_SSMIGRATE:
×
870
      //if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
871
      break;
×
872
#endif
873
    /* TSDB */
874
    case TDMT_VND_SUBMIT: {
13,407,210✔
875
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
13,407,210!
876
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
877
      });
878
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
13,407,119!
879
      break;
13,407,148✔
880
    }
881
    case TDMT_VND_DELETE:
64,678✔
882
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
64,678✔
883
      TSDB_CHECK_CODE(code, lino, _err);
64,679!
884
      break;
64,679✔
885
    case TDMT_VND_BATCH_DEL:
×
886
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
887
      TSDB_CHECK_CODE(code, lino, _err);
×
888
      break;
×
889
      /* TQ */
890
#if defined(USE_TQ) || defined(USE_STREAM)
891
    case TDMT_VND_TMQ_SUBSCRIBE:
2,637✔
892
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
2,637✔
893
      TSDB_CHECK_CODE(code, lino, _err);
2,637!
894
      break;
2,637✔
895
    case TDMT_VND_TMQ_DELETE_SUB:
856✔
896
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
856✔
897
      TSDB_CHECK_CODE(code, lino, _err);
856!
898
      break;
856✔
899
    case TDMT_VND_TMQ_COMMIT_OFFSET:
13,233✔
900
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
13,233✔
901
      TSDB_CHECK_CODE(code, lino, _err);
13,231!
902
      break;
13,231✔
903
    case TDMT_VND_TMQ_ADD_CHECKINFO:
45✔
904
      code = tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len);
45✔
905
      TSDB_CHECK_CODE(code, lino, _err);
45!
906
      break;
45✔
907
    case TDMT_VND_TMQ_DEL_CHECKINFO:
10✔
908
      code = tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len);
10✔
909
      TSDB_CHECK_CODE(code, lino, _err);
10!
910
      break;
10✔
911
#endif
912
    case TDMT_VND_ALTER_CONFIRM:
8,323✔
913
      needCommit = pVnode->config.hashChange;
8,323✔
914
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
8,323✔
915
      TSDB_CHECK_CODE(code, lino, _err);
8,323!
916
      break;
8,323✔
917
    case TDMT_VND_ALTER_CONFIG:
897✔
918
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
897✔
919
      break;
897✔
920
    case TDMT_VND_COMMIT:
27,313✔
921
      needCommit = true;
27,313✔
922
      break;
27,313✔
923
    case TDMT_VND_CREATE_INDEX:
949✔
924
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
949✔
925
      break;
949✔
926
    case TDMT_VND_DROP_INDEX:
2,132✔
927
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,132✔
928
      break;
2,132✔
929
    case TDMT_VND_COMPACT:
173✔
930
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
173✔
931
      goto _exit;
173✔
932
    case TDMT_SYNC_CONFIG_CHANGE:
×
933
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
934
      break;
×
935
#ifdef TD_ENTERPRISE
936
    case TDMT_VND_KILL_COMPACT:
2✔
937
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
2✔
938
      break;
2✔
939
#endif
940
    /* ARB */
941
    case TDMT_VND_ARB_CHECK_SYNC:
16✔
942
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
16✔
943
      break;
16✔
944
    default:
×
945
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
946
      return TSDB_CODE_INVALID_MSG;
×
947
  }
948

949
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
13,693,986!
950

951
  walApplyVer(pVnode->pWal, ver);
13,694,001✔
952

953
  if (pVnode->pTq) {
13,693,925!
954
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
13,693,970✔
955
    if (code) {
13,693,994!
956
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
957
      return code;
×
958
    }
959
  }
960

961
  // commit if need
962
  if (needCommit) {
13,693,984✔
963
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
27,389✔
964
    code = vnodeAsyncCommit(pVnode);
27,401✔
965
    if (code) {
27,403!
966
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
967
      goto _err;
×
968
    }
969

970
    // start a new one
971
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
27,403!
972
      code = vnodeBegin(pVnode);
973
      if (code) {
974
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
975
        goto _err;
976
      }
977
    });
978
  }
979

980
_exit:
13,693,991✔
981
  return 0;
13,721,295✔
982

983
_err:
128✔
984
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
128✔
985
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
4!
986
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
987
  } else if (code) {
124!
988
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
989
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
990
  }
991

992
  return code;
128✔
993
}
994

995
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
7,978,097✔
996
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
7,978,097✔
997
    return 0;
404,065✔
998
  }
999

1000
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
7,574,032✔
1001
}
1002

1003
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
7,983,845✔
1004
  vTrace("message in vnode query queue is processing");
7,983,845!
1005
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
7,983,845✔
1006
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,175✔
1007
    return 0;
1,175✔
1008
  }
1009

1010
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
7,982,668!
1011
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1012
    return 0;
×
1013
  }
1014

1015
  SReadHandle handle = {0};
7,982,668✔
1016
  handle.vnode = pVnode;
7,982,668✔
1017
  handle.pMsgCb = &pVnode->msgCb;
7,982,668✔
1018
  handle.pWorkerCb = pInfo->workerCb;
7,982,668✔
1019
  initStorageAPI(&handle.api);
7,982,668✔
1020
  int32_t code = TSDB_CODE_SUCCESS;
7,981,732✔
1021
  bool    redirected = false;
7,981,732✔
1022

1023
  switch (pMsg->msgType) {
7,981,732!
1024
    case TDMT_SCH_QUERY:
5,830,471✔
1025
      if (!syncIsReadyForRead(pVnode->sync)) {
5,830,471✔
1026
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
122!
1027
        redirected = true;
122✔
1028
      }
1029
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
5,828,622✔
1030
      if (redirected) {
5,829,282✔
1031
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
6✔
1032
        return 0;
122✔
1033
      }
1034

1035
      return code;
5,829,276✔
1036
    case TDMT_SCH_MERGE_QUERY:
1,748,921✔
1037
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,748,921✔
1038
    case TDMT_SCH_QUERY_CONTINUE:
358,259✔
1039
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
358,259✔
1040
    case TDMT_VND_TMQ_CONSUME:
35,580✔
1041
      return tqProcessPollReq(pVnode->pTq, pMsg);
35,580✔
1042
    case TDMT_VND_TMQ_CONSUME_PUSH:
9,039✔
1043
      return tqProcessPollPush(pVnode->pTq);
9,039✔
1044
    default:
×
1045
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1046
      return TSDB_CODE_APP_ERROR;
×
1047
  }
1048
}
1049

1050
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
20,717,251✔
1051
  int32_t code = TSDB_CODE_SUCCESS;
20,717,251✔
1052
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
20,717,251!
1053
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
20,717,251✔
1054
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
14,418,649!
1055
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
13,206,929✔
1056
      !syncIsReadyForRead(pVnode->sync)) {
7,515,040✔
1057
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
4,172✔
1058
    return 0;
4,172✔
1059
  }
1060

1061
  switch (pMsg->msgType) {
20,716,650!
1062
    case TDMT_SCH_FETCH:
9,768,166✔
1063
    case TDMT_SCH_MERGE_FETCH:
1064
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
9,768,166✔
1065
    case TDMT_SCH_FETCH_RSP:
×
1066
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1067
    // case TDMT_SCH_CANCEL_TASK:
1068
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1069
    case TDMT_SCH_DROP_TASK:
7,567,804✔
1070
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,567,804✔
1071
    case TDMT_SCH_TASK_NOTIFY:
56✔
1072
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
56✔
1073
    case TDMT_SCH_QUERY_HEARTBEAT:
2,168,828✔
1074
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
2,168,828✔
1075
    case TDMT_VND_TABLE_META:
3,812✔
1076
    case TDMT_VND_TABLE_NAME:
1077
      return vnodeGetTableMeta(pVnode, pMsg, true);
3,812✔
1078
    case TDMT_VND_TABLE_CFG:
×
1079
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1080
    case TDMT_VND_BATCH_META: {
1,207,791✔
1081
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
1,207,791!
1082
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1083
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
1,208,501!
1084
      return code;
1,208,490✔
1085
    }
1086
    case TDMT_VND_VSUBTABLES_META:
×
1087
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1088
    case TDMT_VND_VSTB_REF_DBS:
×
1089
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1090
#ifdef TD_ENTERPRISE
1091
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
181✔
1092
      return vnodeQueryCompactProgress(pVnode, pMsg);
181✔
1093

1094
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
×
1095
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
×
1096

1097
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
1098
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1099
#endif
1100
      //    case TDMT_VND_TMQ_CONSUME:
1101
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1102
#ifdef USE_TQ
1103
    case TDMT_VND_TMQ_VG_WALINFO:
3✔
1104
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
3✔
1105
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
3✔
1106
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
3✔
1107
    case TDMT_VND_TMQ_SEEK:
6✔
1108
      return tqProcessSeekReq(pVnode->pTq, pMsg);
6✔
1109
#endif
1110
    default:
×
1111
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
1112
      return TSDB_CODE_APP_ERROR;
×
1113
  }
1114
}
1115

1116
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1117
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1118
  if (code) {
×
1119
    vError("failed to process sma result since %s", tstrerror(code));
×
1120
  }
1121
}
×
1122

1123
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
177,352✔
1124
  if (NULL == pMetaRsp) {
177,352!
1125
    return;
×
1126
  }
1127

1128
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
177,352✔
1129
  pMetaRsp->dbId = pVnode->config.dbId;
177,352✔
1130
  pMetaRsp->vgId = TD_VID(pVnode);
177,352✔
1131
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
177,352✔
1132
}
1133

1134
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1135

1136
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
177✔
1137
  if (!pVnode->restored) {
177✔
1138
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
29!
1139
    return 0;
29✔
1140
  }
1141

1142
  int32_t     code = 0;
148✔
1143
  SVTrimDbReq trimReq = {0};
148✔
1144

1145
  // decode
1146
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
148!
1147
    code = TSDB_CODE_INVALID_MSG;
×
1148
    goto _exit;
×
1149
  }
1150

1151
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
148!
1152

1153
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
148✔
1154

1155
_exit:
148✔
1156
  return code;
148✔
1157
}
1158

1159
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1160

1161
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1162
  int32_t          code = 0;
×
1163

1164
  SSsMigrateFileSetReq req = {0};
×
1165
  SSsMigrateFileSetRsp rsp = {0};
×
1166
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
×
1167
  pRsp->code = 0;
×
1168
  pRsp->pCont = NULL;
×
1169
  pRsp->contLen = 0;
×
1170

1171
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
×
1172
    code = TSDB_CODE_INVALID_MSG;
×
1173
    goto _exit;
×
1174
  }
1175

1176
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId, req.ssMigrateId, req.fid, req.startTimeSec);
×
1177
  rsp.ssMigrateId = req.ssMigrateId;
×
1178
  rsp.vgId = TD_VID(pVnode);
×
1179
  rsp.nodeId = req.nodeId;
×
1180
  rsp.fid = req.fid;
×
1181

1182
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
×
1183
  if (code != TSDB_CODE_SUCCESS) {
×
1184
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1185
    pRsp->code = code;
×
1186
    goto _exit;
×
1187
  }
1188

1189
  pRsp->code = TSDB_CODE_SUCCESS;
×
1190
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
×
1191
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1192
  if (pRsp->pCont == NULL) {
×
1193
    vError("vgId:%d, failed to allocate memory for ssmigrate fileset response", TD_VID(pVnode));
×
1194
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1195
    goto _exit;
×
1196
  }
1197
  TAOS_UNUSED(tSerializeSSsMigrateFileSetRsp(pRsp->pCont, pRsp->contLen, &rsp));
×
1198

1199
_exit:
×
1200
  if (code != TSDB_CODE_SUCCESS) {
×
1201
    pRsp->code = code;
×
1202
  }
1203
  return code;
×
1204
}
1205

1206
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1207

1208
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1209
  int32_t          code = 0;
×
1210
  SSsMigrateProgress req = {0};
×
1211

1212
  // decode
1213
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
×
1214
    code = TSDB_CODE_INVALID_MSG;
×
1215
    goto _exit;
×
1216
  }
1217

1218
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1219

1220
_exit:
×
1221
  pRsp->code = code;
×
1222
  return code;
×
1223
}
1224

1225
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1226
  int               ret = 0;
×
1227
  SVDropTtlTableReq ttlReq = {0};
×
1228
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1229
    ret = TSDB_CODE_INVALID_MSG;
×
1230
    goto end;
×
1231
  }
1232

1233
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1234
    ret = TSDB_CODE_INVALID_MSG;
×
1235
    goto end;
×
1236
  }
1237

1238
  if (ttlReq.nUids != 0) {
×
1239
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1240
          ttlReq.nUids);
1241
  }
1242

1243
  if (ttlReq.nUids > 0) {
×
1244
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1245
    if (code) return code;
×
1246

1247
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1248
    if (code) {
×
1249
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1250
    }
1251
  }
1252

1253
end:
×
1254
  taosArrayDestroy(ttlReq.pTbUids);
×
1255
  return ret;
×
1256
}
1257

1258
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
31✔
1259
  int32_t                 code = -1;
31✔
1260
  SMetaReader             mr = {0};
31✔
1261
  SVDropTtlTableReq       ttlReq = {0};
31✔
1262
  SVFetchTtlExpiredTbsRsp rsp = {0};
31✔
1263
  SEncoder                encoder = {0};
31✔
1264
  SArray                 *pNames = NULL;
31✔
1265
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
31✔
1266
  pRsp->code = TSDB_CODE_SUCCESS;
31✔
1267
  pRsp->pCont = NULL;
31✔
1268
  pRsp->contLen = 0;
31✔
1269

1270
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
31!
1271
    terrno = TSDB_CODE_INVALID_MSG;
×
1272
    goto _end;
×
1273
  }
1274

1275
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
31!
1276
    terrno = TSDB_CODE_INVALID_MSG;
×
1277
    goto _end;
×
1278
  }
1279

1280
  tb_uid_t    suid;
1281
  char        ctbName[TSDB_TABLE_NAME_LEN];
1282
  SVDropTbReq expiredTb = {.igNotExists = true};
31✔
1283
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
31✔
1284
  rsp.vgId = TD_VID(pVnode);
31✔
1285
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
31✔
1286
  if (!rsp.pExpiredTbs) goto _end;
31!
1287

1288
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
31✔
1289
  if (!pNames) {
31!
1290
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1291
    goto _end;
×
1292
  }
1293
  char buf[TSDB_TABLE_NAME_LEN];
1294
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
156✔
1295
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
125✔
1296
    expiredTb.suid = *uid;
125✔
1297
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
125✔
1298
    if (terrno < 0) goto _end;
125!
1299
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
125✔
1300
    void *p = taosArrayPush(pNames, buf);
125✔
1301
    if (p == NULL) {
125!
1302
      goto _end;
×
1303
    }
1304

1305
    expiredTb.name = p;
125✔
1306
    if (mr.me.type == TSDB_CHILD_TABLE) {
125✔
1307
      expiredTb.suid = mr.me.ctbEntry.suid;
76✔
1308
    }
1309

1310
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
250!
1311
      goto _end;
×
1312
    }
1313
  }
1314

1315
  int32_t ret = 0;
31✔
1316
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
31!
1317
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
31✔
1318
  if (pRsp->pCont == NULL) {
31!
1319
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1320
    code = -1;
×
1321
    goto _end;
×
1322
  }
1323
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
31✔
1324
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
31✔
1325
  tEncoderClear(&encoder);
31✔
1326

1327
  if (terrno == 0) code = 0;
31!
1328
_end:
×
1329
  metaReaderClear(&mr);
31✔
1330
  tFreeFetchTtlExpiredTbsRsp(&rsp);
31✔
1331
  taosArrayDestroy(ttlReq.pTbUids);
31✔
1332
  if (pNames) taosArrayDestroy(pNames);
31!
1333
  pRsp->code = terrno;
31✔
1334
  return code;
31✔
1335
}
1336

1337
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
28,254✔
1338
  int32_t        code = 0;
28,254✔
1339
  SVCreateStbReq req = {0};
28,254✔
1340
  SDecoder       coder;
1341

1342
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
28,254✔
1343
  pRsp->code = TSDB_CODE_SUCCESS;
28,254✔
1344
  pRsp->pCont = NULL;
28,254✔
1345
  pRsp->contLen = 0;
28,254✔
1346

1347
  // decode and process req
1348
  tDecoderInit(&coder, pReq, len);
28,254✔
1349

1350
  code = tDecodeSVCreateStbReq(&coder, &req);
28,272✔
1351
  if (code) {
28,252!
1352
    pRsp->code = code;
×
1353
    goto _err;
×
1354
  }
1355

1356
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
28,252✔
1357
  if (code) {
28,295✔
1358
    pRsp->code = code;
4✔
1359
    goto _err;
4✔
1360
  }
1361

1362
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
28,291!
1363
    pRsp->code = code;
×
1364
    goto _err;
×
1365
  }
1366

1367
  tDecoderClear(&coder);
28,274✔
1368
  return 0;
28,266✔
1369

1370
_err:
4✔
1371
  tDecoderClear(&coder);
4✔
1372
  return code;
4✔
1373
}
1374

1375
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
85,694✔
1376
                                       SRpcMsg *pOriginRpc) {
1377
  SDecoder           decoder = {0};
85,694✔
1378
  SEncoder           encoder = {0};
85,694✔
1379
  int32_t            rcode = 0;
85,694✔
1380
  SVCreateTbBatchReq req = {0};
85,694✔
1381
  SVCreateTbReq     *pCreateReq;
1382
  SVCreateTbBatchRsp rsp = {0};
85,694✔
1383
  SVCreateTbRsp      cRsp = {0};
85,694✔
1384
  char               tbName[TSDB_TABLE_FNAME_LEN];
1385
  STbUidStore       *pStore = NULL;
85,694✔
1386
  SArray            *tbUids = NULL;
85,694✔
1387
  SArray            *tbNames = NULL;
85,694✔
1388
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
85,694✔
1389
  pRsp->code = TSDB_CODE_SUCCESS;
85,694✔
1390
  pRsp->pCont = NULL;
85,694✔
1391
  pRsp->contLen = 0;
85,694✔
1392

1393
  // decode
1394
  tDecoderInit(&decoder, pReq, len);
85,694✔
1395
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
85,691!
1396
    rcode = -1;
×
1397
    terrno = TSDB_CODE_INVALID_MSG;
×
1398
    goto _exit;
×
1399
  }
1400

1401
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
85,695✔
1402
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
85,704✔
1403
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
85,703✔
1404
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
85,702!
1405
    rcode = -1;
×
1406
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1407
    goto _exit;
×
1408
  }
1409

1410
  // loop to create table
1411
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
216,046✔
1412
    pCreateReq = req.pReqs + iReq;
130,340✔
1413
    memset(&cRsp, 0, sizeof(cRsp));
130,340✔
1414

1415
    if (tsEnableAudit && tsEnableAuditCreateTable) {
130,340!
1416
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
130,340!
1417
      if (str == NULL) {
130,342!
1418
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1419
        rcode = -1;
×
1420
        goto _exit;
×
1421
      }
1422
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
130,342✔
1423
      if (taosArrayPush(tbNames, &str) == NULL) {
130,342!
1424
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1425
        rcode = -1;
×
1426
        goto _exit;
×
1427
      }
1428
    }
1429

1430
    // validate hash
1431
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
130,342✔
1432
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
130,341!
1433
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1434
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1435
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1436
        rcode = -1;
×
1437
        goto _exit;
×
1438
      }
1439
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1440
      continue;
×
1441
    }
1442

1443
    // do create table
1444
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
130,336✔
1445
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
125!
1446
        cRsp.code = TSDB_CODE_SUCCESS;
109✔
1447
      } else {
1448
        cRsp.code = terrno;
16✔
1449
      }
1450
    } else {
1451
      cRsp.code = TSDB_CODE_SUCCESS;
130,221✔
1452
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
130,221!
1453
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1454
      }
1455
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
260,442!
1456
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1457
        rcode = -1;
×
1458
        goto _exit;
×
1459
      }
1460
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
130,221✔
1461
    }
1462

1463
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
260,688!
1464
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1465
      rcode = -1;
×
1466
      goto _exit;
×
1467
    }
1468
  }
1469

1470
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
85,706✔
1471
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
85,706!
1472
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1473
  }
1474
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
85,704!
1475
    goto _exit;
×
1476
  }
1477
  pStore = tdUidStoreFree(pStore);
85,702✔
1478

1479
  // prepare rsp
1480
  int32_t ret = 0;
85,701✔
1481
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
85,701!
1482
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
85,699✔
1483
  if (pRsp->pCont == NULL) {
85,701!
1484
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1485
    rcode = -1;
×
1486
    goto _exit;
×
1487
  }
1488
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
85,701✔
1489
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
85,701!
1490
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1491
  }
1492

1493
  if (tsEnableAudit && tsEnableAuditCreateTable) {
85,701!
1494
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
85,701✔
1495

1496
    SName name = {0};
85,701✔
1497
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
85,701!
1498
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1499
    }
1500

1501
    SStringBuilder sb = {0};
85,698✔
1502
    for (int32_t i = 0; i < tbNames->size; i++) {
216,013✔
1503
      char **key = (char **)taosArrayGet(tbNames, i);
130,319✔
1504
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
130,321✔
1505
      if (i < tbNames->size - 1) {
130,318✔
1506
        taosStringBuilderAppendChar(&sb, ',');
44,619✔
1507
      }
1508
      // taosMemoryFreeClear(*key);
1509
    }
1510

1511
    size_t len = 0;
85,694✔
1512
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
85,694✔
1513

1514
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
85,699!
1515
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
77,563✔
1516
    }
1517

1518
    taosStringBuilderDestroy(&sb);
85,697✔
1519
  }
1520

1521
_exit:
×
1522
  tDeleteSVCreateTbBatchReq(&req);
85,696✔
1523
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
85,702✔
1524
  taosArrayDestroy(tbUids);
85,704✔
1525
  tDecoderClear(&decoder);
85,703✔
1526
  tEncoderClear(&encoder);
85,701✔
1527
  taosArrayDestroyP(tbNames, NULL);
85,704✔
1528
  return rcode;
85,704✔
1529
}
1530

1531
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
17,192✔
1532
  int32_t        code = 0;
17,192✔
1533
  SVCreateStbReq req = {0};
17,192✔
1534
  SDecoder       dc = {0};
17,192✔
1535

1536
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
17,192✔
1537
  pRsp->code = TSDB_CODE_SUCCESS;
17,192✔
1538
  pRsp->pCont = NULL;
17,192✔
1539
  pRsp->contLen = 0;
17,192✔
1540

1541
  tDecoderInit(&dc, pReq, len);
17,192✔
1542

1543
  // decode req
1544
  code = tDecodeSVCreateStbReq(&dc, &req);
17,192✔
1545
  if (code) {
17,192!
1546
    tDecoderClear(&dc);
×
1547
    return code;
×
1548
  }
1549

1550
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
17,192✔
1551
  if (code) {
17,193!
1552
    pRsp->code = code;
×
1553
    tDecoderClear(&dc);
×
1554
    return code;
×
1555
  }
1556

1557
  tDecoderClear(&dc);
17,193✔
1558

1559
  return 0;
17,193✔
1560
}
1561

1562
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
3,057✔
1563
  SVDropStbReq req = {0};
3,057✔
1564
  int32_t      rcode = TSDB_CODE_SUCCESS;
3,057✔
1565
  SDecoder     decoder = {0};
3,057✔
1566
  SArray      *tbUidList = NULL;
3,057✔
1567

1568
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
3,057✔
1569
  pRsp->pCont = NULL;
3,057✔
1570
  pRsp->contLen = 0;
3,057✔
1571

1572
  // decode request
1573
  tDecoderInit(&decoder, pReq, len);
3,057✔
1574
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
3,057!
1575
    rcode = TSDB_CODE_INVALID_MSG;
×
1576
    goto _exit;
×
1577
  }
1578

1579
  STraceId* trace = &(pOriginRpc->info.traceId);
3,059✔
1580

1581
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
3,059!
1582
              trace ? trace->msgId : 0, req.name);
1583

1584
  // process request
1585
  tbUidList = taosArrayInit(8, sizeof(int64_t));
3,065✔
1586
  if (tbUidList == NULL) goto _exit;
3,066!
1587
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
3,066✔
1588
    rcode = terrno;
2✔
1589
    goto _exit;
2✔
1590
  }
1591

1592
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
3,064!
1593
    rcode = terrno;
×
1594
    goto _exit;
×
1595
  }
1596

1597
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
3,064!
1598
    rcode = terrno;
×
1599
    goto _exit;
×
1600
  }
1601

1602
  // return rsp
1603
_exit:
3,064✔
1604
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
3,066!
1605
              trace ? trace->msgId : 0, req.name);
1606
  if (tbUidList) taosArrayDestroy(tbUidList);
3,066!
1607
  pRsp->code = rcode;
3,066✔
1608
  tDecoderClear(&decoder);
3,066✔
1609
  return 0;
3,066✔
1610
}
1611

1612
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15,583✔
1613
  SVAlterTbReq  vAlterTbReq = {0};
15,583✔
1614
  SVAlterTbRsp  vAlterTbRsp = {0};
15,583✔
1615
  SDecoder      dc = {0};
15,583✔
1616
  int32_t       code = 0;
15,583✔
1617
  int32_t       lino = 0;
15,583✔
1618
  int32_t       ret;
1619
  SEncoder      ec = {0};
15,583✔
1620
  STableMetaRsp vMetaRsp = {0};
15,583✔
1621

1622
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
15,583✔
1623
  pRsp->pCont = NULL;
15,583✔
1624
  pRsp->contLen = 0;
15,583✔
1625
  pRsp->code = TSDB_CODE_SUCCESS;
15,583✔
1626

1627
  tDecoderInit(&dc, pReq, len);
15,583✔
1628

1629
  // decode
1630
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
15,583!
1631
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1632
    tDecoderClear(&dc);
×
1633
    goto _exit;
×
1634
  }
1635

1636
  // process
1637
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
15,583✔
1638
    vAlterTbRsp.code = terrno;
921✔
1639
    tDecoderClear(&dc);
921✔
1640
    goto _exit;
921✔
1641
  }
1642
  tDecoderClear(&dc);
14,662✔
1643

1644
  if (NULL != vMetaRsp.pSchemas) {
14,662✔
1645
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
1,395✔
1646
    vAlterTbRsp.pMeta = &vMetaRsp;
1,395✔
1647
  }
1648

1649
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
14,662✔
1650
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
1,515✔
1651
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
13,152✔
1652
    if (uid == 0) {
13,152!
1653
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1654
             vAlterTbReq.tbName);
1655
      goto _exit;
×
1656
    }
1657

1658
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
13,152✔
1659
    void   *p = taosArrayPush(tbUids, &uid);
13,152✔
1660
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
13,152!
1661

1662
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
13,152!
1663
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
13,152!
1664
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1665
    }
1666

1667
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
13,152!
1668
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
13,152!
1669
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1670
    }
1671

1672
    taosArrayDestroy(tbUids);
13,152✔
1673
  }
1674

1675
_exit:
1,510✔
1676
  taosArrayDestroy(vAlterTbReq.pMultiTag);
15,583✔
1677
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
15,583!
1678
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
15,583✔
1679
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
15,583✔
1680
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
15,583!
1681
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1682
  }
1683

1684
  tEncoderClear(&ec);
15,583✔
1685
  if (vMetaRsp.pSchemas) {
15,583✔
1686
    taosMemoryFree(vMetaRsp.pSchemas);
1,395!
1687
    taosMemoryFree(vMetaRsp.pSchemaExt);
1,395!
1688
  }
1689
  if (vMetaRsp.pColRefs) {
15,583✔
1690
    taosMemoryFree(vMetaRsp.pColRefs);
321!
1691
  }
1692
  return 0;
15,583✔
1693
}
1694

1695
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
15,725✔
1696
                                     SRpcMsg *pOriginRpc) {
1697
  SVDropTbBatchReq req = {0};
15,725✔
1698
  SVDropTbBatchRsp rsp = {0};
15,725✔
1699
  SDecoder         decoder = {0};
15,725✔
1700
  SEncoder         encoder = {0};
15,725✔
1701
  int32_t          ret;
1702
  SArray          *tbUids = NULL;
15,725✔
1703
  STbUidStore     *pStore = NULL;
15,725✔
1704
  SArray          *tbNames = NULL;
15,725✔
1705

1706
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
15,725✔
1707
  pRsp->pCont = NULL;
15,725✔
1708
  pRsp->contLen = 0;
15,725✔
1709
  pRsp->code = TSDB_CODE_SUCCESS;
15,725✔
1710

1711
  // decode req
1712
  tDecoderInit(&decoder, pReq, len);
15,725✔
1713
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
15,725✔
1714
  if (ret < 0) {
15,725!
1715
    terrno = TSDB_CODE_INVALID_MSG;
×
1716
    pRsp->code = terrno;
×
1717
    goto _exit;
×
1718
  }
1719

1720
  // process req
1721
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
15,725✔
1722
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
15,725✔
1723
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
15,725✔
1724
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
15,725!
1725

1726
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,853✔
1727
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
16,128✔
1728
    SVDropTbRsp  dropTbRsp = {0};
16,128✔
1729
    tb_uid_t     tbUid = 0;
16,128✔
1730

1731
    /* code */
1732
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
16,128✔
1733
    if (ret < 0) {
16,128!
1734
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1735
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1736
      } else {
1737
        dropTbRsp.code = terrno;
×
1738
      }
1739
    } else {
1740
      dropTbRsp.code = TSDB_CODE_SUCCESS;
16,128✔
1741
      if (tbUid > 0) {
16,128!
1742
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1743
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1744
        }
1745
      }
1746
    }
1747

1748
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
32,256!
1749
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1750
      pRsp->code = terrno;
×
1751
      goto _exit;
×
1752
    }
1753

1754
    if (tsEnableAuditCreateTable) {
16,128!
1755
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
16,128!
1756
      if (str == NULL) {
16,128!
1757
        pRsp->code = terrno;
×
1758
        goto _exit;
×
1759
      }
1760
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
16,128✔
1761
      if (taosArrayPush(tbNames, &str) == NULL) {
16,128!
1762
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1763
        pRsp->code = terrno;
×
1764
        goto _exit;
×
1765
      }
1766
    }
1767
  }
1768

1769
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
15,725!
1770
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1771
  }
1772

1773
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
15,725!
1774
    goto _exit;
×
1775
  }
1776

1777
  if (tsEnableAuditCreateTable) {
15,725!
1778
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
15,725✔
1779

1780
    SName name = {0};
15,725✔
1781
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
15,725!
1782
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1783
    }
1784

1785
    SStringBuilder sb = {0};
15,725✔
1786
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
31,853✔
1787
      char **key = (char **)taosArrayGet(tbNames, iReq);
16,128✔
1788
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
16,128✔
1789
      if (iReq < req.nReqs - 1) {
16,128✔
1790
        taosStringBuilderAppendChar(&sb, ',');
403✔
1791
      }
1792
      taosMemoryFreeClear(*key);
16,128!
1793
    }
1794

1795
    size_t len = 0;
15,725✔
1796
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
15,725✔
1797

1798
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
15,725!
1799
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
15,724✔
1800
    }
1801

1802
    taosStringBuilderDestroy(&sb);
15,725✔
1803
  }
1804

1805
_exit:
×
1806
  taosArrayDestroy(tbUids);
15,725✔
1807
  pStore = tdUidStoreFree(pStore);
15,725✔
1808
  tDecoderClear(&decoder);
15,725✔
1809
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
15,725!
1810
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
15,725✔
1811
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
15,725✔
1812
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
15,725!
1813
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1814
  }
1815
  tEncoderClear(&encoder);
15,725✔
1816
  taosArrayDestroy(rsp.pArray);
15,725✔
1817
  taosArrayDestroy(tbNames);
15,725✔
1818
  return 0;
15,725✔
1819
}
1820

1821
#ifdef BUILD_NO_CALL
1822
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1823
                                              const char *tags) {
1824
  SSubmitBlkIter blkIter = {0};
1825
  STSchema      *pSchema = NULL;
1826
  tb_uid_t       suid = 0;
1827
  STSRow        *row = NULL;
1828
  int32_t        rv = -1;
1829

1830
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1831
  if (blkIter.row == NULL) return 0;
1832

1833
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1834
                                         &pSchema);  // TODO: use the real schema
1835
  if (TSDB_CODE_SUCCESS != code) {
1836
    printf("%s:%d no valid schema\n", tags, __LINE__);
1837
    return code;
1838
  }
1839

1840
  suid = msgIter->suid;
1841
  rv = TD_ROW_SVER(blkIter.row);
1842

1843
  char __tags[128] = {0};
1844
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1845
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1846
    tdSRowPrint(row, pSchema, __tags);
1847
  }
1848

1849
  taosMemoryFreeClear(pSchema);
1850

1851
  return TSDB_CODE_SUCCESS;
1852
}
1853
#endif
1854
typedef struct SSubmitReqConvertCxt {
1855
  SSubmitMsgIter msgIter;
1856
  SSubmitBlk    *pBlock;
1857
  SSubmitBlkIter blkIter;
1858
  STSRow        *pRow;
1859
  STSRowIter     rowIter;
1860
  SSubmitTbData *pTbData;
1861
  STSchema      *pTbSchema;
1862
  SArray        *pColValues;
1863
} SSubmitReqConvertCxt;
1864

1865
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1866
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1867
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1868
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1869
  if (TSDB_CODE_SUCCESS != code) {
×
1870
    return code;
×
1871
  }
1872
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1873

1874
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1875
  if (NULL == pCxt->pTbData) {
×
1876
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1877
    if (NULL == pCxt->pTbData) {
×
1878
      return terrno;
×
1879
    }
1880
  }
1881
  pCxt->pTbData->flags = 0;
×
1882
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1883
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1884
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1885
  pCxt->pTbData->pCreateTbReq = NULL;
×
1886
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1887
  if (NULL == pCxt->pTbData->aRowP) {
×
1888
    return terrno;
×
1889
  }
1890

1891
  taosArrayDestroy(pCxt->pColValues);
×
1892
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1893
  if (NULL == pCxt->pColValues) {
×
1894
    return terrno;
×
1895
  }
1896
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1897
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1898
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1899
      return terrno;
×
1900
    }
1901
  }
1902

1903
  return TSDB_CODE_SUCCESS;
×
1904
}
1905

1906
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1907
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1908
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1909
  taosMemoryFreeClear(pCxt->pTbData);
×
1910
  taosArrayDestroy(pCxt->pColValues);
×
1911
}
×
1912

1913
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1914
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1915
    pColVal->flag = CV_FLAG_NONE;
×
1916
    return TSDB_CODE_SUCCESS;
×
1917
  }
1918

1919
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1920
    pColVal->flag = CV_FLAG_NULL;
×
1921
    return TSDB_CODE_SUCCESS;
×
1922
  }
1923

1924
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1925
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
1926
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
1927
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
1928

1929
    } else {
1930
      pColVal->value.nData = varDataLen(pCellVal->val);
×
1931
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1932
    }
1933
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1934
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1935
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1936
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1937
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1938
  } else {
1939
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1940
  }
1941

1942
  pColVal->flag = CV_FLAG_VALUE;
×
1943
  return TSDB_CODE_SUCCESS;
×
1944
}
1945

1946
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1947
  int32_t code = TSDB_CODE_SUCCESS;
×
1948
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1949
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1950
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1951
    SCellVal  cellVal = {0};
×
1952
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1953
      break;
×
1954
    }
1955
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1956
  }
1957
  return code;
×
1958
}
1959

1960
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1961
  if (pCxt->msgIter.schemaLen <= 0) {
×
1962
    return TSDB_CODE_SUCCESS;
×
1963
  }
1964

1965
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1966
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1967
    return terrno;
×
1968
  }
1969

1970
  SDecoder decoder = {0};
×
1971
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1972
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1973
  tDecoderClear(&decoder);
×
1974

1975
  return code;
×
1976
}
1977

1978
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1979
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1980
  if (NULL == pReq2->aSubmitTbData) {
×
1981
    return terrno;
×
1982
  }
1983

1984
  SSubmitReqConvertCxt cxt = {0};
×
1985

1986
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1987
  while (TSDB_CODE_SUCCESS == code) {
×
1988
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1989
    if (TSDB_CODE_SUCCESS == code) {
×
1990
      if (NULL == cxt.pBlock) {
×
1991
        break;
×
1992
      }
1993
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1994
    }
1995
    if (TSDB_CODE_SUCCESS == code) {
×
1996
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1997
    }
1998
    if (TSDB_CODE_SUCCESS == code) {
×
1999
      code = vnodeDecodeCreateTbReq(&cxt);
×
2000
    }
2001
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2002
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2003
      if (TSDB_CODE_SUCCESS == code) {
×
2004
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2005

2006
        SRowBuildScanInfo sinfo = {0};
×
2007
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2008
      }
2009
    }
2010
    if (TSDB_CODE_SUCCESS == code) {
×
2011
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2012
    }
2013
    if (TSDB_CODE_SUCCESS == code) {
×
2014
      taosMemoryFreeClear(cxt.pTbData);
×
2015
    }
2016
  }
2017

2018
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2019
  return code;
×
2020
}
2021

2022
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2023
  int32_t  code = TSDB_CODE_SUCCESS;
×
2024
  char    *pMsg = NULL;
×
2025
  uint32_t msglen = 0;
×
2026
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2027
  if (TSDB_CODE_SUCCESS == code) {
×
2028
    pMsg = taosMemoryMalloc(msglen);
×
2029
    if (NULL == pMsg) {
×
2030
      code = terrno;
×
2031
    }
2032
  }
2033
  if (TSDB_CODE_SUCCESS == code) {
×
2034
    SEncoder encoder;
2035
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2036
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2037
    tEncoderClear(&encoder);
×
2038
  }
2039
  if (TSDB_CODE_SUCCESS == code) {
×
2040
    *ppMsg = pMsg;
×
2041
  }
2042
  return code;
×
2043
}
2044

2045
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
252✔
2046
  int32_t code = 0;
252✔
2047
  int32_t lino = 0;
252✔
2048

2049
  SMetaEntry *pEntry = NULL;
252✔
2050
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
252✔
2051
  if (code) {
252!
2052
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2053
    TSDB_CHECK_CODE(code, lino, _exit);
×
2054
  }
2055
  if (pEntry->type != TSDB_SUPER_TABLE) {
252!
2056
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2057
           __LINE__);
2058
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2059
    TSDB_CHECK_CODE(code, lino, _exit);
×
2060
  }
2061

2062
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
252!
2063
  if (NULL == *ppRsp) {
252!
2064
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2065
  }
2066
  (*ppRsp)->suid = pSubmitTbData->suid;
252✔
2067
  (*ppRsp)->tuid = pSubmitTbData->uid;
252✔
2068
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
252✔
2069
  (*ppRsp)->vgId = pVnode->config.vgId;
252✔
2070
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
252✔
2071
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
252✔
2072
  (*ppRsp)->pSchemas =
504✔
2073
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
252!
2074
  if (NULL == (*ppRsp)->pSchemas) {
252!
2075
    taosMemoryFree(*ppRsp);
×
2076
    *ppRsp = NULL;
×
2077
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2078
  }
2079
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
252✔
2080
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
252✔
2081
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
252✔
2082
  if (pEntry->pExtSchemas != NULL) {
252!
2083
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2084
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2085
      taosMemoryFree((*ppRsp)->pSchemas);
×
2086
      taosMemoryFree(*ppRsp);
×
2087
      *ppRsp = NULL;
×
2088
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2089
    }
2090
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2091
  }
2092

2093
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
252✔
2094
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
2✔
2095
  }
2096
_exit:
250✔
2097
  metaFetchEntryFree(&pEntry);
252✔
2098
  if (code != TSDB_CODE_SUCCESS) {
252✔
2099
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
2!
2100
  }
2101
  return code;
252✔
2102
}
2103

2104
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
107✔
2105
  int32_t code = 0;
107✔
2106
  int32_t lino = 0;
107✔
2107

2108
  SMetaEntry *pEntry = NULL;
107✔
2109
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
107✔
2110
  if (code) {
107!
2111
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2112
    TSDB_CHECK_CODE(code, lino, _exit);
×
2113
  }
2114
  if (pEntry->type != TSDB_NORMAL_TABLE) {
107!
2115
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode),
×
2116
           uid, __LINE__);
2117
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2118
    TSDB_CHECK_CODE(code, lino, _exit);
×
2119
  }
2120

2121
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
107!
2122
  if (NULL == *ppRsp) {
107!
2123
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2124
  }
2125

2126
  (*ppRsp)->tuid = pEntry->uid;
107✔
2127
  (*ppRsp)->vgId = pVnode->config.vgId;
107✔
2128
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
107✔
2129
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
107✔
2130
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
107!
2131
  if (NULL == (*ppRsp)->pSchemas) {
107!
2132
    taosMemoryFree(*ppRsp);
×
2133
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2134
  }
2135
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
107✔
2136
  if (pEntry->pExtSchemas != NULL) {
107!
2137
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2138
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2139
      taosMemoryFree((*ppRsp)->pSchemas);
×
2140
      taosMemoryFree(*ppRsp);
×
2141
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2142
    }
2143
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2144
  }
2145

2146
_exit:
107✔
2147
  metaFetchEntryFree(&pEntry);
107✔
2148
  if (code != TSDB_CODE_SUCCESS) {
107!
2149
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2150
  }
2151
  return code;
107✔
2152
}
2153

2154
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
2,658✔
2155
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
2,658✔
2156
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
107✔
2157
    if (code) {
107!
2158
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2159
      return code;
×
2160
    }
2161
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
107✔
2162
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
2,551✔
2163
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
252✔
2164
  }
2165
  return TSDB_CODE_SUCCESS;
2,299✔
2166
}
2167

2168
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
13,407,152✔
2169
                                          int64_t      version,   // version
2170
                                          SSubmitReq2 *pRequest,  // request
2171
                                          SSubmitRsp2 *pResponse  // response
2172
) {
2173
  int32_t code = TSDB_CODE_SUCCESS;
13,407,152✔
2174
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
13,407,152✔
2175
  SArray *newTbUids = NULL;
13,407,249✔
2176

2177
  for (int32_t i = 0; i < numTbData; ++i) {
26,889,688✔
2178
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
13,482,482✔
2179

2180
    if (pTbData->pCreateTbReq == NULL) {
13,482,563✔
2181
      continue;
13,427,185✔
2182
    }
2183

2184
    pTbData->uid = pTbData->pCreateTbReq->uid;
55,378✔
2185

2186
    // Alloc necessary resources
2187
    if (pResponse->aCreateTbRsp == NULL) {
55,378✔
2188
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
37,121✔
2189
      if (pResponse->aCreateTbRsp == NULL) {
37,121!
2190
        code = terrno;
×
2191
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2192
               tstrerror(code), version);
2193
        taosArrayDestroy(newTbUids);
×
2194
        return code;
×
2195
      }
2196
    }
2197

2198
    // Do create table
2199
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
55,378✔
2200

2201
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
55,380✔
2202
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
55,395✔
2203
    if (code == TSDB_CODE_SUCCESS) {
55,399✔
2204
      // Allocate necessary resources
2205
      if (newTbUids == NULL) {
45,736✔
2206
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
34,459✔
2207
        if (newTbUids == NULL) {
34,459!
2208
          code = terrno;
×
2209
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2210
                 tstrerror(code), version);
2211
          return code;
×
2212
        }
2213
      }
2214

2215
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
91,472!
2216
        code = terrno;
×
2217
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2218
               tstrerror(code), version);
2219
        taosArrayDestroy(newTbUids);
×
2220
        return code;
×
2221
      }
2222

2223
      if (pCreateTbRsp->pMeta) {
45,736!
2224
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
45,736✔
2225
      }
2226
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
9,663✔
2227
      code = terrno = 0;
9,655✔
2228
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
9,653✔
2229

2230
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2231
      if (i == 0) {
9,653✔
2232
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2233
        // only the first one needs to be processed.
2234
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
2,659✔
2235
        if (code) {
2,658✔
2236
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
109!
2237
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2238
          taosArrayDestroy(newTbUids);
109✔
2239
          return code;
109✔
2240
        }
2241
      }
2242
    } else {
2243
      code = terrno;
8✔
2244
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
8!
2245
             tstrerror(code), version);
2246
      taosArrayDestroy(newTbUids);
8✔
2247
      return code;
8✔
2248
    }
2249
  }
2250

2251
  // Update the affected table uid list
2252
  if (taosArrayGetSize(newTbUids) > 0) {
13,407,206✔
2253
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
34,459✔
2254
           (int32_t)taosArrayGetSize(newTbUids));
2255
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
34,459!
2256
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2257
    }
2258
  }
2259

2260
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
13,407,145✔
2261

2262
  taosArrayDestroy(newTbUids);
13,407,149✔
2263
  return code;
13,407,021✔
2264
}
2265

2266
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse, SSubmitTbData *pTbData,
2✔
2267
                                     int32_t numTbData) {
2268
  int32_t code = 0;
2✔
2269
  int32_t lino = 0;
2✔
2270
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
2!
2271
    return;
×
2272
  }
2273
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
2!
2274
                                  // so table info has exitst and we do not need to add again.
2275
    return;
2✔
2276
  }
2277
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
2278
  if (pResponse->aCreateTbRsp == NULL) {
×
2279
    code = terrno;
×
2280
    TSDB_CHECK_CODE(code, lino, _exit);
×
2281
  }
2282
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
2283
  if (pCreateTbRsp == NULL) {
×
2284
    code = terrno;
×
2285
    TSDB_CHECK_CODE(code, lino, _exit);
×
2286
  }
2287
  if (pTbData->suid == 0) {
×
2288
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2289
    if (code) {
×
2290
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2291
    }
2292
  } else {
2293
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2294
  }
2295

2296
  TSDB_CHECK_CODE(code, lino, _exit);
×
2297
_exit:
×
2298
  if (code != TSDB_CODE_SUCCESS) {
×
2299
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2300
  }
2301
  return;
×
2302
}
2303

2304
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
13,407,001✔
2305
  int32_t code = TSDB_CODE_SUCCESS;
13,407,001✔
2306
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
13,407,001✔
2307
  int8_t  hasBlob = 0;
13,407,097✔
2308

2309
  // Scan submit data
2310
  for (int32_t i = 0; i < numTbData; ++i) {
26,889,603✔
2311
    SMetaInfo      info = {0};
13,482,366✔
2312
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
13,482,366✔
2313

2314
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
13,482,444!
2315
      hasBlob = 1;
×
2316
    }
2317
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,482,444✔
2318
      continue;  // skip column data format
821✔
2319
    }
2320

2321
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
13,481,623✔
2322
    if (code) {
13,481,643✔
2323
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
21✔
2324
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
21✔
2325
            __LINE__, tstrerror(code), version, pTbData->uid);
2326
      return code;
7✔
2327
    }
2328

2329
    if (info.suid != pTbData->suid) {
13,481,622!
2330
      code = TSDB_CODE_INVALID_MSG;
×
2331
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2332
             " info.suid:%" PRId64,
2333
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2334
             info.suid);
2335
      return code;
×
2336
    }
2337

2338
    if (info.suid) {
13,481,622✔
2339
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
12,903,750✔
2340
      if (code) {
12,903,839✔
2341
        code = TSDB_CODE_INTERNAL_ERROR;
24✔
2342
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
24!
2343
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2344
        return code;
×
2345
      }
2346
    }
2347

2348
    if (pTbData->sver != info.skmVer) {
13,481,687✔
2349
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
2✔
2350
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
2✔
2351
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
2!
2352
             " sver:%d"
2353
             " info.skmVer:%d",
2354
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2355
             info.skmVer);
2356
      return code;
2✔
2357
    }
2358
  }
2359

2360
  // Do write data
2361
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
13,407,237✔
2362

2363
  for (int32_t i = 0; i < numTbData; ++i) {
26,889,701✔
2364
    int32_t        affectedRows = 0;
13,482,451✔
2365
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
13,482,451✔
2366

2367
    if (hasBlob) {
13,482,451!
2368
      code = vnodeSubmitBlobData(pVnode, pTbData);
×
2369
      if (code) {
×
2370
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2371
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2372
        return code;
×
2373
      }
2374
    }
2375

2376
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
13,482,451✔
2377
    if (code) {
13,482,545!
2378
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2379
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2380
      return code;
×
2381
    }
2382

2383
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
13,482,545✔
2384
    if (code) {
13,482,517!
2385
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2386
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2387
      return code;
×
2388
    }
2389
    pResponse->affectedRows += affectedRows;
13,482,523✔
2390
  }
2391

2392
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
13,407,250✔
2393
         pResponse->affectedRows);
2394
  return code;
13,407,200✔
2395
}
2396

2397
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
821✔
2398
  int32_t code = 0;
821✔
2399

2400
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
821✔
2401
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
822✔
2402

2403
  if (numCols <= 0) {
822!
2404
    code = TSDB_CODE_INVALID_MSG;
×
2405
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2406
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2407
    return code;
×
2408
  }
2409

2410
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
822!
2411
      aColData[0].nVal <= 0) {
822✔
2412
    code = TSDB_CODE_INVALID_MSG;
1✔
2413
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
1!
2414
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2415
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2416
           aColData[0].type, aColData[0].nVal);
2417
    return code;
×
2418
  }
2419

2420
  for (int32_t i = 1; i < numCols; ++i) {
4,696✔
2421
    if (aColData[i].nVal != aColData[0].nVal) {
3,875!
2422
      code = TSDB_CODE_INVALID_MSG;
×
2423
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2424
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2425
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2426
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2427
      return code;
×
2428
    }
2429
  }
2430

2431
  SRowKey *pLastKey = NULL;
821✔
2432
  SRowKey  lastKey = {0};
821✔
2433
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
45,835✔
2434
    SRowKey key = {0};
45,013✔
2435

2436
    tColDataArrGetRowKey(aColData, numCols, i, &key);
45,013✔
2437

2438
    if (key.ts < minKey || key.ts > maxKey) {
45,012!
2439
      code = TSDB_CODE_INVALID_MSG;
×
2440
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2441
             " is out of range [%" PRId64 ", %" PRId64 "]",
2442
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2443
             minKey, maxKey);
2444
      return code;
×
2445
    }
2446

2447
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
45,012!
2448
      code = TSDB_CODE_INVALID_MSG;
×
2449
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2450
             " is not in order, lastKey:%" PRId64,
2451
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2452
             pLastKey->ts);
2453
      return code;
×
2454
    } else if (pLastKey == NULL) {
45,014✔
2455
      pLastKey = &lastKey;
822✔
2456
    }
2457

2458
    *pLastKey = key;
45,014✔
2459
  }
2460

2461
  return code;
822✔
2462
}
2463

2464
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
13,481,641✔
2465
  int32_t code = 0;
13,481,641✔
2466

2467
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
13,481,641✔
2468
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
13,481,729✔
2469

2470
  if (numRows <= 0) {
13,481,729!
2471
    code = TSDB_CODE_INVALID_MSG;
×
2472
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2473
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2474
    return code;
×
2475
  }
2476

2477
  SRowKey *pLastKey = NULL;
13,481,746✔
2478
  SRowKey  lastKey = {0};
13,481,746✔
2479
  for (int32_t i = 0; i < numRows; ++i) {
871,884,307✔
2480
    SRow *pRow = aRow[i];
858,365,952✔
2481
    if (pRow->sver != pTbData->sver) {
858,365,952!
2482
      code = TSDB_CODE_INVALID_MSG;
×
2483
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2484
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2485
             pTbData->sver);
2486
      return code;
×
2487
    }
2488

2489
    SRowKey key = {0};
858,365,952✔
2490
    tRowGetKey(pRow, &key);
1,716,731,904✔
2491
    if (key.ts < minKey || key.ts > maxKey) {
858,368,908!
2492
      code = TSDB_CODE_INVALID_MSG;
×
2493
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2494
             " is out of range [%" PRId64 ", %" PRId64 "]",
2495
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2496
             minKey, maxKey);
2497
      return code;
×
2498
    }
2499

2500
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
858,398,948!
2501
      code = TSDB_CODE_INVALID_MSG;
×
2502
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2503
             " is not in order, lastKey:%" PRId64,
2504
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2505
             pLastKey->ts);
2506
      return code;
×
2507
    } else if (pLastKey == NULL) {
858,402,561✔
2508
      pLastKey = &lastKey;
13,481,655✔
2509
    }
2510

2511
    *pLastKey = key;
858,402,561✔
2512
  }
2513

2514
  return code;
13,518,355✔
2515
}
2516

2517
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
13,407,160✔
2518
  int32_t code = TSDB_CODE_SUCCESS;
13,407,160✔
2519
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
13,407,160✔
2520

2521
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
13,407,220✔
2522
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
13,407,273✔
2523
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
13,407,273✔
2524
  for (int32_t i = 0; i < numTbData; i++) {
26,889,843✔
2525
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
13,482,533✔
2526

2527
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
13,482,520!
2528
      code = TSDB_CODE_INVALID_MSG;
×
2529
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2530
             tstrerror(code), version);
2531
      return code;
×
2532
    }
2533

2534
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,482,520✔
2535
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
821✔
2536
      if (code) {
821!
2537
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2538
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2539
        return code;
×
2540
      }
2541
    } else {
2542
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
13,481,699✔
2543
      if (code) {
13,481,731!
2544
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2545
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2546
        return code;
×
2547
      }
2548
    }
2549
  }
2550

2551
  return code;
13,407,310✔
2552
}
2553

2554
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
13,407,188✔
2555
                                     SRpcMsg *pOriginalMsg) {
2556
  int32_t code = 0;
13,407,188✔
2557
  int32_t lino = 0;
13,407,188✔
2558
  terrno = 0;
13,407,188✔
2559

2560
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
13,407,295✔
2561
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
13,407,295✔
2562
  int32_t      ret;
2563
  SEncoder     ec = {0};
13,407,295✔
2564

2565
  pRsp->code = TSDB_CODE_SUCCESS;
13,407,295✔
2566

2567
  void           *pAllocMsg = NULL;
13,407,295✔
2568
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
13,407,295✔
2569
  SDecoder        dc = {0};
13,407,295✔
2570
  if (0 == taosHton64(pMsg->version)) {
13,407,295!
2571
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2572
    if (TSDB_CODE_SUCCESS == code) {
×
2573
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2574
    }
2575
    if (TSDB_CODE_SUCCESS == code) {
×
2576
      pAllocMsg = pReq;
×
2577
    }
2578
    TSDB_CHECK_CODE(code, lino, _exit);
×
2579
  } else {
2580
    // decode
2581
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
13,407,200✔
2582
    len -= sizeof(SSubmitReq2Msg);
13,407,200✔
2583

2584
    tDecoderInit(&dc, pReq, len);
13,407,200✔
2585
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
13,407,166!
2586
      code = TSDB_CODE_INVALID_MSG;
×
2587
      TSDB_CHECK_CODE(code, lino, _exit);
×
2588
    }
2589
  }
2590

2591
  // Scan the request
2592
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
13,407,273✔
2593
  if (code) {
13,407,224!
2594
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2595
           tstrerror(code), ver);
2596
    TSDB_CHECK_CODE(code, lino, _exit);
×
2597
  }
2598

2599
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
13,407,224!
2600
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2601

2602
  // Handle auto create table
2603
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
13,407,231✔
2604
  if (code) {
13,407,124✔
2605
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
117!
2606
           tstrerror(code), ver);
2607
    TSDB_CHECK_CODE(code, lino, _exit);
117!
2608
  }
2609

2610
  // Handle data write
2611
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
13,407,007✔
2612
  if (code) {
13,407,188✔
2613
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
7!
2614
           tstrerror(code), ver);
2615
    TSDB_CHECK_CODE(code, lino, _exit);
7!
2616
  }
2617

2618
_exit:
13,407,181✔
2619
  // message
2620
  pRsp->code = code;
13,407,305✔
2621
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
13,407,305!
2622
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
13,407,274✔
2623
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
13,407,313✔
2624
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
13,407,325!
2625
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2626
  }
2627
  tEncoderClear(&ec);
13,407,320✔
2628

2629
  // update statistics
2630
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
13,407,293✔
2631
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
13,407,353✔
2632
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
13,407,348✔
2633

2634
  // update metrics
2635
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
13,407,352!
2636
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
13,407,352!
2637
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
13,407,352!
2638

2639
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
13,407,352!
2640
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2641
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2642
                                   pVnode->monitor.strClusterId,
×
2643
                                   pVnode->monitor.strDnodeId,
×
2644
                                   tsLocalEp,
2645
                                   pVnode->monitor.strVgId,
×
2646
                                   pOriginalMsg->info.conn.user,
×
2647
                                   "Success"};
2648
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2649
  }
2650

2651
  if (code == 0) {
13,407,352✔
2652
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
13,407,174✔
2653
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
13,407,216✔
2654
  }
2655

2656
  // clear
2657
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
13,407,379!
2658
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
13,407,341✔
2659

2660
  if (code) {
13,407,270✔
2661
    terrno = code;
124✔
2662
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
124✔
2663
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
109!
2664
            tstrerror(code), lino, ver);
2665

2666
    } else {
2667
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
15!
2668
             tstrerror(code), lino, ver);
2669
    }
2670
  }
2671

2672
  taosMemoryFree(pAllocMsg);
13,407,219!
2673
  tDecoderClear(&dc);
13,407,252✔
2674

2675
  return code;
13,407,351✔
2676
}
2677

2678
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
74✔
2679
  int32_t code = TSDB_CODE_SUCCESS;
74✔
2680

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

2684
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2685
  code = metaTrimTables(pVnode->pMeta, ver);
74✔
2686

2687
  return code;
74✔
2688
}
2689

2690
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
8,323✔
2691
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
8,323!
2692
  int32_t code = TSDB_CODE_SUCCESS;
8,323✔
2693
  if (!pVnode->config.hashChange) {
8,323✔
2694
    goto _exit;
8,249✔
2695
  }
2696

2697
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
74✔
2698
  if (code < 0) {
74!
2699
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2700
    goto _exit;
×
2701
  }
2702
  pVnode->config.hashChange = false;
74✔
2703

2704
_exit:
8,323✔
2705
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
8,323✔
2706
  pRsp->code = code;
8,323✔
2707
  pRsp->pCont = NULL;
8,323✔
2708
  pRsp->contLen = 0;
8,323✔
2709

2710
  return code;
8,323✔
2711
}
2712

2713
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2714
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2715

2716
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
897✔
2717
  bool walChanged = false;
897✔
2718
  bool tsdbChanged = false;
897✔
2719

2720
  SAlterVnodeConfigReq req = {0};
897✔
2721
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
897!
2722
    terrno = TSDB_CODE_INVALID_MSG;
×
2723
    return TSDB_CODE_INVALID_MSG;
×
2724
  }
2725

2726
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
897!
2727
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2728
        "ssCompact:%d fsync:%d level:%d "
2729
        "walRetentionPeriod:%d walRetentionSize:%d",
2730
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2731
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2732
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2733
        req.walRetentionSize);
2734

2735
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
897✔
2736
    pVnode->config.cacheLastSize = req.cacheLastSize;
348✔
2737
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
348✔
2738
  }
2739

2740
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
897✔
2741
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
14!
2742
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2743
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
14✔
2744
  }
2745

2746
  if (pVnode->config.szCache != req.pages) {
897✔
2747
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
6!
2748
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2749
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2750
      return terrno;
×
2751
    } else {
2752
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
6!
2753
      pVnode->config.szCache = req.pages;
6✔
2754
    }
2755
  }
2756

2757
  if (pVnode->config.cacheLast != req.cacheLast) {
897✔
2758
    pVnode->config.cacheLast = req.cacheLast;
529✔
2759
  }
2760

2761
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
897✔
2762
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
72✔
2763
    walChanged = true;
72✔
2764
  }
2765

2766
  if (pVnode->config.walCfg.level != req.walLevel) {
897✔
2767
    if (pVnode->config.walCfg.level == 0) {
50!
2768
      pVnode->config.walCfg.clearFiles = 1;
×
2769
    }
2770
    pVnode->config.walCfg.level = req.walLevel;
50✔
2771
    walChanged = true;
50✔
2772
  }
2773

2774
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
897✔
2775
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
124✔
2776
    walChanged = true;
124✔
2777
  }
2778

2779
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
897✔
2780
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
2✔
2781
    walChanged = true;
2✔
2782
  }
2783

2784
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
897✔
2785
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
58✔
2786
    if (!VND_IS_RSMA(pVnode)) {
58!
2787
      tsdbChanged = true;
58✔
2788
    }
2789
  }
2790

2791
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
897✔
2792
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
58✔
2793
    if (!VND_IS_RSMA(pVnode)) {
58!
2794
      tsdbChanged = true;
58✔
2795
    }
2796
  }
2797

2798
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
897✔
2799
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
58✔
2800
    if (!VND_IS_RSMA(pVnode)) {
58!
2801
      tsdbChanged = true;
58✔
2802
    }
2803
  }
2804

2805
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
897✔
2806
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
10✔
2807
    if (!VND_IS_RSMA(pVnode)) {
10!
2808
      tsdbChanged = true;
10✔
2809
    }
2810
  }
2811

2812
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
897!
2813
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
4!
2814
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2815
    } else {
2816
      vnodeAWait(&pVnode->commitTask);
2✔
2817

2818
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
2✔
2819
      if (ret != 0) {
2!
2820
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2821
      }
2822

2823
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2824
      tsdbEnableBgTask(pVnode->pTsdb);
2✔
2825
    }
2826
  }
2827

2828
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
897!
2829
    pVnode->config.tsdbCfg.minRows = req.minRows;
2✔
2830
  }
2831

2832
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
897!
2833
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
2✔
2834
  }
2835
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
897!
2836
    pVnode->config.ssCompact = req.ssCompact;
2✔
2837
  }
2838

2839
  if (walChanged) {
897✔
2840
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
244!
2841
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2842
    }
2843
  }
2844

2845
  if (tsdbChanged) {
897✔
2846
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
70✔
2847
  }
2848

2849
  return 0;
897✔
2850
}
2851

2852
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2853
  SBatchDeleteReq deleteReq;
2854
  SDecoder        decoder;
2855
  tDecoderInit(&decoder, pReq, len);
×
2856
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2857
    tDecoderClear(&decoder);
×
2858
    return terrno = TSDB_CODE_INVALID_MSG;
×
2859
  }
2860

2861
  SMetaReader mr = {0};
×
2862
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2863
  STsdb *pTsdb = pVnode->pTsdb;
×
2864

2865
  if (deleteReq.level) {
×
2866
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2867
  }
2868

2869
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2870
  for (int32_t i = 0; i < sz; i++) {
×
2871
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2872
    char             *name = pOneReq->tbname;
×
2873
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2874
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2875
      continue;
×
2876
    }
2877

2878
    int64_t uid = mr.me.uid;
×
2879

2880
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2881
    if (code < 0) {
×
2882
      terrno = code;
×
2883
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2884
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2885
    }
2886

2887
    if (deleteReq.level == 0) {
×
2888
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2889
      if (code < 0) {
×
2890
        terrno = code;
×
2891
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2892
               ", end ts:%" PRId64,
2893
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2894
      }
2895
    }
2896
    tDecoderClear(&mr.coder);
×
2897
  }
2898
  metaReaderClear(&mr);
×
2899
  taosArrayDestroy(deleteReq.deleteReqs);
×
2900
  return 0;
×
2901
}
2902

2903
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
64,678✔
2904
                                     SRpcMsg *pOriginalMsg) {
2905
  int32_t     code = 0;
64,678✔
2906
  SDecoder   *pCoder = &(SDecoder){0};
64,678✔
2907
  SDeleteRes *pRes = &(SDeleteRes){0};
64,678✔
2908

2909
  pRsp->msgType = TDMT_VND_DELETE_RSP;
64,678✔
2910
  pRsp->pCont = NULL;
64,678✔
2911
  pRsp->contLen = 0;
64,678✔
2912
  pRsp->code = TSDB_CODE_SUCCESS;
64,678✔
2913

2914
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
64,678✔
2915
  if (pRes->uidList == NULL) {
64,686!
2916
    code = terrno;
×
2917
    goto _err;
×
2918
  }
2919

2920
  tDecoderInit(pCoder, pReq, len);
64,686✔
2921
  code = tDecodeDeleteRes(pCoder, pRes);
64,677✔
2922
  if (code) goto _err;
64,691!
2923

2924
  if (pRes->affectedRows > 0) {
64,691✔
2925
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
126,621✔
2926
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
65,479✔
2927
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
65,480✔
2928
      if (code) goto _err;
65,477!
2929
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
65,477✔
2930
      if (code) goto _err;
65,479!
2931
    }
2932
  }
2933

2934
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
64,688✔
2935

2936
  tDecoderClear(pCoder);
64,683✔
2937
  taosArrayDestroy(pRes->uidList);
64,683✔
2938

2939
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
64,686✔
2940
  int32_t     ret = 0;
64,686✔
2941
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
64,686!
2942
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
64,683✔
2943
  SEncoder ec = {0};
64,688✔
2944
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
64,688✔
2945
  code = tEncodeSVDeleteRsp(&ec, &rsp);
64,691✔
2946
  if (code) goto _err;
64,692!
2947
  tEncoderClear(&ec);
64,692✔
2948
  return code;
64,692✔
2949

2950
_err:
×
2951
  /*
2952
  if(code == TSDB_CODE_SUCCESS){
2953
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2954
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2955
                                        pOriginalMsg->info.conn.user, "Success"};
2956
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2957
  }
2958
  else{
2959
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2960
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2961
                                        pOriginalMsg->info.conn.user, "Failed"};
2962
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2963
  }
2964
  */
2965

2966
  return code;
×
2967
}
2968
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
949✔
2969
  SVCreateStbReq req = {0};
949✔
2970
  SDecoder       dc = {0};
949✔
2971
  int32_t        code = 0;
949✔
2972

2973
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
949✔
2974
  pRsp->code = TSDB_CODE_SUCCESS;
949✔
2975
  pRsp->pCont = NULL;
949✔
2976
  pRsp->contLen = 0;
949✔
2977

2978
  tDecoderInit(&dc, pReq, len);
949✔
2979
  // decode req
2980
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
949!
2981
    tDecoderClear(&dc);
×
2982
    return terrno = TSDB_CODE_INVALID_MSG;
×
2983
  }
2984

2985
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
949✔
2986
  if (code) {
949!
2987
    pRsp->code = code;
×
2988
    goto _err;
×
2989
  }
2990
  tDecoderClear(&dc);
949✔
2991
  return 0;
949✔
2992

2993
_err:
×
2994
  tDecoderClear(&dc);
×
2995
  return code;
×
2996
}
2997
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,132✔
2998
  SDropIndexReq req = {0};
2,132✔
2999
  int32_t       code = 0;
2,132✔
3000
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,132✔
3001
  pRsp->code = TSDB_CODE_SUCCESS;
2,132✔
3002
  pRsp->pCont = NULL;
2,132✔
3003
  pRsp->contLen = 0;
2,132✔
3004

3005
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,132!
3006
    pRsp->code = code;
×
3007
    return code;
×
3008
  }
3009

3010
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,132✔
3011
  if (code) {
2,132!
3012
    pRsp->code = code;
×
3013
    return code;
×
3014
  }
3015
  return TSDB_CODE_SUCCESS;
2,132✔
3016
}
3017

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

3020
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
173✔
3021
  if (!pVnode->restored) {
173✔
3022
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
46!
3023
    return 0;
46✔
3024
  }
3025
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
127✔
3026
}
3027

3028
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3029
  if (syncCheckMember(pVnode->sync) != 0) {
×
3030
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3031
  }
3032

3033
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3034
  pRsp->code = TSDB_CODE_SUCCESS;
×
3035
  pRsp->pCont = NULL;
×
3036
  pRsp->contLen = 0;
×
3037

3038
  return 0;
×
3039
}
3040

3041
static int32_t vnodeCheckState(SVnode *pVnode) {
60✔
3042
  SSyncState syncState = syncGetState(pVnode->sync);
60✔
3043
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
60✔
3044
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
34✔
3045
  }
3046
  return 0;
26✔
3047
}
3048

3049
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
26✔
3050
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
26✔
3051
  if (vnodeGetArbToken(pVnode, token) != 0) {
26!
3052
    return terrno = TSDB_CODE_NOT_FOUND;
×
3053
  }
3054

3055
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
26✔
3056
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
22✔
3057
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
16✔
3058
  }
3059

3060
  terrno = TSDB_CODE_SUCCESS;
10✔
3061
  return 0;
10✔
3062
}
3063

3064
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
5✔
3065
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
5✔
3066
  if (code != 0) {
5!
3067
    return code;
×
3068
  }
3069

3070
  return syncCheckSynced(pVnode->sync);
5✔
3071
}
3072

3073
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
16✔
3074
  int32_t code = 0;
16✔
3075

3076
  if ((code = vnodeCheckState(pVnode)) != 0) {
16✔
3077
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
11!
3078
    return 0;
11✔
3079
  }
3080

3081
  SVArbCheckSyncReq syncReq = {0};
5✔
3082

3083
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
5✔
3084
  if (code) {
5!
3085
    return code;
×
3086
  }
3087

3088
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
5!
3089
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3090
        "arbTerm:%" PRId64,
3091
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3092
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3093

3094
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
5✔
3095
  pRsp->code = TSDB_CODE_SUCCESS;
5✔
3096
  pRsp->pCont = NULL;
5✔
3097
  pRsp->contLen = 0;
5✔
3098

3099
  SVArbCheckSyncRsp syncRsp = {0};
5✔
3100
  syncRsp.arbToken = syncReq.arbToken;
5✔
3101
  syncRsp.member0Token = syncReq.member0Token;
5✔
3102
  syncRsp.member1Token = syncReq.member1Token;
5✔
3103
  syncRsp.vgId = TD_VID(pVnode);
5✔
3104

3105
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
5!
3106
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3107
  }
3108

3109
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
5!
3110
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3111
    goto _OVER;
×
3112
  }
3113

3114
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
5✔
3115
  if (contLen <= 0) {
5!
3116
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3117
    code = -1;
×
3118
    goto _OVER;
×
3119
  }
3120
  void *pHead = rpcMallocCont(contLen);
5✔
3121
  if (!pHead) {
5!
3122
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3123
    code = -1;
×
3124
    goto _OVER;
×
3125
  }
3126

3127
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
5!
3128
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3129
    rpcFreeCont(pHead);
×
3130
    code = -1;
×
3131
    goto _OVER;
×
3132
  }
3133

3134
  pRsp->pCont = pHead;
5✔
3135
  pRsp->contLen = contLen;
5✔
3136

3137
  vInfo(
5!
3138
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3139
      "member1Token:%s",
3140
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3141

3142
  code = TSDB_CODE_SUCCESS;
5✔
3143

3144
_OVER:
5✔
3145
  if (code != 0) {
5!
3146
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3147
           tstrerror(code));
3148
  }
3149
  tFreeSVArbCheckSyncReq(&syncReq);
5✔
3150
  return code;
5✔
3151
}
3152

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