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

taosdata / TDengine / #3819

01 Apr 2025 09:27AM UTC coverage: 34.076% (+0.01%) from 34.065%
#3819

push

travis-ci

happyguoxy
test:alter gcda dir

148544 of 599532 branches covered (24.78%)

Branch coverage included in aggregate %.

222541 of 489451 relevant lines covered (45.47%)

763329.1 hits per line

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

42.71
/source/dnode/vnode/src/vnd/vnodeSvr.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "audit.h"
17
#include "cos.h"
18
#include "monitor.h"
19
#include "tencode.h"
20
#include "tglobal.h"
21
#include "tmsg.h"
22
#include "tstrbuild.h"
23
#include "vnd.h"
24
#include "vnode.h"
25
#include "vnodeInt.h"
26

27
extern taos_counter_t *tsInsertCounter;
28

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

56
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token);
57
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
58
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
59

60
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
61
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
62

63
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
3,329✔
64
  int32_t code = 0;
3,329✔
65
  int32_t lino = 0;
3,329✔
66

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

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

78
  // name
79
  char *name = NULL;
3,328✔
80
  if (tDecodeCStr(pCoder, &name) < 0) {
3,328!
81
    code = TSDB_CODE_INVALID_MSG;
×
82
    TSDB_CHECK_CODE(code, lino, _exit);
×
83
  }
84

85
  // uid
86
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
3,328✔
87
  if (uid == 0) {
3,328!
88
    uid = tGenIdPI64();
3,328✔
89
  }
90
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
3,329✔
91

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

95
  tEndDecode(pCoder);
3,329✔
96

97
_exit:
3,328✔
98
  if (code) {
3,328!
99
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
100
  } else {
101
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
3,328✔
102
    if (pUid) *pUid = uid;
3,328✔
103
  }
104
  return code;
3,328✔
105
}
106
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,668✔
107
  int32_t code = 0;
2,668✔
108
  int32_t lino = 0;
2,668✔
109

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

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

120
  if (tDecodeI32v(&dc, &nReqs) < 0) {
2,668!
121
    code = TSDB_CODE_INVALID_MSG;
×
122
    TSDB_CHECK_CODE(code, lino, _exit);
×
123
  }
124
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
5,986✔
125
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
3,319✔
126
    TSDB_CHECK_CODE(code, lino, _exit);
3,318!
127
  }
128

129
  tEndDecode(&dc);
2,667✔
130

131
_exit:
2,668✔
132
  tDecoderClear(&dc);
2,668✔
133
  if (code) {
2,668!
134
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
135
           tstrerror(code), TMSG_INFO(pMsg->msgType));
136
  }
137
  return code;
2,668✔
138
}
139

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

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

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

157
  code = 0;
21✔
158

159
_exit:
21✔
160
  tDecoderClear(&dc);
21✔
161
  if (code) {
21!
162
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
163
  } else {
164
    vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
21!
165
           ctimeMs);
166
  }
167
  return code;
21✔
168
}
169

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

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

177
  SArray *tbUids = NULL;
511✔
178
  int64_t timestampMs = 0;
511✔
179

180
  SVDropTtlTableReq ttlReq = {0};
511✔
181
  if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
511!
182
    code = TSDB_CODE_INVALID_MSG;
×
183
    TSDB_CHECK_CODE(code, lino, _exit);
×
184
  }
185

186
  {  // find expired uids
187
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
511✔
188
    if (tbUids == NULL) {
511!
189
      code = terrno;
×
190
      TSDB_CHECK_CODE(code, lino, _exit);
×
191
    }
192

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

200
    ttlReq.nUids = taosArrayGetSize(tbUids);
511✔
201
    ttlReq.pTbUids = tbUids;
511✔
202
  }
203

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

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

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

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

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

230
  code = 0;
×
231

232
_exit:
511✔
233
  taosArrayDestroy(tbUids);
511✔
234

235
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
511!
236
    vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
237
           tstrerror(code), TMSG_INFO(pMsg->msgType));
238
  } else {
239
    vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
511✔
240
  }
241

242
  return code;
511✔
243
}
244

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

250
  if (tStartDecode(pCoder) < 0) {
630,275!
251
    code = TSDB_CODE_INVALID_MSG;
×
252
    TSDB_CHECK_CODE(code, lino, _exit);
×
253
  }
254

255
  SSubmitTbData submitTbData;
256
  uint8_t       version;
257
  if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
634,143!
258
    code = TSDB_CODE_INVALID_MSG;
×
259
    TSDB_CHECK_CODE(code, lino, _exit);
×
260
  }
261
  version = (submitTbData.flags >> 8) & 0xff;
634,143✔
262
  submitTbData.flags = submitTbData.flags & 0xff;
634,143✔
263

264
  int64_t uid;
265
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
634,143✔
266
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
10✔
267
    TSDB_CHECK_CODE(code, lino, _exit);
10!
268
  }
269

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

276
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
631,711✔
277
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
10✔
278
    pCoder->pos += sizeof(int64_t);
10✔
279
  } else {
280
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
630,345!
281
      code = TSDB_CODE_INVALID_MSG;
×
282
      TSDB_CHECK_CODE(code, lino, _exit);
×
283
    }
284
  }
285

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

291
  // scan and check
292
  TSKEY now = btimeMs;
629,611✔
293
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
629,611✔
294
    now *= 1000;
2,000✔
295
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
627,611✔
296
    now *= 1000000;
2,000✔
297
  }
298

299
  int32_t keep = pVnode->config.tsdbCfg.keep2;
629,611✔
300
  /*
301
  int32_t nlevel = tfsGetLevel(pVnode->pTfs);
302
  if (nlevel > 1 && tsS3Enabled) {
303
    if (nlevel == 3) {
304
      keep = pVnode->config.tsdbCfg.keep1;
305
    } else if (nlevel == 2) {
306
      keep = pVnode->config.tsdbCfg.keep0;
307
    }
308
  }
309
  */
310

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

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

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

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

339
    for (uint64_t i = 1; i < nColData; i++) {
×
340
      code = tDecodeColData(version, pCoder, &colData);
×
341
      if (code) {
×
342
        code = TSDB_CODE_INVALID_MSG;
×
343
        TSDB_CHECK_CODE(code, lino, _exit);
×
344
      }
345
    }
346
  } else {
347
    uint64_t nRow;
348
    if (tDecodeU64v(pCoder, &nRow) < 0) {
631,186!
349
      code = TSDB_CODE_INVALID_MSG;
×
350
      TSDB_CHECK_CODE(code, lino, _exit);
×
351
    }
352

353
    for (int32_t iRow = 0; iRow < nRow; ++iRow) {
7,656,728✔
354
      SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
7,025,542✔
355
      pCoder->pos += pRow->len;
7,025,542✔
356
#ifndef NO_UNALIGNED_ACCESS
357
      if (pRow->ts < minKey || pRow->ts > maxKey) {
7,025,542!
358
#else
359
      TSKEY ts = taosGetInt64Aligned(&pRow->ts);
360
      if (ts < minKey || ts > maxKey) {
361
#endif
362
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
778✔
363
        TSDB_CHECK_CODE(code, lino, _exit);
778!
364
      }
365
    }
366
  }
367

368
  if (!tDecodeIsEnd(pCoder)) {
631,186!
369
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
632,091✔
370
    pCoder->pos += sizeof(int64_t);
632,091✔
371
  }
372

373
  tEndDecode(pCoder);
631,186✔
374

375
_exit:
630,185✔
376
  if (code) {
630,185!
377
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
378
  }
379
  return code;
630,421✔
380
}
381
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
63,797✔
382
  int32_t code = 0;
63,797✔
383
  int32_t lino = 0;
63,797✔
384

385
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
63,797!
386
    return TSDB_CODE_MSG_PREPROCESSED;
×
387
  }
388

389
  SDecoder *pCoder = &(SDecoder){0};
63,797✔
390

391
  if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
63,797!
392
    code = TSDB_CODE_INVALID_MSG;
×
393
    TSDB_CHECK_CODE(code, lino, _exit);
×
394
  }
395

396
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
63,797✔
397

398
  if (tStartDecode(pCoder) < 0) {
63,797!
399
    code = TSDB_CODE_INVALID_MSG;
×
400
    TSDB_CHECK_CODE(code, lino, _exit);
×
401
  }
402

403
  uint64_t nSubmitTbData;
404
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
63,797!
405
    code = TSDB_CODE_INVALID_MSG;
×
406
    TSDB_CHECK_CODE(code, lino, _exit);
×
407
  }
408

409
  int64_t btimeMs = taosGetTimestampMs();
63,795✔
410
  int64_t ctimeMs = btimeMs;
63,795✔
411
  for (int32_t i = 0; i < nSubmitTbData; i++) {
694,147✔
412
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
630,504✔
413
    TSDB_CHECK_CODE(code, lino, _exit);
630,352!
414
  }
415

416
  tEndDecode(pCoder);
63,643✔
417

418
_exit:
63,796✔
419
  tDecoderClear(pCoder);
63,796✔
420
  if (code) {
63,796!
421
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
422
           tstrerror(code), TMSG_INFO(pMsg->msgType));
423
  }
424
  return code;
63,796✔
425
}
426

427
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
428
  int32_t code = 0;
×
429

430
  int32_t    size;
431
  int32_t    ret;
432
  uint8_t   *pCont;
433
  SEncoder  *pCoder = &(SEncoder){0};
×
434
  SDeleteRes res = {0};
×
435

436
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
×
437
  initStorageAPI(&handle.api);
×
438

439
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
×
440
  if (code) goto _exit;
×
441

442
  res.ctimeMs = taosGetTimestampMs();
×
443
  // malloc and encode
444
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
×
445
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
×
446

447
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
×
448
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
×
449

450
  tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
×
451
  if (tEncodeDeleteRes(pCoder, &res) != 0) {
×
452
    vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
×
453
  }
454
  tEncoderClear(pCoder);
×
455

456
  rpcFreeCont(pMsg->pCont);
×
457
  pMsg->pCont = pCont;
×
458
  pMsg->contLen = size + sizeof(SMsgHead);
×
459

460
  taosArrayDestroy(res.uidList);
×
461

462
_exit:
×
463
  return code;
×
464
}
465

466
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
467
  int32_t code = 0;
×
468
  int32_t lino = 0;
×
469

470
  int64_t         ctimeMs = taosGetTimestampMs();
×
471
  SBatchDeleteReq pReq = {0};
×
472
  SDecoder       *pCoder = &(SDecoder){0};
×
473

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

476
  if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
×
477
    code = TSDB_CODE_INVALID_MSG;
×
478
  }
479

480
  tDecoderClear(pCoder);
×
481
  taosArrayDestroy(pReq.deleteReqs);
×
482

483
  if (code) {
×
484
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
485
  } else {
486
    vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
×
487
  }
488
  return code;
×
489
}
490

491
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
492
  SVArbCheckSyncReq syncReq = {0};
×
493

494
  if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
×
495
                                    &syncReq) != 0) {
496
    return TSDB_CODE_INVALID_MSG;
×
497
  }
498

499
  int32_t ret = vnodeCheckToken(pVnode, syncReq.member0Token, syncReq.member1Token);
×
500
  if (ret != 0) {
×
501
    vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
×
502
  }
503

504
  int32_t code = terrno;
×
505
  tFreeSVArbCheckSyncReq(&syncReq);
×
506

507
  return code;
×
508
}
509

510
int32_t vnodePreProcessDropTbMsg(SVnode *pVnode, SRpcMsg *pMsg) {
534✔
511
  int32_t          code = TSDB_CODE_SUCCESS;
534✔
512
  int32_t          lino = 0;
534✔
513
  int32_t          size = 0;
534✔
514
  SDecoder         dc = {0};
534✔
515
  SEncoder         ec = {0};
534✔
516
  SVDropTbBatchReq receivedBatchReqs = {0};
534✔
517
  SVDropTbBatchReq sentBatchReqs = {0};
534✔
518

519
  tDecoderInit(&dc, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead));
534✔
520

521
  code = tDecodeSVDropTbBatchReq(&dc, &receivedBatchReqs);
534✔
522
  if (code < 0) {
534!
523
    terrno = code;
×
524
    TSDB_CHECK_CODE(code, lino, _exit);
×
525
  }
526
  sentBatchReqs.pArray = taosArrayInit(receivedBatchReqs.nReqs, sizeof(SVDropTbReq));
534✔
527
  if (!sentBatchReqs.pArray) {
534!
528
    code = terrno;
×
529
    goto _exit;
×
530
  }
531

532
  for (int32_t i = 0; i < receivedBatchReqs.nReqs; ++i) {
1,068✔
533
    SVDropTbReq *pReq = receivedBatchReqs.pReqs + i;
534✔
534
    tb_uid_t     uid = metaGetTableEntryUidByName(pVnode->pMeta, pReq->name);
534✔
535
    if (uid == 0) {
534!
536
      vWarn("vgId:%d, preprocess drop ctb: %s not found", TD_VID(pVnode), pReq->name);
×
537
      continue;
×
538
    }
539
    pReq->uid = uid;
534✔
540
    vDebug("vgId:%d %s for: %s, uid: %" PRId64, TD_VID(pVnode), __func__, pReq->name, pReq->uid);
534!
541
    if (taosArrayPush(sentBatchReqs.pArray, pReq) == NULL) {
1,068!
542
      code = terrno;
×
543
      goto _exit;
×
544
    }
545
  }
546
  sentBatchReqs.nReqs = sentBatchReqs.pArray->size;
534✔
547

548
  tEncodeSize(tEncodeSVDropTbBatchReq, &sentBatchReqs, size, code);
534!
549
  tEncoderInit(&ec, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), size);
534✔
550
  code = tEncodeSVDropTbBatchReq(&ec, &sentBatchReqs);
534✔
551
  tEncoderClear(&ec);
534✔
552
  if (code != TSDB_CODE_SUCCESS) {
534!
553
    vError("vgId:%d %s failed to encode drop tb batch req: %s", TD_VID(pVnode), __func__, tstrerror(code));
×
554
    TSDB_CHECK_CODE(code, lino, _exit);
×
555
  }
556

557
_exit:
534✔
558
  tDecoderClear(&dc);
534✔
559
  if (sentBatchReqs.pArray) {
534!
560
    taosArrayDestroy(sentBatchReqs.pArray);
534✔
561
  }
562
  return code;
534✔
563
}
564

565
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
69,027✔
566
  int32_t code = 0;
69,027✔
567

568
  switch (pMsg->msgType) {
69,027!
569
    case TDMT_VND_CREATE_TABLE: {
2,668✔
570
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
2,668✔
571
    } break;
2,668✔
572
    case TDMT_VND_ALTER_TABLE: {
21✔
573
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
21✔
574
    } break;
21✔
575
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
511✔
576
    case TDMT_VND_DROP_TTL_TABLE: {
577
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
511✔
578
    } break;
511✔
579
    case TDMT_VND_SUBMIT: {
63,797✔
580
      code = vnodePreProcessSubmitMsg(pVnode, pMsg);
63,797✔
581
    } break;
63,796✔
582
    case TDMT_VND_DELETE: {
×
583
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
×
584
    } break;
×
585
    case TDMT_VND_BATCH_DEL: {
×
586
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
587
    } break;
×
588
    case TDMT_VND_ARB_CHECK_SYNC: {
×
589
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
×
590
    } break;
×
591
    case TDMT_VND_DROP_TABLE: {
534✔
592
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
534✔
593
    } break;
534✔
594
    default:
1,496✔
595
      break;
1,496✔
596
  }
597

598
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
69,026!
599
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
×
600
           TMSG_INFO(pMsg->msgType));
601
  }
602
  return code;
69,024✔
603
}
604

605
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
134,872✔
606
  int32_t code = 0;
134,872✔
607
  void   *ptr = NULL;
134,872✔
608
  void   *pReq;
609
  int32_t len;
610

611
  (void)taosThreadMutexLock(&pVnode->mutex);
134,872✔
612
  if (pVnode->disableWrite) {
134,874!
613
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
614
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
615
    return TSDB_CODE_VND_WRITE_DISABLED;
×
616
  }
617
  (void)taosThreadMutexUnlock(&pVnode->mutex);
134,874✔
618

619
  if (ver <= pVnode->state.applied) {
134,873!
620
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
621
           pVnode->state.applied);
622
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
623
  }
624

625
  vGDebug(&pMsg->info.traceId,
134,873!
626
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
627
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
628
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
629
          pMsg->info.conn.applyTerm, pMsg->contLen);
630

631
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
134,874!
632
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
633
  }
634

635
  if (!(pVnode->state.applied + 1 == ver)) {
134,874!
636
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
637
  }
638

639
  atomic_store_64(&pVnode->state.applied, ver);
134,874✔
640
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
134,874✔
641

642
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
134,875✔
643

644
  // skip header
645
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
134,456✔
646
  len = pMsg->contLen - sizeof(SMsgHead);
134,456✔
647
  bool needCommit = false;
134,456✔
648

649
  switch (pMsg->msgType) {
134,456!
650
    /* META */
651
    case TDMT_VND_CREATE_STB:
320✔
652
      if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
320!
653
      break;
320✔
654
    case TDMT_VND_ALTER_STB:
52✔
655
      if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
52!
656
      break;
52✔
657
    case TDMT_VND_DROP_STB:
10✔
658
      if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
10!
659
      break;
10✔
660
    case TDMT_VND_CREATE_TABLE:
2,729✔
661
      if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
2,729!
662
      break;
2,731✔
663
    case TDMT_VND_ALTER_TABLE:
21✔
664
      if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
21!
665
      break;
21✔
666
    case TDMT_VND_DROP_TABLE:
534✔
667
      if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
534!
668
      break;
534✔
669
    case TDMT_VND_DROP_TTL_TABLE:
37✔
670
      if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
37!
671
      break;
37✔
672
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
×
673
      if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
674
      break;
×
675
    case TDMT_VND_TRIM:
×
676
      if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
677
      break;
×
678
    case TDMT_VND_S3MIGRATE:
×
679
      if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
680
      break;
×
681
    case TDMT_VND_CREATE_SMA:
×
682
      if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
683
      break;
×
684
    /* TSDB */
685
    case TDMT_VND_SUBMIT:
128,009✔
686
      if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
128,009!
687
      break;
128,000✔
688
    case TDMT_VND_DELETE:
×
689
      if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
×
690
      break;
×
691
    case TDMT_VND_BATCH_DEL:
×
692
      if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
693
      break;
×
694
    /* TQ */
695
#if defined(USE_TQ) || defined(USE_STREAM)
696
    case TDMT_VND_TMQ_SUBSCRIBE:
1,252✔
697
      if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
1,252!
698
        goto _err;
×
699
      }
700
      break;
1,248✔
701
    case TDMT_VND_TMQ_DELETE_SUB:
21✔
702
      if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
21!
703
        goto _err;
×
704
      }
705
      break;
21✔
706
    case TDMT_VND_TMQ_COMMIT_OFFSET:
542✔
707
      if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
542!
708
        goto _err;
×
709
      }
710
      break;
541✔
711
    case TDMT_VND_TMQ_ADD_CHECKINFO:
2✔
712
      if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
2!
713
        goto _err;
×
714
      }
715
      break;
2✔
716
    case TDMT_VND_TMQ_DEL_CHECKINFO:
×
717
      if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
×
718
        goto _err;
×
719
      }
720
      break;
×
721
    case TDMT_STREAM_TASK_DEPLOY: {
21✔
722
      if ((code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len)) != TSDB_CODE_SUCCESS) {
21!
723
        goto _err;
×
724
      }
725
    } break;
21✔
726
    case TDMT_STREAM_TASK_DROP: {
×
727
      if ((code = tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
×
728
        goto _err;
×
729
      }
730
    } break;
×
731
    case TDMT_STREAM_TASK_UPDATE_CHKPT: {
4✔
732
      if ((code = tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen)) < 0) {
4!
733
        goto _err;
×
734
      }
735
    } break;
4✔
736
    case TDMT_STREAM_CONSEN_CHKPT: {
6✔
737
      if (pVnode->restored && (code = tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg)) < 0) {
6!
738
        goto _err;
×
739
      }
740

741
    } break;
6✔
742
    case TDMT_STREAM_TASK_PAUSE: {
×
743
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
744
          (code = tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
×
745
        goto _err;
×
746
      }
747
    } break;
×
748
    case TDMT_STREAM_TASK_RESUME: {
×
749
      if (pVnode->restored && vnodeIsLeader(pVnode) &&
×
750
          (code = tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen)) < 0) {
×
751
        goto _err;
×
752
      }
753
    } break;
×
754
    case TDMT_VND_STREAM_TASK_RESET: {
×
755
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessTaskResetReq(pVnode->pTq, pMsg)) < 0) {
×
756
        goto _err;
×
757
      }
758

759
    } break;
×
760
    case TDMT_VND_STREAM_ALL_STOP: {
50✔
761
      if (pVnode->restored && vnodeIsLeader(pVnode) && (code = tqProcessAllTaskStopReq(pVnode->pTq, pMsg)) < 0) {
50!
762
        goto _err;
×
763
      }
764

765
    } break;
50✔
766
#endif
767
    case TDMT_VND_ALTER_CONFIRM:
56✔
768
      needCommit = pVnode->config.hashChange;
56✔
769
      if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
56!
770
        goto _err;
×
771
      }
772
      break;
56✔
773
    case TDMT_VND_ALTER_CONFIG:
9✔
774
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
9✔
775
      break;
9✔
776
    case TDMT_VND_COMMIT:
719✔
777
      needCommit = true;
719✔
778
      break;
719✔
779
    case TDMT_VND_CREATE_INDEX:
28✔
780
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
28✔
781
      break;
28✔
782
    case TDMT_VND_DROP_INDEX:
28✔
783
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
28✔
784
      break;
28✔
785
    case TDMT_VND_STREAM_CHECK_POINT_SOURCE:  // always return true
2✔
786
      tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
2✔
787
      break;
2✔
788
    case TDMT_VND_STREAM_TASK_UPDATE:  // always return true
4✔
789
      tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
4✔
790
      break;
4✔
791
    case TDMT_VND_COMPACT:
×
792
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
×
793
      goto _exit;
×
794
    case TDMT_SYNC_CONFIG_CHANGE:
×
795
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
796
      break;
×
797
#ifdef TD_ENTERPRISE
798
    case TDMT_VND_KILL_COMPACT:
×
799
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
800
      break;
×
801
#endif
802
    /* ARB */
803
    case TDMT_VND_ARB_CHECK_SYNC:
×
804
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
×
805
      break;
×
806
    default:
×
807
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
808
      return TSDB_CODE_INVALID_MSG;
×
809
  }
810

811
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
134,444!
812

813
  walApplyVer(pVnode->pWal, ver);
134,448✔
814

815
  code = tqPushMsg(pVnode->pTq, pMsg->msgType);
134,451✔
816
  if (code) {
134,447!
817
    vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
818
    return code;
×
819
  }
820

821
  // commit if need
822
  if (needCommit) {
134,447✔
823
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
719✔
824
    code = vnodeAsyncCommit(pVnode);
719✔
825
    if (code) {
719!
826
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
827
      goto _err;
×
828
    }
829

830
    // start a new one
831
    code = vnodeBegin(pVnode);
719✔
832
    if (code) {
719!
833
      vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
×
834
      goto _err;
×
835
    }
836
  }
837

838
_exit:
134,448✔
839
  return 0;
134,864✔
840

841
_err:
×
842
  vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
×
843
         tstrerror(terrno), ver);
844
  return code;
×
845
}
846

847
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
16,986✔
848
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
16,986✔
849
    return 0;
14,955✔
850
  }
851

852
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
2,031✔
853
}
854

855
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
16,986✔
856
  vTrace("message in vnode query queue is processing");
16,986✔
857
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
16,986!
858
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
859
    return 0;
×
860
  }
861

862
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
16,985!
863
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
864
    return 0;
×
865
  }
866

867
  SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
16,985✔
868
  initStorageAPI(&handle.api);
16,985✔
869
  int32_t code = TSDB_CODE_SUCCESS;
16,984✔
870
  bool    redirected = false;
16,984✔
871

872
  switch (pMsg->msgType) {
16,984!
873
    case TDMT_SCH_QUERY:
1,579✔
874
      if (!syncIsReadyForRead(pVnode->sync)) {
1,579✔
875
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
9!
876
        redirected = true;
9✔
877
      }
878
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,580✔
879
      if (redirected) {
1,578✔
880
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
9✔
881
        return 0;
9✔
882
      }
883
      return code;
1,569✔
884
    case TDMT_SCH_MERGE_QUERY:
451✔
885
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
451✔
886
    case TDMT_SCH_QUERY_CONTINUE:
1,070✔
887
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
1,070✔
888
    case TDMT_VND_TMQ_CONSUME:
10,721✔
889
      return tqProcessPollReq(pVnode->pTq, pMsg);
10,721✔
890
    case TDMT_VND_TMQ_CONSUME_PUSH:
3,163✔
891
      return tqProcessPollPush(pVnode->pTq);
3,163✔
892
    default:
×
893
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
894
      return TSDB_CODE_APP_ERROR;
×
895
  }
896
}
897

898
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
7,750✔
899
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
7,750✔
900
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
7,750!
901
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
4,406!
902
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
4,233!
903
      !syncIsReadyForRead(pVnode->sync)) {
3,517!
904
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
905
    return 0;
×
906
  }
907

908
  switch (pMsg->msgType) {
7,750!
909
    case TDMT_SCH_FETCH:
3,989✔
910
    case TDMT_SCH_MERGE_FETCH:
911
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
3,989✔
912
    case TDMT_SCH_FETCH_RSP:
×
913
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
914
    // case TDMT_SCH_CANCEL_TASK:
915
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
916
    case TDMT_SCH_DROP_TASK:
2,027✔
917
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
2,027✔
918
    case TDMT_SCH_TASK_NOTIFY:
×
919
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
920
    case TDMT_SCH_QUERY_HEARTBEAT:
1,530✔
921
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
1,530✔
922
    case TDMT_VND_TABLE_META:
×
923
    case TDMT_VND_TABLE_NAME:
924
      return vnodeGetTableMeta(pVnode, pMsg, true);
×
925
    case TDMT_VND_TABLE_CFG:
×
926
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
927
    case TDMT_VND_BATCH_META:
173✔
928
      return vnodeGetBatchMeta(pVnode, pMsg);
173✔
929
    case TDMT_VND_VSUBTABLES_META:
×
930
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
931
    case TDMT_VND_VSTB_REF_DBS:
×
932
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
933
#ifdef TD_ENTERPRISE
934
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
×
935
      return vnodeQueryCompactProgress(pVnode, pMsg);
×
936
#endif
937
      //    case TDMT_VND_TMQ_CONSUME:
938
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
939
#ifdef USE_TQ
940
    case TDMT_VND_TMQ_VG_WALINFO:
10✔
941
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
10✔
942
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
1✔
943
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
1✔
944
    case TDMT_VND_TMQ_SEEK:
20✔
945
      return tqProcessSeekReq(pVnode->pTq, pMsg);
20✔
946
#endif
947
    default:
×
948
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
949
      return TSDB_CODE_APP_ERROR;
×
950
  }
951
}
952
#ifdef USE_STREAM
953
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
1,390✔
954
  vTrace("vgId:%d, msg:%p in stream queue is processing", pVnode->config.vgId, pMsg);
1,390✔
955

956
  // todo: NOTE: some command needs to run on follower, such as, stop_all_tasks
957
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
1,390!
958
       pMsg->msgType == TDMT_VND_BATCH_META) &&
1,391!
959
      !syncIsReadyForRead(pVnode->sync)) {
×
960
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
961
    return 0;
×
962
  }
963

964
  switch (pMsg->msgType) {
1,391!
965
    case TDMT_STREAM_TASK_RUN:
1,391✔
966
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
1,391✔
967
    case TDMT_STREAM_RETRIEVE:
×
968
      return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
×
969
    case TDMT_STREAM_RETRIEVE_RSP:
×
970
      return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
×
971
    case TDMT_VND_GET_STREAM_PROGRESS:
×
972
      return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
×
973
    default:
×
974
      vError("unknown msg type:%d in stream queue", pMsg->msgType);
×
975
      return TSDB_CODE_APP_ERROR;
×
976
  }
977
}
978

979
int32_t vnodeProcessStreamCtrlMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
452✔
980
  vTrace("vgId:%d, msg:%p in stream ctrl queue is processing", pVnode->config.vgId, pMsg);
452✔
981
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
452!
982
       pMsg->msgType == TDMT_VND_BATCH_META) &&
452!
983
      !syncIsReadyForRead(pVnode->sync)) {
×
984
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
985
    return 0;
×
986
  }
987

988
  switch (pMsg->msgType) {
452!
989
    case TDMT_MND_STREAM_HEARTBEAT_RSP:
61✔
990
      return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
61✔
991
    case TDMT_STREAM_TASK_DISPATCH:
138✔
992
      return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
138✔
993
    case TDMT_STREAM_TASK_DISPATCH_RSP:
196✔
994
      return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
196✔
995
    case TDMT_VND_STREAM_TASK_CHECK:
21✔
996
      return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
21✔
997
    case TDMT_VND_STREAM_TASK_CHECK_RSP:
24✔
998
      return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
24✔
999
    case TDMT_STREAM_TASK_CHECKPOINT_READY:
2✔
1000
      return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
2✔
1001
    case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
2✔
1002
      return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
2✔
1003
    case TDMT_STREAM_RETRIEVE_TRIGGER:
×
1004
      return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
×
1005
    case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
×
1006
      return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
×
1007
    case TDMT_MND_STREAM_REQ_CHKPT_RSP:
4✔
1008
      return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
4✔
1009
    case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
4✔
1010
      return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
4✔
1011
    default:
×
1012
      vError("unknown msg type:%d in stream ctrl queue", pMsg->msgType);
×
1013
      return TSDB_CODE_APP_ERROR;
×
1014
  }
1015
}
1016

1017
int32_t vnodeProcessStreamLongExecMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
2✔
1018
  vTrace("vgId:%d, msg:%p in stream long exec queue is processing", pVnode->config.vgId, pMsg);
2!
1019
  if (!syncIsReadyForRead(pVnode->sync)) {
2!
1020
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1021
    return 0;
×
1022
  }
1023

1024
  switch (pMsg->msgType) {
2!
1025
    case TDMT_VND_STREAM_SCAN_HISTORY:
2✔
1026
      return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2✔
1027
    default:
×
1028
      vError("unknown msg type:%d in stream long exec queue", pMsg->msgType);
×
1029
      return TSDB_CODE_APP_ERROR;
×
1030
  }
1031
}
1032

1033
int32_t vnodeProcessStreamChkptMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
6✔
1034
  vTrace("vgId:%d, msg:%p in stream chkpt queue is processing", pVnode->config.vgId, pMsg);
6!
1035
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
6!
1036
       pMsg->msgType == TDMT_VND_BATCH_META) &&
6!
1037
      !syncIsReadyForRead(pVnode->sync)) {
×
1038
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1039
    return 0;
×
1040
  }
1041

1042
  switch (pMsg->msgType) {
6!
1043
    case TDMT_STREAM_CHKPT_EXEC:
6✔
1044
      return tqProcessTaskRunReq(pVnode->pTq, pMsg);
6✔
1045
    default:
×
1046
      vError("unknown msg type:%d in stream chkpt queue", pMsg->msgType);
×
1047
      return TSDB_CODE_APP_ERROR;
×
1048
  }
1049
}
1050
#endif
1051

1052
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
×
1053
  int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
×
1054
  if (code) {
×
1055
    vError("failed to process sma result since %s", tstrerror(code));
×
1056
  }
1057
}
×
1058

1059
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
3,459✔
1060
  if (NULL == pMetaRsp) {
3,459!
1061
    return;
×
1062
  }
1063

1064
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
3,459✔
1065
  pMetaRsp->dbId = pVnode->config.dbId;
3,459✔
1066
  pMetaRsp->vgId = TD_VID(pVnode);
3,459✔
1067
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
3,459✔
1068
}
1069

1070
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
1071

1072
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1073
  if (!pVnode->restored) {
×
1074
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
1075
    return 0;
×
1076
  }
1077

1078
  int32_t     code = 0;
×
1079
  SVTrimDbReq trimReq = {0};
×
1080

1081
  // decode
1082
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
×
1083
    code = TSDB_CODE_INVALID_MSG;
×
1084
    goto _exit;
×
1085
  }
1086

1087
  vInfo("vgId:%d, process trim vnode request, time:%d", pVnode->config.vgId, trimReq.timestamp);
×
1088

1089
  code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
×
1090

1091
_exit:
×
1092
  return code;
×
1093
}
1094

1095
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1096

1097
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1098
  int32_t          code = 0;
×
1099
  SVS3MigrateDbReq s3migrateReq = {0};
×
1100

1101
  // decode
1102
  if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
×
1103
    code = TSDB_CODE_INVALID_MSG;
×
1104
    goto _exit;
×
1105
  }
1106

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

1109
  code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
×
1110

1111
_exit:
×
1112
  return code;
×
1113
}
1114

1115
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
37✔
1116
  int               ret = 0;
37✔
1117
  SVDropTtlTableReq ttlReq = {0};
37✔
1118
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
37!
1119
    ret = TSDB_CODE_INVALID_MSG;
×
1120
    goto end;
×
1121
  }
1122

1123
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
37!
1124
    ret = TSDB_CODE_INVALID_MSG;
×
1125
    goto end;
×
1126
  }
1127

1128
  if (ttlReq.nUids != 0) {
37!
1129
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId,
×
1130
          ttlReq.timestampSec, ttlReq.nUids);
1131
  }
1132

1133
  if (ttlReq.nUids > 0) {
37!
1134
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1135
    if (code) return code;
×
1136

1137
    code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
×
1138
    if (code) {
×
1139
      vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1140
    }
1141
  }
1142

1143
end:
37✔
1144
  taosArrayDestroy(ttlReq.pTbUids);
37✔
1145
  return ret;
37✔
1146
}
1147

1148
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1149
  int32_t                 code = -1;
×
1150
  SMetaReader             mr = {0};
×
1151
  SVDropTtlTableReq       ttlReq = {0};
×
1152
  SVFetchTtlExpiredTbsRsp rsp = {0};
×
1153
  SEncoder                encoder = {0};
×
1154
  SArray                 *pNames = NULL;
×
1155
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
×
1156
  pRsp->code = TSDB_CODE_SUCCESS;
×
1157
  pRsp->pCont = NULL;
×
1158
  pRsp->contLen = 0;
×
1159

1160
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1161
    terrno = TSDB_CODE_INVALID_MSG;
×
1162
    goto _end;
×
1163
  }
1164

1165
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
×
1166
    terrno = TSDB_CODE_INVALID_MSG;
×
1167
    goto _end;
×
1168
  }
1169

1170
  tb_uid_t    suid;
1171
  char        ctbName[TSDB_TABLE_NAME_LEN];
1172
  SVDropTbReq expiredTb = {.igNotExists = true};
×
1173
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
×
1174
  rsp.vgId = TD_VID(pVnode);
×
1175
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
×
1176
  if (!rsp.pExpiredTbs) goto _end;
×
1177

1178
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
×
1179
  if (!pNames) {
×
1180
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1181
    goto _end;
×
1182
  }
1183
  char buf[TSDB_TABLE_NAME_LEN];
1184
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
×
1185
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
×
1186
    expiredTb.suid = *uid;
×
1187
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
×
1188
    if (terrno < 0) goto _end;
×
1189
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
×
1190
    void *p = taosArrayPush(pNames, buf);
×
1191
    if (p == NULL) {
×
1192
      goto _end;
×
1193
    }
1194

1195
    expiredTb.name = p;
×
1196
    if (mr.me.type == TSDB_CHILD_TABLE) {
×
1197
      expiredTb.suid = mr.me.ctbEntry.suid;
×
1198
    }
1199

1200
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
×
1201
      goto _end;
×
1202
    }
1203
  }
1204

1205
  int32_t ret = 0;
×
1206
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
×
1207
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1208
  if (pRsp->pCont == NULL) {
×
1209
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1210
    code = -1;
×
1211
    goto _end;
×
1212
  }
1213
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1214
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
×
1215
  tEncoderClear(&encoder);
×
1216

1217
  if (terrno == 0) code = 0;
×
1218
_end:
×
1219
  metaReaderClear(&mr);
×
1220
  tFreeFetchTtlExpiredTbsRsp(&rsp);
×
1221
  taosArrayDestroy(ttlReq.pTbUids);
×
1222
  if (pNames) taosArrayDestroy(pNames);
×
1223
  pRsp->code = terrno;
×
1224
  return code;
×
1225
}
1226

1227
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
320✔
1228
  int32_t        code = 0;
320✔
1229
  SVCreateStbReq req = {0};
320✔
1230
  SDecoder       coder;
1231

1232
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
320✔
1233
  pRsp->code = TSDB_CODE_SUCCESS;
320✔
1234
  pRsp->pCont = NULL;
320✔
1235
  pRsp->contLen = 0;
320✔
1236

1237
  // decode and process req
1238
  tDecoderInit(&coder, pReq, len);
320✔
1239

1240
  code = tDecodeSVCreateStbReq(&coder, &req);
319✔
1241
  if (code) {
318!
1242
    pRsp->code = code;
×
1243
    goto _err;
×
1244
  }
1245

1246
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
318✔
1247
  if (code) {
320!
1248
    pRsp->code = code;
×
1249
    goto _err;
×
1250
  }
1251

1252
  if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
320!
1253
    pRsp->code = code;
×
1254
    goto _err;
×
1255
  }
1256

1257
  tDecoderClear(&coder);
320✔
1258
  return 0;
320✔
1259

1260
_err:
×
1261
  tDecoderClear(&coder);
×
1262
  return code;
×
1263
}
1264

1265
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2,729✔
1266
                                       SRpcMsg *pOriginRpc) {
1267
  SDecoder           decoder = {0};
2,729✔
1268
  SEncoder           encoder = {0};
2,729✔
1269
  int32_t            rcode = 0;
2,729✔
1270
  SVCreateTbBatchReq req = {0};
2,729✔
1271
  SVCreateTbReq     *pCreateReq;
1272
  SVCreateTbBatchRsp rsp = {0};
2,729✔
1273
  SVCreateTbRsp      cRsp = {0};
2,729✔
1274
  char               tbName[TSDB_TABLE_FNAME_LEN];
1275
  STbUidStore       *pStore = NULL;
2,729✔
1276
  SArray            *tbUids = NULL;
2,729✔
1277
  SArray            *tbNames = NULL;
2,729✔
1278
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
2,729✔
1279
  pRsp->code = TSDB_CODE_SUCCESS;
2,729✔
1280
  pRsp->pCont = NULL;
2,729✔
1281
  pRsp->contLen = 0;
2,729✔
1282

1283
  // decode
1284
  tDecoderInit(&decoder, pReq, len);
2,729✔
1285
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
2,729!
1286
    rcode = -1;
×
1287
    terrno = TSDB_CODE_INVALID_MSG;
×
1288
    goto _exit;
×
1289
  }
1290

1291
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
2,731✔
1292
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
2,731✔
1293
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
2,731✔
1294
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
2,731!
1295
    rcode = -1;
×
1296
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1297
    goto _exit;
×
1298
  }
1299

1300
  // loop to create table
1301
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
6,113✔
1302
    pCreateReq = req.pReqs + iReq;
3,382✔
1303
    memset(&cRsp, 0, sizeof(cRsp));
3,382✔
1304

1305
    if (tsEnableAudit && tsEnableAuditCreateTable) {
3,382!
1306
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
3,382!
1307
      if (str == NULL) {
3,382!
1308
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1309
        rcode = -1;
×
1310
        goto _exit;
×
1311
      }
1312
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
3,382✔
1313
      if (taosArrayPush(tbNames, &str) == NULL) {
3,382!
1314
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1315
        rcode = -1;
×
1316
        goto _exit;
×
1317
      }
1318
    }
1319

1320
    // validate hash
1321
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
3,382✔
1322
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
3,382!
1323
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1324
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1325
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1326
        rcode = -1;
×
1327
        goto _exit;
×
1328
      }
1329
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1330
      continue;
×
1331
    }
1332

1333
    // do create table
1334
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
3,381!
1335
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
1336
        cRsp.code = TSDB_CODE_SUCCESS;
×
1337
      } else {
1338
        cRsp.code = terrno;
×
1339
      }
1340
    } else {
1341
      cRsp.code = TSDB_CODE_SUCCESS;
3,382✔
1342
      if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
3,382!
1343
        vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1344
      }
1345
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
6,764!
1346
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1347
        rcode = -1;
×
1348
        goto _exit;
×
1349
      }
1350
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
3,382✔
1351
    }
1352

1353
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
6,764!
1354
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1355
      rcode = -1;
×
1356
      goto _exit;
×
1357
    }
1358
  }
1359

1360
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
2,731✔
1361
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
2,731!
1362
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1363
  }
1364
  if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
2,731!
1365
    goto _exit;
×
1366
  }
1367
  pStore = tdUidStoreFree(pStore);
2,731✔
1368

1369
  // prepare rsp
1370
  int32_t ret = 0;
2,731✔
1371
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
2,731!
1372
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
2,731✔
1373
  if (pRsp->pCont == NULL) {
2,731!
1374
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1375
    rcode = -1;
×
1376
    goto _exit;
×
1377
  }
1378
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
2,731✔
1379
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
2,731!
1380
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1381
  }
1382

1383
  if (tsEnableAudit && tsEnableAuditCreateTable) {
2,731!
1384
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
2,731✔
1385

1386
    SName name = {0};
2,731✔
1387
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
2,731!
1388
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1389
    }
1390

1391
    SStringBuilder sb = {0};
2,731✔
1392
    for (int32_t i = 0; i < tbNames->size; i++) {
6,113✔
1393
      char **key = (char **)taosArrayGet(tbNames, i);
3,382✔
1394
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
3,382✔
1395
      if (i < tbNames->size - 1) {
3,382✔
1396
        taosStringBuilderAppendChar(&sb, ',');
651✔
1397
      }
1398
      // taosMemoryFreeClear(*key);
1399
    }
1400

1401
    size_t len = 0;
2,731✔
1402
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
2,731✔
1403

1404
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
2,731!
1405
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
2,658✔
1406
    }
1407

1408
    taosStringBuilderDestroy(&sb);
2,731✔
1409
  }
1410

1411
_exit:
×
1412
  tDeleteSVCreateTbBatchReq(&req);
2,730✔
1413
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
2,731✔
1414
  taosArrayDestroy(tbUids);
2,731✔
1415
  tDecoderClear(&decoder);
2,731✔
1416
  tEncoderClear(&encoder);
2,730✔
1417
  taosArrayDestroyP(tbNames, NULL);
2,731✔
1418
  return rcode;
2,731✔
1419
}
1420

1421
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
52✔
1422
  int32_t        code = 0;
52✔
1423
  SVCreateStbReq req = {0};
52✔
1424
  SDecoder       dc = {0};
52✔
1425

1426
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
52✔
1427
  pRsp->code = TSDB_CODE_SUCCESS;
52✔
1428
  pRsp->pCont = NULL;
52✔
1429
  pRsp->contLen = 0;
52✔
1430

1431
  tDecoderInit(&dc, pReq, len);
52✔
1432

1433
  // decode req
1434
  code = tDecodeSVCreateStbReq(&dc, &req);
52✔
1435
  if (code) {
52!
1436
    tDecoderClear(&dc);
×
1437
    return code;
×
1438
  }
1439

1440
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
52✔
1441
  if (code) {
52!
1442
    pRsp->code = code;
×
1443
    tDecoderClear(&dc);
×
1444
    return code;
×
1445
  }
1446

1447
  tDecoderClear(&dc);
52✔
1448

1449
  return 0;
52✔
1450
}
1451

1452
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
10✔
1453
  SVDropStbReq req = {0};
10✔
1454
  int32_t      rcode = TSDB_CODE_SUCCESS;
10✔
1455
  SDecoder     decoder = {0};
10✔
1456
  SArray      *tbUidList = NULL;
10✔
1457

1458
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
10✔
1459
  pRsp->pCont = NULL;
10✔
1460
  pRsp->contLen = 0;
10✔
1461

1462
  // decode request
1463
  tDecoderInit(&decoder, pReq, len);
10✔
1464
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
10!
1465
    rcode = TSDB_CODE_INVALID_MSG;
×
1466
    goto _exit;
×
1467
  }
1468

1469
  // process request
1470
  tbUidList = taosArrayInit(8, sizeof(int64_t));
10✔
1471
  if (tbUidList == NULL) goto _exit;
10!
1472
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
10!
1473
    rcode = terrno;
×
1474
    goto _exit;
×
1475
  }
1476

1477
  if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
10!
1478
    rcode = terrno;
×
1479
    goto _exit;
×
1480
  }
1481

1482
  if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
10!
1483
    rcode = terrno;
×
1484
    goto _exit;
×
1485
  }
1486

1487
  // return rsp
1488
_exit:
10✔
1489
  if (tbUidList) taosArrayDestroy(tbUidList);
10!
1490
  pRsp->code = rcode;
10✔
1491
  tDecoderClear(&decoder);
10✔
1492
  return 0;
10✔
1493
}
1494

1495
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
21✔
1496
  SVAlterTbReq  vAlterTbReq = {0};
21✔
1497
  SVAlterTbRsp  vAlterTbRsp = {0};
21✔
1498
  SDecoder      dc = {0};
21✔
1499
  int32_t       code = 0;
21✔
1500
  int32_t       lino = 0;
21✔
1501
  int32_t       ret;
1502
  SEncoder      ec = {0};
21✔
1503
  STableMetaRsp vMetaRsp = {0};
21✔
1504

1505
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
21✔
1506
  pRsp->pCont = NULL;
21✔
1507
  pRsp->contLen = 0;
21✔
1508
  pRsp->code = TSDB_CODE_SUCCESS;
21✔
1509

1510
  tDecoderInit(&dc, pReq, len);
21✔
1511

1512
  // decode
1513
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
21!
1514
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1515
    tDecoderClear(&dc);
×
1516
    goto _exit;
×
1517
  }
1518

1519
  // process
1520
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
21!
1521
    vAlterTbRsp.code = terrno;
×
1522
    tDecoderClear(&dc);
×
1523
    goto _exit;
×
1524
  }
1525
  tDecoderClear(&dc);
21✔
1526

1527
  if (NULL != vMetaRsp.pSchemas) {
21✔
1528
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
19✔
1529
    vAlterTbRsp.pMeta = &vMetaRsp;
19✔
1530
  }
1531

1532
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
21!
1533
    int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq.tbName);
2✔
1534
    if (uid == 0) {
2!
1535
      vError("vgId:%d, %s failed at %s:%d since table %s not found", TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
1536
             vAlterTbReq.tbName);
1537
      goto _exit;
×
1538
    }
1539

1540
    SArray* tbUids = taosArrayInit(4, sizeof(int64_t));
2✔
1541
    void* p = taosArrayPush(tbUids, &uid);
2✔
1542
    TSDB_CHECK_NULL(p, code, lino, _exit, terrno);
2!
1543

1544
    vDebug("vgId:%d, remove tags value altered table:%s from query table list", TD_VID(pVnode), vAlterTbReq.tbName);
2!
1545
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, false)) < 0) {
2!
1546
      vError("vgId:%d, failed to remove tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1547
    }
1548

1549
    vDebug("vgId:%d, try to add table:%s in query table list", TD_VID(pVnode), vAlterTbReq.tbName);
2!
1550
    if ((code = tqUpdateTbUidList(pVnode->pTq, tbUids, true)) < 0) {
2!
1551
      vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1552
    }
1553

1554
    taosArrayDestroy(tbUids);
2✔
1555
  }
1556

1557
_exit:
19✔
1558
  taosArrayDestroy(vAlterTbReq.pMultiTag);
21✔
1559
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
21!
1560
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
21✔
1561
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
21✔
1562
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
21!
1563
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1564
  }
1565

1566
  tEncoderClear(&ec);
21✔
1567
  if (vMetaRsp.pSchemas) {
21✔
1568
    taosMemoryFree(vMetaRsp.pSchemas);
19!
1569
    taosMemoryFree(vMetaRsp.pSchemaExt);
19!
1570
  }
1571
  if (vMetaRsp.pColRefs) {
21!
1572
    taosMemoryFree(vMetaRsp.pColRefs);
×
1573
  }
1574
  return 0;
21✔
1575
}
1576

1577
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
534✔
1578
                                     SRpcMsg *pOriginRpc) {
1579
  SVDropTbBatchReq req = {0};
534✔
1580
  SVDropTbBatchRsp rsp = {0};
534✔
1581
  SDecoder         decoder = {0};
534✔
1582
  SEncoder         encoder = {0};
534✔
1583
  int32_t          ret;
1584
  SArray          *tbUids = NULL;
534✔
1585
  STbUidStore     *pStore = NULL;
534✔
1586
  SArray          *tbNames = NULL;
534✔
1587

1588
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
534✔
1589
  pRsp->pCont = NULL;
534✔
1590
  pRsp->contLen = 0;
534✔
1591
  pRsp->code = TSDB_CODE_SUCCESS;
534✔
1592

1593
  // decode req
1594
  tDecoderInit(&decoder, pReq, len);
534✔
1595
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
534✔
1596
  if (ret < 0) {
534!
1597
    terrno = TSDB_CODE_INVALID_MSG;
×
1598
    pRsp->code = terrno;
×
1599
    goto _exit;
×
1600
  }
1601

1602
  // process req
1603
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
534✔
1604
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
534✔
1605
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
534✔
1606
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
534!
1607

1608
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
1,068✔
1609
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
534✔
1610
    SVDropTbRsp  dropTbRsp = {0};
534✔
1611
    tb_uid_t     tbUid = 0;
534✔
1612

1613
    /* code */
1614
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
534✔
1615
    if (ret < 0) {
534!
1616
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1617
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1618
      } else {
1619
        dropTbRsp.code = terrno;
×
1620
      }
1621
    } else {
1622
      dropTbRsp.code = TSDB_CODE_SUCCESS;
534✔
1623
      if (tbUid > 0) {
534!
1624
        if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
×
1625
          vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
×
1626
        }
1627
      }
1628
    }
1629

1630
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
1,068!
1631
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1632
      pRsp->code = terrno;
×
1633
      goto _exit;
×
1634
    }
1635

1636
    if (tsEnableAuditCreateTable) {
534!
1637
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
534!
1638
      if (str == NULL) {
534!
1639
        pRsp->code = terrno;
×
1640
        goto _exit;
×
1641
      }
1642
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
534✔
1643
      if (taosArrayPush(tbNames, &str) == NULL) {
534!
1644
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1645
        pRsp->code = terrno;
×
1646
        goto _exit;
×
1647
      }
1648
    }
1649
  }
1650

1651
  if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
534!
1652
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1653
  }
1654

1655
  if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
534!
1656
    goto _exit;
×
1657
  }
1658

1659
  if (tsEnableAuditCreateTable) {
534!
1660
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
534✔
1661

1662
    SName name = {0};
534✔
1663
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
534!
1664
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1665
    }
1666

1667
    SStringBuilder sb = {0};
534✔
1668
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
1,068✔
1669
      char **key = (char **)taosArrayGet(tbNames, iReq);
534✔
1670
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
534✔
1671
      if (iReq < req.nReqs - 1) {
534!
1672
        taosStringBuilderAppendChar(&sb, ',');
×
1673
      }
1674
      taosMemoryFreeClear(*key);
534!
1675
    }
1676

1677
    size_t len = 0;
534✔
1678
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
534✔
1679

1680
    if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
534!
1681
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
534✔
1682
    }
1683

1684
    taosStringBuilderDestroy(&sb);
534✔
1685
  }
1686

1687
_exit:
×
1688
  taosArrayDestroy(tbUids);
534✔
1689
  pStore = tdUidStoreFree(pStore);
534✔
1690
  tDecoderClear(&decoder);
534✔
1691
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
534!
1692
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
534✔
1693
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
534✔
1694
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
534!
1695
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1696
  }
1697
  tEncoderClear(&encoder);
534✔
1698
  taosArrayDestroy(rsp.pArray);
534✔
1699
  taosArrayDestroy(tbNames);
534✔
1700
  return 0;
534✔
1701
}
1702

1703
#ifdef BUILD_NO_CALL
1704
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1705
                                              const char *tags) {
1706
  SSubmitBlkIter blkIter = {0};
1707
  STSchema      *pSchema = NULL;
1708
  tb_uid_t       suid = 0;
1709
  STSRow        *row = NULL;
1710
  int32_t        rv = -1;
1711

1712
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1713
  if (blkIter.row == NULL) return 0;
1714

1715
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1716
                                         &pSchema);  // TODO: use the real schema
1717
  if (TSDB_CODE_SUCCESS != code) {
1718
    printf("%s:%d no valid schema\n", tags, __LINE__);
1719
    return code;
1720
  }
1721

1722
  suid = msgIter->suid;
1723
  rv = TD_ROW_SVER(blkIter.row);
1724

1725
  char __tags[128] = {0};
1726
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1727
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1728
    tdSRowPrint(row, pSchema, __tags);
1729
  }
1730

1731
  taosMemoryFreeClear(pSchema);
1732

1733
  return TSDB_CODE_SUCCESS;
1734
}
1735
#endif
1736
typedef struct SSubmitReqConvertCxt {
1737
  SSubmitMsgIter msgIter;
1738
  SSubmitBlk    *pBlock;
1739
  SSubmitBlkIter blkIter;
1740
  STSRow        *pRow;
1741
  STSRowIter     rowIter;
1742
  SSubmitTbData *pTbData;
1743
  STSchema      *pTbSchema;
1744
  SArray        *pColValues;
1745
} SSubmitReqConvertCxt;
1746

1747
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1748
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1749
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1750
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1751
  if (TSDB_CODE_SUCCESS != code) {
×
1752
    return code;
×
1753
  }
1754
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1755

1756
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1757
  if (NULL == pCxt->pTbData) {
×
1758
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1759
    if (NULL == pCxt->pTbData) {
×
1760
      return terrno;
×
1761
    }
1762
  }
1763
  pCxt->pTbData->flags = 0;
×
1764
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
1765
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
1766
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
1767
  pCxt->pTbData->pCreateTbReq = NULL;
×
1768
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
1769
  if (NULL == pCxt->pTbData->aRowP) {
×
1770
    return terrno;
×
1771
  }
1772

1773
  taosArrayDestroy(pCxt->pColValues);
×
1774
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
1775
  if (NULL == pCxt->pColValues) {
×
1776
    return terrno;
×
1777
  }
1778
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
1779
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
1780
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
1781
      return terrno;
×
1782
    }
1783
  }
1784

1785
  return TSDB_CODE_SUCCESS;
×
1786
}
1787

1788
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
1789
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1790
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1791
  taosMemoryFreeClear(pCxt->pTbData);
×
1792
  taosArrayDestroy(pCxt->pColValues);
×
1793
}
×
1794

1795
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
1796
  if (tdValTypeIsNone(pCellVal->valType)) {
×
1797
    pColVal->flag = CV_FLAG_NONE;
×
1798
    return TSDB_CODE_SUCCESS;
×
1799
  }
1800

1801
  if (tdValTypeIsNull(pCellVal->valType)) {
×
1802
    pColVal->flag = CV_FLAG_NULL;
×
1803
    return TSDB_CODE_SUCCESS;
×
1804
  }
1805

1806
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
1807
    pColVal->value.nData = varDataLen(pCellVal->val);
×
1808
    pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
1809
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
1810
    float f = GET_FLOAT_VAL(pCellVal->val);
×
1811
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
1812
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
1813
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
1814
  } else {
1815
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
1816
  }
1817

1818
  pColVal->flag = CV_FLAG_VALUE;
×
1819
  return TSDB_CODE_SUCCESS;
×
1820
}
1821

1822
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
1823
  int32_t code = TSDB_CODE_SUCCESS;
×
1824
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
1825
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
1826
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
1827
    SCellVal  cellVal = {0};
×
1828
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
1829
      break;
×
1830
    }
1831
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
1832
  }
1833
  return code;
×
1834
}
1835

1836
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
1837
  if (pCxt->msgIter.schemaLen <= 0) {
×
1838
    return TSDB_CODE_SUCCESS;
×
1839
  }
1840

1841
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
1842
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
1843
    return terrno;
×
1844
  }
1845

1846
  SDecoder decoder = {0};
×
1847
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
1848
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
1849
  tDecoderClear(&decoder);
×
1850

1851
  return code;
×
1852
}
1853

1854
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
1855
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
1856
  if (NULL == pReq2->aSubmitTbData) {
×
1857
    return terrno;
×
1858
  }
1859

1860
  SSubmitReqConvertCxt cxt = {0};
×
1861

1862
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
1863
  while (TSDB_CODE_SUCCESS == code) {
×
1864
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
1865
    if (TSDB_CODE_SUCCESS == code) {
×
1866
      if (NULL == cxt.pBlock) {
×
1867
        break;
×
1868
      }
1869
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
1870
    }
1871
    if (TSDB_CODE_SUCCESS == code) {
×
1872
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
1873
    }
1874
    if (TSDB_CODE_SUCCESS == code) {
×
1875
      code = vnodeDecodeCreateTbReq(&cxt);
×
1876
    }
1877
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
1878
      code = vnodeTSRowConvertToColValArray(&cxt);
×
1879
      if (TSDB_CODE_SUCCESS == code) {
×
1880
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
1881
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
×
1882
      }
1883
    }
1884
    if (TSDB_CODE_SUCCESS == code) {
×
1885
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
1886
    }
1887
    if (TSDB_CODE_SUCCESS == code) {
×
1888
      taosMemoryFreeClear(cxt.pTbData);
×
1889
    }
1890
  }
1891

1892
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
1893
  return code;
×
1894
}
1895

1896
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
1897
  int32_t  code = TSDB_CODE_SUCCESS;
×
1898
  char    *pMsg = NULL;
×
1899
  uint32_t msglen = 0;
×
1900
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
1901
  if (TSDB_CODE_SUCCESS == code) {
×
1902
    pMsg = taosMemoryMalloc(msglen);
×
1903
    if (NULL == pMsg) {
×
1904
      code = terrno;
×
1905
    }
1906
  }
1907
  if (TSDB_CODE_SUCCESS == code) {
×
1908
    SEncoder encoder;
1909
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
1910
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
1911
    tEncoderClear(&encoder);
×
1912
  }
1913
  if (TSDB_CODE_SUCCESS == code) {
×
1914
    *ppMsg = pMsg;
×
1915
  }
1916
  return code;
×
1917
}
1918

1919
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
128,009✔
1920
                                     SRpcMsg *pOriginalMsg) {
1921
  int32_t code = 0;
128,009✔
1922
  terrno = 0;
128,009✔
1923

1924
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
128,009✔
1925
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
128,009✔
1926
  SArray      *newTbUids = NULL;
128,009✔
1927
  int32_t      ret;
1928
  SEncoder     ec = {0};
128,009✔
1929

1930
  pRsp->code = TSDB_CODE_SUCCESS;
128,009✔
1931

1932
  void           *pAllocMsg = NULL;
128,009✔
1933
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
128,009✔
1934
  if (0 == pMsg->version) {
128,009!
1935
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
1936
    if (TSDB_CODE_SUCCESS == code) {
×
1937
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
1938
    }
1939
    if (TSDB_CODE_SUCCESS == code) {
×
1940
      pAllocMsg = pReq;
×
1941
    }
1942
    if (TSDB_CODE_SUCCESS != code) {
×
1943
      goto _exit;
×
1944
    }
1945
  } else {
1946
    // decode
1947
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
128,009✔
1948
    len -= sizeof(SSubmitReq2Msg);
128,009✔
1949
    SDecoder dc = {0};
128,009✔
1950
    tDecoderInit(&dc, pReq, len);
128,009✔
1951
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
128,009!
1952
      code = TSDB_CODE_INVALID_MSG;
×
1953
      goto _exit;
×
1954
    }
1955
    tDecoderClear(&dc);
128,006✔
1956
  }
1957

1958
  // scan
1959
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
128,008✔
1960
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
128,007✔
1961
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
128,007✔
1962
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
828,991✔
1963
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
701,054✔
1964

1965
    if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
700,981!
1966
      code = TSDB_CODE_INVALID_MSG;
×
1967
      goto _exit;
×
1968
    }
1969

1970
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
700,981✔
1971
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
8!
1972
        code = TSDB_CODE_INVALID_MSG;
×
1973
        goto _exit;
×
1974
      }
1975

1976
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
8✔
1977
      SRowKey   lastKey;
1978
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
8✔
1979
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
16✔
1980
        SRowKey key;
1981
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
8✔
1982
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
8!
1983
          code = TSDB_CODE_INVALID_MSG;
×
1984
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
1985
          goto _exit;
×
1986
        }
1987
      }
1988
    } else {
1989
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
700,973✔
1990
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
700,973✔
1991
      SRowKey lastRowKey;
1992
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
7,846,319✔
1993
#ifndef NO_UNALIGNED_ACCESS
1994
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
7,145,343✔
1995
#else
1996
        TSKEY ts = taosGetInt64Aligned(&(aRow[iRow]->ts));
1997
        if (ts < minKey || ts > maxKey) {
1998
#endif
1999
          code = TSDB_CODE_INVALID_MSG;
507✔
2000
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
507!
2001
          goto _exit;
×
2002
        }
2003
        if (iRow == 0) {
7,144,836✔
2004
          tRowGetKey(aRow[iRow], &lastRowKey);
1,402,042!
2005
        } else {
2006
          SRowKey rowKey;
2007
          tRowGetKey(aRow[iRow], &rowKey);
12,887,630!
2008

2009
          if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
6,443,815!
2010
            code = TSDB_CODE_INVALID_MSG;
2011
            vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
2012
            goto _exit;
×
2013
          }
2014
          lastRowKey = rowKey;
6,444,363✔
2015
        }
2016
      }
2017
    }
2018
  }
2019

2020
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
830,504✔
2021
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
702,428✔
2022

2023
    if (pSubmitTbData->pCreateTbReq) {
702,271✔
2024
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
58✔
2025
    } else {
2026
      SMetaInfo info = {0};
702,213✔
2027

2028
      code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
702,213✔
2029
      if (code) {
702,270!
2030
        code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
2031
        vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
×
2032
        goto _exit;
×
2033
      }
2034

2035
      if (info.suid != pSubmitTbData->suid) {
702,298!
2036
        code = TSDB_CODE_INVALID_MSG;
×
2037
        goto _exit;
×
2038
      }
2039

2040
      if (info.suid) {
702,298✔
2041
        if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
702,229!
2042
          vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
×
2043
        }
2044
      }
2045

2046
      if (pSubmitTbData->sver != info.skmVer) {
702,509!
2047
        code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
2048
        goto _exit;
×
2049
      }
2050
    }
2051

2052
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
702,567✔
2053
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
8✔
2054
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
8✔
2055

2056
      if (nColData <= 0) {
8!
2057
        code = TSDB_CODE_INVALID_MSG;
×
2058
        goto _exit;
×
2059
      }
2060

2061
      if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
8!
2062
          aColData[0].nVal <= 0) {
8!
2063
        code = TSDB_CODE_INVALID_MSG;
×
2064
        goto _exit;
×
2065
      }
2066

2067
      for (int32_t j = 1; j < nColData; j++) {
16✔
2068
        if (aColData[j].nVal != aColData[0].nVal) {
8!
2069
          code = TSDB_CODE_INVALID_MSG;
×
2070
          goto _exit;
×
2071
        }
2072
      }
2073
    }
2074
  }
2075

2076
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
128,076!
2077
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2078

2079
  // loop to handle
2080
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
828,658✔
2081
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
700,686✔
2082

2083
    // create table
2084
    if (pSubmitTbData->pCreateTbReq) {
700,520✔
2085
      // alloc if need
2086
      if (pSubmitRsp->aCreateTbRsp == NULL &&
58✔
2087
          (pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
57!
2088
              NULL) {
2089
        code = terrno;
×
2090
        goto _exit;
×
2091
      }
2092

2093
      SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
58✔
2094

2095
      // create table
2096
      if (metaCreateTable2(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
58!
2097
        // create table success
2098

2099
        if (newTbUids == NULL &&
115!
2100
            (newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
57✔
2101
          code = terrno;
×
2102
          goto _exit;
×
2103
        }
2104

2105
        if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
116!
2106
          code = terrno;
×
2107
          goto _exit;
×
2108
        }
2109

2110
        if (pCreateTbRsp->pMeta) {
58!
2111
          vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
58✔
2112
        }
2113
      } else {  // create table failed
2114
        if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
2115
          code = terrno;
×
2116
          vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
×
2117
                 tstrerror(terrno));
2118
          goto _exit;
×
2119
        }
2120
        terrno = 0;
×
2121
        pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;  // update uid if table exist for using below
×
2122
      }
2123
    }
2124

2125
    // insert data
2126
    int32_t affectedRows;
2127
    code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
700,520✔
2128
    if (code) goto _exit;
700,803!
2129

2130
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
700,803✔
2131
    if (code) goto _exit;
700,650!
2132

2133
    pSubmitRsp->affectedRows += affectedRows;
700,650✔
2134
  }
2135

2136
  // update the affected table uid list
2137
  if (taosArrayGetSize(newTbUids) > 0) {
127,972✔
2138
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
57✔
2139
           (int32_t)taosArrayGetSize(newTbUids));
2140
    if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
57!
2141
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2142
    }
2143
  }
2144

2145
_exit:
128,005✔
2146
  // message
2147
  pRsp->code = code;
128,005✔
2148
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
128,005!
2149
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
128,004✔
2150
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
128,008✔
2151
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
128,008!
2152
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
2153
  }
2154
  tEncoderClear(&ec);
128,006✔
2155

2156
  // update statistics
2157
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
128,002✔
2158
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
128,008✔
2159
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
128,008✔
2160

2161
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
128,008!
2162
      strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
×
2163
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
2164
                                   pVnode->monitor.strClusterId,
×
2165
                                   pVnode->monitor.strDnodeId,
×
2166
                                   tsLocalEp,
2167
                                   pVnode->monitor.strVgId,
×
2168
                                   pOriginalMsg->info.conn.user,
×
2169
                                   "Success"};
2170
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
2171
  }
2172

2173
  if (code == 0) {
128,008✔
2174
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
128,004✔
2175
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
128,009✔
2176
  }
2177
  /*
2178
  if (code == 0) {
2179
    atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2180
    code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2181

2182
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2183
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2184
                                          pOriginalMsg->info.conn.user, "Success"};
2185
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2186
  }
2187
  else{
2188
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
2189
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2190
                                        pOriginalMsg->info.conn.user, "Failed"};
2191
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2192
  }
2193
  */
2194

2195
  // clear
2196
  taosArrayDestroy(newTbUids);
128,006✔
2197
  tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
128,001!
2198
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
128,008✔
2199

2200
  if (code) terrno = code;
128,007!
2201

2202
  taosMemoryFree(pAllocMsg);
128,007!
2203

2204
  return code;
128,003✔
2205
}
2206

2207
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2208
#ifdef USE_TSMA
2209
  SVCreateTSmaReq req = {0};
×
2210
  SDecoder        coder = {0};
×
2211

2212
  if (pRsp) {
×
2213
    pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
×
2214
    pRsp->code = TSDB_CODE_SUCCESS;
×
2215
    pRsp->pCont = NULL;
×
2216
    pRsp->contLen = 0;
×
2217
  }
2218

2219
  // decode and process req
2220
  tDecoderInit(&coder, pReq, len);
×
2221

2222
  if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
×
2223
    terrno = TSDB_CODE_MSG_DECODE_ERROR;
×
2224
    if (pRsp) pRsp->code = terrno;
×
2225
    goto _err;
×
2226
  }
2227

2228
  if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
×
2229
    if (pRsp) pRsp->code = terrno;
×
2230
    goto _err;
×
2231
  }
2232

2233
  tDecoderClear(&coder);
×
2234
  vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
×
2235
         req.indexName, req.indexUid, ver, req.tableUid);
2236
  return 0;
×
2237

2238
_err:
×
2239
  tDecoderClear(&coder);
×
2240
  vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
×
2241
         TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2242
  return terrno;
×
2243
#else
2244
  return TSDB_CODE_INTERNAL_ERROR;
2245
#endif
2246
}
2247

2248
/**
2249
 * @brief specific for smaDstVnode
2250
 *
2251
 * @param pVnode
2252
 * @param pCont
2253
 * @param contLen
2254
 * @return int32_t
2255
 */
2256
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
×
2257
  return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
×
2258
}
2259

2260
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
×
2261
  int32_t code = TSDB_CODE_SUCCESS;
×
2262

2263
  vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
×
2264
        pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2265

2266
  // TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2267
  code = metaTrimTables(pVnode->pMeta, ver);
×
2268

2269
  return code;
×
2270
}
2271

2272
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
56✔
2273
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
56!
2274
  int32_t code = TSDB_CODE_SUCCESS;
56✔
2275
  if (!pVnode->config.hashChange) {
56!
2276
    goto _exit;
56✔
2277
  }
2278

2279
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
×
2280
  if (code < 0) {
×
2281
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
2282
    goto _exit;
×
2283
  }
2284
  pVnode->config.hashChange = false;
×
2285

2286
_exit:
56✔
2287
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
56✔
2288
  pRsp->code = code;
56✔
2289
  pRsp->pCont = NULL;
56✔
2290
  pRsp->contLen = 0;
56✔
2291

2292
  return code;
56✔
2293
}
2294

2295
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2296
extern void    tsdbEnableBgTask(STsdb *pTsdb);
2297

2298
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
9✔
2299
  bool walChanged = false;
9✔
2300
  bool tsdbChanged = false;
9✔
2301

2302
  SAlterVnodeConfigReq req = {0};
9✔
2303
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
9!
2304
    terrno = TSDB_CODE_INVALID_MSG;
×
2305
    return TSDB_CODE_INVALID_MSG;
×
2306
  }
2307

2308
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
9!
2309
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
2310
        "s3Compact:%d fsync:%d level:%d "
2311
        "walRetentionPeriod:%d walRetentionSize:%d",
2312
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
2313
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
2314
        req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
2315
        req.walRetentionSize);
2316

2317
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
9!
2318
    pVnode->config.cacheLastSize = req.cacheLastSize;
×
2319
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
×
2320
  }
2321

2322
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
9!
2323
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
2324
          (uint64_t)(req.buffer * 1024LL * 1024LL));
2325
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
2326
  }
2327

2328
  if (pVnode->config.szCache != req.pages) {
9!
2329
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
×
2330
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
2331
             pVnode->config.szCache, req.pages, tstrerror(terrno));
2332
      return terrno;
×
2333
    } else {
2334
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
2335
      pVnode->config.szCache = req.pages;
×
2336
    }
2337
  }
2338

2339
  if (pVnode->config.cacheLast != req.cacheLast) {
9✔
2340
    pVnode->config.cacheLast = req.cacheLast;
2✔
2341
  }
2342

2343
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
9!
2344
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
×
2345
    walChanged = true;
×
2346
  }
2347

2348
  if (pVnode->config.walCfg.level != req.walLevel) {
9✔
2349
    if (pVnode->config.walCfg.level == 0) {
3!
2350
      pVnode->config.walCfg.clearFiles = 1;
3✔
2351
    }
2352
    pVnode->config.walCfg.level = req.walLevel;
3✔
2353
    walChanged = true;
3✔
2354
  }
2355

2356
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
9✔
2357
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
4✔
2358
    walChanged = true;
4✔
2359
  }
2360

2361
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
9✔
2362
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
2✔
2363
    walChanged = true;
2✔
2364
  }
2365

2366
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
9!
2367
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
×
2368
    if (!VND_IS_RSMA(pVnode)) {
×
2369
      tsdbChanged = true;
×
2370
    }
2371
  }
2372

2373
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
9!
2374
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
×
2375
    if (!VND_IS_RSMA(pVnode)) {
×
2376
      tsdbChanged = true;
×
2377
    }
2378
  }
2379

2380
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
9!
2381
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
×
2382
    if (!VND_IS_RSMA(pVnode)) {
×
2383
      tsdbChanged = true;
×
2384
    }
2385
  }
2386

2387
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
9✔
2388
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
4✔
2389
    if (!VND_IS_RSMA(pVnode)) {
4!
2390
      tsdbChanged = true;
4✔
2391
    }
2392
  }
2393

2394
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
9!
2395
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
2!
2396
      pVnode->config.sttTrigger = req.sttTrigger;
×
2397
    } else {
2398
      vnodeAWait(&pVnode->commitTask);
2✔
2399

2400
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
2✔
2401
      if (ret != 0) {
2!
2402
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2403
      }
2404

2405
      pVnode->config.sttTrigger = req.sttTrigger;
2✔
2406
      tsdbEnableBgTask(pVnode->pTsdb);
2✔
2407
    }
2408
  }
2409

2410
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
9!
2411
    pVnode->config.tsdbCfg.minRows = req.minRows;
2✔
2412
  }
2413

2414
  if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
9!
2415
    pVnode->config.s3KeepLocal = req.s3KeepLocal;
2✔
2416
  }
2417
  if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
9!
2418
    pVnode->config.s3Compact = req.s3Compact;
×
2419
  }
2420

2421
  if (walChanged) {
9✔
2422
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
7!
2423
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
2424
    }
2425
  }
2426

2427
  if (tsdbChanged) {
9✔
2428
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
4✔
2429
  }
2430

2431
  return 0;
9✔
2432
}
2433

2434
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2435
  SBatchDeleteReq deleteReq;
2436
  SDecoder        decoder;
2437
  tDecoderInit(&decoder, pReq, len);
×
2438
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
2439
    tDecoderClear(&decoder);
×
2440
    return terrno = TSDB_CODE_INVALID_MSG;
×
2441
  }
2442

2443
  SMetaReader mr = {0};
×
2444
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
2445
  STsdb *pTsdb = pVnode->pTsdb;
×
2446

2447
  if (deleteReq.level) {
×
2448
    pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
×
2449
  }
2450

2451
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
2452
  for (int32_t i = 0; i < sz; i++) {
×
2453
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
2454
    char             *name = pOneReq->tbname;
×
2455
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
2456
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
2457
      continue;
×
2458
    }
2459

2460
    int64_t uid = mr.me.uid;
×
2461

2462
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
2463
    if (code < 0) {
×
2464
      terrno = code;
×
2465
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
2466
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2467
    }
2468

2469
    if (deleteReq.level == 0) {
×
2470
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
2471
      if (code < 0) {
×
2472
        terrno = code;
×
2473
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
2474
               ", end ts:%" PRId64,
2475
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
2476
      }
2477
    }
2478
    tDecoderClear(&mr.coder);
×
2479
  }
2480
  metaReaderClear(&mr);
×
2481
  taosArrayDestroy(deleteReq.deleteReqs);
×
2482
  return 0;
×
2483
}
2484

2485
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
2486
                                     SRpcMsg *pOriginalMsg) {
2487
  int32_t     code = 0;
×
2488
  SDecoder   *pCoder = &(SDecoder){0};
×
2489
  SDeleteRes *pRes = &(SDeleteRes){0};
×
2490

2491
  pRsp->msgType = TDMT_VND_DELETE_RSP;
×
2492
  pRsp->pCont = NULL;
×
2493
  pRsp->contLen = 0;
×
2494
  pRsp->code = TSDB_CODE_SUCCESS;
×
2495

2496
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
×
2497
  if (pRes->uidList == NULL) {
×
2498
    code = terrno;
×
2499
    goto _err;
×
2500
  }
2501

2502
  tDecoderInit(pCoder, pReq, len);
×
2503
  code = tDecodeDeleteRes(pCoder, pRes);
×
2504
  if (code) goto _err;
×
2505

2506
  if (pRes->affectedRows > 0) {
×
2507
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
×
2508
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
×
2509
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
×
2510
      if (code) goto _err;
×
2511
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
×
2512
      if (code) goto _err;
×
2513
    }
2514
  }
2515

2516
  code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
×
2517

2518
  tDecoderClear(pCoder);
×
2519
  taosArrayDestroy(pRes->uidList);
×
2520

2521
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
×
2522
  int32_t     ret = 0;
×
2523
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
×
2524
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
2525
  SEncoder ec = {0};
×
2526
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
2527
  code = tEncodeSVDeleteRsp(&ec, &rsp);
×
2528
  if (code) goto _err;
×
2529
  tEncoderClear(&ec);
×
2530
  return code;
×
2531

2532
_err:
×
2533
  /*
2534
  if(code == TSDB_CODE_SUCCESS){
2535
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2536
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2537
                                        pOriginalMsg->info.conn.user, "Success"};
2538
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2539
  }
2540
  else{
2541
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
2542
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2543
                                        pOriginalMsg->info.conn.user, "Failed"};
2544
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2545
  }
2546
  */
2547

2548
  return code;
×
2549
}
2550
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
28✔
2551
  SVCreateStbReq req = {0};
28✔
2552
  SDecoder       dc = {0};
28✔
2553
  int32_t        code = 0;
28✔
2554

2555
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
28✔
2556
  pRsp->code = TSDB_CODE_SUCCESS;
28✔
2557
  pRsp->pCont = NULL;
28✔
2558
  pRsp->contLen = 0;
28✔
2559

2560
  tDecoderInit(&dc, pReq, len);
28✔
2561
  // decode req
2562
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
28!
2563
    tDecoderClear(&dc);
×
2564
    return terrno = TSDB_CODE_INVALID_MSG;
×
2565
  }
2566

2567
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
28✔
2568
  if (code) {
28!
2569
    pRsp->code = code;
×
2570
    goto _err;
×
2571
  }
2572
  tDecoderClear(&dc);
28✔
2573
  return 0;
28✔
2574

2575
_err:
×
2576
  tDecoderClear(&dc);
×
2577
  return code;
×
2578
}
2579
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
28✔
2580
  SDropIndexReq req = {0};
28✔
2581
  int32_t       code = 0;
28✔
2582
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
28✔
2583
  pRsp->code = TSDB_CODE_SUCCESS;
28✔
2584
  pRsp->pCont = NULL;
28✔
2585
  pRsp->contLen = 0;
28✔
2586

2587
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
28!
2588
    pRsp->code = code;
×
2589
    return code;
×
2590
  }
2591

2592
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
28✔
2593
  if (code) {
28!
2594
    pRsp->code = code;
×
2595
    return code;
×
2596
  }
2597
  return TSDB_CODE_SUCCESS;
28✔
2598
}
2599

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

2602
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2603
  if (!pVnode->restored) {
×
2604
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
2605
    return 0;
×
2606
  }
2607
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
×
2608
}
2609

2610
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2611
  if (syncCheckMember(pVnode->sync) != 0) {
×
2612
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
2613
  }
2614

2615
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
2616
  pRsp->code = TSDB_CODE_SUCCESS;
×
2617
  pRsp->pCont = NULL;
×
2618
  pRsp->contLen = 0;
×
2619

2620
  return 0;
×
2621
}
2622

2623
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2624
  SSyncState syncState = syncGetState(pVnode->sync);
×
2625
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
×
2626
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
×
2627
  }
2628

2629
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
×
2630
  if (vnodeGetArbToken(pVnode, token) != 0) {
×
2631
    return terrno = TSDB_CODE_NOT_FOUND;
×
2632
  }
2633

2634
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
×
2635
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
×
2636
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
×
2637
  }
2638

2639
  terrno = TSDB_CODE_SUCCESS;
×
2640
  return 0;
×
2641
}
2642

2643
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
2644
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
×
2645
  if (code != 0) {
×
2646
    return code;
×
2647
  }
2648

2649
  return syncCheckSynced(pVnode->sync);
×
2650
}
2651

2652
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
2653
  int32_t code = 0;
×
2654

2655
  SVArbCheckSyncReq syncReq = {0};
×
2656

2657
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
2658
  if (code) {
×
2659
    return terrno = code;
×
2660
  }
2661

2662
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
2663
  pRsp->code = TSDB_CODE_SUCCESS;
×
2664
  pRsp->pCont = NULL;
×
2665
  pRsp->contLen = 0;
×
2666

2667
  SVArbCheckSyncRsp syncRsp = {0};
×
2668
  syncRsp.arbToken = syncReq.arbToken;
×
2669
  syncRsp.member0Token = syncReq.member0Token;
×
2670
  syncRsp.member1Token = syncReq.member1Token;
×
2671
  syncRsp.vgId = TD_VID(pVnode);
×
2672

2673
  if (vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
×
2674
    vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
×
2675
  }
2676
  syncRsp.errCode = terrno;
×
2677

2678
  if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
×
2679
    vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
×
2680
    code = -1;
×
2681
    goto _OVER;
×
2682
  }
2683

2684
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
×
2685
  if (contLen <= 0) {
×
2686
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2687
    code = -1;
×
2688
    goto _OVER;
×
2689
  }
2690
  void *pHead = rpcMallocCont(contLen);
×
2691
  if (!pHead) {
×
2692
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2693
    code = -1;
×
2694
    goto _OVER;
×
2695
  }
2696

2697
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
2698
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2699
    rpcFreeCont(pHead);
×
2700
    code = -1;
×
2701
    goto _OVER;
×
2702
  }
2703

2704
  pRsp->pCont = pHead;
×
2705
  pRsp->contLen = contLen;
×
2706

2707
  terrno = TSDB_CODE_SUCCESS;
×
2708

2709
_OVER:
×
2710
  tFreeSVArbCheckSyncReq(&syncReq);
×
2711
  return code;
×
2712
}
2713

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