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

taosdata / TDengine / #4523

17 Jul 2025 02:02AM UTC coverage: 56.768% (+0.3%) from 56.447%
#4523

push

travis-ci

web-flow
Merge pull request #31914 from taosdata/fix/3.0/compare-ans-failed

fix:Convert line endings from LF to CRLF for ans file

140094 of 313745 branches covered (44.65%)

Branch coverage included in aggregate %.

212455 of 307292 relevant lines covered (69.14%)

18276193.53 hits per line

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

53.31
/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);
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 vnodeProcessSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
49
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
50
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
51
                                     SRpcMsg *pOriginalMsg);
52
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
53
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
54
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
55
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
56
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
57
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
58
static int32_t vnodeProcessDropTSmaCtbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
59
                                          SRpcMsg *pOriginRpc);
60

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

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

69
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
183,146✔
70
  int32_t code = 0;
183,146✔
71
  int32_t lino = 0;
183,146✔
72

73
  if (tStartDecode(pCoder) < 0) {
183,146!
74
    code = TSDB_CODE_INVALID_MSG;
×
75
    TSDB_CHECK_CODE(code, lino, _exit);
×
76
  }
77

78
  // flags
79
  if (tDecodeI32v(pCoder, NULL) < 0) {
183,306!
80
    code = TSDB_CODE_INVALID_MSG;
×
81
    TSDB_CHECK_CODE(code, lino, _exit);
×
82
  }
83

84
  // name
85
  char *name = NULL;
183,306✔
86
  if (tDecodeCStr(pCoder, &name) < 0) {
183,126!
87
    code = TSDB_CODE_INVALID_MSG;
×
88
    TSDB_CHECK_CODE(code, lino, _exit);
×
89
  }
90

91
  // uid
92
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
183,126✔
93
  if (uid == 0) {
183,354✔
94
    uid = tGenIdPI64();
176,484✔
95
  }
96
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
183,301✔
97

98
  // btime
99
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
183,301✔
100

101
  tEndDecode(pCoder);
183,301✔
102

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

116
  int64_t  btime = taosGetTimestampMs();
77,732✔
117
  SDecoder dc = {0};
77,732✔
118
  int32_t  nReqs;
119

120
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
77,732✔
121
  if (tStartDecode(&dc) < 0) {
77,714!
122
    code = TSDB_CODE_INVALID_MSG;
×
123
    return code;
×
124
  }
125

126
  if (tDecodeI32v(&dc, &nReqs) < 0) {
77,729!
127
    code = TSDB_CODE_INVALID_MSG;
×
128
    TSDB_CHECK_CODE(code, lino, _exit);
×
129
  }
130
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
181,854✔
131
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
104,165✔
132
    TSDB_CHECK_CODE(code, lino, _exit);
104,125!
133
  }
134

135
  tEndDecode(&dc);
77,689✔
136

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

146
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,532✔
147
  int32_t code = TSDB_CODE_INVALID_MSG;
10,532✔
148
  int32_t lino = 0;
10,532✔
149

150
  SDecoder dc = {0};
10,532✔
151
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
10,532✔
152

153
  SVAlterTbReq vAlterTbReq = {0};
10,532✔
154
  int64_t      ctimeMs = taosGetTimestampMs();
10,532✔
155
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
10,532!
156
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
157
    vAlterTbReq.pMultiTag = NULL;
×
158
    goto _exit;
×
159
  }
160
  taosArrayDestroy(vAlterTbReq.pMultiTag);
10,532✔
161
  vAlterTbReq.pMultiTag = NULL;
10,532✔
162

163
  code = 0;
10,532✔
164

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

176
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
61,718✔
177
  int32_t code = TSDB_CODE_INVALID_MSG;
61,718✔
178
  int32_t lino = 0;
61,718✔
179

180
  SMsgHead *pContOld = pMsg->pCont;
61,718✔
181
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
61,718✔
182

183
  SArray *tbUids = NULL;
61,718✔
184
  int64_t timestampMs = 0;
61,718✔
185

186
  SVDropTtlTableReq ttlReq = {0};
61,718✔
187
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
61,718!
188
    code = TSDB_CODE_INVALID_MSG;
×
189
    TSDB_CHECK_CODE(code, lino, _exit);
×
190
  }
191

192
  {  // find expired uids
193
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
61,862✔
194
    if (tbUids == NULL) {
61,898!
195
      code = terrno;
×
196
      TSDB_CHECK_CODE(code, lino, _exit);
×
197
    }
198

199
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
61,898✔
200
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
61,898✔
201
    if (code != 0) {
61,769!
202
      code = TSDB_CODE_INVALID_MSG;
×
203
      TSDB_CHECK_CODE(code, lino, _exit);
×
204
    }
205

206
    ttlReq.nUids = taosArrayGetSize(tbUids);
61,769✔
207
    ttlReq.pTbUids = tbUids;
61,688✔
208
  }
209

210
  if (ttlReq.nUids == 0) {
61,688!
211
    code = TSDB_CODE_MSG_PREPROCESSED;
61,688✔
212
    TSDB_CHECK_CODE(code, lino, _exit);
61,688!
213
  }
214

215
  {  // prepare new content
216
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
217
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
33✔
218

219
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
33✔
220
    if (pContNew == NULL) {
33!
221
      code = terrno;
×
222
      TSDB_CHECK_CODE(code, lino, _exit);
×
223
    }
224

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

231
    rpcFreeCont(pContOld);
33✔
232
    pMsg->pCont = pContNew;
33✔
233
    pMsg->contLen = contLenNew;
33✔
234
  }
235

236
  code = 0;
33✔
237

238
_exit:
61,740✔
239
  taosArrayDestroy(tbUids);
61,740✔
240

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

248
  return code;
61,751✔
249
}
250

251
extern int64_t tsMaxKeyByPrecision[];
252
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
11,387,887✔
253
  int32_t code = 0;
11,387,887✔
254
  int32_t lino = 0;
11,387,887✔
255

256
  if (tStartDecode(pCoder) < 0) {
11,387,887!
257
    code = TSDB_CODE_INVALID_MSG;
×
258
    TSDB_CHECK_CODE(code, lino, _exit);
×
259
  }
260

261
  SSubmitTbData submitTbData;
262
  uint8_t       version;
263
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
11,388,486!
264
    code = TSDB_CODE_INVALID_MSG;
×
265
    TSDB_CHECK_CODE(code, lino, _exit);
×
266
  }
267
  version = (submitTbData.flags >> 8) & 0xff;
11,388,486✔
268
  submitTbData.flags = submitTbData.flags & 0xff;
11,388,486✔
269

270
  int64_t uid;
271
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
11,388,486✔
272
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
78,985✔
273
    TSDB_CHECK_CODE(code, lino, _exit);
79,112!
274
  }
275

276
  // submit data
277
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
11,388,581!
278
    code = TSDB_CODE_INVALID_MSG;
×
279
    TSDB_CHECK_CODE(code, lino, _exit);
×
280
  }
281

282
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
11,388,581✔
283
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
79,105✔
284
    pCoder->pos += sizeof(int64_t);
79,105✔
285
  } else {
286
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
11,309,438!
287
      code = TSDB_CODE_INVALID_MSG;
×
288
      TSDB_CHECK_CODE(code, lino, _exit);
×
289
    }
290
  }
291

292
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
11,388,476!
293
    code = TSDB_CODE_INVALID_MSG;
×
294
    TSDB_CHECK_CODE(code, lino, _exit);
×
295
  }
296

297
  // scan and check
298
  TSKEY now = btimeMs;
11,388,476✔
299
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
11,388,476✔
300
    now *= 1000;
23,365✔
301
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
11,365,111✔
302
    now *= 1000000;
4,631✔
303
  }
304

305
  int32_t keep = pVnode->config.tsdbCfg.keep2;
11,388,476✔
306
  /*
307
  int32_t nlevel = tfsGetLevel(pVnode->pTfs);
308
  if (nlevel > 1 && tsSsEnabled) {
309
    if (nlevel == 3) {
310
      keep = pVnode->config.tsdbCfg.keep1;
311
    } else if (nlevel == 2) {
312
      keep = pVnode->config.tsdbCfg.keep0;
313
    }
314
  }
315
  */
316

317
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
11,388,476✔
318
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
11,388,476✔
319
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
11,388,476✔
320
    uint64_t nColData;
321
    if (tDecodeU64v(pCoder, &nColData) < 0) {
10,144!
322
      code = TSDB_CODE_INVALID_MSG;
×
323
      TSDB_CHECK_CODE(code, lino, _exit);
1!
324
    }
325

326
    SColData colData = {0};
10,144✔
327
    code = tDecodeColData(version, pCoder, &colData);
10,144✔
328
    if (code) {
10,140!
329
      code = TSDB_CODE_INVALID_MSG;
×
330
      TSDB_CHECK_CODE(code, lino, _exit);
×
331
    }
332

333
    if (colData.flag != HAS_VALUE) {
10,140!
334
      code = TSDB_CODE_INVALID_MSG;
×
335
      TSDB_CHECK_CODE(code, lino, _exit);
×
336
    }
337

338
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
9,974,776✔
339
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
9,964,637!
340
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
562✔
341
        TSDB_CHECK_CODE(code, lino, _exit);
562✔
342
      }
343
    }
344

345
    for (uint64_t i = 1; i < nColData; i++) {
30,639✔
346
      code = tDecodeColData(version, pCoder, &colData);
20,502✔
347
      if (code) {
20,500!
348
        code = TSDB_CODE_INVALID_MSG;
×
349
        TSDB_CHECK_CODE(code, lino, _exit);
×
350
      }
351
    }
352
  } else {
353
    uint64_t nRow;
354
    if (tDecodeU64v(pCoder, &nRow) < 0) {
11,378,188!
355
      code = TSDB_CODE_INVALID_MSG;
×
356
      TSDB_CHECK_CODE(code, lino, _exit);
579!
357
    }
358

359
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
723,051,123✔
360
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
711,673,514✔
361
      pCoder->pos += pRow->len;
711,673,514✔
362
#ifndef NO_UNALIGNED_ACCESS
363
      if (pRow->ts < minKey || pRow->ts > maxKey) {
711,673,514!
364
#else
365
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
366
      if (ts < minKey || ts > maxKey) {
367
#endif
368
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
369
        TSDB_CHECK_CODE(code, lino, _exit);
×
370
      }
371

372
      // Check pRow->sver
373
      if (pRow->sver != submitTbData.sver) {
711,672,935!
374
        code = TSDB_CODE_INVALID_DATA_FMT;
×
375
        TSDB_CHECK_CODE(code, lino, _exit);
×
376
      }
377
    }
378
  }
379

380
  if (!tDecodeIsEnd(pCoder)) {
11,387,746!
381
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
11,387,853✔
382
    pCoder->pos += sizeof(int64_t);
11,387,853✔
383
  }
384

385
  tEndDecode(pCoder);
11,387,746✔
386

387
_exit:
11,387,840✔
388
  if (code) {
11,387,840✔
389
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
580!
390
           lino, tstrerror(code));
391
  }
392
  return code;
11,387,845✔
393
}
394
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,676,456✔
395
  int32_t code = 0;
10,676,456✔
396
  int32_t lino = 0;
10,676,456✔
397

398
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
10,676,456!
399
    return TSDB_CODE_MSG_PREPROCESSED;
×
400
  }
401

402
  SDecoder *pCoder = &(SDecoder){0};
10,676,456✔
403

404
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
10,676,456!
405
    code = TSDB_CODE_INVALID_MSG;
×
406
    TSDB_CHECK_CODE(code, lino, _exit);
×
407
  }
408

409
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
10,676,511✔
410

411
  if (tStartDecode(pCoder) < 0) {
10,676,435!
412
    code = TSDB_CODE_INVALID_MSG;
×
413
    TSDB_CHECK_CODE(code, lino, _exit);
×
414
  }
415

416
  uint64_t nSubmitTbData;
417
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
10,676,523!
418
    code = TSDB_CODE_INVALID_MSG;
×
419
    TSDB_CHECK_CODE(code, lino, _exit);
×
420
  }
421

422
  int64_t btimeMs = taosGetTimestampMs();
10,676,470✔
423
  int64_t ctimeMs = btimeMs;
10,676,470✔
424
  for (int32_t i = 0; i < nSubmitTbData; i++) {
22,063,741✔
425
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
11,387,935✔
426
    TSDB_CHECK_CODE(code, lino, _exit);
11,387,851✔
427
  }
428

429
  tEndDecode(pCoder);
10,675,806✔
430

431
_exit:
10,676,467✔
432
  tDecoderClear(pCoder);
10,676,467✔
433
  if (code) {
10,676,478✔
434
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
580!
435
           tstrerror(code), TMSG_INFO(pMsg->msgType));
436
  }
437
  return code;
10,676,457✔
438
}
439

440
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
61,468✔
441
  int32_t code = 0;
61,468✔
442

443
  int32_t    size;
444
  int32_t    ret;
445
  uint8_t   *pCont;
446
  SEncoder  *pCoder = &(SEncoder){0};
61,468✔
447
  SDeleteRes res = {0};
61,468✔
448

449
  SReadHandle handle = {0};
61,468✔
450
  handle.vnode = pVnode;
61,468✔
451
  handle.pMsgCb = &pVnode->msgCb;
61,468✔
452
  handle.skipRollup = 1;
61,468✔
453
  initStorageAPI(&handle.api);
61,468✔
454

455
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
61,470✔
456
  if (code) goto _exit;
61,467!
457

458
  res.ctimeMs = taosGetTimestampMs();
61,466✔
459
  // malloc and encode
460
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
61,466!
461
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
61,465✔
462

463
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
61,470✔
464
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
61,470✔
465

466
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
61,470✔
467
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
61,470!
468
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
469
  }
470
  tEncoderClear(pCoder);
61,470✔
471

472
  rpcFreeCont(pMsg->pCont);
61,468✔
473
  pMsg->pCont = pCont;
61,470✔
474
  pMsg->contLen = size + sizeof(SMsgHead);
61,470✔
475

476
  taosArrayDestroy(res.uidList);
61,470✔
477

478
_exit:
61,470✔
479
  return code;
61,470✔
480
}
481

482
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
483
  int32_t code = 0;
×
484
  int32_t lino = 0;
×
485

486
  int64_t         ctimeMs = taosGetTimestampMs();
×
487
  SBatchDeleteReq pReq = {0};
×
488
  SDecoder       *pCoder = &(SDecoder){0};
×
489

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

492
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
493
    code = TSDB_CODE_INVALID_MSG;
×
494
  }
495

496
  tDecoderClear(pCoder);
×
497
  taosArrayDestroy(pReq.deleteReqs);
×
498

499
  if (code) {
×
500
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
501
  } else {
502
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
×
503
  }
504
  return code;
×
505
}
506

507
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
32✔
508
  SVArbCheckSyncReq syncReq = {0};
32✔
509

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

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

520
  int32_t code = terrno;
32✔
521
  tFreeSVArbCheckSyncReq(&syncReq);
32✔
522

523
  return code;
32✔
524
}
525

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

535
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
16,524✔
536

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

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

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

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

581

582
int32_t vnodePreProcessSsMigrateReq(SVnode* pVnode, SRpcMsg* pMsg) {
×
583
  int32_t          code = TSDB_CODE_SUCCESS;
×
584
  int32_t          lino = 0;
×
585
  SSsMigrateVgroupReq req = {0};
×
586

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

593
  req.nodeId = vnodeNodeId(pVnode);
×
594
  tSerializeSSsMigrateVgroupReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead), &req);
×
595

596
_exit:
×
597
  return code;
×
598
}
599

600

601
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,984,422✔
602
  int32_t code = 0;
10,984,422✔
603

604
  if (pVnode->mounted) {
10,984,422!
605
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
×
606
           TMSG_INFO(pMsg->msgType));
607
    return TSDB_CODE_OPS_NOT_SUPPORT;
×
608
  }
609

610
  switch (pMsg->msgType) {
10,984,422!
611
    case TDMT_VND_CREATE_TABLE: {
77,719✔
612
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
77,719✔
613
    } break;
77,725✔
614
    case TDMT_VND_ALTER_TABLE: {
10,532✔
615
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
10,532✔
616
    } break;
10,532✔
617
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
61,793✔
618
    case TDMT_VND_DROP_TTL_TABLE: {
619
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
61,793✔
620
    } break;
61,710✔
621
    case TDMT_VND_SUBMIT: {
10,676,489✔
622
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
10,676,489!
623
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
624
    } break;
10,676,459✔
625
    case TDMT_VND_DELETE: {
61,468✔
626
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
61,468✔
627
    } break;
61,467✔
628
    case TDMT_VND_BATCH_DEL: {
×
629
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
630
    } break;
×
631
    case TDMT_VND_ARB_CHECK_SYNC: {
32✔
632
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
32✔
633
    } break;
32✔
634
    case TDMT_VND_DROP_TABLE: {
16,524✔
635
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
16,524✔
636
    } break;
16,524✔
637
    case TDMT_VND_SSMIGRATE: {
×
638
      code = vnodePreProcessSsMigrateReq(pVnode, pMsg);
×
639
    } break;
×
640
    default:
79,865✔
641
      break;
79,865✔
642
  }
643

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

651
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
14,891,787✔
652
  int32_t code = 0;
14,891,787✔
653
  int32_t lino = 0;
14,891,787✔
654
  void   *ptr = NULL;
14,891,787✔
655
  void   *pReq;
656
  int32_t len;
657

658
  (void)taosThreadMutexLock(&pVnode->mutex);
14,891,787✔
659
  if (pVnode->disableWrite) {
14,892,147!
660
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
661
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
662
    return TSDB_CODE_VND_WRITE_DISABLED;
×
663
  }
664
  (void)taosThreadMutexUnlock(&pVnode->mutex);
14,892,147✔
665

666
  if (ver <= pVnode->state.applied) {
14,892,062!
667
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
668
           pVnode->state.applied);
669
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
670
  }
671

672
  vGDebug(&pMsg->info.traceId,
14,892,062!
673
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
674
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
675
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
676
          pMsg->info.conn.applyTerm, pMsg->contLen);
677

678
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
14,891,911!
679
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
680
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
681
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
682
  }
683

684
  if (!(pVnode->state.applied + 1 == ver)) {
14,891,911!
685
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode), pVnode->config.mountVgId,
×
686
           pVnode->state.applied + 1, ver);
687
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
688
  }
689

690
  atomic_store_64(&pVnode->state.applied, ver);
14,891,911✔
691
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
14,892,174✔
692

693
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
14,892,151✔
694

695
  // skip header
696
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
14,851,006✔
697
  len = pMsg->contLen - sizeof(SMsgHead);
14,851,006✔
698
  bool needCommit = false;
14,851,006✔
699

700
  switch (pMsg->msgType) {
14,851,006!
701
    /* META */
702
    case TDMT_VND_CREATE_STB:
27,931✔
703
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
27,931✔
704
      TSDB_CHECK_CODE(code, lino, _err);
28,009✔
705
      break;
28,003✔
706
    case TDMT_VND_ALTER_STB:
11,462✔
707
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
11,462✔
708
      TSDB_CHECK_CODE(code, lino, _err);
11,481!
709
      break;
11,481✔
710
    case TDMT_VND_DROP_STB:
2,806✔
711
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp);
2,806✔
712
      TSDB_CHECK_CODE(code, lino, _err);
2,821!
713
      break;
2,821✔
714
    case TDMT_VND_CREATE_TABLE:
88,752✔
715
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
88,752✔
716
      TSDB_CHECK_CODE(code, lino, _err);
88,792!
717
      break;
88,792✔
718
    case TDMT_VND_ALTER_TABLE:
10,538✔
719
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
10,538✔
720
      TSDB_CHECK_CODE(code, lino, _err);
10,538!
721
      break;
10,538✔
722
    case TDMT_VND_DROP_TABLE:
16,566✔
723
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
16,566✔
724
      TSDB_CHECK_CODE(code, lino, _err);
16,566!
725
      break;
16,566✔
726
    case TDMT_VND_DROP_TTL_TABLE:
×
727
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
728
      TSDB_CHECK_CODE(code, lino, _err);
×
729
      break;
×
730
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
33✔
731
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
33✔
732
      TSDB_CHECK_CODE(code, lino, _err);
33!
733
      break;
33✔
734
    case TDMT_VND_TRIM:
120✔
735
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
120✔
736
      TSDB_CHECK_CODE(code, lino, _err);
120!
737
      break;
120✔
738
    case TDMT_VND_SSMIGRATE:
×
739
      if (vnodeProcessSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
740
      break;
×
741
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
742
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
743
      break;
×
744
      break;
745
    /* TSDB */
746
    case TDMT_VND_SUBMIT: {
14,564,348✔
747
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
14,564,348!
748
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
749
      });
750
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
14,564,381!
751
      break;
14,564,378✔
752
    }
753
    case TDMT_VND_DELETE:
63,669✔
754
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
63,669✔
755
      TSDB_CHECK_CODE(code, lino, _err);
63,668!
756
      break;
63,668✔
757
    case TDMT_VND_BATCH_DEL:
×
758
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
759
      TSDB_CHECK_CODE(code, lino, _err);
×
760
      break;
×
761
      /* TQ */
762
#if defined(USE_TQ) || defined(USE_STREAM)
763
    case TDMT_VND_TMQ_SUBSCRIBE:
4,386✔
764
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
4,386✔
765
      TSDB_CHECK_CODE(code, lino, _err);
4,390!
766
      break;
4,390✔
767
    case TDMT_VND_TMQ_DELETE_SUB:
960✔
768
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
960✔
769
      TSDB_CHECK_CODE(code, lino, _err);
963!
770
      break;
963✔
771
    case TDMT_VND_TMQ_COMMIT_OFFSET:
9,068✔
772
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
9,068✔
773
      TSDB_CHECK_CODE(code, lino, _err);
9,086!
774
      break;
9,086✔
775
    case TDMT_VND_TMQ_ADD_CHECKINFO:
123✔
776
      code = tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len);
123✔
777
      TSDB_CHECK_CODE(code, lino, _err);
123!
778
      break;
123✔
779
    case TDMT_VND_TMQ_DEL_CHECKINFO:
11✔
780
      code = tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len);
11✔
781
      TSDB_CHECK_CODE(code, lino, _err);
11!
782
      break;
11✔
783
#endif
784
    case TDMT_VND_ALTER_CONFIRM:
15,297✔
785
      needCommit = pVnode->config.hashChange;
15,297✔
786
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
15,297✔
787
      TSDB_CHECK_CODE(code, lino, _err);
15,297!
788
      break;
15,297✔
789
    case TDMT_VND_ALTER_CONFIG:
1,122✔
790
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
1,122✔
791
      break;
1,124✔
792
    case TDMT_VND_COMMIT:
30,441✔
793
      needCommit = true;
30,441✔
794
      break;
30,441✔
795
    case TDMT_VND_CREATE_INDEX:
978✔
796
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
978✔
797
      break;
978✔
798
    case TDMT_VND_DROP_INDEX:
2,144✔
799
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,144✔
800
      break;
2,144✔
801
    case TDMT_VND_COMPACT:
240✔
802
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
240✔
803
      goto _exit;
241✔
804
    case TDMT_SYNC_CONFIG_CHANGE:
×
805
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
806
      break;
×
807
#ifdef TD_ENTERPRISE
808
    case TDMT_VND_KILL_COMPACT:
2✔
809
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
2✔
810
      break;
2✔
811
#endif
812
    /* ARB */
813
    case TDMT_VND_ARB_CHECK_SYNC:
9✔
814
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
9✔
815
      break;
9✔
816
    default:
×
817
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
818
      return TSDB_CODE_INVALID_MSG;
×
819
  }
820

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

823
  walApplyVer(pVnode->pWal, ver);
14,851,034✔
824

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

831
  // commit if need
832
  if (needCommit) {
14,850,952✔
833
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
30,508!
834
    code = vnodeAsyncCommit(pVnode);
30,583✔
835
    if (code) {
30,590!
836
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
837
      goto _err;
×
838
    }
839

840
    // start a new one
841
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
30,590!
842
      code = vnodeBegin(pVnode);
843
      if (code) {
844
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
845
        goto _err;
846
      }
847
    });
848
  }
849

850
_exit:
14,851,023✔
851
  return 0;
14,891,931✔
852

853
_err:
15✔
854
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
15✔
855
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
6!
856
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
857
  } else if (code) {
9!
858
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
859
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
860
  }
861

862
  return code;
15✔
863
}
864

865
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
8,253,363✔
866
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
8,253,363✔
867
    return 0;
445,961✔
868
  }
869

870
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
7,807,402✔
871
}
872

873
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
8,261,319✔
874
  vTrace("message in vnode query queue is processing");
8,261,319✔
875
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
8,261,319✔
876
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,298✔
877
    return 0;
1,298✔
878
  }
879

880
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
8,260,017!
881
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
882
    return 0;
×
883
  }
884

885
  SReadHandle handle = {0};
8,260,017✔
886
  handle.vnode = pVnode;
8,260,017✔
887
  handle.pMsgCb = &pVnode->msgCb;
8,260,017✔
888
  handle.pWorkerCb = pInfo->workerCb;
8,260,017✔
889
  initStorageAPI(&handle.api);
8,260,017✔
890
  int32_t code = TSDB_CODE_SUCCESS;
8,258,886✔
891
  bool    redirected = false;
8,258,886✔
892

893
  switch (pMsg->msgType) {
8,258,886!
894
    case TDMT_SCH_QUERY:
6,038,963✔
895
      if (!syncIsReadyForRead(pVnode->sync)) {
6,038,963✔
896
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
4,387!
897
        redirected = true;
4,387✔
898
      }
899
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
6,037,905✔
900
      if (redirected) {
6,037,584✔
901
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
4,441✔
902
        return 0;
4,387✔
903
      }
904

905
      return code;
6,033,143✔
906
    case TDMT_SCH_MERGE_QUERY:
1,775,653✔
907
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,775,653✔
908
    case TDMT_SCH_QUERY_CONTINUE:
375,355✔
909
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
375,355✔
910
    case TDMT_VND_TMQ_CONSUME:
53,031✔
911
      return tqProcessPollReq(pVnode->pTq, pMsg);
53,031✔
912
    case TDMT_VND_TMQ_CONSUME_PUSH:
16,264✔
913
      return tqProcessPollPush(pVnode->pTq);
16,264✔
914
    default:
×
915
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
916
      return TSDB_CODE_APP_ERROR;
×
917
  }
918
}
919

920
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
21,206,008✔
921
  int32_t code = TSDB_CODE_SUCCESS;
21,206,008✔
922
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
21,206,008✔
923
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
21,206,008✔
924
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
14,754,150!
925
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
13,592,207✔
926
      !syncIsReadyForRead(pVnode->sync)) {
7,619,649✔
927
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
1,984✔
928
    return 0;
1,984✔
929
  }
930

931
  switch (pMsg->msgType) {
21,209,549!
932
    case TDMT_SCH_FETCH:
9,902,294✔
933
    case TDMT_SCH_MERGE_FETCH:
934
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
9,902,294✔
935
    case TDMT_SCH_FETCH_RSP:
×
936
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
937
    // case TDMT_SCH_CANCEL_TASK:
938
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
939
    case TDMT_SCH_DROP_TASK:
7,801,158✔
940
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
7,801,158✔
941
    case TDMT_SCH_TASK_NOTIFY:
117✔
942
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
117✔
943
    case TDMT_SCH_QUERY_HEARTBEAT:
2,341,854✔
944
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
2,341,854✔
945
    case TDMT_VND_TABLE_META:
3,236✔
946
    case TDMT_VND_TABLE_NAME:
947
      return vnodeGetTableMeta(pVnode, pMsg, true);
3,236✔
948
    case TDMT_VND_TABLE_CFG:
×
949
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
950
    case TDMT_VND_BATCH_META: {
1,160,783✔
951
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
1,160,783!
952
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
953
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
1,161,480!
954
      return code;
1,161,485✔
955
    }
956
    case TDMT_VND_VSUBTABLES_META:
×
957
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
958
    case TDMT_VND_VSTB_REF_DBS:
×
959
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
960
#ifdef TD_ENTERPRISE
961
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
70✔
962
      return vnodeQueryCompactProgress(pVnode, pMsg);
70✔
963
#endif
964
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
965
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
966
      //    case TDMT_VND_TMQ_CONSUME:
967
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
968
#ifdef USE_TQ
969
    case TDMT_VND_TMQ_VG_WALINFO:
11✔
970
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
11✔
971
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
2✔
972
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
2✔
973
    case TDMT_VND_TMQ_SEEK:
24✔
974
      return tqProcessSeekReq(pVnode->pTq, pMsg);
24✔
975
#endif
976
    default:
×
977
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
978
      return TSDB_CODE_APP_ERROR;
×
979
  }
980
}
981

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

989
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
209,681✔
990
  if (NULL == pMetaRsp) {
209,681!
991
    return;
×
992
  }
993

994
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
209,681✔
995
  pMetaRsp->dbId = pVnode->config.dbId;
209,681✔
996
  pMetaRsp->vgId = TD_VID(pVnode);
209,681✔
997
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
209,681✔
998
}
999

1000
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1001

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

1008
  int32_t     code = 0;
114✔
1009
  SVTrimDbReq trimReq = {0};
114✔
1010

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

1017
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
114!
1018

1019
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
114✔
1020

1021
_exit:
114✔
1022
  return code;
114✔
1023
}
1024

1025
extern int32_t vnodeAsyncSsMigrate(SVnode *pVnode, SSsMigrateVgroupReq *pReq);
1026

1027
static int32_t vnodeProcessSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1028
  int32_t          code = 0;
×
1029
  SSsMigrateVgroupReq req = {0};
×
1030
  SSsMigrateVgroupRsp rsp = {0};
×
1031
  pRsp->msgType = TDMT_VND_SSMIGRATE_RSP;
×
1032
  pRsp->code = 0;
×
1033
  pRsp->pCont = NULL;
×
1034
  pRsp->contLen = 0;
×
1035

1036
  if (tDeserializeSSsMigrateVgroupReq(pReq, len, &req) != 0) {
×
1037
    code = TSDB_CODE_INVALID_MSG;
×
1038
    goto _exit;
×
1039
  }
1040

1041
  vInfo("vgId:%d, process ssmigrate vnode request, time:%" PRId64, pVnode->config.vgId, req.timestamp);
×
1042

1043
  code = vnodeAsyncSsMigrate(pVnode, &req);
×
1044
  if (code != TSDB_CODE_SUCCESS) {
×
1045
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1046
    pRsp->code = code;
×
1047
    goto _exit;
×
1048
  }
1049

1050
  rsp.ssMigrateId = req.ssMigrateId;
×
1051
  rsp.vgId = TD_VID(pVnode);
×
1052
  rsp.nodeId = req.nodeId;
×
1053

1054
  pRsp->code = TSDB_CODE_SUCCESS;
×
1055
  pRsp->contLen = tSerializeSSsMigrateVgroupRsp(NULL, 0, &rsp);
×
1056
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1057
  if (pRsp->pCont == NULL) {
×
1058
    vError("vgId:%d, failed to allocate memory for ssmigrate response", TD_VID(pVnode));
×
1059
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1060
    goto _exit;
×
1061
  }
1062
  tSerializeSSsMigrateVgroupRsp(pRsp->pCont, pRsp->contLen, &rsp);
×
1063

1064
_exit:
×
1065
  if (code != TSDB_CODE_SUCCESS) {
×
1066
    pRsp->code = code;
×
1067
  }
1068
  return code;
×
1069
}
1070

1071

1072
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SFollowerSsMigrateReq *pReq);
1073

1074
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1075
  int32_t          code = 0;
×
1076
  SFollowerSsMigrateReq req = {0};
×
1077

1078
  // decode
1079
  if (tDeserializeSFollowerSsMigrateReq(pReq, len, &req) != 0) {
×
1080
    code = TSDB_CODE_INVALID_MSG;
×
1081
    goto _exit;
×
1082
  }
1083

1084
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1085

1086
_exit:
×
1087
  tFreeSFollowerSsMigrateReq(&req);
×
1088
  return code;
×
1089
}
1090

1091
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1092
  int               ret = 0;
×
1093
  SVDropTtlTableReq ttlReq = {0};
×
1094
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1095
    ret = TSDB_CODE_INVALID_MSG;
×
1096
    goto end;
×
1097
  }
1098

1099
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1100
    ret = TSDB_CODE_INVALID_MSG;
×
1101
    goto end;
×
1102
  }
1103

1104
  if (ttlReq.nUids != 0) {
×
1105
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1106
          ttlReq.nUids);
1107
  }
1108

1109
  if (ttlReq.nUids > 0) {
×
1110
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1111
    if (code) return code;
×
1112

1113
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1114
    if (code) {
×
1115
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1116
    }
1117
  }
1118

1119
end:
×
1120
  taosArrayDestroy(ttlReq.pTbUids);
×
1121
  return ret;
×
1122
}
1123

1124
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
33✔
1125
  int32_t                 code = -1;
33✔
1126
  SMetaReader             mr = {0};
33✔
1127
  SVDropTtlTableReq       ttlReq = {0};
33✔
1128
  SVFetchTtlExpiredTbsRsp rsp = {0};
33✔
1129
  SEncoder                encoder = {0};
33✔
1130
  SArray                 *pNames = NULL;
33✔
1131
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
33✔
1132
  pRsp->code = TSDB_CODE_SUCCESS;
33✔
1133
  pRsp->pCont = NULL;
33✔
1134
  pRsp->contLen = 0;
33✔
1135

1136
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
33!
1137
    terrno = TSDB_CODE_INVALID_MSG;
×
1138
    goto _end;
×
1139
  }
1140

1141
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
33!
1142
    terrno = TSDB_CODE_INVALID_MSG;
×
1143
    goto _end;
×
1144
  }
1145

1146
  tb_uid_t    suid;
1147
  char        ctbName[TSDB_TABLE_NAME_LEN];
1148
  SVDropTbReq expiredTb = {.igNotExists = true};
33✔
1149
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
33✔
1150
  rsp.vgId = TD_VID(pVnode);
33✔
1151
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
33✔
1152
  if (!rsp.pExpiredTbs) goto _end;
33!
1153

1154
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
33✔
1155
  if (!pNames) {
33!
1156
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1157
    goto _end;
×
1158
  }
1159
  char buf[TSDB_TABLE_NAME_LEN];
1160
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
169✔
1161
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
136✔
1162
    expiredTb.suid = *uid;
136✔
1163
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
136✔
1164
    if (terrno < 0) goto _end;
136!
1165
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
136✔
1166
    void *p = taosArrayPush(pNames, buf);
136✔
1167
    if (p == NULL) {
136!
1168
      goto _end;
×
1169
    }
1170

1171
    expiredTb.name = p;
136✔
1172
    if (mr.me.type == TSDB_CHILD_TABLE) {
136✔
1173
      expiredTb.suid = mr.me.ctbEntry.suid;
86✔
1174
    }
1175

1176
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
272!
1177
      goto _end;
×
1178
    }
1179
  }
1180

1181
  int32_t ret = 0;
33✔
1182
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
33!
1183
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
33✔
1184
  if (pRsp->pCont == NULL) {
33!
1185
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1186
    code = -1;
×
1187
    goto _end;
×
1188
  }
1189
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
33✔
1190
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
33✔
1191
  tEncoderClear(&encoder);
33✔
1192

1193
  if (terrno == 0) code = 0;
33!
1194
_end:
×
1195
  metaReaderClear(&mr);
33✔
1196
  tFreeFetchTtlExpiredTbsRsp(&rsp);
33✔
1197
  taosArrayDestroy(ttlReq.pTbUids);
33✔
1198
  if (pNames) taosArrayDestroy(pNames);
33!
1199
  pRsp->code = terrno;
33✔
1200
  return code;
33✔
1201
}
1202

1203
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
27,917✔
1204
  int32_t        code = 0;
27,917✔
1205
  SVCreateStbReq req = {0};
27,917✔
1206
  SDecoder       coder;
1207

1208
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
27,917✔
1209
  pRsp->code = TSDB_CODE_SUCCESS;
27,917✔
1210
  pRsp->pCont = NULL;
27,917✔
1211
  pRsp->contLen = 0;
27,917✔
1212

1213
  // decode and process req
1214
  tDecoderInit(&coder, pReq, len);
27,917✔
1215

1216
  code = tDecodeSVCreateStbReq(&coder, &req);
27,909✔
1217
  if (code) {
27,976!
1218
    pRsp->code = code;
×
1219
    goto _err;
×
1220
  }
1221

1222
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
27,976✔
1223
  if (code) {
28,059✔
1224
    pRsp->code = code;
6✔
1225
    goto _err;
6✔
1226
  }
1227

1228
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
28,053!
1229
    pRsp->code = code;
×
1230
    goto _err;
×
1231
  }
1232

1233
  tDecoderClear(&coder);
28,033✔
1234
  return 0;
27,985✔
1235

1236
_err:
6✔
1237
  tDecoderClear(&coder);
6✔
1238
  return code;
6✔
1239
}
1240

1241
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
88,750✔
1242
                                       SRpcMsg *pOriginRpc) {
1243
  SDecoder           decoder = {0};
88,750✔
1244
  SEncoder           encoder = {0};
88,750✔
1245
  int32_t            rcode = 0;
88,750✔
1246
  SVCreateTbBatchReq req = {0};
88,750✔
1247
  SVCreateTbReq     *pCreateReq;
1248
  SVCreateTbBatchRsp rsp = {0};
88,750✔
1249
  SVCreateTbRsp      cRsp = {0};
88,750✔
1250
  char               tbName[TSDB_TABLE_FNAME_LEN];
1251
  STbUidStore       *pStore = NULL;
88,750✔
1252
  SArray            *tbUids = NULL;
88,750✔
1253
  SArray            *tbNames = NULL;
88,750✔
1254
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
88,750✔
1255
  pRsp->code = TSDB_CODE_SUCCESS;
88,750✔
1256
  pRsp->pCont = NULL;
88,750✔
1257
  pRsp->contLen = 0;
88,750✔
1258

1259
  // decode
1260
  tDecoderInit(&decoder, pReq, len);
88,750✔
1261
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
88,743!
1262
    rcode = -1;
×
1263
    terrno = TSDB_CODE_INVALID_MSG;
×
1264
    goto _exit;
×
1265
  }
1266

1267
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
88,775✔
1268
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
88,783✔
1269
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
88,789✔
1270
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
88,793!
1271
    rcode = -1;
×
1272
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1273
    goto _exit;
×
1274
  }
1275

1276
  // loop to create table
1277
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
220,378✔
1278
    pCreateReq = req.pReqs + iReq;
131,574✔
1279
    memset(&cRsp, 0, sizeof(cRsp));
131,574✔
1280

1281
    if (tsEnableAudit && tsEnableAuditCreateTable) {
131,574!
1282
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
131,575!
1283
      if (str == NULL) {
131,580!
1284
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1285
        rcode = -1;
×
1286
        goto _exit;
×
1287
      }
1288
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
131,580✔
1289
      if (taosArrayPush(tbNames, &str) == NULL) {
131,577!
1290
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1291
        rcode = -1;
×
1292
        goto _exit;
×
1293
      }
1294
    }
1295

1296
    // validate hash
1297
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
131,576✔
1298
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
131,586!
1299
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1300
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1301
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1302
        rcode = -1;
×
1303
        goto _exit;
×
1304
      }
1305
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1306
      continue;
×
1307
    }
1308

1309
    // do create table
1310
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
131,563✔
1311
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
38!
1312
        cRsp.code = TSDB_CODE_SUCCESS;
6✔
1313
      } else {
1314
        cRsp.code = terrno;
32✔
1315
      }
1316
    } else {
1317
      cRsp.code = TSDB_CODE_SUCCESS;
131,552✔
1318
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
131,552!
1319
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1320
      }
1321
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
263,101!
1322
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1323
        rcode = -1;
×
1324
        goto _exit;
×
1325
      }
1326
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
131,550✔
1327
    }
1328

1329
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
263,172!
1330
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1331
      rcode = -1;
×
1332
      goto _exit;
×
1333
    }
1334
  }
1335

1336
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
88,804✔
1337
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
88,804!
1338
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1339
  }
1340
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
88,795!
1341
    goto _exit;
×
1342
  }
1343
  pStore = tdUidStoreFree(pStore);
88,795✔
1344

1345
  // prepare rsp
1346
  int32_t ret = 0;
88,795✔
1347
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
88,795!
1348
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
88,794✔
1349
  if (pRsp->pCont == NULL) {
88,794!
1350
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1351
    rcode = -1;
×
1352
    goto _exit;
×
1353
  }
1354
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
88,794✔
1355
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
88,794!
1356
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1357
  }
1358

1359
  if (tsEnableAudit && tsEnableAuditCreateTable) {
88,795!
1360
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
88,793✔
1361

1362
    SName name = {0};
88,793✔
1363
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
88,793!
1364
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1365
    }
1366

1367
    SStringBuilder sb = {0};
88,788✔
1368
    for (int32_t i = 0; i < tbNames->size; i++) {
220,371✔
1369
      char **key = (char **)taosArrayGet(tbNames, i);
131,582✔
1370
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
131,583✔
1371
      if (i < tbNames->size - 1) {
131,583✔
1372
        taosStringBuilderAppendChar(&sb, ',');
42,793✔
1373
      }
1374
      // taosMemoryFreeClear(*key);
1375
    }
1376

1377
    size_t len = 0;
88,789✔
1378
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
88,789✔
1379

1380
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
88,792!
1381
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
77,687✔
1382
    }
1383

1384
    taosStringBuilderDestroy(&sb);
88,789✔
1385
  }
1386

1387
_exit:
2✔
1388
  tDeleteSVCreateTbBatchReq(&req);
88,795✔
1389
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
88,796✔
1390
  taosArrayDestroy(tbUids);
88,796✔
1391
  tDecoderClear(&decoder);
88,796✔
1392
  tEncoderClear(&encoder);
88,793✔
1393
  taosArrayDestroyP(tbNames, NULL);
88,795✔
1394
  return rcode;
88,796✔
1395
}
1396

1397
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,458✔
1398
  int32_t        code = 0;
11,458✔
1399
  SVCreateStbReq req = {0};
11,458✔
1400
  SDecoder       dc = {0};
11,458✔
1401

1402
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
11,458✔
1403
  pRsp->code = TSDB_CODE_SUCCESS;
11,458✔
1404
  pRsp->pCont = NULL;
11,458✔
1405
  pRsp->contLen = 0;
11,458✔
1406

1407
  tDecoderInit(&dc, pReq, len);
11,458✔
1408

1409
  // decode req
1410
  code = tDecodeSVCreateStbReq(&dc, &req);
11,461✔
1411
  if (code) {
11,474!
1412
    tDecoderClear(&dc);
×
1413
    return code;
×
1414
  }
1415

1416
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
11,474✔
1417
  if (code) {
11,481!
1418
    pRsp->code = code;
×
1419
    tDecoderClear(&dc);
×
1420
    return code;
×
1421
  }
1422

1423
  tDecoderClear(&dc);
11,481✔
1424

1425
  return 0;
11,481✔
1426
}
1427

1428
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,806✔
1429
  SVDropStbReq req = {0};
2,806✔
1430
  int32_t      rcode = TSDB_CODE_SUCCESS;
2,806✔
1431
  SDecoder     decoder = {0};
2,806✔
1432
  SArray      *tbUidList = NULL;
2,806✔
1433

1434
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
2,806✔
1435
  pRsp->pCont = NULL;
2,806✔
1436
  pRsp->contLen = 0;
2,806✔
1437

1438
  // decode request
1439
  tDecoderInit(&decoder, pReq, len);
2,806✔
1440
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2,806!
1441
    rcode = TSDB_CODE_INVALID_MSG;
×
1442
    goto _exit;
×
1443
  }
1444

1445
  // process request
1446
  tbUidList = taosArrayInit(8, sizeof(int64_t));
2,819✔
1447
  if (tbUidList == NULL) goto _exit;
2,815!
1448
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
2,815✔
1449
    rcode = terrno;
30✔
1450
    goto _exit;
30✔
1451
  }
1452

1453
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
2,791!
1454
    rcode = terrno;
×
1455
    goto _exit;
×
1456
  }
1457

1458
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
2,791!
1459
    rcode = terrno;
×
1460
    goto _exit;
×
1461
  }
1462

1463
  // return rsp
1464
_exit:
2,791✔
1465
  if (tbUidList) taosArrayDestroy(tbUidList);
2,821!
1466
  pRsp->code = rcode;
2,821✔
1467
  tDecoderClear(&decoder);
2,821✔
1468
  return 0;
2,821✔
1469
}
1470

1471
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
10,538✔
1472
  SVAlterTbReq  vAlterTbReq = {0};
10,538✔
1473
  SVAlterTbRsp  vAlterTbRsp = {0};
10,538✔
1474
  SDecoder      dc = {0};
10,538✔
1475
  int32_t       code = 0;
10,538✔
1476
  int32_t       lino = 0;
10,538✔
1477
  int32_t       ret;
1478
  SEncoder      ec = {0};
10,538✔
1479
  STableMetaRsp vMetaRsp = {0};
10,538✔
1480

1481
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
10,538✔
1482
  pRsp->pCont = NULL;
10,538✔
1483
  pRsp->contLen = 0;
10,538✔
1484
  pRsp->code = TSDB_CODE_SUCCESS;
10,538✔
1485

1486
  tDecoderInit(&dc, pReq, len);
10,538✔
1487

1488
  // decode
1489
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
10,538!
1490
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1491
    tDecoderClear(&dc);
×
1492
    goto _exit;
×
1493
  }
1494

1495
  // process
1496
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
10,538✔
1497
    vAlterTbRsp.code = terrno;
917✔
1498
    tDecoderClear(&dc);
917✔
1499
    goto _exit;
917✔
1500
  }
1501
  tDecoderClear(&dc);
9,621✔
1502

1503
  if (NULL != vMetaRsp.pSchemas) {
9,621✔
1504
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
5,752✔
1505
    vAlterTbRsp.pMeta = &vMetaRsp;
5,752✔
1506
  }
1507

1508
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
9,621✔
1509
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
5,882✔
1510
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
3,749✔
1511
    if (uid == 0) {
3,749!
1512
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1513
             vAlterTbReq.tbName);
1514
      goto _exit;
×
1515
    }
1516

1517
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
3,749✔
1518
    void   *p = taosArrayPush(tbUids, &uid);
3,749✔
1519
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
3,749!
1520

1521
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,749✔
1522
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
3,749!
1523
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1524
    }
1525

1526
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
3,749✔
1527
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
3,749!
1528
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1529
    }
1530

1531
    taosArrayDestroy(tbUids);
3,749✔
1532
  }
1533

1534
_exit:
5,872✔
1535
  taosArrayDestroy(vAlterTbReq.pMultiTag);
10,538✔
1536
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
10,538!
1537
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
10,538✔
1538
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
10,538✔
1539
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
10,538!
1540
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1541
  }
1542

1543
  tEncoderClear(&ec);
10,538✔
1544
  if (vMetaRsp.pSchemas) {
10,538✔
1545
    taosMemoryFree(vMetaRsp.pSchemas);
5,752!
1546
    taosMemoryFree(vMetaRsp.pSchemaExt);
5,752!
1547
  }
1548
  if (vMetaRsp.pColRefs) {
10,538✔
1549
    taosMemoryFree(vMetaRsp.pColRefs);
329!
1550
  }
1551
  return 0;
10,538✔
1552
}
1553

1554
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
16,566✔
1555
                                     SRpcMsg *pOriginRpc) {
1556
  SVDropTbBatchReq req = {0};
16,566✔
1557
  SVDropTbBatchRsp rsp = {0};
16,566✔
1558
  SDecoder         decoder = {0};
16,566✔
1559
  SEncoder         encoder = {0};
16,566✔
1560
  int32_t          ret;
1561
  SArray          *tbUids = NULL;
16,566✔
1562
  STbUidStore     *pStore = NULL;
16,566✔
1563
  SArray          *tbNames = NULL;
16,566✔
1564

1565
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
16,566✔
1566
  pRsp->pCont = NULL;
16,566✔
1567
  pRsp->contLen = 0;
16,566✔
1568
  pRsp->code = TSDB_CODE_SUCCESS;
16,566✔
1569

1570
  // decode req
1571
  tDecoderInit(&decoder, pReq, len);
16,566✔
1572
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
16,566✔
1573
  if (ret < 0) {
16,566!
1574
    terrno = TSDB_CODE_INVALID_MSG;
×
1575
    pRsp->code = terrno;
×
1576
    goto _exit;
×
1577
  }
1578

1579
  // process req
1580
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
16,566✔
1581
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
16,566✔
1582
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
16,566✔
1583
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
16,566!
1584

1585
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
33,152✔
1586
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
16,586✔
1587
    SVDropTbRsp  dropTbRsp = {0};
16,586✔
1588
    tb_uid_t     tbUid = 0;
16,586✔
1589

1590
    /* code */
1591
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
16,586✔
1592
    if (ret < 0) {
16,586!
1593
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1594
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1595
      } else {
1596
        dropTbRsp.code = terrno;
×
1597
      }
1598
    } else {
1599
      dropTbRsp.code = TSDB_CODE_SUCCESS;
16,586✔
1600
      if (tbUid > 0) {
16,586!
1601
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1602
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1603
        }
1604
      }
1605
    }
1606

1607
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
33,172!
1608
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1609
      pRsp->code = terrno;
×
1610
      goto _exit;
×
1611
    }
1612

1613
    if (tsEnableAuditCreateTable) {
16,586!
1614
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
16,586!
1615
      if (str == NULL) {
16,586!
1616
        pRsp->code = terrno;
×
1617
        goto _exit;
×
1618
      }
1619
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
16,586✔
1620
      if (taosArrayPush(tbNames, &str) == NULL) {
16,586!
1621
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1622
        pRsp->code = terrno;
×
1623
        goto _exit;
×
1624
      }
1625
    }
1626
  }
1627

1628
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
16,566!
1629
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1630
  }
1631

1632
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
16,566!
1633
    goto _exit;
×
1634
  }
1635

1636
  if (tsEnableAuditCreateTable) {
16,566!
1637
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
16,566✔
1638

1639
    SName name = {0};
16,566✔
1640
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
16,566!
1641
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1642
    }
1643

1644
    SStringBuilder sb = {0};
16,566✔
1645
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
33,152✔
1646
      char **key = (char **)taosArrayGet(tbNames, iReq);
16,586✔
1647
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
16,586✔
1648
      if (iReq < req.nReqs - 1) {
16,586✔
1649
        taosStringBuilderAppendChar(&sb, ',');
20✔
1650
      }
1651
      taosMemoryFreeClear(*key);
16,586!
1652
    }
1653

1654
    size_t len = 0;
16,566✔
1655
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
16,566✔
1656

1657
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
16,566!
1658
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
16,524✔
1659
    }
1660

1661
    taosStringBuilderDestroy(&sb);
16,566✔
1662
  }
1663

1664
_exit:
×
1665
  taosArrayDestroy(tbUids);
16,566✔
1666
  pStore = tdUidStoreFree(pStore);
16,566✔
1667
  tDecoderClear(&decoder);
16,566✔
1668
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
16,566!
1669
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
16,566✔
1670
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
16,566✔
1671
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
16,566!
1672
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1673
  }
1674
  tEncoderClear(&encoder);
16,566✔
1675
  taosArrayDestroy(rsp.pArray);
16,566✔
1676
  taosArrayDestroy(tbNames);
16,566✔
1677
  return 0;
16,566✔
1678
}
1679

1680
#ifdef BUILD_NO_CALL
1681
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1682
                                              const char *tags) {
1683
  SSubmitBlkIter blkIter = {0};
1684
  STSchema      *pSchema = NULL;
1685
  tb_uid_t       suid = 0;
1686
  STSRow        *row = NULL;
1687
  int32_t        rv = -1;
1688

1689
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1690
  if (blkIter.row == NULL) return 0;
1691

1692
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1693
                                         &pSchema);  // TODO: use the real schema
1694
  if (TSDB_CODE_SUCCESS != code) {
1695
    printf("%s:%d no valid schema\n", tags, __LINE__);
1696
    return code;
1697
  }
1698

1699
  suid = msgIter->suid;
1700
  rv = TD_ROW_SVER(blkIter.row);
1701

1702
  char __tags[128] = {0};
1703
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1704
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1705
    tdSRowPrint(row, pSchema, __tags);
1706
  }
1707

1708
  taosMemoryFreeClear(pSchema);
1709

1710
  return TSDB_CODE_SUCCESS;
1711
}
1712
#endif
1713
typedef struct SSubmitReqConvertCxt {
1714
  SSubmitMsgIter msgIter;
1715
  SSubmitBlk    *pBlock;
1716
  SSubmitBlkIter blkIter;
1717
  STSRow        *pRow;
1718
  STSRowIter     rowIter;
1719
  SSubmitTbData *pTbData;
1720
  STSchema      *pTbSchema;
1721
  SArray        *pColValues;
1722
} SSubmitReqConvertCxt;
1723

1724
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1725
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1726
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1727
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1728
  if (TSDB_CODE_SUCCESS != code) {
×
1729
    return code;
×
1730
  }
1731
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1732

1733
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1734
  if (NULL == pCxt->pTbData) {
×
1735
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1736
    if (NULL == pCxt->pTbData) {
×
1737
      return terrno;
×
1738
    }
1739
  }
1740
  pCxt->pTbData->flags = 0;
×
1741
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1742
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1743
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1744
  pCxt->pTbData->pCreateTbReq = NULL;
×
1745
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1746
  if (NULL == pCxt->pTbData->aRowP) {
×
1747
    return terrno;
×
1748
  }
1749

1750
  taosArrayDestroy(pCxt->pColValues);
×
1751
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1752
  if (NULL == pCxt->pColValues) {
×
1753
    return terrno;
×
1754
  }
1755
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1756
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1757
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1758
      return terrno;
×
1759
    }
1760
  }
1761

1762
  return TSDB_CODE_SUCCESS;
×
1763
}
1764

1765
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1766
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1767
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1768
  taosMemoryFreeClear(pCxt->pTbData);
×
1769
  taosArrayDestroy(pCxt->pColValues);
×
1770
}
×
1771

1772
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1773
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1774
    pColVal->flag = CV_FLAG_NONE;
×
1775
    return TSDB_CODE_SUCCESS;
×
1776
  }
1777

1778
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1779
    pColVal->flag = CV_FLAG_NULL;
×
1780
    return TSDB_CODE_SUCCESS;
×
1781
  }
1782

1783
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1784
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1785
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1786
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1787
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1788
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1789
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1790
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1791
  } else {
1792
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1793
  }
1794

1795
  pColVal->flag = CV_FLAG_VALUE;
×
1796
  return TSDB_CODE_SUCCESS;
×
1797
}
1798

1799
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1800
  int32_t code = TSDB_CODE_SUCCESS;
×
1801
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1802
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1803
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1804
    SCellVal  cellVal = {0};
×
1805
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1806
      break;
×
1807
    }
1808
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1809
  }
1810
  return code;
×
1811
}
1812

1813
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1814
  if (pCxt->msgIter.schemaLen <= 0) {
×
1815
    return TSDB_CODE_SUCCESS;
×
1816
  }
1817

1818
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1819
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1820
    return terrno;
×
1821
  }
1822

1823
  SDecoder decoder = {0};
×
1824
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1825
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1826
  tDecoderClear(&decoder);
×
1827

1828
  return code;
×
1829
}
1830

1831
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1832
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1833
  if (NULL == pReq2->aSubmitTbData) {
×
1834
    return terrno;
×
1835
  }
1836

1837
  SSubmitReqConvertCxt cxt = {0};
×
1838

1839
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1840
  while (TSDB_CODE_SUCCESS == code) {
×
1841
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1842
    if (TSDB_CODE_SUCCESS == code) {
×
1843
      if (NULL == cxt.pBlock) {
×
1844
        break;
×
1845
      }
1846
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1847
    }
1848
    if (TSDB_CODE_SUCCESS == code) {
×
1849
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1850
    }
1851
    if (TSDB_CODE_SUCCESS == code) {
×
1852
      code = vnodeDecodeCreateTbReq(&cxt);
×
1853
    }
1854
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1855
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1856
      if (TSDB_CODE_SUCCESS == code) {
×
1857
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1858
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1859
      }
1860
    }
1861
    if (TSDB_CODE_SUCCESS == code) {
×
1862
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1863
    }
1864
    if (TSDB_CODE_SUCCESS == code) {
×
1865
      taosMemoryFreeClear(cxt.pTbData);
×
1866
    }
1867
  }
1868

1869
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1870
  return code;
×
1871
}
1872

1873
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1874
  int32_t  code = TSDB_CODE_SUCCESS;
×
1875
  char    *pMsg = NULL;
×
1876
  uint32_t msglen = 0;
×
1877
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1878
  if (TSDB_CODE_SUCCESS == code) {
×
1879
    pMsg = taosMemoryMalloc(msglen);
×
1880
    if (NULL == pMsg) {
×
1881
      code = terrno;
×
1882
    }
1883
  }
1884
  if (TSDB_CODE_SUCCESS == code) {
×
1885
    SEncoder encoder;
1886
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1887
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1888
    tEncoderClear(&encoder);
×
1889
  }
1890
  if (TSDB_CODE_SUCCESS == code) {
×
1891
    *ppMsg = pMsg;
×
1892
  }
1893
  return code;
×
1894
}
1895

1896
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
×
1897
  int32_t code = 0;
×
1898
  int32_t lino = 0;
×
1899

1900
  SMetaEntry *pEntry = NULL;
×
1901
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
×
1902
  if (code) {
×
1903
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
1904
    TSDB_CHECK_CODE(code, lino, _exit);
×
1905
  }
1906
    if (pEntry->type != TSDB_SUPER_TABLE) {
×
1907
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
1908
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
1909
    TSDB_CHECK_CODE(code, lino, _exit);
×
1910
  }
1911

1912
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
×
1913
  if (NULL == *ppRsp) {
×
1914
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
1915
  }
1916
  (*ppRsp)->suid = pSubmitTbData->suid;
×
1917
  (*ppRsp)->tuid = pSubmitTbData->uid;
×
1918
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
×
1919
  (*ppRsp)->vgId = pVnode->config.vgId;
×
1920
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
×
1921
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
×
1922
  (*ppRsp)->pSchemas =
×
1923
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
×
1924
  if (NULL == (*ppRsp)->pSchemas) {
×
1925
    taosMemoryFree(*ppRsp);
×
1926
    *ppRsp = NULL;
×
1927
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
1928
  }
1929
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
×
1930
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
×
1931
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
×
1932
  if (pEntry->pExtSchemas != NULL) {
×
1933
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
1934
    if (NULL == (*ppRsp)->pSchemaExt) {
×
1935
      taosMemoryFree((*ppRsp)->pSchemas);
×
1936
      taosMemoryFree(*ppRsp);
×
1937
      *ppRsp = NULL;
×
1938
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
1939
    }
1940
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
1941
  }
1942

1943
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
×
1944
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
×
1945
  }
1946
_exit:
×
1947
  metaFetchEntryFree(&pEntry);
×
1948
  if (code != TSDB_CODE_SUCCESS) {
×
1949
    vError("vgId:%d, failed to build exist sub table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
1950
  }
1951
  return code;
×
1952
}
1953

1954
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
×
1955
  int32_t code = 0;
×
1956
  int32_t lino = 0;
×
1957

1958
  SMetaEntry *pEntry = NULL;
×
1959
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->uid, &pEntry);
×
1960
  if (code) {
×
1961
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
1962
    TSDB_CHECK_CODE(code, lino, _exit);
×
1963
  }
1964
  if (pEntry->type != TSDB_NORMAL_TABLE) {
×
1965
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
1966
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
1967
    TSDB_CHECK_CODE(code, lino, _exit);
×
1968
  }
1969

1970
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
×
1971
  if (NULL == *ppRsp) {
×
1972
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
1973
  }
1974

1975
  (*ppRsp)->tuid = pEntry->uid;
×
1976
  (*ppRsp)->vgId = pVnode->config.vgId;
×
1977
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
×
1978
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
×
1979
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
×
1980
  if (NULL == (*ppRsp)->pSchemas) {
×
1981
    taosMemoryFree(*ppRsp);
×
1982
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
1983
  }
1984
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
×
1985
  if (pEntry->pExtSchemas != NULL) {
×
1986
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
1987
    if (NULL == (*ppRsp)->pSchemaExt) {
×
1988
      taosMemoryFree((*ppRsp)->pSchemas);
×
1989
      taosMemoryFree(*ppRsp);
×
1990
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
1991
    }
1992
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
1993
  }
1994

1995
_exit:
×
1996
  metaFetchEntryFree(&pEntry);
×
1997
  if (code != TSDB_CODE_SUCCESS) {
×
1998
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
1999
  }
2000
  return code;
×
2001
}
2002

2003
static int32_t buildExistTalbeInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
998✔
2004
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
998!
2005
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData, ppRsp);
×
2006
    if (code) {
×
2007
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2008
      return code;
×
2009
    }
2010
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
2011
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
998!
2012
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
×
2013
  }
2014
  return TSDB_CODE_SUCCESS;
998✔
2015
}
2016

2017
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
14,564,311✔
2018
                                     SRpcMsg *pOriginalMsg) {
2019
  int32_t code = 0;
14,564,311✔
2020
  int32_t lino = 0;
14,564,311✔
2021
  terrno = 0;
14,564,311✔
2022

2023
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
14,564,376✔
2024
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
14,564,376✔
2025
  SArray      *newTbUids = NULL;
14,564,376✔
2026
  int32_t      ret;
2027
  SEncoder     ec = {0};
14,564,376✔
2028

2029
  pRsp->code = TSDB_CODE_SUCCESS;
14,564,376✔
2030

2031
  void           *pAllocMsg = NULL;
14,564,376✔
2032
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
14,564,376✔
2033
  SDecoder dc = {0};
14,564,376✔
2034
  if (0 == taosHton64(pMsg->version)) {
14,564,376!
2035
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2036
    if (TSDB_CODE_SUCCESS == code) {
×
2037
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2038
    }
2039
    if (TSDB_CODE_SUCCESS == code) {
×
2040
      pAllocMsg = pReq;
×
2041
    }
2042
    TSDB_CHECK_CODE(code, lino, _exit);
×
2043
  } else {
2044
    // decode
2045
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
14,564,340✔
2046
    len -= sizeof(SSubmitReq2Msg);
14,564,340✔
2047

2048
    tDecoderInit(&dc, pReq, len);
14,564,340✔
2049
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
14,564,295!
2050
      code = TSDB_CODE_INVALID_MSG;
×
2051
      TSDB_CHECK_CODE(code, lino, _exit);
×
2052
    }
2053
  }
2054

2055
  // scan
2056
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
14,564,350✔
2057
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
14,564,407✔
2058
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
14,564,407✔
2059
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
29,877,785✔
2060
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
15,276,451✔
2061

2062
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
15,276,432!
2063
      code = TSDB_CODE_INVALID_MSG;
×
2064
      TSDB_CHECK_CODE(code, lino, _exit);
×
2065
    }
2066

2067
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
15,276,432✔
2068
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
10,137!
2069
        code = TSDB_CODE_INVALID_MSG;
×
2070
        TSDB_CHECK_CODE(code, lino, _exit);
×
2071
      }
2072

2073
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
10,137✔
2074
      SRowKey   lastKey;
2075
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
10,137✔
2076
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
9,715,348✔
2077
        SRowKey key;
2078
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
9,706,275✔
2079
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
9,703,305!
2080
          code = TSDB_CODE_INVALID_MSG;
×
2081
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
2082
          TSDB_CHECK_CODE(code, lino, _exit);
649!
2083
        }
2084
      }
2085
    } else {
2086
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
15,266,295✔
2087
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
15,266,295✔
2088
      SRowKey lastRowKey;
2089
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
791,115,038✔
2090
#ifndef NO_UNALIGNED_ACCESS
2091
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
775,810,733!
2092
#else
2093
        TSKEY ts = taosGetInt64Aligned(&(aRow[iRow]->ts));
2094
        if (ts < minKey || ts > maxKey) {
2095
#endif
2096
          code = TSDB_CODE_INVALID_MSG;
27,446✔
2097
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
27,446✔
2098
          TSDB_CHECK_CODE(code, lino, _exit);
8,523✔
2099
        }
2100
        if (iRow == 0) {
775,791,809✔
2101
          tRowGetKey(aRow[iRow], &lastRowKey);
30,532,570✔
2102
        } else {
2103
          SRowKey rowKey;
2104
          tRowGetKey(aRow[iRow], &rowKey);
1,521,051,048✔
2105

2106
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
760,523,892!
2107
            code = TSDB_CODE_INVALID_MSG;
×
2108
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
2109
            TSDB_CHECK_CODE(code, lino, _exit);
80,709!
2110
          }
2111
          lastRowKey = rowKey;
760,579,823✔
2112
        }
2113
      }
2114
    }
2115
  }
2116

2117
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
29,877,957✔
2118
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
15,276,436✔
2119

2120
    if (pSubmitTbData->pCreateTbReq) {
15,276,440✔
2121
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
79,217✔
2122
    } else {
2123
      SMetaInfo info = {0};
15,197,223✔
2124

2125
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
15,197,223✔
2126
      if (code) {
15,197,344!
2127
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2128
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
2129
        TSDB_CHECK_CODE(code, lino, _exit);
×
2130
      }
2131

2132
      if (info.suid != pSubmitTbData->suid) {
15,197,344!
2133
        vError("vgId:%d, submit uid:%" PRId64 " submit suid:%" PRId64 " info suid:%" PRId64 " not match, line:%d",
×
2134
               TD_VID(pVnode), pSubmitTbData->uid, pSubmitTbData->suid, info.suid, __LINE__);
2135
        code = TSDB_CODE_INVALID_MSG;
×
2136
        TSDB_CHECK_CODE(code, lino, _exit);
×
2137
      }
2138

2139
      if (info.suid) {
15,197,344✔
2140
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
13,877,254!
2141
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2142
        }
2143
      }
2144

2145
      if (pSubmitTbData->sver != info.skmVer) {
15,197,406!
2146
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
2147
        TSDB_CHECK_CODE(code, lino, _exit);
×
2148
      }
2149
    }
2150

2151
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
15,276,623✔
2152
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
10,141✔
2153
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
10,141✔
2154

2155
      if (nColData <= 0) {
10,141!
2156
        code = TSDB_CODE_INVALID_MSG;
×
2157
        TSDB_CHECK_CODE(code, lino, _exit);
×
2158
      }
2159

2160
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
10,141!
2161
          aColData[0].nVal <= 0) {
10,141!
2162
        code = TSDB_CODE_INVALID_MSG;
×
2163
        TSDB_CHECK_CODE(code, lino, _exit);
×
2164
      }
2165

2166
      for (int32_t j = 1; j < nColData; j++) {
30,640✔
2167
        if (aColData[j].nVal != aColData[0].nVal) {
20,499!
2168
          code = TSDB_CODE_INVALID_MSG;
×
2169
          TSDB_CHECK_CODE(code, lino, _exit);
×
2170
        }
2171
      }
2172
    }
2173
  }
2174

2175
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
14,601,521!
2176
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2177

2178
  // loop to handle
2179
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
29,841,036✔
2180
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
15,276,562✔
2181

2182
    // create table
2183
    if (pSubmitTbData->pCreateTbReq) {
15,276,569✔
2184
      // alloc if need
2185
      if (pSubmitRsp->aCreateTbRsp == NULL &&
79,213✔
2186
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
67,127!
2187
              NULL) {
2188
        code = terrno;
×
2189
        TSDB_CHECK_CODE(code, lino, _exit);
8!
2190
      }
2191

2192
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
79,214✔
2193

2194
      // create table
2195
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
79,209✔
2196
        // create table success
2197

2198
        if (newTbUids == NULL &&
138,507!
2199
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
66,128✔
2200
          code = terrno;
×
2201
          TSDB_CHECK_CODE(code, lino, _exit);
×
2202
        }
2203

2204
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
144,758!
2205
          code = terrno;
×
2206
          TSDB_CHECK_CODE(code, lino, _exit);
×
2207
        }
2208

2209
        if (pCreateTbRsp->pMeta) {
72,379!
2210
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
72,379✔
2211
        }
2212
      } else {  // create table failed
2213
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
6,831✔
2214
          code = terrno;
8✔
2215
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
8!
2216
                 tstrerror(terrno));
2217
          TSDB_CHECK_CODE(code, lino, _exit);
8!
2218
        }
2219
        terrno = 0;
6,823✔
2220
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
6,821✔
2221

2222
        // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2223
        if (i == 0) {
6,821✔
2224
          // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2225
          // only the first one needs to be processed.
2226
          code = buildExistTalbeInStreamRsp(pVnode, pSubmitTbData, &pCreateTbRsp->pMeta);
998✔
2227
          if (code) {
998!
2228
            vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
×
2229
                  pSubmitTbData->pCreateTbReq->name,  code, tstrerror(code));
2230
            TSDB_CHECK_CODE(code, lino, _exit);
×
2231
          }
2232
        }
2233
      }
2234
    }
2235

2236
    // insert data
2237
    int32_t affectedRows;
2238
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
15,276,556✔
2239
    TSDB_CHECK_CODE(code, lino, _exit);
15,276,606!
2240

2241
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
15,276,606✔
2242
    TSDB_CHECK_CODE(code, lino, _exit);
15,276,604!
2243

2244
    pSubmitRsp->affectedRows += affectedRows;
15,276,604✔
2245
  }
2246

2247
  // update the affected table uid list
2248
  if (taosArrayGetSize(newTbUids) > 0) {
14,564,474✔
2249
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
66,126✔
2250
           (int32_t)taosArrayGetSize(newTbUids));
2251
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
66,128!
2252
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2253
    }
2254
  }
2255

2256
_exit:
14,564,457✔
2257
  // message
2258
  pRsp->code = code;
14,564,466✔
2259
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
14,564,466!
2260
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
14,564,431✔
2261
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
14,564,435✔
2262
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
14,564,460!
2263
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2264
  }
2265
  tEncoderClear(&ec);
14,564,403✔
2266

2267
  // update statistics
2268
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
14,564,491✔
2269
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
14,564,517✔
2270
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
14,564,523✔
2271

2272
  // update metrics
2273
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
14,564,517!
2274
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
14,564,517!
2275
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
14,564,517!
2276

2277
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
14,564,517!
2278
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
100!
2279
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
100✔
2280
                                   pVnode->monitor.strClusterId,
100✔
2281
                                   pVnode->monitor.strDnodeId,
100✔
2282
                                   tsLocalEp,
2283
                                   pVnode->monitor.strVgId,
100✔
2284
                                   pOriginalMsg->info.conn.user,
100✔
2285
                                   "Success"};
2286
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
100✔
2287
  }
2288

2289
  if (code == 0) {
14,564,517✔
2290
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
14,564,404✔
2291
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
14,564,508✔
2292
  }
2293
  /*
2294
  if (code == 0) {
2295
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2296
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2297

2298
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2299
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2300
                                          pOriginalMsg->info.conn.user, "Success"};
2301
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2302
  }
2303
  else{
2304
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2305
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2306
                                        pOriginalMsg->info.conn.user, "Failed"};
2307
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2308
  }
2309
  */
2310

2311
  // clear
2312
  taosArrayDestroy(newTbUids);
14,564,570✔
2313
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
14,564,459!
2314
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
14,564,442✔
2315

2316
  if (code) {
14,564,378✔
2317
    terrno = code;
9✔
2318
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
9!
2319
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
×
2320
            tstrerror(code), lino, ver);
2321

2322
    } else {
2323
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
9!
2324
             tstrerror(code), lino, ver);
2325
    }
2326
  }
2327

2328
  taosMemoryFree(pAllocMsg);
14,564,315!
2329
  tDecoderClear(&dc);
14,564,355✔
2330

2331
  return code;
14,564,493✔
2332
}
2333

2334
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
76✔
2335
  int32_t code = TSDB_CODE_SUCCESS;
76✔
2336

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

2340
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2341
  code = metaTrimTables(pVnode->pMeta, ver);
76✔
2342

2343
  return code;
76✔
2344
}
2345

2346
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
15,297✔
2347
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
15,297!
2348
  int32_t code = TSDB_CODE_SUCCESS;
15,297✔
2349
  if (!pVnode->config.hashChange) {
15,297✔
2350
    goto _exit;
15,221✔
2351
  }
2352

2353
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
76✔
2354
  if (code < 0) {
76!
2355
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2356
    goto _exit;
×
2357
  }
2358
  pVnode->config.hashChange = false;
76✔
2359

2360
_exit:
15,297✔
2361
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
15,297✔
2362
  pRsp->code = code;
15,297✔
2363
  pRsp->pCont = NULL;
15,297✔
2364
  pRsp->contLen = 0;
15,297✔
2365

2366
  return code;
15,297✔
2367
}
2368

2369
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2370
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2371

2372
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
1,123✔
2373
  bool walChanged = false;
1,123✔
2374
  bool tsdbChanged = false;
1,123✔
2375

2376
  SAlterVnodeConfigReq req = {0};
1,123✔
2377
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
1,123!
2378
    terrno = TSDB_CODE_INVALID_MSG;
×
2379
    return TSDB_CODE_INVALID_MSG;
×
2380
  }
2381

2382
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
1,123!
2383
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2384
        "ssCompact:%d fsync:%d level:%d "
2385
        "walRetentionPeriod:%d walRetentionSize:%d",
2386
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2387
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2388
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2389
        req.walRetentionSize);
2390

2391
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
1,124✔
2392
    pVnode->config.cacheLastSize = req.cacheLastSize;
363✔
2393
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
363✔
2394
  }
2395

2396
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
1,124✔
2397
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
10!
2398
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2399
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
10✔
2400
  }
2401

2402
  if (pVnode->config.szCache != req.pages) {
1,124✔
2403
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
6!
2404
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2405
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2406
      return terrno;
×
2407
    } else {
2408
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
6!
2409
      pVnode->config.szCache = req.pages;
6✔
2410
    }
2411
  }
2412

2413
  if (pVnode->config.cacheLast != req.cacheLast) {
1,124✔
2414
    pVnode->config.cacheLast = req.cacheLast;
637✔
2415
  }
2416

2417
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
1,124✔
2418
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
124✔
2419
    walChanged = true;
124✔
2420
  }
2421

2422
  if (pVnode->config.walCfg.level != req.walLevel) {
1,124✔
2423
    if (pVnode->config.walCfg.level == 0) {
87✔
2424
      pVnode->config.walCfg.clearFiles = 1;
3✔
2425
    }
2426
    pVnode->config.walCfg.level = req.walLevel;
87✔
2427
    walChanged = true;
87✔
2428
  }
2429

2430
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
1,124✔
2431
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
132✔
2432
    walChanged = true;
132✔
2433
  }
2434

2435
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
1,124!
2436
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
2437
    walChanged = true;
×
2438
  }
2439

2440
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
1,124✔
2441
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
100✔
2442
    if (!VND_IS_RSMA(pVnode)) {
100!
2443
      tsdbChanged = true;
100✔
2444
    }
2445
  }
2446

2447
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
1,124✔
2448
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
100✔
2449
    if (!VND_IS_RSMA(pVnode)) {
100!
2450
      tsdbChanged = true;
100✔
2451
    }
2452
  }
2453

2454
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
1,124✔
2455
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
102✔
2456
    if (!VND_IS_RSMA(pVnode)) {
102!
2457
      tsdbChanged = true;
102✔
2458
    }
2459
  }
2460

2461
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
1,124✔
2462
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
8✔
2463
    if (!VND_IS_RSMA(pVnode)) {
8!
2464
      tsdbChanged = true;
8✔
2465
    }
2466
  }
2467

2468
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
1,124!
2469
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
2470
      pVnode->config.sttTrigger = req.sttTrigger;
×
2471
    } else {
2472
      vnodeAWait(&pVnode->commitTask);
×
2473

2474
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
2475
      if (ret != 0) {
×
2476
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2477
      }
2478

2479
      pVnode->config.sttTrigger = req.sttTrigger;
×
2480
      tsdbEnableBgTask(pVnode->pTsdb);
×
2481
    }
2482
  }
2483

2484
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
1,124!
2485
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
2486
  }
2487

2488
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
1,124!
2489
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
×
2490
  }
2491
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
1,124!
2492
    pVnode->config.ssCompact = req.ssCompact;
×
2493
  }
2494

2495
  if (walChanged) {
1,124✔
2496
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
339!
2497
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2498
    }
2499
  }
2500

2501
  if (tsdbChanged) {
1,124✔
2502
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
112✔
2503
  }
2504

2505
  return 0;
1,124✔
2506
}
2507

2508
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2509
  SBatchDeleteReq deleteReq;
2510
  SDecoder        decoder;
2511
  tDecoderInit(&decoder, pReq, len);
×
2512
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2513
    tDecoderClear(&decoder);
×
2514
    return terrno = TSDB_CODE_INVALID_MSG;
×
2515
  }
2516

2517
  SMetaReader mr = {0};
×
2518
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2519
  STsdb *pTsdb = pVnode->pTsdb;
×
2520

2521
  if (deleteReq.level) {
×
2522
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2523
  }
2524

2525
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2526
  for (int32_t i = 0; i < sz; i++) {
×
2527
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2528
    char             *name = pOneReq->tbname;
×
2529
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2530
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2531
      continue;
×
2532
    }
2533

2534
    int64_t uid = mr.me.uid;
×
2535

2536
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2537
    if (code < 0) {
×
2538
      terrno = code;
×
2539
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2540
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2541
    }
2542

2543
    if (deleteReq.level == 0) {
×
2544
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2545
      if (code < 0) {
×
2546
        terrno = code;
×
2547
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2548
               ", end ts:%" PRId64,
2549
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2550
      }
2551
    }
2552
    tDecoderClear(&mr.coder);
×
2553
  }
2554
  metaReaderClear(&mr);
×
2555
  taosArrayDestroy(deleteReq.deleteReqs);
×
2556
  return 0;
×
2557
}
2558

2559
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
63,668✔
2560
                                     SRpcMsg *pOriginalMsg) {
2561
  int32_t     code = 0;
63,668✔
2562
  SDecoder   *pCoder = &(SDecoder){0};
63,668✔
2563
  SDeleteRes *pRes = &(SDeleteRes){0};
63,668✔
2564

2565
  pRsp->msgType = TDMT_VND_DELETE_RSP;
63,668✔
2566
  pRsp->pCont = NULL;
63,668✔
2567
  pRsp->contLen = 0;
63,668✔
2568
  pRsp->code = TSDB_CODE_SUCCESS;
63,668✔
2569

2570
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
63,668✔
2571
  if (pRes->uidList == NULL) {
63,671!
2572
    code = terrno;
×
2573
    goto _err;
×
2574
  }
2575

2576
  tDecoderInit(pCoder, pReq, len);
63,671✔
2577
  code = tDecodeDeleteRes(pCoder, pRes);
63,666✔
2578
  if (code) goto _err;
63,675!
2579

2580
  if (pRes->affectedRows > 0) {
63,675✔
2581
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
124,661✔
2582
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
64,497✔
2583
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
64,495✔
2584
      if (code) goto _err;
64,497!
2585
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
64,497✔
2586
      if (code) goto _err;
64,497!
2587
    }
2588
  }
2589

2590
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
63,673✔
2591

2592
  tDecoderClear(pCoder);
63,673✔
2593
  taosArrayDestroy(pRes->uidList);
63,672✔
2594

2595
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
63,676✔
2596
  int32_t     ret = 0;
63,676✔
2597
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
63,676!
2598
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
63,673✔
2599
  SEncoder ec = {0};
63,673✔
2600
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
63,673✔
2601
  code = tEncodeSVDeleteRsp(&ec, &rsp);
63,676✔
2602
  if (code) goto _err;
63,672!
2603
  tEncoderClear(&ec);
63,672✔
2604
  return code;
63,676✔
2605

2606
_err:
×
2607
  /*
2608
  if(code == TSDB_CODE_SUCCESS){
2609
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2610
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2611
                                        pOriginalMsg->info.conn.user, "Success"};
2612
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2613
  }
2614
  else{
2615
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2616
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2617
                                        pOriginalMsg->info.conn.user, "Failed"};
2618
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2619
  }
2620
  */
2621

2622
  return code;
×
2623
}
2624
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
978✔
2625
  SVCreateStbReq req = {0};
978✔
2626
  SDecoder       dc = {0};
978✔
2627
  int32_t        code = 0;
978✔
2628

2629
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
978✔
2630
  pRsp->code = TSDB_CODE_SUCCESS;
978✔
2631
  pRsp->pCont = NULL;
978✔
2632
  pRsp->contLen = 0;
978✔
2633

2634
  tDecoderInit(&dc, pReq, len);
978✔
2635
  // decode req
2636
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
978!
2637
    tDecoderClear(&dc);
×
2638
    return terrno = TSDB_CODE_INVALID_MSG;
×
2639
  }
2640

2641
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
978✔
2642
  if (code) {
978!
2643
    pRsp->code = code;
×
2644
    goto _err;
×
2645
  }
2646
  tDecoderClear(&dc);
978✔
2647
  return 0;
978✔
2648

2649
_err:
×
2650
  tDecoderClear(&dc);
×
2651
  return code;
×
2652
}
2653
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,144✔
2654
  SDropIndexReq req = {0};
2,144✔
2655
  int32_t       code = 0;
2,144✔
2656
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,144✔
2657
  pRsp->code = TSDB_CODE_SUCCESS;
2,144✔
2658
  pRsp->pCont = NULL;
2,144✔
2659
  pRsp->contLen = 0;
2,144✔
2660

2661
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,144!
2662
    pRsp->code = code;
×
2663
    return code;
×
2664
  }
2665

2666
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,144✔
2667
  if (code) {
2,144!
2668
    pRsp->code = code;
×
2669
    return code;
×
2670
  }
2671
  return TSDB_CODE_SUCCESS;
2,144✔
2672
}
2673

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

2676
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
240✔
2677
  if (!pVnode->restored) {
240✔
2678
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
174!
2679
    return 0;
174✔
2680
  }
2681
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
66✔
2682
}
2683

2684
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2685
  if (syncCheckMember(pVnode->sync) != 0) {
×
2686
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2687
  }
2688

2689
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2690
  pRsp->code = TSDB_CODE_SUCCESS;
×
2691
  pRsp->pCont = NULL;
×
2692
  pRsp->contLen = 0;
×
2693

2694
  return 0;
×
2695
}
2696

2697
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
41✔
2698
  SSyncState syncState = syncGetState(pVnode->sync);
41✔
2699
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
41✔
2700
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
9✔
2701
  }
2702

2703
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
32✔
2704
  if (vnodeGetArbToken(pVnode, token) != 0) {
32!
2705
    return terrno = TSDB_CODE_NOT_FOUND;
×
2706
  }
2707

2708
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
32✔
2709
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
28✔
2710
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
24✔
2711
  }
2712

2713
  terrno = TSDB_CODE_SUCCESS;
8✔
2714
  return 0;
8✔
2715
}
2716

2717
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
9✔
2718
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
9✔
2719
  if (code != 0) {
9✔
2720
    return code;
5✔
2721
  }
2722

2723
  return syncCheckSynced(pVnode->sync);
4✔
2724
}
2725

2726
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
9✔
2727
  int32_t code = 0;
9✔
2728

2729
  SVArbCheckSyncReq syncReq = {0};
9✔
2730

2731
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
9✔
2732
  if (code) {
9!
2733
    return terrno = code;
×
2734
  }
2735

2736
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
9✔
2737
  pRsp->code = TSDB_CODE_SUCCESS;
9✔
2738
  pRsp->pCont = NULL;
9✔
2739
  pRsp->contLen = 0;
9✔
2740

2741
  SVArbCheckSyncRsp syncRsp = {0};
9✔
2742
  syncRsp.arbToken = syncReq.arbToken;
9✔
2743
  syncRsp.member0Token = syncReq.member0Token;
9✔
2744
  syncRsp.member1Token = syncReq.member1Token;
9✔
2745
  syncRsp.vgId = TD_VID(pVnode);
9✔
2746

2747
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
9✔
2748
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
5!
2749
  }
2750
  syncRsp.errCode = terrno;
9✔
2751

2752
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
9!
2753
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2754
    code = -1;
×
2755
    goto _OVER;
×
2756
  }
2757

2758
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
9✔
2759
  if (contLen <= 0) {
9!
2760
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2761
    code = -1;
×
2762
    goto _OVER;
×
2763
  }
2764
  void *pHead = rpcMallocCont(contLen);
9✔
2765
  if (!pHead) {
9!
2766
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2767
    code = -1;
×
2768
    goto _OVER;
×
2769
  }
2770

2771
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
9!
2772
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2773
    rpcFreeCont(pHead);
×
2774
    code = -1;
×
2775
    goto _OVER;
×
2776
  }
2777

2778
  pRsp->pCont = pHead;
9✔
2779
  pRsp->contLen = contLen;
9✔
2780

2781
  terrno = TSDB_CODE_SUCCESS;
9✔
2782

2783
_OVER:
9✔
2784
  tFreeSVArbCheckSyncReq(&syncReq);
9✔
2785
  return code;
9✔
2786
}
2787

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