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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

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

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

32
extern taos_counter_t *tsInsertCounter;
33

34
extern int32_t vnodeProcessScanVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
35
extern int32_t vnodeQueryScanProgress(SVnode *pVnode, SRpcMsg *pMsg);
36

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

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

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

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

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

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

85
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
×
86
  int32_t code = 0;
×
87
  int32_t lino = 0;
×
88

89
  if (tStartDecode(pCoder) < 0) {
×
90
    code = TSDB_CODE_INVALID_MSG;
×
91
    TSDB_CHECK_CODE(code, lino, _exit);
×
92
  }
93

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

100
  // name
101
  char *name = NULL;
×
102
  if (tDecodeCStr(pCoder, &name) < 0) {
×
103
    code = TSDB_CODE_INVALID_MSG;
×
104
    TSDB_CHECK_CODE(code, lino, _exit);
×
105
  }
106

107
  // uid
108
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
×
109
  if (uid == 0) {
×
110
    uid = tGenIdPI64();
×
111
  }
112
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
×
113

114
  // btime
115
  taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos + 8), btime);
×
116

117
  tEndDecode(pCoder);
×
118

119
_exit:
×
120
  if (code) {
×
121
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
122
  } else {
123
    vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
×
124
    if (pUid) *pUid = uid;
×
125
  }
126
  return code;
×
127
}
128
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
129
  int32_t code = 0;
×
130
  int32_t lino = 0;
×
131

132
  int64_t  btime = taosGetTimestampMs();
×
133
  SDecoder dc = {0};
×
134
  int32_t  nReqs;
135

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

142
  if (tDecodeI32v(&dc, &nReqs) < 0) {
×
143
    code = TSDB_CODE_INVALID_MSG;
×
144
    TSDB_CHECK_CODE(code, lino, _exit);
×
145
  }
146
  for (int32_t iReq = 0; iReq < nReqs; iReq++) {
×
147
    code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
×
148
    TSDB_CHECK_CODE(code, lino, _exit);
×
149
  }
150

151
  tEndDecode(&dc);
×
152

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

162
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
163
  int32_t code = TSDB_CODE_INVALID_MSG;
×
164
  int32_t lino = 0;
×
165

166
  if (pVnode->config.isAudit) {
×
167
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
168
  }
169

170
  SDecoder dc = {0};
×
171
  tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
×
172

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

183
  code = 0;
×
184

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

196
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1✔
197
  int32_t code = TSDB_CODE_INVALID_MSG;
1✔
198
  int32_t lino = 0;
1✔
199

200
  SMsgHead *pContOld = pMsg->pCont;
1✔
201
  int32_t   reqLenOld = pMsg->contLen - sizeof(SMsgHead);
1✔
202

203
  SArray *tbUids = NULL;
1✔
204
  int64_t timestampMs = 0;
1✔
205

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

212
  {  // find expired uids
213
    tbUids = taosArrayInit(8, sizeof(tb_uid_t));
1✔
214
    if (tbUids == NULL) {
1✔
215
      code = terrno;
×
216
      TSDB_CHECK_CODE(code, lino, _exit);
×
217
    }
218

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

226
    ttlReq.nUids = taosArrayGetSize(tbUids);
1✔
227
    ttlReq.pTbUids = tbUids;
1✔
228
  }
229

230
  if (ttlReq.nUids == 0) {
1✔
231
    code = TSDB_CODE_MSG_PREPROCESSED;
1✔
232
    TSDB_CHECK_CODE(code, lino, _exit);
1✔
233
  }
234

235
  {  // prepare new content
236
    int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
×
237
    int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
×
238

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

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

251
    rpcFreeCont(pContOld);
×
252
    pMsg->pCont = pContNew;
×
253
    pMsg->contLen = contLenNew;
×
254
  }
255

256
  code = 0;
×
257

258
_exit:
1✔
259
  taosArrayDestroy(tbUids);
1✔
260

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

268
  return code;
1✔
269
}
270

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

276
  if (tStartDecode(pCoder) < 0) {
×
277
    code = TSDB_CODE_INVALID_MSG;
×
278
    TSDB_CHECK_CODE(code, lino, _exit);
×
279
  }
280

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

290
  int64_t uid;
291
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
×
292
    code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
×
293
    TSDB_CHECK_CODE(code, lino, _exit);
×
294
  }
295

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

302
  if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
×
303
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), uid);
×
304
    pCoder->pos += sizeof(int64_t);
×
305
  } else {
306
    if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
×
307
      code = TSDB_CODE_INVALID_MSG;
×
308
      TSDB_CHECK_CODE(code, lino, _exit);
×
309
    }
310
  }
311

312
  if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
×
313
    code = TSDB_CODE_INVALID_MSG;
×
314
    TSDB_CHECK_CODE(code, lino, _exit);
×
315
  }
316

317
  // scan and check
318
  TSKEY now = btimeMs;
×
319
  if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
×
320
    now *= 1000;
×
321
  } else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
×
322
    now *= 1000000;
×
323
  }
324

325
  int32_t keep = pVnode->config.tsdbCfg.keep2;
×
326

327
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
×
328
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
×
329
  if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
330
    uint64_t nColData;
331
    if (tDecodeU64v(pCoder, &nColData) < 0) {
×
332
      code = TSDB_CODE_INVALID_MSG;
×
333
      TSDB_CHECK_CODE(code, lino, _exit);
×
334
    }
335

336
    SColData colData = {0};
×
337
    code = tDecodeColData(version, pCoder, &colData, false);
×
338
    if (code) {
×
339
      code = TSDB_CODE_INVALID_MSG;
×
340
      TSDB_CHECK_CODE(code, lino, _exit);
×
341
    }
342

343
    if (colData.flag != HAS_VALUE) {
×
344
      code = TSDB_CODE_INVALID_MSG;
×
345
      TSDB_CHECK_CODE(code, lino, _exit);
×
346
    }
347

348
    for (uint32_t iRow = 0; iRow < colData.nVal; iRow++) {
×
349
      if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
×
350
        code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
×
351
        TSDB_CHECK_CODE(code, lino, _exit);
×
352
      }
353
    }
354

355
    for (uint64_t i = 1; i < nColData; i++) {
×
356
      code = tDecodeColData(version, pCoder, &colData, true);
×
357
      if (code) {
×
358
        code = TSDB_CODE_INVALID_MSG;
×
359
        TSDB_CHECK_CODE(code, lino, _exit);
×
360
      }
361
    }
362
  } else {
363
    uint64_t nRow;
364
    if (tDecodeU64v(pCoder, &nRow) < 0) {
×
365
      code = TSDB_CODE_INVALID_MSG;
×
366
      TSDB_CHECK_CODE(code, lino, _exit);
×
367
    }
368

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

382
      // Check pRow->sver
383
      if (pRow->sver != submitTbData.sver) {
×
384
        code = TSDB_CODE_INVALID_DATA_FMT;
×
385
        TSDB_CHECK_CODE(code, lino, _exit);
×
386
      }
387
    }
388
  }
389

390
  if (!tDecodeIsEnd(pCoder)) {
×
391
    taosSetInt64Aligned((int64_t *)(pCoder->data + pCoder->pos), ctimeMs);
×
392
    pCoder->pos += sizeof(int64_t);
×
393
  }
394

395
  tEndDecode(pCoder);
×
396

397
_exit:
×
398
  if (code) {
×
399
    vError("vgId:%d, %s:%d failed to vnodePreProcessSubmitTbData submit request since %s", TD_VID(pVnode), __func__,
×
400
           lino, tstrerror(code));
401
  }
402
  return code;
×
403
}
404
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
405
  int32_t code = 0;
×
406
  int32_t lino = 0;
×
407

408
  if (tsBypassFlag & TSDB_BYPASS_RA_RPC_RECV_SUBMIT) {
×
409
    return TSDB_CODE_MSG_PREPROCESSED;
×
410
  }
411

412
  SDecoder *pCoder = &(SDecoder){0};
×
413

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

419
  tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
×
420

421
  if (tStartDecode(pCoder) < 0) {
×
422
    code = TSDB_CODE_INVALID_MSG;
×
423
    TSDB_CHECK_CODE(code, lino, _exit);
×
424
  }
425

426
  uint64_t nSubmitTbData;
427
  if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
×
428
    code = TSDB_CODE_INVALID_MSG;
×
429
    TSDB_CHECK_CODE(code, lino, _exit);
×
430
  }
431

432
  int64_t btimeMs = taosGetTimestampMs();
×
433
  int64_t ctimeMs = btimeMs;
×
434
  for (uint64_t i = 0; i < nSubmitTbData; i++) {
×
435
    code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
×
436
    TSDB_CHECK_CODE(code, lino, _exit);
×
437
  }
438

439
  tEndDecode(pCoder);
×
440

441
_exit:
×
442
  tDecoderClear(pCoder);
×
443
  if (code) {
×
444
    vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
×
445
           tstrerror(code), TMSG_INFO(pMsg->msgType));
446
  }
447
  return code;
×
448
}
449

450
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
451
  int32_t code = 0;
×
452

453
  int32_t    size;
454
  int32_t    ret;
455
  uint8_t   *pCont;
456
  SEncoder  *pCoder = &(SEncoder){0};
×
457
  SDeleteRes res = {0};
×
458

459
  SReadHandle handle = {0};
×
460
  handle.vnode = pVnode;
×
461
  handle.pMsgCb = &pVnode->msgCb;
×
462
  handle.skipRollup = 1;
×
463
  initStorageAPI(&handle.api);
×
464

465
  code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
×
466
  if (code) goto _exit;
×
467

468
  res.ctimeMs = taosGetTimestampMs();
×
469
  // malloc and encode
470
  tEncodeSize(tEncodeDeleteRes, &res, size, ret);
×
471
  pCont = rpcMallocCont(size + sizeof(SMsgHead));
×
472

473
  ((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
×
474
  ((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
×
475

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

482
  rpcFreeCont(pMsg->pCont);
×
483
  pMsg->pCont = pCont;
×
484
  pMsg->contLen = size + sizeof(SMsgHead);
×
485

486
  taosArrayDestroy(res.uidList);
×
487

488
_exit:
×
489
  return code;
×
490
}
491

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

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

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

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

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

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

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

524
  SVArbCheckSyncReq syncReq = {0};
×
525

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

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

536
  int32_t code = terrno;
×
537
  tFreeSVArbCheckSyncReq(&syncReq);
×
538

539
  return code;
×
540
}
541

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

551
  if (pVnode->config.isAudit) {
×
552
    return TSDB_CODE_PAR_PERMISSION_DENIED;
×
553
  }
554

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

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

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

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

593
_exit:
×
594
  tDecoderClear(&dc);
×
595
  if (sentBatchReqs.pArray) {
×
596
    taosArrayDestroy(sentBatchReqs.pArray);
×
597
  }
598
  return code;
×
599
}
600

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

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

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

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

620
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
81✔
621
  int32_t code = 0;
81✔
622

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

629
  switch (pMsg->msgType) {
81✔
630
    case TDMT_VND_CREATE_TABLE: {
×
631
      code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
×
632
    } break;
×
633
    case TDMT_VND_ALTER_TABLE: {
×
634
      code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
×
635
    } break;
×
636
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
1✔
637
    case TDMT_VND_DROP_TTL_TABLE: {
638
      code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
1✔
639
    } break;
1✔
640
    case TDMT_VND_SUBMIT: {
×
641
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.preprocess_time, METRIC_LEVEL_LOW,
×
642
                           { code = vnodePreProcessSubmitMsg(pVnode, pMsg); });
643
    } break;
×
644
    case TDMT_VND_DELETE: {
×
645
      code = vnodePreProcessDeleteMsg(pVnode, pMsg);
×
646
    } break;
×
647
    case TDMT_VND_BATCH_DEL: {
×
648
      code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
×
649
    } break;
×
650
    case TDMT_VND_ARB_CHECK_SYNC: {
×
651
      code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
×
652
    } break;
×
653
    case TDMT_VND_DROP_TABLE: {
×
654
      code = vnodePreProcessDropTbMsg(pVnode, pMsg);
×
655
    } break;
×
656
#ifdef TD_ENTERPRISE
657
    case TDMT_VND_SSMIGRATE_FILESET: {
×
658
      code = vnodePreProcessSsMigrateFileSetReq(pVnode, pMsg);
×
659
    } break;
×
660
#endif
661
    case TDMT_VND_AUDIT_RECORD: {
×
662
      vTrace("vgId:%d, pre process TDMT_VND_AUDIT_RECORD", TD_VID(pVnode));
×
663
    } break;
×
664
    default:
80✔
665
      break;
80✔
666
  }
667

668
  if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
81✔
669
    vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
×
670
           TMSG_INFO(pMsg->msgType));
671
  }
672
  return code;
81✔
673
}
674

675
static int32_t inline vnodeSubmitSubRowBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
676
  int32_t code = 0;
×
677
  int32_t lino = 0;
×
678

679
  int64_t   st = taosGetTimestampUs();
×
680
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
681
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
682

683
  SBseBatch *pBatch = NULL;
×
684

685
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
686
  TSDB_CHECK_CODE(code, lino, _exit);
×
687

688
  SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
689
  int32_t rowIdx = -1;
×
690
  for (int32_t i = 0; i < sz; i++) {
×
691
    int64_t     seq = 0;
×
692
    SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
693
    if (p->type == TSDB_DATA_BLOB_EMPTY_VALUE || p->type == TSDB_DATA_BLOB_NULL_VALUE) {
×
694
      // skip empty or null blob
695
      continue;
×
696
    }
697

698
    code = bseBatchPut(pBatch, &seq, pBlobSet->data + p->offset, p->len);
×
699
    TSDB_CHECK_CODE(code, lino, _exit);
×
700

701
    SRow *row = taosArrayGetP(pSubmitTbData->aRowP, i);
×
702
    if (row == NULL) {
×
703
      int32_t tlen = taosArrayGetSize(pBlobSet->pSeqTable);
×
704
      uTrace("blob invalid row index:%d, sz:%d, pBlobSet size:%d", rowIdx, sz, tlen);
×
705
      break;
×
706
    }
707

708
    if (tPutU64(row->data + p->dataOffset, seq) < 0) {
×
709
      code = TSDB_CODE_INVALID_MSG;
×
710
      TSDB_CHECK_CODE(code, lino, _exit);
×
711
    }
712
  }
713

714
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
715
  TSDB_CHECK_CODE(code, lino, _exit);
×
716

717
  int64_t cost = taosGetTimestampUs() - st;
×
718
  if (cost >= 500) {
×
719
    vDebug("vgId:%d, %s, cost:%" PRId64 "us, rows:%d, size:%" PRId64 "", TD_VID(pVnode), __func__, cost, sz,
×
720
           pBlobSet->len);
721
  }
722
_exit:
×
723
  if (code != 0) {
×
724
    vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
×
725
  }
726
  return code;
×
727
}
728

729
static int32_t inline vnodeSubmitSubColBlobData(SVnode *pVnode, SSubmitTbData *pSubmitTbData) {
×
730
  int32_t code = 0;
×
731
  int32_t lino = 0;
×
732

733
  int32_t   blobColIdx = 0;
×
734
  SColData *pBlobCol = NULL;
×
735
  int64_t   st = taosGetTimestampUs();
×
736
  SBlobSet *pBlobSet = pSubmitTbData->pBlobSet;
×
737
  int32_t   sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
738

739
  SBseBatch *pBatch = NULL;
×
740

741
  code = bseBatchInit(pVnode->pBse, &pBatch, sz);
×
742
  TSDB_CHECK_CODE(code, lino, _exit);
×
743

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

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

769
    memcpy(pBlobCol->pData + offset, (void *)&seq, BSE_SEQUECE_SIZE);
×
770
    offset += BSE_SEQUECE_SIZE;
×
771
  }
772

773
  code = bseCommitBatch(pVnode->pBse, pBatch);
×
774
  TSDB_CHECK_CODE(code, lino, _exit);
×
775

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

797
  return code;
×
798
}
799

800
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
120✔
801
  int32_t code = 0;
120✔
802
  int32_t lino = 0;
120✔
803
  void   *ptr = NULL;
120✔
804
  void   *pReq;
805
  int32_t len;
806

807
  (void)taosThreadMutexLock(&pVnode->mutex);
120✔
808
  if (pVnode->disableWrite) {
120✔
809
    (void)taosThreadMutexUnlock(&pVnode->mutex);
×
810
    vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
×
811
    return TSDB_CODE_VND_WRITE_DISABLED;
×
812
  }
813
  (void)taosThreadMutexUnlock(&pVnode->mutex);
120✔
814

815
  if (ver <= pVnode->state.applied) {
120✔
816
    vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64, TD_VID(pVnode), ver,
×
817
           pVnode->state.applied);
818
    return terrno = TSDB_CODE_VND_DUP_REQUEST;
×
819
  }
820

821
  vGDebug(&pMsg->info.traceId,
120✔
822
          "vgId:%d, index:%" PRId64 ", process write request:%s, applied:%" PRId64 ", state.applyTerm:%" PRId64
823
          ", conn.applyTerm:%" PRId64 ", contLen:%d",
824
          TD_VID(pVnode), ver, TMSG_INFO(pMsg->msgType), pVnode->state.applied, pVnode->state.applyTerm,
825
          pMsg->info.conn.applyTerm, pMsg->contLen);
826

827
  if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
120✔
828
    vError("vgId:%d, applyTerm mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
829
           pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
830
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
831
  }
832

833
  if (!(pVnode->state.applied + 1 == ver)) {
120✔
834
    vError("vgId:%d, mountVgId:%d, ver mismatch, expected: %" PRId64 ", received: %" PRId64, TD_VID(pVnode),
×
835
           pVnode->config.mountVgId, pVnode->state.applied + 1, ver);
836
    return terrno = TSDB_CODE_INTERNAL_ERROR;
×
837
  }
838

839
  atomic_store_64(&pVnode->state.applied, ver);
120✔
840
  atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
120✔
841

842
  if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
120✔
843

844
  // skip header
845
  pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
80✔
846
  len = pMsg->contLen - sizeof(SMsgHead);
80✔
847
  bool needCommit = false;
80✔
848
  bool forceTrimWal = false;
80✔
849

850
  switch (pMsg->msgType) {
80✔
851
    /* META */
852
    case TDMT_VND_CREATE_STB:
36✔
853
      code = vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp);
36✔
854
      TSDB_CHECK_CODE(code, lino, _err);
36✔
855
      break;
36✔
856
    case TDMT_VND_ALTER_STB:
32✔
857
      code = vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp);
32✔
858
      TSDB_CHECK_CODE(code, lino, _err);
32✔
859
      break;
32✔
860
    case TDMT_VND_DROP_STB:
8✔
861
      code = vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp, pMsg);
8✔
862
      TSDB_CHECK_CODE(code, lino, _err);
8✔
863
      break;
8✔
864
    case TDMT_VND_CREATE_TABLE:
×
865
      code = vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
×
866
      TSDB_CHECK_CODE(code, lino, _err);
×
867
      break;
×
868
    case TDMT_VND_ALTER_TABLE:
×
869
      code = vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp);
×
870
      TSDB_CHECK_CODE(code, lino, _err);
×
871
      break;
×
872
    case TDMT_VND_DROP_TABLE:
×
873
    case TDMT_VND_SNODE_DROP_TABLE:
874
      code = vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg);
×
875
      TSDB_CHECK_CODE(code, lino, _err);
×
876
      break;
×
877
    case TDMT_VND_CREATE_RSMA:
×
878
      code = vnodeProcessCreateRsmaReq(pVnode, ver, pReq, len, pRsp);
×
879
      TSDB_CHECK_CODE(code, lino, _err);
×
880
      break;
×
881
    case TDMT_VND_DROP_RSMA:
×
882
      code = vnodeProcessDropRsmaReq(pVnode, ver, pReq, len, pRsp);
×
883
      TSDB_CHECK_CODE(code, lino, _err);
×
884
      break;
×
885
    case TDMT_VND_ALTER_RSMA:
×
886
      code = vnodeProcessAlterRsmaReq(pVnode, ver, pReq, len, pRsp);
×
887
      TSDB_CHECK_CODE(code, lino, _err);
×
888
      break;
×
889
    case TDMT_VND_DROP_TTL_TABLE:
×
890
      code = vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp);
×
891
      TSDB_CHECK_CODE(code, lino, _err);
×
892
      break;
×
893
    case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
×
894
      code = vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp);
×
895
      TSDB_CHECK_CODE(code, lino, _err);
×
896
      break;
×
897
    case TDMT_VND_TRIM:
×
898
      code = vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp);
×
899
      TSDB_CHECK_CODE(code, lino, _err);
×
900
      break;
×
901
      case TDMT_VND_TRIM_WAL:
×
902
        needCommit = true;
×
903
        forceTrimWal = true;
×
904
        break;
×
905
#ifdef TD_ENTERPRISE
906
    case TDMT_VND_SSMIGRATE_FILESET:
×
907
      if (vnodeProcessSsMigrateFileSetReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
908
      break;
×
909
    case TDMT_VND_FOLLOWER_SSMIGRATE:
×
910
      if (vnodeProcessFollowerSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
911
      break;
×
912
    case TDMT_VND_KILL_SSMIGRATE:
×
913
      if (vnodeProcessKillSsMigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
×
914
      break;
×
915
#endif
916

917
    /* TSDB */
918
    case TDMT_VND_SUBMIT: {
×
919
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.apply_time, METRIC_LEVEL_LOW, {
×
920
        if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
921
      });
922
      METRICS_UPDATE(pVnode->writeMetrics.apply_bytes, METRIC_LEVEL_LOW, (int64_t)pMsg->contLen);
×
923
      break;
×
924
    }
925
    case TDMT_VND_DELETE:
×
926
      code = vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg);
×
927
      TSDB_CHECK_CODE(code, lino, _err);
×
928
      break;
×
929
    case TDMT_VND_BATCH_DEL:
×
930
      code = vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp);
×
931
      TSDB_CHECK_CODE(code, lino, _err);
×
932
      break;
×
933
      /* TQ */
934
#if defined(USE_TQ) || defined(USE_STREAM)
935
    case TDMT_VND_TMQ_SUBSCRIBE:
×
936
      code = tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len);
×
937
      TSDB_CHECK_CODE(code, lino, _err);
×
938
      break;
×
939
    case TDMT_VND_TMQ_DELETE_SUB:
×
940
      code = tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen);
×
941
      TSDB_CHECK_CODE(code, lino, _err);
×
942
      break;
×
943
    case TDMT_VND_TMQ_COMMIT_OFFSET:
×
944
      code = tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len);
×
945
      TSDB_CHECK_CODE(code, lino, _err);
×
946
      break;
×
947
#endif
948
    case TDMT_VND_ALTER_CONFIRM:
×
949
      needCommit = pVnode->config.hashChange;
×
950
      code = vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp);
×
951
      TSDB_CHECK_CODE(code, lino, _err);
×
952
      break;
×
953
    case TDMT_VND_ALTER_CONFIG:
×
954
      vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
×
955
      break;
×
956
    case TDMT_VND_COMMIT:
4✔
957
      needCommit = true;
4✔
958
      break;
4✔
959
    case TDMT_VND_CREATE_INDEX:
×
960
      vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
×
961
      break;
×
962
    case TDMT_VND_DROP_INDEX:
×
963
      vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
×
964
      break;
×
965
    case TDMT_VND_COMPACT:
×
966
      vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
×
967
      goto _exit;
×
968
    case TDMT_VND_SCAN:
×
969
      vnodeProcessScanVnodeReq(pVnode, ver, pReq, len, pRsp);
×
970
      goto _exit;
×
971
    case TDMT_SYNC_CONFIG_CHANGE:
×
972
      vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
×
973
      break;
×
974
#ifdef TD_ENTERPRISE
975
    case TDMT_VND_KILL_COMPACT:
×
976
      vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
×
977
      break;
×
978
#endif
979
    case TDMT_VND_KILL_SCAN:
×
980
      vnodeProcessKillScanReq(pVnode, ver, pReq, len, pRsp);
×
981
      break;
×
982
    case TDMT_VND_KILL_TRIM:
×
983
      vnodeProcessKillRetentionReq(pVnode, ver, pReq, len, pRsp);
×
984
      break;
×
985
    /* ARB */
986
    case TDMT_VND_ARB_CHECK_SYNC:
×
987
      vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
×
988
      break;
×
989
    case TDMT_VND_AUDIT_RECORD:
×
990
      vTrace("vgId:%d, processed audit msg", TD_VID(pVnode));
×
991
      code = vnodeProcessAuditRecordReq(pVnode, ver, pReq, len, pMsg);
×
992
      pRsp->code = code;
×
993
      pRsp->msgType = TDMT_VND_AUDIT_RECORD_RSP;
×
994
      pRsp->pCont = NULL;
×
995
      pRsp->contLen = 0;
×
996
      if (code) {
×
997
        goto _err;
×
998
      }
999
      break;
×
1000
    default:
×
1001
      vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
×
1002
      return TSDB_CODE_INVALID_MSG;
×
1003
  }
1004

1005
  vGDebug(&pMsg->info.traceId, "vgId:%d, index:%" PRId64 ", msg processed, code:0x%x", TD_VID(pVnode), ver, pRsp->code);
80✔
1006

1007
  walApplyVer(pVnode->pWal, ver);
80✔
1008

1009
  if (pVnode->pTq) {
80✔
1010
    code = tqPushMsg(pVnode->pTq, pMsg->msgType);
80✔
1011
    if (code) {
80✔
1012
      vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
×
1013
      return code;
×
1014
    }
1015
  }
1016

1017
  // commit if need
1018
  if (needCommit) {
80✔
1019
    vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
4✔
1020
    code = vnodeAsyncCommit(pVnode, forceTrimWal);
4✔
1021
    if (code) {
4✔
1022
      vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
×
1023
      goto _err;
×
1024
    }
1025

1026
    // start a new one
1027
    METRICS_TIMING_BLOCK(pVnode->writeMetrics.memtable_wait_time, METRIC_LEVEL_LOW, {
4✔
1028
      code = vnodeBegin(pVnode);
1029
      if (code) {
1030
        vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
1031
        goto _err;
1032
      }
1033
    });
1034
  }
1035

1036
_exit:
80✔
1037
  return 0;
120✔
1038

1039
_err:
×
1040
  if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
1041
    vInfo("vgId:%d, process %s request failed since %s, lino 1:%d ver:%" PRId64, TD_VID(pVnode),
×
1042
          TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1043
  } else if (code) {
×
1044
    vError("vgId:%d, process %s request failed since %s, lino 2:%d ver:%" PRId64, TD_VID(pVnode),
×
1045
           TMSG_INFO(pMsg->msgType), tstrerror(code), lino, ver);
1046
  }
1047

1048
  return code;
×
1049
}
1050

1051
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
×
1052
  if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
×
1053
    return 0;
×
1054
  }
1055

1056
  int32_t qType = 0;
×
1057
  return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, (TDMT_SCH_QUERY == pMsg->msgType), &qType);
×
1058
}
1059

1060
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
1061
  const STraceId *trace = &pMsg->info.traceId;
×
1062
  vDebug("vgId:%d, msg:%s, %p in vnode query queue is processing, %" PRIx64 ":%" PRIx64, 
×
1063
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1064

1065
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !syncIsReadyForRead(pVnode->sync)) {
×
1066
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1067
    return 0;
×
1068
  }
1069

1070
  if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
×
1071
    vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
×
1072
    return 0;
×
1073
  }
1074

1075
  SReadHandle handle = {0};
×
1076
  handle.vnode = pVnode;
×
1077
  handle.pMsgCb = &pVnode->msgCb;
×
1078
  handle.pWorkerCb = pInfo->workerCb;
×
1079
  initStorageAPI(&handle.api);
×
1080
  int32_t code = TSDB_CODE_SUCCESS;
×
1081
  bool    redirected = false;
×
1082

1083
  switch (pMsg->msgType) {
×
1084
    case TDMT_SCH_QUERY:
×
1085
      if (!syncIsReadyForRead(pVnode->sync)) {
×
1086
        pMsg->code = (terrno) ? terrno : TSDB_CODE_SYN_NOT_LEADER;
×
1087
        redirected = true;
×
1088
      }
1089
      code = qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
×
1090
      if (redirected) {
×
1091
        vnodeRedirectRpcMsg(pVnode, pMsg, pMsg->code);
×
1092
        return 0;
×
1093
      }
1094

1095
      return code;
×
1096
    case TDMT_SCH_MERGE_QUERY:
×
1097
      return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
×
1098
    case TDMT_SCH_QUERY_CONTINUE:
×
1099
      return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
×
1100
    case TDMT_VND_TMQ_CONSUME:
×
1101
      return tqProcessPollReq(pVnode->pTq, pMsg);
×
1102
    case TDMT_VND_TMQ_CONSUME_PUSH:
×
1103
      return tqProcessPollPush(pVnode->pTq);
×
1104
    default:
×
1105
      vError("unknown msg type:%d in query queue", pMsg->msgType);
×
1106
      return TSDB_CODE_APP_ERROR;
×
1107
  }
1108
}
1109

1110
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
×
1111
  int32_t code = TSDB_CODE_SUCCESS;
×
1112
  const STraceId *trace = &pMsg->info.traceId;
×
1113
  vDebug("vgId:%d, msg:%s, %p in fetch queue is processing, %" PRIx64 ":%" PRIx64, 
×
1114
    pVnode->config.vgId, TMSG_INFO(pMsg->msgType), pMsg, TRACE_GET_ROOTID(trace), TRACE_GET_MSGID(trace));
1115
  if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
×
1116
       pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME ||
×
1117
       pMsg->msgType == TDMT_VND_VSUBTABLES_META || pMsg->msgType == TDMT_VND_VSTB_REF_DBS) &&
×
1118
      !syncIsReadyForRead(pVnode->sync)) {
×
1119
    vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
×
1120
    return 0;
×
1121
  }
1122

1123
  switch (pMsg->msgType) {
×
1124
    case TDMT_SCH_FETCH:
×
1125
    case TDMT_SCH_MERGE_FETCH:
1126
      return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1127
    case TDMT_SCH_FETCH_RSP:
×
1128
      return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1129
    // case TDMT_SCH_CANCEL_TASK:
1130
    //   return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
1131
    case TDMT_SCH_DROP_TASK:
×
1132
      return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1133
    case TDMT_SCH_TASK_NOTIFY:
×
1134
      return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1135
    case TDMT_SCH_QUERY_HEARTBEAT:
×
1136
      return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
×
1137
    case TDMT_VND_TABLE_META:
×
1138
    case TDMT_VND_TABLE_NAME:
1139
      return vnodeGetTableMeta(pVnode, pMsg, true);
×
1140
    case TDMT_VND_TABLE_CFG:
×
1141
      return vnodeGetTableCfg(pVnode, pMsg, true);
×
1142
    case TDMT_VND_BATCH_META: {
×
1143
      METRICS_TIMING_BLOCK(pVnode->writeMetrics.fetch_batch_meta_time, METRIC_LEVEL_LOW,
×
1144
                           { code = vnodeGetBatchMeta(pVnode, pMsg); });
1145
      METRICS_UPDATE(pVnode->writeMetrics.fetch_batch_meta_count, METRIC_LEVEL_LOW, 1);
×
1146
      return code;
×
1147
    }
1148
    case TDMT_VND_VSUBTABLES_META:
×
1149
      return vnodeGetVSubtablesMeta(pVnode, pMsg);
×
1150
    case TDMT_VND_VSTB_REF_DBS:
×
1151
      return vnodeGetVStbRefDbs(pVnode, pMsg);
×
1152
    case TDMT_VND_QUERY_SCAN_PROGRESS:
×
1153
      return vnodeQueryScanProgress(pVnode, pMsg);
×
1154
#ifdef TD_ENTERPRISE
1155
    case TDMT_VND_QUERY_COMPACT_PROGRESS:
×
1156
      return vnodeQueryCompactProgress(pVnode, pMsg);
×
1157

1158
    case TDMT_VND_LIST_SSMIGRATE_FILESETS:
×
1159
      return vnodeListSsMigrateFileSets(pVnode, pMsg);
×
1160

1161
    case TDMT_VND_QUERY_SSMIGRATE_PROGRESS:
×
1162
      return vnodeQuerySsMigrateProgress(pVnode, pMsg);
×
1163
#endif
1164
    case TDMT_VND_QUERY_TRIM_PROGRESS:
×
1165
      return vnodeQueryRetentionProgress(pVnode, pMsg);
×
1166
      //    case TDMT_VND_TMQ_CONSUME:
1167
      //      return tqProcessPollReq(pVnode->pTq, pMsg);
1168
#ifdef USE_TQ
1169
    case TDMT_VND_TMQ_VG_WALINFO:
×
1170
      return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
×
1171
    case TDMT_VND_TMQ_VG_COMMITTEDINFO:
×
1172
      return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
×
1173
    case TDMT_VND_TMQ_SEEK:
×
1174
      return tqProcessSeekReq(pVnode->pTq, pMsg);
×
1175
#endif
1176
    default:
×
1177
      vError("unknown msg type:%d in fetch queue", pMsg->msgType);
×
1178
      return TSDB_CODE_APP_ERROR;
×
1179
  }
1180
}
1181

1182
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
×
1183
  if (NULL == pMetaRsp) {
×
1184
    return;
×
1185
  }
1186

1187
  tstrncpy(pMetaRsp->dbFName, pVnode->config.dbname, TSDB_DB_FNAME_LEN);
×
1188
  pMetaRsp->dbId = pVnode->config.dbId;
×
1189
  pMetaRsp->vgId = TD_VID(pVnode);
×
1190
  pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
×
1191
}
1192

1193
extern int32_t vnodeAsyncRetention(SVnode *pVnode, STimeWindow tw, int8_t optrType, int8_t triggerType);
1194

1195
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1196
  if (!pVnode->restored) {
×
1197
    vInfo("vgId:%d, ignore trim req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
1198
    return 0;
×
1199
  }
1200

1201
  int32_t     code = 0;
×
1202
  SVTrimDbReq trimReq = {0};
×
1203

1204
  // decode
1205
  if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
×
1206
    code = TSDB_CODE_INVALID_MSG;
×
1207
    goto _exit;
×
1208
  }
1209

1210
  vInfo("vgId:%d, process trim vnode request, time:%" PRIi64 ",%" PRIi64 ", optr:%d, trigger:%d", pVnode->config.vgId,
×
1211
        trimReq.tw.skey, trimReq.tw.ekey, (int32_t)trimReq.optrType, (int32_t)trimReq.triggerType);
1212

1213
  code = vnodeAsyncRetention(pVnode, trimReq.tw, (int8_t)trimReq.optrType, (int8_t)trimReq.triggerType);
×
1214

1215
_exit:
×
1216
  return code;
×
1217
}
1218

1219
extern int32_t vnodeAsyncSsMigrateFileSet(SVnode *pVnode, SSsMigrateFileSetReq *pReq);
1220

1221
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
1222

1223
static int32_t vnodeProcessSsMigrateFileSetReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1224
  int32_t code = 0;
×
1225

1226
  SSsMigrateFileSetReq req = {0};
×
1227
  SSsMigrateFileSetRsp rsp = {0};
×
1228
  pRsp->msgType = TDMT_VND_SSMIGRATE_FILESET_RSP;
×
1229
  pRsp->code = 0;
×
1230
  pRsp->pCont = NULL;
×
1231
  pRsp->contLen = 0;
×
1232

1233
  if (tDeserializeSSsMigrateFileSetReq(pReq, len, &req) != 0) {
×
1234
    code = TSDB_CODE_INVALID_MSG;
×
1235
    goto _exit;
×
1236
  }
1237

1238
  vInfo("vgId:%d, ssmigrate:%d, fid:%d, process ssmigrate fileset request, time:%" PRId64, pVnode->config.vgId,
×
1239
        req.ssMigrateId, req.fid, req.startTimeSec);
1240
  rsp.ssMigrateId = req.ssMigrateId;
×
1241
  rsp.vgId = TD_VID(pVnode);
×
1242
  rsp.nodeId = req.nodeId;
×
1243
  rsp.fid = req.fid;
×
1244

1245
  code = vnodeAsyncSsMigrateFileSet(pVnode, &req);
×
1246
  if (code != TSDB_CODE_SUCCESS) {
×
1247
    vError("vgId:%d, failed to async ssmigrate since %s", TD_VID(pVnode), tstrerror(code));
×
1248
    pRsp->code = code;
×
1249
    goto _exit;
×
1250
  }
1251

1252
  pRsp->code = TSDB_CODE_SUCCESS;
×
1253
  pRsp->contLen = tSerializeSSsMigrateFileSetRsp(NULL, 0, &rsp);
×
1254
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1255
  if (pRsp->pCont == NULL) {
×
1256
    vError("vgId:%d, failed to allocate memory for ssmigrate fileset response", TD_VID(pVnode));
×
1257
    code = TSDB_CODE_OUT_OF_MEMORY;
×
1258
    goto _exit;
×
1259
  }
1260
  TAOS_UNUSED(tSerializeSSsMigrateFileSetRsp(pRsp->pCont, pRsp->contLen, &rsp));
×
1261

1262
_exit:
×
1263
  pRsp->code = code;
×
1264
  return code;
×
1265
}
1266

1267
extern int32_t vnodeFollowerSsMigrate(SVnode *pVnode, SSsMigrateProgress *pReq);
1268

1269
static int32_t vnodeProcessFollowerSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1270
  int32_t            code = 0;
×
1271
  SSsMigrateProgress req = {0};
×
1272

1273
  // decode
1274
  if (tDeserializeSSsMigrateProgress(pReq, len, &req) != 0) {
×
1275
    code = TSDB_CODE_INVALID_MSG;
×
1276
    goto _exit;
×
1277
  }
1278

1279
  code = vnodeFollowerSsMigrate(pVnode, &req);
×
1280

1281
_exit:
×
1282
  pRsp->code = code;
×
1283
  return code;
×
1284
}
1285

1286
extern int32_t vnodeKillSsMigrate(SVnode *pVnode, SVnodeKillSsMigrateReq *pReq);
1287

1288
static int32_t vnodeProcessKillSsMigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1289
  int32_t          code = 0;
×
1290
  SVnodeKillSsMigrateReq req = {0};
×
1291

1292
  // decode
1293
  if (tDeserializeSVnodeKillSsMigrateReq(pReq, len, &req) != 0) {
×
1294
    code = TSDB_CODE_INVALID_MSG;
×
1295
    goto _exit;
×
1296
  }
1297

1298
  code = vnodeKillSsMigrate(pVnode, &req);
×
1299

1300
_exit:
×
1301
  pRsp->code = code;
×
1302
  return code;
×
1303
}
1304

1305
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1306
  int               ret = 0;
×
1307
  SVDropTtlTableReq ttlReq = {0};
×
1308
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1309
    ret = TSDB_CODE_INVALID_MSG;
×
1310
    goto end;
×
1311
  }
1312

1313
  if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
×
1314
    ret = TSDB_CODE_INVALID_MSG;
×
1315
    goto end;
×
1316
  }
1317

1318
  if (ttlReq.nUids != 0) {
×
1319
    vInfo("vgId:%d, process drop ttl table request, time:%d, ntbUids:%d", pVnode->config.vgId, ttlReq.timestampSec,
×
1320
          ttlReq.nUids);
1321
  }
1322

1323
  if (ttlReq.nUids > 0) {
×
1324
    int32_t code = metaDropMultipleTables(pVnode->pMeta, ver, ttlReq.pTbUids);
×
1325
    if (code) return code;
×
1326

1327
    code = tqDeleteTbUidList(pVnode->pTq, ttlReq.pTbUids);
×
1328
    if (code) {
×
1329
      vError("vgId:%d, failed to delete tbUid list since %s", TD_VID(pVnode), tstrerror(code));
×
1330
    }
1331
  }
1332

1333
end:
×
1334
  taosArrayDestroy(ttlReq.pTbUids);
×
1335
  return ret;
×
1336
}
1337

1338
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1339
  int32_t                 code = -1;
×
1340
  SMetaReader             mr = {0};
×
1341
  SVDropTtlTableReq       ttlReq = {0};
×
1342
  SVFetchTtlExpiredTbsRsp rsp = {0};
×
1343
  SEncoder                encoder = {0};
×
1344
  SArray                 *pNames = NULL;
×
1345
  pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
×
1346
  pRsp->code = TSDB_CODE_SUCCESS;
×
1347
  pRsp->pCont = NULL;
×
1348
  pRsp->contLen = 0;
×
1349

1350
  if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
×
1351
    terrno = TSDB_CODE_INVALID_MSG;
×
1352
    goto _end;
×
1353
  }
1354

1355
  if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
×
1356
    terrno = TSDB_CODE_INVALID_MSG;
×
1357
    goto _end;
×
1358
  }
1359

1360
  tb_uid_t    suid;
1361
  char        ctbName[TSDB_TABLE_NAME_LEN];
1362
  SVDropTbReq expiredTb = {.igNotExists = true};
×
1363
  metaReaderDoInit(&mr, pVnode->pMeta, 0);
×
1364
  rsp.vgId = TD_VID(pVnode);
×
1365
  rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
×
1366
  if (!rsp.pExpiredTbs) goto _end;
×
1367

1368
  pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
×
1369
  if (!pNames) {
×
1370
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1371
    goto _end;
×
1372
  }
1373
  char buf[TSDB_TABLE_NAME_LEN];
1374
  for (int32_t i = 0; i < ttlReq.nUids; ++i) {
×
1375
    tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
×
1376
    expiredTb.suid = *uid;
×
1377
    terrno = metaReaderGetTableEntryByUid(&mr, *uid);
×
1378
    if (terrno < 0) goto _end;
×
1379
    tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
×
1380
    void *p = taosArrayPush(pNames, buf);
×
1381
    if (p == NULL) {
×
1382
      goto _end;
×
1383
    }
1384

1385
    expiredTb.name = p;
×
1386
    if (mr.me.type == TSDB_CHILD_TABLE) {
×
1387
      expiredTb.suid = mr.me.ctbEntry.suid;
×
1388
    }
1389

1390
    if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
×
1391
      goto _end;
×
1392
    }
1393
  }
1394

1395
  int32_t ret = 0;
×
1396
  tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
×
1397
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1398
  if (pRsp->pCont == NULL) {
×
1399
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1400
    code = -1;
×
1401
    goto _end;
×
1402
  }
1403
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1404
  terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
×
1405
  tEncoderClear(&encoder);
×
1406

1407
  if (terrno == 0) code = 0;
×
1408
_end:
×
1409
  metaReaderClear(&mr);
×
1410
  tFreeFetchTtlExpiredTbsRsp(&rsp);
×
1411
  taosArrayDestroy(ttlReq.pTbUids);
×
1412
  if (pNames) taosArrayDestroy(pNames);
×
1413
  pRsp->code = terrno;
×
1414
  return code;
×
1415
}
1416

1417
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
36✔
1418
  int32_t        code = 0;
36✔
1419
  SVCreateStbReq req = {0};
36✔
1420
  SDecoder       coder;
1421

1422
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
36✔
1423
  pRsp->code = TSDB_CODE_SUCCESS;
36✔
1424
  pRsp->pCont = NULL;
36✔
1425
  pRsp->contLen = 0;
36✔
1426

1427
  // decode and process req
1428
  tDecoderInit(&coder, pReq, len);
36✔
1429

1430
  code = tDecodeSVCreateStbReq(&coder, &req);
36✔
1431
  if (code) {
36✔
1432
    pRsp->code = code;
×
1433
    goto _err;
×
1434
  }
1435

1436
  code = metaCreateSuperTable(pVnode->pMeta, ver, &req);
36✔
1437
  if (code) {
36✔
1438
    pRsp->code = code;
×
1439
    goto _err;
×
1440
  }
1441

1442
  tDecoderClear(&coder);
36✔
1443
  return 0;
36✔
1444

1445
_err:
×
1446
  tDecoderClear(&coder);
×
1447
  return code;
×
1448
}
1449

1450
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1451
                                       SRpcMsg *pOriginRpc) {
1452
  SDecoder           decoder = {0};
×
1453
  SEncoder           encoder = {0};
×
1454
  int32_t            rcode = 0;
×
1455
  SVCreateTbBatchReq req = {0};
×
1456
  SVCreateTbReq     *pCreateReq;
1457
  SVCreateTbBatchRsp rsp = {0};
×
1458
  SVCreateTbRsp      cRsp = {0};
×
1459
  char               tbName[TSDB_TABLE_FNAME_LEN];
1460
  SArray            *tbUids = NULL;
×
1461
  SArray            *tbNames = NULL;
×
1462
  int64_t            tss = taosGetTimestampMs();
×
1463
  pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
×
1464
  pRsp->code = TSDB_CODE_SUCCESS;
×
1465
  pRsp->pCont = NULL;
×
1466
  pRsp->contLen = 0;
×
1467

1468
  // decode
1469
  tDecoderInit(&decoder, pReq, len);
×
1470
  if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
×
1471
    rcode = -1;
×
1472
    terrno = TSDB_CODE_INVALID_MSG;
×
1473
    goto _exit;
×
1474
  }
1475

1476
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
×
1477
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
×
1478
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
×
1479
  if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
×
1480
    rcode = -1;
×
1481
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1482
    goto _exit;
×
1483
  }
1484

1485
  // loop to create table
1486
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1487
    pCreateReq = req.pReqs + iReq;
×
1488
    memset(&cRsp, 0, sizeof(cRsp));
×
1489

1490
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
×
1491
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1492
      if (str == NULL) {
×
1493
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1494
        rcode = -1;
×
1495
        goto _exit;
×
1496
      }
1497
      tstrncpy(str, pCreateReq->name, TSDB_TABLE_FNAME_LEN);
×
1498
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1499
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1500
        rcode = -1;
×
1501
        goto _exit;
×
1502
      }
1503
    }
1504

1505
    // validate hash
1506
    (void)tsnprintf(tbName, TSDB_TABLE_FNAME_LEN, "%s.%s", pVnode->config.dbname, pCreateReq->name);
×
1507
    if (vnodeValidateTableHash(pVnode, tbName) < 0) {
×
1508
      cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
×
1509
      if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1510
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1511
        rcode = -1;
×
1512
        goto _exit;
×
1513
      }
1514
      vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
×
1515
      continue;
×
1516
    }
1517

1518
    // do create table
1519
    if (metaCreateTable2(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
×
1520
      if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
1521
        cRsp.code = TSDB_CODE_SUCCESS;
×
1522
      } else {
1523
        cRsp.code = terrno;
×
1524
      }
1525
    } else {
1526
      cRsp.code = TSDB_CODE_SUCCESS;
×
1527
      if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
×
1528
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1529
        rcode = -1;
×
1530
        goto _exit;
×
1531
      }
1532
      vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
×
1533
    }
1534

1535
    if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
×
1536
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1537
      rcode = -1;
×
1538
      goto _exit;
×
1539
    }
1540
  }
1541

1542
  vTrace("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
×
1543
  if (tqAddTbUidList(pVnode->pTq, tbUids) < 0) {
×
1544
    vError("vgId:%d, failed to add tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1545
  }
1546

1547
  // prepare rsp
1548
  int32_t ret = 0;
×
1549
  tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
×
1550
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1551
  if (pRsp->pCont == NULL) {
×
1552
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1553
    rcode = -1;
×
1554
    goto _exit;
×
1555
  }
1556
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1557
  if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
×
1558
    vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
×
1559
  }
1560

1561
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
×
1562
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1563

1564
    SName name = {0};
×
1565
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
×
1566
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1567
    }
1568

1569
    SStringBuilder sb = {0};
×
1570
    for (int32_t i = 0; i < tbNames->size; i++) {
×
1571
      char **key = (char **)taosArrayGet(tbNames, i);
×
1572
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1573
      if (i < tbNames->size - 1) {
×
1574
        taosStringBuilderAppendChar(&sb, ',');
×
1575
      }
1576
      // taosMemoryFreeClear(*key);
1577
    }
1578

1579
    size_t len = 0;
×
1580
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1581

1582
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
1583
      int64_t tse = taosGetTimestampMs();
×
1584
      double  duration = (double)(tse - tss);
×
1585
      duration = duration / 1000;
×
1586
      auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len, duration, 0);
×
1587
    }
1588

1589
    taosStringBuilderDestroy(&sb);
×
1590
  }
1591

1592
_exit:
×
1593
  tDeleteSVCreateTbBatchReq(&req);
×
1594
  taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
×
1595
  taosArrayDestroy(tbUids);
×
1596
  tDecoderClear(&decoder);
×
1597
  tEncoderClear(&encoder);
×
1598
  taosArrayDestroyP(tbNames, NULL);
×
1599
  return rcode;
×
1600
}
1601

1602
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
32✔
1603
  int32_t        code = 0;
32✔
1604
  SVCreateStbReq req = {0};
32✔
1605
  SDecoder       dc = {0};
32✔
1606

1607
  pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
32✔
1608
  pRsp->code = TSDB_CODE_SUCCESS;
32✔
1609
  pRsp->pCont = NULL;
32✔
1610
  pRsp->contLen = 0;
32✔
1611

1612
  tDecoderInit(&dc, pReq, len);
32✔
1613

1614
  // decode req
1615
  code = tDecodeSVCreateStbReq(&dc, &req);
32✔
1616
  if (code) {
32✔
1617
    tDecoderClear(&dc);
×
1618
    return code;
×
1619
  }
1620

1621
  code = metaAlterSuperTable(pVnode->pMeta, ver, &req);
32✔
1622
  if (code) {
32✔
1623
    pRsp->code = code;
×
1624
    tDecoderClear(&dc);
×
1625
    return code;
×
1626
  }
1627

1628
  tDecoderClear(&dc);
32✔
1629

1630
  return 0;
32✔
1631
}
1632

1633
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp, SRpcMsg *pOriginRpc) {
8✔
1634
  SVDropStbReq req = {0};
8✔
1635
  int32_t      rcode = TSDB_CODE_SUCCESS;
8✔
1636
  SDecoder     decoder = {0};
8✔
1637
  SArray      *tbUidList = NULL;
8✔
1638

1639
  pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
8✔
1640
  pRsp->pCont = NULL;
8✔
1641
  pRsp->contLen = 0;
8✔
1642

1643
  // decode request
1644
  tDecoderInit(&decoder, pReq, len);
8✔
1645
  if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
8✔
1646
    rcode = TSDB_CODE_INVALID_MSG;
×
1647
    goto _exit;
×
1648
  }
1649

1650
  STraceId* trace = &(pOriginRpc->info.traceId);
8✔
1651

1652
  vInfo("vgId:%d, start to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
8✔
1653
              trace ? trace->msgId : 0, req.name);
1654

1655
  // process request
1656
  tbUidList = taosArrayInit(8, sizeof(int64_t));
8✔
1657
  if (tbUidList == NULL) goto _exit;
8✔
1658
  if (metaDropSuperTable(pVnode->pMeta, ver, &req) < 0) {
8✔
1659
    rcode = terrno;
×
1660
    goto _exit;
×
1661
  }
1662

1663
  if (tqDeleteTbUidList(pVnode->pTq, tbUidList) < 0) {
8✔
1664
    rcode = terrno;
×
1665
    goto _exit;
×
1666
  }
1667

1668
  // return rsp
1669
_exit:
8✔
1670
  vInfo("vgId:%d, finished to process vnode-drop-stb, QID:0x%" PRIx64 ":0x%" PRIx64 ", drop stb:%s", TD_VID(pVnode), trace ? trace->rootId : 0, 
8✔
1671
              trace ? trace->msgId : 0, req.name);
1672
  if (tbUidList) taosArrayDestroy(tbUidList);
8✔
1673
  pRsp->code = rcode;
8✔
1674
  tDecoderClear(&decoder);
8✔
1675
  return 0;
8✔
1676
}
1677

1678
static void alterTagForTmq(SVnode *pVnode, SVAlterTbReq *vAlterTbReq) {
×
1679
  int32_t       code = 0;
×
1680
  int32_t       lino = 0;
×
1681
  SArray* tbUids = NULL;
×
1682
  SArray* cidList = NULL;
×
1683

1684
  int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, vAlterTbReq->tbName);
×
1685
  QUERY_CHECK_CONDITION(uid != 0, code, lino, end, TSDB_CODE_TDB_TABLE_NOT_EXIST);
×
1686
  
1687
  tbUids = taosArrayInit(4, sizeof(int64_t));
×
1688
  QUERY_CHECK_NULL(tbUids, code, lino, end, terrno);
×
1689

1690
  QUERY_CHECK_CONDITION(taosArrayPush(tbUids, &uid) != NULL, code, lino, end, terrno);
×
1691

1692
  cidList = taosArrayInit(4, sizeof(col_id_t));
×
1693
  QUERY_CHECK_NULL(cidList, code, lino, end, terrno);
×
1694

1695
  if (vAlterTbReq->action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL){
×
1696
    col_id_t cid = vAlterTbReq->colId;
×
1697
    QUERY_CHECK_CONDITION(taosArrayPush(cidList, &cid) != NULL, code, lino, end, terrno);
×
1698
  } else {
1699
    for (int32_t i = 0; i < taosArrayGetSize(vAlterTbReq->pMultiTag); i++) {
×
1700
      SMultiTagUpateVal *pTagVal = taosArrayGet(vAlterTbReq->pMultiTag, i);
×
1701
      QUERY_CHECK_NULL(pTagVal, code, lino, end, terrno);
×
1702
      col_id_t cid = pTagVal->colId;
×
1703
      QUERY_CHECK_CONDITION(taosArrayPush(cidList, &cid) != NULL, code, lino, end, terrno);
×
1704
    }
1705
  }
1706
  
1707
  vDebug("vgId:%d, try to add table:%s in query table list, cidList size:%"PRIzu, TD_VID(pVnode), vAlterTbReq->tbName, taosArrayGetSize(cidList));
×
1708
  code = tqUpdateTbUidList(pVnode->pTq, tbUids, cidList);
×
1709
  QUERY_CHECK_CODE(code, lino, end);
×
1710

1711
end:
×
1712
  if (code != 0) {
×
1713
    qError("vgId:%d, failed to alter table:%s since %s", TD_VID(pVnode), vAlterTbReq->tbName, tstrerror(code));
×
1714
  }
1715
  taosArrayDestroy(tbUids);
×
1716
  taosArrayDestroy(cidList);
×
1717
}
×
1718

1719
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1720
  SVAlterTbReq  vAlterTbReq = {0};
×
1721
  SVAlterTbRsp  vAlterTbRsp = {0};
×
1722
  SDecoder      dc = {0};
×
1723
  int32_t       code = 0;
×
1724
  int32_t       lino = 0;
×
1725
  int32_t       ret;
1726
  SEncoder      ec = {0};
×
1727
  STableMetaRsp vMetaRsp = {0};
×
1728

1729
  pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
×
1730
  pRsp->pCont = NULL;
×
1731
  pRsp->contLen = 0;
×
1732
  pRsp->code = TSDB_CODE_SUCCESS;
×
1733

1734
  tDecoderInit(&dc, pReq, len);
×
1735

1736
  // decode
1737
  if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
×
1738
    vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
×
1739
    tDecoderClear(&dc);
×
1740
    goto _exit;
×
1741
  }
1742

1743
  // process
1744
  if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
×
1745
    vAlterTbRsp.code = terrno;
×
1746
    tDecoderClear(&dc);
×
1747
    goto _exit;
×
1748
  }
1749
  tDecoderClear(&dc);
×
1750

1751
  if (NULL != vMetaRsp.pSchemas) {
×
1752
    vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
×
1753
    vAlterTbRsp.pMeta = &vMetaRsp;
×
1754
  }
1755

1756
  if (vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_TAG_VAL || vAlterTbReq.action == TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL) {
×
1757
    alterTagForTmq(pVnode, &vAlterTbReq);
×
1758
  }
1759

1760
_exit:
×
1761
  taosArrayDestroy(vAlterTbReq.pMultiTag);
×
1762
  tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
×
1763
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1764
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
1765
  if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
×
1766
    vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
×
1767
  }
1768

1769
  tEncoderClear(&ec);
×
1770
  if (vMetaRsp.pSchemas) {
×
1771
    taosMemoryFree(vMetaRsp.pSchemas);
×
1772
    taosMemoryFree(vMetaRsp.pSchemaExt);
×
1773
  }
1774
  if (vMetaRsp.pColRefs) {
×
1775
    taosMemoryFree(vMetaRsp.pColRefs);
×
1776
  }
1777
  return 0;
×
1778
}
1779

1780
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
1781
                                     SRpcMsg *pOriginRpc) {
1782
  SVDropTbBatchReq req = {0};
×
1783
  SVDropTbBatchRsp rsp = {0};
×
1784
  SDecoder         decoder = {0};
×
1785
  SEncoder         encoder = {0};
×
1786
  int32_t          ret;
1787
  SArray          *tbUids = NULL;
×
1788
  SArray          *tbNames = NULL;
×
1789
  int64_t          tss = taosGetTimestampMs();
×
1790

1791
  pRsp->msgType = ((SRpcMsg *)pReq)->msgType + 1;
×
1792
  pRsp->pCont = NULL;
×
1793
  pRsp->contLen = 0;
×
1794
  pRsp->code = TSDB_CODE_SUCCESS;
×
1795

1796
  // decode req
1797
  tDecoderInit(&decoder, pReq, len);
×
1798
  ret = tDecodeSVDropTbBatchReq(&decoder, &req);
×
1799
  if (ret < 0) {
×
1800
    terrno = TSDB_CODE_INVALID_MSG;
×
1801
    pRsp->code = terrno;
×
1802
    goto _exit;
×
1803
  }
1804

1805
  // process req
1806
  tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
×
1807
  rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
×
1808
  tbNames = taosArrayInit(req.nReqs, sizeof(char *));
×
1809
  if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
×
1810

1811
  for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1812
    SVDropTbReq *pDropTbReq = req.pReqs + iReq;
×
1813
    SVDropTbRsp  dropTbRsp = {0};
×
1814
    tb_uid_t     tbUid = 0;
×
1815

1816
    /* code */
1817
    ret = metaDropTable2(pVnode->pMeta, ver, pDropTbReq);
×
1818
    if (ret < 0) {
×
1819
      if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
×
1820
        dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1821
      } else {
1822
        dropTbRsp.code = terrno;
×
1823
      }
1824
    } else {
1825
      dropTbRsp.code = TSDB_CODE_SUCCESS;
×
1826
    }
1827

1828
    if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
×
1829
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1830
      pRsp->code = terrno;
×
1831
      goto _exit;
×
1832
    }
1833

1834
    if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
×
1835
      char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
×
1836
      if (str == NULL) {
×
1837
        pRsp->code = terrno;
×
1838
        goto _exit;
×
1839
      }
1840
      tstrncpy(str, pDropTbReq->name, TSDB_TABLE_FNAME_LEN);
×
1841
      if (taosArrayPush(tbNames, &str) == NULL) {
×
1842
        terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1843
        pRsp->code = terrno;
×
1844
        goto _exit;
×
1845
      }
1846
    }
1847
  }
1848

1849
  if (tqDeleteTbUidList(pVnode->pTq, tbUids) < 0) {
×
1850
    vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
×
1851
  }
1852

1853
  if (tsEnableAudit && tsEnableAuditCreateTable && tsAuditLevel >= AUDIT_LEVEL_CHILDTABLE) {
×
1854
    int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
×
1855

1856
    SName name = {0};
×
1857
    if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
×
1858
      vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
×
1859
    }
1860

1861
    SStringBuilder sb = {0};
×
1862
    for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
×
1863
      char **key = (char **)taosArrayGet(tbNames, iReq);
×
1864
      taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
×
1865
      if (iReq < req.nReqs - 1) {
×
1866
        taosStringBuilderAppendChar(&sb, ',');
×
1867
      }
1868
      taosMemoryFreeClear(*key);
×
1869
    }
1870

1871
    size_t len = 0;
×
1872
    char  *keyJoined = taosStringBuilderGetResult(&sb, &len);
×
1873

1874
    if (strlen(RPC_MSG_USER(pOriginRpc)) > 0) {
×
1875
      int64_t tse = taosGetTimestampMs();
×
1876
      double  duration = (double)(tse - tss);
×
1877
      duration = duration / 1000;
×
1878
      auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len, duration, 0);
×
1879
    }
1880

1881
    taosStringBuilderDestroy(&sb);
×
1882
  }
1883

1884
_exit:
×
1885
  taosArrayDestroy(tbUids);
×
1886
  tDecoderClear(&decoder);
×
1887
  tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
×
1888
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
1889
  tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
×
1890
  if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
×
1891
    vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
×
1892
  }
1893
  tEncoderClear(&encoder);
×
1894
  taosArrayDestroy(rsp.pArray);
×
1895
  taosArrayDestroy(tbNames);
×
1896
  return 0;
×
1897
}
1898

1899
static int32_t vnodeProcessCreateRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1900
  int32_t         code = 0, lino = 0;
×
1901
  SVCreateRsmaReq req = {0};
×
1902
  TAOS_CHECK_EXIT(tDeserializeSVCreateRsmaReq(pReq, len, &req));
×
1903
  TAOS_CHECK_EXIT(metaCreateRsma(pVnode->pMeta, ver, &req));
×
1904
_exit:
×
1905
  pRsp->msgType = TDMT_VND_CREATE_RSMA_RSP;
×
1906
  pRsp->pCont = NULL;
×
1907
  pRsp->code = code;
×
1908
  pRsp->contLen = 0;
×
1909
  tFreeSVCreateRsmaReq(&req);
×
1910
  return code;
×
1911
}
1912

1913
static int32_t vnodeProcessDropRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1914
  int32_t       code = 0, lino = 0;
×
1915
  SVDropRsmaReq req = {0};
×
1916
  TAOS_CHECK_EXIT(tDeserializeSVDropRsmaReq(pReq, len, &req));
×
1917
  TAOS_CHECK_EXIT(metaDropRsma(pVnode->pMeta, ver, &req));
×
1918
_exit:
×
1919
  pRsp->msgType = TDMT_VND_DROP_RSMA_RSP;
×
1920
  pRsp->pCont = NULL;
×
1921
  pRsp->code = code;
×
1922
  pRsp->contLen = 0;
×
1923
  return 0;
×
1924
}
1925

1926
static int32_t vnodeProcessAlterRsmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
1927
  int32_t         code = 0, lino = 0;
×
1928
  SVAlterRsmaReq req = {0};
×
1929
  TAOS_CHECK_EXIT(tDeserializeSVAlterRsmaReq(pReq, len, &req));
×
1930
  TAOS_CHECK_EXIT(metaAlterRsma(pVnode->pMeta, ver, &req));
×
1931
_exit:
×
1932
  pRsp->msgType = TDMT_VND_ALTER_RSMA_RSP;
×
1933
  pRsp->pCont = NULL;
×
1934
  pRsp->code = code;
×
1935
  pRsp->contLen = 0;
×
1936
  tFreeSVAlterRsmaReq(&req);
×
1937
  return code;
×
1938
}
1939

1940
#ifdef BUILD_NO_CALL
1941
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
1942
                                              const char *tags) {
1943
  SSubmitBlkIter blkIter = {0};
1944
  STSchema      *pSchema = NULL;
1945
  tb_uid_t       suid = 0;
1946
  STSRow        *row = NULL;
1947
  int32_t        rv = -1;
1948

1949
  tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
1950
  if (blkIter.row == NULL) return 0;
1951

1952
  int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
1953
                                         &pSchema);  // TODO: use the real schema
1954
  if (TSDB_CODE_SUCCESS != code) {
1955
    printf("%s:%d no valid schema\n", tags, __LINE__);
1956
    return code;
1957
  }
1958

1959
  suid = msgIter->suid;
1960
  rv = TD_ROW_SVER(blkIter.row);
1961

1962
  char __tags[128] = {0};
1963
  snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
1964
  while ((row = tGetSubmitBlkNext(&blkIter))) {
1965
    tdSRowPrint(row, pSchema, __tags);
1966
  }
1967

1968
  taosMemoryFreeClear(pSchema);
1969

1970
  return TSDB_CODE_SUCCESS;
1971
}
1972
#endif
1973
typedef struct SSubmitReqConvertCxt {
1974
  SSubmitMsgIter msgIter;
1975
  SSubmitBlk    *pBlock;
1976
  SSubmitBlkIter blkIter;
1977
  STSRow        *pRow;
1978
  STSRowIter     rowIter;
1979
  SSubmitTbData *pTbData;
1980
  STSchema      *pTbSchema;
1981
  SArray        *pColValues;
1982
} SSubmitReqConvertCxt;
1983

1984
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
×
1985
  taosMemoryFreeClear(pCxt->pTbSchema);
×
1986
  int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
×
1987
                                         pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
1988
  if (TSDB_CODE_SUCCESS != code) {
×
1989
    return code;
×
1990
  }
1991
  tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
×
1992

1993
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
1994
  if (NULL == pCxt->pTbData) {
×
1995
    pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
×
1996
    if (NULL == pCxt->pTbData) {
×
1997
      return terrno;
×
1998
    }
1999
  }
2000
  pCxt->pTbData->flags = 0;
×
2001
  pCxt->pTbData->suid = pCxt->msgIter.suid;
×
2002
  pCxt->pTbData->uid = pCxt->msgIter.uid;
×
2003
  pCxt->pTbData->sver = pCxt->msgIter.sversion;
×
2004
  pCxt->pTbData->pCreateTbReq = NULL;
×
2005
  pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
×
2006
  if (NULL == pCxt->pTbData->aRowP) {
×
2007
    return terrno;
×
2008
  }
2009

2010
  taosArrayDestroy(pCxt->pColValues);
×
2011
  pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
×
2012
  if (NULL == pCxt->pColValues) {
×
2013
    return terrno;
×
2014
  }
2015
  for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
×
2016
    SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
×
2017
    if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
×
2018
      return terrno;
×
2019
    }
2020
  }
2021

2022
  return TSDB_CODE_SUCCESS;
×
2023
}
2024

2025
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
×
2026
  taosMemoryFreeClear(pCxt->pTbSchema);
×
2027
  tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
×
2028
  taosMemoryFreeClear(pCxt->pTbData);
×
2029
  taosArrayDestroy(pCxt->pColValues);
×
2030
}
×
2031

2032
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
×
2033
  if (tdValTypeIsNone(pCellVal->valType)) {
×
2034
    pColVal->flag = CV_FLAG_NONE;
×
2035
    return TSDB_CODE_SUCCESS;
×
2036
  }
2037

2038
  if (tdValTypeIsNull(pCellVal->valType)) {
×
2039
    pColVal->flag = CV_FLAG_NULL;
×
2040
    return TSDB_CODE_SUCCESS;
×
2041
  }
2042

2043
  if (IS_VAR_DATA_TYPE(pCol->type)) {
×
2044
    if (IS_STR_DATA_BLOB(pCol->type)) {
×
2045
      pColVal->value.nData = blobDataLen(pCellVal->val);
×
2046
      pColVal->value.pData = (uint8_t *)blobDataVal(pCellVal->val);
×
2047

2048
    } else {
2049
      pColVal->value.nData = varDataLen(pCellVal->val);
×
2050
      pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
×
2051
    }
2052
  } else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
×
2053
    float f = GET_FLOAT_VAL(pCellVal->val);
×
2054
    valueSetDatum(&pColVal->value, pCol->type, &f, sizeof(f));
×
2055
  } else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
×
2056
    taosSetPInt64Aligned(&pColVal->value.val, (int64_t *)pCellVal->val);
×
2057
  } else {
2058
    valueSetDatum(&pColVal->value, pCol->type, pCellVal->val, tDataTypes[pCol->type].bytes);
×
2059
  }
2060

2061
  pColVal->flag = CV_FLAG_VALUE;
×
2062
  return TSDB_CODE_SUCCESS;
×
2063
}
2064

2065
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
×
2066
  int32_t code = TSDB_CODE_SUCCESS;
×
2067
  tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
×
2068
  for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
×
2069
    STColumn *pCol = pCxt->pTbSchema->columns + i;
×
2070
    SCellVal  cellVal = {0};
×
2071
    if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
×
2072
      break;
×
2073
    }
2074
    code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
×
2075
  }
2076
  return code;
×
2077
}
2078

2079
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
×
2080
  if (pCxt->msgIter.schemaLen <= 0) {
×
2081
    return TSDB_CODE_SUCCESS;
×
2082
  }
2083

2084
  pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2085
  if (NULL == pCxt->pTbData->pCreateTbReq) {
×
2086
    return terrno;
×
2087
  }
2088

2089
  SDecoder decoder = {0};
×
2090
  tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
×
2091
  int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
×
2092
  tDecoderClear(&decoder);
×
2093

2094
  return code;
×
2095
}
2096

2097
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
×
2098
  pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
×
2099
  if (NULL == pReq2->aSubmitTbData) {
×
2100
    return terrno;
×
2101
  }
2102

2103
  SSubmitReqConvertCxt cxt = {0};
×
2104

2105
  int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
×
2106
  while (TSDB_CODE_SUCCESS == code) {
×
2107
    code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
×
2108
    if (TSDB_CODE_SUCCESS == code) {
×
2109
      if (NULL == cxt.pBlock) {
×
2110
        break;
×
2111
      }
2112
      code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
×
2113
    }
2114
    if (TSDB_CODE_SUCCESS == code) {
×
2115
      code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
×
2116
    }
2117
    if (TSDB_CODE_SUCCESS == code) {
×
2118
      code = vnodeDecodeCreateTbReq(&cxt);
×
2119
    }
2120
    while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
×
2121
      code = vnodeTSRowConvertToColValArray(&cxt);
×
2122
      if (TSDB_CODE_SUCCESS == code) {
×
2123
        SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
×
2124

2125
        SRowBuildScanInfo sinfo = {0};
×
2126
        code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow, &sinfo);
×
2127
      }
2128
    }
2129
    if (TSDB_CODE_SUCCESS == code) {
×
2130
      code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
×
2131
    }
2132
    if (TSDB_CODE_SUCCESS == code) {
×
2133
      taosMemoryFreeClear(cxt.pTbData);
×
2134
    }
2135
  }
2136

2137
  vnodeDestroySubmitReqConvertCxt(&cxt);
×
2138
  return code;
×
2139
}
2140

2141
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
×
2142
  int32_t  code = TSDB_CODE_SUCCESS;
×
2143
  char    *pMsg = NULL;
×
2144
  uint32_t msglen = 0;
×
2145
  tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
×
2146
  if (TSDB_CODE_SUCCESS == code) {
×
2147
    pMsg = taosMemoryMalloc(msglen);
×
2148
    if (NULL == pMsg) {
×
2149
      code = terrno;
×
2150
    }
2151
  }
2152
  if (TSDB_CODE_SUCCESS == code) {
×
2153
    SEncoder encoder;
2154
    tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
×
2155
    code = tEncodeSubmitReq(&encoder, pSubmitReq);
×
2156
    tEncoderClear(&encoder);
×
2157
  }
2158
  if (TSDB_CODE_SUCCESS == code) {
×
2159
    *ppMsg = pMsg;
×
2160
  }
2161
  return code;
×
2162
}
2163

2164
static int32_t buildExistSubTalbeRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
×
2165
  int32_t code = 0;
×
2166
  int32_t lino = 0;
×
2167

2168
  SMetaEntry *pEntry = NULL;
×
2169
  code = metaFetchEntryByUid(pVnode->pMeta, pSubmitTbData->suid, &pEntry);
×
2170
  if (code) {
×
2171
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2172
    TSDB_CHECK_CODE(code, lino, _exit);
×
2173
  }
2174
  if (pEntry->type != TSDB_SUPER_TABLE) {
×
2175
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not super table, line:%d", TD_VID(pVnode), pSubmitTbData->uid,
×
2176
           __LINE__);
2177
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2178
    TSDB_CHECK_CODE(code, lino, _exit);
×
2179
  }
2180

2181
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
×
2182
  if (NULL == *ppRsp) {
×
2183
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2184
  }
2185
  (*ppRsp)->suid = pSubmitTbData->suid;
×
2186
  (*ppRsp)->tuid = pSubmitTbData->uid;
×
2187
  (*ppRsp)->sversion = pEntry->stbEntry.schemaRow.version;
×
2188
  (*ppRsp)->vgId = pVnode->config.vgId;
×
2189
  (*ppRsp)->numOfColumns = pEntry->stbEntry.schemaRow.nCols;
×
2190
  (*ppRsp)->numOfTags = pEntry->stbEntry.schemaTag.nCols;
×
2191
  (*ppRsp)->pSchemas =
×
2192
      taosMemoryCalloc(pEntry->stbEntry.schemaRow.nCols + pEntry->stbEntry.schemaTag.nCols, sizeof(SSchema));
×
2193
  if (NULL == (*ppRsp)->pSchemas) {
×
2194
    taosMemoryFree(*ppRsp);
×
2195
    *ppRsp = NULL;
×
2196
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2197
  }
2198
  memcpy((*ppRsp)->pSchemas, pEntry->stbEntry.schemaRow.pSchema, pEntry->stbEntry.schemaRow.nCols * sizeof(SSchema));
×
2199
  memcpy((*ppRsp)->pSchemas + pEntry->stbEntry.schemaRow.nCols, pEntry->stbEntry.schemaTag.pSchema,
×
2200
         pEntry->stbEntry.schemaTag.nCols * sizeof(SSchema));
×
2201
  if (pEntry->pExtSchemas != NULL) {
×
2202
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->colCmpr.nCols, sizeof(SExtSchema));
×
2203
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2204
      taosMemoryFree((*ppRsp)->pSchemas);
×
2205
      taosMemoryFree(*ppRsp);
×
2206
      *ppRsp = NULL;
×
2207
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2208
    }
2209
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->colCmpr.nCols * sizeof(SExtSchema));
×
2210
  }
2211

2212
  if (pEntry->stbEntry.schemaRow.version != pSubmitTbData->sver) {
×
2213
    TSDB_CHECK_CODE(code = TSDB_CODE_TDB_TABLE_ALREADY_EXIST, lino, _exit);
×
2214
  }
2215
_exit:
×
2216
  metaFetchEntryFree(&pEntry);
×
2217
  if (code != TSDB_CODE_SUCCESS) {
×
2218
    vError("vgId:%d, failed to build exist sub table response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2219
  }
2220
  return code;
×
2221
}
2222

2223
static int32_t buildExistNormalTalbeRsp(SVnode *pVnode, int64_t uid, STableMetaRsp **ppRsp) {
×
2224
  int32_t code = 0;
×
2225
  int32_t lino = 0;
×
2226

2227
  SMetaEntry *pEntry = NULL;
×
2228
  code = metaFetchEntryByUid(pVnode->pMeta, uid, &pEntry);
×
2229
  if (code) {
×
2230
    vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2231
    TSDB_CHECK_CODE(code, lino, _exit);
×
2232
  }
2233
  if (pEntry->type != TSDB_NORMAL_TABLE) {
×
2234
    vError("vgId:%d, table uid:%" PRId64 " exists, but is not normal table, line:%d", TD_VID(pVnode), uid, __LINE__);
×
2235
    code = TSDB_CODE_STREAM_INSERT_SCHEMA_NOT_MATCH;
×
2236
    TSDB_CHECK_CODE(code, lino, _exit);
×
2237
  }
2238

2239
  *ppRsp = taosMemoryCalloc(1, sizeof(STableMetaRsp));
×
2240
  if (NULL == *ppRsp) {
×
2241
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2242
  }
2243

2244
  (*ppRsp)->tuid = pEntry->uid;
×
2245
  (*ppRsp)->vgId = pVnode->config.vgId;
×
2246
  (*ppRsp)->sversion = pEntry->ntbEntry.schemaRow.version;
×
2247
  (*ppRsp)->numOfColumns = pEntry->ntbEntry.schemaRow.nCols;
×
2248
  (*ppRsp)->pSchemas = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchema));
×
2249
  if (NULL == (*ppRsp)->pSchemas) {
×
2250
    taosMemoryFree(*ppRsp);
×
2251
    TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2252
  }
2253
  memcpy((*ppRsp)->pSchemas, pEntry->ntbEntry.schemaRow.pSchema, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchema));
×
2254
  if (pEntry->pExtSchemas != NULL) {
×
2255
    (*ppRsp)->pSchemaExt = taosMemoryCalloc(pEntry->ntbEntry.schemaRow.nCols, sizeof(SSchemaExt));
×
2256
    if (NULL == (*ppRsp)->pSchemaExt) {
×
2257
      taosMemoryFree((*ppRsp)->pSchemas);
×
2258
      taosMemoryFree(*ppRsp);
×
2259
      TSDB_CHECK_CODE(code = terrno, lino, _exit);
×
2260
    }
2261
    memcpy((*ppRsp)->pSchemaExt, pEntry->pExtSchemas, pEntry->ntbEntry.schemaRow.nCols * sizeof(SSchemaExt));
×
2262
  }
2263

2264
_exit:
×
2265
  metaFetchEntryFree(&pEntry);
×
2266
  if (code != TSDB_CODE_SUCCESS) {
×
2267
    vError("vgId:%d, failed to build exist normal table response, code:%d, line:%d", TD_VID(pVnode), code, lino);
×
2268
  }
2269
  return code;
×
2270
}
2271

2272
static int32_t buildExistTableInStreamRsp(SVnode *pVnode, SSubmitTbData *pSubmitTbData, STableMetaRsp **ppRsp) {
×
2273
  if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_NORMAL_TB_IN_STREAM) {
×
2274
    int32_t code = buildExistNormalTalbeRsp(pVnode, pSubmitTbData->uid, ppRsp);
×
2275
    if (code) {
×
2276
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pSubmitTbData->uid, __LINE__);
×
2277
      return code;
×
2278
    }
2279
    return TSDB_CODE_TDB_TABLE_ALREADY_EXIST;
×
2280
  } else if (pSubmitTbData->pCreateTbReq->flags & TD_CREATE_SUB_TB_IN_STREAM) {
×
2281
    return buildExistSubTalbeRsp(pVnode, pSubmitTbData, ppRsp);
×
2282
  }
2283
  return TSDB_CODE_SUCCESS;
×
2284
}
2285

2286
static int32_t vnodeHandleAutoCreateTable(SVnode      *pVnode,    // vnode
×
2287
                                          int64_t      version,   // version
2288
                                          SSubmitReq2 *pRequest,  // request
2289
                                          SSubmitRsp2 *pResponse  // response
2290
) {
2291
  int32_t code = TSDB_CODE_SUCCESS;
×
2292
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
×
2293
  SArray *newTbUids = NULL;
×
2294

2295
  for (int32_t i = 0; i < numTbData; ++i) {
×
2296
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
×
2297

2298
    if (pTbData->pCreateTbReq == NULL) {
×
2299
      continue;
×
2300
    }
2301

2302
    pTbData->uid = pTbData->pCreateTbReq->uid;
×
2303

2304
    // Alloc necessary resources
2305
    if (pResponse->aCreateTbRsp == NULL) {
×
2306
      pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
2307
      if (pResponse->aCreateTbRsp == NULL) {
×
2308
        code = terrno;
×
2309
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2310
               tstrerror(code), version);
2311
        taosArrayDestroy(newTbUids);
×
2312
        return code;
×
2313
      }
2314
    }
2315

2316
    // Do create table
2317
    vDebug("vgId:%d start to handle auto create table, version:%" PRId64, TD_VID(pVnode), version);
×
2318

2319
    SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
2320
    code = metaCreateTable2(pVnode->pMeta, version, pTbData->pCreateTbReq, &pCreateTbRsp->pMeta);
×
2321
    if (code == TSDB_CODE_SUCCESS) {
×
2322
      // Allocate necessary resources
2323
      if (newTbUids == NULL) {
×
2324
        newTbUids = taosArrayInit(numTbData, sizeof(int64_t));
×
2325
        if (newTbUids == NULL) {
×
2326
          code = terrno;
×
2327
          vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2328
                 tstrerror(code), version);
2329
          return code;
×
2330
        }
2331
      }
2332

2333
      if (taosArrayPush(newTbUids, &pTbData->uid) == NULL) {
×
2334
        code = terrno;
×
2335
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2336
               tstrerror(code), version);
2337
        taosArrayDestroy(newTbUids);
×
2338
        return code;
×
2339
      }
2340

2341
      if (pCreateTbRsp->pMeta) {
×
2342
        vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
×
2343
      }
2344
    } else if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
2345
      code = terrno = 0;
×
2346
      pTbData->uid = pTbData->pCreateTbReq->uid;  // update uid if table exist for using below
×
2347

2348
      // stream: get sver from meta, write to pCreateTbRsp, and need to check crateTbReq is same as meta.
2349
      if (i == 0) {
×
2350
        // In the streaming scenario, multiple grouped req requests will only operate on the same write table, and
2351
        // only the first one needs to be processed.
2352
        code = buildExistTableInStreamRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2353
        if (code) {
×
2354
          vInfo("vgId:%d failed to create table in stream:%s, code(0x%0x):%s", TD_VID(pVnode),
×
2355
                pTbData->pCreateTbReq->name, code, tstrerror(code));
2356
          taosArrayDestroy(newTbUids);
×
2357
          return code;
×
2358
        }
2359
      }
2360
    } else {
2361
      code = terrno;
×
2362
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2363
             tstrerror(code), version);
2364
      taosArrayDestroy(newTbUids);
×
2365
      return code;
×
2366
    }
2367
  }
2368

2369
  // Update the affected table uid list
2370
  if (taosArrayGetSize(newTbUids) > 0) {
×
2371
    vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
×
2372
           (int32_t)taosArrayGetSize(newTbUids));
2373
    if (tqAddTbUidList(pVnode->pTq, newTbUids) != 0) {
×
2374
      vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
×
2375
    }
2376
  }
2377

2378
  vDebug("vgId:%d, handle auto create table done, version:%" PRId64, TD_VID(pVnode), version);
×
2379

2380
  taosArrayDestroy(newTbUids);
×
2381
  return code;
×
2382
}
2383

2384
static void addExistTableInfoIntoRes(SVnode *pVnode, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse,
×
2385
                                     SSubmitTbData *pTbData, int32_t numTbData) {
2386
  int32_t code = 0;
×
2387
  int32_t lino = 0;
×
2388
  if ((pTbData->flags & SUBMIT_REQ_SCHEMA_RES) == 0) {
×
2389
    return;
×
2390
  }
2391
  if (pResponse->aCreateTbRsp) {  // If aSubmitTbData is not NULL, it means that the request is a create table request,
×
2392
                                  // so table info has exitst and we do not need to add again.
2393
    return;
×
2394
  }
2395
  pResponse->aCreateTbRsp = taosArrayInit(numTbData, sizeof(SVCreateTbRsp));
×
2396
  if (pResponse->aCreateTbRsp == NULL) {
×
2397
    code = terrno;
×
2398
    TSDB_CHECK_CODE(code, lino, _exit);
×
2399
  }
2400
  SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pResponse->aCreateTbRsp, 1);
×
2401
  if (pCreateTbRsp == NULL) {
×
2402
    code = terrno;
×
2403
    TSDB_CHECK_CODE(code, lino, _exit);
×
2404
  }
2405
  if (pTbData->suid == 0) {
×
2406
    code = buildExistNormalTalbeRsp(pVnode, pTbData->uid, &pCreateTbRsp->pMeta);
×
2407
    if (code) {
×
2408
      vError("vgId:%d, table uid:%" PRId64 " not exists, line:%d", TD_VID(pVnode), pTbData->uid, __LINE__);
×
2409
    }
2410
  } else {
2411
    code = buildExistSubTalbeRsp(pVnode, pTbData, &pCreateTbRsp->pMeta);
×
2412
  }
2413

2414
  TSDB_CHECK_CODE(code, lino, _exit);
×
2415
_exit:
×
2416
  if (code != TSDB_CODE_SUCCESS) {
×
2417
    vError("vgId:%d, failed to add exist table info into response, code:0x%0x, line:%d", TD_VID(pVnode), code, lino);
×
2418
  }
2419
  return;
×
2420
}
2421

2422
static int32_t vnodeHandleDataWrite(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
×
2423
  int32_t code = TSDB_CODE_SUCCESS;
×
2424
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
×
2425
  int8_t  hasBlob = 0;
×
2426

2427
  // Scan submit data
2428
  for (int32_t i = 0; i < numTbData; ++i) {
×
2429
    SMetaInfo      info = {0};
×
2430
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
×
2431

2432
    if (pTbData->flags & SUBMIT_REQ_WITH_BLOB) {
×
2433
      hasBlob = 1;
×
2434
    }
2435
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
2436
      continue;  // skip column data format
×
2437
    }
2438
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
×
2439
      continue;  // skip only crate table request
×
2440
    }
2441

2442
    code = metaGetInfo(pVnode->pMeta, pTbData->uid, &info, NULL);
×
2443
    if (code) {
×
2444
      code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
2445
      vWarn("vgId:%d, error occurred at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __FILE__,
×
2446
            __LINE__, tstrerror(code), version, pTbData->uid);
2447
      return code;
×
2448
    }
2449

2450
    if (info.suid != pTbData->suid) {
×
2451
      code = TSDB_CODE_INVALID_MSG;
×
2452
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64 " suid:%" PRId64
×
2453
             " info.suid:%" PRId64,
2454
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->suid,
2455
             info.suid);
2456
      return code;
×
2457
    }
2458

2459
    if (info.suid) {
×
2460
      code = metaGetInfo(pVnode->pMeta, info.suid, &info, NULL);
×
2461
      if (code) {
×
2462
        code = TSDB_CODE_INTERNAL_ERROR;
×
2463
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " suid:%" PRId64, TD_VID(pVnode), __func__,
×
2464
               __FILE__, __LINE__, tstrerror(code), version, info.suid);
2465
        return code;
×
2466
      }
2467
    }
2468

2469
    if (pTbData->sver != info.skmVer) {
×
2470
      code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
×
2471
      addExistTableInfoIntoRes(pVnode, pRequest, pResponse, pTbData, numTbData);
×
2472
      vDebug("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64
×
2473
             " sver:%d"
2474
             " info.skmVer:%d",
2475
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), version, pTbData->uid, pTbData->sver,
2476
             info.skmVer);
2477
      return code;
×
2478
    }
2479
  }
2480

2481
  // Do write data
2482
  vDebug("vgId:%d start to handle data write, version:%" PRId64, TD_VID(pVnode), version);
×
2483

2484
  for (int32_t i = 0; i < numTbData; ++i) {
×
2485
    int32_t        affectedRows = 0;
×
2486
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
×
2487

2488
    if (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) {
×
2489
      continue;
×
2490
    }
2491

2492
    if (hasBlob) {
×
2493
      code = vnodeSubmitBlobData(pVnode, pTbData);
×
2494
      if (code) {
×
2495
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2496
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2497
        return code;
×
2498
      }
2499
    }
2500

2501
    code = tsdbInsertTableData(pVnode->pTsdb, version, pTbData, &affectedRows);
×
2502
    if (code) {
×
2503
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2504
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2505
      return code;
×
2506
    }
2507

2508
    code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pTbData->uid, pTbData->ctimeMs);
×
2509
    if (code) {
×
2510
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2511
             __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2512
      return code;
×
2513
    }
2514
    pResponse->affectedRows += affectedRows;
×
2515
  }
2516

2517
  vDebug("vgId:%d, handle data write done, version:%" PRId64 ", affectedRows:%d", TD_VID(pVnode), version,
×
2518
         pResponse->affectedRows);
2519
  return code;
×
2520
}
2521

2522
static int32_t vnodeScanColumnData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
×
2523
  int32_t code = 0;
×
2524

2525
  int32_t   numCols = taosArrayGetSize(pTbData->aCol);
×
2526
  SColData *aColData = (SColData *)TARRAY_DATA(pTbData->aCol);
×
2527

2528
  if (numCols <= 0) {
×
2529
    code = TSDB_CODE_INVALID_MSG;
×
2530
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numCols:%d", TD_VID(pVnode), __func__,
×
2531
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numCols);
2532
    return code;
×
2533
  }
2534

2535
  if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
×
2536
      aColData[0].nVal <= 0) {
×
2537
    code = TSDB_CODE_INVALID_MSG;
×
2538
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2539
           " first column is not primary key timestamp, cid:%d type:%d nVal:%d",
2540
           TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, aColData[0].cid,
2541
           aColData[0].type, aColData[0].nVal);
2542
    return code;
×
2543
  }
2544

2545
  for (int32_t i = 1; i < numCols; ++i) {
×
2546
    if (aColData[i].nVal != aColData[0].nVal) {
×
2547
      code = TSDB_CODE_INVALID_MSG;
×
2548
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64
×
2549
             " column cid:%d type:%d nVal:%d is not equal to primary key timestamp nVal:%d",
2550
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid,
2551
             aColData[i].cid, aColData[i].type, aColData[i].nVal, aColData[0].nVal);
2552
      return code;
×
2553
    }
2554
  }
2555

2556
  SRowKey *pLastKey = NULL;
×
2557
  SRowKey  lastKey = {0};
×
2558
  for (int32_t i = 0; i < aColData[0].nVal; ++i) {
×
2559
    SRowKey key = {0};
×
2560

2561
    tColDataArrGetRowKey(aColData, numCols, i, &key);
×
2562

2563
    if (key.ts < minKey || key.ts > maxKey) {
×
2564
      code = TSDB_CODE_INVALID_MSG;
×
2565
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2566
             " is out of range [%" PRId64 ", %" PRId64 "]",
2567
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2568
             minKey, maxKey);
2569
      return code;
×
2570
    }
2571

2572
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
×
2573
      code = TSDB_CODE_INVALID_MSG;
×
2574
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2575
             " is not in order, lastKey:%" PRId64,
2576
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2577
             pLastKey->ts);
2578
      return code;
×
2579
    } else if (pLastKey == NULL) {
×
2580
      pLastKey = &lastKey;
×
2581
    }
2582

2583
    *pLastKey = key;
×
2584
  }
2585

2586
  return code;
×
2587
}
2588

2589
static int32_t vnodeScanSubmitRowData(SVnode *pVnode, SSubmitTbData *pTbData, TSKEY minKey, TSKEY maxKey) {
×
2590
  int32_t code = 0;
×
2591

2592
  int32_t numRows = taosArrayGetSize(pTbData->aRowP);
×
2593
  SRow  **aRow = (SRow **)TARRAY_DATA(pTbData->aRowP);
×
2594

2595
  if (numRows <= 0 && (pTbData->flags & SUBMIT_REQ_ONLY_CREATE_TABLE) == 0) {
×
2596
    code = TSDB_CODE_INVALID_MSG;
×
2597
    vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " numRows:%d", TD_VID(pVnode), __func__,
×
2598
           __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, numRows);
2599
    return code;
×
2600
  }
2601

2602
  SRowKey *pLastKey = NULL;
×
2603
  SRowKey  lastKey = {0};
×
2604
  for (int32_t i = 0; i < numRows; ++i) {
×
2605
    SRow *pRow = aRow[i];
×
2606
    if (pRow->sver != pTbData->sver) {
×
2607
      code = TSDB_CODE_INVALID_MSG;
×
2608
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] sver:%d pTbData->sver:%d",
×
2609
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, pRow->sver,
2610
             pTbData->sver);
2611
      return code;
×
2612
    }
2613

2614
    SRowKey key = {0};
×
2615
    tRowGetKey(pRow, &key);
×
2616
    if (key.ts < minKey || key.ts > maxKey) {
×
2617
      code = TSDB_CODE_INVALID_MSG;
×
2618
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2619
             " is out of range [%" PRId64 ", %" PRId64 "]",
2620
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2621
             minKey, maxKey);
2622
      return code;
×
2623
    }
2624

2625
    if (pLastKey && tRowKeyCompare(pLastKey, &key) >= 0) {
×
2626
      code = TSDB_CODE_INVALID_MSG;
×
2627
      vError("vgId:%d, %s failed at %s:%d since %s, version:%d uid:%" PRId64 " row[%d] key:%" PRId64
×
2628
             " is not in order, lastKey:%" PRId64,
2629
             TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code), pTbData->sver, pTbData->uid, i, key.ts,
2630
             pLastKey->ts);
2631
      return code;
×
2632
    } else if (pLastKey == NULL) {
×
2633
      pLastKey = &lastKey;
×
2634
    }
2635

2636
    *pLastKey = key;
×
2637
  }
2638

2639
  return code;
×
2640
}
2641

2642
static int32_t vnodeScanSubmitReq(SVnode *pVnode, int64_t version, SSubmitReq2 *pRequest, SSubmitRsp2 *pResponse) {
×
2643
  int32_t code = TSDB_CODE_SUCCESS;
×
2644
  int32_t numTbData = taosArrayGetSize(pRequest->aSubmitTbData);
×
2645

2646
  TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
×
2647
  TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
×
2648
  TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
×
2649
  for (int32_t i = 0; i < numTbData; i++) {
×
2650
    SSubmitTbData *pTbData = taosArrayGet(pRequest->aSubmitTbData, i);
×
2651

2652
    if (pTbData->pCreateTbReq && pTbData->pCreateTbReq->uid == 0) {
×
2653
      code = TSDB_CODE_INVALID_MSG;
×
2654
      vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
2655
             tstrerror(code), version);
2656
      return code;
×
2657
    }
2658

2659
    if (pTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
2660
      code = vnodeScanColumnData(pVnode, pTbData, minKey, maxKey);
×
2661
      if (code) {
×
2662
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2663
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2664
        return code;
×
2665
      }
2666
    } else {
2667
      code = vnodeScanSubmitRowData(pVnode, pTbData, minKey, maxKey);
×
2668
      if (code) {
×
2669
        vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64 " uid:%" PRId64, TD_VID(pVnode), __func__,
×
2670
               __FILE__, __LINE__, tstrerror(code), version, pTbData->uid);
2671
        return code;
×
2672
      }
2673
    }
2674
  }
2675

2676
  return code;
×
2677
}
2678

2679
static int32_t vnodeDecodeAuditRecord(const SJson *pJson, void *pObj) {
×
2680
  SAuditRecord *record = (SAuditRecord *)pObj;
×
2681
  int32_t       code = 0;
×
2682

2683
  tjsonGetNumberValue(pJson, "timestamp", record->curTime, code);
×
2684
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
×
2685
  code = tjsonGetStringValue2(pJson, "cluster_id", record->strClusterId, TSDB_CLUSTER_ID_LEN);
×
2686
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
×
2687
  code = tjsonGetStringValue2(pJson, "client_add", record->clientAddress, AUDIT_CLIENT_ADD_LEN);
×
2688
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
×
2689
  code = tjsonGetStringValue2(pJson, "user", record->user, TSDB_USER_LEN);
×
2690
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
×
2691
  code = tjsonGetStringValue2(pJson, "operation", record->operation, AUDIT_OPERATION_LEN);
×
2692
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
×
2693
  code = tjsonGetStringValue2(pJson, "db", record->target1, TSDB_DB_NAME_LEN);
×
2694
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
×
2695
  code = tjsonGetStringValue2(pJson, "resource", record->target2, TSDB_STREAM_NAME_LEN);
×
2696
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
×
2697
  record->detail = (char *)tjsonGetStringPointer(pJson, "details");
×
2698
  if (record->detail == NULL) return TSDB_CODE_INVALID_JSON_FORMAT;
×
2699
  code = tjsonGetDoubleValue(pJson, "duration", &record->duration);
×
2700
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
×
2701
  tjsonGetNumberValue(pJson, "affected_rows", record->affectedRows, code);
×
2702
  if (code < 0) return TSDB_CODE_INVALID_JSON_FORMAT;
×
2703

2704
  return 0;
×
2705
}
2706

2707
static int32_t vnodeBuildCreateTbReq(SVCreateTbReq *pTbReq, const char *tname, STag *pTag, int64_t suid,
×
2708
                                     const char *sname, SArray *tagName, uint8_t tagNum, int32_t ttl) {
2709
  int32_t code = TSDB_CODE_SUCCESS;
×
2710
  int32_t lino = 0;
×
2711

2712
  if (tname == NULL || sname == NULL) {
×
2713
    return TSDB_CODE_INVALID_PARA;
×
2714
  }
2715

2716
  pTbReq->name = taosStrdup(tname);
×
2717
  if (!pTbReq->name) {
×
2718
    code = terrno;
×
2719
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2720
  }
2721

2722
  pTbReq->ctb.stbName = taosStrdup(sname);
×
2723
  if (!pTbReq->ctb.stbName) {
×
2724
    code = terrno;
×
2725
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2726
  }
2727

2728
  pTbReq->type = TD_CHILD_TABLE;
×
2729
  pTbReq->ctb.suid = suid;
×
2730
  pTbReq->ctb.tagNum = tagNum;
×
2731
  pTbReq->ttl = ttl;
×
2732
  pTbReq->commentLen = -1;
×
2733
  pTbReq->ctb.tagName = tagName;
×
2734
  pTbReq->ctb.pTag = (uint8_t *)pTag;
×
2735

2736
  return code;
×
2737
_exit:
×
2738
  if (code != 0) {
×
2739
    if (pTbReq->name != NULL) {
×
2740
      taosMemoryFreeClear(pTbReq->name);
×
2741
    }
2742
    if (pTbReq->ctb.stbName != NULL) {
×
2743
      taosMemoryFreeClear(pTbReq->ctb.stbName);
×
2744
    }
2745
    vError("failed to build create tb at %d since %s", lino, tstrerror(code));
×
2746
  }
2747
  return code;
×
2748
}
2749

2750
static int32_t vnodePrepareCreateTb(SVCreateTbReq *pTbReq, char *tbName, int64_t suid, SSchemaWrapper *pTagSchema,
×
2751
                                    SAuditRecord *record) {
2752
  int32_t code = 0;
×
2753
  int32_t lino = 0;
×
2754

2755
  SArray *TagNames = NULL;
×
2756
  SArray *pTagVals = NULL;
×
2757
  STag   *pTag = NULL;
×
2758

2759
  SSchema *tSchema = &pTagSchema->pSchema[0];
×
2760
  vTrace("schema name:%s", tSchema->name);
×
2761

2762
  TagNames = taosArrayInit(1, TSDB_COL_NAME_LEN);
×
2763
  if (!TagNames) {
×
2764
    code = terrno;
×
2765
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2766
  }
2767

2768
  if (NULL == taosArrayPush(TagNames, tSchema->name)) {
×
2769
    code = terrno;
×
2770
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2771
  }
2772

2773
  STagVal tv = (STagVal){.cid = tSchema->colId,
×
2774
                         .type = tSchema->type,
×
2775
                         .nData = strlen(record->strClusterId),
×
2776
                         .pData = record->strClusterId};  // address copy, no value
×
2777
  if ((pTagVals = taosArrayInit(1, sizeof(STagVal))) == NULL) {
×
2778
    code = terrno;
×
2779
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2780
  }
2781
  if (NULL == taosArrayPush(pTagVals, &tv)) {
×
2782
    code = terrno;
×
2783
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2784
  }
2785

2786
  // version hardcode to 1 according to other module
2787
  code = tTagNew(pTagVals, 1, false, &pTag);
×
2788
  if (code != TSDB_CODE_SUCCESS) {
×
2789
    vError("failed to create tag, error:%s", tstrerror(code));
×
2790
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2791
  }
2792

2793
  code = vnodeBuildCreateTbReq(pTbReq, tbName, pTag, suid, AUDIT_STABLE_NAME, TagNames, pTagSchema->nCols,
×
2794
                               TSDB_DEFAULT_TABLE_TTL);
2795
_exit:
×
2796
  if (code != 0) {
×
2797
    vError("failed to prepare create tb at %d since %s", lino, tstrerror(code));
×
2798
    if (TagNames != NULL) {
×
2799
      taosArrayDestroy(TagNames);
×
2800
      TagNames = NULL;
×
2801
      pTbReq->ctb.tagName = NULL;
×
2802
    }
2803
    if (pTag != NULL) {
×
2804
      tTagFree(pTag);
×
2805
      pTag = NULL;
×
2806
      pTbReq->ctb.pTag = NULL;
×
2807
    }
2808
  }
2809

2810
  if (pTagVals != NULL) {
×
2811
    taosArrayDestroy(pTagVals);
×
2812
    pTagVals = NULL;
×
2813
  }
2814

2815
  return code;
×
2816
}
2817

2818
static SArray *vnodePrepareRow(SVnode *pVnode, STSchema *pSchema, SAuditRecord *record) {
×
2819
  int32_t code = 0;
×
2820
  int32_t lino = 0;
×
2821

2822
  SArray *aRows = NULL;
×
2823
  SArray *pVals = NULL;
×
2824
  SRow   *pRow = NULL;
×
2825

2826
  if (!(aRows = taosArrayInit(1, sizeof(SRow *)))) {
×
2827
    code = terrno;
×
2828
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2829
  }
2830

2831
  if ((pVals = taosArrayInit(1, sizeof(SColVal))) == NULL) {
×
2832
    code = terrno;
×
2833
    goto _exit;
×
2834
  }
2835

2836
  for (int32_t k = 0; k < pSchema->numOfCols; ++k) {
×
2837
    const STColumn *pCol = &pSchema->columns[k];
×
2838
    vTrace("vgId:%d, schema column id:%d, type:%d", TD_VID(pVnode), pCol->colId, pCol->type);
×
2839

2840
    // colId is consistent with audit_columns in mndStb.c
2841
    void *data = NULL;
×
2842
    if (pCol->colId == 1) {
×
2843
      data = &record->curTime;
×
2844
    } else if (pCol->colId == 2) {
×
2845
      data = record->detail;
×
2846
    } else if (pCol->colId == 3) {
×
2847
      data = record->user;
×
2848
    } else if (pCol->colId == 4) {
×
2849
      data = record->operation;
×
2850
    } else if (pCol->colId == 5) {
×
2851
      data = record->target1;
×
2852
    } else if (pCol->colId == 6) {
×
2853
      data = record->target2;
×
2854
    } else if (pCol->colId == 7) {
×
2855
      data = record->clientAddress;
×
2856
    } else if (pCol->colId == 8) {
×
2857
      data = &record->duration;
×
2858
    } else if (pCol->colId == 9) {
×
2859
      data = &record->affectedRows;
×
2860
    } else {
2861
      vError("the column id %" PRIi16 " is not defined in audit record table", pCol->colId);
×
2862
      code = TSDB_CODE_APP_ERROR;
×
2863
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2864
    }
2865

2866
    switch (pCol->type) {
×
2867
      case TSDB_DATA_TYPE_NCHAR:
×
2868
      case TSDB_DATA_TYPE_VARBINARY:
2869
      case TSDB_DATA_TYPE_VARCHAR: {
2870
        // if (colDataIsNull_s(pColInfoData, j)) {
2871
        //   SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);
2872
        //   if (NULL == taosArrayPush(pVals, &cv)) {
2873
        //     goto _end;
2874
        //   }
2875
        // } else {
2876
        char   *str = data;
×
2877
        int32_t len = strlen(str);
×
2878
        vTrace("set svalue id:%d %s, len:%d, bytes:%d", pCol->colId, str, len, pCol->bytes);
×
2879
        if (len > pCol->bytes - VARSTR_HEADER_SIZE) {
×
2880
          vWarn("vgId:%d, audit record string field with colId %d is too long, truncated from %d to %d bytes",
×
2881
                TD_VID(pVnode), pCol->colId, len, (int32_t)(pCol->bytes - VARSTR_HEADER_SIZE));
2882
          len = pCol->bytes - VARSTR_HEADER_SIZE;
×
2883
        }
2884
        SValue sv = {0};
×
2885
        sv.type = pCol->type;
×
2886
        sv.nData = len;
×
2887
        sv.pData = str;
×
2888
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
×
2889
        if (NULL == taosArrayPush(pVals, &cv)) {
×
2890
          code = terrno;
×
2891
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2892
        }
2893
        //}
2894
        break;
×
2895
      }
2896
      case TSDB_DATA_TYPE_BLOB:
×
2897
      case TSDB_DATA_TYPE_MEDIUMBLOB:
2898
      case TSDB_DATA_TYPE_JSON: {
2899
        vError("the column type %" PRIi16 " is defined but not implemented yet", pCol->type);
×
2900
        code = TSDB_CODE_APP_ERROR;
×
2901
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
2902
        break;
×
2903
      }
2904
      default: {
×
2905
        /*
2906
          if (colDataIsNull_s(pColInfoData, j)) {
2907
            if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId) {
2908
              qError("Primary timestamp column should not be null");
2909
              terrno = TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL;
2910
              goto _end;
2911
            }
2912

2913
            SColVal cv = COL_VAL_NULL(pCol->colId, pCol->type);  // should use pCol->type
2914
            if (NULL == taosArrayPush(pVals, &cv)) {
2915
              goto _end;
2916
            }
2917
          } else {
2918
           */
2919
        SValue sv = {0};
×
2920
        sv.type = pCol->type;
×
2921
        valueSetDatum(&sv, sv.type, data, pCol->bytes);
×
2922
        SColVal cv = COL_VAL_VALUE(pCol->colId, sv);
×
2923
        if (NULL == taosArrayPush(pVals, &cv)) {
×
2924
          code = terrno;
×
2925
          TAOS_CHECK_GOTO(code, &lino, _exit);
×
2926
        }
2927
        //}
2928
        break;
×
2929
      }
2930
    }
2931
  }
2932

2933
  SRowBuildScanInfo sinfo = {0};
×
2934
  if ((code = tRowBuild(pVals, pSchema, &pRow, &sinfo)) < 0) {
×
2935
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2936
  }
2937
  if (NULL == taosArrayPush(aRows, &pRow)) {
×
2938
    code = terrno;
×
2939
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2940
  }
2941
  taosArrayDestroy(pVals);
×
2942
  pVals = NULL;
×
2943

2944
_exit:
×
2945
  if (code != 0) {
×
2946
    vError("vgId:%d, failed to prepare row at:%d since %s", TD_VID(pVnode), lino, tstrerror(code));
×
2947
    taosArrayDestroy(pVals);
×
2948
    tRowDestroy(pRow);
×
2949
    taosArrayDestroy(aRows);
×
2950
    terrno = code;
×
2951
    aRows = NULL;
×
2952
  }
2953
  return aRows;
×
2954
}
2955

2956
static SArray *vnodePrepareSubmitTb(SVnode *pVnode, SAuditRecord *record, STSchema *pSchema, SSchemaWrapper *pTagSchema,
×
2957
                                    int64_t suid) {
2958
  int32_t code = 0;
×
2959
  int32_t lino = 0;
×
2960

2961
  SArray *aSubmitTbData = NULL;
×
2962
  SSubmitTbData tbData = {0};
×
2963

2964
  if (!(aSubmitTbData = taosArrayInit(1, sizeof(SSubmitTbData)))) {
×
2965
    code = terrno;
×
2966
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2967
  }
2968

2969
  char tbName[TSDB_TABLE_NAME_LEN] = {0};
×
2970
  (void)tsnprintf(tbName, TSDB_TABLE_NAME_LEN, "t_operations_%s", record->strClusterId);
×
2971

2972
  SMetaReader merTb = {0};
×
2973
  metaReaderDoInit(&merTb, pVnode->pMeta, META_READER_LOCK);
×
2974
  if ((code = metaGetTableEntryByName(&merTb, tbName)) == 0) {
×
2975
    vTrace("vgId:%d, get table entry, table:%s uid:%" PRId64 ", suid:% " PRId64 ", version:%" PRId64 ", api:%p",
×
2976
           TD_VID(pVnode), tbName, merTb.me.uid, merTb.me.ctbEntry.suid, merTb.me.version, merTb.pAPI);
2977
    tbData.uid = merTb.me.uid;
×
2978
    metaReaderClear(&merTb);
×
2979
  } else if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST) {
×
2980
    vTrace("vgId:%d, %s table not exist", TD_VID(pVnode), tbName);
×
2981

2982
    tbData.uid = tGenIdPI64();
×
2983
    tbData.flags |= SUBMIT_REQ_AUTO_CREATE_TABLE;
×
2984

2985
    tbData.pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
×
2986

2987
    if (tbData.pCreateTbReq == NULL) {
×
2988
      code = terrno;
×
2989
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
2990
    }
2991

2992
    tbData.pCreateTbReq->uid = tbData.uid;
×
2993
    tbData.pCreateTbReq->btime = taosGetTimestampMs();
×
2994

2995
    code = vnodePrepareCreateTb(tbData.pCreateTbReq, tbName, suid, pTagSchema, record);
×
2996

2997
    metaReaderClear(&merTb);
×
2998
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
2999
  } else {
3000
    metaReaderClear(&merTb);
×
3001
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3002
  }
3003

3004
  tbData.suid = suid;
×
3005
  tbData.sver = pSchema->version;
×
3006

3007
  tbData.aRowP = vnodePrepareRow(pVnode, pSchema, record);
×
3008
  if (tbData.aRowP == NULL) {
×
3009
    code = terrno;
×
3010
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3011
  }
3012

3013
  if (NULL == taosArrayPush(aSubmitTbData, &tbData)) {
×
3014
    code = terrno;
×
3015
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3016
  }
3017

3018
_exit:
×
3019
  if (code != 0) {
×
3020
    vError("vgId:%d, failed to prepare submitTb at:%d since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3021
    tDestroySubmitTbData(&tbData, TSDB_MSG_FLG_ENCODE);
×
3022
    if (aSubmitTbData != NULL) {
×
3023
      taosArrayDestroy(aSubmitTbData);
×
3024
    }
3025
    terrno = code;
×
3026
    aSubmitTbData = NULL;
×
3027
  }
3028
  return aSubmitTbData;
×
3029
}
3030

3031
static int32_t vnodeSaveOneAuditRecord(SVnode *pVnode, int64_t ver, SJson *pJson, SSchemaWrapper *pTagSchema,
×
3032
                                       int64_t suid, STSchema *pSchema, SRpcMsg *pOriginalMsg) {
3033
  int32_t code = 0;
×
3034
  int32_t lino = 0;
×
3035
  terrno = 0;
×
3036

3037
  SAuditRecord record = {0};
×
3038
  SSubmitReq2 *pSubmitReq = NULL;
×
3039
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
×
3040

3041
  TAOS_CHECK_GOTO(vnodeDecodeAuditRecord(pJson, &record), &lino, _exit);
×
3042

3043
  vTrace("vgId:%d, start to audit operation:%s", TD_VID(pVnode), record.operation);
×
3044

3045
  if (!(pSubmitReq = taosMemoryCalloc(1, sizeof(SSubmitReq2)))) {
×
3046
    code = terrno;
×
3047
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3048
  }
3049

3050
  pSubmitReq->aSubmitTbData = vnodePrepareSubmitTb(pVnode, &record, pSchema, pTagSchema, suid);
×
3051
  if (pSubmitReq->aSubmitTbData == NULL) {
×
3052
    code = terrno;
×
3053
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3054
  }
3055

3056
  vTrace("vgId:%d, going to create table", TD_VID(pVnode));
×
3057
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
×
3058
  if (code) {
×
3059
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3060
           tstrerror(code), ver);
3061
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3062
  }
3063

3064
  vTrace("vgId:%d, going to data write", TD_VID(pVnode));
×
3065
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
×
3066
  if (code) {
×
3067
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3068
           tstrerror(code), ver);
3069
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3070
  }
3071

3072
  vTrace("vgId:%d, affectedRow:%d", TD_VID(pVnode), pSubmitRsp->affectedRows);
×
3073

3074
_exit:
×
3075
  if (code != 0)
×
3076
    vError("vgId:%d, failed to save one AuditRecord at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3077

3078
  // update statistics
3079
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
×
3080
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
×
3081
  if (code == 0) {
×
3082
    (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
×
3083
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
×
3084
  }
3085

3086
  // update metrics
3087
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
×
3088
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
×
3089
  // METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
3090

3091
  if (code == 0 && tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
×
3092
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
3093
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
3094
                                   pVnode->monitor.strClusterId,
×
3095
                                   pVnode->monitor.strDnodeId,
×
3096
                                   tsLocalEp,
3097
                                   pVnode->monitor.strVgId,
×
3098
                                   RPC_MSG_USER(pOriginalMsg),
×
3099
                                   "Success"};
3100
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
3101
    if (tv != 0) vError("vgId:%d, failed to taos counter add since return is %d", TD_VID(pVnode), tv);
×
3102
  }
3103

3104
  // clear
3105
  if (pSubmitReq != NULL) {
×
3106
    tDestroySubmitReq(pSubmitReq, TSDB_MSG_FLG_ENCODE);
×
3107
    taosMemoryFree(pSubmitReq);
×
3108
  }
3109
  if (pSubmitRsp != NULL) {
×
3110
    tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
×
3111
  }
3112

3113
  return code;
×
3114
}
3115

3116
static int32_t vnodeProcessAuditRecordReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pOriginalMsg) {
×
3117
  int32_t code = 0;
×
3118
  int32_t lino = 0;
×
3119
  terrno = 0;
×
3120

3121
  SVAuditRecordReq req = {0};
×
3122
  if (tDeserializeSVAuditRecordReq(pReq, len, &req) != 0) {
×
3123
    vError("vgId:%d, failed to deserialize SVAuditRecordReq", TD_VID(pVnode));
×
3124
    code = TSDB_CODE_INVALID_MSG;
×
3125
    return code;
×
3126
  }
3127

3128
  vTrace("vgId:%d, start to process AuditRecord Req", TD_VID(pVnode));
×
3129

3130
  SJson          *pJson = NULL;
×
3131
  STSchema       *pSchema = NULL;
×
3132
  SSchemaWrapper *pTagSchema = NULL;
×
3133
  int64_t         suid = 0;
×
3134

3135
  pJson = tjsonParse(req.data);
×
3136
  if (pJson == NULL) {
×
3137
    code = TSDB_CODE_INVALID_JSON_FORMAT;
×
3138
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3139
  }
3140

3141
  SMetaReader merStb = {0};
×
3142
  metaReaderDoInit(&merStb, pVnode->pMeta, META_READER_LOCK);
×
3143
  code = metaGetTableEntryByName(&merStb, AUDIT_STABLE_NAME);
×
3144
  if (code != 0) {
×
3145
    metaReaderClear(&merStb);
×
3146
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3147
  }
3148
  vTrace("vgId:%d, get audit stable entry, uid:%" PRId64 ", suid:% " PRId64 ", version:%" PRId64 ", api:%p",
×
3149
         TD_VID(pVnode), merStb.me.uid, merStb.me.ctbEntry.suid, merStb.me.version, merStb.pAPI);
3150

3151
  code = vnodeGetTableSchema(pVnode, merStb.me.uid, &pSchema, &suid, &pTagSchema);
×
3152
  if (code != 0) {
×
3153
    metaReaderClear(&merStb);
×
3154
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
3155
  }
3156
  vTrace("vgId:%d, get audit stable schema, version:%d, suid:%" PRId64, TD_VID(pVnode), pSchema->version, suid);
×
3157
  metaReaderClear(&merStb);
×
3158
  // pSchema pTagSchema can be cached in the future
3159

3160
  SJson *pRecords = tjsonGetObjectItem(pJson, "records");
×
3161
  if (pRecords == NULL) {
×
3162
    TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pJson, pTagSchema, suid, pSchema, pOriginalMsg), &lino, _exit);
×
3163
  } else {
3164
    int32_t size = tjsonGetArraySize(pRecords);
×
3165
    vTrace("%d items in records", size);
×
3166
    for (int32_t i = 0; i < size; ++i) {
×
3167
      SJson *pRecord = tjsonGetArrayItem(pRecords, i);
×
3168
      if (pRecord == NULL) {
×
3169
        vError("vgId:%d, failed to get array item %d", TD_VID(pVnode), i);
×
3170
        code = TSDB_CODE_INVALID_JSON_FORMAT;
×
3171
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
3172
      }
3173
      TAOS_CHECK_GOTO(vnodeSaveOneAuditRecord(pVnode, ver, pRecord, pTagSchema, suid, pSchema, pOriginalMsg), &lino,
×
3174
                      _exit);
3175
    }
3176
  }
3177

3178
_exit:
×
3179
  if (code != 0)
×
3180
    vError("vgId:%d, failed to process AuditRecordReq at line:%d, since %s", TD_VID(pVnode), lino, tstrerror(code));
×
3181

3182
  // clear
3183
  if (pSchema) taosMemoryFree(pSchema);
×
3184
  if (pTagSchema) {
×
3185
    taosMemoryFreeClear(pTagSchema->pSchema);
×
3186
    taosMemoryFree(pTagSchema);
×
3187
  }
3188
  if (pJson != NULL) {
×
3189
    cJSON_Delete(pJson);
×
3190
    pJson = NULL;
×
3191
  }
3192
  tFreeSVAuditRecordReq(&req);
×
3193

3194
  return code;
×
3195
}
3196

3197
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
3198
                                     SRpcMsg *pOriginalMsg) {
3199
  int32_t code = 0;
×
3200
  int32_t lino = 0;
×
3201
  terrno = 0;
×
3202

3203
  SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
×
3204
  SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
×
3205
  int32_t      ret;
3206
  SEncoder     ec = {0};
×
3207

3208
  pRsp->code = TSDB_CODE_SUCCESS;
×
3209

3210
  void           *pAllocMsg = NULL;
×
3211
  SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
×
3212
  SDecoder        dc = {0};
×
3213
  if (0 == taosHton64(pMsg->version)) {
×
3214
    code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
×
3215
    if (TSDB_CODE_SUCCESS == code) {
×
3216
      code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
×
3217
    }
3218
    if (TSDB_CODE_SUCCESS == code) {
×
3219
      pAllocMsg = pReq;
×
3220
    }
3221
    TSDB_CHECK_CODE(code, lino, _exit);
×
3222
  } else {
3223
    // decode
3224
    pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
×
3225
    len -= sizeof(SSubmitReq2Msg);
×
3226

3227
    tDecoderInit(&dc, pReq, len);
×
3228
    if (tDecodeSubmitReq(&dc, pSubmitReq, NULL) < 0) {
×
3229
      code = TSDB_CODE_INVALID_MSG;
×
3230
      TSDB_CHECK_CODE(code, lino, _exit);
×
3231
    }
3232
  }
3233

3234
  // Scan the request
3235
  code = vnodeScanSubmitReq(pVnode, ver, pSubmitReq, pSubmitRsp);
×
3236
  if (code) {
×
3237
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3238
           tstrerror(code), ver);
3239
    TSDB_CHECK_CODE(code, lino, _exit);
×
3240
  }
3241

3242
  vGDebug(pOriginalMsg ? &pOriginalMsg->info.traceId : NULL, "vgId:%d, index:%" PRId64 ", submit block, rows:%d",
×
3243
          TD_VID(pVnode), ver, (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
3244

3245
  // Handle auto create table
3246
  code = vnodeHandleAutoCreateTable(pVnode, ver, pSubmitReq, pSubmitRsp);
×
3247
  if (code) {
×
3248
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3249
           tstrerror(code), ver);
3250
    TSDB_CHECK_CODE(code, lino, _exit);
×
3251
  }
3252

3253
  // Handle data write
3254
  code = vnodeHandleDataWrite(pVnode, ver, pSubmitReq, pSubmitRsp);
×
3255
  if (code) {
×
3256
    vError("vgId:%d, %s failed at %s:%d since %s, version:%" PRId64, TD_VID(pVnode), __func__, __FILE__, __LINE__,
×
3257
           tstrerror(code), ver);
3258
    TSDB_CHECK_CODE(code, lino, _exit);
×
3259
  }
3260

3261
_exit:
×
3262
  // message
3263
  pRsp->code = code;
×
3264
  tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
×
3265
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
3266
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
3267
  if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
×
3268
    vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
×
3269
  }
3270
  tEncoderClear(&ec);
×
3271

3272
  // update statistics
3273
  (void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
×
3274
  (void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
×
3275
  (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
×
3276

3277
  // update metrics
3278
  METRICS_UPDATE(pVnode->writeMetrics.total_requests, METRIC_LEVEL_LOW, 1);
×
3279
  METRICS_UPDATE(pVnode->writeMetrics.total_rows, METRIC_LEVEL_HIGH, pSubmitRsp->affectedRows);
×
3280
  METRICS_UPDATE(pVnode->writeMetrics.total_bytes, METRIC_LEVEL_LOW, pMsg->header.contLen);
×
3281

3282
  if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
×
3283
      strlen(RPC_MSG_USER(pOriginalMsg)) > 0 && tsInsertCounter != NULL) {
×
3284
    const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
×
3285
                                   pVnode->monitor.strClusterId,
×
3286
                                   pVnode->monitor.strDnodeId,
×
3287
                                   tsLocalEp,
3288
                                   pVnode->monitor.strVgId,
×
3289
                                   RPC_MSG_USER(pOriginalMsg),
×
3290
                                   "Success"};
3291
    int         tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
×
3292
    if (tv != 0) vError("vgId:%d, failed to taos counter add since return is %d", TD_VID(pVnode), tv);
×
3293
  }
3294

3295
  if (code == 0) {
×
3296
    (void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
×
3297
  }
3298

3299
  // clear
3300
  tDestroySubmitReq(pSubmitReq, 0 == taosHton64(pMsg->version) ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
×
3301
  tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
×
3302

3303
  if (code) {
×
3304
    terrno = code;
×
3305
    if (code == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
×
3306
      vInfo("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
×
3307
            tstrerror(code), lino, ver);
3308

3309
    } else {
3310
      vError("vgId:%d, failed to process submit request since %s, lino:%d, version:%" PRId64, TD_VID(pVnode),
×
3311
             tstrerror(code), lino, ver);
3312
    }
3313
  }
3314

3315
  taosMemoryFree(pAllocMsg);
×
3316
  tDecoderClear(&dc);
×
3317

3318
  return code;
×
3319
}
3320

3321
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
×
3322
  int32_t code = TSDB_CODE_SUCCESS;
×
3323

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

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

3330
  return code;
×
3331
}
3332

3333
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3334
  vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
×
3335
  int32_t code = TSDB_CODE_SUCCESS;
×
3336
  if (!pVnode->config.hashChange) {
×
3337
    goto _exit;
×
3338
  }
3339

3340
  code = vnodeConsolidateAlterHashRange(pVnode, ver);
×
3341
  if (code < 0) {
×
3342
    vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
×
3343
    goto _exit;
×
3344
  }
3345
  pVnode->config.hashChange = false;
×
3346

3347
_exit:
×
3348
  pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
×
3349
  pRsp->code = code;
×
3350
  pRsp->pCont = NULL;
×
3351
  pRsp->contLen = 0;
×
3352

3353
  return code;
×
3354
}
3355

3356
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
3357
extern void    tsdbEnableBgTask(STsdb *pTsdb);
3358

3359
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3360
  bool walChanged = false;
×
3361
  bool tsdbChanged = false;
×
3362

3363
  SAlterVnodeConfigReq req = {0};
×
3364
  if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
×
3365
    terrno = TSDB_CODE_INVALID_MSG;
×
3366
    return TSDB_CODE_INVALID_MSG;
×
3367
  }
3368

3369
  vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
×
3370
        " cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d ssKeepLocal:%d "
3371
        "ssCompact:%d allowDrop:%d fsync:%d level:%d "
3372
        "walRetentionPeriod:%d walRetentionSize:%d",
3373
        TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
3374
        req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
3375
        req.keepTimeOffset, req.ssKeepLocal, req.ssCompact, req.allowDrop, req.walFsyncPeriod, req.walLevel,
3376
        req.walRetentionPeriod, req.walRetentionSize);
3377

3378
  if (pVnode->config.cacheLastSize != req.cacheLastSize) {
×
3379
    pVnode->config.cacheLastSize = req.cacheLastSize;
×
3380
    tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
×
3381
  }
3382

3383
  if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
×
3384
    vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
×
3385
          (uint64_t)(req.buffer * 1024LL * 1024LL));
3386
    pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
×
3387
  }
3388

3389
  if (pVnode->config.szCache != req.pages) {
×
3390
    if ((terrno = metaAlterCache(pVnode->pMeta, req.pages)) < 0) {
×
3391
      vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
×
3392
             pVnode->config.szCache, req.pages, tstrerror(terrno));
3393
      return terrno;
×
3394
    } else {
3395
      vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
×
3396
      pVnode->config.szCache = req.pages;
×
3397
    }
3398
  }
3399

3400
  if (pVnode->config.cacheLast != req.cacheLast) {
×
3401
    pVnode->config.cacheLast = req.cacheLast;
×
3402
  }
3403

3404
  if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
×
3405
    pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
×
3406
    walChanged = true;
×
3407
  }
3408

3409
  if (pVnode->config.walCfg.level != req.walLevel) {
×
3410
    if (pVnode->config.walCfg.level == 0) {
×
3411
      pVnode->config.walCfg.clearFiles = 1;
×
3412
    }
3413
    pVnode->config.walCfg.level = req.walLevel;
×
3414
    walChanged = true;
×
3415
  }
3416

3417
  if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
×
3418
    pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
×
3419
    walChanged = true;
×
3420
  }
3421

3422
  if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
×
3423
    pVnode->config.walCfg.retentionSize = req.walRetentionSize;
×
3424
    walChanged = true;
×
3425
  }
3426

3427
  if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
×
3428
    pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
×
3429
    tsdbChanged = true;
×
3430
  }
3431

3432
  if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
×
3433
    pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
×
3434
    tsdbChanged = true;
×
3435
  }
3436

3437
  if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
×
3438
    pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
×
3439
    tsdbChanged = true;
×
3440
  }
3441

3442
  if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
×
3443
    pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
×
3444
    tsdbChanged = true;
×
3445
  }
3446

3447
  if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
×
3448
    if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
×
3449
      pVnode->config.sttTrigger = req.sttTrigger;
×
3450
    } else {
3451
      vnodeAWait(&pVnode->commitTask);
×
3452

3453
      int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
×
3454
      if (ret != 0) {
×
3455
        vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3456
      }
3457

3458
      pVnode->config.sttTrigger = req.sttTrigger;
×
3459
      tsdbEnableBgTask(pVnode->pTsdb);
×
3460
    }
3461
  }
3462

3463
  if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
×
3464
    pVnode->config.tsdbCfg.minRows = req.minRows;
×
3465
  }
3466

3467
  if (req.ssKeepLocal != -1 && req.ssKeepLocal != pVnode->config.ssKeepLocal) {
×
3468
    pVnode->config.ssKeepLocal = req.ssKeepLocal;
×
3469
  }
3470
  if (req.ssCompact != -1 && req.ssCompact != pVnode->config.ssCompact) {
×
3471
    pVnode->config.ssCompact = req.ssCompact;
×
3472
  }
3473
  if (req.allowDrop != pVnode->config.allowDrop) {
×
3474
    pVnode->config.allowDrop = req.allowDrop;
×
3475
  }
3476

3477
  if (walChanged) {
×
3478
    if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
×
3479
      vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(ERRNO));
×
3480
    }
3481
  }
3482

3483
  if (tsdbChanged) {
×
3484
    tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
×
3485
  }
3486

3487
  return 0;
×
3488
}
3489

3490
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3491
  SBatchDeleteReq deleteReq;
3492
  SDecoder        decoder;
3493
  tDecoderInit(&decoder, pReq, len);
×
3494
  if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
×
3495
    tDecoderClear(&decoder);
×
3496
    return terrno = TSDB_CODE_INVALID_MSG;
×
3497
  }
3498

3499
  SMetaReader mr = {0};
×
3500
  metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
×
3501
  STsdb *pTsdb = pVnode->pTsdb;
×
3502

3503
  int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
×
3504
  for (int32_t i = 0; i < sz; i++) {
×
3505
    SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
×
3506
    char             *name = pOneReq->tbname;
×
3507
    if (metaGetTableEntryByName(&mr, name) < 0) {
×
3508
      vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
×
3509
      continue;
×
3510
    }
3511

3512
    int64_t uid = mr.me.uid;
×
3513

3514
    int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
×
3515
    if (code < 0) {
×
3516
      terrno = code;
×
3517
      vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
×
3518
             TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3519
    }
3520

3521
    if (deleteReq.level == 0) {
×
3522
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
×
3523
      if (code < 0) {
×
3524
        terrno = code;
×
3525
        vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
×
3526
               ", end ts:%" PRId64,
3527
               TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
3528
      }
3529
    }
3530
    tDecoderClear(&mr.coder);
×
3531
  }
3532
  metaReaderClear(&mr);
×
3533
  taosArrayDestroy(deleteReq.deleteReqs);
×
3534
  return 0;
×
3535
}
3536

3537
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
×
3538
                                     SRpcMsg *pOriginalMsg) {
3539
  int32_t     code = 0;
×
3540
  SDecoder   *pCoder = &(SDecoder){0};
×
3541
  SDeleteRes *pRes = &(SDeleteRes){0};
×
3542

3543
  pRsp->msgType = TDMT_VND_DELETE_RSP;
×
3544
  pRsp->pCont = NULL;
×
3545
  pRsp->contLen = 0;
×
3546
  pRsp->code = TSDB_CODE_SUCCESS;
×
3547

3548
  pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
×
3549
  if (pRes->uidList == NULL) {
×
3550
    code = terrno;
×
3551
    goto _err;
×
3552
  }
3553

3554
  tDecoderInit(pCoder, pReq, len);
×
3555
  code = tDecodeDeleteRes(pCoder, pRes);
×
3556
  if (code) goto _err;
×
3557

3558
  if (pRes->affectedRows > 0) {
×
3559
    for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
×
3560
      uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
×
3561
      code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
×
3562
      if (code) goto _err;
×
3563
      code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
×
3564
      if (code) goto _err;
×
3565
    }
3566
  }
3567

3568
  tDecoderClear(pCoder);
×
3569
  taosArrayDestroy(pRes->uidList);
×
3570

3571
  SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
×
3572
  int32_t     ret = 0;
×
3573
  tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
×
3574
  pRsp->pCont = rpcMallocCont(pRsp->contLen);
×
3575
  SEncoder ec = {0};
×
3576
  tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
×
3577
  code = tEncodeSVDeleteRsp(&ec, &rsp);
×
3578
  if (code) goto _err;
×
3579
  tEncoderClear(&ec);
×
3580
  return code;
×
3581

3582
_err:
×
3583
  /*
3584
  if(code == TSDB_CODE_SUCCESS){
3585
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3586
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3587
                                        RPC_MSG_USER(pOriginalMsg), "Success"};
3588
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3589
  }
3590
  else{
3591
    const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
3592
                                        pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
3593
                                        RPC_MSG_USER(pOriginalMsg), "Failed"};
3594
    taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
3595
  }
3596
  */
3597

3598
  return code;
×
3599
}
3600
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3601
  SVCreateStbReq req = {0};
×
3602
  SDecoder       dc = {0};
×
3603
  int32_t        code = 0;
×
3604

3605
  pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
×
3606
  pRsp->code = TSDB_CODE_SUCCESS;
×
3607
  pRsp->pCont = NULL;
×
3608
  pRsp->contLen = 0;
×
3609

3610
  tDecoderInit(&dc, pReq, len);
×
3611
  // decode req
3612
  if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
×
3613
    tDecoderClear(&dc);
×
3614
    return terrno = TSDB_CODE_INVALID_MSG;
×
3615
  }
3616

3617
  code = metaAddIndexToSuperTable(pVnode->pMeta, ver, &req);
×
3618
  if (code) {
×
3619
    pRsp->code = code;
×
3620
    goto _err;
×
3621
  }
3622
  tDecoderClear(&dc);
×
3623
  return 0;
×
3624

3625
_err:
×
3626
  tDecoderClear(&dc);
×
3627
  return code;
×
3628
}
3629
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3630
  SDropIndexReq req = {0};
×
3631
  int32_t       code = 0;
×
3632
  pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
×
3633
  pRsp->code = TSDB_CODE_SUCCESS;
×
3634
  pRsp->pCont = NULL;
×
3635
  pRsp->contLen = 0;
×
3636

3637
  if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
×
3638
    pRsp->code = code;
×
3639
    return code;
×
3640
  }
3641

3642
  code = metaDropIndexFromSuperTable(pVnode->pMeta, ver, &req);
×
3643
  if (code) {
×
3644
    pRsp->code = code;
×
3645
    return code;
×
3646
  }
3647
  return TSDB_CODE_SUCCESS;
×
3648
}
3649

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

3652
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3653
  if (!pVnode->restored) {
×
3654
    vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
×
3655
    return 0;
×
3656
  }
3657
  return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
×
3658
}
3659

3660
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3661
  if (syncCheckMember(pVnode->sync) != 0) {
×
3662
    vError("vgId:%d, failed to check member", TD_VID(pVnode));
×
3663
  }
3664

3665
  pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
×
3666
  pRsp->code = TSDB_CODE_SUCCESS;
×
3667
  pRsp->pCont = NULL;
×
3668
  pRsp->contLen = 0;
×
3669

3670
  return 0;
×
3671
}
3672

3673
static int32_t vnodeCheckState(SVnode *pVnode) {
×
3674
  SSyncState syncState = syncGetState(pVnode->sync);
×
3675
  if (syncState.state != TAOS_SYNC_STATE_LEADER) {
×
3676
    return terrno = TSDB_CODE_SYN_NOT_LEADER;
×
3677
  }
3678
  return 0;
×
3679
}
3680

3681
static int32_t vnodeCheckToken(SVnode *pVnode, char *member0Token, char *member1Token) {
×
3682
  char token[TSDB_ARB_TOKEN_SIZE] = {0};
×
3683
  if (vnodeGetArbToken(pVnode, token) != 0) {
×
3684
    return terrno = TSDB_CODE_NOT_FOUND;
×
3685
  }
3686

3687
  if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
×
3688
      strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
×
3689
    return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
×
3690
  }
3691

3692
  terrno = TSDB_CODE_SUCCESS;
×
3693
  return 0;
×
3694
}
3695

3696
static int32_t vnodeCheckSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
×
3697
  int32_t code = vnodeCheckToken(pVnode, member0Token, member1Token);
×
3698
  if (code != 0) {
×
3699
    return code;
×
3700
  }
3701

3702
  return syncCheckSynced(pVnode->sync);
×
3703
}
3704

3705
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
×
3706
  int32_t code = 0;
×
3707

3708
  if ((code = vnodeCheckState(pVnode)) != 0) {
×
3709
    vDebug("vgId:%d, failed to preprocess vnode-arb-check-sync request since %s", TD_VID(pVnode), tstrerror(code));
×
3710
    return 0;
×
3711
  }
3712

3713
  SVArbCheckSyncReq syncReq = {0};
×
3714

3715
  code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
×
3716
  if (code) {
×
3717
    return code;
×
3718
  }
3719

3720
  vInfo("vgId:%d, start to process vnode-arb-check-sync req QID:0x%" PRIx64 ":0x%" PRIx64 ", seqNum:%" PRIx64
×
3721
        ", arbToken:%s, member0Token:%s, member1Token:%s, "
3722
        "arbTerm:%" PRId64,
3723
        TD_VID(pVnode), pRsp->info.traceId.rootId, pRsp->info.traceId.msgId, pRsp->info.seqNum, syncReq.arbToken,
3724
        syncReq.member0Token, syncReq.member1Token, syncReq.arbTerm);
3725

3726
  pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
×
3727
  pRsp->code = TSDB_CODE_SUCCESS;
×
3728
  pRsp->pCont = NULL;
×
3729
  pRsp->contLen = 0;
×
3730

3731
  SVArbCheckSyncRsp syncRsp = {0};
×
3732
  syncRsp.arbToken = syncReq.arbToken;
×
3733
  syncRsp.member0Token = syncReq.member0Token;
×
3734
  syncRsp.member1Token = syncReq.member1Token;
×
3735
  syncRsp.vgId = TD_VID(pVnode);
×
3736

3737
  if ((syncRsp.errCode = vnodeCheckSyncd(pVnode, syncReq.member0Token, syncReq.member1Token)) != 0) {
×
3738
    vError("vgId:%d, failed to check assigned log syncd since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode));
×
3739
  }
3740

3741
  if ((code = vnodeUpdateArbTerm(pVnode, syncReq.arbTerm)) != 0) {
×
3742
    vError("vgId:%d, failed to update arb term since %s", TD_VID(pVnode), tstrerror(code));
×
3743
    goto _OVER;
×
3744
  }
3745

3746
  int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
×
3747
  if (contLen <= 0) {
×
3748
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3749
    code = -1;
×
3750
    goto _OVER;
×
3751
  }
3752
  void *pHead = rpcMallocCont(contLen);
×
3753
  if (!pHead) {
×
3754
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3755
    code = -1;
×
3756
    goto _OVER;
×
3757
  }
3758

3759
  if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
×
3760
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
3761
    rpcFreeCont(pHead);
×
3762
    code = -1;
×
3763
    goto _OVER;
×
3764
  }
3765

3766
  pRsp->pCont = pHead;
×
3767
  pRsp->contLen = contLen;
×
3768

3769
  vInfo(
×
3770
      "vgId:%d, suceed to process vnode-arb-check-sync req rsp.code:%s, arbToken:%s, member0Token:%s, "
3771
      "member1Token:%s",
3772
      TD_VID(pVnode), tstrerror(syncRsp.errCode), syncRsp.arbToken, syncRsp.member0Token, syncRsp.member1Token);
3773

3774
  code = TSDB_CODE_SUCCESS;
×
3775

3776
_OVER:
×
3777
  if (code != 0) {
×
3778
    vError("vgId:%d, failed to process arb check req rsp.code:%s since %s", TD_VID(pVnode), tstrerror(syncRsp.errCode),
×
3779
           tstrerror(code));
3780
  }
3781
  tFreeSVArbCheckSyncReq(&syncReq);
×
3782
  return code;
×
3783
}
3784

3785
#ifndef TD_ENTERPRISE
3786
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
3787
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, ETsdbOpType type, bool force) { return 0; }
3788
#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