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

taosdata / TDengine / #4513

17 Jul 2025 02:02AM UTC coverage: 31.359% (-31.1%) from 62.446%
#4513

push

travis-ci

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

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

68541 of 301034 branches covered (22.77%)

Branch coverage included in aggregate %.

117356 of 291771 relevant lines covered (40.22%)

602262.98 hits per line

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

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

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

32
extern taos_counter_t *tsInsertCounter;
33

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

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

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

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

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

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

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

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

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

101
  tEndDecode(pCoder);
116✔
102

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

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

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

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

135
  tEndDecode(&dc);
87✔
136

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

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

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

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

163
  code = 0;
×
164

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

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

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

183
  SArray *tbUids = NULL;
80✔
184
  int64_t timestampMs = 0;
80✔
185

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

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

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

206
    ttlReq.nUids = taosArrayGetSize(tbUids);
79✔
207
    ttlReq.pTbUids = tbUids;
79✔
208
  }
209

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

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

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

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

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

236
  code = 0;
×
237

238
_exit:
80✔
239
  taosArrayDestroy(tbUids);
80✔
240

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

248
  return code;
79✔
249
}
250

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

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

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

270
  int64_t uid;
271
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
28,273✔
272
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
29✔
273
    TSDB_CHECK_CODE(code, lino, _exit);
29!
274
  }
275

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

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

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

297
  // scan and check
298
  TSKEY now = btimeMs;
28,273✔
299
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
28,273✔
300
    now *= 1000;
16✔
301
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
28,257!
302
    now *= 1000000;
×
303
  }
304

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

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

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

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

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

345
    for (uint64_t i = 1; i < nColData; i++) {
×
346
      code = tDecodeColData(version, pCoder, &colData);
×
347
      if (code) {
×
348
        code = TSDB_CODE_INVALID_MSG;
×
349
        TSDB_CHECK_CODE(code, lino, _exit);
×
350
      }
351
    }
352
  } else {
353
    uint64_t nRow;
354
    if (tDecodeU64v(pCoder, &nRow) < 0) {
28,273!
355
      code = TSDB_CODE_INVALID_MSG;
×
356
      TSDB_CHECK_CODE(code, lino, _exit);
×
357
    }
358

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

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

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

385
  tEndDecode(pCoder);
28,273✔
386

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

398
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
25,873!
399
    return TSDB_CODE_MSG_PREPROCESSED;
×
400
  }
401

402
  SDecoder *pCoder = &(SDecoder){0};
25,873✔
403

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

409
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
25,873✔
410

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

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

422
  int64_t btimeMs = taosGetTimestampMs();
25,873✔
423
  int64_t ctimeMs = btimeMs;
25,873✔
424
  for (int32_t i = 0; i < nSubmitTbData; i++) {
54,146✔
425
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
28,273✔
426
    TSDB_CHECK_CODE(code, lino, _exit);
28,273!
427
  }
428

429
  tEndDecode(pCoder);
25,873✔
430

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

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

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

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

455
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
×
456
  if (code) goto _exit;
×
457

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

463
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
×
464
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
×
465

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

472
  rpcFreeCont(pMsg->pCont);
×
473
  pMsg->pCont = pCont;
×
474
  pMsg->contLen = size + sizeof(SMsgHead);
×
475

476
  taosArrayDestroy(res.uidList);
×
477

478
_exit:
×
479
  return code;
×
480
}
481

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

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

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

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

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

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

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

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

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

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

523
  return code;
×
524
}
525

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

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

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

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

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

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

581

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

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

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

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

600

601
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
26,175✔
602
  int32_t code = 0;
26,175✔
603

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

610
  switch (pMsg->msgType) {
26,175!
611
    case TDMT_VND_CREATE_TABLE: {
87✔
612
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
87✔
613
    } break;
87✔
614
    case TDMT_VND_ALTER_TABLE: {
×
615
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
×
616
    } break;
×
617
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
80✔
618
    case TDMT_VND_DROP_TTL_TABLE: {
619
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
80✔
620
    } break;
79✔
621
    case TDMT_VND_SUBMIT: {
25,873✔
622
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
25,873!
623
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
624
    } break;
25,873✔
625
    case TDMT_VND_DELETE: {
×
626
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
×
627
    } break;
×
628
    case TDMT_VND_BATCH_DEL: {
×
629
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
630
    } break;
×
631
    case TDMT_VND_ARB_CHECK_SYNC: {
×
632
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
×
633
    } break;
×
634
    case TDMT_VND_DROP_TABLE: {
×
635
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
×
636
    } break;
×
637
    case TDMT_VND_SSMIGRATE: {
×
638
      code = vnodePreProcessSsMigrateReq(pVnode, pMsg);
×
639
    } break;
×
640
    default:
135✔
641
      break;
135✔
642
  }
643

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

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

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

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

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

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

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

690
  atomic_store_64(&pVnode->state.applied, ver);
26,148✔
691
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
26,148✔
692

693
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
26,148✔
694

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

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

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

823
  walApplyVer(pVnode->pWal, ver);
26,097✔
824

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

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

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

850
_exit:
26,095✔
851
  return 0;
26,148✔
852

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

862
  return code;
×
863
}
864

865
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
17,926✔
866
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
17,926✔
867
    return 0;
366✔
868
  }
869

870
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
17,560✔
871
}
872

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

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

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

893
  switch (pMsg->msgType) {
17,926!
894
    case TDMT_SCH_QUERY:
14,897✔
895
      if (!syncIsReadyForRead(pVnode->sync)) {
14,897!
896
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
×
897
        redirected = true;
×
898
      }
899
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
14,896✔
900
      if (redirected) {
14,898!
901
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
×
902
        return 0;
×
903
      }
904

905
      return code;
14,898✔
906
    case TDMT_SCH_MERGE_QUERY:
2,663✔
907
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
2,663✔
908
    case TDMT_SCH_QUERY_CONTINUE:
343✔
909
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
343✔
910
    case TDMT_VND_TMQ_CONSUME:
22✔
911
      return tqProcessPollReq(pVnode->pTq, pMsg);
22✔
912
    case TDMT_VND_TMQ_CONSUME_PUSH:
1✔
913
      return tqProcessPollPush(pVnode->pTq);
1✔
914
    default:
×
915
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
916
      return TSDB_CODE_APP_ERROR;
×
917
  }
918
}
919

920
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
42,438✔
921
  int32_t code = TSDB_CODE_SUCCESS;
42,438✔
922
  vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
42,438!
923
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
42,438!
924
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
27,717!
925
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
27,671✔
926
      !syncIsReadyForRead(pVnode->sync)) {
14,770!
927
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
928
    return 0;
×
929
  }
930

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

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

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

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

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

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

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

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

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

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

1021
_exit:
×
1022
  return code;
×
1023
}
1024

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

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

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

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

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

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

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

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

1071

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1222
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
56✔
1223
  if (code) {
56!
1224
    pRsp->code = code;
×
1225
    goto _err;
×
1226
  }
1227

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

1233
  tDecoderClear(&coder);
56✔
1234
  return 0;
56✔
1235

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

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

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

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

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

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

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

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

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

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

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

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

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

1367
    SStringBuilder sb = {0};
87✔
1368
    for (int32_t i = 0; i < tbNames->size; i++) {
174✔
1369
      char **key = (char **)taosArrayGet(tbNames, i);
87✔
1370
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
87✔
1371
      if (i < tbNames->size - 1) {
87!
1372
        taosStringBuilderAppendChar(&sb, ',');
×
1373
      }
1374
      // taosMemoryFreeClear(*key);
1375
    }
1376

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

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

1384
    taosStringBuilderDestroy(&sb);
87✔
1385
  }
1386

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

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

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

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

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

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

1423
  tDecoderClear(&dc);
16✔
1424

1425
  return 0;
16✔
1426
}
1427

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

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

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

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

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

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

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

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

1481
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
×
1482
  pRsp->pCont = NULL;
×
1483
  pRsp->contLen = 0;
×
1484
  pRsp->code = TSDB_CODE_SUCCESS;
×
1485

1486
  tDecoderInit(&dc, pReq, len);
×
1487

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

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

1503
  if (NULL != vMetaRsp.pSchemas) {
×
1504
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
×
1505
    vAlterTbRsp.pMeta = &vMetaRsp;
×
1506
  }
1507

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

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

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

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

1531
    taosArrayDestroy(tbUids);
×
1532
  }
1533

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

1543
  tEncoderClear(&ec);
×
1544
  if (vMetaRsp.pSchemas) {
×
1545
    taosMemoryFree(vMetaRsp.pSchemas);
×
1546
    taosMemoryFree(vMetaRsp.pSchemaExt);
×
1547
  }
1548
  if (vMetaRsp.pColRefs) {
×
1549
    taosMemoryFree(vMetaRsp.pColRefs);
×
1550
  }
1551
  return 0;
×
1552
}
1553

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

1565
  pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
×
1566
  pRsp->pCont = NULL;
×
1567
  pRsp->contLen = 0;
×
1568
  pRsp->code = TSDB_CODE_SUCCESS;
×
1569

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

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

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

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

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

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

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

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

1636
  if (tsEnableAuditCreateTable) {
×
1637
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1638

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

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

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

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

1661
    taosStringBuilderDestroy(&sb);
×
1662
  }
1663

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

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

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

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

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

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

1708
  taosMemoryFreeClear(pSchema);
1709

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

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

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

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

1762
  return TSDB_CODE_SUCCESS;
×
1763
}
1764

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

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

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

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

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

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

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

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

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

1828
  return code;
×
1829
}
1830

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

1837
  SSubmitReqConvertCxt cxt = {0};
×
1838

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2029
  pRsp->code = TSDB_CODE_SUCCESS;
25,873✔
2030

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

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

2055
  // scan
2056
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
25,873✔
2057
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
25,873✔
2058
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
25,873✔
2059
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
54,146✔
2060
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
28,273✔
2061

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

2067
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
28,273!
2068
      if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
×
2069
        code = TSDB_CODE_INVALID_MSG;
×
2070
        TSDB_CHECK_CODE(code, lino, _exit);
×
2071
      }
2072

2073
      SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
×
2074
      SRowKey   lastKey;
2075
      tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
×
2076
      for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
×
2077
        SRowKey key;
2078
        tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
×
2079
        if (tRowKeyCompare(&lastKey, &key) >= 0) {
×
2080
          code = TSDB_CODE_INVALID_MSG;
×
2081
          vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
×
2082
          TSDB_CHECK_CODE(code, lino, _exit);
×
2083
        }
2084
      }
2085
    } else {
2086
      int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
28,273✔
2087
      SRow  **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
28,273✔
2088
      SRowKey lastRowKey;
2089
      for (int32_t iRow = 0; iRow < nRow; ++iRow) {
56,546✔
2090
#ifndef NO_UNALIGNED_ACCESS
2091
        if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
28,273!
2092
#else
2093
        TSKEY ts = taosGetInt64Aligned(&(aRow[iRow]->ts));
2094
        if (ts < minKey || ts > maxKey) {
2095
#endif
2096
          code = TSDB_CODE_INVALID_MSG;
×
2097
          vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
×
2098
          TSDB_CHECK_CODE(code, lino, _exit);
×
2099
        }
2100
        if (iRow == 0) {
28,273!
2101
          tRowGetKey(aRow[iRow], &lastRowKey);
56,546!
2102
        } else {
2103
          SRowKey rowKey;
2104
          tRowGetKey(aRow[iRow], &rowKey);
×
2105

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

2117
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
54,146✔
2118
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
28,273✔
2119

2120
    if (pSubmitTbData->pCreateTbReq) {
28,273✔
2121
      pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
29✔
2122
    } else {
2123
      SMetaInfo info = {0};
28,244✔
2124

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

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

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

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

2151
    if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
28,273!
2152
      int32_t   nColData = TARRAY_SIZE(pSubmitTbData->aCol);
×
2153
      SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
×
2154

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

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

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

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

2178
  // loop to handle
2179
  for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
54,145✔
2180
    SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
28,273✔
2181

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

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

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

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

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

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

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

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

2241
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
28,271✔
2242
    TSDB_CHECK_CODE(code, lino, _exit);
28,272!
2243

2244
    pSubmitRsp->affectedRows += affectedRows;
28,272✔
2245
  }
2246

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

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

2267
  // update statistics
2268
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
25,873✔
2269
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
25,873✔
2270
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
25,873✔
2271

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

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

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

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

2311
  // clear
2312
  taosArrayDestroy(newTbUids);
25,875✔
2313
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
25,873!
2314
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
25,872✔
2315

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

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

2328
  taosMemoryFree(pAllocMsg);
25,870!
2329
  tDecoderClear(&dc);
25,871✔
2330

2331
  return code;
25,870✔
2332
}
2333

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

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

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

2343
  return code;
×
2344
}
2345

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

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

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

2366
  return code;
×
2367
}
2368

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

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

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

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

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

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

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

2413
  if (pVnode->config.cacheLast != req.cacheLast) {
×
2414
    pVnode->config.cacheLast = req.cacheLast;
×
2415
  }
2416

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2505
  return 0;
×
2506
}
2507

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

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

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

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

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

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

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

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

2565
  pRsp->msgType = TDMT_VND_DELETE_RSP;
×
2566
  pRsp->pCont = NULL;
×
2567
  pRsp->contLen = 0;
×
2568
  pRsp->code = TSDB_CODE_SUCCESS;
×
2569

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

2576
  tDecoderInit(pCoder, pReq, len);
×
2577
  code = tDecodeDeleteRes(pCoder, pRes);
×
2578
  if (code) goto _err;
×
2579

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

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

2592
  tDecoderClear(pCoder);
×
2593
  taosArrayDestroy(pRes->uidList);
×
2594

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

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

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

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

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

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

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

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

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

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

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

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

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

2694
  return 0;
×
2695
}
2696

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

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

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

2713
  terrno = TSDB_CODE_SUCCESS;
×
2714
  return 0;
×
2715
}
2716

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

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

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

2729
  SVArbCheckSyncReq syncReq = {0};
×
2730

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

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

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

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

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

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

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

2778
  pRsp->pCont = pHead;
×
2779
  pRsp->contLen = contLen;
×
2780

2781
  terrno = TSDB_CODE_SUCCESS;
×
2782

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

2788
#ifndef TD_ENTERPRISE
2789
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
2790
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
2791
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc