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

taosdata / TDengine / #4929

16 Jan 2026 02:32AM UTC coverage: 66.723% (+0.02%) from 66.708%
#4929

push

travis-ci

web-flow
enh: interp supports using non-null prev/next values to fill (#34236)

281 of 327 new or added lines in 11 files covered. (85.93%)

539 existing lines in 127 files now uncovered.

203225 of 304580 relevant lines covered (66.72%)

128590777.46 hits per line

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

64.84
/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) {
53,811,278✔
85
  int32_t code = 0;
53,811,278✔
86
  int32_t lino = 0;
53,811,278✔
87

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

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

99
  // name
100
  char *name = NULL;
53,816,755✔
101
  if (tDecodeCStr(pCoder, &name) < 0) {
53,814,935✔
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);
53,814,935✔
108
  if (uid == 0) {
53,803,044✔
109
    uid = tGenIdPI64();
53,444,495✔
110
  }
111
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
53,799,021✔
112

113
  // btime
114
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
53,800,584✔
115

116
  tEndDecode(pCoder);
53,797,563✔
117

118
_exit:
53,803,611✔
119
  if (code) {
53,803,651✔
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);
53,803,651✔
123
    if (pUid) *pUid = uid;
53,800,225✔
124
  }
125
  return code;
53,801,515✔
126
}
127
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
37,252,610✔
128
  int32_t code = 0;
37,252,610✔
129
  int32_t lino = 0;
37,252,610✔
130

131
  int64_t  btime = taosGetTimestampMs();
37,252,610✔
132
  SDecoder dc = {0};
37,252,610✔
133
  int32_t  nReqs;
37,251,808✔
134

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

141
  if (tDecodeI32v(&dc, &nReqs) < 0) {
37,251,766✔
142
    code = TSDB_CODE_INVALID_MSG;
×
143
    TSDB_CHECK_CODE(code, lino, _exit);
×
144
  }
145
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
79,383,250✔
146
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
42,144,936✔
147
    TSDB_CHECK_CODE(code, lino, _exit);
42,131,484✔
148
  }
149

150
  tEndDecode(&dc);
37,238,314✔
151

152
_exit:
37,251,911✔
153
  tDecoderClear(&dc);
37,251,083✔
154
  if (code) {
37,251,252✔
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,248,746✔
159
}
160

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

165
  SDecoder dc = {0};
11,829,074✔
166
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
11,829,074✔
167

168
  SVAlterTbReq vAlterTbReq = {0};
11,829,074✔
169
  int64_t      ctimeMs = taosGetTimestampMs();
11,829,074✔
170
  if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
11,829,074✔
171
    taosArrayDestroy(vAlterTbReq.pMultiTag);
×
172
    vAlterTbReq.pMultiTag = NULL;
×
173
    goto _exit;
×
174
  }
175
  taosArrayDestroy(vAlterTbReq.pMultiTag);
11,829,074✔
176
  vAlterTbReq.pMultiTag = NULL;
11,829,074✔
177

178
  code = 0;
11,829,074✔
179

180
_exit:
11,829,074✔
181
  tDecoderClear(&dc);
11,829,074✔
182
  if (code) {
11,829,074✔
183
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
184
  } else {
185
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
11,829,074✔
186
           ctimeMs);
187
  }
188
  return code;
11,829,074✔
189
}
190

191
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
9,677,111✔
192
  int32_t code = TSDB_CODE_INVALID_MSG;
9,677,111✔
193
  int32_t lino = 0;
9,677,111✔
194

195
  SMsgHead *pContOld = pMsg->pCont;
9,677,111✔
196
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
9,678,552✔
197

198
  SArray *tbUids = NULL;
9,678,956✔
199
  int64_t timestampMs = 0;
9,678,956✔
200

201
  SVDropTtlTableReq ttlReq = {0};
9,678,956✔
202
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
9,677,241✔
203
    code = TSDB_CODE_INVALID_MSG;
×
204
    TSDB_CHECK_CODE(code, lino, _exit);
×
205
  }
206

207
  {  // find expired uids
208
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
9,677,797✔
209
    if (tbUids == NULL) {
9,674,418✔
210
      code = terrno;
×
211
      TSDB_CHECK_CODE(code, lino, _exit);
×
212
    }
213

214
    timestampMs = (int64_t)ttlReq.timestampSec * 1000;
9,674,418✔
215
    code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
9,674,418✔
216
    if (code != 0) {
9,667,332✔
217
      code = TSDB_CODE_INVALID_MSG;
×
218
      TSDB_CHECK_CODE(code, lino, _exit);
×
219
    }
220

221
    ttlReq.nUids = taosArrayGetSize(tbUids);
9,667,332✔
222
    ttlReq.pTbUids = tbUids;
9,672,656✔
223
  }
224

225
  if (ttlReq.nUids == 0) {
9,672,656✔
226
    code = TSDB_CODE_MSG_PREPROCESSED;
9,654,712✔
227
    TSDB_CHECK_CODE(code, lino, _exit);
9,654,712✔
228
  }
229

230
  {  // prepare new content
231
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
11,890✔
232
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
11,391✔
233

234
    SMsgHead *pContNew = rpcMallocCont(contLenNew);
11,391✔
235
    if (pContNew == NULL) {
11,391✔
236
      code = terrno;
×
237
      TSDB_CHECK_CODE(code, lino, _exit);
×
238
    }
239

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

246
    rpcFreeCont(pContOld);
11,391✔
247
    pMsg->pCont = pContNew;
11,391✔
248
    pMsg->contLen = contLenNew;
11,391✔
249
  }
250

251
  code = 0;
11,391✔
252

253
_exit:
9,672,230✔
254
  taosArrayDestroy(tbUids);
9,672,925✔
255

256
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
9,657,348✔
257
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
258
           tstrerror(code), TMSG_INFO(pMsg->msgType));
259
  } else {
260
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
9,657,348✔
261
  }
262

263
  return code;
9,657,348✔
264
}
265

266
extern int64_t tsMaxKeyByPrecision[];
267
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
594,908,101✔
268
  int32_t code = 0;
594,908,101✔
269
  int32_t lino = 0;
594,908,101✔
270

271
  if (tStartDecode(pCoder) < 0) {
594,908,101✔
272
    code = TSDB_CODE_INVALID_MSG;
×
273
    TSDB_CHECK_CODE(code, lino, _exit);
×
274
  }
275

276
  SSubmitTbData submitTbData;
594,901,471✔
277
  uint8_t       version;
278
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
594,896,662✔
279
    code = TSDB_CODE_INVALID_MSG;
×
280
    TSDB_CHECK_CODE(code, lino, _exit);
×
281
  }
282
  version = (submitTbData.flags >> 8) & 0xff;
594,896,662✔
283
  submitTbData.flags = submitTbData.flags & 0xff;
594,896,662✔
284

285
  int64_t uid;
594,893,588✔
286
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
594,893,277✔
287
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
11,665,123✔
288
    TSDB_CHECK_CODE(code, lino, _exit);
11,663,103✔
289
  }
290

291
  // submit data
292
  if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
594,905,112✔
293
    code = TSDB_CODE_INVALID_MSG;
×
294
    TSDB_CHECK_CODE(code, lino, _exit);
×
295
  }
296

297
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
594,905,112✔
298
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
11,664,158✔
299
    pCoder->pos += sizeof(int64_t);
11,663,653✔
300
  } else {
301
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
583,235,876✔
302
      code = TSDB_CODE_INVALID_MSG;
×
303
      TSDB_CHECK_CODE(code, lino, _exit);
×
304
    }
305
  }
306

307
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
594,890,246✔
308
    code = TSDB_CODE_INVALID_MSG;
×
309
    TSDB_CHECK_CODE(code, lino, _exit);
×
310
  }
311

312
  // scan and check
313
  TSKEY now = btimeMs;
594,890,246✔
314
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
594,890,246✔
315
    now *= 1000;
2,457,554✔
316
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
592,435,152✔
317
    now *= 1000000;
1,433,398✔
318
  }
319

320
  int32_t keep = pVnode->config.tsdbCfg.keep2;
594,888,442✔
321

322
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
594,889,446✔
323
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
594,888,589✔
324
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
594,888,251✔
325
    uint64_t nColData;
3,355,688✔
326
    if (tDecodeU64v(pCoder, &nColData) < 0) {
3,357,074✔
327
      code = TSDB_CODE_INVALID_MSG;
×
328
      TSDB_CHECK_CODE(code, lino, _exit);
×
329
    }
330

331
    SColData colData = {0};
3,357,074✔
332
    code = tDecodeColData(version, pCoder, &colData, false);
3,355,139✔
333
    if (code) {
3,357,031✔
334
      code = TSDB_CODE_INVALID_MSG;
×
335
      TSDB_CHECK_CODE(code, lino, _exit);
×
336
    }
337

338
    if (colData.flag != HAS_VALUE) {
3,357,031✔
339
      code = TSDB_CODE_INVALID_MSG;
×
340
      TSDB_CHECK_CODE(code, lino, _exit);
×
341
    }
342

343
    for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
2,147,483,647✔
344
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
2,147,483,647✔
345
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
579✔
346
        TSDB_CHECK_CODE(code, lino, _exit);
579✔
347
      }
348
    }
349

350
    for (uint64_t i = 1; i < nColData; i++) {
10,795,582✔
351
      code = tDecodeColData(version, pCoder, &colData, true);
7,236,417✔
352
      if (code) {
7,236,135✔
353
        code = TSDB_CODE_INVALID_MSG;
×
354
        TSDB_CHECK_CODE(code, lino, _exit);
×
355
      }
356
    }
357
  } else {
358
    uint64_t nRow;
591,529,489✔
359
    if (tDecodeU64v(pCoder, &nRow) < 0) {
591,539,136✔
360
      code = TSDB_CODE_INVALID_MSG;
×
361
      TSDB_CHECK_CODE(code, lino, _exit);
×
362
    }
363

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

377
      // Check pRow->sver
378
      if (pRow->sver != submitTbData.sver) {
2,147,483,647✔
379
        code = TSDB_CODE_INVALID_DATA_FMT;
×
380
        TSDB_CHECK_CODE(code, lino, _exit);
×
381
      }
382
    }
383
  }
384

385
  if (!tDecodeIsEnd(pCoder)) {
594,827,818✔
386
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
594,829,692✔
387
    pCoder->pos += sizeof(int64_t);
594,829,626✔
388
  }
389

390
  tEndDecode(pCoder);
594,827,434✔
391

392
_exit:
594,897,111✔
393
  if (code) {
594,900,712✔
394
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
65,898✔
395
           lino, tstrerror(code));
396
  }
397
  return code;
594,900,712✔
398
}
399
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
567,590,249✔
400
  int32_t code = 0;
567,590,249✔
401
  int32_t lino = 0;
567,590,249✔
402

403
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
567,590,249✔
404
    return TSDB_CODE_MSG_PREPROCESSED;
78✔
405
  }
406

407
  SDecoder *pCoder = &(SDecoder){0};
567,590,171✔
408

409
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
567,592,473✔
410
    code = TSDB_CODE_INVALID_MSG;
×
411
    TSDB_CHECK_CODE(code, lino, _exit);
×
412
  }
413

414
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
567,591,755✔
415

416
  if (tStartDecode(pCoder) < 0) {
567,593,191✔
417
    code = TSDB_CODE_INVALID_MSG;
×
418
    TSDB_CHECK_CODE(code, lino, _exit);
×
419
  }
420

421
  uint64_t nSubmitTbData;
567,588,931✔
422
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
567,593,191✔
423
    code = TSDB_CODE_INVALID_MSG;
×
424
    TSDB_CHECK_CODE(code, lino, _exit);
×
425
  }
426

427
  int64_t btimeMs = taosGetTimestampMs();
567,592,328✔
428
  int64_t ctimeMs = btimeMs;
567,592,328✔
429
  for (int32_t i = 0; i < nSubmitTbData; i++) {
1,162,423,190✔
430
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
594,901,915✔
431
    TSDB_CHECK_CODE(code, lino, _exit);
594,896,760✔
432
  }
433

434
  tEndDecode(pCoder);
567,521,275✔
435

436
_exit:
567,591,246✔
437
  tDecoderClear(pCoder);
567,591,246✔
438
  if (code) {
567,591,404✔
439
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
65,898✔
440
           tstrerror(code), TMSG_INFO(pMsg->msgType));
441
  }
442
  return code;
567,583,769✔
443
}
444

445
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,693,682✔
446
  int32_t code = 0;
1,693,682✔
447

448
  int32_t    size;
449
  int32_t    ret;
450
  uint8_t   *pCont;
451
  SEncoder  *pCoder = &(SEncoder){0};
1,693,682✔
452
  SDeleteRes res = {0};
1,693,682✔
453

454
  SReadHandle handle = {0};
1,693,682✔
455
  handle.vnode = pVnode;
1,693,682✔
456
  handle.pMsgCb = &pVnode->msgCb;
1,693,682✔
457
  handle.skipRollup = 1;
1,693,682✔
458
  initStorageAPI(&handle.api);
1,693,682✔
459

460
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
1,693,682✔
461
  if (code) goto _exit;
1,693,682✔
462

463
  res.ctimeMs = taosGetTimestampMs();
1,693,682✔
464
  // malloc and encode
465
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
1,693,682✔
466
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
1,693,682✔
467

468
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
1,693,682✔
469
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
1,693,682✔
470

471
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
1,693,682✔
472
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
1,693,682✔
473
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
474
  }
475
  tEncoderClear(pCoder);
1,693,682✔
476

477
  rpcFreeCont(pMsg->pCont);
1,693,682✔
478
  pMsg->pCont = pCont;
1,693,682✔
479
  pMsg->contLen = size + sizeof(SMsgHead);
1,693,682✔
480

481
  taosArrayDestroy(res.uidList);
1,693,682✔
482

483
_exit:
1,693,682✔
484
  return code;
1,693,682✔
485
}
486

487
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
488
  int32_t code = 0;
×
489
  int32_t lino = 0;
×
490

491
  int64_t         ctimeMs = taosGetTimestampMs();
×
492
  SBatchDeleteReq pReq = {0};
×
493
  SDecoder       *pCoder = &(SDecoder){0};
×
494

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

497
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
498
    code = TSDB_CODE_INVALID_MSG;
×
499
  }
500

501
  tDecoderClear(pCoder);
×
502
  taosArrayDestroy(pReq.deleteReqs);
×
503

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

512
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
46,599✔
513
  int32_t ret = 0;
46,599✔
514
  if ((ret = vnodeCheckState(pVnode)) != 0) {
46,599✔
515
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
11,940✔
516
    return 0;
11,940✔
517
  }
518

519
  SVArbCheckSyncReq syncReq = {0};
34,659✔
520

521
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
34,659✔
522
                                    &syncReq) != 0) {
523
    return TSDB_CODE_INVALID_MSG;
×
524
  }
525

526
  ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
34,659✔
527
  if (ret != 0) {
34,659✔
528
    vError("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(ret));
32,524✔
529
  }
530

531
  int32_t code = terrno;
34,659✔
532
  tFreeSVArbCheckSyncReq(&syncReq);
34,659✔
533

534
  return code;
34,659✔
535
}
536

537
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,333,540✔
538
  int32_t          code = TSDB_CODE_SUCCESS;
1,333,540✔
539
  int32_t          lino = 0;
1,333,540✔
540
  int32_t          size = 0;
1,333,540✔
541
  SDecoder         dc = {0};
1,333,540✔
542
  SEncoder         ec = {0};
1,333,540✔
543
  SVDropTbBatchReq receivedBatchReqs = {0};
1,333,540✔
544
  SVDropTbBatchReq sentBatchReqs = {0};
1,333,540✔
545

546
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
1,333,540✔
547

548
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
1,333,540✔
549
  if (code < 0) {
1,333,540✔
550
    terrno = code;
×
551
    TSDB_CHECK_CODE(code, lino, _exit);
×
552
  }
553
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
1,333,540✔
554
  if (!sentBatchReqs.pArray) {
1,333,540✔
555
    code = terrno;
×
556
    goto _exit;
×
557
  }
558

559
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
2,722,245✔
560
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
1,388,705✔
561
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
1,388,705✔
562
    if (uid == 0) {
1,388,705✔
563
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
564
      continue;
×
565
    }
566
    pReq->uid = uid;
1,388,705✔
567
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
1,388,705✔
568
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
2,777,410✔
569
      code = terrno;
×
570
      goto _exit;
×
571
    }
572
  }
573
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
1,333,540✔
574

575
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
1,333,540✔
576
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
1,333,540✔
577
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
1,333,540✔
578
  tEncoderClear(&ec);
1,333,540✔
579
  if (code != TSDB_CODE_SUCCESS) {
1,333,540✔
580
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
581
    TSDB_CHECK_CODE(code, lino, _exit);
×
582
  }
583

584
_exit:
1,333,540✔
585
  tDecoderClear(&dc);
1,333,540✔
586
  if (sentBatchReqs.pArray) {
1,333,540✔
587
    taosArrayDestroy(sentBatchReqs.pArray);
1,333,540✔
588
  }
589
  return code;
1,333,540✔
590
}
591

592
int32_t vnodePreProcessSsMigrateFileSetReq(SVnode *pVnode, SRpcMsg *pMsg) {
×
593
  int32_t              code = TSDB_CODE_SUCCESS, lino = 0;
×
594
  SSsMigrateFileSetReq req = {0};
×
595

596
  code = tDeserializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
×
597
                                          pMsg->contLen - sizeof(SMsgHead), &req);
×
598
  if (code < 0) {
×
599
    terrno = code;
×
600
    TSDB_CHECK_CODE(code, lino, _exit);
×
601
  }
602

603
  req.nodeId = vnodeNodeId(pVnode);
×
604
  TAOS_UNUSED(tSerializeSSsMigrateFileSetReq(POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)),
×
605
                                             pMsg->contLen - sizeof(SMsgHead), &req));
606

607
_exit:
×
608
  return code;
×
609
}
610

611
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
648,803,640✔
612
  int32_t code = 0;
648,803,640✔
613

614
  if (pVnode->mounted) {
648,803,640✔
615
    vError("vgId:%d, mountVgId:%d, skip write msg:%s", TD_VID(pVnode), pVnode->config.mountVgId,
1,340✔
616
           TMSG_INFO(pMsg->msgType));
617
    return TSDB_CODE_OPS_NOT_SUPPORT;
1,340✔
618
  }
619

620
  switch (pMsg->msgType) {
648,804,248✔
621
    case TDMT_VND_CREATE_TABLE: {
37,252,610✔
622
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
37,252,610✔
623
    } break;
37,250,023✔
624
    case TDMT_VND_ALTER_TABLE: {
11,829,074✔
625
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
11,829,074✔
626
    } break;
11,829,074✔
627
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
9,680,996✔
628
    case TDMT_VND_DROP_TTL_TABLE: {
629
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
9,680,996✔
630
    } break;
9,665,527✔
631
    case TDMT_VND_SUBMIT: {
567,593,224✔
632
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
567,593,224✔
633
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
634
    } break;
567,586,886✔
635
    case TDMT_VND_DELETE: {
1,693,682✔
636
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
1,693,682✔
637
    } break;
1,693,682✔
638
    case TDMT_VND_BATCH_DEL: {
×
639
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
640
    } break;
×
641
    case TDMT_VND_ARB_CHECK_SYNC: {
46,599✔
642
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
46,599✔
643
    } break;
46,599✔
644
    case TDMT_VND_DROP_TABLE: {
1,333,540✔
645
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
1,333,540✔
646
    } break;
1,333,540✔
647
#ifdef TD_ENTERPRISE
UNCOV
648
    case TDMT_VND_SSMIGRATE_FILESET: {
×
UNCOV
649
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
×
650
    } break;
×
651
#endif
652
    default:
19,377,786✔
653
      break;
19,377,786✔
654
  }
655

656
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
648,783,117✔
657
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
98,422✔
658
           TMSG_INFO(pMsg->msgType));
659
  }
660
  return code;
648,786,302✔
661
}
662

663
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
21,713✔
664
  int32_t code = 0;
21,713✔
665
  int32_t lino = 0;
21,713✔
666

667
  int64_t   st = taosGetTimestampUs();
21,713✔
668
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
21,713✔
669
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
21,713✔
670

671
  SBseBatch *pBatch = NULL;
21,713✔
672

673
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
21,713✔
674
  TSDB_CHECK_CODE(code, lino, _exit);
21,713✔
675

676
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
21,713✔
677
  int32_t rowIdx = -1;
21,713✔
678
  for (int32_t i = 0; i < sz; i++) {
24,238,666✔
679
    int64_t     seq = 0;
24,216,953✔
680
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
24,216,953✔
681
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
24,216,953✔
682
      // skip empty or null blob
683
      continue;
2,432,592✔
684
    }
685

686
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
21,784,361✔
687
    TSDB_CHECK_CODE(code, lino, _exit);
21,784,361✔
688

689
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
21,784,361✔
690
    if (row == NULL) {
21,784,361✔
691
      int32_t tlen = taosArrayGetSize(pBlobSet->pSeqTable);
×
692
      uTrace("blob invalid row index:%d, sz:%d, pBlobSet size:%d", rowIdx, sz, tlen);
×
693
      break;
×
694
    }
695

696
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
43,568,722✔
697
      code = TSDB_CODE_INVALID_MSG;
×
698
      TSDB_CHECK_CODE(code, lino, _exit);
×
699
    }
700
  }
701

702
  code = bseCommitBatch(pVnode->pBse, pBatch);
21,713✔
703
  TSDB_CHECK_CODE(code, lino, _exit);
21,713✔
704

705
  int64_t cost = taosGetTimestampUs() - st;
21,713✔
706
  if (cost >= 500) {
21,713✔
707
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
7,577✔
708
           pBlobSet->len);
709
  }
710
_exit:
21,713✔
711
  if (code != 0) {
21,713✔
712
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
713
  }
714
  return code;
21,713✔
715
}
716

717
static int32_t inline vnodeSubmitSubColBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
718
  int32_t code = 0;
×
719
  int32_t lino = 0;
×
720

721
  int32_t   blobColIdx = 0;
×
722
  SColData *pBlobCol = NULL;
×
723
  int64_t   st = taosGetTimestampUs();
×
724
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
725
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
726

727
  SBseBatch *pBatch = NULL;
×
728

729
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
730
  TSDB_CHECK_CODE(code, lino, _exit);
×
731

732
  SColData *p = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
×
733
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitTbData->aCol); i++) {
×
734
    if (IS_STR_DATA_BLOB(p[i].type)) {
×
735
      pBlobCol = &p[i];
×
736
      break;
×
737
    }
738
  }
739
  if (pBlobCol == NULL) {
×
740
    vError("vgId:%d %s failed to find blob column in submit data", TD_VID(pVnode), __func__);
×
741
    code = TSDB_CODE_INVALID_MSG;
×
742
    goto _exit;
×
743
  }
744

745
  int32_t offset = 0;
×
746
  // int32_t   rowIdx = -1;
747
  for (int32_t i = 0; i < sz; i++) {
×
748
    int64_t     seq = 0;
×
749
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
750
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
751
      // skip empty or null blob
752
      continue;
×
753
    }
754
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
755
    TSDB_CHECK_CODE(code, lino, _exit);
×
756

757
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
×
758
    offset += BSE_SEQUECE_SIZE;
×
759
  }
760

761
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
762
  TSDB_CHECK_CODE(code, lino, _exit);
×
763

764
  int64_t cost = taosGetTimestampUs() - st;
×
765
  if (cost >= 500) {
×
766
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
767
           pBlobSet->len);
768
  }
769
_exit:
×
770
  if (code != 0) {
×
771
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
772
  }
773
  return code;
×
774
}
775
static int32_t inline vnodeSubmitBlobData(SVnode *pVnode, SSubmitTbData *pData) {
21,713✔
776
  int32_t code = 0;
21,713✔
777
  if (pData->flags & SUBMIT_REQ_WITH_BLOB) {
21,713✔
778
    if (pData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
21,713✔
779
      code = vnodeSubmitSubColBlobData(pVnode, pData);
×
780
    } else {
781
      code = vnodeSubmitSubRowBlobData(pVnode, pData);
21,713✔
782
    }
783
  }
784

785
  return code;
21,713✔
786
}
787

788
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
779,425,564✔
789
  int32_t code = 0;
779,425,564✔
790
  int32_t lino = 0;
779,425,564✔
791
  void   *ptr = NULL;
779,425,564✔
792
  void   *pReq;
793
  int32_t len;
794

795
  (void)taosThreadMutexLock(&pVnode->mutex);
779,425,564✔
796
  if (pVnode->disableWrite) {
779,490,079✔
797
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
798
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
799
    return TSDB_CODE_VND_WRITE_DISABLED;
×
800
  }
801
  (void)taosThreadMutexUnlock(&pVnode->mutex);
779,411,421✔
802

803
  if (ver <= pVnode->state.applied) {
779,451,075✔
804
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
805
           pVnode->state.applied);
806
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
807
  }
808

809
  vGDebug(&pMsg->info.traceId,
779,424,597✔
810
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
811
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
812
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
813
          pMsg->info.conn.applyTerm, pMsg->contLen);
814

815
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
779,510,116✔
816
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
817
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
818
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
819
  }
820

821
  if (!(pVnode->state.applied + 1 == ver)) {
779,507,575✔
822
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
823
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
824
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
825
  }
826

827
  atomic_store_64(&pVnode->state.applied, ver);
779,507,027✔
828
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
779,511,526✔
829

830
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
779,511,830✔
831

832
  // skip header
833
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
769,882,406✔
834
  len = pMsg->contLen - sizeof(SMsgHead);
769,882,731✔
835
  bool needCommit = false;
769,880,009✔
836
  bool forceTrimWal = false;
769,880,009✔
837

838
  switch (pMsg->msgType) {
769,880,009✔
839
    /* META */
840
    case TDMT_VND_CREATE_STB:
4,958,955✔
841
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
4,958,955✔
842
      TSDB_CHECK_CODE(code, lino, _err);
4,959,746✔
843
      break;
4,956,770✔
844
    case TDMT_VND_ALTER_STB:
7,331,542✔
845
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
7,331,542✔
846
      TSDB_CHECK_CODE(code, lino, _err);
7,330,914✔
847
      break;
7,330,914✔
848
    case TDMT_VND_DROP_STB:
891,175✔
849
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
891,175✔
850
      TSDB_CHECK_CODE(code, lino, _err);
891,175✔
851
      break;
891,175✔
852
    case TDMT_VND_CREATE_TABLE:
40,061,009✔
853
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
40,061,009✔
854
      TSDB_CHECK_CODE(code, lino, _err);
40,052,231✔
855
      break;
40,052,231✔
856
    case TDMT_VND_ALTER_TABLE:
11,829,672✔
857
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
11,829,672✔
858
      TSDB_CHECK_CODE(code, lino, _err);
11,829,672✔
859
      break;
11,829,672✔
860
    case TDMT_VND_DROP_TABLE:
1,333,868✔
861
    case TDMT_VND_SNODE_DROP_TABLE:
862
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
1,333,868✔
863
      TSDB_CHECK_CODE(code, lino, _err);
1,333,868✔
864
      break;
1,333,868✔
865
    case TDMT_VND_CREATE_RSMA:
22,320✔
866
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
22,320✔
867
      TSDB_CHECK_CODE(code, lino, _err);
22,320✔
868
      break;
22,320✔
869
    case TDMT_VND_DROP_RSMA:
4,464✔
870
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
4,464✔
871
      TSDB_CHECK_CODE(code, lino, _err);
4,464✔
872
      break;
4,464✔
873
    case TDMT_VND_ALTER_RSMA:
11,904✔
874
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
11,904✔
875
      TSDB_CHECK_CODE(code, lino, _err);
11,904✔
876
      break;
11,904✔
877
    case TDMT_VND_DROP_TTL_TABLE:
×
878
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
879
      TSDB_CHECK_CODE(code, lino, _err);
×
880
      break;
×
881
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
11,391✔
882
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
11,391✔
883
      TSDB_CHECK_CODE(code, lino, _err);
11,391✔
884
      break;
11,391✔
885
    case TDMT_VND_TRIM:
50,656✔
886
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
50,656✔
887
      TSDB_CHECK_CODE(code, lino, _err);
51,372✔
888
      break;
51,372✔
889
      case TDMT_VND_TRIM_WAL:
6,516✔
890
        needCommit = true;
6,516✔
891
        forceTrimWal = true;
6,516✔
892
        break;
6,516✔
893
#ifdef TD_ENTERPRISE
894
    case TDMT_VND_SSMIGRATE_FILESET:
×
895
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
896
      break;
×
897
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
898
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
899
      break;
×
900
    case TDMT_VND_KILL_SSMIGRATE:
×
901
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
902
      break;
×
903
#endif
904

905
    /* TSDB */
906
    case TDMT_VND_SUBMIT: {
687,242,969✔
907
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
687,242,969✔
908
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
909
      });
910
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
687,210,584✔
911
      break;
687,190,020✔
912
    }
913
    case TDMT_VND_DELETE:
2,062,615✔
914
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
2,062,615✔
915
      TSDB_CHECK_CODE(code, lino, _err);
2,062,615✔
916
      break;
2,062,615✔
917
    case TDMT_VND_BATCH_DEL:
×
918
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
919
      TSDB_CHECK_CODE(code, lino, _err);
×
920
      break;
×
921
      /* TQ */
922
#if defined(USE_TQ) || defined(USE_STREAM)
923
    case TDMT_VND_TMQ_SUBSCRIBE:
1,327,583✔
924
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
1,327,583✔
925
      TSDB_CHECK_CODE(code, lino, _err);
1,323,646✔
926
      break;
1,323,646✔
927
    case TDMT_VND_TMQ_DELETE_SUB:
279,635✔
928
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
279,635✔
929
      TSDB_CHECK_CODE(code, lino, _err);
278,989✔
930
      break;
278,989✔
931
    case TDMT_VND_TMQ_COMMIT_OFFSET:
3,564,229✔
932
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
3,564,229✔
933
      TSDB_CHECK_CODE(code, lino, _err);
3,563,046✔
934
      break;
3,563,046✔
935
#endif
936
    case TDMT_VND_ALTER_CONFIRM:
3,626,006✔
937
      needCommit = pVnode->config.hashChange;
3,626,006✔
938
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
3,626,006✔
939
      TSDB_CHECK_CODE(code, lino, _err);
3,626,006✔
940
      break;
3,626,006✔
941
    case TDMT_VND_ALTER_CONFIG:
316,932✔
942
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
316,932✔
943
      break;
316,932✔
944
    case TDMT_VND_COMMIT:
4,858,416✔
945
      needCommit = true;
4,858,416✔
946
      break;
4,858,416✔
947
    case TDMT_VND_CREATE_INDEX:
4,468✔
948
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
4,468✔
949
      break;
4,468✔
950
    case TDMT_VND_DROP_INDEX:
2,960✔
951
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2,960✔
952
      break;
2,960✔
953
    case TDMT_VND_COMPACT:
65,167✔
954
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
65,167✔
955
      goto _exit;
65,167✔
956
    case TDMT_VND_SCAN:
320✔
957
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
320✔
958
      goto _exit;
320✔
959
    case TDMT_SYNC_CONFIG_CHANGE:
×
960
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
961
      break;
×
962
#ifdef TD_ENTERPRISE
963
    case TDMT_VND_KILL_COMPACT:
1,488✔
964
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
1,488✔
965
      break;
1,488✔
966
#endif
967
    case TDMT_VND_KILL_SCAN:
128✔
968
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
128✔
969
      break;
128✔
970
    case TDMT_VND_KILL_TRIM:
×
971
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
972
      break;
×
973
    /* ARB */
974
    case TDMT_VND_ARB_CHECK_SYNC:
12,602✔
975
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
12,602✔
976
      break;
12,602✔
977
    default:
×
978
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
979
      return TSDB_CODE_INVALID_MSG;
×
980
  }
981

982
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
769,743,913✔
983

984
  walApplyVer(pVnode->pWal, ver);
769,786,113✔
985

986
  if (pVnode->pTq) {
769,786,343✔
987
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
769,786,343✔
988
    if (code) {
769,786,195✔
989
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
990
      return code;
×
991
    }
992
  }
993

994
  // commit if need
995
  if (needCommit) {
769,786,195✔
996
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
4,889,112✔
997
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
4,889,112✔
998
    if (code) {
4,888,734✔
999
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
1000
      goto _err;
×
1001
    }
1002

1003
    // start a new one
1004
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
4,888,734✔
1005
      code = vnodeBegin(pVnode);
1006
      if (code) {
1007
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
1008
        goto _err;
1009
      }
1010
    });
1011
  }
1012

1013
_exit:
769,785,291✔
1014
  return 0;
779,471,795✔
1015

1016
_err:
33,639✔
1017
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
33,639✔
1018
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
2,976✔
1019
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1020
  } else if (code) {
30,663✔
1021
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1022
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1023
  }
1024

1025
  return code;
33,639✔
1026
}
1027

1028
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
526,429,290✔
1029
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
526,429,290✔
1030
    return 0;
119,058,310✔
1031
  }
1032

1033
  int32_t qType = 0;
407,432,633✔
1034
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
407,461,505✔
1035
}
1036

1037
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
526,584,836✔
1038
  vTrace("message in vnode query queue is processing");
526,584,836✔
1039
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
526,584,836✔
1040
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
360,064✔
1041
    return 0;
360,064✔
1042
  }
1043

1044
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
526,226,111✔
1045
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1046
    return 0;
×
1047
  }
1048

1049
  SReadHandle handle = {0};
526,231,380✔
1050
  handle.vnode = pVnode;
526,227,128✔
1051
  handle.pMsgCb = &pVnode->msgCb;
526,227,128✔
1052
  handle.pWorkerCb = pInfo->workerCb;
526,178,942✔
1053
  initStorageAPI(&handle.api);
526,184,602✔
1054
  int32_t code = TSDB_CODE_SUCCESS;
525,967,000✔
1055
  bool    redirected = false;
525,967,000✔
1056

1057
  switch (pMsg->msgType) {
525,967,000✔
1058
    case TDMT_SCH_QUERY:
323,431,512✔
1059
      if (!syncIsReadyForRead(pVnode->sync)) {
323,431,512✔
1060
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
1,972,297✔
1061
        redirected = true;
1,972,297✔
1062
      }
1063
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
323,487,099✔
1064
      if (redirected) {
323,255,617✔
1065
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
1,972,297✔
1066
        return 0;
1,972,297✔
1067
      }
1068

1069
      return code;
321,283,320✔
1070
    case TDMT_SCH_MERGE_QUERY:
84,023,125✔
1071
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
84,023,125✔
1072
    case TDMT_SCH_QUERY_CONTINUE:
84,362,203✔
1073
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
84,362,203✔
1074
    case TDMT_VND_TMQ_CONSUME:
26,137,381✔
1075
      return tqProcessPollReq(pVnode->pTq, pMsg);
26,137,381✔
1076
    case TDMT_VND_TMQ_CONSUME_PUSH:
8,175,537✔
1077
      return tqProcessPollPush(pVnode->pTq);
8,175,537✔
1078
    default:
×
1079
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1080
      return TSDB_CODE_APP_ERROR;
×
1081
  }
1082
}
1083

1084
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,079,265,117✔
1085
  int32_t code = TSDB_CODE_SUCCESS;
1,079,265,117✔
1086
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
1,079,265,117✔
1087
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
1,079,265,117✔
1088
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
712,049,296✔
1089
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
686,544,663✔
1090
      !syncIsReadyForRead(pVnode->sync)) {
392,738,785✔
1091
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
577,008✔
1092
    return 0;
577,008✔
1093
  }
1094

1095
  switch (pMsg->msgType) {
1,078,850,891✔
1096
    case TDMT_SCH_FETCH:
474,795,066✔
1097
    case TDMT_SCH_MERGE_FETCH:
1098
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
474,795,066✔
1099
    case TDMT_SCH_FETCH_RSP:
×
1100
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1101
    // case TDMT_SCH_CANCEL_TASK:
1102
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1103
    case TDMT_SCH_DROP_TASK:
407,254,637✔
1104
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
407,254,637✔
1105
    case TDMT_SCH_TASK_NOTIFY:
11,960✔
1106
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
11,960✔
1107
    case TDMT_SCH_QUERY_HEARTBEAT:
170,689,065✔
1108
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
170,689,065✔
1109
    case TDMT_VND_TABLE_META:
892,370✔
1110
    case TDMT_VND_TABLE_NAME:
1111
      return vnodeGetTableMeta(pVnode, pMsg, true);
892,370✔
1112
    case TDMT_VND_TABLE_CFG:
×
1113
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1114
    case TDMT_VND_BATCH_META: {
24,939,964✔
1115
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
24,939,964✔
1116
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1117
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
24,943,598✔
1118
      return code;
24,939,644✔
1119
    }
1120
    case TDMT_VND_VSUBTABLES_META:
×
1121
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1122
    case TDMT_VND_VSTB_REF_DBS:
×
1123
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1124
    case TDMT_VND_QUERY_SCAN_PROGRESS:
640✔
1125
      return vnodeQueryScanProgress(pVnode, pMsg);
640✔
1126
#ifdef TD_ENTERPRISE
1127
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
97,385✔
1128
      return vnodeQueryCompactProgress(pVnode, pMsg);
97,385✔
1129

1130
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
×
1131
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
×
1132

1133
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
1134
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1135
#endif
1136
    case TDMT_VND_QUERY_TRIM_PROGRESS:
81,739✔
1137
      return vnodeQueryRetentionProgress(pVnode, pMsg);
81,739✔
1138
      //    case TDMT_VND_TMQ_CONSUME:
1139
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1140
#ifdef USE_TQ
1141
    case TDMT_VND_TMQ_VG_WALINFO:
586✔
1142
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
586✔
1143
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
293✔
1144
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
293✔
1145
    case TDMT_VND_TMQ_SEEK:
1,172✔
1146
      return tqProcessSeekReq(pVnode->pTq, pMsg);
1,172✔
1147
#endif
1148
    default:
3,587✔
1149
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
3,587✔
1150
      return TSDB_CODE_APP_ERROR;
×
1151
  }
1152
}
1153

1154
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
62,828,132✔
1155
  if (NULL == pMetaRsp) {
62,828,132✔
1156
    return;
×
1157
  }
1158

1159
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
62,828,132✔
1160
  pMetaRsp->dbId = pVnode->config.dbId;
62,830,330✔
1161
  pMetaRsp->vgId = TD_VID(pVnode);
62,831,293✔
1162
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
62,829,327✔
1163
}
1164

1165
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1166

1167
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
50,656✔
1168
  if (!pVnode->restored) {
50,656✔
1169
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
769✔
1170
    return 0;
769✔
1171
  }
1172

1173
  int32_t     code = 0;
50,603✔
1174
  SVTrimDbReq trimReq = {0};
50,603✔
1175

1176
  // decode
1177
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
50,603✔
1178
    code = TSDB_CODE_INVALID_MSG;
×
1179
    goto _exit;
×
1180
  }
1181

1182
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
50,603✔
1183
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1184

1185
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
50,603✔
1186

1187
_exit:
50,603✔
1188
  return code;
50,603✔
1189
}
1190

1191
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1192

1193
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1194

1195
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1196
  int32_t code = 0;
×
1197

1198
  SSsMigrateFileSetReq req = {0};
×
1199
  SSsMigrateFileSetRsp rsp = {0};
×
1200
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
×
1201
  pRsp->code = 0;
×
1202
  pRsp->pCont = NULL;
×
1203
  pRsp->contLen = 0;
×
1204

1205
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
×
1206
    code = TSDB_CODE_INVALID_MSG;
×
1207
    goto _exit;
×
1208
  }
1209

1210
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId,
×
1211
        req.ssMigrateId, req.fid, req.startTimeSec);
1212
  rsp.ssMigrateId = req.ssMigrateId;
×
1213
  rsp.vgId = TD_VID(pVnode);
×
1214
  rsp.nodeId = req.nodeId;
×
1215
  rsp.fid = req.fid;
×
1216

1217
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
×
1218
  if (code != TSDB_CODE_SUCCESS) {
×
1219
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1220
    pRsp->code = code;
×
1221
    goto _exit;
×
1222
  }
1223

1224
  pRsp->code = TSDB_CODE_SUCCESS;
×
1225
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
×
1226
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1227
  if (pRsp->pCont == NULL) {
×
1228
    vError("vgId:%d, failed to allocate memory for ssmigrate fileset response", TD_VID(pVnode));
×
1229
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1230
    goto _exit;
×
1231
  }
1232
  TAOS_UNUSED(tSerializeSSsMigrateFileSetRsp(pRsp->pCont, pRsp->contLen, &rsp));
×
1233

1234
_exit:
×
1235
  pRsp->code = code;
×
1236
  return code;
×
1237
}
1238

1239
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1240

1241
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1242
  int32_t            code = 0;
×
1243
  SSsMigrateProgress req = {0};
×
1244

1245
  // decode
1246
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
×
1247
    code = TSDB_CODE_INVALID_MSG;
×
1248
    goto _exit;
×
1249
  }
1250

1251
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1252

1253
_exit:
×
1254
  pRsp->code = code;
×
1255
  return code;
×
1256
}
1257

1258
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1259

1260
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1261
  int32_t          code = 0;
×
1262
  SVnodeKillSsMigrateReq req = {0};
×
1263

1264
  // decode
1265
  if (tDeserializeSVnodeKillSsMigrateReq(pReq, len, &req) != 0) {
×
1266
    code = TSDB_CODE_INVALID_MSG;
×
1267
    goto _exit;
×
1268
  }
1269

1270
  code = vnodeKillSsMigrate(pVnode, &req);
×
1271

1272
_exit:
×
1273
  pRsp->code = code;
×
1274
  return code;
×
1275
}
1276

1277
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1278
  int               ret = 0;
×
1279
  SVDropTtlTableReq ttlReq = {0};
×
1280
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1281
    ret = TSDB_CODE_INVALID_MSG;
×
1282
    goto end;
×
1283
  }
1284

1285
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1286
    ret = TSDB_CODE_INVALID_MSG;
×
1287
    goto end;
×
1288
  }
1289

1290
  if (ttlReq.nUids != 0) {
×
1291
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1292
          ttlReq.nUids);
1293
  }
1294

1295
  if (ttlReq.nUids > 0) {
×
1296
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1297
    if (code) return code;
×
1298

1299
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1300
    if (code) {
×
1301
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1302
    }
1303
  }
1304

1305
end:
×
1306
  taosArrayDestroy(ttlReq.pTbUids);
×
1307
  return ret;
×
1308
}
1309

1310
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,391✔
1311
  int32_t                 code = -1;
11,391✔
1312
  SMetaReader             mr = {0};
11,391✔
1313
  SVDropTtlTableReq       ttlReq = {0};
11,391✔
1314
  SVFetchTtlExpiredTbsRsp rsp = {0};
11,391✔
1315
  SEncoder                encoder = {0};
11,391✔
1316
  SArray                 *pNames = NULL;
11,391✔
1317
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
11,391✔
1318
  pRsp->code = TSDB_CODE_SUCCESS;
11,391✔
1319
  pRsp->pCont = NULL;
11,391✔
1320
  pRsp->contLen = 0;
11,391✔
1321

1322
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
11,391✔
1323
    terrno = TSDB_CODE_INVALID_MSG;
×
1324
    goto _end;
×
1325
  }
1326

1327
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
11,391✔
1328
    terrno = TSDB_CODE_INVALID_MSG;
×
1329
    goto _end;
×
1330
  }
1331

1332
  tb_uid_t    suid;
1333
  char        ctbName[TSDB_TABLE_NAME_LEN];
1334
  SVDropTbReq expiredTb = {.igNotExists = true};
11,391✔
1335
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
11,391✔
1336
  rsp.vgId = TD_VID(pVnode);
11,391✔
1337
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
11,391✔
1338
  if (!rsp.pExpiredTbs) goto _end;
11,391✔
1339

1340
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
11,391✔
1341
  if (!pNames) {
11,391✔
1342
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1343
    goto _end;
×
1344
  }
1345
  char buf[TSDB_TABLE_NAME_LEN];
11,391✔
1346
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
58,211✔
1347
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
46,820✔
1348
    expiredTb.suid = *uid;
46,820✔
1349
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
46,820✔
1350
    if (terrno < 0) goto _end;
46,820✔
1351
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
46,820✔
1352
    void *p = taosArrayPush(pNames, buf);
46,820✔
1353
    if (p == NULL) {
46,820✔
1354
      goto _end;
×
1355
    }
1356

1357
    expiredTb.name = p;
46,820✔
1358
    if (mr.me.type == TSDB_CHILD_TABLE) {
46,820✔
1359
      expiredTb.suid = mr.me.ctbEntry.suid;
30,434✔
1360
    }
1361

1362
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
93,640✔
1363
      goto _end;
×
1364
    }
1365
  }
1366

1367
  int32_t ret = 0;
11,391✔
1368
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
11,391✔
1369
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,391✔
1370
  if (pRsp->pCont == NULL) {
11,391✔
1371
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1372
    code = -1;
×
1373
    goto _end;
×
1374
  }
1375
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
11,391✔
1376
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
11,391✔
1377
  tEncoderClear(&encoder);
11,391✔
1378

1379
  if (terrno == 0) code = 0;
11,391✔
1380
_end:
11,391✔
1381
  metaReaderClear(&mr);
11,391✔
1382
  tFreeFetchTtlExpiredTbsRsp(&rsp);
11,391✔
1383
  taosArrayDestroy(ttlReq.pTbUids);
11,391✔
1384
  if (pNames) taosArrayDestroy(pNames);
11,391✔
1385
  pRsp->code = terrno;
11,391✔
1386
  return code;
11,391✔
1387
}
1388

1389
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,956,651✔
1390
  int32_t        code = 0;
4,956,651✔
1391
  SVCreateStbReq req = {0};
4,956,651✔
1392
  SDecoder       coder;
4,959,093✔
1393

1394
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
4,958,955✔
1395
  pRsp->code = TSDB_CODE_SUCCESS;
4,959,303✔
1396
  pRsp->pCont = NULL;
4,959,453✔
1397
  pRsp->contLen = 0;
4,959,453✔
1398

1399
  // decode and process req
1400
  tDecoderInit(&coder, pReq, len);
4,958,815✔
1401

1402
  code = tDecodeSVCreateStbReq(&coder, &req);
4,959,651✔
1403
  if (code) {
4,938,720✔
1404
    pRsp->code = code;
×
1405
    goto _err;
×
1406
  }
1407

1408
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
4,938,720✔
1409
  if (code) {
4,960,149✔
1410
    pRsp->code = code;
2,976✔
1411
    goto _err;
2,976✔
1412
  }
1413

1414
  tDecoderClear(&coder);
4,957,173✔
1415
  return 0;
4,956,770✔
1416

1417
_err:
2,976✔
1418
  tDecoderClear(&coder);
2,976✔
1419
  return code;
2,976✔
1420
}
1421

1422
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
40,061,009✔
1423
                                       SRpcMsg *pOriginRpc) {
1424
  SDecoder           decoder = {0};
40,061,009✔
1425
  SEncoder           encoder = {0};
40,061,009✔
1426
  int32_t            rcode = 0;
40,061,009✔
1427
  SVCreateTbBatchReq req = {0};
40,061,009✔
1428
  SVCreateTbReq     *pCreateReq;
1429
  SVCreateTbBatchRsp rsp = {0};
40,061,009✔
1430
  SVCreateTbRsp      cRsp = {0};
40,061,009✔
1431
  char               tbName[TSDB_TABLE_FNAME_LEN];
40,060,175✔
1432
  SArray            *tbUids = NULL;
40,060,699✔
1433
  SArray            *tbNames = NULL;
40,060,699✔
1434
  int64_t            tss = taosGetTimestampMs();
40,061,009✔
1435
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
40,061,009✔
1436
  pRsp->code = TSDB_CODE_SUCCESS;
40,061,009✔
1437
  pRsp->pCont = NULL;
40,061,009✔
1438
  pRsp->contLen = 0;
40,061,009✔
1439

1440
  // decode
1441
  tDecoderInit(&decoder, pReq, len);
40,061,009✔
1442
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
40,061,009✔
1443
    rcode = -1;
×
1444
    terrno = TSDB_CODE_INVALID_MSG;
×
1445
    goto _exit;
×
1446
  }
1447

1448
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
40,059,520✔
1449
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
40,056,007✔
1450
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
40,054,365✔
1451
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
40,053,997✔
1452
    rcode = -1;
2✔
1453
    terrno = TSDB_CODE_OUT_OF_MEMORY;
2✔
1454
    goto _exit;
×
1455
  }
1456

1457
  // loop to create table
1458
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
87,913,872✔
1459
    pCreateReq = req.pReqs + iReq;
47,852,077✔
1460
    memset(&cRsp, 0, sizeof(cRsp));
47,850,053✔
1461

1462
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
47,850,053✔
1463
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
80✔
1464
      if (str == NULL) {
80✔
1465
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1466
        rcode = -1;
×
1467
        goto _exit;
×
1468
      }
1469
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
80✔
1470
      if (taosArrayPush(tbNames, &str) == NULL) {
80✔
1471
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1472
        rcode = -1;
×
1473
        goto _exit;
×
1474
      }
1475
    }
1476

1477
    // validate hash
1478
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
47,850,053✔
1479
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
47,853,096✔
1480
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1481
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1482
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1483
        rcode = -1;
×
1484
        goto _exit;
×
1485
      }
1486
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1487
      continue;
×
1488
    }
1489

1490
    // do create table
1491
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
47,838,538✔
1492
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
17,005✔
1493
        cRsp.code = TSDB_CODE_SUCCESS;
5,308✔
1494
      } else {
1495
        cRsp.code = terrno;
11,697✔
1496
      }
1497
    } else {
1498
      cRsp.code = TSDB_CODE_SUCCESS;
47,841,483✔
1499
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
95,683,692✔
1500
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1501
        rcode = -1;
×
1502
        goto _exit;
×
1503
      }
1504
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
47,842,209✔
1505
    }
1506

1507
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
95,713,079✔
1508
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1509
      rcode = -1;
×
1510
      goto _exit;
×
1511
    }
1512
  }
1513

1514
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
40,061,795✔
1515
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
40,061,795✔
1516
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1517
  }
1518

1519
  // prepare rsp
1520
  int32_t ret = 0;
40,061,007✔
1521
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
40,061,007✔
1522
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
40,057,042✔
1523
  if (pRsp->pCont == NULL) {
40,051,807✔
1524
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1525
    rcode = -1;
×
1526
    goto _exit;
×
1527
  }
1528
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
40,048,408✔
1529
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
40,055,492✔
1530
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1531
  }
1532

1533
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
40,055,032✔
1534
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
80✔
1535

1536
    SName name = {0};
80✔
1537
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
80✔
1538
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1539
    }
1540

1541
    SStringBuilder sb = {0};
80✔
1542
    for (int32_t i = 0; i < tbNames->size; i++) {
160✔
1543
      char **key = (char **)taosArrayGet(tbNames, i);
80✔
1544
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
80✔
1545
      if (i < tbNames->size - 1) {
80✔
1546
        taosStringBuilderAppendChar(&sb, ',');
×
1547
      }
1548
      // taosMemoryFreeClear(*key);
1549
    }
1550

1551
    size_t len = 0;
80✔
1552
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
80✔
1553

1554
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
80✔
1555
      int64_t tse = taosGetTimestampMs();
80✔
1556
      double  duration = (double)(tse - tss);
80✔
1557
      duration = duration / 1000;
80✔
1558
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len, duration, 0);
80✔
1559
    }
1560

1561
    taosStringBuilderDestroy(&sb);
80✔
1562
  }
1563

1564
_exit:
40,055,032✔
1565
  tDeleteSVCreateTbBatchReq(&req);
40,052,084✔
1566
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
40,050,404✔
1567
  taosArrayDestroy(tbUids);
40,036,381✔
1568
  tDecoderClear(&decoder);
40,041,583✔
1569
  tEncoderClear(&encoder);
40,058,133✔
1570
  taosArrayDestroyP(tbNames, NULL);
40,056,525✔
1571
  return rcode;
40,049,208✔
1572
}
1573

1574
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
7,331,542✔
1575
  int32_t        code = 0;
7,331,542✔
1576
  SVCreateStbReq req = {0};
7,331,542✔
1577
  SDecoder       dc = {0};
7,331,542✔
1578

1579
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
7,331,542✔
1580
  pRsp->code = TSDB_CODE_SUCCESS;
7,331,542✔
1581
  pRsp->pCont = NULL;
7,331,542✔
1582
  pRsp->contLen = 0;
7,331,542✔
1583

1584
  tDecoderInit(&dc, pReq, len);
7,331,203✔
1585

1586
  // decode req
1587
  code = tDecodeSVCreateStbReq(&dc, &req);
7,331,203✔
1588
  if (code) {
7,323,001✔
1589
    tDecoderClear(&dc);
×
1590
    return code;
×
1591
  }
1592

1593
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
7,323,001✔
1594
  if (code) {
7,331,279✔
1595
    pRsp->code = code;
×
1596
    tDecoderClear(&dc);
×
1597
    return code;
×
1598
  }
1599

1600
  tDecoderClear(&dc);
7,331,279✔
1601

1602
  return 0;
7,331,542✔
1603
}
1604

1605
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
890,599✔
1606
  SVDropStbReq req = {0};
890,599✔
1607
  int32_t      rcode = TSDB_CODE_SUCCESS;
891,175✔
1608
  SDecoder     decoder = {0};
891,175✔
1609
  SArray      *tbUidList = NULL;
891,175✔
1610

1611
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
891,175✔
1612
  pRsp->pCont = NULL;
891,175✔
1613
  pRsp->contLen = 0;
891,175✔
1614

1615
  // decode request
1616
  tDecoderInit(&decoder, pReq, len);
891,175✔
1617
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
891,175✔
1618
    rcode = TSDB_CODE_INVALID_MSG;
×
1619
    goto _exit;
×
1620
  }
1621

1622
  STraceId* trace = &(pOriginRpc->info.traceId);
891,175✔
1623

1624
  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,175✔
1625
              trace ? trace->msgId : 0, req.name);
1626

1627
  // process request
1628
  tbUidList = taosArrayInit(8, sizeof(int64_t));
891,175✔
1629
  if (tbUidList == NULL) goto _exit;
891,175✔
1630
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
891,175✔
1631
    rcode = terrno;
1,488✔
1632
    goto _exit;
1,488✔
1633
  }
1634

1635
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
889,687✔
1636
    rcode = terrno;
×
1637
    goto _exit;
×
1638
  }
1639

1640
  // return rsp
1641
_exit:
891,175✔
1642
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
891,175✔
1643
              trace ? trace->msgId : 0, req.name);
1644
  if (tbUidList) taosArrayDestroy(tbUidList);
891,175✔
1645
  pRsp->code = rcode;
891,175✔
1646
  tDecoderClear(&decoder);
891,175✔
1647
  return 0;
891,175✔
1648
}
1649

1650
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,829,672✔
1651
  SVAlterTbReq  vAlterTbReq = {0};
11,829,672✔
1652
  SVAlterTbRsp  vAlterTbRsp = {0};
11,829,672✔
1653
  SDecoder      dc = {0};
11,829,672✔
1654
  int32_t       code = 0;
11,829,672✔
1655
  int32_t       lino = 0;
11,829,672✔
1656
  int32_t       ret;
1657
  SEncoder      ec = {0};
11,829,672✔
1658
  STableMetaRsp vMetaRsp = {0};
11,829,672✔
1659

1660
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
11,829,672✔
1661
  pRsp->pCont = NULL;
11,829,672✔
1662
  pRsp->contLen = 0;
11,829,672✔
1663
  pRsp->code = TSDB_CODE_SUCCESS;
11,829,672✔
1664

1665
  tDecoderInit(&dc, pReq, len);
11,829,672✔
1666

1667
  // decode
1668
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
11,829,672✔
1669
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1670
    tDecoderClear(&dc);
×
1671
    goto _exit;
×
1672
  }
1673

1674
  // process
1675
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
11,829,672✔
1676
    vAlterTbRsp.code = terrno;
682,200✔
1677
    tDecoderClear(&dc);
682,200✔
1678
    goto _exit;
682,200✔
1679
  }
1680
  tDecoderClear(&dc);
11,147,472✔
1681

1682
  if (NULL != vMetaRsp.pSchemas) {
11,147,472✔
1683
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
3,651,995✔
1684
    vAlterTbRsp.pMeta = &vMetaRsp;
3,651,995✔
1685
  }
1686

1687
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL ||
11,147,472✔
1688
      vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
3,685,127✔
1689
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
7,464,973✔
1690
    if (uid == 0) {
7,464,973✔
1691
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1692
             vAlterTbReq.tbName);
1693
      goto _exit;
×
1694
    }
1695

1696
    SArray *tbUids = taosArrayInit(4, sizeof(int64_t));
7,464,973✔
1697
    void   *p = taosArrayPush(tbUids, &uid);
7,464,973✔
1698
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
7,464,973✔
1699

1700
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
7,464,973✔
1701
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
7,464,973✔
1702
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1703
    }
1704

1705
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
7,464,973✔
1706
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
7,464,973✔
1707
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1708
    }
1709

1710
    taosArrayDestroy(tbUids);
7,464,973✔
1711
  }
1712

1713
_exit:
11,829,672✔
1714
  taosArrayDestroy(vAlterTbReq.pMultiTag);
11,829,672✔
1715
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
11,829,672✔
1716
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
11,829,672✔
1717
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
11,829,672✔
1718
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
11,829,672✔
1719
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1720
  }
1721

1722
  tEncoderClear(&ec);
11,829,672✔
1723
  if (vMetaRsp.pSchemas) {
11,829,672✔
1724
    taosMemoryFree(vMetaRsp.pSchemas);
3,651,995✔
1725
    taosMemoryFree(vMetaRsp.pSchemaExt);
3,651,995✔
1726
  }
1727
  if (vMetaRsp.pColRefs) {
11,829,672✔
1728
    taosMemoryFree(vMetaRsp.pColRefs);
260,397✔
1729
  }
1730
  return 0;
11,829,672✔
1731
}
1732

1733
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
1,333,868✔
1734
                                     SRpcMsg *pOriginRpc) {
1735
  SVDropTbBatchReq req = {0};
1,333,868✔
1736
  SVDropTbBatchRsp rsp = {0};
1,333,868✔
1737
  SDecoder         decoder = {0};
1,333,868✔
1738
  SEncoder         encoder = {0};
1,333,868✔
1739
  int32_t          ret;
1740
  SArray          *tbUids = NULL;
1,333,868✔
1741
  SArray          *tbNames = NULL;
1,333,868✔
1742
  int64_t          tss = taosGetTimestampMs();
1,333,868✔
1743

1744
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
1,333,868✔
1745
  pRsp->pCont = NULL;
1,333,868✔
1746
  pRsp->contLen = 0;
1,333,868✔
1747
  pRsp->code = TSDB_CODE_SUCCESS;
1,333,868✔
1748

1749
  // decode req
1750
  tDecoderInit(&decoder, pReq, len);
1,333,868✔
1751
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
1,333,868✔
1752
  if (ret < 0) {
1,333,868✔
1753
    terrno = TSDB_CODE_INVALID_MSG;
×
1754
    pRsp->code = terrno;
×
1755
    goto _exit;
×
1756
  }
1757

1758
  // process req
1759
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
1,333,868✔
1760
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
1,333,868✔
1761
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
1,333,868✔
1762
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
1,333,868✔
1763

1764
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2,722,901✔
1765
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
1,389,033✔
1766
    SVDropTbRsp  dropTbRsp = {0};
1,389,033✔
1767
    tb_uid_t     tbUid = 0;
1,389,033✔
1768

1769
    /* code */
1770
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
1,389,033✔
1771
    if (ret < 0) {
1,389,033✔
1772
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1773
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1774
      } else {
1775
        dropTbRsp.code = terrno;
×
1776
      }
1777
    } else {
1778
      dropTbRsp.code = TSDB_CODE_SUCCESS;
1,389,033✔
1779
    }
1780

1781
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
2,778,066✔
1782
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1783
      pRsp->code = terrno;
×
1784
      goto _exit;
×
1785
    }
1786

1787
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,389,033✔
1788
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1789
      if (str == NULL) {
×
1790
        pRsp->code = terrno;
×
1791
        goto _exit;
×
1792
      }
1793
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1794
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1795
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1796
        pRsp->code = terrno;
×
1797
        goto _exit;
×
1798
      }
1799
    }
1800
  }
1801

1802
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
1,333,868✔
1803
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1804
  }
1805

1806
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
1,333,868✔
1807
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1808

1809
    SName name = {0};
×
1810
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1811
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1812
    }
1813

1814
    SStringBuilder sb = {0};
×
1815
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1816
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1817
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1818
      if (iReq < req.nReqs - 1) {
×
1819
        taosStringBuilderAppendChar(&sb, ',');
×
1820
      }
1821
      taosMemoryFreeClear(*key);
×
1822
    }
1823

1824
    size_t len = 0;
×
1825
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1826

1827
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
1828
      int64_t tse = taosGetTimestampMs();
×
1829
      double  duration = (double)(tse - tss);
×
1830
      duration = duration / 1000;
×
1831
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len, duration, 0);
×
1832
    }
1833

1834
    taosStringBuilderDestroy(&sb);
×
1835
  }
1836

1837
_exit:
1,333,868✔
1838
  taosArrayDestroy(tbUids);
1,333,868✔
1839
  tDecoderClear(&decoder);
1,333,868✔
1840
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
1,333,868✔
1841
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
1,333,868✔
1842
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
1,333,868✔
1843
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
1,333,868✔
1844
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1845
  }
1846
  tEncoderClear(&encoder);
1,333,868✔
1847
  taosArrayDestroy(rsp.pArray);
1,333,868✔
1848
  taosArrayDestroy(tbNames);
1,333,868✔
1849
  return 0;
1,333,868✔
1850
}
1851

1852
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
22,320✔
1853
  int32_t         code = 0, lino = 0;
22,320✔
1854
  SVCreateRsmaReq req = {0};
22,320✔
1855
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
22,320✔
1856
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
22,320✔
1857
_exit:
22,320✔
1858
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
22,320✔
1859
  pRsp->pCont = NULL;
22,320✔
1860
  pRsp->code = code;
22,320✔
1861
  pRsp->contLen = 0;
22,320✔
1862
  tFreeSVCreateRsmaReq(&req);
22,320✔
1863
  return code;
22,320✔
1864
}
1865

1866
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,464✔
1867
  int32_t       code = 0, lino = 0;
4,464✔
1868
  SVDropRsmaReq req = {0};
4,464✔
1869
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
4,464✔
1870
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
4,464✔
1871
_exit:
4,464✔
1872
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
4,464✔
1873
  pRsp->pCont = NULL;
4,464✔
1874
  pRsp->code = code;
4,464✔
1875
  pRsp->contLen = 0;
4,464✔
1876
  return 0;
4,464✔
1877
}
1878

1879
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
11,904✔
1880
  int32_t         code = 0, lino = 0;
11,904✔
1881
  SVAlterRsmaReq req = {0};
11,904✔
1882
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
11,904✔
1883
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
11,904✔
1884
_exit:
11,904✔
1885
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
11,904✔
1886
  pRsp->pCont = NULL;
11,904✔
1887
  pRsp->code = code;
11,904✔
1888
  pRsp->contLen = 0;
11,904✔
1889
  tFreeSVAlterRsmaReq(&req);
11,904✔
1890
  return code;
11,904✔
1891
}
1892

1893
#ifdef BUILD_NO_CALL
1894
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1895
                                              const char *tags) {
1896
  SSubmitBlkIter blkIter = {0};
1897
  STSchema      *pSchema = NULL;
1898
  tb_uid_t       suid = 0;
1899
  STSRow        *row = NULL;
1900
  int32_t        rv = -1;
1901

1902
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1903
  if (blkIter.row == NULL) return 0;
1904

1905
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1906
                                         &pSchema);  // TODO: use the real schema
1907
  if (TSDB_CODE_SUCCESS != code) {
1908
    printf("%s:%d no valid schema\n", tags, __LINE__);
1909
    return code;
1910
  }
1911

1912
  suid = msgIter->suid;
1913
  rv = TD_ROW_SVER(blkIter.row);
1914

1915
  char __tags[128] = {0};
1916
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1917
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1918
    tdSRowPrint(row, pSchema, __tags);
1919
  }
1920

1921
  taosMemoryFreeClear(pSchema);
1922

1923
  return TSDB_CODE_SUCCESS;
1924
}
1925
#endif
1926
typedef struct SSubmitReqConvertCxt {
1927
  SSubmitMsgIter msgIter;
1928
  SSubmitBlk    *pBlock;
1929
  SSubmitBlkIter blkIter;
1930
  STSRow        *pRow;
1931
  STSRowIter     rowIter;
1932
  SSubmitTbData *pTbData;
1933
  STSchema      *pTbSchema;
1934
  SArray        *pColValues;
1935
} SSubmitReqConvertCxt;
1936

1937
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1938
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1939
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1940
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1941
  if (TSDB_CODE_SUCCESS != code) {
×
1942
    return code;
×
1943
  }
1944
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1945

1946
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1947
  if (NULL == pCxt->pTbData) {
×
1948
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1949
    if (NULL == pCxt->pTbData) {
×
1950
      return terrno;
×
1951
    }
1952
  }
1953
  pCxt->pTbData->flags = 0;
×
1954
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1955
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1956
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1957
  pCxt->pTbData->pCreateTbReq = NULL;
×
1958
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1959
  if (NULL == pCxt->pTbData->aRowP) {
×
1960
    return terrno;
×
1961
  }
1962

1963
  taosArrayDestroy(pCxt->pColValues);
×
1964
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1965
  if (NULL == pCxt->pColValues) {
×
1966
    return terrno;
×
1967
  }
1968
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1969
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1970
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1971
      return terrno;
×
1972
    }
1973
  }
1974

1975
  return TSDB_CODE_SUCCESS;
×
1976
}
1977

1978
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1979
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1980
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1981
  taosMemoryFreeClear(pCxt->pTbData);
×
1982
  taosArrayDestroy(pCxt->pColValues);
×
1983
}
×
1984

1985
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1986
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1987
    pColVal->flag = CV_FLAG_NONE;
×
1988
    return TSDB_CODE_SUCCESS;
×
1989
  }
1990

1991
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1992
    pColVal->flag = CV_FLAG_NULL;
×
1993
    return TSDB_CODE_SUCCESS;
×
1994
  }
1995

1996
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1997
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
1998
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
1999
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2000

2001
    } else {
2002
      pColVal->value.nData = varDataLen(pCellVal->val);
×
2003
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
2004
    }
2005
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
2006
    float f = GET_FLOAT_VAL(pCellVal->val);
×
2007
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
2008
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
2009
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
2010
  } else {
2011
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
2012
  }
2013

2014
  pColVal->flag = CV_FLAG_VALUE;
×
2015
  return TSDB_CODE_SUCCESS;
×
2016
}
2017

2018
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
2019
  int32_t code = TSDB_CODE_SUCCESS;
×
2020
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
2021
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
2022
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
2023
    SCellVal  cellVal = {0};
×
2024
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
2025
      break;
×
2026
    }
2027
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2028
  }
2029
  return code;
×
2030
}
2031

2032
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2033
  if (pCxt->msgIter.schemaLen <= 0) {
×
2034
    return TSDB_CODE_SUCCESS;
×
2035
  }
2036

2037
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2038
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2039
    return terrno;
×
2040
  }
2041

2042
  SDecoder decoder = {0};
×
2043
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2044
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2045
  tDecoderClear(&decoder);
×
2046

2047
  return code;
×
2048
}
2049

2050
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2051
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2052
  if (NULL == pReq2->aSubmitTbData) {
×
2053
    return terrno;
×
2054
  }
2055

2056
  SSubmitReqConvertCxt cxt = {0};
×
2057

2058
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2059
  while (TSDB_CODE_SUCCESS == code) {
×
2060
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
2061
    if (TSDB_CODE_SUCCESS == code) {
×
2062
      if (NULL == cxt.pBlock) {
×
2063
        break;
×
2064
      }
2065
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2066
    }
2067
    if (TSDB_CODE_SUCCESS == code) {
×
2068
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2069
    }
2070
    if (TSDB_CODE_SUCCESS == code) {
×
2071
      code = vnodeDecodeCreateTbReq(&cxt);
×
2072
    }
2073
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2074
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2075
      if (TSDB_CODE_SUCCESS == code) {
×
2076
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2077

2078
        SRowBuildScanInfo sinfo = {0};
×
2079
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2080
      }
2081
    }
2082
    if (TSDB_CODE_SUCCESS == code) {
×
2083
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2084
    }
2085
    if (TSDB_CODE_SUCCESS == code) {
×
2086
      taosMemoryFreeClear(cxt.pTbData);
×
2087
    }
2088
  }
2089

2090
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2091
  return code;
×
2092
}
2093

2094
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2095
  int32_t  code = TSDB_CODE_SUCCESS;
×
2096
  char    *pMsg = NULL;
×
2097
  uint32_t msglen = 0;
×
2098
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2099
  if (TSDB_CODE_SUCCESS == code) {
×
2100
    pMsg = taosMemoryMalloc(msglen);
×
2101
    if (NULL == pMsg) {
×
2102
      code = terrno;
×
2103
    }
2104
  }
2105
  if (TSDB_CODE_SUCCESS == code) {
×
2106
    SEncoder encoder;
×
2107
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2108
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2109
    tEncoderClear(&encoder);
×
2110
  }
2111
  if (TSDB_CODE_SUCCESS == code) {
×
2112
    *ppMsg = pMsg;
×
2113
  }
2114
  return code;
×
2115
}
2116

2117
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
60,816✔
2118
  int32_t code = 0;
60,816✔
2119
  int32_t lino = 0;
60,816✔
2120

2121
  SMetaEntry *pEntry = NULL;
60,816✔
2122
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
60,816✔
2123
  if (code) {
60,816✔
2124
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2125
    TSDB_CHECK_CODE(code, lino, _exit);
×
2126
  }
2127
  if (pEntry->type != TSDB_SUPER_TABLE) {
60,816✔
2128
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2129
           __LINE__);
2130
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2131
    TSDB_CHECK_CODE(code, lino, _exit);
×
2132
  }
2133

2134
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
60,816✔
2135
  if (NULL == *ppRsp) {
60,816✔
2136
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2137
  }
2138
  (*ppRsp)->suid = pSubmitTbData->suid;
60,816✔
2139
  (*ppRsp)->tuid = pSubmitTbData->uid;
60,816✔
2140
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
60,816✔
2141
  (*ppRsp)->vgId = pVnode->config.vgId;
60,816✔
2142
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
60,816✔
2143
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
60,816✔
2144
  (*ppRsp)->pSchemas =
60,816✔
2145
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
60,816✔
2146
  if (NULL == (*ppRsp)->pSchemas) {
60,816✔
2147
    taosMemoryFree(*ppRsp);
×
2148
    *ppRsp = NULL;
×
2149
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2150
  }
2151
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
60,816✔
2152
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
60,816✔
2153
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
60,816✔
2154
  if (pEntry->pExtSchemas != NULL) {
60,816✔
2155
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2156
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2157
      taosMemoryFree((*ppRsp)->pSchemas);
×
2158
      taosMemoryFree(*ppRsp);
×
2159
      *ppRsp = NULL;
×
2160
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2161
    }
2162
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2163
  }
2164

2165
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
60,816✔
2166
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
438✔
2167
  }
2168
_exit:
60,816✔
2169
  metaFetchEntryFree(&pEntry);
60,816✔
2170
  if (code != TSDB_CODE_SUCCESS) {
60,816✔
2171
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
438✔
2172
  }
2173
  return code;
60,816✔
2174
}
2175

2176
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
26,264✔
2177
  int32_t code = 0;
26,264✔
2178
  int32_t lino = 0;
26,264✔
2179

2180
  SMetaEntry *pEntry = NULL;
26,264✔
2181
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
26,264✔
2182
  if (code) {
26,264✔
2183
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2184
    TSDB_CHECK_CODE(code, lino, _exit);
×
2185
  }
2186
  if (pEntry->type != TSDB_NORMAL_TABLE) {
26,264✔
2187
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2188
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2189
    TSDB_CHECK_CODE(code, lino, _exit);
×
2190
  }
2191

2192
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
26,264✔
2193
  if (NULL == *ppRsp) {
26,264✔
2194
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2195
  }
2196

2197
  (*ppRsp)->tuid = pEntry->uid;
26,264✔
2198
  (*ppRsp)->vgId = pVnode->config.vgId;
26,264✔
2199
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
26,264✔
2200
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
26,264✔
2201
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
26,264✔
2202
  if (NULL == (*ppRsp)->pSchemas) {
26,264✔
2203
    taosMemoryFree(*ppRsp);
×
2204
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2205
  }
2206
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
26,264✔
2207
  if (pEntry->pExtSchemas != NULL) {
26,264✔
2208
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2209
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2210
      taosMemoryFree((*ppRsp)->pSchemas);
×
2211
      taosMemoryFree(*ppRsp);
×
2212
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2213
    }
2214
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2215
  }
2216

2217
_exit:
26,264✔
2218
  metaFetchEntryFree(&pEntry);
26,264✔
2219
  if (code != TSDB_CODE_SUCCESS) {
26,264✔
2220
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2221
  }
2222
  return code;
26,264✔
2223
}
2224

2225
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
288,192✔
2226
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
288,192✔
2227
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
26,264✔
2228
    if (code) {
26,264✔
2229
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2230
      return code;
×
2231
    }
2232
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
26,264✔
2233
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
261,928✔
2234
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
60,816✔
2235
  }
2236
  return TSDB_CODE_SUCCESS;
201,112✔
2237
}
2238

2239
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
687,246,557✔
2240
                                          int64_t      version,   // version
2241
                                          SSubmitReq2 *pRequest,  // request
2242
                                          SSubmitRsp2 *pResponse  // response
2243
) {
2244
  int32_t code = TSDB_CODE_SUCCESS;
687,246,557✔
2245
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
687,246,557✔
2246
  SArray *newTbUids = NULL;
687,246,877✔
2247

2248
  for (int32_t i = 0; i < numTbData; ++i) {
1,401,790,454✔
2249
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
714,571,641✔
2250

2251
    if (pTbData->pCreateTbReq == NULL) {
714,572,359✔
2252
      continue;
702,885,554✔
2253
    }
2254

2255
    pTbData->uid = pTbData->pCreateTbReq->uid;
11,686,087✔
2256

2257
    // Alloc necessary resources
2258
    if (pResponse->aCreateTbRsp == NULL) {
11,686,087✔
2259
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
8,844,017✔
2260
      if (pResponse->aCreateTbRsp == NULL) {
8,844,017✔
2261
        code = terrno;
×
2262
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2263
               tstrerror(code), version);
2264
        taosArrayDestroy(newTbUids);
×
2265
        return code;
×
2266
      }
2267
    }
2268

2269
    // Do create table
2270
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
11,686,087✔
2271

2272
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
11,686,087✔
2273
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
11,686,087✔
2274
    if (code == TSDB_CODE_SUCCESS) {
11,686,042✔
2275
      // Allocate necessary resources
2276
      if (newTbUids == NULL) {
11,338,659✔
2277
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
8,555,929✔
2278
        if (newTbUids == NULL) {
8,555,929✔
2279
          code = terrno;
×
2280
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2281
                 tstrerror(code), version);
2282
          return code;
×
2283
        }
2284
      }
2285

2286
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
22,677,318✔
2287
        code = terrno;
×
2288
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2289
               tstrerror(code), version);
2290
        taosArrayDestroy(newTbUids);
×
2291
        return code;
×
2292
      }
2293

2294
      if (pCreateTbRsp->pMeta) {
11,338,659✔
2295
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
11,338,085✔
2296
      }
2297
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
347,383✔
2298
      code = terrno = 0;
346,341✔
2299
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
346,386✔
2300

2301
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2302
      if (i == 0) {
346,386✔
2303
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2304
        // only the first one needs to be processed.
2305
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
288,192✔
2306
        if (code) {
288,192✔
2307
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
26,702✔
2308
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2309
          taosArrayDestroy(newTbUids);
26,702✔
2310
          return code;
26,702✔
2311
        }
2312
      }
2313
    } else {
2314
      code = terrno;
1,042✔
2315
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
1,042✔
2316
             tstrerror(code), version);
2317
      taosArrayDestroy(newTbUids);
1,042✔
2318
      return code;
1,042✔
2319
    }
2320
  }
2321

2322
  // Update the affected table uid list
2323
  if (taosArrayGetSize(newTbUids) > 0) {
687,218,813✔
2324
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
8,555,609✔
2325
           (int32_t)taosArrayGetSize(newTbUids));
2326
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
8,555,929✔
2327
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2328
    }
2329
  }
2330

2331
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
687,218,813✔
2332

2333
  taosArrayDestroy(newTbUids);
687,218,813✔
2334
  return code;
687,218,769✔
2335
}
2336

2337
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
438✔
2338
                                     SSubmitTbData *pTbData, int32_t numTbData) {
2339
  int32_t code = 0;
438✔
2340
  int32_t lino = 0;
438✔
2341
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
438✔
2342
    return;
×
2343
  }
2344
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
438✔
2345
                                  // so table info has exitst and we do not need to add again.
2346
    return;
438✔
2347
  }
2348
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
2349
  if (pResponse->aCreateTbRsp == NULL) {
×
2350
    code = terrno;
×
2351
    TSDB_CHECK_CODE(code, lino, _exit);
×
2352
  }
2353
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
2354
  if (pCreateTbRsp == NULL) {
×
2355
    code = terrno;
×
2356
    TSDB_CHECK_CODE(code, lino, _exit);
×
2357
  }
2358
  if (pTbData->suid == 0) {
×
2359
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2360
    if (code) {
×
2361
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2362
    }
2363
  } else {
2364
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2365
  }
2366

2367
  TSDB_CHECK_CODE(code, lino, _exit);
×
2368
_exit:
×
2369
  if (code != TSDB_CODE_SUCCESS) {
×
2370
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2371
  }
2372
  return;
×
2373
}
2374

2375
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
687,217,333✔
2376
  int32_t code = TSDB_CODE_SUCCESS;
687,217,333✔
2377
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
687,217,333✔
2378
  int8_t  hasBlob = 0;
687,216,593✔
2379

2380
  // Scan submit data
2381
  for (int32_t i = 0; i < numTbData; ++i) {
1,401,754,310✔
2382
    SMetaInfo      info = {0};
714,541,295✔
2383
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
714,541,246✔
2384

2385
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
714,541,729✔
2386
      hasBlob = 1;
21,713✔
2387
    }
2388
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
714,543,275✔
2389
      continue;  // skip column data format
3,357,775✔
2390
    }
2391
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
711,185,332✔
2392
      continue;  // skip only crate table request
2,381,080✔
2393
    }
2394

2395
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
708,804,710✔
2396
    if (code) {
708,802,217✔
2397
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
2,481✔
2398
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
2,481✔
2399
            __LINE__, tstrerror(code), version, pTbData->uid);
2400
      return code;
2,481✔
2401
    }
2402

2403
    if (info.suid != pTbData->suid) {
708,799,736✔
2404
      code = TSDB_CODE_INVALID_MSG;
×
2405
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2406
             " info.suid:%" PRId64,
2407
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2408
             info.suid);
2409
      return code;
×
2410
    }
2411

2412
    if (info.suid) {
708,798,840✔
2413
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
521,973,368✔
2414
      if (code) {
521,977,589✔
2415
        code = TSDB_CODE_INTERNAL_ERROR;
×
2416
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2417
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2418
        return code;
×
2419
      }
2420
    }
2421

2422
    if (pTbData->sver != info.skmVer) {
708,803,061✔
2423
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
438✔
2424
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
438✔
2425
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
438✔
2426
             " sver:%d"
2427
             " info.skmVer:%d",
2428
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2429
             info.skmVer);
2430
      return code;
438✔
2431
    }
2432
  }
2433

2434
  // Do write data
2435
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
687,213,015✔
2436

2437
  for (int32_t i = 0; i < numTbData; ++i) {
1,401,716,658✔
2438
    int32_t        affectedRows = 0;
714,534,749✔
2439
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
714,533,926✔
2440

2441
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
714,532,054✔
2442
      continue;
2,381,080✔
2443
    }
2444

2445
    if (hasBlob) {
712,150,349✔
2446
      code = vnodeSubmitBlobData(pVnode, pTbData);
21,713✔
2447
      if (code) {
21,713✔
2448
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2449
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2450
        return code;
×
2451
      }
2452
    }
2453

2454
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
712,150,349✔
2455
    if (code) {
712,128,715✔
2456
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2457
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2458
      return code;
×
2459
    }
2460

2461
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
712,128,715✔
2462
    if (code) {
712,106,052✔
2463
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2464
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2465
      return code;
×
2466
    }
2467
    pResponse->affectedRows += affectedRows;
712,106,052✔
2468
  }
2469

2470
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
687,181,909✔
2471
         pResponse->affectedRows);
2472
  return code;
687,216,011✔
2473
}
2474

2475
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
3,357,135✔
2476
  int32_t code = 0;
3,357,135✔
2477

2478
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
3,357,135✔
2479
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
3,356,490✔
2480

2481
  if (numCols <= 0) {
3,355,855✔
2482
    code = TSDB_CODE_INVALID_MSG;
×
2483
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2484
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2485
    return code;
×
2486
  }
2487

2488
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
3,355,855✔
2489
      aColData[0].nVal <= 0) {
3,356,815✔
2490
    code = TSDB_CODE_INVALID_MSG;
5✔
2491
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
5✔
2492
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2493
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2494
           aColData[0].type, aColData[0].nVal);
2495
    return code;
×
2496
  }
2497

2498
  for (int32_t i = 1; i < numCols; ++i) {
10,588,140✔
2499
    if (aColData[i].nVal != aColData[0].nVal) {
7,233,245✔
2500
      code = TSDB_CODE_INVALID_MSG;
×
2501
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2502
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2503
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2504
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2505
      return code;
×
2506
    }
2507
  }
2508

2509
  SRowKey *pLastKey = NULL;
3,354,895✔
2510
  SRowKey  lastKey = {0};
3,354,895✔
2511
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
2,147,483,647✔
2512
    SRowKey key = {0};
2,147,483,647✔
2513

2514
    tColDataArrGetRowKey(aColData, numCols, i, &key);
2,147,483,647✔
2515

2516
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2517
      code = TSDB_CODE_INVALID_MSG;
×
2518
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2519
             " is out of range [%" PRId64 ", %" PRId64 "]",
2520
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2521
             minKey, maxKey);
2522
      return code;
×
2523
    }
2524

2525
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2526
      code = TSDB_CODE_INVALID_MSG;
×
2527
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2528
             " is not in order, lastKey:%" PRId64,
2529
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2530
             pLastKey->ts);
2531
      return code;
×
2532
    } else if (pLastKey == NULL) {
2,147,483,647✔
2533
      pLastKey = &lastKey;
3,356,815✔
2534
    }
2535

2536
    *pLastKey = key;
2,147,483,647✔
2537
  }
2538

2539
  return code;
3,357,775✔
2540
}
2541

2542
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
711,192,347✔
2543
  int32_t code = 0;
711,192,347✔
2544

2545
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
711,192,347✔
2546
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
711,191,142✔
2547

2548
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
711,185,499✔
2549
    code = TSDB_CODE_INVALID_MSG;
×
2550
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2551
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2552
    return code;
×
2553
  }
2554

2555
  SRowKey *pLastKey = NULL;
711,185,499✔
2556
  SRowKey  lastKey = {0};
711,185,499✔
2557
  for (int32_t i = 0; i < numRows; ++i) {
2,147,483,647✔
2558
    SRow *pRow = aRow[i];
2,147,483,647✔
2559
    if (pRow->sver != pTbData->sver) {
2,147,483,647✔
2560
      code = TSDB_CODE_INVALID_MSG;
×
2561
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2562
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2563
             pTbData->sver);
2564
      return code;
×
2565
    }
2566

2567
    SRowKey key = {0};
2,147,483,647✔
2568
    tRowGetKey(pRow, &key);
2,147,483,647✔
2569
    if (key.ts < minKey || key.ts > maxKey) {
2,147,483,647✔
2570
      code = TSDB_CODE_INVALID_MSG;
2,907,438✔
2571
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
2,907,438✔
2572
             " is out of range [%" PRId64 ", %" PRId64 "]",
2573
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2574
             minKey, maxKey);
2575
      return code;
×
2576
    }
2577

2578
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
2,147,483,647✔
2579
      code = TSDB_CODE_INVALID_MSG;
×
2580
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2581
             " is not in order, lastKey:%" PRId64,
2582
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2583
             pLastKey->ts);
2584
      return code;
×
2585
    } else if (pLastKey == NULL) {
2,147,483,647✔
2586
      pLastKey = &lastKey;
708,786,856✔
2587
    }
2588

2589
    *pLastKey = key;
2,147,483,647✔
2590
  }
2591

2592
  return code;
708,755,353✔
2593
}
2594

2595
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
687,232,331✔
2596
  int32_t code = TSDB_CODE_SUCCESS;
687,232,331✔
2597
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
687,232,331✔
2598

2599
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
687,225,943✔
2600
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
687,231,648✔
2601
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
687,225,622✔
2602
  for (int32_t i = 0; i < numTbData; i++) {
1,401,762,398✔
2603
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
714,551,409✔
2604

2605
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
714,548,219✔
2606
      code = TSDB_CODE_INVALID_MSG;
×
2607
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2608
             tstrerror(code), version);
2609
      return code;
×
2610
    }
2611

2612
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
714,549,795✔
2613
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
3,356,490✔
2614
      if (code) {
3,357,775✔
2615
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2616
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2617
        return code;
×
2618
      }
2619
    } else {
2620
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
711,187,593✔
2621
      if (code) {
711,159,670✔
UNCOV
2622
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2623
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2624
        return code;
×
2625
      }
2626
    }
2627
  }
2628

2629
  return code;
687,210,989✔
2630
}
2631

2632
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
687,242,676✔
2633
                                     SRpcMsg *pOriginalMsg) {
2634
  int32_t code = 0;
687,242,676✔
2635
  int32_t lino = 0;
687,242,676✔
2636
  terrno = 0;
687,242,676✔
2637

2638
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
687,245,296✔
2639
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
687,244,976✔
2640
  int32_t      ret;
2641
  SEncoder     ec = {0};
687,242,321✔
2642

2643
  pRsp->code = TSDB_CODE_SUCCESS;
687,244,475✔
2644

2645
  void           *pAllocMsg = NULL;
687,246,044✔
2646
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
687,246,044✔
2647
  SDecoder        dc = {0};
687,246,044✔
2648
  if (0 == taosHton64(pMsg->version)) {
687,244,756✔
2649
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
2650
    if (TSDB_CODE_SUCCESS == code) {
×
2651
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
2652
    }
2653
    if (TSDB_CODE_SUCCESS == code) {
×
2654
      pAllocMsg = pReq;
×
2655
    }
2656
    TSDB_CHECK_CODE(code, lino, _exit);
×
2657
  } else {
2658
    // decode
2659
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
687,242,990✔
2660
    len -= sizeof(SSubmitReq2Msg);
687,243,217✔
2661

2662
    tDecoderInit(&dc, pReq, len);
687,243,217✔
2663
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
687,244,823✔
2664
      code = TSDB_CODE_INVALID_MSG;
×
2665
      TSDB_CHECK_CODE(code, lino, _exit);
×
2666
    }
2667
  }
2668

2669
  // Scan the request
2670
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
687,230,176✔
2671
  if (code) {
687,207,674✔
2672
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2673
           tstrerror(code), ver);
2674
    TSDB_CHECK_CODE(code, lino, _exit);
×
2675
  }
2676

2677
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
687,207,674✔
2678
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2679

2680
  // Handle auto create table
2681
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
687,240,868✔
2682
  if (code) {
687,246,513✔
2683
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
27,744✔
2684
           tstrerror(code), ver);
2685
    TSDB_CHECK_CODE(code, lino, _exit);
27,744✔
2686
  }
2687

2688
  // Handle data write
2689
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
687,218,769✔
2690
  if (code) {
687,219,033✔
2691
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
2,919✔
2692
           tstrerror(code), ver);
2693
    TSDB_CHECK_CODE(code, lino, _exit);
2,919✔
2694
  }
2695

2696
_exit:
687,246,609✔
2697
  // message
2698
  pRsp->code = code;
687,247,275✔
2699
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
687,247,275✔
2700
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
687,246,950✔
2701
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
687,246,901✔
2702
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
687,246,746✔
2703
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2704
  }
2705
  tEncoderClear(&ec);
687,243,697✔
2706

2707
  // update statistics
2708
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
687,242,584✔
2709
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
687,243,662✔
2710
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
687,244,874✔
2711

2712
  // update metrics
2713
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
687,244,015✔
2714
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
687,238,358✔
2715
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
687,239,021✔
2716

2717
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
687,236,773✔
2718
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
2719
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2720
                                   pVnode->monitor.strClusterId,
×
2721
                                   pVnode->monitor.strDnodeId,
×
2722
                                   tsLocalEp,
2723
                                   pVnode->monitor.strVgId,
×
2724
                                   RPC_MSG_USER(pOriginalMsg),
×
2725
                                   "Success"};
2726
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2727
  }
2728

2729
  if (code == 0) {
687,238,008✔
2730
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
687,207,345✔
2731
  }
2732

2733
  // clear
2734
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
687,241,094✔
2735
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
687,227,869✔
2736

2737
  if (code) {
687,214,013✔
2738
    terrno = code;
30,663✔
2739
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
30,663✔
2740
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
26,702✔
2741
            tstrerror(code), lino, ver);
2742

2743
    } else {
2744
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
3,961✔
2745
             tstrerror(code), lino, ver);
2746
    }
2747
  }
2748

2749
  taosMemoryFree(pAllocMsg);
687,216,623✔
2750
  tDecoderClear(&dc);
687,218,422✔
2751

2752
  return code;
687,242,881✔
2753
}
2754

2755
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
24,328✔
2756
  int32_t code = TSDB_CODE_SUCCESS;
24,328✔
2757

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

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

2764
  return code;
24,328✔
2765
}
2766

2767
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
3,626,006✔
2768
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
3,626,006✔
2769
  int32_t code = TSDB_CODE_SUCCESS;
3,626,006✔
2770
  if (!pVnode->config.hashChange) {
3,626,006✔
2771
    goto _exit;
3,601,678✔
2772
  }
2773

2774
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
24,328✔
2775
  if (code < 0) {
24,328✔
2776
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2777
    goto _exit;
×
2778
  }
2779
  pVnode->config.hashChange = false;
24,328✔
2780

2781
_exit:
3,626,006✔
2782
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
3,626,006✔
2783
  pRsp->code = code;
3,626,006✔
2784
  pRsp->pCont = NULL;
3,626,006✔
2785
  pRsp->contLen = 0;
3,626,006✔
2786

2787
  return code;
3,626,006✔
2788
}
2789

2790
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2791
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2792

2793
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
316,188✔
2794
  bool walChanged = false;
316,188✔
2795
  bool tsdbChanged = false;
316,188✔
2796

2797
  SAlterVnodeConfigReq req = {0};
316,188✔
2798
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
316,932✔
2799
    terrno = TSDB_CODE_INVALID_MSG;
×
2800
    return TSDB_CODE_INVALID_MSG;
×
2801
  }
2802

2803
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
316,622✔
2804
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
2805
        "ssCompact:%d fsync:%d level:%d "
2806
        "walRetentionPeriod:%d walRetentionSize:%d",
2807
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2808
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2809
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2810
        req.walRetentionSize);
2811

2812
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
316,932✔
2813
    pVnode->config.cacheLastSize = req.cacheLastSize;
8,294✔
2814
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
8,294✔
2815
  }
2816

2817
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
316,932✔
2818
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
8,872✔
2819
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2820
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
8,872✔
2821
  }
2822

2823
  if (pVnode->config.szCache != req.pages) {
316,932✔
2824
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
4,464✔
2825
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2826
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2827
      return terrno;
×
2828
    } else {
2829
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
4,464✔
2830
      pVnode->config.szCache = req.pages;
4,464✔
2831
    }
2832
  }
2833

2834
  if (pVnode->config.cacheLast != req.cacheLast) {
316,932✔
2835
    pVnode->config.cacheLast = req.cacheLast;
88,783✔
2836
  }
2837

2838
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
316,932✔
2839
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
51,616✔
2840
    walChanged = true;
51,616✔
2841
  }
2842

2843
  if (pVnode->config.walCfg.level != req.walLevel) {
316,932✔
2844
    if (pVnode->config.walCfg.level == 0) {
38,001✔
2845
      pVnode->config.walCfg.clearFiles = 1;
2,153✔
2846
    }
2847
    pVnode->config.walCfg.level = req.walLevel;
38,001✔
2848
    walChanged = true;
38,001✔
2849
  }
2850

2851
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
316,932✔
2852
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
47,043✔
2853
    walChanged = true;
47,043✔
2854
  }
2855

2856
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
316,932✔
2857
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
1,432✔
2858
    walChanged = true;
1,432✔
2859
  }
2860

2861
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
316,932✔
2862
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
41,186✔
2863
    tsdbChanged = true;
41,186✔
2864
  }
2865

2866
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
316,932✔
2867
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
39,698✔
2868
    tsdbChanged = true;
39,698✔
2869
  }
2870

2871
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
316,622✔
2872
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
33,846✔
2873
    tsdbChanged = true;
33,846✔
2874
  }
2875

2876
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
316,622✔
2877
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
7,224✔
2878
    tsdbChanged = true;
7,224✔
2879
  }
2880

2881
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
316,622✔
2882
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2,485✔
2883
      pVnode->config.sttTrigger = req.sttTrigger;
1,432✔
2884
    } else {
2885
      vnodeAWait(&pVnode->commitTask);
1,053✔
2886

2887
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
1,053✔
2888
      if (ret != 0) {
1,053✔
2889
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2890
      }
2891

2892
      pVnode->config.sttTrigger = req.sttTrigger;
1,053✔
2893
      tsdbEnableBgTask(pVnode->pTsdb);
1,053✔
2894
    }
2895
  }
2896

2897
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
316,622✔
2898
    pVnode->config.tsdbCfg.minRows = req.minRows;
1,432✔
2899
  }
2900

2901
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
316,932✔
2902
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
1,432✔
2903
  }
2904
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
316,932✔
2905
    pVnode->config.ssCompact = req.ssCompact;
1,432✔
2906
  }
2907

2908
  if (walChanged) {
317,242✔
2909
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
135,212✔
2910
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2911
    }
2912
  }
2913

2914
  if (tsdbChanged) {
316,932✔
2915
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
55,796✔
2916
  }
2917

2918
  return 0;
316,932✔
2919
}
2920

2921
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2922
  SBatchDeleteReq deleteReq;
×
2923
  SDecoder        decoder;
×
2924
  tDecoderInit(&decoder, pReq, len);
×
2925
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2926
    tDecoderClear(&decoder);
×
2927
    return terrno = TSDB_CODE_INVALID_MSG;
×
2928
  }
2929

2930
  SMetaReader mr = {0};
×
2931
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2932
  STsdb *pTsdb = pVnode->pTsdb;
×
2933

2934
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2935
  for (int32_t i = 0; i < sz; i++) {
×
2936
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2937
    char             *name = pOneReq->tbname;
×
2938
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2939
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2940
      continue;
×
2941
    }
2942

2943
    int64_t uid = mr.me.uid;
×
2944

2945
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2946
    if (code < 0) {
×
2947
      terrno = code;
×
2948
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2949
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2950
    }
2951

2952
    if (deleteReq.level == 0) {
×
2953
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2954
      if (code < 0) {
×
2955
        terrno = code;
×
2956
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2957
               ", end ts:%" PRId64,
2958
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2959
      }
2960
    }
2961
    tDecoderClear(&mr.coder);
×
2962
  }
2963
  metaReaderClear(&mr);
×
2964
  taosArrayDestroy(deleteReq.deleteReqs);
×
2965
  return 0;
×
2966
}
2967

2968
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,062,615✔
2969
                                     SRpcMsg *pOriginalMsg) {
2970
  int32_t     code = 0;
2,062,615✔
2971
  SDecoder   *pCoder = &(SDecoder){0};
2,062,615✔
2972
  SDeleteRes *pRes = &(SDeleteRes){0};
2,062,615✔
2973

2974
  pRsp->msgType = TDMT_VND_DELETE_RSP;
2,062,615✔
2975
  pRsp->pCont = NULL;
2,062,615✔
2976
  pRsp->contLen = 0;
2,062,615✔
2977
  pRsp->code = TSDB_CODE_SUCCESS;
2,062,615✔
2978

2979
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
2,062,615✔
2980
  if (pRes->uidList == NULL) {
2,062,615✔
2981
    code = terrno;
×
2982
    goto _err;
×
2983
  }
2984

2985
  tDecoderInit(pCoder, pReq, len);
2,062,615✔
2986
  code = tDecodeDeleteRes(pCoder, pRes);
2,062,615✔
2987
  if (code) goto _err;
2,062,615✔
2988

2989
  if (pRes->affectedRows > 0) {
2,062,615✔
2990
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
3,673,353✔
2991
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
2,096,948✔
2992
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
2,096,948✔
2993
      if (code) goto _err;
2,096,948✔
2994
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
2,096,948✔
2995
      if (code) goto _err;
2,096,948✔
2996
    }
2997
  }
2998

2999
  tDecoderClear(pCoder);
2,062,615✔
3000
  taosArrayDestroy(pRes->uidList);
2,062,615✔
3001

3002
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2,062,615✔
3003
  int32_t     ret = 0;
2,062,615✔
3004
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
2,062,615✔
3005
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,062,615✔
3006
  SEncoder ec = {0};
2,062,615✔
3007
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2,062,615✔
3008
  code = tEncodeSVDeleteRsp(&ec, &rsp);
2,062,615✔
3009
  if (code) goto _err;
2,062,615✔
3010
  tEncoderClear(&ec);
2,062,615✔
3011
  return code;
2,062,615✔
3012

3013
_err:
×
3014
  /*
3015
  if(code == TSDB_CODE_SUCCESS){
3016
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3017
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3018
                                        RPC_MSG_USER(pOriginalMsg), "Success"};
3019
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3020
  }
3021
  else{
3022
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3023
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3024
                                        RPC_MSG_USER(pOriginalMsg), "Failed"};
3025
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3026
  }
3027
  */
3028

3029
  return code;
×
3030
}
3031
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
4,468✔
3032
  SVCreateStbReq req = {0};
4,468✔
3033
  SDecoder       dc = {0};
4,468✔
3034
  int32_t        code = 0;
4,468✔
3035

3036
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
4,468✔
3037
  pRsp->code = TSDB_CODE_SUCCESS;
4,468✔
3038
  pRsp->pCont = NULL;
4,468✔
3039
  pRsp->contLen = 0;
4,468✔
3040

3041
  tDecoderInit(&dc, pReq, len);
4,468✔
3042
  // decode req
3043
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
4,468✔
3044
    tDecoderClear(&dc);
×
3045
    return terrno = TSDB_CODE_INVALID_MSG;
×
3046
  }
3047

3048
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
4,468✔
3049
  if (code) {
4,468✔
3050
    pRsp->code = code;
×
3051
    goto _err;
×
3052
  }
3053
  tDecoderClear(&dc);
4,468✔
3054
  return 0;
4,468✔
3055

3056
_err:
×
3057
  tDecoderClear(&dc);
×
3058
  return code;
×
3059
}
3060
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2,960✔
3061
  SDropIndexReq req = {0};
2,960✔
3062
  int32_t       code = 0;
2,960✔
3063
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2,960✔
3064
  pRsp->code = TSDB_CODE_SUCCESS;
2,960✔
3065
  pRsp->pCont = NULL;
2,960✔
3066
  pRsp->contLen = 0;
2,960✔
3067

3068
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2,960✔
3069
    pRsp->code = code;
×
3070
    return code;
×
3071
  }
3072

3073
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
2,960✔
3074
  if (code) {
2,960✔
3075
    pRsp->code = code;
×
3076
    return code;
×
3077
  }
3078
  return TSDB_CODE_SUCCESS;
2,960✔
3079
}
3080

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

3083
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
65,167✔
3084
  if (!pVnode->restored) {
65,167✔
3085
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
1,647✔
3086
    return 0;
1,647✔
3087
  }
3088
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
63,520✔
3089
}
3090

3091
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3092
  if (syncCheckMember(pVnode->sync) != 0) {
×
3093
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3094
  }
3095

3096
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3097
  pRsp->code = TSDB_CODE_SUCCESS;
×
3098
  pRsp->pCont = NULL;
×
3099
  pRsp->contLen = 0;
×
3100

3101
  return 0;
×
3102
}
3103

3104
static int32_t vnodeCheckState(SVnode *pVnode) {
59,201✔
3105
  SSyncState syncState = syncGetState(pVnode->sync);
59,201✔
3106
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
59,201✔
3107
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
19,631✔
3108
  }
3109
  return 0;
39,570✔
3110
}
3111

3112
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
39,570✔
3113
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
39,570✔
3114
  if (vnodeGetArbToken(pVnode, token) != 0) {
39,570✔
3115
    return terrno = TSDB_CODE_NOT_FOUND;
×
3116
  }
3117

3118
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
39,570✔
3119
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
37,604✔
3120
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
35,300✔
3121
  }
3122

3123
  terrno = TSDB_CODE_SUCCESS;
4,270✔
3124
  return 0;
4,270✔
3125
}
3126

3127
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
4,911✔
3128
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
4,911✔
3129
  if (code != 0) {
4,911✔
3130
    return code;
2,776✔
3131
  }
3132

3133
  return syncCheckSynced(pVnode->sync);
2,135✔
3134
}
3135

3136
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
12,602✔
3137
  int32_t code = 0;
12,602✔
3138

3139
  if ((code = vnodeCheckState(pVnode)) != 0) {
12,602✔
3140
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
7,691✔
3141
    return 0;
7,691✔
3142
  }
3143

3144
  SVArbCheckSyncReq syncReq = {0};
4,911✔
3145

3146
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
4,911✔
3147
  if (code) {
4,911✔
3148
    return code;
×
3149
  }
3150

3151
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
4,911✔
3152
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3153
        "arbTerm:%" PRId64,
3154
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3155
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3156

3157
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
4,911✔
3158
  pRsp->code = TSDB_CODE_SUCCESS;
4,911✔
3159
  pRsp->pCont = NULL;
4,911✔
3160
  pRsp->contLen = 0;
4,911✔
3161

3162
  SVArbCheckSyncRsp syncRsp = {0};
4,911✔
3163
  syncRsp.arbToken = syncReq.arbToken;
4,911✔
3164
  syncRsp.member0Token = syncReq.member0Token;
4,911✔
3165
  syncRsp.member1Token = syncReq.member1Token;
4,911✔
3166
  syncRsp.vgId = TD_VID(pVnode);
4,911✔
3167

3168
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
4,911✔
3169
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
2,776✔
3170
  }
3171

3172
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
4,911✔
3173
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3174
    goto _OVER;
×
3175
  }
3176

3177
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
4,911✔
3178
  if (contLen <= 0) {
4,911✔
3179
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3180
    code = -1;
×
3181
    goto _OVER;
×
3182
  }
3183
  void *pHead = rpcMallocCont(contLen);
4,911✔
3184
  if (!pHead) {
4,911✔
3185
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3186
    code = -1;
×
3187
    goto _OVER;
×
3188
  }
3189

3190
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
4,911✔
3191
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3192
    rpcFreeCont(pHead);
×
3193
    code = -1;
×
3194
    goto _OVER;
×
3195
  }
3196

3197
  pRsp->pCont = pHead;
4,911✔
3198
  pRsp->contLen = contLen;
4,911✔
3199

3200
  vInfo(
4,911✔
3201
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3202
      "member1Token:%s",
3203
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3204

3205
  code = TSDB_CODE_SUCCESS;
4,911✔
3206

3207
_OVER:
4,911✔
3208
  if (code != 0) {
4,911✔
3209
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3210
           tstrerror(code));
3211
  }
3212
  tFreeSVArbCheckSyncReq(&syncReq);
4,911✔
3213
  return code;
4,911✔
3214
}
3215

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