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

taosdata / TDengine / #4944

30 Jan 2026 06:19AM UTC coverage: 66.849% (+0.1%) from 66.718%
#4944

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1124 of 2018 new or added lines in 72 files covered. (55.7%)

13677 existing lines in 155 files now uncovered.

205211 of 306978 relevant lines covered (66.85%)

125657591.7 hits per line

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

64.53
/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
extern int32_t vnodeProcessScanVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
35
extern int32_t vnodeQueryScanProgress(SVnode *pVnode, SRpcMsg *pMsg);
36

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

65
static int32_t vnodeCheckState(SVnode *pVnode);
66
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
67
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
68
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
69

70
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
71
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
72

73
extern int32_t vnodeProcessKillRetentionReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
74
extern int32_t vnodeQueryRetentionProgress(SVnode *pVnode, SRpcMsg *pMsg);
75

76
extern int32_t vnodeProcessKillScanReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
77

78
extern int32_t vnodeListSsMigrateFileSets(SVnode *pVnode, SRpcMsg *pMsg);
79
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
80
extern int32_t vnodeQuerySsMigrateProgress(SVnode *pVnode, SRpcMsg *pMsg);
81
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
82
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
83

84
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
57,033,672✔
85
  int32_t code = 0;
57,033,672✔
86
  int32_t lino = 0;
57,033,672✔
87

88
  if (tStartDecode(pCoder) < 0) {
57,033,672✔
89
    code = TSDB_CODE_INVALID_MSG;
×
90
    TSDB_CHECK_CODE(code, lino, _exit);
×
91
  }
92

93
  // flags
94
  if (tDecodeI32v(pCoder, NULL) < 0) {
57,035,612✔
95
    code = TSDB_CODE_INVALID_MSG;
×
96
    TSDB_CHECK_CODE(code, lino, _exit);
×
97
  }
98

99
  // name
100
  char *name = NULL;
57,035,612✔
101
  if (tDecodeCStr(pCoder, &name) < 0) {
57,036,009✔
102
    code = TSDB_CODE_INVALID_MSG;
×
103
    TSDB_CHECK_CODE(code, lino, _exit);
×
104
  }
105

106
  // uid
107
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
57,036,009✔
108
  if (uid == 0) {
57,031,282✔
109
    uid = tGenIdPI64();
56,651,645✔
110
  }
111
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
57,027,501✔
112

113
  // btime
114
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
57,026,789✔
115

116
  tEndDecode(pCoder);
57,023,408✔
117

118
_exit:
57,029,848✔
119
  if (code) {
57,031,674✔
120
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
121
  } else {
122
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
57,031,674✔
123
    if (pUid) *pUid = uid;
57,020,238✔
124
  }
125
  return code;
57,019,612✔
126
}
127
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
37,326,237✔
128
  int32_t code = 0;
37,326,237✔
129
  int32_t lino = 0;
37,326,237✔
130

131
  int64_t  btime = taosGetTimestampMs();
37,326,550✔
132
  SDecoder dc = {0};
37,326,550✔
133
  int32_t  nReqs;
37,325,436✔
134

135
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
37,326,238✔
136
  if (tStartDecode(&dc) < 0) {
37,326,241✔
137
    code = TSDB_CODE_INVALID_MSG;
×
138
    return code;
×
139
  }
140

141
  if (tDecodeI32v(&dc, &nReqs) < 0) {
37,326,214✔
142
    code = TSDB_CODE_INVALID_MSG;
×
143
    TSDB_CHECK_CODE(code, lino, _exit);
×
144
  }
145
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
79,527,648✔
146
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
42,210,880✔
147
    TSDB_CHECK_CODE(code, lino, _exit);
42,201,434✔
148
  }
149

150
  tEndDecode(&dc);
37,316,768✔
151

152
_exit:
37,323,109✔
153
  tDecoderClear(&dc);
37,323,422✔
154
  if (code) {
37,325,315✔
155
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
156
           tstrerror(code), TMSG_INFO(pMsg->msgType));
157
  }
158
  return code;
37,325,113✔
159
}
160

161
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
11,788,301✔
162
  int32_t code = TSDB_CODE_INVALID_MSG;
11,788,301✔
163
  int32_t lino = 0;
11,788,301✔
164

165
  if (pVnode->config.isAudit) {
11,788,301✔
NEW
166
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
167
  }
168

169
  SDecoder dc = {0};
11,788,301✔
170
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
11,788,301✔
171

172
  SVAlterTbReq vAlterTbReq = {0};
11,788,301✔
173
  int64_t      ctimeMs = taosGetTimestampMs();
11,788,301✔
174
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
11,788,301✔
UNCOV
175
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
UNCOV
176
    vAlterTbReq.pMultiTag = NULL;
×
UNCOV
177
    goto _exit;
×
178
  }
179
  taosArrayDestroy(vAlterTbReq.pMultiTag);
11,788,301✔
180
  vAlterTbReq.pMultiTag = NULL;
11,788,301✔
181

182
  code = 0;
11,788,301✔
183

184
_exit:
11,788,301✔
185
  tDecoderClear(&dc);
11,788,301✔
186
  if (code) {
11,788,301✔
UNCOV
187
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
188
  } else {
189
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
11,788,301✔
190
           ctimeMs);
191
  }
192
  return code;
11,788,301✔
193
}
194

195
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,255,701✔
196
  int32_t code = TSDB_CODE_INVALID_MSG;
10,255,701✔
197
  int32_t lino = 0;
10,255,701✔
198

199
  SMsgHead *pContOld = pMsg->pCont;
10,255,701✔
200
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
10,255,728✔
201

202
  SArray *tbUids = NULL;
10,256,398✔
203
  int64_t timestampMs = 0;
10,256,398✔
204

205
  SVDropTtlTableReq ttlReq = {0};
10,256,398✔
206
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
10,256,171✔
UNCOV
207
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
208
    TSDB_CHECK_CODE(code, lino, _exit);
×
209
  }
210

211
  {  // find expired uids
212
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
10,256,110✔
213
    if (tbUids == NULL) {
10,251,417✔
UNCOV
214
      code = terrno;
×
UNCOV
215
      TSDB_CHECK_CODE(code, lino, _exit);
×
216
    }
217

218
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
10,251,417✔
219
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
10,251,417✔
220
    if (code != 0) {
10,243,526✔
UNCOV
221
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
222
      TSDB_CHECK_CODE(code, lino, _exit);
×
223
    }
224

225
    ttlReq.nUids = taosArrayGetSize(tbUids);
10,243,526✔
226
    ttlReq.pTbUids = tbUids;
10,244,041✔
227
  }
228

229
  if (ttlReq.nUids == 0) {
10,244,041✔
230
    code = TSDB_CODE_MSG_PREPROCESSED;
10,226,899✔
231
    TSDB_CHECK_CODE(code, lino, _exit);
10,226,899✔
232
  }
233

234
  {  // prepare new content
235
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
8,596✔
236
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
11,319✔
237

238
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
11,319✔
239
    if (pContNew == NULL) {
11,319✔
UNCOV
240
      code = terrno;
×
UNCOV
241
      TSDB_CHECK_CODE(code, lino, _exit);
×
242
    }
243

244
    if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
11,319✔
245
      vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
11,319✔
246
    }
247
    pContNew->contLen = htonl(reqLenNew);
11,319✔
248
    pContNew->vgId = pContOld->vgId;
11,319✔
249

250
    rpcFreeCont(pContOld);
11,319✔
251
    pMsg->pCont = pContNew;
11,319✔
252
    pMsg->contLen = contLenNew;
11,319✔
253
  }
254

255
  code = 0;
11,319✔
256

257
_exit:
10,246,877✔
258
  taosArrayDestroy(tbUids);
10,248,591✔
259

260
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
10,230,926✔
UNCOV
261
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
262
           tstrerror(code), TMSG_INFO(pMsg->msgType));
263
  } else {
264
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
10,230,926✔
265
  }
266

267
  return code;
10,230,926✔
268
}
269

270
extern int64_t tsMaxKeyByPrecision[];
271
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
594,953,276✔
272
  int32_t code = 0;
594,953,276✔
273
  int32_t lino = 0;
594,953,276✔
274

275
  if (tStartDecode(pCoder) < 0) {
594,953,276✔
UNCOV
276
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
277
    TSDB_CHECK_CODE(code, lino, _exit);
×
278
  }
279

280
  SSubmitTbData submitTbData;
594,953,735✔
281
  uint8_t       version;
282
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
594,954,275✔
UNCOV
283
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
284
    TSDB_CHECK_CODE(code, lino, _exit);
×
285
  }
286
  version = (submitTbData.flags >> 8) & 0xff;
594,954,275✔
287
  submitTbData.flags = submitTbData.flags & 0xff;
594,954,275✔
288

289
  int64_t uid;
594,951,239✔
290
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
594,954,943✔
291
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
14,821,780✔
292
    TSDB_CHECK_CODE(code, lino, _exit);
14,819,880✔
293
  }
294

295
  // submit data
296
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
594,953,962✔
UNCOV
297
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
298
    TSDB_CHECK_CODE(code, lino, _exit);
×
299
  }
300

301
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
594,953,962✔
302
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
14,821,070✔
303
    pCoder->pos += sizeof(int64_t);
14,818,365✔
304
  } else {
305
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
580,131,783✔
UNCOV
306
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
307
      TSDB_CHECK_CODE(code, lino, _exit);
×
308
    }
309
  }
310

311
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
594,949,552✔
UNCOV
312
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
313
    TSDB_CHECK_CODE(code, lino, _exit);
×
314
  }
315

316
  // scan and check
317
  TSKEY now = btimeMs;
594,949,552✔
318
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
594,949,552✔
319
    now *= 1000;
2,434,783✔
320
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
592,516,299✔
321
    now *= 1000000;
1,422,708✔
322
  }
323

324
  int32_t keep = pVnode->config.tsdbCfg.keep2;
594,951,213✔
325

326
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
594,948,396✔
327
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
594,952,233✔
328
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
594,954,620✔
329
    uint64_t nColData;
6,345,314✔
330
    if (tDecodeU64v(pCoder, &nColData) < 0) {
6,344,795✔
UNCOV
331
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
332
      TSDB_CHECK_CODE(code, lino, _exit);
×
333
    }
334

335
    SColData colData = {0};
6,344,795✔
336
    code = tDecodeColData(version, pCoder, &colData, false);
6,343,250✔
337
    if (code) {
6,343,876✔
UNCOV
338
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
339
      TSDB_CHECK_CODE(code, lino, _exit);
×
340
    }
341

342
    if (colData.flag != HAS_VALUE) {
6,343,876✔
343
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
344
      TSDB_CHECK_CODE(code, lino, _exit);
×
345
    }
346

347
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
2,147,483,647✔
348
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
2,147,483,647✔
349
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
575✔
350
        TSDB_CHECK_CODE(code, lino, _exit);
575✔
351
      }
352
    }
353

354
    for (uint64_t i = 1; i < nColData; i++) {
19,554,259✔
355
      code = tDecodeColData(version, pCoder, &colData, true);
13,206,820✔
356
      if (code) {
13,207,438✔
UNCOV
357
        code = TSDB_CODE_INVALID_MSG;
×
UNCOV
358
        TSDB_CHECK_CODE(code, lino, _exit);
×
359
      }
360
    }
361
  } else {
362
    uint64_t nRow;
588,606,270✔
363
    if (tDecodeU64v(pCoder, &nRow) < 0) {
588,604,696✔
UNCOV
364
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
365
      TSDB_CHECK_CODE(code, lino, _exit);
×
366
    }
367

368
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
2,147,483,647✔
369
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
2,147,483,647✔
370
      pCoder->pos += pRow->len;
2,147,483,647✔
371
#ifndef NO_UNALIGNED_ACCESS
372
      if (pRow->ts < minKey || pRow->ts > maxKey) {
2,147,483,647✔
373
#else
374
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
375
      if (ts < minKey || ts > maxKey) {
376
#endif
377
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
65,323✔
378
        TSDB_CHECK_CODE(code, lino, _exit);
65,323✔
379
      }
380

381
      // Check pRow->sver
382
      if (pRow->sver != submitTbData.sver) {
2,147,483,647✔
UNCOV
383
        code = TSDB_CODE_INVALID_DATA_FMT;
×
UNCOV
384
        TSDB_CHECK_CODE(code, lino, _exit);
×
385
      }
386
    }
387
  }
388

389
  if (!tDecodeIsEnd(pCoder)) {
594,884,969✔
390
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
594,886,707✔
391
    pCoder->pos += sizeof(int64_t);
594,884,043✔
392
  }
393

394
  tEndDecode(pCoder);
594,882,905✔
395

396
_exit:
594,953,192✔
397
  if (code) {
594,954,377✔
398
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
65,898✔
399
           lino, tstrerror(code));
400
  }
401
  return code;
594,954,377✔
402
}
403
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
567,900,762✔
404
  int32_t code = 0;
567,900,762✔
405
  int32_t lino = 0;
567,900,762✔
406

407
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
567,900,762✔
408
    return TSDB_CODE_MSG_PREPROCESSED;
80✔
409
  }
410

411
  SDecoder *pCoder = &(SDecoder){0};
567,900,682✔
412

413
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
567,902,070✔
UNCOV
414
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
415
    TSDB_CHECK_CODE(code, lino, _exit);
×
416
  }
417

418
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
567,903,997✔
419

420
  if (tStartDecode(pCoder) < 0) {
567,902,329✔
UNCOV
421
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
422
    TSDB_CHECK_CODE(code, lino, _exit);
×
423
  }
424

425
  uint64_t nSubmitTbData;
567,901,383✔
426
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
567,902,078✔
UNCOV
427
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
428
    TSDB_CHECK_CODE(code, lino, _exit);
×
429
  }
430

431
  int64_t btimeMs = taosGetTimestampMs();
567,903,154✔
432
  int64_t ctimeMs = btimeMs;
567,903,154✔
433
  for (int32_t i = 0; i < nSubmitTbData; i++) {
1,162,786,731✔
434
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
594,953,494✔
435
    TSDB_CHECK_CODE(code, lino, _exit);
594,949,475✔
436
  }
437

438
  tEndDecode(pCoder);
567,833,237✔
439

440
_exit:
567,902,530✔
441
  tDecoderClear(pCoder);
567,902,530✔
442
  if (code) {
567,899,583✔
443
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
65,898✔
444
           tstrerror(code), TMSG_INFO(pMsg->msgType));
445
  }
446
  return code;
567,895,611✔
447
}
448

449
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,677,828✔
450
  int32_t code = 0;
1,677,828✔
451

452
  int32_t    size;
453
  int32_t    ret;
454
  uint8_t   *pCont;
455
  SEncoder  *pCoder = &(SEncoder){0};
1,677,828✔
456
  SDeleteRes res = {0};
1,677,828✔
457

458
  SReadHandle handle = {0};
1,677,828✔
459
  handle.vnode = pVnode;
1,677,828✔
460
  handle.pMsgCb = &pVnode->msgCb;
1,677,828✔
461
  handle.skipRollup = 1;
1,677,828✔
462
  initStorageAPI(&handle.api);
1,677,828✔
463

464
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
1,677,828✔
465
  if (code) goto _exit;
1,677,828✔
466

467
  res.ctimeMs = taosGetTimestampMs();
1,677,828✔
468
  // malloc and encode
469
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
1,677,828✔
470
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
1,677,828✔
471

472
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
1,677,828✔
473
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
1,677,828✔
474

475
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
1,677,828✔
476
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
1,677,828✔
UNCOV
477
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
478
  }
479
  tEncoderClear(pCoder);
1,677,828✔
480

481
  rpcFreeCont(pMsg->pCont);
1,677,828✔
482
  pMsg->pCont = pCont;
1,677,828✔
483
  pMsg->contLen = size + sizeof(SMsgHead);
1,677,828✔
484

485
  taosArrayDestroy(res.uidList);
1,677,828✔
486

487
_exit:
1,677,828✔
488
  return code;
1,677,828✔
489
}
490

UNCOV
491
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
UNCOV
492
  int32_t code = 0;
×
UNCOV
493
  int32_t lino = 0;
×
494

495
  int64_t         ctimeMs = taosGetTimestampMs();
×
496
  SBatchDeleteReq pReq = {0};
×
497
  SDecoder       *pCoder = &(SDecoder){0};
×
498

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

501
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
UNCOV
502
    code = TSDB_CODE_INVALID_MSG;
×
503
  }
504

505
  tDecoderClear(pCoder);
×
506
  taosArrayDestroy(pReq.deleteReqs);
×
507

UNCOV
508
  if (code) {
×
509
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
510
  } else {
UNCOV
511
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
×
512
  }
513
  return code;
×
514
}
515

516
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
49,629✔
517
  int32_t ret = 0;
49,629✔
518
  if ((ret = vnodeCheckState(pVnode)) != 0) {
49,629✔
519
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
13,680✔
520
    return 0;
13,680✔
521
  }
522

523
  SVArbCheckSyncReq syncReq = {0};
35,949✔
524

525
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
35,949✔
526
                                    &syncReq) != 0) {
UNCOV
527
    return TSDB_CODE_INVALID_MSG;
×
528
  }
529

530
  ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
35,949✔
531
  if (ret != 0) {
35,949✔
532
    vError("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
34,644✔
533
  }
534

535
  int32_t code = terrno;
35,949✔
536
  tFreeSVArbCheckSyncReq(&syncReq);
35,949✔
537

538
  return code;
35,949✔
539
}
540

541
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,338,288✔
542
  int32_t          code = TSDB_CODE_SUCCESS;
1,338,288✔
543
  int32_t          lino = 0;
1,338,288✔
544
  int32_t          size = 0;
1,338,288✔
545
  SDecoder         dc = {0};
1,338,288✔
546
  SEncoder         ec = {0};
1,338,288✔
547
  SVDropTbBatchReq receivedBatchReqs = {0};
1,338,288✔
548
  SVDropTbBatchReq sentBatchReqs = {0};
1,338,288✔
549

550
  if (pVnode->config.isAudit) {
1,338,288✔
NEW
551
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
552
  }
553

554
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
1,338,288✔
555

556
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
1,338,288✔
557
  if (code < 0) {
1,338,288✔
UNCOV
558
    terrno = code;
×
559
    TSDB_CHECK_CODE(code, lino, _exit);
×
560
  }
561
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
1,338,288✔
562
  if (!sentBatchReqs.pArray) {
1,338,288✔
UNCOV
563
    code = terrno;
×
UNCOV
564
    goto _exit;
×
565
  }
566

567
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
2,731,341✔
568
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
1,393,053✔
569
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
1,393,053✔
570
    if (uid == 0) {
1,393,053✔
571
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
572
      continue;
×
573
    }
574
    pReq->uid = uid;
1,393,053✔
575
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
1,393,053✔
576
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
2,786,106✔
UNCOV
577
      code = terrno;
×
UNCOV
578
      goto _exit;
×
579
    }
580
  }
581
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
1,338,288✔
582

583
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
1,338,288✔
584
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
1,338,288✔
585
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
1,338,288✔
586
  tEncoderClear(&ec);
1,338,288✔
587
  if (code != TSDB_CODE_SUCCESS) {
1,338,288✔
UNCOV
588
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
UNCOV
589
    TSDB_CHECK_CODE(code, lino, _exit);
×
590
  }
591

592
_exit:
1,338,288✔
593
  tDecoderClear(&dc);
1,338,288✔
594
  if (sentBatchReqs.pArray) {
1,338,288✔
595
    taosArrayDestroy(sentBatchReqs.pArray);
1,338,288✔
596
  }
597
  return code;
1,338,288✔
598
}
599

UNCOV
600
int32_t vnodePreProcessSsMigrateFileSetReq(SVnode *pVnode, SRpcMsg *pMsg) {
×
UNCOV
601
  int32_t              code = TSDB_CODE_SUCCESS, lino = 0;
×
UNCOV
602
  SSsMigrateFileSetReq req = {0};
×
603

UNCOV
604
  code = tDeserializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
×
UNCOV
605
                                          pMsg->contLen - sizeof(SMsgHead), &req);
×
UNCOV
606
  if (code < 0) {
×
UNCOV
607
    terrno = code;
×
608
    TSDB_CHECK_CODE(code, lino, _exit);
×
609
  }
610

UNCOV
611
  req.nodeId = vnodeNodeId(pVnode);
×
612
  TAOS_UNUSED(tSerializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
×
613
                                             pMsg->contLen - sizeof(SMsgHead), &req));
614

615
_exit:
×
616
  return code;
×
617
}
618

619
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
650,474,592✔
620
  int32_t code = 0;
650,474,592✔
621

622
  if (pVnode->mounted) {
650,474,592✔
623
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
1,340✔
624
           TMSG_INFO(pMsg->msgType));
625
    return TSDB_CODE_OPS_NOT_SUPPORT;
1,340✔
626
  }
627

628
  switch (pMsg->msgType) {
650,473,342✔
629
    case TDMT_VND_CREATE_TABLE: {
37,326,238✔
630
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
37,326,238✔
631
    } break;
37,324,532✔
632
    case TDMT_VND_ALTER_TABLE: {
11,788,301✔
633
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
11,788,301✔
634
    } break;
11,788,301✔
635
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
10,259,657✔
636
    case TDMT_VND_DROP_TTL_TABLE: {
637
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
10,259,657✔
638
    } break;
10,244,006✔
639
    case TDMT_VND_SUBMIT: {
567,904,494✔
640
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
567,904,494✔
641
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
642
    } break;
567,902,360✔
643
    case TDMT_VND_DELETE: {
1,677,828✔
644
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,677,828✔
645
    } break;
1,677,828✔
UNCOV
646
    case TDMT_VND_BATCH_DEL: {
×
UNCOV
647
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
UNCOV
648
    } break;
×
649
    case TDMT_VND_ARB_CHECK_SYNC: {
49,629✔
650
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
49,629✔
651
    } break;
49,629✔
652
    case TDMT_VND_DROP_TABLE: {
1,338,288✔
653
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,338,288✔
654
    } break;
1,338,288✔
655
#ifdef TD_ENTERPRISE
656
    case TDMT_VND_SSMIGRATE_FILESET: {
×
UNCOV
657
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
×
UNCOV
658
    } break;
×
659
#endif
660
    default:
20,130,230✔
661
      break;
20,130,230✔
662
  }
663

664
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
650,455,174✔
665
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
100,542✔
666
           TMSG_INFO(pMsg->msgType));
667
  }
668
  return code;
650,455,531✔
669
}
670

671
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
21,560✔
672
  int32_t code = 0;
21,560✔
673
  int32_t lino = 0;
21,560✔
674

675
  int64_t   st = taosGetTimestampUs();
21,560✔
676
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
21,560✔
677
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
21,560✔
678

679
  SBseBatch *pBatch = NULL;
21,560✔
680

681
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
21,560✔
682
  TSDB_CHECK_CODE(code, lino, _exit);
21,560✔
683

684
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
21,560✔
685
  int32_t rowIdx = -1;
21,560✔
686
  for (int32_t i = 0; i < sz; i++) {
24,154,318✔
687
    int64_t     seq = 0;
24,132,758✔
688
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
24,132,758✔
689
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
24,132,758✔
690
      // skip empty or null blob
691
      continue;
2,424,084✔
692
    }
693

694
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
21,708,674✔
695
    TSDB_CHECK_CODE(code, lino, _exit);
21,708,674✔
696

697
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
21,708,674✔
698
    if (row == NULL) {
21,708,674✔
UNCOV
699
      int32_t tlen = taosArrayGetSize(pBlobSet->pSeqTable);
×
UNCOV
700
      uTrace("blob invalid row index:%d, sz:%d, pBlobSet size:%d", rowIdx, sz, tlen);
×
UNCOV
701
      break;
×
702
    }
703

704
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
43,417,348✔
UNCOV
705
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
706
      TSDB_CHECK_CODE(code, lino, _exit);
×
707
    }
708
  }
709

710
  code = bseCommitBatch(pVnode->pBse, pBatch);
21,560✔
711
  TSDB_CHECK_CODE(code, lino, _exit);
21,560✔
712

713
  int64_t cost = taosGetTimestampUs() - st;
21,560✔
714
  if (cost >= 500) {
21,560✔
715
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
7,538✔
716
           pBlobSet->len);
717
  }
718
_exit:
21,560✔
719
  if (code != 0) {
21,560✔
UNCOV
720
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
721
  }
722
  return code;
21,560✔
723
}
724

UNCOV
725
static int32_t inline vnodeSubmitSubColBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
UNCOV
726
  int32_t code = 0;
×
UNCOV
727
  int32_t lino = 0;
×
728

UNCOV
729
  int32_t   blobColIdx = 0;
×
UNCOV
730
  SColData *pBlobCol = NULL;
×
UNCOV
731
  int64_t   st = taosGetTimestampUs();
×
UNCOV
732
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
733
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
734

735
  SBseBatch *pBatch = NULL;
×
736

737
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
738
  TSDB_CHECK_CODE(code, lino, _exit);
×
739

740
  SColData *p = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
×
741
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitTbData->aCol); i++) {
×
UNCOV
742
    if (IS_STR_DATA_BLOB(p[i].type)) {
×
743
      pBlobCol = &p[i];
×
UNCOV
744
      break;
×
745
    }
746
  }
UNCOV
747
  if (pBlobCol == NULL) {
×
748
    vError("vgId:%d %s failed to find blob column in submit data", TD_VID(pVnode), __func__);
×
749
    code = TSDB_CODE_INVALID_MSG;
×
750
    goto _exit;
×
751
  }
752

UNCOV
753
  int32_t offset = 0;
×
754
  // int32_t   rowIdx = -1;
755
  for (int32_t i = 0; i < sz; i++) {
×
756
    int64_t     seq = 0;
×
757
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
758
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
759
      // skip empty or null blob
UNCOV
760
      continue;
×
761
    }
UNCOV
762
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
763
    TSDB_CHECK_CODE(code, lino, _exit);
×
764

765
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
×
766
    offset += BSE_SEQUECE_SIZE;
×
767
  }
768

UNCOV
769
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
770
  TSDB_CHECK_CODE(code, lino, _exit);
×
771

UNCOV
772
  int64_t cost = taosGetTimestampUs() - st;
×
773
  if (cost >= 500) {
×
774
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
775
           pBlobSet->len);
776
  }
777
_exit:
×
778
  if (code != 0) {
×
UNCOV
779
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
780
  }
781
  return code;
×
782
}
783
static int32_t inline vnodeSubmitBlobData(SVnode *pVnode, SSubmitTbData *pData) {
21,560✔
784
  int32_t code = 0;
21,560✔
785
  if (pData->flags & SUBMIT_REQ_WITH_BLOB) {
21,560✔
786
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
21,560✔
787
      code = vnodeSubmitSubColBlobData(pVnode, pData);
×
788
    } else {
789
      code = vnodeSubmitSubRowBlobData(pVnode, pData);
21,560✔
790
    }
791
  }
792

793
  return code;
21,560✔
794
}
795

796
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
775,118,199✔
797
  int32_t code = 0;
775,118,199✔
798
  int32_t lino = 0;
775,118,199✔
799
  void   *ptr = NULL;
775,118,199✔
800
  void   *pReq;
801
  int32_t len;
802

803
  (void)taosThreadMutexLock(&pVnode->mutex);
775,118,199✔
804
  if (pVnode->disableWrite) {
775,185,034✔
UNCOV
805
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
UNCOV
806
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
UNCOV
807
    return TSDB_CODE_VND_WRITE_DISABLED;
×
808
  }
809
  (void)taosThreadMutexUnlock(&pVnode->mutex);
775,092,560✔
810

811
  if (ver <= pVnode->state.applied) {
775,196,182✔
UNCOV
812
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
813
           pVnode->state.applied);
814
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
815
  }
816

817
  vGDebug(&pMsg->info.traceId,
775,121,118✔
818
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
819
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
820
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
821
          pMsg->info.conn.applyTerm, pMsg->contLen);
822

823
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
775,165,335✔
UNCOV
824
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
825
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
UNCOV
826
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
827
  }
828

829
  if (!(pVnode->state.applied + 1 == ver)) {
775,197,987✔
UNCOV
830
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
831
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
832
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
833
  }
834

835
  atomic_store_64(&pVnode->state.applied, ver);
775,195,366✔
836
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
775,200,956✔
837

838
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
775,200,242✔
839

840
  // skip header
841
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
764,478,405✔
842
  len = pMsg->contLen - sizeof(SMsgHead);
764,479,632✔
843
  bool needCommit = false;
764,476,764✔
844
  bool forceTrimWal = false;
764,476,764✔
845

846
  switch (pMsg->msgType) {
764,476,764✔
847
    /* META */
848
    case TDMT_VND_CREATE_STB:
5,049,302✔
849
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
5,049,302✔
850
      TSDB_CHECK_CODE(code, lino, _err);
5,050,264✔
851
      break;
5,048,050✔
852
    case TDMT_VND_ALTER_STB:
7,529,762✔
853
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,529,762✔
854
      TSDB_CHECK_CODE(code, lino, _err);
7,529,868✔
855
      break;
7,529,868✔
856
    case TDMT_VND_DROP_STB:
892,006✔
857
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
892,006✔
858
      TSDB_CHECK_CODE(code, lino, _err);
892,006✔
859
      break;
892,006✔
860
    case TDMT_VND_CREATE_TABLE:
40,136,426✔
861
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
40,136,426✔
862
      TSDB_CHECK_CODE(code, lino, _err);
40,133,399✔
863
      break;
40,133,399✔
864
    case TDMT_VND_ALTER_TABLE:
11,789,007✔
865
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
11,789,007✔
866
      TSDB_CHECK_CODE(code, lino, _err);
11,789,007✔
867
      break;
11,789,007✔
868
    case TDMT_VND_DROP_TABLE:
1,338,616✔
869
    case TDMT_VND_SNODE_DROP_TABLE:
870
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,338,616✔
871
      TSDB_CHECK_CODE(code, lino, _err);
1,338,616✔
872
      break;
1,338,616✔
873
    case TDMT_VND_CREATE_RSMA:
22,140✔
874
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
22,140✔
875
      TSDB_CHECK_CODE(code, lino, _err);
22,140✔
876
      break;
22,140✔
877
    case TDMT_VND_DROP_RSMA:
4,428✔
878
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
4,428✔
879
      TSDB_CHECK_CODE(code, lino, _err);
4,428✔
880
      break;
4,428✔
881
    case TDMT_VND_ALTER_RSMA:
11,808✔
882
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
11,808✔
883
      TSDB_CHECK_CODE(code, lino, _err);
11,808✔
884
      break;
11,808✔
UNCOV
885
    case TDMT_VND_DROP_TTL_TABLE:
×
UNCOV
886
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
887
      TSDB_CHECK_CODE(code, lino, _err);
×
UNCOV
888
      break;
×
889
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,319✔
890
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
11,319✔
891
      TSDB_CHECK_CODE(code, lino, _err);
11,319✔
892
      break;
11,319✔
893
    case TDMT_VND_TRIM:
77,312✔
894
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
77,312✔
895
      TSDB_CHECK_CODE(code, lino, _err);
77,312✔
896
      break;
77,312✔
897
      case TDMT_VND_TRIM_WAL:
6,180✔
898
        needCommit = true;
6,180✔
899
        forceTrimWal = true;
6,180✔
900
        break;
6,180✔
901
#ifdef TD_ENTERPRISE
UNCOV
902
    case TDMT_VND_SSMIGRATE_FILESET:
×
UNCOV
903
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
UNCOV
904
      break;
×
UNCOV
905
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
UNCOV
906
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
UNCOV
907
      break;
×
UNCOV
908
    case TDMT_VND_KILL_SSMIGRATE:
×
UNCOV
909
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
910
      break;
×
911
#endif
912

913
    /* TSDB */
914
    case TDMT_VND_SUBMIT: {
679,646,325✔
915
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
679,646,325✔
916
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
917
      });
918
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
679,604,220✔
919
      break;
679,584,937✔
920
    }
921
    case TDMT_VND_DELETE:
2,047,311✔
922
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,047,311✔
923
      TSDB_CHECK_CODE(code, lino, _err);
2,047,311✔
924
      break;
2,047,311✔
UNCOV
925
    case TDMT_VND_BATCH_DEL:
×
UNCOV
926
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
927
      TSDB_CHECK_CODE(code, lino, _err);
×
UNCOV
928
      break;
×
929
      /* TQ */
930
#if defined(USE_TQ) || defined(USE_STREAM)
931
    case TDMT_VND_TMQ_SUBSCRIBE:
1,257,169✔
932
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,257,169✔
933
      TSDB_CHECK_CODE(code, lino, _err);
1,252,621✔
934
      break;
1,252,621✔
935
    case TDMT_VND_TMQ_DELETE_SUB:
283,604✔
936
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
283,604✔
937
      TSDB_CHECK_CODE(code, lino, _err);
283,297✔
938
      break;
283,297✔
939
    case TDMT_VND_TMQ_COMMIT_OFFSET:
4,853,766✔
940
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
4,853,766✔
941
      TSDB_CHECK_CODE(code, lino, _err);
4,852,814✔
942
      break;
4,852,814✔
943
#endif
944
    case TDMT_VND_ALTER_CONFIRM:
4,302,416✔
945
      needCommit = pVnode->config.hashChange;
4,302,416✔
946
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
4,302,416✔
947
      TSDB_CHECK_CODE(code, lino, _err);
4,302,416✔
948
      break;
4,302,416✔
949
    case TDMT_VND_ALTER_CONFIG:
313,090✔
950
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
313,090✔
951
      break;
313,090✔
952
    case TDMT_VND_COMMIT:
4,824,162✔
953
      needCommit = true;
4,824,162✔
954
      break;
4,824,162✔
955
    case TDMT_VND_CREATE_INDEX:
4,434✔
956
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
4,434✔
957
      break;
4,434✔
958
    case TDMT_VND_DROP_INDEX:
2,940✔
959
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,940✔
960
      break;
2,940✔
961
    case TDMT_VND_COMPACT:
66,329✔
962
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
66,329✔
963
      goto _exit;
66,329✔
964
    case TDMT_VND_SCAN:
290✔
965
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
290✔
966
      goto _exit;
290✔
UNCOV
967
    case TDMT_SYNC_CONFIG_CHANGE:
×
UNCOV
968
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
969
      break;
×
970
#ifdef TD_ENTERPRISE
971
    case TDMT_VND_KILL_COMPACT:
1,476✔
972
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,476✔
973
      break;
1,476✔
974
#endif
975
    case TDMT_VND_KILL_SCAN:
116✔
976
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
116✔
977
      break;
116✔
UNCOV
978
    case TDMT_VND_KILL_TRIM:
×
UNCOV
979
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
UNCOV
980
      break;
×
981
    /* ARB */
982
    case TDMT_VND_ARB_CHECK_SYNC:
2,575✔
983
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
2,575✔
984
      break;
2,575✔
UNCOV
985
    default:
×
986
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
987
      return TSDB_CODE_INVALID_MSG;
×
988
  }
989

990
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
764,336,322✔
991

992
  walApplyVer(pVnode->pWal, ver);
764,357,479✔
993

994
  if (pVnode->pTq) {
764,369,464✔
995
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
764,369,515✔
996
    if (code) {
764,369,164✔
UNCOV
997
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
UNCOV
998
      return code;
×
999
    }
1000
  }
1001

1002
  // commit if need
1003
  if (needCommit) {
764,369,164✔
1004
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
4,854,139✔
1005
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
4,854,578✔
1006
    if (code) {
4,854,578✔
UNCOV
1007
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
UNCOV
1008
      goto _err;
×
1009
    }
1010

1011
    // start a new one
1012
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
4,854,578✔
1013
      code = vnodeBegin(pVnode);
1014
      if (code) {
1015
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
1016
        goto _err;
1017
      }
1018
    });
1019
  }
1020

1021
_exit:
764,367,544✔
1022
  return 0;
775,151,478✔
1023

1024
_err:
42,912✔
1025
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
42,912✔
1026
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
2,214✔
1027
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1028
  } else if (code) {
40,698✔
UNCOV
1029
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1030
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1031
  }
1032

1033
  return code;
42,912✔
1034
}
1035

1036
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
335,341,531✔
1037
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
335,341,531✔
1038
    return 0;
77,646,426✔
1039
  }
1040

1041
  int32_t qType = 0;
257,735,710✔
1042
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
257,723,718✔
1043
}
1044

1045
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
335,401,640✔
1046
  const STraceId *trace = &pMsg->info.traceId;
335,401,640✔
1047
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
335,406,531✔
1048
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1049

1050
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
335,430,138✔
1051
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
346,491✔
1052
    return 0;
346,491✔
1053
  }
1054

1055
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
335,088,219✔
UNCOV
1056
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
UNCOV
1057
    return 0;
×
1058
  }
1059

1060
  SReadHandle handle = {0};
335,090,143✔
1061
  handle.vnode = pVnode;
335,090,262✔
1062
  handle.pMsgCb = &pVnode->msgCb;
335,090,262✔
1063
  handle.pWorkerCb = pInfo->workerCb;
335,064,934✔
1064
  initStorageAPI(&handle.api);
335,065,506✔
1065
  int32_t code = TSDB_CODE_SUCCESS;
334,925,555✔
1066
  bool    redirected = false;
334,925,555✔
1067

1068
  switch (pMsg->msgType) {
334,925,555✔
1069
    case TDMT_SCH_QUERY:
202,349,990✔
1070
      if (!syncIsReadyForRead(pVnode->sync)) {
202,349,990✔
1071
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
2,067,700✔
1072
        redirected = true;
2,067,700✔
1073
      }
1074
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
202,397,197✔
1075
      if (redirected) {
202,370,821✔
1076
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
2,067,700✔
1077
        return 0;
2,067,700✔
1078
      }
1079

1080
      return code;
200,303,121✔
1081
    case TDMT_SCH_MERGE_QUERY:
55,357,359✔
1082
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
55,357,359✔
1083
    case TDMT_SCH_QUERY_CONTINUE:
40,871,380✔
1084
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
40,871,380✔
1085
    case TDMT_VND_TMQ_CONSUME:
27,772,638✔
1086
      return tqProcessPollReq(pVnode->pTq, pMsg);
27,772,638✔
1087
    case TDMT_VND_TMQ_CONSUME_PUSH:
8,654,211✔
1088
      return tqProcessPollPush(pVnode->pTq);
8,654,211✔
UNCOV
1089
    default:
×
UNCOV
1090
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
UNCOV
1091
      return TSDB_CODE_APP_ERROR;
×
1092
  }
1093
}
1094

1095
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
730,289,836✔
1096
  int32_t code = TSDB_CODE_SUCCESS;
730,289,836✔
1097
  const STraceId *trace = &pMsg->info.traceId;
730,289,836✔
1098
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
730,349,223✔
1099
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1100
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
730,498,368✔
1101
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
498,109,794✔
1102
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
471,226,452✔
1103
      !syncIsReadyForRead(pVnode->sync)) {
259,243,710✔
1104
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
606,160✔
1105
    return 0;
606,160✔
1106
  }
1107

1108
  switch (pMsg->msgType) {
729,881,178✔
1109
    case TDMT_SCH_FETCH:
309,156,912✔
1110
    case TDMT_SCH_MERGE_FETCH:
1111
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
309,156,912✔
UNCOV
1112
    case TDMT_SCH_FETCH_RSP:
×
UNCOV
1113
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1114
    // case TDMT_SCH_CANCEL_TASK:
1115
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1116
    case TDMT_SCH_DROP_TASK:
257,526,418✔
1117
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
257,526,418✔
1118
    case TDMT_SCH_TASK_NOTIFY:
12,063✔
1119
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
12,063✔
1120
    case TDMT_SCH_QUERY_HEARTBEAT:
135,776,623✔
1121
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
135,776,623✔
1122
    case TDMT_VND_TABLE_META:
885,781✔
1123
    case TDMT_VND_TABLE_NAME:
1124
      return vnodeGetTableMeta(pVnode, pMsg, true);
885,781✔
UNCOV
1125
    case TDMT_VND_TABLE_CFG:
×
UNCOV
1126
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1127
    case TDMT_VND_BATCH_META: {
26,277,667✔
1128
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
26,277,667✔
1129
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1130
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
26,283,869✔
1131
      return code;
26,276,129✔
1132
    }
1133
    case TDMT_VND_VSUBTABLES_META:
×
1134
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
UNCOV
1135
    case TDMT_VND_VSTB_REF_DBS:
×
UNCOV
1136
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1137
    case TDMT_VND_QUERY_SCAN_PROGRESS:
580✔
1138
      return vnodeQueryScanProgress(pVnode, pMsg);
580✔
1139
#ifdef TD_ENTERPRISE
1140
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
104,769✔
1141
      return vnodeQueryCompactProgress(pVnode, pMsg);
104,769✔
1142

1143
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
×
1144
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
×
1145

UNCOV
1146
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
UNCOV
1147
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1148
#endif
1149
    case TDMT_VND_QUERY_TRIM_PROGRESS:
113,676✔
1150
      return vnodeQueryRetentionProgress(pVnode, pMsg);
113,676✔
1151
      //    case TDMT_VND_TMQ_CONSUME:
1152
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1153
#ifdef USE_TQ
1154
    case TDMT_VND_TMQ_VG_WALINFO:
610✔
1155
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
610✔
1156
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
305✔
1157
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
305✔
1158
    case TDMT_VND_TMQ_SEEK:
1,220✔
1159
      return tqProcessSeekReq(pVnode->pTq, pMsg);
1,220✔
1160
#endif
1161
    default:
16,977✔
1162
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
16,977✔
UNCOV
1163
      return TSDB_CODE_APP_ERROR;
×
1164
  }
1165
}
1166

1167
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
65,812,679✔
1168
  if (NULL == pMetaRsp) {
65,812,679✔
UNCOV
1169
    return;
×
1170
  }
1171

1172
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
65,812,679✔
1173
  pMetaRsp->dbId = pVnode->config.dbId;
65,812,098✔
1174
  pMetaRsp->vgId = TD_VID(pVnode);
65,813,030✔
1175
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
65,812,109✔
1176
}
1177

1178
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1179

1180
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
77,312✔
1181
  if (!pVnode->restored) {
77,312✔
1182
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
2,766✔
1183
    return 0;
2,766✔
1184
  }
1185

1186
  int32_t     code = 0;
74,546✔
1187
  SVTrimDbReq trimReq = {0};
74,546✔
1188

1189
  // decode
1190
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
74,053✔
UNCOV
1191
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1192
    goto _exit;
×
1193
  }
1194

1195
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
74,546✔
1196
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1197

1198
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
74,546✔
1199

1200
_exit:
74,546✔
1201
  return code;
74,546✔
1202
}
1203

1204
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1205

1206
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1207

UNCOV
1208
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
1209
  int32_t code = 0;
×
1210

UNCOV
1211
  SSsMigrateFileSetReq req = {0};
×
UNCOV
1212
  SSsMigrateFileSetRsp rsp = {0};
×
UNCOV
1213
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
×
UNCOV
1214
  pRsp->code = 0;
×
UNCOV
1215
  pRsp->pCont = NULL;
×
1216
  pRsp->contLen = 0;
×
1217

UNCOV
1218
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
×
1219
    code = TSDB_CODE_INVALID_MSG;
×
1220
    goto _exit;
×
1221
  }
1222

1223
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId,
×
1224
        req.ssMigrateId, req.fid, req.startTimeSec);
UNCOV
1225
  rsp.ssMigrateId = req.ssMigrateId;
×
1226
  rsp.vgId = TD_VID(pVnode);
×
1227
  rsp.nodeId = req.nodeId;
×
1228
  rsp.fid = req.fid;
×
1229

UNCOV
1230
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
×
1231
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
1232
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1233
    pRsp->code = code;
×
1234
    goto _exit;
×
1235
  }
1236

UNCOV
1237
  pRsp->code = TSDB_CODE_SUCCESS;
×
1238
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
×
1239
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1240
  if (pRsp->pCont == NULL) {
×
1241
    vError("vgId:%d, failed to allocate memory for ssmigrate fileset response", TD_VID(pVnode));
×
1242
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1243
    goto _exit;
×
1244
  }
1245
  TAOS_UNUSED(tSerializeSSsMigrateFileSetRsp(pRsp->pCont, pRsp->contLen, &rsp));
×
1246

1247
_exit:
×
1248
  pRsp->code = code;
×
1249
  return code;
×
1250
}
1251

1252
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1253

UNCOV
1254
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1255
  int32_t            code = 0;
×
1256
  SSsMigrateProgress req = {0};
×
1257

1258
  // decode
UNCOV
1259
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
×
UNCOV
1260
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1261
    goto _exit;
×
1262
  }
1263

1264
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1265

UNCOV
1266
_exit:
×
1267
  pRsp->code = code;
×
1268
  return code;
×
1269
}
1270

1271
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1272

UNCOV
1273
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1274
  int32_t          code = 0;
×
1275
  SVnodeKillSsMigrateReq req = {0};
×
1276

1277
  // decode
UNCOV
1278
  if (tDeserializeSVnodeKillSsMigrateReq(pReq, len, &req) != 0) {
×
UNCOV
1279
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1280
    goto _exit;
×
1281
  }
1282

1283
  code = vnodeKillSsMigrate(pVnode, &req);
×
1284

UNCOV
1285
_exit:
×
1286
  pRsp->code = code;
×
1287
  return code;
×
1288
}
1289

UNCOV
1290
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1291
  int               ret = 0;
×
UNCOV
1292
  SVDropTtlTableReq ttlReq = {0};
×
1293
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1294
    ret = TSDB_CODE_INVALID_MSG;
×
1295
    goto end;
×
1296
  }
1297

1298
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1299
    ret = TSDB_CODE_INVALID_MSG;
×
1300
    goto end;
×
1301
  }
1302

1303
  if (ttlReq.nUids != 0) {
×
UNCOV
1304
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1305
          ttlReq.nUids);
1306
  }
1307

1308
  if (ttlReq.nUids > 0) {
×
UNCOV
1309
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
UNCOV
1310
    if (code) return code;
×
1311

1312
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
UNCOV
1313
    if (code) {
×
UNCOV
1314
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1315
    }
1316
  }
1317

1318
end:
×
UNCOV
1319
  taosArrayDestroy(ttlReq.pTbUids);
×
1320
  return ret;
×
1321
}
1322

1323
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,319✔
1324
  int32_t                 code = -1;
11,319✔
1325
  SMetaReader             mr = {0};
11,319✔
1326
  SVDropTtlTableReq       ttlReq = {0};
11,319✔
1327
  SVFetchTtlExpiredTbsRsp rsp = {0};
11,319✔
1328
  SEncoder                encoder = {0};
11,319✔
1329
  SArray                 *pNames = NULL;
11,319✔
1330
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
11,319✔
1331
  pRsp->code = TSDB_CODE_SUCCESS;
11,319✔
1332
  pRsp->pCont = NULL;
11,319✔
1333
  pRsp->contLen = 0;
11,319✔
1334

1335
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
11,319✔
UNCOV
1336
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1337
    goto _end;
×
1338
  }
1339

1340
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
11,319✔
UNCOV
1341
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1342
    goto _end;
×
1343
  }
1344

1345
  tb_uid_t    suid;
1346
  char        ctbName[TSDB_TABLE_NAME_LEN];
1347
  SVDropTbReq expiredTb = {.igNotExists = true};
11,319✔
1348
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
11,319✔
1349
  rsp.vgId = TD_VID(pVnode);
11,319✔
1350
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
11,319✔
1351
  if (!rsp.pExpiredTbs) goto _end;
11,319✔
1352

1353
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
11,319✔
1354
  if (!pNames) {
11,319✔
UNCOV
1355
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1356
    goto _end;
×
1357
  }
1358
  char buf[TSDB_TABLE_NAME_LEN];
11,319✔
1359
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
55,335✔
1360
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
44,016✔
1361
    expiredTb.suid = *uid;
44,016✔
1362
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
44,016✔
1363
    if (terrno < 0) goto _end;
44,016✔
1364
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
44,016✔
1365
    void *p = taosArrayPush(pNames, buf);
44,016✔
1366
    if (p == NULL) {
44,016✔
UNCOV
1367
      goto _end;
×
1368
    }
1369

1370
    expiredTb.name = p;
44,016✔
1371
    if (mr.me.type == TSDB_CHILD_TABLE) {
44,016✔
1372
      expiredTb.suid = mr.me.ctbEntry.suid;
29,540✔
1373
    }
1374

1375
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
88,032✔
UNCOV
1376
      goto _end;
×
1377
    }
1378
  }
1379

1380
  int32_t ret = 0;
11,319✔
1381
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
11,319✔
1382
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,319✔
1383
  if (pRsp->pCont == NULL) {
11,319✔
1384
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1385
    code = -1;
×
UNCOV
1386
    goto _end;
×
1387
  }
1388
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
11,319✔
1389
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
11,319✔
1390
  tEncoderClear(&encoder);
11,319✔
1391

1392
  if (terrno == 0) code = 0;
11,319✔
1393
_end:
11,319✔
1394
  metaReaderClear(&mr);
11,319✔
1395
  tFreeFetchTtlExpiredTbsRsp(&rsp);
11,319✔
1396
  taosArrayDestroy(ttlReq.pTbUids);
11,319✔
1397
  if (pNames) taosArrayDestroy(pNames);
11,319✔
1398
  pRsp->code = terrno;
11,319✔
1399
  return code;
11,319✔
1400
}
1401

1402
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
5,041,119✔
1403
  int32_t        code = 0;
5,041,119✔
1404
  SVCreateStbReq req = {0};
5,041,119✔
1405
  SDecoder       coder;
5,049,435✔
1406

1407
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
5,049,795✔
1408
  pRsp->code = TSDB_CODE_SUCCESS;
5,049,795✔
1409
  pRsp->pCont = NULL;
5,049,795✔
1410
  pRsp->contLen = 0;
5,048,896✔
1411

1412
  // decode and process req
1413
  tDecoderInit(&coder, pReq, len);
5,047,646✔
1414

1415
  code = tDecodeSVCreateStbReq(&coder, &req);
5,049,507✔
1416
  if (code) {
5,033,317✔
UNCOV
1417
    pRsp->code = code;
×
UNCOV
1418
    goto _err;
×
1419
  }
1420

1421
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
5,033,317✔
1422
  if (code) {
5,049,237✔
1423
    pRsp->code = code;
2,214✔
1424
    goto _err;
2,214✔
1425
  }
1426

1427
  tDecoderClear(&coder);
5,047,023✔
1428
  return 0;
5,048,050✔
1429

1430
_err:
2,214✔
1431
  tDecoderClear(&coder);
2,214✔
1432
  return code;
2,214✔
1433
}
1434

1435
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
40,136,426✔
1436
                                       SRpcMsg *pOriginRpc) {
1437
  SDecoder           decoder = {0};
40,136,426✔
1438
  SEncoder           encoder = {0};
40,136,426✔
1439
  int32_t            rcode = 0;
40,136,426✔
1440
  SVCreateTbBatchReq req = {0};
40,136,426✔
1441
  SVCreateTbReq     *pCreateReq;
1442
  SVCreateTbBatchRsp rsp = {0};
40,136,426✔
1443
  SVCreateTbRsp      cRsp = {0};
40,136,426✔
1444
  char               tbName[TSDB_TABLE_FNAME_LEN];
40,135,592✔
1445
  SArray            *tbUids = NULL;
40,136,426✔
1446
  SArray            *tbNames = NULL;
40,136,426✔
1447
  int64_t            tss = taosGetTimestampMs();
40,136,426✔
1448
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
40,136,426✔
1449
  pRsp->code = TSDB_CODE_SUCCESS;
40,135,594✔
1450
  pRsp->pCont = NULL;
40,136,426✔
1451
  pRsp->contLen = 0;
40,136,426✔
1452

1453
  // decode
1454
  tDecoderInit(&decoder, pReq, len);
40,136,426✔
1455
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
40,136,426✔
UNCOV
1456
    rcode = -1;
×
UNCOV
1457
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1458
    goto _exit;
×
1459
  }
1460

1461
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
40,131,463✔
1462
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
40,132,870✔
1463
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
40,131,200✔
1464
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
40,130,248✔
1465
    rcode = -1;
×
1466
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1467
    goto _exit;
×
1468
  }
1469

1470
  // loop to create table
1471
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
87,832,973✔
1472
    pCreateReq = req.pReqs + iReq;
47,699,287✔
1473
    memset(&cRsp, 0, sizeof(cRsp));
47,695,630✔
1474

1475
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
47,695,630✔
1476
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
80✔
1477
      if (str == NULL) {
80✔
UNCOV
1478
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1479
        rcode = -1;
×
UNCOV
1480
        goto _exit;
×
1481
      }
1482
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
80✔
1483
      if (taosArrayPush(tbNames, &str) == NULL) {
80✔
UNCOV
1484
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1485
        rcode = -1;
×
1486
        goto _exit;
×
1487
      }
1488
    }
1489

1490
    // validate hash
1491
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
47,695,630✔
1492
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
47,696,635✔
1493
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1494
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
UNCOV
1495
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1496
        rcode = -1;
×
UNCOV
1497
        goto _exit;
×
1498
      }
UNCOV
1499
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
UNCOV
1500
      continue;
×
1501
    }
1502

1503
    // do create table
1504
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
47,687,639✔
1505
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
17,448✔
1506
        cRsp.code = TSDB_CODE_SUCCESS;
5,099✔
1507
      } else {
1508
        cRsp.code = terrno;
12,349✔
1509
      }
1510
    } else {
1511
      cRsp.code = TSDB_CODE_SUCCESS;
47,673,036✔
1512
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
95,357,388✔
UNCOV
1513
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1514
        rcode = -1;
×
UNCOV
1515
        goto _exit;
×
1516
      }
1517
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
47,684,352✔
1518
    }
1519

1520
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
95,401,262✔
1521
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1522
      rcode = -1;
×
1523
      goto _exit;
×
1524
    }
1525
  }
1526

1527
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
40,133,686✔
1528
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
40,133,686✔
1529
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1530
  }
1531

1532
  // prepare rsp
1533
  int32_t ret = 0;
40,136,426✔
1534
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
40,136,426✔
1535
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
40,130,154✔
1536
  if (pRsp->pCont == NULL) {
40,125,526✔
1537
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1538
    rcode = -1;
×
UNCOV
1539
    goto _exit;
×
1540
  }
1541
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
40,130,326✔
1542
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
40,127,564✔
UNCOV
1543
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1544
  }
1545

1546
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
40,131,156✔
1547
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
80✔
1548

1549
    SName name = {0};
80✔
1550
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
80✔
1551
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1552
    }
1553

1554
    SStringBuilder sb = {0};
80✔
1555
    for (int32_t i = 0; i < tbNames->size; i++) {
160✔
1556
      char **key = (char **)taosArrayGet(tbNames, i);
80✔
1557
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
80✔
1558
      if (i < tbNames->size - 1) {
80✔
1559
        taosStringBuilderAppendChar(&sb, ',');
×
1560
      }
1561
      // taosMemoryFreeClear(*key);
1562
    }
1563

1564
    size_t len = 0;
80✔
1565
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
80✔
1566

1567
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
80✔
1568
      int64_t tse = taosGetTimestampMs();
80✔
1569
      double  duration = (double)(tse - tss);
80✔
1570
      duration = duration / 1000;
80✔
1571
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len, duration, 0);
80✔
1572
    }
1573

1574
    taosStringBuilderDestroy(&sb);
80✔
1575
  }
1576

1577
_exit:
40,131,156✔
1578
  tDeleteSVCreateTbBatchReq(&req);
40,127,790✔
1579
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
40,127,812✔
1580
  taosArrayDestroy(tbUids);
40,113,494✔
1581
  tDecoderClear(&decoder);
40,115,317✔
1582
  tEncoderClear(&encoder);
40,132,241✔
1583
  taosArrayDestroyP(tbNames, NULL);
40,128,731✔
1584
  return rcode;
40,124,134✔
1585
}
1586

1587
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,530,390✔
1588
  int32_t        code = 0;
7,530,390✔
1589
  SVCreateStbReq req = {0};
7,530,390✔
1590
  SDecoder       dc = {0};
7,530,390✔
1591

1592
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
7,530,390✔
1593
  pRsp->code = TSDB_CODE_SUCCESS;
7,530,390✔
1594
  pRsp->pCont = NULL;
7,530,390✔
1595
  pRsp->contLen = 0;
7,530,390✔
1596

1597
  tDecoderInit(&dc, pReq, len);
7,530,390✔
1598

1599
  // decode req
1600
  code = tDecodeSVCreateStbReq(&dc, &req);
7,529,762✔
1601
  if (code) {
7,512,816✔
UNCOV
1602
    tDecoderClear(&dc);
×
UNCOV
1603
    return code;
×
1604
  }
1605

1606
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,512,816✔
1607
  if (code) {
7,529,868✔
UNCOV
1608
    pRsp->code = code;
×
UNCOV
1609
    tDecoderClear(&dc);
×
1610
    return code;
×
1611
  }
1612

1613
  tDecoderClear(&dc);
7,529,868✔
1614

1615
  return 0;
7,529,868✔
1616
}
1617

1618
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
892,006✔
1619
  SVDropStbReq req = {0};
892,006✔
1620
  int32_t      rcode = TSDB_CODE_SUCCESS;
892,006✔
1621
  SDecoder     decoder = {0};
892,006✔
1622
  SArray      *tbUidList = NULL;
892,006✔
1623

1624
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
892,006✔
1625
  pRsp->pCont = NULL;
892,006✔
1626
  pRsp->contLen = 0;
892,006✔
1627

1628
  // decode request
1629
  tDecoderInit(&decoder, pReq, len);
892,006✔
1630
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
892,006✔
UNCOV
1631
    rcode = TSDB_CODE_INVALID_MSG;
×
UNCOV
1632
    goto _exit;
×
1633
  }
1634

1635
  STraceId* trace = &(pOriginRpc->info.traceId);
891,432✔
1636

1637
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
891,432✔
1638
              trace ? trace->msgId : 0, req.name);
1639

1640
  // process request
1641
  tbUidList = taosArrayInit(8, sizeof(int64_t));
892,744✔
1642
  if (tbUidList == NULL) goto _exit;
892,006✔
1643
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
892,006✔
1644
    rcode = terrno;
1,854✔
1645
    goto _exit;
1,854✔
1646
  }
1647

1648
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
890,152✔
UNCOV
1649
    rcode = terrno;
×
UNCOV
1650
    goto _exit;
×
1651
  }
1652

1653
  // return rsp
1654
_exit:
892,006✔
1655
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
892,006✔
1656
              trace ? trace->msgId : 0, req.name);
1657
  if (tbUidList) taosArrayDestroy(tbUidList);
892,006✔
1658
  pRsp->code = rcode;
892,006✔
1659
  tDecoderClear(&decoder);
892,006✔
1660
  return 0;
892,006✔
1661
}
1662

1663
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,789,007✔
1664
  SVAlterTbReq  vAlterTbReq = {0};
11,789,007✔
1665
  SVAlterTbRsp  vAlterTbRsp = {0};
11,789,007✔
1666
  SDecoder      dc = {0};
11,789,007✔
1667
  int32_t       code = 0;
11,789,007✔
1668
  int32_t       lino = 0;
11,789,007✔
1669
  int32_t       ret;
1670
  SEncoder      ec = {0};
11,789,007✔
1671
  STableMetaRsp vMetaRsp = {0};
11,789,007✔
1672

1673
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
11,789,007✔
1674
  pRsp->pCont = NULL;
11,789,007✔
1675
  pRsp->contLen = 0;
11,789,007✔
1676
  pRsp->code = TSDB_CODE_SUCCESS;
11,789,007✔
1677

1678
  tDecoderInit(&dc, pReq, len);
11,789,007✔
1679

1680
  // decode
1681
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
11,789,007✔
UNCOV
1682
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
UNCOV
1683
    tDecoderClear(&dc);
×
UNCOV
1684
    goto _exit;
×
1685
  }
1686

1687
  // process
1688
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
11,789,007✔
1689
    vAlterTbRsp.code = terrno;
681,728✔
1690
    tDecoderClear(&dc);
681,728✔
1691
    goto _exit;
681,728✔
1692
  }
1693
  tDecoderClear(&dc);
11,107,279✔
1694

1695
  if (NULL != vMetaRsp.pSchemas) {
11,107,279✔
1696
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,654,025✔
1697
    vAlterTbRsp.pMeta = &vMetaRsp;
3,654,025✔
1698
  }
1699

1700
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
11,107,279✔
1701
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
3,686,791✔
1702
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
7,423,104✔
1703
    if (uid == 0) {
7,423,104✔
UNCOV
1704
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1705
             vAlterTbReq.tbName);
UNCOV
1706
      goto _exit;
×
1707
    }
1708

1709
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
7,423,104✔
1710
    void   *p = taosArrayPush(tbUids, &uid);
7,423,104✔
1711
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
7,423,104✔
1712

1713
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
7,423,104✔
1714
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
7,423,104✔
UNCOV
1715
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1716
    }
1717

1718
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
7,423,104✔
1719
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
7,423,104✔
UNCOV
1720
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1721
    }
1722

1723
    taosArrayDestroy(tbUids);
7,423,104✔
1724
  }
1725

1726
_exit:
11,789,007✔
1727
  taosArrayDestroy(vAlterTbReq.pMultiTag);
11,789,007✔
1728
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
11,789,007✔
1729
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,789,007✔
1730
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
11,789,007✔
1731
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
11,789,007✔
UNCOV
1732
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1733
  }
1734

1735
  tEncoderClear(&ec);
11,789,007✔
1736
  if (vMetaRsp.pSchemas) {
11,789,007✔
1737
    taosMemoryFree(vMetaRsp.pSchemas);
3,654,025✔
1738
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,654,025✔
1739
  }
1740
  if (vMetaRsp.pColRefs) {
11,789,007✔
1741
    taosMemoryFree(vMetaRsp.pColRefs);
259,967✔
1742
  }
1743
  return 0;
11,789,007✔
1744
}
1745

1746
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,338,616✔
1747
                                     SRpcMsg *pOriginRpc) {
1748
  SVDropTbBatchReq req = {0};
1,338,616✔
1749
  SVDropTbBatchRsp rsp = {0};
1,338,616✔
1750
  SDecoder         decoder = {0};
1,338,616✔
1751
  SEncoder         encoder = {0};
1,338,616✔
1752
  int32_t          ret;
1753
  SArray          *tbUids = NULL;
1,338,616✔
1754
  SArray          *tbNames = NULL;
1,338,616✔
1755
  int64_t          tss = taosGetTimestampMs();
1,338,616✔
1756

1757
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
1,338,616✔
1758
  pRsp->pCont = NULL;
1,338,616✔
1759
  pRsp->contLen = 0;
1,338,616✔
1760
  pRsp->code = TSDB_CODE_SUCCESS;
1,338,616✔
1761

1762
  // decode req
1763
  tDecoderInit(&decoder, pReq, len);
1,338,616✔
1764
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,338,616✔
1765
  if (ret < 0) {
1,338,616✔
UNCOV
1766
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
1767
    pRsp->code = terrno;
×
UNCOV
1768
    goto _exit;
×
1769
  }
1770

1771
  // process req
1772
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,338,616✔
1773
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,338,616✔
1774
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,338,616✔
1775
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,338,616✔
1776

1777
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2,731,997✔
1778
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,393,381✔
1779
    SVDropTbRsp  dropTbRsp = {0};
1,393,381✔
1780
    tb_uid_t     tbUid = 0;
1,393,381✔
1781

1782
    /* code */
1783
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,393,381✔
1784
    if (ret < 0) {
1,393,381✔
UNCOV
1785
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
UNCOV
1786
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1787
      } else {
UNCOV
1788
        dropTbRsp.code = terrno;
×
1789
      }
1790
    } else {
1791
      dropTbRsp.code = TSDB_CODE_SUCCESS;
1,393,381✔
1792
    }
1793

1794
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
2,786,762✔
UNCOV
1795
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1796
      pRsp->code = terrno;
×
UNCOV
1797
      goto _exit;
×
1798
    }
1799

1800
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,393,381✔
UNCOV
1801
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
UNCOV
1802
      if (str == NULL) {
×
1803
        pRsp->code = terrno;
×
1804
        goto _exit;
×
1805
      }
UNCOV
1806
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
UNCOV
1807
      if (taosArrayPush(tbNames, &str) == NULL) {
×
UNCOV
1808
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1809
        pRsp->code = terrno;
×
1810
        goto _exit;
×
1811
      }
1812
    }
1813
  }
1814

1815
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
1,338,616✔
1816
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1817
  }
1818

1819
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,338,616✔
UNCOV
1820
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1821

UNCOV
1822
    SName name = {0};
×
UNCOV
1823
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1824
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1825
    }
1826

UNCOV
1827
    SStringBuilder sb = {0};
×
1828
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
UNCOV
1829
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1830
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1831
      if (iReq < req.nReqs - 1) {
×
1832
        taosStringBuilderAppendChar(&sb, ',');
×
1833
      }
UNCOV
1834
      taosMemoryFreeClear(*key);
×
1835
    }
1836

1837
    size_t len = 0;
×
1838
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1839

1840
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
UNCOV
1841
      int64_t tse = taosGetTimestampMs();
×
1842
      double  duration = (double)(tse - tss);
×
UNCOV
1843
      duration = duration / 1000;
×
UNCOV
1844
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len, duration, 0);
×
1845
    }
1846

UNCOV
1847
    taosStringBuilderDestroy(&sb);
×
1848
  }
1849

1850
_exit:
1,338,616✔
1851
  taosArrayDestroy(tbUids);
1,338,616✔
1852
  tDecoderClear(&decoder);
1,338,616✔
1853
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,338,616✔
1854
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,338,616✔
1855
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,338,616✔
1856
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,338,616✔
UNCOV
1857
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1858
  }
1859
  tEncoderClear(&encoder);
1,338,616✔
1860
  taosArrayDestroy(rsp.pArray);
1,338,348✔
1861
  taosArrayDestroy(tbNames);
1,338,348✔
1862
  return 0;
1,338,348✔
1863
}
1864

1865
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
22,140✔
1866
  int32_t         code = 0, lino = 0;
22,140✔
1867
  SVCreateRsmaReq req = {0};
22,140✔
1868
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
22,140✔
1869
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
22,140✔
1870
_exit:
22,140✔
1871
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
22,140✔
1872
  pRsp->pCont = NULL;
22,140✔
1873
  pRsp->code = code;
22,140✔
1874
  pRsp->contLen = 0;
22,140✔
1875
  tFreeSVCreateRsmaReq(&req);
22,140✔
1876
  return code;
22,140✔
1877
}
1878

1879
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,428✔
1880
  int32_t       code = 0, lino = 0;
4,428✔
1881
  SVDropRsmaReq req = {0};
4,428✔
1882
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
4,428✔
1883
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
4,428✔
1884
_exit:
4,428✔
1885
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
4,428✔
1886
  pRsp->pCont = NULL;
4,428✔
1887
  pRsp->code = code;
4,428✔
1888
  pRsp->contLen = 0;
4,428✔
1889
  return 0;
4,428✔
1890
}
1891

1892
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,808✔
1893
  int32_t         code = 0, lino = 0;
11,808✔
1894
  SVAlterRsmaReq req = {0};
11,808✔
1895
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
11,808✔
1896
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
11,808✔
1897
_exit:
11,808✔
1898
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
11,808✔
1899
  pRsp->pCont = NULL;
11,808✔
1900
  pRsp->code = code;
11,808✔
1901
  pRsp->contLen = 0;
11,808✔
1902
  tFreeSVAlterRsmaReq(&req);
11,808✔
1903
  return code;
11,808✔
1904
}
1905

1906
#ifdef BUILD_NO_CALL
1907
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1908
                                              const char *tags) {
1909
  SSubmitBlkIter blkIter = {0};
1910
  STSchema      *pSchema = NULL;
1911
  tb_uid_t       suid = 0;
1912
  STSRow        *row = NULL;
1913
  int32_t        rv = -1;
1914

1915
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1916
  if (blkIter.row == NULL) return 0;
1917

1918
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1919
                                         &pSchema);  // TODO: use the real schema
1920
  if (TSDB_CODE_SUCCESS != code) {
1921
    printf("%s:%d no valid schema\n", tags, __LINE__);
1922
    return code;
1923
  }
1924

1925
  suid = msgIter->suid;
1926
  rv = TD_ROW_SVER(blkIter.row);
1927

1928
  char __tags[128] = {0};
1929
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1930
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1931
    tdSRowPrint(row, pSchema, __tags);
1932
  }
1933

1934
  taosMemoryFreeClear(pSchema);
1935

1936
  return TSDB_CODE_SUCCESS;
1937
}
1938
#endif
1939
typedef struct SSubmitReqConvertCxt {
1940
  SSubmitMsgIter msgIter;
1941
  SSubmitBlk    *pBlock;
1942
  SSubmitBlkIter blkIter;
1943
  STSRow        *pRow;
1944
  STSRowIter     rowIter;
1945
  SSubmitTbData *pTbData;
1946
  STSchema      *pTbSchema;
1947
  SArray        *pColValues;
1948
} SSubmitReqConvertCxt;
1949

UNCOV
1950
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
UNCOV
1951
  taosMemoryFreeClear(pCxt->pTbSchema);
×
UNCOV
1952
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1953
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
UNCOV
1954
  if (TSDB_CODE_SUCCESS != code) {
×
UNCOV
1955
    return code;
×
1956
  }
UNCOV
1957
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1958

1959
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1960
  if (NULL == pCxt->pTbData) {
×
UNCOV
1961
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1962
    if (NULL == pCxt->pTbData) {
×
1963
      return terrno;
×
1964
    }
1965
  }
UNCOV
1966
  pCxt->pTbData->flags = 0;
×
1967
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1968
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1969
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1970
  pCxt->pTbData->pCreateTbReq = NULL;
×
1971
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
UNCOV
1972
  if (NULL == pCxt->pTbData->aRowP) {
×
UNCOV
1973
    return terrno;
×
1974
  }
1975

1976
  taosArrayDestroy(pCxt->pColValues);
×
1977
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1978
  if (NULL == pCxt->pColValues) {
×
1979
    return terrno;
×
1980
  }
1981
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
UNCOV
1982
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
UNCOV
1983
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1984
      return terrno;
×
1985
    }
1986
  }
1987

UNCOV
1988
  return TSDB_CODE_SUCCESS;
×
1989
}
1990

1991
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1992
  taosMemoryFreeClear(pCxt->pTbSchema);
×
UNCOV
1993
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
UNCOV
1994
  taosMemoryFreeClear(pCxt->pTbData);
×
UNCOV
1995
  taosArrayDestroy(pCxt->pColValues);
×
1996
}
×
1997

UNCOV
1998
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1999
  if (tdValTypeIsNone(pCellVal->valType)) {
×
2000
    pColVal->flag = CV_FLAG_NONE;
×
2001
    return TSDB_CODE_SUCCESS;
×
2002
  }
2003

2004
  if (tdValTypeIsNull(pCellVal->valType)) {
×
UNCOV
2005
    pColVal->flag = CV_FLAG_NULL;
×
2006
    return TSDB_CODE_SUCCESS;
×
2007
  }
2008

2009
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
UNCOV
2010
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
UNCOV
2011
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
2012
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2013

2014
    } else {
UNCOV
2015
      pColVal->value.nData = varDataLen(pCellVal->val);
×
UNCOV
2016
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
2017
    }
2018
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
2019
    float f = GET_FLOAT_VAL(pCellVal->val);
×
2020
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
UNCOV
2021
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
UNCOV
2022
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
2023
  } else {
2024
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
2025
  }
2026

2027
  pColVal->flag = CV_FLAG_VALUE;
×
2028
  return TSDB_CODE_SUCCESS;
×
2029
}
2030

UNCOV
2031
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
2032
  int32_t code = TSDB_CODE_SUCCESS;
×
UNCOV
2033
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
UNCOV
2034
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
2035
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
2036
    SCellVal  cellVal = {0};
×
UNCOV
2037
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
UNCOV
2038
      break;
×
2039
    }
2040
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2041
  }
2042
  return code;
×
2043
}
2044

2045
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2046
  if (pCxt->msgIter.schemaLen <= 0) {
×
UNCOV
2047
    return TSDB_CODE_SUCCESS;
×
2048
  }
2049

2050
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
UNCOV
2051
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
UNCOV
2052
    return terrno;
×
2053
  }
2054

2055
  SDecoder decoder = {0};
×
UNCOV
2056
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
UNCOV
2057
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2058
  tDecoderClear(&decoder);
×
2059

2060
  return code;
×
2061
}
2062

2063
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2064
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2065
  if (NULL == pReq2->aSubmitTbData) {
×
2066
    return terrno;
×
2067
  }
2068

UNCOV
2069
  SSubmitReqConvertCxt cxt = {0};
×
2070

2071
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2072
  while (TSDB_CODE_SUCCESS == code) {
×
2073
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
2074
    if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
2075
      if (NULL == cxt.pBlock) {
×
UNCOV
2076
        break;
×
2077
      }
UNCOV
2078
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2079
    }
2080
    if (TSDB_CODE_SUCCESS == code) {
×
2081
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2082
    }
2083
    if (TSDB_CODE_SUCCESS == code) {
×
2084
      code = vnodeDecodeCreateTbReq(&cxt);
×
2085
    }
2086
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
UNCOV
2087
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2088
      if (TSDB_CODE_SUCCESS == code) {
×
2089
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2090

2091
        SRowBuildScanInfo sinfo = {0};
×
2092
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2093
      }
2094
    }
2095
    if (TSDB_CODE_SUCCESS == code) {
×
2096
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2097
    }
UNCOV
2098
    if (TSDB_CODE_SUCCESS == code) {
×
2099
      taosMemoryFreeClear(cxt.pTbData);
×
2100
    }
2101
  }
2102

2103
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2104
  return code;
×
2105
}
2106

2107
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
UNCOV
2108
  int32_t  code = TSDB_CODE_SUCCESS;
×
UNCOV
2109
  char    *pMsg = NULL;
×
UNCOV
2110
  uint32_t msglen = 0;
×
2111
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2112
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
2113
    pMsg = taosMemoryMalloc(msglen);
×
UNCOV
2114
    if (NULL == pMsg) {
×
2115
      code = terrno;
×
2116
    }
2117
  }
2118
  if (TSDB_CODE_SUCCESS == code) {
×
2119
    SEncoder encoder;
×
2120
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2121
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2122
    tEncoderClear(&encoder);
×
2123
  }
UNCOV
2124
  if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
2125
    *ppMsg = pMsg;
×
2126
  }
2127
  return code;
×
2128
}
2129

2130
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
64,859✔
2131
  int32_t code = 0;
64,859✔
2132
  int32_t lino = 0;
64,859✔
2133

2134
  SMetaEntry *pEntry = NULL;
64,859✔
2135
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
64,859✔
2136
  if (code) {
64,859✔
UNCOV
2137
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
UNCOV
2138
    TSDB_CHECK_CODE(code, lino, _exit);
×
2139
  }
2140
  if (pEntry->type != TSDB_SUPER_TABLE) {
64,859✔
UNCOV
2141
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2142
           __LINE__);
UNCOV
2143
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
UNCOV
2144
    TSDB_CHECK_CODE(code, lino, _exit);
×
2145
  }
2146

2147
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
64,859✔
2148
  if (NULL == *ppRsp) {
64,859✔
2149
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2150
  }
2151
  (*ppRsp)->suid = pSubmitTbData->suid;
64,859✔
2152
  (*ppRsp)->tuid = pSubmitTbData->uid;
64,859✔
2153
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
64,859✔
2154
  (*ppRsp)->vgId = pVnode->config.vgId;
64,859✔
2155
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
64,859✔
2156
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
64,859✔
2157
  (*ppRsp)->pSchemas =
64,859✔
2158
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
64,859✔
2159
  if (NULL == (*ppRsp)->pSchemas) {
64,859✔
UNCOV
2160
    taosMemoryFree(*ppRsp);
×
UNCOV
2161
    *ppRsp = NULL;
×
UNCOV
2162
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2163
  }
2164
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
64,859✔
2165
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
64,859✔
2166
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
64,859✔
2167
  if (pEntry->pExtSchemas != NULL) {
64,859✔
2168
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2169
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2170
      taosMemoryFree((*ppRsp)->pSchemas);
×
UNCOV
2171
      taosMemoryFree(*ppRsp);
×
UNCOV
2172
      *ppRsp = NULL;
×
UNCOV
2173
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2174
    }
UNCOV
2175
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2176
  }
2177

2178
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
64,859✔
2179
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
436✔
2180
  }
2181
_exit:
64,859✔
2182
  metaFetchEntryFree(&pEntry);
64,859✔
2183
  if (code != TSDB_CODE_SUCCESS) {
64,859✔
2184
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
436✔
2185
  }
2186
  return code;
64,859✔
2187
}
2188

2189
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
36,944✔
2190
  int32_t code = 0;
36,944✔
2191
  int32_t lino = 0;
36,944✔
2192

2193
  SMetaEntry *pEntry = NULL;
36,944✔
2194
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
36,944✔
2195
  if (code) {
36,944✔
UNCOV
2196
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
UNCOV
2197
    TSDB_CHECK_CODE(code, lino, _exit);
×
2198
  }
2199
  if (pEntry->type != TSDB_NORMAL_TABLE) {
36,944✔
UNCOV
2200
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
UNCOV
2201
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
UNCOV
2202
    TSDB_CHECK_CODE(code, lino, _exit);
×
2203
  }
2204

2205
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
36,944✔
2206
  if (NULL == *ppRsp) {
36,944✔
UNCOV
2207
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2208
  }
2209

2210
  (*ppRsp)->tuid = pEntry->uid;
36,944✔
2211
  (*ppRsp)->vgId = pVnode->config.vgId;
36,944✔
2212
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
36,944✔
2213
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
36,944✔
2214
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
36,944✔
2215
  if (NULL == (*ppRsp)->pSchemas) {
36,944✔
UNCOV
2216
    taosMemoryFree(*ppRsp);
×
UNCOV
2217
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2218
  }
2219
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
36,944✔
2220
  if (pEntry->pExtSchemas != NULL) {
36,944✔
UNCOV
2221
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
UNCOV
2222
    if (NULL == (*ppRsp)->pSchemaExt) {
×
UNCOV
2223
      taosMemoryFree((*ppRsp)->pSchemas);
×
2224
      taosMemoryFree(*ppRsp);
×
2225
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2226
    }
UNCOV
2227
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2228
  }
2229

2230
_exit:
36,944✔
2231
  metaFetchEntryFree(&pEntry);
36,944✔
2232
  if (code != TSDB_CODE_SUCCESS) {
36,944✔
2233
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2234
  }
2235
  return code;
36,944✔
2236
}
2237

2238
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
304,483✔
2239
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
304,483✔
2240
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
36,944✔
2241
    if (code) {
36,944✔
UNCOV
2242
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
UNCOV
2243
      return code;
×
2244
    }
2245
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
36,944✔
2246
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
267,493✔
2247
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
64,859✔
2248
  }
2249
  return TSDB_CODE_SUCCESS;
202,680✔
2250
}
2251

2252
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
679,649,882✔
2253
                                          int64_t      version,   // version
2254
                                          SSubmitReq2 *pRequest,  // request
2255
                                          SSubmitRsp2 *pResponse  // response
2256
) {
2257
  int32_t code = TSDB_CODE_SUCCESS;
679,649,882✔
2258
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
679,649,882✔
2259
  SArray *newTbUids = NULL;
679,650,086✔
2260

2261
  for (int32_t i = 0; i < numTbData; ++i) {
1,386,311,990✔
2262
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
706,700,918✔
2263

2264
    if (pTbData->pCreateTbReq == NULL) {
706,702,271✔
2265
      continue;
691,862,330✔
2266
    }
2267

2268
    pTbData->uid = pTbData->pCreateTbReq->uid;
14,839,789✔
2269

2270
    // Alloc necessary resources
2271
    if (pResponse->aCreateTbRsp == NULL) {
14,840,295✔
2272
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
12,072,604✔
2273
      if (pResponse->aCreateTbRsp == NULL) {
12,072,604✔
UNCOV
2274
        code = terrno;
×
UNCOV
2275
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2276
               tstrerror(code), version);
UNCOV
2277
        taosArrayDestroy(newTbUids);
×
UNCOV
2278
        return code;
×
2279
      }
2280
    }
2281

2282
    // Do create table
2283
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
14,838,776✔
2284

2285
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
14,839,328✔
2286
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
14,840,249✔
2287
    if (code == TSDB_CODE_SUCCESS) {
14,839,940✔
2288
      // Allocate necessary resources
2289
      if (newTbUids == NULL) {
14,475,263✔
2290
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
11,767,894✔
2291
        if (newTbUids == NULL) {
11,767,894✔
UNCOV
2292
          code = terrno;
×
UNCOV
2293
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2294
                 tstrerror(code), version);
UNCOV
2295
          return code;
×
2296
        }
2297
      }
2298

2299
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
28,950,526✔
2300
        code = terrno;
×
2301
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2302
               tstrerror(code), version);
2303
        taosArrayDestroy(newTbUids);
×
UNCOV
2304
        return code;
×
2305
      }
2306

2307
      if (pCreateTbRsp->pMeta) {
14,475,263✔
2308
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
14,475,263✔
2309
      }
2310
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
364,677✔
2311
      code = terrno = 0;
363,663✔
2312
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
363,663✔
2313

2314
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2315
      if (i == 0) {
363,617✔
2316
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2317
        // only the first one needs to be processed.
2318
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
304,483✔
2319
        if (code) {
304,483✔
2320
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
37,380✔
2321
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2322
          taosArrayDestroy(newTbUids);
37,380✔
2323
          return code;
37,380✔
2324
        }
2325
      }
2326
    } else {
2327
      code = terrno;
1,014✔
2328
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,014✔
2329
             tstrerror(code), version);
2330
      taosArrayDestroy(newTbUids);
1,014✔
2331
      return code;
1,014✔
2332
    }
2333
  }
2334

2335
  // Update the affected table uid list
2336
  if (taosArrayGetSize(newTbUids) > 0) {
679,611,072✔
2337
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
11,767,894✔
2338
           (int32_t)taosArrayGetSize(newTbUids));
2339
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
11,767,894✔
UNCOV
2340
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2341
    }
2342
  }
2343

2344
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
679,611,381✔
2345

2346
  taosArrayDestroy(newTbUids);
679,611,381✔
2347
  return code;
679,610,772✔
2348
}
2349

2350
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
436✔
2351
                                     SSubmitTbData *pTbData, int32_t numTbData) {
2352
  int32_t code = 0;
436✔
2353
  int32_t lino = 0;
436✔
2354
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
436✔
UNCOV
2355
    return;
×
2356
  }
2357
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
436✔
2358
                                  // so table info has exitst and we do not need to add again.
2359
    return;
436✔
2360
  }
UNCOV
2361
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
UNCOV
2362
  if (pResponse->aCreateTbRsp == NULL) {
×
2363
    code = terrno;
×
UNCOV
2364
    TSDB_CHECK_CODE(code, lino, _exit);
×
2365
  }
UNCOV
2366
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
UNCOV
2367
  if (pCreateTbRsp == NULL) {
×
UNCOV
2368
    code = terrno;
×
2369
    TSDB_CHECK_CODE(code, lino, _exit);
×
2370
  }
2371
  if (pTbData->suid == 0) {
×
2372
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
UNCOV
2373
    if (code) {
×
2374
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2375
    }
2376
  } else {
2377
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2378
  }
2379

2380
  TSDB_CHECK_CODE(code, lino, _exit);
×
2381
_exit:
×
2382
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2383
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2384
  }
2385
  return;
×
2386
}
2387

2388
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
679,610,504✔
2389
  int32_t code = TSDB_CODE_SUCCESS;
679,610,504✔
2390
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
679,610,504✔
2391
  int8_t  hasBlob = 0;
679,610,817✔
2392

2393
  // Scan submit data
2394
  for (int32_t i = 0; i < numTbData; ++i) {
1,386,271,897✔
2395
    SMetaInfo      info = {0};
706,662,746✔
2396
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
706,662,701✔
2397

2398
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
706,664,135✔
2399
      hasBlob = 1;
21,560✔
2400
    }
2401
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
706,664,071✔
2402
      continue;  // skip column data format
6,345,773✔
2403
    }
2404
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
700,317,018✔
2405
      continue;  // skip only crate table request
2,497,506✔
2406
    }
2407

2408
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
697,819,614✔
2409
    if (code) {
697,818,095✔
2410
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
1,868✔
2411
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
1,868✔
2412
            __LINE__, tstrerror(code), version, pTbData->uid);
2413
      return code;
1,868✔
2414
    }
2415

2416
    if (info.suid != pTbData->suid) {
697,816,227✔
UNCOV
2417
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2418
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2419
             " info.suid:%" PRId64,
2420
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2421
             info.suid);
UNCOV
2422
      return code;
×
2423
    }
2424

2425
    if (info.suid) {
697,812,495✔
2426
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
514,416,124✔
2427
      if (code) {
514,420,447✔
UNCOV
2428
        code = TSDB_CODE_INTERNAL_ERROR;
×
UNCOV
2429
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2430
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
UNCOV
2431
        return code;
×
2432
      }
2433
    }
2434

2435
    if (pTbData->sver != info.skmVer) {
697,816,818✔
2436
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
436✔
2437
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
436✔
2438
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
436✔
2439
             " sver:%d"
2440
             " info.skmVer:%d",
2441
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2442
             info.skmVer);
2443
      return code;
436✔
2444
    }
2445
  }
2446

2447
  // Do write data
2448
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
679,609,151✔
2449

2450
  for (int32_t i = 0; i < numTbData; ++i) {
1,386,242,829✔
2451
    int32_t        affectedRows = 0;
706,657,149✔
2452
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
706,657,543✔
2453

2454
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
706,658,063✔
2455
      continue;
2,497,506✔
2456
    }
2457

2458
    if (hasBlob) {
704,161,121✔
2459
      code = vnodeSubmitBlobData(pVnode, pTbData);
21,560✔
2460
      if (code) {
21,560✔
UNCOV
2461
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2462
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2463
        return code;
×
2464
      }
2465
    }
2466

2467
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
704,161,121✔
2468
    if (code) {
704,146,605✔
2469
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2470
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2471
      return code;
×
2472
    }
2473

2474
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
704,146,605✔
2475
    if (code) {
704,114,885✔
UNCOV
2476
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2477
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2478
      return code;
×
2479
    }
2480
    pResponse->affectedRows += affectedRows;
704,114,885✔
2481
  }
2482

2483
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
679,585,680✔
2484
         pResponse->affectedRows);
2485
  return code;
679,607,734✔
2486
}
2487

2488
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
6,345,147✔
2489
  int32_t code = 0;
6,345,147✔
2490

2491
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
6,345,147✔
2492
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
6,344,269✔
2493

2494
  if (numCols <= 0) {
6,343,610✔
UNCOV
2495
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2496
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2497
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
UNCOV
2498
    return code;
×
2499
  }
2500

2501
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
6,343,610✔
2502
      aColData[0].nVal <= 0) {
6,343,651✔
2503
    code = TSDB_CODE_INVALID_MSG;
×
2504
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2505
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2506
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2507
           aColData[0].type, aColData[0].nVal);
UNCOV
2508
    return code;
×
2509
  }
2510

2511
  for (int32_t i = 1; i < numCols; ++i) {
19,546,438✔
2512
    if (aColData[i].nVal != aColData[0].nVal) {
13,202,527✔
UNCOV
2513
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2514
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2515
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2516
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2517
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
UNCOV
2518
      return code;
×
2519
    }
2520
  }
2521

2522
  SRowKey *pLastKey = NULL;
6,343,911✔
2523
  SRowKey  lastKey = {0};
6,343,911✔
2524
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2525
    SRowKey key = {0};
2,147,483,647✔
2526

2527
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2528

2529
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
UNCOV
2530
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2531
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2532
             " is out of range [%" PRId64 ", %" PRId64 "]",
2533
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2534
             minKey, maxKey);
UNCOV
2535
      return code;
×
2536
    }
2537

2538
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2539
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2540
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2541
             " is not in order, lastKey:%" PRId64,
2542
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2543
             pLastKey->ts);
UNCOV
2544
      return code;
×
2545
    } else if (pLastKey == NULL) {
2,147,483,647✔
2546
      pLastKey = &lastKey;
6,342,716✔
2547
    }
2548

2549
    *pLastKey = key;
2,147,483,647✔
2550
  }
2551

2552
  return code;
6,345,505✔
2553
}
2554

2555
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
700,322,660✔
2556
  int32_t code = 0;
700,322,660✔
2557

2558
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
700,322,660✔
2559
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
700,328,110✔
2560

2561
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
700,319,263✔
UNCOV
2562
    code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2563
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2564
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
UNCOV
2565
    return code;
×
2566
  }
2567

2568
  SRowKey *pLastKey = NULL;
700,319,263✔
2569
  SRowKey  lastKey = {0};
700,319,263✔
2570
  for (int32_t i = 0; i < numRows; ++i) {
2,147,483,647✔
2571
    SRow *pRow = aRow[i];
2,147,483,647✔
2572
    if (pRow->sver != pTbData->sver) {
2,147,483,647✔
2573
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2574
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2575
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2576
             pTbData->sver);
UNCOV
2577
      return code;
×
2578
    }
2579

2580
    SRowKey key = {0};
2,147,483,647✔
2581
    tRowGetKey(pRow, &key);
2,147,483,647✔
2582
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2583
      code = TSDB_CODE_INVALID_MSG;
244,090✔
2584
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
244,090✔
2585
             " is out of range [%" PRId64 ", %" PRId64 "]",
2586
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2587
             minKey, maxKey);
UNCOV
2588
      return code;
×
2589
    }
2590

2591
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2592
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2593
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2594
             " is not in order, lastKey:%" PRId64,
2595
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2596
             pLastKey->ts);
UNCOV
2597
      return code;
×
2598
    } else if (pLastKey == NULL) {
2,147,483,647✔
2599
      pLastKey = &lastKey;
697,821,562✔
2600
    }
2601

2602
    *pLastKey = key;
2,147,483,647✔
2603
  }
2604

2605
  return code;
695,169,811✔
2606
}
2607

2608
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
679,638,172✔
2609
  int32_t code = TSDB_CODE_SUCCESS;
679,638,172✔
2610
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
679,638,172✔
2611

2612
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
679,631,850✔
2613
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
679,637,392✔
2614
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
679,635,645✔
2615
  for (int32_t i = 0; i < numTbData; i++) {
1,386,304,606✔
2616
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
706,688,553✔
2617

2618
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
706,690,690✔
UNCOV
2619
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2620
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2621
             tstrerror(code), version);
UNCOV
2622
      return code;
×
2623
    }
2624

2625
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
706,692,499✔
2626
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
6,344,261✔
2627
      if (code) {
6,345,505✔
2628
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2629
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2630
        return code;
×
2631
      }
2632
    } else {
2633
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
700,340,789✔
2634
      if (code) {
700,310,317✔
UNCOV
2635
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2636
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
UNCOV
2637
        return code;
×
2638
      }
2639
    }
2640
  }
2641

2642
  return code;
679,616,053✔
2643
}
2644

2645
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
679,646,534✔
2646
                                     SRpcMsg *pOriginalMsg) {
2647
  int32_t code = 0;
679,646,534✔
2648
  int32_t lino = 0;
679,646,534✔
2649
  terrno = 0;
679,646,534✔
2650

2651
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
679,649,775✔
2652
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
679,648,343✔
2653
  int32_t      ret;
2654
  SEncoder     ec = {0};
679,646,240✔
2655

2656
  pRsp->code = TSDB_CODE_SUCCESS;
679,647,171✔
2657

2658
  void           *pAllocMsg = NULL;
679,645,435✔
2659
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
679,645,435✔
2660
  SDecoder        dc = {0};
679,645,435✔
2661
  if (0 == taosHton64(pMsg->version)) {
679,646,607✔
UNCOV
2662
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
UNCOV
2663
    if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
2664
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2665
    }
UNCOV
2666
    if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
2667
      pAllocMsg = pReq;
×
2668
    }
UNCOV
2669
    TSDB_CHECK_CODE(code, lino, _exit);
×
2670
  } else {
2671
    // decode
2672
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
679,645,807✔
2673
    len -= sizeof(SSubmitReq2Msg);
679,647,475✔
2674

2675
    tDecoderInit(&dc, pReq, len);
679,647,475✔
2676
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
679,646,575✔
2677
      code = TSDB_CODE_INVALID_MSG;
×
UNCOV
2678
      TSDB_CHECK_CODE(code, lino, _exit);
×
2679
    }
2680
  }
2681

2682
  // Scan the request
2683
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
679,635,121✔
2684
  if (code) {
679,610,486✔
2685
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2686
           tstrerror(code), ver);
UNCOV
2687
    TSDB_CHECK_CODE(code, lino, _exit);
×
2688
  }
2689

2690
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
679,610,486✔
2691
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2692

2693
  // Handle auto create table
2694
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
679,639,783✔
2695
  if (code) {
679,648,075✔
2696
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
38,394✔
2697
           tstrerror(code), ver);
2698
    TSDB_CHECK_CODE(code, lino, _exit);
38,394✔
2699
  }
2700

2701
  // Handle data write
2702
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
679,609,681✔
2703
  if (code) {
679,611,470✔
2704
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
2,304✔
2705
           tstrerror(code), ver);
2706
    TSDB_CHECK_CODE(code, lino, _exit);
2,304✔
2707
  }
2708

2709
_exit:
679,649,702✔
2710
  // message
2711
  pRsp->code = code;
679,650,643✔
2712
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
679,649,712✔
2713
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
679,649,619✔
2714
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
679,647,853✔
2715
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
679,649,174✔
UNCOV
2716
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2717
  }
2718
  tEncoderClear(&ec);
679,648,213✔
2719

2720
  // update statistics
2721
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
679,646,141✔
2722
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
679,650,035✔
2723
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
679,650,259✔
2724

2725
  // update metrics
2726
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
679,649,465✔
2727
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
679,642,418✔
2728
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
679,641,013✔
2729

2730
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
679,640,857✔
UNCOV
2731
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
UNCOV
2732
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
UNCOV
2733
                                   pVnode->monitor.strClusterId,
×
UNCOV
2734
                                   pVnode->monitor.strDnodeId,
×
2735
                                   tsLocalEp,
UNCOV
2736
                                   pVnode->monitor.strVgId,
×
UNCOV
2737
                                   RPC_MSG_USER(pOriginalMsg),
×
2738
                                   "Success"};
2739
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2740
  }
2741

2742
  if (code == 0) {
679,640,516✔
2743
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
679,599,818✔
2744
  }
2745

2746
  // clear
2747
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
679,645,538✔
2748
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
679,627,235✔
2749

2750
  if (code) {
679,621,632✔
2751
    terrno = code;
40,698✔
2752
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
40,698✔
2753
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
37,380✔
2754
            tstrerror(code), lino, ver);
2755

2756
    } else {
2757
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
3,318✔
2758
             tstrerror(code), lino, ver);
2759
    }
2760
  }
2761

2762
  taosMemoryFree(pAllocMsg);
679,627,747✔
2763
  tDecoderClear(&dc);
679,627,019✔
2764

2765
  return code;
679,647,893✔
2766
}
2767

2768
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
24,236✔
2769
  int32_t code = TSDB_CODE_SUCCESS;
24,236✔
2770

2771
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
24,236✔
2772
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2773

2774
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2775
  code = metaTrimTables(pVnode->pMeta, ver);
24,236✔
2776

2777
  return code;
24,236✔
2778
}
2779

2780
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,302,416✔
2781
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
4,302,416✔
2782
  int32_t code = TSDB_CODE_SUCCESS;
4,302,416✔
2783
  if (!pVnode->config.hashChange) {
4,302,416✔
2784
    goto _exit;
4,278,180✔
2785
  }
2786

2787
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
24,236✔
2788
  if (code < 0) {
24,236✔
UNCOV
2789
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
UNCOV
2790
    goto _exit;
×
2791
  }
2792
  pVnode->config.hashChange = false;
24,236✔
2793

2794
_exit:
4,302,416✔
2795
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
4,302,416✔
2796
  pRsp->code = code;
4,302,416✔
2797
  pRsp->pCont = NULL;
4,302,416✔
2798
  pRsp->contLen = 0;
4,302,416✔
2799

2800
  return code;
4,302,416✔
2801
}
2802

2803
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2804
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2805

2806
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
313,090✔
2807
  bool walChanged = false;
313,090✔
2808
  bool tsdbChanged = false;
313,090✔
2809

2810
  SAlterVnodeConfigReq req = {0};
313,090✔
2811
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
313,090✔
UNCOV
2812
    terrno = TSDB_CODE_INVALID_MSG;
×
UNCOV
2813
    return TSDB_CODE_INVALID_MSG;
×
2814
  }
2815

2816
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
313,090✔
2817
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2818
        "ssCompact:%d allowDrop:%d fsync:%d level:%d "
2819
        "walRetentionPeriod:%d walRetentionSize:%d",
2820
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2821
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2822
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.allowDrop, req.walFsyncPeriod, req.walLevel,
2823
        req.walRetentionPeriod, req.walRetentionSize);
2824

2825
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
313,090✔
2826
    pVnode->config.cacheLastSize = req.cacheLastSize;
5,904✔
2827
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
5,904✔
2828
  }
2829

2830
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
313,090✔
2831
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
8,806✔
2832
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2833
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
8,806✔
2834
  }
2835

2836
  if (pVnode->config.szCache != req.pages) {
313,090✔
2837
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,428✔
UNCOV
2838
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2839
             pVnode->config.szCache, req.pages, tstrerror(terrno));
UNCOV
2840
      return terrno;
×
2841
    } else {
2842
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,428✔
2843
      pVnode->config.szCache = req.pages;
4,428✔
2844
    }
2845
  }
2846

2847
  if (pVnode->config.cacheLast != req.cacheLast) {
313,090✔
2848
    pVnode->config.cacheLast = req.cacheLast;
86,124✔
2849
  }
2850

2851
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
313,090✔
2852
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
51,416✔
2853
    walChanged = true;
51,416✔
2854
  }
2855

2856
  if (pVnode->config.walCfg.level != req.walLevel) {
313,090✔
2857
    if (pVnode->config.walCfg.level == 0) {
37,856✔
2858
      pVnode->config.walCfg.clearFiles = 1;
2,146✔
2859
    }
2860
    pVnode->config.walCfg.level = req.walLevel;
37,856✔
2861
    walChanged = true;
37,856✔
2862
  }
2863

2864
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
313,090✔
2865
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
46,688✔
2866
    walChanged = true;
46,688✔
2867
  }
2868

2869
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
313,090✔
2870
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,426✔
2871
    walChanged = true;
1,426✔
2872
  }
2873

2874
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
313,090✔
2875
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
41,034✔
2876
    tsdbChanged = true;
41,034✔
2877
  }
2878

2879
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
313,090✔
2880
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
39,558✔
2881
    tsdbChanged = true;
39,558✔
2882
  }
2883

2884
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
313,090✔
2885
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
33,758✔
2886
    tsdbChanged = true;
33,758✔
2887
  }
2888

2889
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
313,090✔
2890
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,178✔
2891
    tsdbChanged = true;
7,178✔
2892
  }
2893

2894
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
313,090✔
2895
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,476✔
2896
      pVnode->config.sttTrigger = req.sttTrigger;
1,426✔
2897
    } else {
2898
      vnodeAWait(&pVnode->commitTask);
1,050✔
2899

2900
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,050✔
2901
      if (ret != 0) {
1,050✔
UNCOV
2902
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2903
      }
2904

2905
      pVnode->config.sttTrigger = req.sttTrigger;
1,050✔
2906
      tsdbEnableBgTask(pVnode->pTsdb);
1,050✔
2907
    }
2908
  }
2909

2910
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
313,090✔
2911
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,426✔
2912
  }
2913

2914
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
313,090✔
2915
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,426✔
2916
  }
2917
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
313,090✔
2918
    pVnode->config.ssCompact = req.ssCompact;
1,426✔
2919
  }
2920
  if (req.allowDrop != pVnode->config.allowDrop) {
313,090✔
NEW
2921
    pVnode->config.allowDrop = req.allowDrop;
×
2922
  }
2923

2924
  if (walChanged) {
313,090✔
2925
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
134,514✔
UNCOV
2926
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2927
    }
2928
  }
2929

2930
  if (tsdbChanged) {
313,090✔
2931
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
55,548✔
2932
  }
2933

2934
  return 0;
313,090✔
2935
}
2936

2937
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
2938
  SBatchDeleteReq deleteReq;
×
UNCOV
2939
  SDecoder        decoder;
×
UNCOV
2940
  tDecoderInit(&decoder, pReq, len);
×
UNCOV
2941
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
UNCOV
2942
    tDecoderClear(&decoder);
×
UNCOV
2943
    return terrno = TSDB_CODE_INVALID_MSG;
×
2944
  }
2945

UNCOV
2946
  SMetaReader mr = {0};
×
UNCOV
2947
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2948
  STsdb *pTsdb = pVnode->pTsdb;
×
2949

2950
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2951
  for (int32_t i = 0; i < sz; i++) {
×
2952
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2953
    char             *name = pOneReq->tbname;
×
2954
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
UNCOV
2955
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
UNCOV
2956
      continue;
×
2957
    }
2958

2959
    int64_t uid = mr.me.uid;
×
2960

2961
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2962
    if (code < 0) {
×
2963
      terrno = code;
×
2964
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2965
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2966
    }
2967

UNCOV
2968
    if (deleteReq.level == 0) {
×
UNCOV
2969
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2970
      if (code < 0) {
×
UNCOV
2971
        terrno = code;
×
2972
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2973
               ", end ts:%" PRId64,
2974
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2975
      }
2976
    }
UNCOV
2977
    tDecoderClear(&mr.coder);
×
2978
  }
2979
  metaReaderClear(&mr);
×
2980
  taosArrayDestroy(deleteReq.deleteReqs);
×
2981
  return 0;
×
2982
}
2983

2984
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,047,311✔
2985
                                     SRpcMsg *pOriginalMsg) {
2986
  int32_t     code = 0;
2,047,311✔
2987
  SDecoder   *pCoder = &(SDecoder){0};
2,047,311✔
2988
  SDeleteRes *pRes = &(SDeleteRes){0};
2,047,311✔
2989

2990
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,047,311✔
2991
  pRsp->pCont = NULL;
2,047,311✔
2992
  pRsp->contLen = 0;
2,047,311✔
2993
  pRsp->code = TSDB_CODE_SUCCESS;
2,047,311✔
2994

2995
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,047,311✔
2996
  if (pRes->uidList == NULL) {
2,047,311✔
UNCOV
2997
    code = terrno;
×
UNCOV
2998
    goto _err;
×
2999
  }
3000

3001
  tDecoderInit(pCoder, pReq, len);
2,047,311✔
3002
  code = tDecodeDeleteRes(pCoder, pRes);
2,047,311✔
3003
  if (code) goto _err;
2,047,311✔
3004

3005
  if (pRes->affectedRows > 0) {
2,047,311✔
3006
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,647,511✔
3007
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,080,428✔
3008
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
2,080,428✔
3009
      if (code) goto _err;
2,080,428✔
3010
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,080,428✔
3011
      if (code) goto _err;
2,080,428✔
3012
    }
3013
  }
3014

3015
  tDecoderClear(pCoder);
2,047,311✔
3016
  taosArrayDestroy(pRes->uidList);
2,047,311✔
3017

3018
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,047,311✔
3019
  int32_t     ret = 0;
2,047,311✔
3020
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,047,311✔
3021
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,047,311✔
3022
  SEncoder ec = {0};
2,047,311✔
3023
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,047,311✔
3024
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,047,311✔
3025
  if (code) goto _err;
2,047,311✔
3026
  tEncoderClear(&ec);
2,047,311✔
3027
  return code;
2,047,311✔
3028

UNCOV
3029
_err:
×
3030
  /*
3031
  if(code == TSDB_CODE_SUCCESS){
3032
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3033
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3034
                                        RPC_MSG_USER(pOriginalMsg), "Success"};
3035
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3036
  }
3037
  else{
3038
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3039
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3040
                                        RPC_MSG_USER(pOriginalMsg), "Failed"};
3041
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3042
  }
3043
  */
3044

UNCOV
3045
  return code;
×
3046
}
3047
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,434✔
3048
  SVCreateStbReq req = {0};
4,434✔
3049
  SDecoder       dc = {0};
4,434✔
3050
  int32_t        code = 0;
4,434✔
3051

3052
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
4,434✔
3053
  pRsp->code = TSDB_CODE_SUCCESS;
4,434✔
3054
  pRsp->pCont = NULL;
4,434✔
3055
  pRsp->contLen = 0;
4,434✔
3056

3057
  tDecoderInit(&dc, pReq, len);
4,434✔
3058
  // decode req
3059
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
4,434✔
UNCOV
3060
    tDecoderClear(&dc);
×
UNCOV
3061
    return terrno = TSDB_CODE_INVALID_MSG;
×
3062
  }
3063

3064
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
4,434✔
3065
  if (code) {
4,434✔
UNCOV
3066
    pRsp->code = code;
×
UNCOV
3067
    goto _err;
×
3068
  }
3069
  tDecoderClear(&dc);
4,434✔
3070
  return 0;
4,434✔
3071

3072
_err:
×
UNCOV
3073
  tDecoderClear(&dc);
×
UNCOV
3074
  return code;
×
3075
}
3076
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,940✔
3077
  SDropIndexReq req = {0};
2,940✔
3078
  int32_t       code = 0;
2,940✔
3079
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,940✔
3080
  pRsp->code = TSDB_CODE_SUCCESS;
2,940✔
3081
  pRsp->pCont = NULL;
2,940✔
3082
  pRsp->contLen = 0;
2,940✔
3083

3084
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,940✔
3085
    pRsp->code = code;
×
UNCOV
3086
    return code;
×
3087
  }
3088

3089
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,940✔
3090
  if (code) {
2,940✔
UNCOV
3091
    pRsp->code = code;
×
UNCOV
3092
    return code;
×
3093
  }
3094
  return TSDB_CODE_SUCCESS;
2,940✔
3095
}
3096

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

3099
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
66,329✔
3100
  if (!pVnode->restored) {
66,329✔
3101
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
2,229✔
3102
    return 0;
2,229✔
3103
  }
3104
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
64,100✔
3105
}
3106

UNCOV
3107
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
UNCOV
3108
  if (syncCheckMember(pVnode->sync) != 0) {
×
UNCOV
3109
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3110
  }
3111

UNCOV
3112
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
UNCOV
3113
  pRsp->code = TSDB_CODE_SUCCESS;
×
UNCOV
3114
  pRsp->pCont = NULL;
×
UNCOV
3115
  pRsp->contLen = 0;
×
3116

UNCOV
3117
  return 0;
×
3118
}
3119

3120
static int32_t vnodeCheckState(SVnode *pVnode) {
52,204✔
3121
  SSyncState syncState = syncGetState(pVnode->sync);
52,204✔
3122
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
52,204✔
3123
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
14,950✔
3124
  }
3125
  return 0;
37,254✔
3126
}
3127

3128
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
37,254✔
3129
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
37,254✔
3130
  if (vnodeGetArbToken(pVnode, token) != 0) {
37,254✔
UNCOV
3131
    return terrno = TSDB_CODE_NOT_FOUND;
×
3132
  }
3133

3134
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
37,254✔
3135
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
35,256✔
3136
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
34,644✔
3137
  }
3138

3139
  terrno = TSDB_CODE_SUCCESS;
2,610✔
3140
  return 0;
2,610✔
3141
}
3142

3143
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
1,305✔
3144
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
1,305✔
3145
  if (code != 0) {
1,305✔
UNCOV
3146
    return code;
×
3147
  }
3148

3149
  return syncCheckSynced(pVnode->sync);
1,305✔
3150
}
3151

3152
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,575✔
3153
  int32_t code = 0;
2,575✔
3154

3155
  if ((code = vnodeCheckState(pVnode)) != 0) {
2,575✔
3156
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
1,270✔
3157
    return 0;
1,270✔
3158
  }
3159

3160
  SVArbCheckSyncReq syncReq = {0};
1,305✔
3161

3162
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
1,305✔
3163
  if (code) {
1,305✔
UNCOV
3164
    return code;
×
3165
  }
3166

3167
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
1,305✔
3168
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3169
        "arbTerm:%" PRId64,
3170
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3171
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3172

3173
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
1,305✔
3174
  pRsp->code = TSDB_CODE_SUCCESS;
1,305✔
3175
  pRsp->pCont = NULL;
1,305✔
3176
  pRsp->contLen = 0;
1,305✔
3177

3178
  SVArbCheckSyncRsp syncRsp = {0};
1,305✔
3179
  syncRsp.arbToken = syncReq.arbToken;
1,305✔
3180
  syncRsp.member0Token = syncReq.member0Token;
1,305✔
3181
  syncRsp.member1Token = syncReq.member1Token;
1,305✔
3182
  syncRsp.vgId = TD_VID(pVnode);
1,305✔
3183

3184
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
1,305✔
UNCOV
3185
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3186
  }
3187

3188
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
1,305✔
UNCOV
3189
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
UNCOV
3190
    goto _OVER;
×
3191
  }
3192

3193
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
1,305✔
3194
  if (contLen <= 0) {
1,305✔
UNCOV
3195
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3196
    code = -1;
×
UNCOV
3197
    goto _OVER;
×
3198
  }
3199
  void *pHead = rpcMallocCont(contLen);
1,305✔
3200
  if (!pHead) {
1,305✔
3201
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
3202
    code = -1;
×
UNCOV
3203
    goto _OVER;
×
3204
  }
3205

3206
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
1,305✔
3207
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3208
    rpcFreeCont(pHead);
×
UNCOV
3209
    code = -1;
×
UNCOV
3210
    goto _OVER;
×
3211
  }
3212

3213
  pRsp->pCont = pHead;
1,305✔
3214
  pRsp->contLen = contLen;
1,305✔
3215

3216
  vInfo(
1,305✔
3217
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3218
      "member1Token:%s",
3219
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3220

3221
  code = TSDB_CODE_SUCCESS;
1,305✔
3222

3223
_OVER:
1,305✔
3224
  if (code != 0) {
1,305✔
UNCOV
3225
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3226
           tstrerror(code));
3227
  }
3228
  tFreeSVArbCheckSyncReq(&syncReq);
1,305✔
3229
  return code;
1,305✔
3230
}
3231

3232
#ifndef TD_ENTERPRISE
3233
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
3234
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, ETsdbOpType type) { return 0; }
3235
#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