TDengine/source/dnode/vnode/src/vnd/vnodeSvr.c

2518 lines
80 KiB
C
Raw Normal View History

2022-04-14 08:42:50 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "audit.h"
2023-11-15 05:41:00 +00:00
#include "cos.h"
2024-08-20 03:48:42 +00:00
#include "monitor.h"
2023-02-15 09:59:54 +00:00
#include "tencode.h"
2024-02-27 10:57:32 +00:00
#include "tglobal.h"
2023-02-15 09:59:54 +00:00
#include "tmsg.h"
#include "tstrbuild.h"
2022-04-26 11:04:26 +00:00
#include "vnd.h"
2023-02-15 09:59:54 +00:00
#include "vnode.h"
#include "vnodeInt.h"
2022-04-14 08:42:50 +00:00
2024-08-20 03:48:42 +00:00
extern taos_counter_t *tsInsertCounter;
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
2023-11-15 05:41:00 +00:00
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
SRpcMsg *pOriginRpc);
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
2023-11-15 05:41:00 +00:00
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
SRpcMsg *pOriginRpc);
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2024-03-26 06:43:28 +00:00
SRpcMsg *pOriginalMsg);
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2024-03-26 06:43:28 +00:00
SRpcMsg *pOriginalMsg);
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
2023-09-01 05:24:47 +00:00
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp);
static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token);
2024-05-23 09:57:07 +00:00
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
2022-04-14 10:21:06 +00:00
2023-12-08 09:26:44 +00:00
extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg);
2023-10-23 08:59:54 +00:00
static int32_t vnodePreprocessCreateTableReq(SVnode *pVnode, SDecoder *pCoder, int64_t btime, int64_t *pUid) {
2023-02-15 09:59:54 +00:00
int32_t code = 0;
int32_t lino = 0;
if (tStartDecode(pCoder) < 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
// flags
if (tDecodeI32v(pCoder, NULL) < 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
// name
char *name = NULL;
if (tDecodeCStr(pCoder, &name) < 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
// uid
int64_t uid = metaGetTableEntryUidByName(pVnode->pMeta, name);
if (uid == 0) {
uid = tGenIdPI64();
}
*(int64_t *)(pCoder->data + pCoder->pos) = uid;
// btime
*(int64_t *)(pCoder->data + pCoder->pos + 8) = btime;
2023-02-15 09:59:54 +00:00
tEndDecode(pCoder);
_exit:
if (code) {
vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
} else {
vTrace("vgId:%d %s done, table:%s uid generated:%" PRId64, TD_VID(pVnode), __func__, name, uid);
2023-02-16 07:55:59 +00:00
if (pUid) *pUid = uid;
2023-02-15 09:59:54 +00:00
}
return code;
}
2023-02-03 06:46:45 +00:00
static int32_t vnodePreProcessCreateTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
int32_t code = 0;
int32_t lino = 0;
int64_t btime = taosGetTimestampMs();
2022-05-23 08:38:05 +00:00
SDecoder dc = {0};
2023-02-03 06:46:45 +00:00
int32_t nReqs;
2022-04-14 08:42:50 +00:00
2023-02-03 06:46:45 +00:00
tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
if (tStartDecode(&dc) < 0) {
code = TSDB_CODE_INVALID_MSG;
return code;
}
if (tDecodeI32v(&dc, &nReqs) < 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
for (int32_t iReq = 0; iReq < nReqs; iReq++) {
code = vnodePreprocessCreateTableReq(pVnode, &dc, btime, NULL);
2023-02-15 09:59:54 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-02-03 06:46:45 +00:00
}
tEndDecode(&dc);
_exit:
tDecoderClear(&dc);
2023-06-26 10:43:00 +00:00
if (code) {
vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
tstrerror(code), TMSG_INFO(pMsg->msgType));
}
2023-02-03 06:46:45 +00:00
return code;
}
static int32_t vnodePreProcessAlterTableMsg(SVnode *pVnode, SRpcMsg *pMsg) {
int32_t code = TSDB_CODE_INVALID_MSG;
int32_t lino = 0;
SDecoder dc = {0};
tDecoderInit(&dc, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
SVAlterTbReq vAlterTbReq = {0};
int64_t ctimeMs = taosGetTimestampMs();
if (tDecodeSVAlterTbReqSetCtime(&dc, &vAlterTbReq, ctimeMs) < 0) {
goto _exit;
}
code = 0;
_exit:
tDecoderClear(&dc);
if (code) {
vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
} else {
vTrace("vgId:%d %s done, table:%s ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, vAlterTbReq.tbName,
ctimeMs);
}
return code;
}
2023-09-01 05:24:47 +00:00
static int32_t vnodePreProcessDropTtlMsg(SVnode *pVnode, SRpcMsg *pMsg) {
int32_t code = TSDB_CODE_INVALID_MSG;
int32_t lino = 0;
SMsgHead *pContOld = pMsg->pCont;
int32_t reqLenOld = pMsg->contLen - sizeof(SMsgHead);
SArray *tbUids = NULL;
int64_t timestampMs = 0;
SVDropTtlTableReq ttlReq = {0};
if (tDeserializeSVDropTtlTableReq((char *)pContOld + sizeof(SMsgHead), reqLenOld, &ttlReq) != 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
{ // find expired uids
tbUids = taosArrayInit(8, sizeof(tb_uid_t));
2023-09-01 05:24:47 +00:00
if (tbUids == NULL) {
2024-09-20 00:56:46 +00:00
code = terrno;
2023-09-01 05:24:47 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
}
timestampMs = (int64_t)ttlReq.timestampSec * 1000;
code = metaTtlFindExpired(pVnode->pMeta, timestampMs, tbUids, ttlReq.ttlDropMaxCount);
if (code != 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
ttlReq.nUids = taosArrayGetSize(tbUids);
ttlReq.pTbUids = tbUids;
}
if (ttlReq.nUids == 0) {
code = TSDB_CODE_MSG_PREPROCESSED;
TSDB_CHECK_CODE(code, lino, _exit);
}
{ // prepare new content
2023-09-01 05:24:47 +00:00
int32_t reqLenNew = tSerializeSVDropTtlTableReq(NULL, 0, &ttlReq);
int32_t contLenNew = reqLenNew + sizeof(SMsgHead);
SMsgHead *pContNew = rpcMallocCont(contLenNew);
if (pContNew == NULL) {
2024-09-20 00:56:46 +00:00
code = terrno;
2023-09-01 05:24:47 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
}
2024-09-26 02:43:16 +00:00
if (tSerializeSVDropTtlTableReq((char *)pContNew + sizeof(SMsgHead), reqLenNew, &ttlReq) != 0) {
vError("vgId:%d %s:%d failed to serialize drop ttl request", TD_VID(pVnode), __func__, lino);
}
2023-09-01 05:24:47 +00:00
pContNew->contLen = htonl(reqLenNew);
pContNew->vgId = pContOld->vgId;
rpcFreeCont(pContOld);
pMsg->pCont = pContNew;
pMsg->contLen = contLenNew;
}
code = 0;
_exit:
taosArrayDestroy(tbUids);
if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
2023-09-01 05:24:47 +00:00
vError("vgId:%d, %s:%d failed to preprocess drop ttl request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
tstrerror(code), TMSG_INFO(pMsg->msgType));
} else {
vTrace("vgId:%d, %s done, timestampSec:%d, nUids:%d", TD_VID(pVnode), __func__, ttlReq.timestampSec, ttlReq.nUids);
}
return code;
}
2023-02-15 09:59:54 +00:00
extern int64_t tsMaxKeyByPrecision[];
static int32_t vnodePreProcessSubmitTbData(SVnode *pVnode, SDecoder *pCoder, int64_t btimeMs, int64_t ctimeMs) {
2023-02-03 06:46:45 +00:00
int32_t code = 0;
int32_t lino = 0;
2023-02-15 09:59:54 +00:00
if (tStartDecode(pCoder) < 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
2023-02-03 06:46:45 +00:00
2023-02-15 09:59:54 +00:00
SSubmitTbData submitTbData;
2024-04-10 07:06:43 +00:00
uint8_t version;
2023-02-15 09:59:54 +00:00
if (tDecodeI32v(pCoder, &submitTbData.flags) < 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
2024-04-10 07:06:43 +00:00
version = (submitTbData.flags >> 8) & 0xff;
submitTbData.flags = submitTbData.flags & 0xff;
2023-02-03 06:46:45 +00:00
2023-02-16 07:55:59 +00:00
int64_t uid;
2023-02-15 09:59:54 +00:00
if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
code = vnodePreprocessCreateTableReq(pVnode, pCoder, btimeMs, &uid);
2023-02-15 09:59:54 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
}
// submit data
if (tDecodeI64(pCoder, &submitTbData.suid) < 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
2023-02-16 07:55:59 +00:00
if (submitTbData.flags & SUBMIT_REQ_AUTO_CREATE_TABLE) {
*(int64_t *)(pCoder->data + pCoder->pos) = uid;
2023-02-16 09:50:45 +00:00
pCoder->pos += sizeof(int64_t);
2023-02-16 07:55:59 +00:00
} else {
2023-02-22 06:29:14 +00:00
if (tDecodeI64(pCoder, &submitTbData.uid) < 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
2023-02-15 09:59:54 +00:00
}
2023-02-16 07:55:59 +00:00
2023-02-15 09:59:54 +00:00
if (tDecodeI32v(pCoder, &submitTbData.sver) < 0) {
2023-02-03 06:46:45 +00:00
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
2023-02-15 09:59:54 +00:00
// scan and check
TSKEY now = btimeMs;
2023-02-15 09:59:54 +00:00
if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_MICRO) {
now *= 1000;
} else if (pVnode->config.tsdbCfg.precision == TSDB_TIME_PRECISION_NANO) {
now *= 1000000;
}
2023-09-01 05:24:47 +00:00
int32_t keep = pVnode->config.tsdbCfg.keep2;
/*
int32_t nlevel = tfsGetLevel(pVnode->pTfs);
2023-09-01 05:24:47 +00:00
if (nlevel > 1 && tsS3Enabled) {
if (nlevel == 3) {
keep = pVnode->config.tsdbCfg.keep1;
} else if (nlevel == 2) {
keep = pVnode->config.tsdbCfg.keep0;
}
}
*/
2023-09-01 05:24:47 +00:00
TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * keep;
2023-02-15 09:59:54 +00:00
TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
if (submitTbData.flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
uint64_t nColData;
if (tDecodeU64v(pCoder, &nColData) < 0) {
2023-02-03 06:46:45 +00:00
code = TSDB_CODE_INVALID_MSG;
2023-02-15 09:59:54 +00:00
goto _exit;
2023-02-03 06:46:45 +00:00
}
2023-02-15 09:59:54 +00:00
SColData colData = {0};
2024-04-10 07:06:43 +00:00
pCoder->pos += tGetColData(version, pCoder->data + pCoder->pos, &colData);
2023-02-22 06:29:14 +00:00
if (colData.flag != HAS_VALUE) {
code = TSDB_CODE_INVALID_MSG;
goto _exit;
}
2023-02-15 09:59:54 +00:00
for (int32_t iRow = 0; iRow < colData.nVal; iRow++) {
if (((TSKEY *)colData.pData)[iRow] < minKey || ((TSKEY *)colData.pData)[iRow] > maxKey) {
code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
goto _exit;
}
}
for (uint64_t i = 1; i < nColData; i++) {
2024-04-10 07:06:43 +00:00
pCoder->pos += tGetColData(version, pCoder->data + pCoder->pos, &colData);
}
2023-02-15 09:59:54 +00:00
} else {
uint64_t nRow;
if (tDecodeU64v(pCoder, &nRow) < 0) {
2023-02-03 06:46:45 +00:00
code = TSDB_CODE_INVALID_MSG;
2023-02-15 09:59:54 +00:00
goto _exit;
2023-02-03 06:46:45 +00:00
}
2022-05-23 08:38:05 +00:00
2023-02-15 09:59:54 +00:00
for (int32_t iRow = 0; iRow < nRow; ++iRow) {
SRow *pRow = (SRow *)(pCoder->data + pCoder->pos);
pCoder->pos += pRow->len;
2022-05-23 08:38:05 +00:00
2023-02-15 09:59:54 +00:00
if (pRow->ts < minKey || pRow->ts > maxKey) {
code = TSDB_CODE_TDB_TIMESTAMP_OUT_OF_RANGE;
goto _exit;
2022-10-15 03:20:37 +00:00
}
2023-02-15 09:59:54 +00:00
}
}
2022-05-23 08:38:05 +00:00
2023-06-29 03:42:06 +00:00
if (!tDecodeIsEnd(pCoder)) {
*(int64_t *)(pCoder->data + pCoder->pos) = ctimeMs;
pCoder->pos += sizeof(int64_t);
}
2023-02-15 09:59:54 +00:00
tEndDecode(pCoder);
2022-05-23 08:38:05 +00:00
2023-02-15 09:59:54 +00:00
_exit:
2023-02-16 07:55:59 +00:00
return code;
2023-02-15 09:59:54 +00:00
}
static int32_t vnodePreProcessSubmitMsg(SVnode *pVnode, SRpcMsg *pMsg) {
int32_t code = 0;
int32_t lino = 0;
2022-05-23 08:38:05 +00:00
2023-02-15 09:59:54 +00:00
SDecoder *pCoder = &(SDecoder){0};
2022-11-29 07:47:24 +00:00
2023-03-08 07:19:55 +00:00
if (taosHton64(((SSubmitReq2Msg *)pMsg->pCont)->version) != 1) {
2023-03-08 06:45:04 +00:00
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
2023-02-17 01:23:41 +00:00
tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SSubmitReq2Msg), pMsg->contLen - sizeof(SSubmitReq2Msg));
2022-05-23 08:38:05 +00:00
2023-02-15 09:59:54 +00:00
if (tStartDecode(pCoder) < 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
2022-05-23 08:38:05 +00:00
2023-02-15 09:59:54 +00:00
uint64_t nSubmitTbData;
if (tDecodeU64v(pCoder, &nSubmitTbData) < 0) {
code = TSDB_CODE_INVALID_MSG;
TSDB_CHECK_CODE(code, lino, _exit);
}
2022-05-23 08:38:05 +00:00
int64_t btimeMs = taosGetTimestampMs();
int64_t ctimeMs = btimeMs;
2023-02-15 09:59:54 +00:00
for (int32_t i = 0; i < nSubmitTbData; i++) {
code = vnodePreProcessSubmitTbData(pVnode, pCoder, btimeMs, ctimeMs);
2023-02-15 09:59:54 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-02-03 06:46:45 +00:00
}
2022-05-23 09:34:08 +00:00
2023-02-15 09:59:54 +00:00
tEndDecode(pCoder);
2022-11-29 07:47:24 +00:00
2023-02-03 06:46:45 +00:00
_exit:
2023-06-26 10:43:00 +00:00
tDecoderClear(pCoder);
if (code) {
2023-06-26 10:43:00 +00:00
vError("vgId:%d, %s:%d failed to preprocess submit request since %s, msg type:%s", TD_VID(pVnode), __func__, lino,
tstrerror(code), TMSG_INFO(pMsg->msgType));
}
2023-02-03 06:46:45 +00:00
return code;
}
2022-05-23 08:38:05 +00:00
2023-02-03 06:46:45 +00:00
static int32_t vnodePreProcessDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
int32_t code = 0;
int32_t size;
int32_t ret;
uint8_t *pCont;
SEncoder *pCoder = &(SEncoder){0};
SDeleteRes res = {0};
2023-05-24 16:50:03 +00:00
2023-11-09 11:51:01 +00:00
SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .skipRollup = 1};
2023-05-24 16:50:03 +00:00
initStorageAPI(&handle.api);
2023-02-03 06:46:45 +00:00
code = qWorkerProcessDeleteMsg(&handle, pVnode->pQuery, pMsg, &res);
if (code) goto _exit;
res.ctimeMs = taosGetTimestampMs();
2023-02-03 06:46:45 +00:00
// malloc and encode
tEncodeSize(tEncodeDeleteRes, &res, size, ret);
pCont = rpcMallocCont(size + sizeof(SMsgHead));
2022-06-28 10:01:56 +00:00
2023-02-03 06:46:45 +00:00
((SMsgHead *)pCont)->contLen = size + sizeof(SMsgHead);
((SMsgHead *)pCont)->vgId = TD_VID(pVnode);
2022-06-28 10:01:56 +00:00
2023-02-03 06:46:45 +00:00
tEncoderInit(pCoder, pCont + sizeof(SMsgHead), size);
2024-09-26 02:43:16 +00:00
if (tEncodeDeleteRes(pCoder, &res) != 0) {
vError("vgId:%d %s failed to encode delete response", TD_VID(pVnode), __func__);
}
2023-02-03 06:46:45 +00:00
tEncoderClear(pCoder);
2022-06-28 10:01:56 +00:00
2023-02-03 06:46:45 +00:00
rpcFreeCont(pMsg->pCont);
pMsg->pCont = pCont;
pMsg->contLen = size + sizeof(SMsgHead);
2022-06-28 10:01:56 +00:00
2023-02-03 06:46:45 +00:00
taosArrayDestroy(res.uidList);
_exit:
return code;
}
2022-06-28 10:01:56 +00:00
static int32_t vnodePreProcessBatchDeleteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
int32_t code = 0;
int32_t lino = 0;
int64_t ctimeMs = taosGetTimestampMs();
SBatchDeleteReq pReq = {0};
SDecoder *pCoder = &(SDecoder){0};
tDecoderInit(pCoder, (uint8_t *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead));
if (tDecodeSBatchDeleteReqSetCtime(pCoder, &pReq, ctimeMs) < 0) {
code = TSDB_CODE_INVALID_MSG;
}
tDecoderClear(pCoder);
taosArrayDestroy(pReq.deleteReqs);
if (code) {
vError("vgId:%d %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code));
} else {
vTrace("vgId:%d %s done, ctimeMs generated:%" PRId64, TD_VID(pVnode), __func__, ctimeMs);
}
return code;
}
static int32_t vnodePreProcessArbCheckSyncMsg(SVnode *pVnode, SRpcMsg *pMsg) {
SVArbCheckSyncReq syncReq = {0};
if (tDeserializeSVArbCheckSyncReq((char *)pMsg->pCont + sizeof(SMsgHead), pMsg->contLen - sizeof(SMsgHead),
&syncReq) != 0) {
return TSDB_CODE_INVALID_MSG;
}
2024-09-24 02:06:30 +00:00
int32_t ret = vnodePreCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token);
if (ret != 0) {
vError("vgId:%d, failed to preprocess arb check sync request since %s", TD_VID(pVnode), tstrerror(ret));
}
int32_t code = terrno;
tFreeSVArbCheckSyncReq(&syncReq);
return code;
}
2023-02-03 06:46:45 +00:00
int32_t vnodePreProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
int32_t code = 0;
switch (pMsg->msgType) {
case TDMT_VND_CREATE_TABLE: {
code = vnodePreProcessCreateTableMsg(pVnode, pMsg);
} break;
case TDMT_VND_ALTER_TABLE: {
code = vnodePreProcessAlterTableMsg(pVnode, pMsg);
} break;
2024-03-15 01:09:16 +00:00
case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
2023-09-01 05:24:47 +00:00
case TDMT_VND_DROP_TTL_TABLE: {
code = vnodePreProcessDropTtlMsg(pVnode, pMsg);
} break;
2023-02-03 06:46:45 +00:00
case TDMT_VND_SUBMIT: {
code = vnodePreProcessSubmitMsg(pVnode, pMsg);
} break;
case TDMT_VND_DELETE: {
code = vnodePreProcessDeleteMsg(pVnode, pMsg);
2022-06-28 10:01:56 +00:00
} break;
case TDMT_VND_BATCH_DEL: {
code = vnodePreProcessBatchDeleteMsg(pVnode, pMsg);
} break;
case TDMT_VND_ARB_CHECK_SYNC: {
code = vnodePreProcessArbCheckSyncMsg(pVnode, pMsg);
} break;
2022-05-23 08:38:05 +00:00
default:
break;
}
2022-04-19 02:37:45 +00:00
if (code && code != TSDB_CODE_MSG_PREPROCESSED) {
2023-06-26 10:43:00 +00:00
vError("vgId:%d, failed to preprocess write request since %s, msg type:%s", TD_VID(pVnode), tstrerror(code),
TMSG_INFO(pMsg->msgType));
2023-02-03 06:46:45 +00:00
}
2022-06-28 10:01:56 +00:00
return code;
2022-04-14 08:42:50 +00:00
}
2023-05-23 11:10:50 +00:00
int32_t vnodeProcessWriteMsg(SVnode *pVnode, SRpcMsg *pMsg, int64_t ver, SRpcMsg *pRsp) {
2024-07-25 03:45:15 +00:00
int32_t code = 0;
void *ptr = NULL;
void *pReq;
int32_t len;
(void)taosThreadMutexLock(&pVnode->mutex);
if (pVnode->disableWrite) {
(void)taosThreadMutexUnlock(&pVnode->mutex);
vError("vgId:%d write is disabled for snapshot, version:%" PRId64, TD_VID(pVnode), ver);
return TSDB_CODE_VND_WRITE_DISABLED;
}
(void)taosThreadMutexUnlock(&pVnode->mutex);
2023-05-23 11:10:50 +00:00
if (ver <= pVnode->state.applied) {
vError("vgId:%d, duplicate write request. ver: %" PRId64 ", applied: %" PRId64 "", TD_VID(pVnode), ver,
pVnode->state.applied);
2024-07-25 03:45:15 +00:00
return terrno = TSDB_CODE_VND_DUP_REQUEST;
}
2023-09-01 05:24:47 +00:00
vDebug("vgId:%d, start to process write request %s, index:%" PRId64 ", applied:%" PRId64 ", state.applyTerm:%" PRId64
", conn.applyTerm:%" PRId64,
TD_VID(pVnode), TMSG_INFO(pMsg->msgType), ver, pVnode->state.applied, pVnode->state.applyTerm,
pMsg->info.conn.applyTerm);
2022-04-14 08:42:50 +00:00
2024-08-22 09:28:05 +00:00
if (!(pVnode->state.applyTerm <= pMsg->info.conn.applyTerm)) {
return terrno = TSDB_CODE_INTERNAL_ERROR;
}
if (!(pVnode->state.applied + 1 == ver)) {
return terrno = TSDB_CODE_INTERNAL_ERROR;
}
2023-05-23 11:10:50 +00:00
atomic_store_64(&pVnode->state.applied, ver);
atomic_store_64(&pVnode->state.applyTerm, pMsg->info.conn.applyTerm);
2022-04-21 03:47:58 +00:00
if (!syncUtilUserCommit(pMsg->msgType)) goto _exit;
2022-04-20 08:50:45 +00:00
// skip header
pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
len = pMsg->contLen - sizeof(SMsgHead);
2022-12-23 12:16:23 +00:00
bool needCommit = false;
2022-04-14 08:42:50 +00:00
switch (pMsg->msgType) {
2022-04-20 08:50:45 +00:00
/* META */
2022-04-14 10:21:06 +00:00
case TDMT_VND_CREATE_STB:
2023-05-23 11:10:50 +00:00
if (vnodeProcessCreateStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2022-04-19 03:45:22 +00:00
break;
2022-04-14 10:21:06 +00:00
case TDMT_VND_ALTER_STB:
2023-05-23 11:10:50 +00:00
if (vnodeProcessAlterStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2022-04-19 03:45:22 +00:00
break;
2022-04-14 08:42:50 +00:00
case TDMT_VND_DROP_STB:
2023-05-23 11:10:50 +00:00
if (vnodeProcessDropStbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2022-04-14 08:42:50 +00:00
break;
2022-04-20 08:50:45 +00:00
case TDMT_VND_CREATE_TABLE:
2023-11-10 08:23:09 +00:00
if (vnodeProcessCreateTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
2022-04-20 08:50:45 +00:00
break;
case TDMT_VND_ALTER_TABLE:
2023-05-23 11:10:50 +00:00
if (vnodeProcessAlterTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2022-04-20 08:50:45 +00:00
break;
2022-04-14 08:42:50 +00:00
case TDMT_VND_DROP_TABLE:
2023-11-10 08:23:09 +00:00
if (vnodeProcessDropTbReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
2022-04-14 08:42:50 +00:00
break;
case TDMT_VND_DROP_TTL_TABLE:
2023-05-23 11:10:50 +00:00
if (vnodeProcessDropTtlTbReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
break;
2024-03-15 01:09:16 +00:00
case TDMT_VND_FETCH_TTL_EXPIRED_TBS:
if (vnodeProcessFetchTtlExpiredTbs(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
break;
2022-07-11 07:16:10 +00:00
case TDMT_VND_TRIM:
2023-05-23 11:10:50 +00:00
if (vnodeProcessTrimReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2022-07-11 07:16:10 +00:00
break;
case TDMT_VND_S3MIGRATE:
if (vnodeProcessS3MigrateReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
break;
2022-07-11 07:16:10 +00:00
case TDMT_VND_CREATE_SMA:
2023-05-23 11:10:50 +00:00
if (vnodeProcessCreateTSmaReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
2022-07-11 07:16:10 +00:00
break;
2022-04-20 08:50:45 +00:00
/* TSDB */
2022-04-14 08:42:50 +00:00
case TDMT_VND_SUBMIT:
2024-01-24 09:44:44 +00:00
if (vnodeProcessSubmitReq(pVnode, ver, pMsg->pCont, pMsg->contLen, pRsp, pMsg) < 0) goto _err;
2022-04-19 03:45:22 +00:00
break;
2022-06-05 12:33:21 +00:00
case TDMT_VND_DELETE:
2024-01-24 09:44:44 +00:00
if (vnodeProcessDeleteReq(pVnode, ver, pReq, len, pRsp, pMsg) < 0) goto _err;
2022-06-05 12:33:21 +00:00
break;
case TDMT_VND_BATCH_DEL:
2023-05-23 11:10:50 +00:00
if (vnodeProcessBatchDeleteReq(pVnode, ver, pReq, len, pRsp) < 0) goto _err;
break;
2022-04-20 08:50:45 +00:00
/* TQ */
2022-10-31 10:37:28 +00:00
case TDMT_VND_TMQ_SUBSCRIBE:
2023-05-23 11:10:50 +00:00
if (tqProcessSubscribeReq(pVnode->pTq, ver, pReq, len) < 0) {
2022-06-14 05:58:40 +00:00
goto _err;
2022-04-20 13:36:55 +00:00
}
break;
2022-10-31 10:37:28 +00:00
case TDMT_VND_TMQ_DELETE_SUB:
2023-05-23 11:10:50 +00:00
if (tqProcessDeleteSubReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
2022-06-14 05:58:40 +00:00
goto _err;
}
break;
2022-10-31 10:37:28 +00:00
case TDMT_VND_TMQ_COMMIT_OFFSET:
if (tqProcessOffsetCommitReq(pVnode->pTq, ver, pReq, len) < 0) {
goto _err;
}
break;
2022-10-31 10:37:28 +00:00
case TDMT_VND_TMQ_ADD_CHECKINFO:
2023-05-23 11:10:50 +00:00
if (tqProcessAddCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
goto _err;
}
break;
2022-10-31 10:37:28 +00:00
case TDMT_VND_TMQ_DEL_CHECKINFO:
2023-05-23 11:10:50 +00:00
if (tqProcessDelCheckInfoReq(pVnode->pTq, ver, pReq, len) < 0) {
2022-07-22 08:05:28 +00:00
goto _err;
}
break;
2022-06-07 08:24:50 +00:00
case TDMT_STREAM_TASK_DEPLOY: {
int32_t code = tqProcessTaskDeployReq(pVnode->pTq, ver, pReq, len);
if (code != TSDB_CODE_SUCCESS) {
terrno = code;
2022-06-14 05:58:40 +00:00
goto _err;
2022-04-14 08:42:50 +00:00
}
} break;
2022-06-21 03:32:36 +00:00
case TDMT_STREAM_TASK_DROP: {
if (tqProcessTaskDropReq(pVnode->pTq, pMsg->pCont, pMsg->contLen) < 0) {
2022-06-15 13:13:47 +00:00
goto _err;
}
} break;
case TDMT_STREAM_TASK_UPDATE_CHKPT: {
if (tqProcessTaskUpdateCheckpointReq(pVnode->pTq, pMsg->pCont, pMsg->contLen) < 0) {
goto _err;
}
} break;
case TDMT_STREAM_CONSEN_CHKPT: {
if (pVnode->restored) {
2024-09-26 02:43:16 +00:00
if (tqProcessTaskConsenChkptIdReq(pVnode->pTq, pMsg) < 0) {
goto _err;
}
}
} break;
2023-03-28 11:39:38 +00:00
case TDMT_STREAM_TASK_PAUSE: {
2023-09-01 05:24:47 +00:00
if (pVnode->restored && vnodeIsLeader(pVnode) &&
tqProcessTaskPauseReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
2023-03-28 11:39:38 +00:00
goto _err;
}
} break;
case TDMT_STREAM_TASK_RESUME: {
2023-09-01 05:24:47 +00:00
if (pVnode->restored && vnodeIsLeader(pVnode) &&
tqProcessTaskResumeReq(pVnode->pTq, ver, pMsg->pCont, pMsg->contLen) < 0) {
2023-03-28 11:39:38 +00:00
goto _err;
}
} break;
case TDMT_VND_STREAM_TASK_RESET: {
if (pVnode->restored && vnodeIsLeader(pVnode)) {
2024-09-26 02:43:16 +00:00
if (tqProcessTaskResetReq(pVnode->pTq, pMsg) < 0) {
goto _err;
}
}
} break;
case TDMT_VND_ALTER_CONFIRM:
needCommit = pVnode->config.hashChange;
2023-05-23 11:10:50 +00:00
if (vnodeProcessAlterConfirmReq(pVnode, ver, pReq, len, pRsp) < 0) {
goto _err;
}
break;
case TDMT_VND_ALTER_CONFIG:
2023-05-23 11:10:50 +00:00
vnodeProcessAlterConfigReq(pVnode, ver, pReq, len, pRsp);
2022-04-20 06:58:14 +00:00
break;
2022-06-29 09:40:33 +00:00
case TDMT_VND_COMMIT:
2022-12-23 12:16:23 +00:00
needCommit = true;
break;
2023-01-05 07:39:35 +00:00
case TDMT_VND_CREATE_INDEX:
2023-05-23 11:10:50 +00:00
vnodeProcessCreateIndexReq(pVnode, ver, pReq, len, pRsp);
2023-01-05 07:39:35 +00:00
break;
case TDMT_VND_DROP_INDEX:
2023-05-23 11:10:50 +00:00
vnodeProcessDropIndexReq(pVnode, ver, pReq, len, pRsp);
2023-01-05 07:39:35 +00:00
break;
2023-09-01 05:24:47 +00:00
case TDMT_VND_STREAM_CHECK_POINT_SOURCE:
tqProcessTaskCheckPointSourceReq(pVnode->pTq, pMsg, pRsp);
2023-09-01 05:24:47 +00:00
break;
case TDMT_VND_STREAM_TASK_UPDATE:
tqProcessTaskUpdateReq(pVnode->pTq, pMsg);
break;
2022-12-19 07:23:12 +00:00
case TDMT_VND_COMPACT:
2023-05-23 11:10:50 +00:00
vnodeProcessCompactVnodeReq(pVnode, ver, pReq, len, pRsp);
2022-12-19 07:23:12 +00:00
goto _exit;
2023-09-01 05:24:47 +00:00
case TDMT_SYNC_CONFIG_CHANGE:
vnodeProcessConfigChangeReq(pVnode, ver, pReq, len, pRsp);
break;
2023-12-08 09:26:44 +00:00
#ifdef TD_ENTERPRISE
2023-12-07 09:37:47 +00:00
case TDMT_VND_KILL_COMPACT:
2023-12-08 09:26:44 +00:00
vnodeProcessKillCompactReq(pVnode, ver, pReq, len, pRsp);
2023-12-07 09:37:47 +00:00
break;
#endif
/* ARB */
case TDMT_VND_ARB_CHECK_SYNC:
vnodeProcessArbCheckSyncReq(pVnode, pReq, len, pRsp);
break;
2022-04-14 08:42:50 +00:00
default:
2022-11-01 13:22:36 +00:00
vError("vgId:%d, unprocessed msg, %d", TD_VID(pVnode), pMsg->msgType);
2024-07-25 03:45:15 +00:00
return TSDB_CODE_INVALID_MSG;
2022-04-14 08:42:50 +00:00
}
2022-10-31 13:33:06 +00:00
vTrace("vgId:%d, process %s request, code:0x%x index:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType), pRsp->code,
2023-05-23 11:10:50 +00:00
ver);
2022-04-14 08:42:50 +00:00
2024-09-24 02:06:30 +00:00
walApplyVer(pVnode->pWal, ver);
2024-07-25 03:45:15 +00:00
code = tqPushMsg(pVnode->pTq, pMsg->msgType);
if (code) {
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to push msg to TQ since %s", TD_VID(pVnode), tstrerror(terrno));
2024-07-25 03:45:15 +00:00
return code;
}
2022-04-24 07:34:53 +00:00
// commit if need
2022-12-23 12:16:23 +00:00
if (needCommit) {
2023-05-23 11:10:50 +00:00
vInfo("vgId:%d, commit at version %" PRId64, TD_VID(pVnode), ver);
2024-07-25 03:45:15 +00:00
code = vnodeAsyncCommit(pVnode);
if (code) {
vError("vgId:%d, failed to vnode async commit since %s.", TD_VID(pVnode), tstrerror(terrno));
goto _err;
}
2022-04-24 07:34:53 +00:00
// start a new one
2024-07-25 03:45:15 +00:00
code = vnodeBegin(pVnode);
if (code) {
2022-10-17 01:58:34 +00:00
vError("vgId:%d, failed to begin vnode since %s.", TD_VID(pVnode), tstrerror(terrno));
goto _err;
}
2022-04-14 08:42:50 +00:00
}
2022-12-02 07:46:36 +00:00
_exit:
2022-04-14 08:42:50 +00:00
return 0;
2022-04-21 03:47:58 +00:00
_err:
2023-05-23 11:10:50 +00:00
vError("vgId:%d, process %s request failed since %s, ver:%" PRId64, TD_VID(pVnode), TMSG_INFO(pMsg->msgType),
tstrerror(terrno), ver);
2024-07-25 03:45:15 +00:00
return code;
2022-04-14 08:42:50 +00:00
}
int32_t vnodePreprocessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2022-06-29 02:51:22 +00:00
if (TDMT_SCH_QUERY != pMsg->msgType && TDMT_SCH_MERGE_QUERY != pMsg->msgType) {
2022-06-02 13:02:02 +00:00
return 0;
}
return qWorkerPreprocessQueryMsg(pVnode->pQuery, pMsg, TDMT_SCH_QUERY == pMsg->msgType);
2022-06-02 13:02:02 +00:00
}
2024-07-08 07:39:25 +00:00
int32_t vnodeProcessQueryMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
2022-04-24 12:11:34 +00:00
vTrace("message in vnode query queue is processing");
if ((pMsg->msgType == TDMT_SCH_QUERY || pMsg->msgType == TDMT_VND_TMQ_CONSUME) && !syncIsReadyForRead(pVnode->sync)) {
2022-12-02 14:45:11 +00:00
vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
2022-07-06 07:15:55 +00:00
return 0;
}
if (pMsg->msgType == TDMT_VND_TMQ_CONSUME && !pVnode->restored) {
vnodeRedirectRpcMsg(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
return 0;
}
SReadHandle handle = {.vnode = pVnode, .pMsgCb = &pVnode->msgCb, .pWorkerCb = pInfo->workerCb};
initStorageAPI(&handle.api);
2022-04-14 08:42:50 +00:00
switch (pMsg->msgType) {
2022-06-28 02:34:51 +00:00
case TDMT_SCH_QUERY:
2022-06-29 02:51:22 +00:00
case TDMT_SCH_MERGE_QUERY:
2022-05-28 14:13:26 +00:00
return qWorkerProcessQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
2022-06-28 02:34:51 +00:00
case TDMT_SCH_QUERY_CONTINUE:
2022-05-28 14:13:26 +00:00
return qWorkerProcessCQueryMsg(&handle, pVnode->pQuery, pMsg, 0);
case TDMT_VND_TMQ_CONSUME:
return tqProcessPollReq(pVnode->pTq, pMsg);
case TDMT_VND_TMQ_CONSUME_PUSH:
return tqProcessPollPush(pVnode->pTq, pMsg);
2022-04-14 08:42:50 +00:00
default:
vError("unknown msg type:%d in query queue", pMsg->msgType);
2022-11-02 07:28:32 +00:00
return TSDB_CODE_APP_ERROR;
2022-04-14 08:42:50 +00:00
}
}
int32_t vnodeProcessFetchMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
2022-08-02 07:57:37 +00:00
vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
2022-08-02 14:23:33 +00:00
if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
2024-09-17 07:41:10 +00:00
pMsg->msgType == TDMT_VND_BATCH_META || pMsg->msgType == TDMT_VND_TABLE_NAME) &&
2022-11-03 05:57:57 +00:00
!syncIsReadyForRead(pVnode->sync)) {
2022-12-02 14:45:11 +00:00
vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
2022-07-06 07:15:55 +00:00
return 0;
}
2022-04-14 08:42:50 +00:00
switch (pMsg->msgType) {
2022-06-28 02:34:51 +00:00
case TDMT_SCH_FETCH:
2022-07-06 12:33:23 +00:00
case TDMT_SCH_MERGE_FETCH:
2022-05-28 14:13:26 +00:00
return qWorkerProcessFetchMsg(pVnode, pVnode->pQuery, pMsg, 0);
2022-06-29 09:15:08 +00:00
case TDMT_SCH_FETCH_RSP:
2022-07-06 13:00:31 +00:00
return qWorkerProcessRspMsg(pVnode, pVnode->pQuery, pMsg, 0);
2022-10-28 09:03:17 +00:00
// case TDMT_SCH_CANCEL_TASK:
// return qWorkerProcessCancelMsg(pVnode, pVnode->pQuery, pMsg, 0);
2022-06-28 02:34:51 +00:00
case TDMT_SCH_DROP_TASK:
2022-05-28 14:13:26 +00:00
return qWorkerProcessDropMsg(pVnode, pVnode->pQuery, pMsg, 0);
2023-09-01 05:24:47 +00:00
case TDMT_SCH_TASK_NOTIFY:
return qWorkerProcessNotifyMsg(pVnode, pVnode->pQuery, pMsg, 0);
2022-06-28 02:34:51 +00:00
case TDMT_SCH_QUERY_HEARTBEAT:
2022-05-28 14:13:26 +00:00
return qWorkerProcessHbMsg(pVnode, pVnode->pQuery, pMsg, 0);
2022-04-14 08:42:50 +00:00
case TDMT_VND_TABLE_META:
2024-09-17 07:41:10 +00:00
case TDMT_VND_TABLE_NAME:
2022-07-26 10:00:50 +00:00
return vnodeGetTableMeta(pVnode, pMsg, true);
2022-06-20 05:32:21 +00:00
case TDMT_VND_TABLE_CFG:
2022-07-26 10:00:50 +00:00
return vnodeGetTableCfg(pVnode, pMsg, true);
case TDMT_VND_BATCH_META:
return vnodeGetBatchMeta(pVnode, pMsg);
2023-12-08 09:26:44 +00:00
#ifdef TD_ENTERPRISE
2023-12-07 11:42:32 +00:00
case TDMT_VND_QUERY_COMPACT_PROGRESS:
return vnodeQueryCompactProgress(pVnode, pMsg);
2023-12-08 09:26:44 +00:00
#endif
2023-07-03 06:13:01 +00:00
// case TDMT_VND_TMQ_CONSUME:
// return tqProcessPollReq(pVnode->pTq, pMsg);
case TDMT_VND_TMQ_VG_WALINFO:
return tqProcessVgWalInfoReq(pVnode->pTq, pMsg);
2023-07-31 10:48:50 +00:00
case TDMT_VND_TMQ_VG_COMMITTEDINFO:
return tqProcessVgCommittedInfoReq(pVnode->pTq, pMsg);
case TDMT_VND_TMQ_SEEK:
return tqProcessSeekReq(pVnode->pTq, pMsg);
default:
vError("unknown msg type:%d in fetch queue", pMsg->msgType);
return TSDB_CODE_APP_ERROR;
}
}
int32_t vnodeProcessStreamMsg(SVnode *pVnode, SRpcMsg *pMsg, SQueueInfo *pInfo) {
vTrace("vgId:%d, msg:%p in fetch queue is processing", pVnode->config.vgId, pMsg);
if ((pMsg->msgType == TDMT_SCH_FETCH || pMsg->msgType == TDMT_VND_TABLE_META || pMsg->msgType == TDMT_VND_TABLE_CFG ||
pMsg->msgType == TDMT_VND_BATCH_META) &&
!syncIsReadyForRead(pVnode->sync)) {
vnodeRedirectRpcMsg(pVnode, pMsg, terrno);
return 0;
}
switch (pMsg->msgType) {
2022-06-07 08:24:50 +00:00
case TDMT_STREAM_TASK_RUN:
return tqProcessTaskRunReq(pVnode->pTq, pMsg);
case TDMT_STREAM_TASK_DISPATCH:
return tqProcessTaskDispatchReq(pVnode->pTq, pMsg);
2022-06-07 08:24:50 +00:00
case TDMT_STREAM_TASK_DISPATCH_RSP:
2022-05-20 10:53:21 +00:00
return tqProcessTaskDispatchRsp(pVnode->pTq, pMsg);
2023-09-01 05:24:47 +00:00
case TDMT_VND_STREAM_TASK_CHECK:
2023-09-26 04:15:12 +00:00
return tqProcessTaskCheckReq(pVnode->pTq, pMsg);
2023-09-01 05:24:47 +00:00
case TDMT_VND_STREAM_TASK_CHECK_RSP:
2023-09-26 04:15:12 +00:00
return tqProcessTaskCheckRsp(pVnode->pTq, pMsg);
2022-10-31 10:37:28 +00:00
case TDMT_STREAM_RETRIEVE:
return tqProcessTaskRetrieveReq(pVnode->pTq, pMsg);
2022-06-22 09:56:46 +00:00
case TDMT_STREAM_RETRIEVE_RSP:
return tqProcessTaskRetrieveRsp(pVnode->pTq, pMsg);
2023-06-12 14:48:23 +00:00
case TDMT_VND_STREAM_SCAN_HISTORY:
return tqProcessTaskScanHistory(pVnode->pTq, pMsg);
2023-09-01 05:24:47 +00:00
case TDMT_STREAM_TASK_CHECKPOINT_READY:
2023-09-26 04:15:12 +00:00
return tqProcessTaskCheckpointReadyMsg(pVnode->pTq, pMsg);
case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
return tqProcessTaskCheckpointReadyRsp(pVnode->pTq, pMsg);
case TDMT_STREAM_RETRIEVE_TRIGGER:
return tqProcessTaskRetrieveTriggerReq(pVnode->pTq, pMsg);
case TDMT_STREAM_RETRIEVE_TRIGGER_RSP:
return tqProcessTaskRetrieveTriggerRsp(pVnode->pTq, pMsg);
case TDMT_MND_STREAM_HEARTBEAT_RSP:
return tqProcessStreamHbRsp(pVnode->pTq, pMsg);
case TDMT_MND_STREAM_REQ_CHKPT_RSP:
return tqProcessStreamReqCheckpointRsp(pVnode->pTq, pMsg);
2024-02-07 03:40:47 +00:00
case TDMT_VND_GET_STREAM_PROGRESS:
return tqStreamProgressRetrieveReq(pVnode->pTq, pMsg);
case TDMT_MND_STREAM_CHKPT_REPORT_RSP:
return tqProcessTaskChkptReportRsp(pVnode->pTq, pMsg);
2022-04-14 08:42:50 +00:00
default:
2023-06-12 14:48:23 +00:00
vError("unknown msg type:%d in stream queue", pMsg->msgType);
2022-11-02 07:28:32 +00:00
return TSDB_CODE_APP_ERROR;
2022-04-14 08:42:50 +00:00
}
}
void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) {
2024-09-24 02:06:30 +00:00
int32_t code = tdProcessTSmaInsert(((SVnode *)pVnode)->pSma, smaId, (const char *)data);
if (code) {
vError("failed to process sma result since %s", tstrerror(code));
}
2022-04-14 08:42:50 +00:00
}
2022-06-01 12:28:29 +00:00
void vnodeUpdateMetaRsp(SVnode *pVnode, STableMetaRsp *pMetaRsp) {
if (NULL == pMetaRsp) {
return;
}
2022-09-02 07:34:16 +00:00
2022-06-01 12:28:29 +00:00
strcpy(pMetaRsp->dbFName, pVnode->config.dbname);
pMetaRsp->dbId = pVnode->config.dbId;
pMetaRsp->vgId = TD_VID(pVnode);
pMetaRsp->precision = pVnode->config.tsdbCfg.precision;
}
2024-05-23 09:57:07 +00:00
extern int32_t vnodeAsyncRetention(SVnode *pVnode, int64_t now);
2023-06-20 10:23:58 +00:00
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessTrimReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2022-07-11 08:17:03 +00:00
int32_t code = 0;
2022-07-11 07:16:10 +00:00
SVTrimDbReq trimReq = {0};
2022-07-11 08:17:03 +00:00
// decode
if (tDeserializeSVTrimDbReq(pReq, len, &trimReq) != 0) {
code = TSDB_CODE_INVALID_MSG;
goto _exit;
2022-07-11 07:16:10 +00:00
}
2022-08-24 02:57:06 +00:00
vInfo("vgId:%d, trim vnode request will be processed, time:%d", pVnode->config.vgId, trimReq.timestamp);
2024-05-23 09:57:07 +00:00
code = vnodeAsyncRetention(pVnode, trimReq.timestamp);
2022-08-02 08:13:36 +00:00
2022-07-11 08:17:03 +00:00
_exit:
return code;
2022-07-11 07:16:10 +00:00
}
2024-05-23 09:57:07 +00:00
extern int32_t vnodeAsyncS3Migrate(SVnode *pVnode, int64_t now);
static int32_t vnodeProcessS3MigrateReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
int32_t code = 0;
SVS3MigrateDbReq s3migrateReq = {0};
// decode
if (tDeserializeSVS3MigrateDbReq(pReq, len, &s3migrateReq) != 0) {
code = TSDB_CODE_INVALID_MSG;
goto _exit;
}
vInfo("vgId:%d, s3migrate vnode request will be processed, time:%d", pVnode->config.vgId, s3migrateReq.timestamp);
2024-05-23 09:57:07 +00:00
code = vnodeAsyncS3Migrate(pVnode, s3migrateReq.timestamp);
_exit:
return code;
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessDropTtlTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2024-08-22 01:47:44 +00:00
int ret = 0;
2022-07-11 07:16:10 +00:00
SVDropTtlTableReq ttlReq = {0};
if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
2024-08-22 01:47:44 +00:00
ret = TSDB_CODE_INVALID_MSG;
2022-07-11 07:16:10 +00:00
goto end;
}
2024-08-22 01:47:44 +00:00
if (ttlReq.nUids != taosArrayGetSize(ttlReq.pTbUids)) {
ret = TSDB_CODE_INVALID_MSG;
goto end;
}
2023-09-01 05:24:47 +00:00
if (ttlReq.nUids != 0) {
vInfo("vgId:%d, drop ttl table req will be processed, time:%d, ntbUids:%d", pVnode->config.vgId,
ttlReq.timestampSec, ttlReq.nUids);
2022-06-16 12:45:00 +00:00
}
2023-09-01 05:24:47 +00:00
if (ttlReq.nUids > 0) {
2024-08-27 01:32:48 +00:00
int32_t code = metaDropTables(pVnode->pMeta, ttlReq.pTbUids);
if (code) return code;
2024-09-24 02:06:30 +00:00
code = tqUpdateTbUidList(pVnode->pTq, ttlReq.pTbUids, false);
if (code) {
vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(code));
}
2023-09-01 05:24:47 +00:00
}
2023-02-17 02:38:55 +00:00
end:
2023-09-01 05:24:47 +00:00
taosArrayDestroy(ttlReq.pTbUids);
return ret;
}
2024-05-23 09:57:07 +00:00
static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2024-03-15 01:09:16 +00:00
int32_t code = -1;
SMetaReader mr = {0};
SVDropTtlTableReq ttlReq = {0};
SVFetchTtlExpiredTbsRsp rsp = {0};
SEncoder encoder = {0};
2024-05-23 09:57:07 +00:00
SArray *pNames = NULL;
2024-03-15 01:09:16 +00:00
pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP;
pRsp->code = TSDB_CODE_SUCCESS;
pRsp->pCont = NULL;
pRsp->contLen = 0;
if (tDeserializeSVDropTtlTableReq(pReq, len, &ttlReq) != 0) {
terrno = TSDB_CODE_INVALID_MSG;
goto _end;
}
2024-08-22 09:28:05 +00:00
if (!(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids))) {
terrno = TSDB_CODE_INVALID_MSG;
goto _end;
}
2024-03-15 01:09:16 +00:00
2024-05-23 09:57:07 +00:00
tb_uid_t suid;
char ctbName[TSDB_TABLE_NAME_LEN];
2024-03-18 06:09:17 +00:00
SVDropTbReq expiredTb = {.igNotExists = true};
2024-03-15 01:09:16 +00:00
metaReaderDoInit(&mr, pVnode->pMeta, 0);
rsp.vgId = TD_VID(pVnode);
2024-03-18 06:09:17 +00:00
rsp.pExpiredTbs = taosArrayInit(ttlReq.nUids, sizeof(SVDropTbReq));
2024-03-15 01:09:16 +00:00
if (!rsp.pExpiredTbs) goto _end;
2024-03-18 06:09:17 +00:00
pNames = taosArrayInit(ttlReq.nUids, TSDB_TABLE_NAME_LEN);
if (!pNames) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _end;
}
char buf[TSDB_TABLE_NAME_LEN];
2024-03-15 01:09:16 +00:00
for (int32_t i = 0; i < ttlReq.nUids; ++i) {
2024-05-23 09:57:07 +00:00
tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i);
2024-03-15 01:09:16 +00:00
expiredTb.suid = *uid;
terrno = metaReaderGetTableEntryByUid(&mr, *uid);
if (terrno < 0) goto _end;
2024-03-18 06:09:17 +00:00
strncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
2024-05-23 09:57:07 +00:00
void *p = taosArrayPush(pNames, buf);
2024-07-25 03:45:15 +00:00
if (p == NULL) {
goto _end;
}
2024-03-18 06:09:17 +00:00
expiredTb.name = p;
2024-03-15 01:09:16 +00:00
if (mr.me.type == TSDB_CHILD_TABLE) {
expiredTb.suid = mr.me.ctbEntry.suid;
}
2024-07-25 03:45:15 +00:00
if (taosArrayPush(rsp.pExpiredTbs, &expiredTb) == NULL) {
goto _end;
}
2024-03-15 01:09:16 +00:00
}
int32_t ret = 0;
tEncodeSize(tEncodeVFetchTtlExpiredTbsRsp, &rsp, pRsp->contLen, ret);
pRsp->pCont = rpcMallocCont(pRsp->contLen);
if (pRsp->pCont == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
code = -1;
goto _end;
}
tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
terrno = tEncodeVFetchTtlExpiredTbsRsp(&encoder, &rsp);
tEncoderClear(&encoder);
if (terrno == 0) code = 0;
_end:
metaReaderClear(&mr);
tFreeFetchTtlExpiredTbsRsp(&rsp);
2024-03-18 06:09:17 +00:00
taosArrayDestroy(ttlReq.pTbUids);
if (pNames) taosArrayDestroy(pNames);
2024-03-15 01:09:16 +00:00
pRsp->code = terrno;
return code;
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessCreateStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2024-07-25 03:45:15 +00:00
int32_t code = 0;
2022-04-20 10:03:50 +00:00
SVCreateStbReq req = {0};
2022-05-07 10:03:06 +00:00
SDecoder coder;
2022-04-20 10:03:50 +00:00
2022-04-21 03:47:58 +00:00
pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
pRsp->code = TSDB_CODE_SUCCESS;
pRsp->pCont = NULL;
pRsp->contLen = 0;
// decode and process req
2022-05-07 10:03:06 +00:00
tDecoderInit(&coder, pReq, len);
2022-04-20 10:03:50 +00:00
2024-07-25 03:45:15 +00:00
code = tDecodeSVCreateStbReq(&coder, &req);
if (code) {
pRsp->code = code;
2022-04-21 03:47:58 +00:00
goto _err;
2022-04-20 10:03:50 +00:00
}
2024-07-25 03:45:15 +00:00
code = metaCreateSTable(pVnode->pMeta, ver, &req);
if (code) {
pRsp->code = code;
2022-04-21 03:47:58 +00:00
goto _err;
2022-04-14 10:21:06 +00:00
}
2024-07-25 03:45:15 +00:00
if ((code = tdProcessRSmaCreate(pVnode->pSma, &req)) < 0) {
pRsp->code = code;
goto _err;
}
2022-04-27 11:49:04 +00:00
2022-05-07 10:03:06 +00:00
tDecoderClear(&coder);
2022-04-14 10:21:06 +00:00
return 0;
2022-04-21 03:47:58 +00:00
_err:
2022-05-07 10:03:06 +00:00
tDecoderClear(&coder);
2024-07-25 03:45:15 +00:00
return code;
2022-04-14 10:21:06 +00:00
}
2023-11-15 05:41:00 +00:00
static int32_t vnodeProcessCreateTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
SRpcMsg *pOriginRpc) {
2022-05-07 10:03:06 +00:00
SDecoder decoder = {0};
SEncoder encoder = {0};
int32_t rcode = 0;
2022-04-22 09:42:31 +00:00
SVCreateTbBatchReq req = {0};
SVCreateTbReq *pCreateReq;
2022-04-23 10:50:26 +00:00
SVCreateTbBatchRsp rsp = {0};
SVCreateTbRsp cRsp = {0};
char tbName[TSDB_TABLE_FNAME_LEN];
2022-04-27 11:49:04 +00:00
STbUidStore *pStore = NULL;
SArray *tbUids = NULL;
2023-10-27 09:30:30 +00:00
SArray *tbNames = NULL;
2022-04-21 03:47:58 +00:00
pRsp->msgType = TDMT_VND_CREATE_TABLE_RSP;
2022-04-23 10:50:26 +00:00
pRsp->code = TSDB_CODE_SUCCESS;
pRsp->pCont = NULL;
pRsp->contLen = 0;
2022-04-14 10:21:06 +00:00
2022-04-22 09:42:31 +00:00
// decode
2022-05-07 10:03:06 +00:00
tDecoderInit(&decoder, pReq, len);
if (tDecodeSVCreateTbBatchReq(&decoder, &req) < 0) {
2022-04-22 09:42:31 +00:00
rcode = -1;
terrno = TSDB_CODE_INVALID_MSG;
goto _exit;
}
2022-04-14 10:21:06 +00:00
2022-05-06 08:34:38 +00:00
rsp.pArray = taosArrayInit(req.nReqs, sizeof(cRsp));
tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
tbNames = taosArrayInit(req.nReqs, sizeof(char *));
2023-10-27 09:30:30 +00:00
if (rsp.pArray == NULL || tbUids == NULL || tbNames == NULL) {
2022-04-23 10:50:26 +00:00
rcode = -1;
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
}
2022-04-22 09:42:31 +00:00
// loop to create table
for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2022-04-22 09:42:31 +00:00
pCreateReq = req.pReqs + iReq;
2022-09-01 10:00:41 +00:00
memset(&cRsp, 0, sizeof(cRsp));
2022-04-23 10:50:26 +00:00
2023-11-15 05:41:00 +00:00
if (tsEnableAudit && tsEnableAuditCreateTable) {
char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
2023-11-09 09:51:35 +00:00
if (str == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
rcode = -1;
goto _exit;
}
strcpy(str, pCreateReq->name);
2024-07-25 03:45:15 +00:00
if (taosArrayPush(tbNames, &str) == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
rcode = -1;
goto _exit;
}
2023-11-09 09:51:35 +00:00
}
2022-04-23 10:50:26 +00:00
// validate hash
sprintf(tbName, "%s.%s", pVnode->config.dbname, pCreateReq->name);
if (vnodeValidateTableHash(pVnode, tbName) < 0) {
cRsp.code = TSDB_CODE_VND_HASH_MISMATCH;
2024-07-27 10:03:16 +00:00
if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
rcode = -1;
goto _exit;
}
2024-06-26 02:32:15 +00:00
vError("vgId:%d create-table:%s failed due to hash value mismatch", TD_VID(pVnode), tbName);
2022-04-23 10:50:26 +00:00
continue;
}
// do create table
2023-05-23 11:10:50 +00:00
if (metaCreateTable(pVnode->pMeta, ver, pCreateReq, &cRsp.pMeta) < 0) {
2022-05-06 13:06:10 +00:00
if (pCreateReq->flags & TD_CREATE_IF_NOT_EXISTS && terrno == TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
cRsp.code = TSDB_CODE_SUCCESS;
} else {
cRsp.code = terrno;
}
2022-04-14 10:21:06 +00:00
} else {
2022-04-23 10:50:26 +00:00
cRsp.code = TSDB_CODE_SUCCESS;
2024-09-24 02:06:30 +00:00
if (tdFetchTbUidList(pVnode->pSma, &pStore, pCreateReq->ctb.suid, pCreateReq->uid) < 0) {
vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
}
2024-07-27 10:03:16 +00:00
if (taosArrayPush(tbUids, &pCreateReq->uid) == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
rcode = -1;
goto _exit;
}
2022-09-02 07:34:16 +00:00
vnodeUpdateMetaRsp(pVnode, cRsp.pMeta);
2022-04-14 10:21:06 +00:00
}
2022-04-23 10:50:26 +00:00
2024-07-27 10:03:16 +00:00
if (taosArrayPush(rsp.pArray, &cRsp) == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
rcode = -1;
goto _exit;
}
2022-04-14 10:21:06 +00:00
}
2022-09-16 05:06:57 +00:00
vDebug("vgId:%d, add %d new created tables into query table list", TD_VID(pVnode), (int32_t)taosArrayGetSize(tbUids));
2024-09-24 02:06:30 +00:00
if (tqUpdateTbUidList(pVnode->pTq, tbUids, true) < 0) {
vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
}
if (tdUpdateTbUidList(pVnode->pSma, pStore, true) < 0) {
2022-08-12 13:00:48 +00:00
goto _exit;
}
2024-09-24 02:06:30 +00:00
pStore = tdUidStoreFree(pStore);
2022-04-27 11:49:04 +00:00
2022-04-22 09:42:31 +00:00
// prepare rsp
int32_t ret = 0;
2022-04-27 09:39:54 +00:00
tEncodeSize(tEncodeSVCreateTbBatchRsp, &rsp, pRsp->contLen, ret);
2022-04-23 10:50:26 +00:00
pRsp->pCont = rpcMallocCont(pRsp->contLen);
if (pRsp->pCont == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
rcode = -1;
goto _exit;
}
2022-05-07 10:03:06 +00:00
tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
2024-09-24 02:06:30 +00:00
if (tEncodeSVCreateTbBatchRsp(&encoder, &rsp) != 0) {
vError("vgId:%d, failed to encode create table batch response", TD_VID(pVnode));
}
2022-04-14 10:21:06 +00:00
2023-11-15 05:41:00 +00:00
if (tsEnableAudit && tsEnableAuditCreateTable) {
2023-10-27 09:30:30 +00:00
int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
2024-09-24 05:06:24 +00:00
SName name = {0};
2024-09-26 02:43:16 +00:00
if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) < 0) {
vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
}
2023-10-27 09:30:30 +00:00
SStringBuilder sb = {0};
2023-11-15 05:41:00 +00:00
for (int32_t i = 0; i < tbNames->size; i++) {
char **key = (char **)taosArrayGet(tbNames, i);
2023-10-30 11:34:34 +00:00
taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
2023-11-15 05:41:00 +00:00
if (i < tbNames->size - 1) {
2023-10-27 09:30:30 +00:00
taosStringBuilderAppendChar(&sb, ',');
}
2024-05-23 09:57:07 +00:00
// taosMemoryFreeClear(*key);
2023-10-27 09:30:30 +00:00
}
size_t len = 0;
char *keyJoined = taosStringBuilderGetResult(&sb, &len);
2023-10-27 09:30:30 +00:00
2023-12-07 09:37:47 +00:00
if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
2023-12-14 11:21:41 +00:00
auditAddRecord(pOriginRpc, clusterId, "createTable", name.dbname, "", keyJoined, len);
2023-11-14 11:10:22 +00:00
}
2023-10-27 09:30:30 +00:00
taosStringBuilderDestroy(&sb);
}
2022-04-22 09:42:31 +00:00
_exit:
2024-05-31 09:18:25 +00:00
tDeleteSVCreateTbBatchReq(&req);
taosArrayDestroyEx(rsp.pArray, tFreeSVCreateTbRsp);
taosArrayDestroy(tbUids);
2022-05-07 10:03:06 +00:00
tDecoderClear(&decoder);
tEncoderClear(&encoder);
2024-04-28 06:45:32 +00:00
taosArrayDestroyP(tbNames, taosMemoryFree);
2022-04-22 09:42:31 +00:00
return rcode;
2022-04-14 10:21:06 +00:00
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessAlterStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2024-07-25 03:45:15 +00:00
int32_t code = 0;
2022-05-14 14:29:04 +00:00
SVCreateStbReq req = {0};
SDecoder dc = {0};
pRsp->msgType = TDMT_VND_ALTER_STB_RSP;
pRsp->code = TSDB_CODE_SUCCESS;
pRsp->pCont = NULL;
pRsp->contLen = 0;
tDecoderInit(&dc, pReq, len);
// decode req
2024-07-25 03:45:15 +00:00
code = tDecodeSVCreateStbReq(&dc, &req);
if (code) {
2022-05-14 14:29:04 +00:00
tDecoderClear(&dc);
2024-07-25 03:45:15 +00:00
return code;
2022-04-14 10:21:06 +00:00
}
2022-05-14 14:29:04 +00:00
2024-07-25 03:45:15 +00:00
code = metaAlterSTable(pVnode->pMeta, ver, &req);
if (code) {
pRsp->code = code;
2022-05-14 14:29:04 +00:00
tDecoderClear(&dc);
2024-07-25 03:45:15 +00:00
return code;
2022-05-14 14:29:04 +00:00
}
tDecoderClear(&dc);
2022-04-21 03:47:58 +00:00
return 0;
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessDropStbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2022-04-28 06:20:00 +00:00
SVDropStbReq req = {0};
int32_t rcode = TSDB_CODE_SUCCESS;
2022-05-07 10:03:06 +00:00
SDecoder decoder = {0};
SArray *tbUidList = NULL;
2022-04-28 06:20:00 +00:00
pRsp->msgType = TDMT_VND_CREATE_STB_RSP;
pRsp->pCont = NULL;
pRsp->contLen = 0;
// decode request
2022-05-07 10:03:06 +00:00
tDecoderInit(&decoder, pReq, len);
if (tDecodeSVDropStbReq(&decoder, &req) < 0) {
2022-04-28 06:20:00 +00:00
rcode = TSDB_CODE_INVALID_MSG;
goto _exit;
}
// process request
tbUidList = taosArrayInit(8, sizeof(int64_t));
if (tbUidList == NULL) goto _exit;
2023-05-23 11:10:50 +00:00
if (metaDropSTable(pVnode->pMeta, ver, &req, tbUidList) < 0) {
rcode = terrno;
goto _exit;
}
if (tqUpdateTbUidList(pVnode->pTq, tbUidList, false) < 0) {
rcode = terrno;
goto _exit;
}
2022-04-28 06:20:00 +00:00
if (tdProcessRSmaDrop(pVnode->pSma, &req) < 0) {
rcode = terrno;
goto _exit;
}
2022-04-28 06:20:00 +00:00
// return rsp
_exit:
if (tbUidList) taosArrayDestroy(tbUidList);
2022-04-28 06:20:00 +00:00
pRsp->code = rcode;
2022-05-07 10:03:06 +00:00
tDecoderClear(&decoder);
2022-04-21 03:47:58 +00:00
return 0;
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessAlterTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2022-06-01 12:28:29 +00:00
SVAlterTbReq vAlterTbReq = {0};
SVAlterTbRsp vAlterTbRsp = {0};
SDecoder dc = {0};
int32_t rcode = 0;
int32_t ret;
2022-06-01 12:28:29 +00:00
SEncoder ec = {0};
STableMetaRsp vMetaRsp = {0};
2022-05-16 06:17:56 +00:00
pRsp->msgType = TDMT_VND_ALTER_TABLE_RSP;
pRsp->pCont = NULL;
pRsp->contLen = 0;
pRsp->code = TSDB_CODE_SUCCESS;
tDecoderInit(&dc, pReq, len);
// decode
if (tDecodeSVAlterTbReq(&dc, &vAlterTbReq) < 0) {
2022-05-16 12:13:59 +00:00
vAlterTbRsp.code = TSDB_CODE_INVALID_MSG;
2022-05-16 06:17:56 +00:00
tDecoderClear(&dc);
2022-05-16 12:13:59 +00:00
rcode = -1;
goto _exit;
2022-05-16 06:17:56 +00:00
}
// process
2023-05-23 11:10:50 +00:00
if (metaAlterTable(pVnode->pMeta, ver, &vAlterTbReq, &vMetaRsp) < 0) {
2022-07-13 08:00:06 +00:00
vAlterTbRsp.code = terrno;
2022-05-16 06:17:56 +00:00
tDecoderClear(&dc);
2022-05-16 12:13:59 +00:00
rcode = -1;
goto _exit;
2022-05-16 06:17:56 +00:00
}
tDecoderClear(&dc);
2022-05-16 12:13:59 +00:00
2022-06-01 12:28:29 +00:00
if (NULL != vMetaRsp.pSchemas) {
vnodeUpdateMetaRsp(pVnode, &vMetaRsp);
vAlterTbRsp.pMeta = &vMetaRsp;
}
2022-05-16 12:13:59 +00:00
_exit:
tEncodeSize(tEncodeSVAlterTbRsp, &vAlterTbRsp, pRsp->contLen, ret);
pRsp->pCont = rpcMallocCont(pRsp->contLen);
tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2024-09-24 02:06:30 +00:00
if (tEncodeSVAlterTbRsp(&ec, &vAlterTbRsp) != 0) {
vError("vgId:%d, failed to encode alter table response", TD_VID(pVnode));
}
2022-05-16 12:13:59 +00:00
tEncoderClear(&ec);
2022-07-18 05:31:13 +00:00
if (vMetaRsp.pSchemas) {
taosMemoryFree(vMetaRsp.pSchemas);
taosMemoryFree(vMetaRsp.pSchemaExt);
2022-07-18 05:31:13 +00:00
}
2022-04-21 03:47:58 +00:00
return 0;
}
2023-11-10 08:23:09 +00:00
static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2023-11-15 05:41:00 +00:00
SRpcMsg *pOriginRpc) {
2022-05-04 03:57:16 +00:00
SVDropTbBatchReq req = {0};
SVDropTbBatchRsp rsp = {0};
2022-05-07 10:03:06 +00:00
SDecoder decoder = {0};
2022-05-11 08:26:18 +00:00
SEncoder encoder = {0};
int32_t ret;
SArray *tbUids = NULL;
STbUidStore *pStore = NULL;
2023-10-27 09:30:30 +00:00
SArray *tbNames = NULL;
2022-05-04 03:57:16 +00:00
2022-05-06 05:47:03 +00:00
pRsp->msgType = TDMT_VND_DROP_TABLE_RSP;
2022-05-04 03:57:16 +00:00
pRsp->pCont = NULL;
pRsp->contLen = 0;
pRsp->code = TSDB_CODE_SUCCESS;
2022-04-28 04:09:31 +00:00
// decode req
2022-05-07 10:03:06 +00:00
tDecoderInit(&decoder, pReq, len);
ret = tDecodeSVDropTbBatchReq(&decoder, &req);
2022-05-04 03:57:16 +00:00
if (ret < 0) {
terrno = TSDB_CODE_INVALID_MSG;
pRsp->code = terrno;
goto _exit;
}
2022-04-28 04:09:31 +00:00
// process req
tbUids = taosArrayInit(req.nReqs, sizeof(int64_t));
2022-05-11 08:26:18 +00:00
rsp.pArray = taosArrayInit(req.nReqs, sizeof(SVDropTbRsp));
tbNames = taosArrayInit(req.nReqs, sizeof(char *));
2023-10-27 09:30:30 +00:00
if (tbUids == NULL || rsp.pArray == NULL || tbNames == NULL) goto _exit;
for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
2022-05-04 03:57:16 +00:00
SVDropTbReq *pDropTbReq = req.pReqs + iReq;
SVDropTbRsp dropTbRsp = {0};
tb_uid_t tbUid = 0;
2022-04-28 04:09:31 +00:00
2022-05-04 03:57:16 +00:00
/* code */
2023-05-23 11:10:50 +00:00
ret = metaDropTable(pVnode->pMeta, ver, pDropTbReq, tbUids, &tbUid);
2022-05-04 03:57:16 +00:00
if (ret < 0) {
2022-11-02 07:28:32 +00:00
if (pDropTbReq->igNotExists && terrno == TSDB_CODE_TDB_TABLE_NOT_EXIST) {
2022-05-06 05:47:03 +00:00
dropTbRsp.code = TSDB_CODE_SUCCESS;
} else {
dropTbRsp.code = terrno;
}
2022-05-04 03:57:16 +00:00
} else {
2022-05-06 05:47:03 +00:00
dropTbRsp.code = TSDB_CODE_SUCCESS;
2024-09-24 02:06:30 +00:00
if (tbUid > 0) {
if (tdFetchTbUidList(pVnode->pSma, &pStore, pDropTbReq->suid, tbUid) < 0) {
vError("vgId:%d, failed to fetch tbUid list", TD_VID(pVnode));
}
}
2022-05-04 03:57:16 +00:00
}
2024-07-27 10:03:16 +00:00
if (taosArrayPush(rsp.pArray, &dropTbRsp) == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
pRsp->code = terrno;
goto _exit;
}
if (tsEnableAuditCreateTable) {
char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN);
2024-09-18 07:22:09 +00:00
if (str == NULL) {
pRsp->code = terrno;
goto _exit;
}
2023-10-30 11:34:34 +00:00
strcpy(str, pDropTbReq->name);
2024-07-27 10:03:16 +00:00
if (taosArrayPush(tbNames, &str) == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
pRsp->code = terrno;
goto _exit;
}
2023-10-30 11:34:34 +00:00
}
2022-05-04 03:57:16 +00:00
}
2024-09-24 02:06:30 +00:00
if (tqUpdateTbUidList(pVnode->pTq, tbUids, false) < 0) {
vError("vgId:%d, failed to update tbUid list since %s", TD_VID(pVnode), tstrerror(terrno));
}
if (tdUpdateTbUidList(pVnode->pSma, pStore, false) < 0) {
goto _exit;
}
if (tsEnableAuditCreateTable) {
2023-10-27 09:30:30 +00:00
int64_t clusterId = pVnode->config.syncCfg.nodeInfo[0].clusterId;
SName name = {0};
2024-09-24 02:06:30 +00:00
if (tNameFromString(&name, pVnode->config.dbname, T_NAME_ACCT | T_NAME_DB) != 0) {
vError("vgId:%d, failed to get name from string", TD_VID(pVnode));
}
2023-10-27 09:30:30 +00:00
SStringBuilder sb = {0};
for (int32_t iReq = 0; iReq < req.nReqs; iReq++) {
char **key = (char **)taosArrayGet(tbNames, iReq);
2023-10-30 11:34:34 +00:00
taosStringBuilderAppendStringLen(&sb, *key, strlen(*key));
if (iReq < req.nReqs - 1) {
2023-10-27 09:30:30 +00:00
taosStringBuilderAppendChar(&sb, ',');
}
2023-10-30 11:34:34 +00:00
taosMemoryFreeClear(*key);
2023-10-27 09:30:30 +00:00
}
size_t len = 0;
char *keyJoined = taosStringBuilderGetResult(&sb, &len);
2023-10-27 09:30:30 +00:00
2023-12-07 09:37:47 +00:00
if (pOriginRpc->info.conn.user != NULL && strlen(pOriginRpc->info.conn.user) > 0) {
2023-12-14 11:21:41 +00:00
auditAddRecord(pOriginRpc, clusterId, "dropTable", name.dbname, "", keyJoined, len);
2023-11-14 11:10:22 +00:00
}
2023-10-27 09:30:30 +00:00
taosStringBuilderDestroy(&sb);
}
2022-05-04 03:57:16 +00:00
_exit:
taosArrayDestroy(tbUids);
2024-09-24 02:06:30 +00:00
pStore = tdUidStoreFree(pStore);
2022-05-07 10:03:06 +00:00
tDecoderClear(&decoder);
2022-05-11 08:26:18 +00:00
tEncodeSize(tEncodeSVDropTbBatchRsp, &rsp, pRsp->contLen, ret);
pRsp->pCont = rpcMallocCont(pRsp->contLen);
tEncoderInit(&encoder, pRsp->pCont, pRsp->contLen);
2024-09-24 02:06:30 +00:00
if (tEncodeSVDropTbBatchRsp(&encoder, &rsp) != 0) {
vError("vgId:%d, failed to encode drop table batch response", TD_VID(pVnode));
}
2022-05-11 08:26:18 +00:00
tEncoderClear(&encoder);
2022-07-16 09:45:19 +00:00
taosArrayDestroy(rsp.pArray);
2023-10-30 11:34:34 +00:00
taosArrayDestroy(tbNames);
2022-04-16 06:32:34 +00:00
return 0;
}
2023-12-21 06:22:22 +00:00
#ifdef BUILD_NO_CALL
static int32_t vnodeDebugPrintSingleSubmitMsg(SMeta *pMeta, SSubmitBlk *pBlock, SSubmitMsgIter *msgIter,
const char *tags) {
2022-05-11 03:44:09 +00:00
SSubmitBlkIter blkIter = {0};
STSchema *pSchema = NULL;
tb_uid_t suid = 0;
STSRow *row = NULL;
2022-05-25 07:10:56 +00:00
int32_t rv = -1;
2022-05-11 03:44:09 +00:00
tInitSubmitBlkIter(msgIter, pBlock, &blkIter);
if (blkIter.row == NULL) return 0;
int32_t code = metaGetTbTSchemaNotNull(pMeta, msgIter->suid, TD_ROW_SVER(blkIter.row), 1,
&pSchema); // TODO: use the real schema
if (TSDB_CODE_SUCCESS != code) {
2022-05-11 03:44:09 +00:00
printf("%s:%d no valid schema\n", tags, __LINE__);
return code;
2022-05-11 03:44:09 +00:00
}
suid = msgIter->suid;
rv = TD_ROW_SVER(blkIter.row);
2022-05-11 03:44:09 +00:00
char __tags[128] = {0};
snprintf(__tags, 128, "%s: uid %" PRIi64 " ", tags, msgIter->uid);
while ((row = tGetSubmitBlkNext(&blkIter))) {
tdSRowPrint(row, pSchema, __tags);
}
taosMemoryFreeClear(pSchema);
return TSDB_CODE_SUCCESS;
}
2023-12-21 06:22:22 +00:00
#endif
typedef struct SSubmitReqConvertCxt {
SSubmitMsgIter msgIter;
SSubmitBlk *pBlock;
SSubmitBlkIter blkIter;
STSRow *pRow;
STSRowIter rowIter;
SSubmitTbData *pTbData;
STSchema *pTbSchema;
SArray *pColValues;
} SSubmitReqConvertCxt;
static int32_t vnodeResetTableCxt(SMeta *pMeta, SSubmitReqConvertCxt *pCxt) {
taosMemoryFreeClear(pCxt->pTbSchema);
int32_t code = metaGetTbTSchemaNotNull(pMeta, pCxt->msgIter.suid > 0 ? pCxt->msgIter.suid : pCxt->msgIter.uid,
pCxt->msgIter.sversion, 1, &pCxt->pTbSchema);
if (TSDB_CODE_SUCCESS != code) {
return code;
}
tdSTSRowIterInit(&pCxt->rowIter, pCxt->pTbSchema);
2023-05-04 08:15:14 +00:00
tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
if (NULL == pCxt->pTbData) {
pCxt->pTbData = taosMemoryCalloc(1, sizeof(SSubmitTbData));
if (NULL == pCxt->pTbData) {
return terrno;
}
}
pCxt->pTbData->flags = 0;
pCxt->pTbData->suid = pCxt->msgIter.suid;
pCxt->pTbData->uid = pCxt->msgIter.uid;
pCxt->pTbData->sver = pCxt->msgIter.sversion;
pCxt->pTbData->pCreateTbReq = NULL;
pCxt->pTbData->aRowP = taosArrayInit(128, POINTER_BYTES);
if (NULL == pCxt->pTbData->aRowP) {
2024-09-20 00:56:46 +00:00
return terrno;
}
taosArrayDestroy(pCxt->pColValues);
pCxt->pColValues = taosArrayInit(pCxt->pTbSchema->numOfCols, sizeof(SColVal));
if (NULL == pCxt->pColValues) {
2024-07-27 10:03:16 +00:00
return terrno;
}
for (int32_t i = 0; i < pCxt->pTbSchema->numOfCols; ++i) {
SColVal val = COL_VAL_NONE(pCxt->pTbSchema->columns[i].colId, pCxt->pTbSchema->columns[i].type);
2024-07-27 10:03:16 +00:00
if (taosArrayPush(pCxt->pColValues, &val) == NULL) {
return terrno;
}
}
return TSDB_CODE_SUCCESS;
}
static void vnodeDestroySubmitReqConvertCxt(SSubmitReqConvertCxt *pCxt) {
taosMemoryFreeClear(pCxt->pTbSchema);
2023-05-04 08:15:14 +00:00
tDestroySubmitTbData(pCxt->pTbData, TSDB_MSG_FLG_ENCODE);
taosMemoryFreeClear(pCxt->pTbData);
taosArrayDestroy(pCxt->pColValues);
}
static int32_t vnodeCellValConvertToColVal(STColumn *pCol, SCellVal *pCellVal, SColVal *pColVal) {
if (tdValTypeIsNone(pCellVal->valType)) {
pColVal->flag = CV_FLAG_NONE;
return TSDB_CODE_SUCCESS;
}
if (tdValTypeIsNull(pCellVal->valType)) {
pColVal->flag = CV_FLAG_NULL;
return TSDB_CODE_SUCCESS;
}
if (IS_VAR_DATA_TYPE(pCol->type)) {
pColVal->value.nData = varDataLen(pCellVal->val);
pColVal->value.pData = (uint8_t *)varDataVal(pCellVal->val);
} else if (TSDB_DATA_TYPE_FLOAT == pCol->type) {
float f = GET_FLOAT_VAL(pCellVal->val);
memcpy(&pColVal->value.val, &f, sizeof(f));
} else if (TSDB_DATA_TYPE_DOUBLE == pCol->type) {
pColVal->value.val = *(int64_t *)pCellVal->val;
} else {
GET_TYPED_DATA(pColVal->value.val, int64_t, pCol->type, pCellVal->val);
}
pColVal->flag = CV_FLAG_VALUE;
return TSDB_CODE_SUCCESS;
}
static int32_t vnodeTSRowConvertToColValArray(SSubmitReqConvertCxt *pCxt) {
int32_t code = TSDB_CODE_SUCCESS;
tdSTSRowIterReset(&pCxt->rowIter, pCxt->pRow);
for (int32_t i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->pTbSchema->numOfCols; ++i) {
STColumn *pCol = pCxt->pTbSchema->columns + i;
SCellVal cellVal = {0};
if (!tdSTSRowIterFetch(&pCxt->rowIter, pCol->colId, pCol->type, &cellVal)) {
break;
}
code = vnodeCellValConvertToColVal(pCol, &cellVal, (SColVal *)taosArrayGet(pCxt->pColValues, i));
}
return code;
}
static int32_t vnodeDecodeCreateTbReq(SSubmitReqConvertCxt *pCxt) {
if (pCxt->msgIter.schemaLen <= 0) {
return TSDB_CODE_SUCCESS;
}
pCxt->pTbData->pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
if (NULL == pCxt->pTbData->pCreateTbReq) {
return terrno;
}
SDecoder decoder = {0};
tDecoderInit(&decoder, (uint8_t *)pCxt->pBlock->data, pCxt->msgIter.schemaLen);
int32_t code = tDecodeSVCreateTbReq(&decoder, pCxt->pTbData->pCreateTbReq);
tDecoderClear(&decoder);
return code;
}
static int32_t vnodeSubmitReqConvertToSubmitReq2(SVnode *pVnode, SSubmitReq *pReq, SSubmitReq2 *pReq2) {
pReq2->aSubmitTbData = taosArrayInit(128, sizeof(SSubmitTbData));
if (NULL == pReq2->aSubmitTbData) {
2024-09-20 00:56:46 +00:00
return terrno;
}
SSubmitReqConvertCxt cxt = {0};
int32_t code = tInitSubmitMsgIter(pReq, &cxt.msgIter);
while (TSDB_CODE_SUCCESS == code) {
code = tGetSubmitMsgNext(&cxt.msgIter, &cxt.pBlock);
if (TSDB_CODE_SUCCESS == code) {
if (NULL == cxt.pBlock) {
break;
}
code = vnodeResetTableCxt(pVnode->pMeta, &cxt);
}
if (TSDB_CODE_SUCCESS == code) {
code = tInitSubmitBlkIter(&cxt.msgIter, cxt.pBlock, &cxt.blkIter);
}
if (TSDB_CODE_SUCCESS == code) {
code = vnodeDecodeCreateTbReq(&cxt);
}
while (TSDB_CODE_SUCCESS == code && (cxt.pRow = tGetSubmitBlkNext(&cxt.blkIter)) != NULL) {
code = vnodeTSRowConvertToColValArray(&cxt);
if (TSDB_CODE_SUCCESS == code) {
SRow **pNewRow = taosArrayReserve(cxt.pTbData->aRowP, 1);
code = tRowBuild(cxt.pColValues, cxt.pTbSchema, pNewRow);
}
}
if (TSDB_CODE_SUCCESS == code) {
2024-09-20 00:56:46 +00:00
code = (NULL == taosArrayPush(pReq2->aSubmitTbData, cxt.pTbData) ? terrno : TSDB_CODE_SUCCESS);
}
if (TSDB_CODE_SUCCESS == code) {
taosMemoryFreeClear(cxt.pTbData);
}
}
vnodeDestroySubmitReqConvertCxt(&cxt);
return code;
}
static int32_t vnodeRebuildSubmitReqMsg(SSubmitReq2 *pSubmitReq, void **ppMsg) {
int32_t code = TSDB_CODE_SUCCESS;
char *pMsg = NULL;
uint32_t msglen = 0;
2023-05-04 08:15:14 +00:00
tEncodeSize(tEncodeSubmitReq, pSubmitReq, msglen, code);
if (TSDB_CODE_SUCCESS == code) {
pMsg = taosMemoryMalloc(msglen);
if (NULL == pMsg) {
2024-09-20 00:56:46 +00:00
code = terrno;
}
}
if (TSDB_CODE_SUCCESS == code) {
SEncoder encoder;
tEncoderInit(&encoder, (uint8_t *)pMsg, msglen);
2023-05-04 08:15:14 +00:00
code = tEncodeSubmitReq(&encoder, pSubmitReq);
tEncoderClear(&encoder);
}
if (TSDB_CODE_SUCCESS == code) {
*ppMsg = pMsg;
}
return code;
}
static int32_t vnodeProcessSubmitReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2024-03-26 06:43:28 +00:00
SRpcMsg *pOriginalMsg) {
2022-11-28 09:45:07 +00:00
int32_t code = 0;
2022-12-05 07:45:25 +00:00
terrno = 0;
2022-05-09 13:52:39 +00:00
2022-11-29 07:23:11 +00:00
SSubmitReq2 *pSubmitReq = &(SSubmitReq2){0};
2022-11-29 09:29:49 +00:00
SSubmitRsp2 *pSubmitRsp = &(SSubmitRsp2){0};
2022-11-28 09:45:07 +00:00
SArray *newTbUids = NULL;
2022-11-29 09:29:49 +00:00
int32_t ret;
SEncoder ec = {0};
pRsp->code = TSDB_CODE_SUCCESS;
2022-05-12 07:09:27 +00:00
void *pAllocMsg = NULL;
SSubmitReq2Msg *pMsg = (SSubmitReq2Msg *)pReq;
2023-05-23 11:20:25 +00:00
if (0 == pMsg->version) {
code = vnodeSubmitReqConvertToSubmitReq2(pVnode, (SSubmitReq *)pMsg, pSubmitReq);
if (TSDB_CODE_SUCCESS == code) {
code = vnodeRebuildSubmitReqMsg(pSubmitReq, &pReq);
}
if (TSDB_CODE_SUCCESS == code) {
pAllocMsg = pReq;
}
if (TSDB_CODE_SUCCESS != code) {
goto _exit;
}
} else {
// decode
pReq = POINTER_SHIFT(pReq, sizeof(SSubmitReq2Msg));
len -= sizeof(SSubmitReq2Msg);
SDecoder dc = {0};
tDecoderInit(&dc, pReq, len);
2023-05-04 08:15:14 +00:00
if (tDecodeSubmitReq(&dc, pSubmitReq) < 0) {
code = TSDB_CODE_INVALID_MSG;
goto _exit;
}
tDecoderClear(&dc);
2022-05-11 03:44:09 +00:00
}
2022-05-09 13:52:39 +00:00
2023-03-02 10:10:02 +00:00
// scan
TSKEY now = taosGetTimestamp(pVnode->config.tsdbCfg.precision);
TSKEY minKey = now - tsTickPerMin[pVnode->config.tsdbCfg.precision] * pVnode->config.tsdbCfg.keep2;
TSKEY maxKey = tsMaxKeyByPrecision[pVnode->config.tsdbCfg.precision];
for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
2023-03-08 06:45:04 +00:00
if (pSubmitTbData->pCreateTbReq && pSubmitTbData->pCreateTbReq->uid == 0) {
code = TSDB_CODE_INVALID_MSG;
goto _exit;
}
2023-03-02 10:10:02 +00:00
if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
if (TARRAY_SIZE(pSubmitTbData->aCol) <= 0) {
code = TSDB_CODE_INVALID_MSG;
goto _exit;
}
2024-04-10 07:06:43 +00:00
SColData *colDataArr = TARRAY_DATA(pSubmitTbData->aCol);
SRowKey lastKey;
tColDataArrGetRowKey(colDataArr, TARRAY_SIZE(pSubmitTbData->aCol), 0, &lastKey);
for (int32_t iRow = 1; iRow < colDataArr[0].nVal; iRow++) {
SRowKey key;
tColDataArrGetRowKey(TARRAY_DATA(pSubmitTbData->aCol), TARRAY_SIZE(pSubmitTbData->aCol), iRow, &key);
if (tRowKeyCompare(&lastKey, &key) >= 0) {
2023-03-02 10:10:02 +00:00
code = TSDB_CODE_INVALID_MSG;
vError("vgId:%d %s failed 1 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(terrno), ver);
2023-03-02 10:10:02 +00:00
goto _exit;
}
}
} else {
int32_t nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
SRow **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
2024-03-26 06:43:28 +00:00
SRowKey lastRowKey;
2023-03-02 10:10:02 +00:00
for (int32_t iRow = 0; iRow < nRow; ++iRow) {
2024-03-26 06:43:28 +00:00
if (aRow[iRow]->ts < minKey || aRow[iRow]->ts > maxKey) {
2023-03-02 10:10:02 +00:00
code = TSDB_CODE_INVALID_MSG;
vError("vgId:%d %s failed 2 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
2023-03-02 10:10:02 +00:00
goto _exit;
}
2024-03-26 06:43:28 +00:00
if (iRow == 0) {
tRowGetKey(aRow[iRow], &lastRowKey);
} else {
SRowKey rowKey;
tRowGetKey(aRow[iRow], &rowKey);
if (tRowKeyCompare(&lastRowKey, &rowKey) >= 0) {
code = TSDB_CODE_INVALID_MSG;
vError("vgId:%d %s failed 3 since %s, version:%" PRId64, TD_VID(pVnode), __func__, tstrerror(code), ver);
2024-03-26 06:43:28 +00:00
goto _exit;
}
lastRowKey = rowKey;
}
2023-03-02 10:10:02 +00:00
}
}
}
2022-11-29 09:29:49 +00:00
for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
2022-11-29 08:02:33 +00:00
SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
if (pSubmitTbData->pCreateTbReq) {
pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid;
} else {
SMetaInfo info = {0};
code = metaGetInfo(pVnode->pMeta, pSubmitTbData->uid, &info, NULL);
if (code) {
code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
2022-12-05 07:45:25 +00:00
vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), pSubmitTbData->uid);
2022-11-29 08:02:33 +00:00
goto _exit;
}
if (info.suid != pSubmitTbData->suid) {
code = TSDB_CODE_INVALID_MSG;
goto _exit;
}
if (info.suid) {
2024-09-24 10:04:58 +00:00
if (metaGetInfo(pVnode->pMeta, info.suid, &info, NULL) != 0) {
vWarn("vgId:%d, table uid:%" PRId64 " not exists", TD_VID(pVnode), info.suid);
}
2022-11-29 08:02:33 +00:00
}
if (pSubmitTbData->sver != info.skmVer) {
code = TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER;
goto _exit;
}
}
2022-12-03 07:14:12 +00:00
if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
int32_t nColData = TARRAY_SIZE(pSubmitTbData->aCol);
SColData *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
if (nColData <= 0) {
code = TSDB_CODE_INVALID_MSG;
goto _exit;
}
if (aColData[0].cid != PRIMARYKEY_TIMESTAMP_COL_ID || aColData[0].type != TSDB_DATA_TYPE_TIMESTAMP ||
aColData[0].nVal <= 0) {
code = TSDB_CODE_INVALID_MSG;
goto _exit;
}
for (int32_t j = 1; j < nColData; j++) {
if (aColData[j].nVal != aColData[0].nVal) {
2022-12-03 07:14:12 +00:00
code = TSDB_CODE_INVALID_MSG;
goto _exit;
}
}
}
2022-11-29 08:02:33 +00:00
}
2022-12-26 02:14:59 +00:00
vDebug("vgId:%d, submit block size %d", TD_VID(pVnode), (int32_t)taosArrayGetSize(pSubmitReq->aSubmitTbData));
2022-11-29 08:02:33 +00:00
// loop to handle
2022-11-29 09:29:49 +00:00
for (int32_t i = 0; i < TARRAY_SIZE(pSubmitReq->aSubmitTbData); ++i) {
2022-11-29 08:02:33 +00:00
SSubmitTbData *pSubmitTbData = taosArrayGet(pSubmitReq->aSubmitTbData, i);
// create table
if (pSubmitTbData->pCreateTbReq) {
2022-11-29 09:29:49 +00:00
// alloc if need
if (pSubmitRsp->aCreateTbRsp == NULL &&
(pSubmitRsp->aCreateTbRsp = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(SVCreateTbRsp))) ==
NULL) {
2024-09-20 00:56:46 +00:00
code = terrno;
2022-11-29 09:29:49 +00:00
goto _exit;
}
SVCreateTbRsp *pCreateTbRsp = taosArrayReserve(pSubmitRsp->aCreateTbRsp, 1);
// create table
2023-05-23 11:10:50 +00:00
if (metaCreateTable(pVnode->pMeta, ver, pSubmitTbData->pCreateTbReq, &pCreateTbRsp->pMeta) == 0) {
// create table success
2022-11-29 09:29:49 +00:00
if (newTbUids == NULL &&
(newTbUids = taosArrayInit(TARRAY_SIZE(pSubmitReq->aSubmitTbData), sizeof(int64_t))) == NULL) {
2024-09-20 00:56:46 +00:00
code = terrno;
2022-11-29 09:29:49 +00:00
goto _exit;
}
2024-07-27 10:03:16 +00:00
if (taosArrayPush(newTbUids, &pSubmitTbData->uid) == NULL) {
code = terrno;
goto _exit;
}
2022-11-29 09:29:49 +00:00
if (pCreateTbRsp->pMeta) {
2022-12-01 12:22:57 +00:00
vnodeUpdateMetaRsp(pVnode, pCreateTbRsp->pMeta);
2022-11-29 09:29:49 +00:00
}
} else { // create table failed
if (terrno != TSDB_CODE_TDB_TABLE_ALREADY_EXIST) {
code = terrno;
vError("vgId:%d failed to create table:%s, code:%s", TD_VID(pVnode), pSubmitTbData->pCreateTbReq->name,
tstrerror(terrno));
2022-11-29 09:29:49 +00:00
goto _exit;
}
2023-04-23 02:53:28 +00:00
terrno = 0;
pSubmitTbData->uid = pSubmitTbData->pCreateTbReq->uid; // update uid if table exist for using below
2022-11-29 08:41:46 +00:00
}
2022-11-29 08:02:33 +00:00
}
// insert data
2022-11-29 09:29:49 +00:00
int32_t affectedRows;
2023-05-23 11:10:50 +00:00
code = tsdbInsertTableData(pVnode->pTsdb, ver, pSubmitTbData, &affectedRows);
2022-11-29 09:29:49 +00:00
if (code) goto _exit;
2023-09-01 05:24:47 +00:00
code = metaUpdateChangeTimeWithLock(pVnode->pMeta, pSubmitTbData->uid, pSubmitTbData->ctimeMs);
if (code) goto _exit;
2022-11-29 09:29:49 +00:00
pSubmitRsp->affectedRows += affectedRows;
2022-11-29 08:02:33 +00:00
}
2022-11-28 05:41:06 +00:00
// update the affected table uid list
2022-11-29 09:29:49 +00:00
if (taosArrayGetSize(newTbUids) > 0) {
vDebug("vgId:%d, add %d table into query table list in handling submit", TD_VID(pVnode),
(int32_t)taosArrayGetSize(newTbUids));
2024-09-24 02:06:30 +00:00
if (tqUpdateTbUidList(pVnode->pTq, newTbUids, true) != 0) {
vError("vgId:%d, failed to update tbUid list", TD_VID(pVnode));
}
2022-11-28 09:45:07 +00:00
}
2022-11-29 09:29:49 +00:00
_exit:
// message
pRsp->code = code;
tEncodeSize(tEncodeSSubmitRsp2, pSubmitRsp, pRsp->contLen, ret);
pRsp->pCont = rpcMallocCont(pRsp->contLen);
tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2024-09-26 02:43:16 +00:00
if (tEncodeSSubmitRsp2(&ec, pSubmitRsp) < 0) {
vError("vgId:%d, failed to encode submit response", TD_VID(pVnode));
}
2022-11-29 09:29:49 +00:00
tEncoderClear(&ec);
2022-11-29 09:45:50 +00:00
// update statistics
2024-07-29 05:54:50 +00:00
(void)atomic_add_fetch_64(&pVnode->statis.nInsert, pSubmitRsp->affectedRows);
(void)atomic_add_fetch_64(&pVnode->statis.nInsertSuccess, pSubmitRsp->affectedRows);
(void)atomic_add_fetch_64(&pVnode->statis.nBatchInsert, 1);
2024-01-24 09:44:44 +00:00
2024-08-21 07:54:39 +00:00
if (tsEnableMonitor && tsMonitorFqdn[0] != 0 && tsMonitorPort != 0 && pSubmitRsp->affectedRows > 0 &&
strlen(pOriginalMsg->info.conn.user) > 0 && tsInsertCounter != NULL) {
2024-03-26 06:43:28 +00:00
const char *sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT_AFFECTED_ROWS,
pVnode->monitor.strClusterId,
pVnode->monitor.strDnodeId,
tsLocalEp,
pVnode->monitor.strVgId,
pOriginalMsg->info.conn.user,
"Success"};
2024-09-24 02:06:30 +00:00
int tv = taos_counter_add(tsInsertCounter, pSubmitRsp->affectedRows, sample_labels);
2024-02-21 04:12:45 +00:00
}
2024-01-24 09:44:44 +00:00
2022-11-29 09:45:50 +00:00
if (code == 0) {
2024-07-29 05:54:50 +00:00
(void)atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
2023-11-09 11:51:01 +00:00
code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2022-11-29 09:45:50 +00:00
}
2024-01-24 09:44:44 +00:00
/*
if (code == 0) {
atomic_add_fetch_64(&pVnode->statis.nBatchInsertSuccess, 1);
code = tdProcessRSmaSubmit(pVnode->pSma, ver, pSubmitReq, pReq, len);
2022-11-29 09:45:50 +00:00
const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2024-01-24 09:44:44 +00:00
pOriginalMsg->info.conn.user, "Success"};
taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
2023-10-23 08:59:54 +00:00
}
2024-01-24 09:44:44 +00:00
else{
const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_INSERT, pVnode->monitor.strClusterId,
pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2024-01-24 09:44:44 +00:00
pOriginalMsg->info.conn.user, "Failed"};
taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
}
*/
2023-10-23 08:59:54 +00:00
2022-11-29 09:29:49 +00:00
// clear
taosArrayDestroy(newTbUids);
2023-05-04 08:15:14 +00:00
tDestroySubmitReq(pSubmitReq, 0 == pMsg->version ? TSDB_MSG_FLG_CMPT : TSDB_MSG_FLG_DECODE);
2022-11-29 09:45:50 +00:00
tDestroySSubmitRsp2(pSubmitRsp, TSDB_MSG_FLG_ENCODE);
2022-11-29 09:29:49 +00:00
2022-12-05 07:45:25 +00:00
if (code) terrno = code;
taosMemoryFree(pAllocMsg);
2022-11-28 09:45:07 +00:00
return code;
2022-04-20 13:36:55 +00:00
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
SVCreateTSmaReq req = {0};
2022-08-02 08:13:36 +00:00
SDecoder coder = {0};
2022-06-01 07:06:12 +00:00
if (pRsp) {
pRsp->msgType = TDMT_VND_CREATE_SMA_RSP;
pRsp->code = TSDB_CODE_SUCCESS;
pRsp->pCont = NULL;
pRsp->contLen = 0;
}
// decode and process req
tDecoderInit(&coder, pReq, len);
if (tDecodeSVCreateTSmaReq(&coder, &req) < 0) {
2022-06-01 07:06:12 +00:00
terrno = TSDB_CODE_MSG_DECODE_ERROR;
if (pRsp) pRsp->code = terrno;
goto _err;
}
2022-05-01 16:30:47 +00:00
2023-05-23 11:10:50 +00:00
if (tdProcessTSmaCreate(pVnode->pSma, ver, (const char *)&req) < 0) {
2022-06-01 07:06:12 +00:00
if (pRsp) pRsp->code = terrno;
goto _err;
}
2022-05-01 16:30:47 +00:00
tDecoderClear(&coder);
2022-06-02 05:57:39 +00:00
vDebug("vgId:%d, success to create tsma %s:%" PRIi64 " version %" PRIi64 " for table %" PRIi64, TD_VID(pVnode),
2023-05-23 11:10:50 +00:00
req.indexName, req.indexUid, ver, req.tableUid);
2022-04-14 10:21:06 +00:00
return 0;
_err:
tDecoderClear(&coder);
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to create tsma %s:%" PRIi64 " version %" PRIi64 "for table %" PRIi64 " since %s",
2023-05-23 11:10:50 +00:00
TD_VID(pVnode), req.indexName, req.indexUid, ver, req.tableUid, terrstr());
2024-08-14 09:50:20 +00:00
return terrno;
2022-04-20 13:36:55 +00:00
}
2022-06-01 07:06:12 +00:00
/**
* @brief specific for smaDstVnode
*
* @param pVnode
* @param pCont
* @param contLen
* @return int32_t
*/
int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen) {
return vnodeProcessCreateTSmaReq(pVnode, 1, pCont, contLen, NULL);
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeConsolidateAlterHashRange(SVnode *pVnode, int64_t ver) {
2023-05-04 05:51:48 +00:00
int32_t code = TSDB_CODE_SUCCESS;
vInfo("vgId:%d, trim meta of tables per hash range [%" PRIu32 ", %" PRIu32 "]. apply-index:%" PRId64, TD_VID(pVnode),
2023-05-23 11:10:50 +00:00
pVnode->config.hashBegin, pVnode->config.hashEnd, ver);
2023-05-04 05:51:48 +00:00
// TODO: trim meta of tables from TDB per hash range [pVnode->config.hashBegin, pVnode->config.hashEnd]
2023-06-01 00:13:32 +00:00
code = metaTrimTables(pVnode->pMeta);
2023-05-04 05:51:48 +00:00
return code;
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessAlterConfirmReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2023-09-01 05:24:47 +00:00
vInfo("vgId:%d, vnode handle msgType:alter-confirm, alter confirm msg is processed", TD_VID(pVnode));
2023-05-04 05:51:48 +00:00
int32_t code = TSDB_CODE_SUCCESS;
if (!pVnode->config.hashChange) {
goto _exit;
}
2023-05-23 11:10:50 +00:00
code = vnodeConsolidateAlterHashRange(pVnode, ver);
if (code < 0) {
vError("vgId:%d, failed to consolidate alter hashrange since %s. version:%" PRId64, TD_VID(pVnode), terrstr(), ver);
goto _exit;
}
2023-05-04 05:51:48 +00:00
pVnode->config.hashChange = false;
_exit:
pRsp->msgType = TDMT_VND_ALTER_CONFIRM_RSP;
2023-05-04 05:51:48 +00:00
pRsp->code = code;
pRsp->pCont = NULL;
pRsp->contLen = 0;
return code;
2022-06-06 05:58:01 +00:00
}
2022-06-09 12:54:59 +00:00
2024-07-08 07:39:25 +00:00
extern int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb);
2024-09-10 06:35:43 +00:00
extern void tsdbEnableBgTask(STsdb *pTsdb);
2024-07-08 07:39:25 +00:00
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessAlterConfigReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2022-10-20 08:47:03 +00:00
bool walChanged = false;
bool tsdbChanged = false;
2022-10-20 08:47:03 +00:00
SAlterVnodeConfigReq req = {0};
if (tDeserializeSAlterVnodeConfigReq(pReq, len, &req) != 0) {
terrno = TSDB_CODE_INVALID_MSG;
return TSDB_CODE_INVALID_MSG;
}
vInfo("vgId:%d, start to alter vnode config, page:%d pageSize:%d buffer:%d szPage:%d szBuf:%" PRIu64
" cacheLast:%d cacheLastSize:%d days:%d keep0:%d keep1:%d keep2:%d keepTimeOffset:%d s3KeepLocal:%d "
"s3Compact:%d fsync:%d level:%d "
"walRetentionPeriod:%d walRetentionSize:%d",
TD_VID(pVnode), req.pages, req.pageSize, req.buffer, req.pageSize * 1024, (uint64_t)req.buffer * 1024 * 1024,
req.cacheLast, req.cacheLastSize, req.daysPerFile, req.daysToKeep0, req.daysToKeep1, req.daysToKeep2,
req.keepTimeOffset, req.s3KeepLocal, req.s3Compact, req.walFsyncPeriod, req.walLevel, req.walRetentionPeriod,
req.walRetentionSize);
if (pVnode->config.cacheLastSize != req.cacheLastSize) {
pVnode->config.cacheLastSize = req.cacheLastSize;
tsdbCacheSetCapacity(pVnode, (size_t)pVnode->config.cacheLastSize * 1024 * 1024);
}
if (pVnode->config.szBuf != req.buffer * 1024LL * 1024LL) {
2022-10-21 02:39:19 +00:00
vInfo("vgId:%d, vnode buffer is changed from %" PRId64 " to %" PRId64, TD_VID(pVnode), pVnode->config.szBuf,
(uint64_t)(req.buffer * 1024LL * 1024LL));
pVnode->config.szBuf = req.buffer * 1024LL * 1024LL;
2022-10-09 05:52:44 +00:00
}
if (pVnode->config.szCache != req.pages) {
if (metaAlterCache(pVnode->pMeta, req.pages) < 0) {
2022-10-21 02:39:19 +00:00
vError("vgId:%d, failed to change vnode pages from %d to %d failed since %s", TD_VID(pVnode),
pVnode->config.szCache, req.pages, tstrerror(errno));
2022-10-09 07:01:20 +00:00
return errno;
} else {
2022-10-21 02:39:19 +00:00
vInfo("vgId:%d, vnode pages is changed from %d to %d", TD_VID(pVnode), pVnode->config.szCache, req.pages);
pVnode->config.szCache = req.pages;
2022-10-09 07:01:20 +00:00
}
2022-10-09 05:52:44 +00:00
}
if (pVnode->config.cacheLast != req.cacheLast) {
pVnode->config.cacheLast = req.cacheLast;
}
if (pVnode->config.walCfg.fsyncPeriod != req.walFsyncPeriod) {
pVnode->config.walCfg.fsyncPeriod = req.walFsyncPeriod;
walChanged = true;
}
if (pVnode->config.walCfg.level != req.walLevel) {
if (pVnode->config.walCfg.level == 0) {
pVnode->config.walCfg.clearFiles = 1;
}
pVnode->config.walCfg.level = req.walLevel;
walChanged = true;
}
if (pVnode->config.walCfg.retentionPeriod != req.walRetentionPeriod) {
pVnode->config.walCfg.retentionPeriod = req.walRetentionPeriod;
walChanged = true;
}
if (pVnode->config.walCfg.retentionSize != req.walRetentionSize) {
pVnode->config.walCfg.retentionSize = req.walRetentionSize;
walChanged = true;
}
if (pVnode->config.tsdbCfg.keep0 != req.daysToKeep0) {
pVnode->config.tsdbCfg.keep0 = req.daysToKeep0;
if (!VND_IS_RSMA(pVnode)) {
2022-07-30 11:26:10 +00:00
tsdbChanged = true;
}
}
if (pVnode->config.tsdbCfg.keep1 != req.daysToKeep1) {
pVnode->config.tsdbCfg.keep1 = req.daysToKeep1;
if (!VND_IS_RSMA(pVnode)) {
2022-07-30 11:26:10 +00:00
tsdbChanged = true;
}
}
if (pVnode->config.tsdbCfg.keep2 != req.daysToKeep2) {
pVnode->config.tsdbCfg.keep2 = req.daysToKeep2;
if (!VND_IS_RSMA(pVnode)) {
2022-07-30 11:26:10 +00:00
tsdbChanged = true;
}
}
if (pVnode->config.tsdbCfg.keepTimeOffset != req.keepTimeOffset) {
pVnode->config.tsdbCfg.keepTimeOffset = req.keepTimeOffset;
if (!VND_IS_RSMA(pVnode)) {
tsdbChanged = true;
}
}
2023-03-09 07:25:47 +00:00
if (req.sttTrigger != -1 && req.sttTrigger != pVnode->config.sttTrigger) {
2024-07-08 07:39:25 +00:00
if (req.sttTrigger > 1 && pVnode->config.sttTrigger > 1) {
pVnode->config.sttTrigger = req.sttTrigger;
} else {
2024-09-10 06:35:43 +00:00
vnodeAWait(&pVnode->commitTask);
2024-09-24 02:06:30 +00:00
int32_t ret = tsdbDisableAndCancelAllBgTask(pVnode->pTsdb);
if (ret != 0) {
vError("vgId:%d, failed to disable bg task since %s", TD_VID(pVnode), tstrerror(errno));
}
2024-07-08 07:39:25 +00:00
pVnode->config.sttTrigger = req.sttTrigger;
2024-09-10 06:35:43 +00:00
tsdbEnableBgTask(pVnode->pTsdb);
2024-07-08 07:39:25 +00:00
}
2023-03-09 07:25:47 +00:00
}
if (req.minRows != -1 && req.minRows != pVnode->config.tsdbCfg.minRows) {
pVnode->config.tsdbCfg.minRows = req.minRows;
}
if (req.s3KeepLocal != -1 && req.s3KeepLocal != pVnode->config.s3KeepLocal) {
pVnode->config.s3KeepLocal = req.s3KeepLocal;
}
if (req.s3Compact != -1 && req.s3Compact != pVnode->config.s3Compact) {
pVnode->config.s3Compact = req.s3Compact;
}
if (walChanged) {
2024-09-24 02:06:30 +00:00
if (walAlter(pVnode->pWal, &pVnode->config.walCfg) != 0) {
vError("vgId:%d, failed to alter wal config since %s", TD_VID(pVnode), tstrerror(errno));
}
2022-07-30 11:26:10 +00:00
}
if (tsdbChanged) {
tsdbSetKeepCfg(pVnode->pTsdb, &pVnode->config.tsdbCfg);
}
return 0;
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessBatchDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
SBatchDeleteReq deleteReq;
SDecoder decoder;
tDecoderInit(&decoder, pReq, len);
2024-09-26 03:27:01 +00:00
if (tDecodeSBatchDeleteReq(&decoder, &deleteReq) < 0) {
tDecoderClear(&decoder);
return terrno = TSDB_CODE_INVALID_MSG;
}
SMetaReader mr = {0};
metaReaderDoInit(&mr, pVnode->pMeta, META_READER_NOLOCK);
2023-11-10 06:44:29 +00:00
STsdb *pTsdb = pVnode->pTsdb;
2023-11-10 07:12:10 +00:00
if (deleteReq.level) {
pTsdb = deleteReq.level == 1 ? VND_RSMA1(pVnode) : VND_RSMA2(pVnode);
2023-11-10 06:44:29 +00:00
}
int32_t sz = taosArrayGetSize(deleteReq.deleteReqs);
for (int32_t i = 0; i < sz; i++) {
SSingleDeleteReq *pOneReq = taosArrayGet(deleteReq.deleteReqs, i);
char *name = pOneReq->tbname;
if (metaGetTableEntryByName(&mr, name) < 0) {
2022-12-20 07:01:03 +00:00
vDebug("vgId:%d, stream delete msg, skip since no table: %s", pVnode->config.vgId, name);
continue;
}
int64_t uid = mr.me.uid;
2023-11-10 06:44:29 +00:00
int32_t code = tsdbDeleteTableData(pTsdb, ver, deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
if (code < 0) {
terrno = code;
vError("vgId:%d, delete error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64 ", end ts:%" PRId64,
2022-11-29 15:39:58 +00:00
TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
}
2023-11-10 06:44:29 +00:00
if (deleteReq.level == 0) {
code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, deleteReq.ctimeMs);
if (code < 0) {
terrno = code;
vError("vgId:%d, update change time error since %s, suid:%" PRId64 ", uid:%" PRId64 ", start ts:%" PRId64
", end ts:%" PRId64,
TD_VID(pVnode), terrstr(), deleteReq.suid, uid, pOneReq->startTs, pOneReq->endTs);
}
}
tDecoderClear(&mr.coder);
}
metaReaderClear(&mr);
taosArrayDestroy(deleteReq.deleteReqs);
return 0;
}
static int32_t vnodeProcessDeleteReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp,
2024-03-26 06:43:28 +00:00
SRpcMsg *pOriginalMsg) {
2022-06-28 10:01:56 +00:00
int32_t code = 0;
SDecoder *pCoder = &(SDecoder){0};
SDeleteRes *pRes = &(SDeleteRes){0};
2022-07-26 09:43:22 +00:00
pRsp->msgType = TDMT_VND_DELETE_RSP;
pRsp->pCont = NULL;
pRsp->contLen = 0;
pRsp->code = TSDB_CODE_SUCCESS;
2022-06-28 10:01:56 +00:00
pRes->uidList = taosArrayInit(0, sizeof(tb_uid_t));
if (pRes->uidList == NULL) {
2024-09-20 00:56:46 +00:00
code = terrno;
2022-06-28 10:01:56 +00:00
goto _err;
}
tDecoderInit(pCoder, pReq, len);
2024-07-29 05:54:50 +00:00
code = tDecodeDeleteRes(pCoder, pRes);
if (code) goto _err;
2022-06-28 10:01:56 +00:00
2024-04-15 10:25:23 +00:00
if (pRes->affectedRows > 0) {
for (int32_t iUid = 0; iUid < taosArrayGetSize(pRes->uidList); iUid++) {
uint64_t uid = *(uint64_t *)taosArrayGet(pRes->uidList, iUid);
code = tsdbDeleteTableData(pVnode->pTsdb, ver, pRes->suid, uid, pRes->skey, pRes->ekey);
if (code) goto _err;
code = metaUpdateChangeTimeWithLock(pVnode->pMeta, uid, pRes->ctimeMs);
if (code) goto _err;
}
2022-06-28 10:01:56 +00:00
}
2023-11-09 12:16:29 +00:00
code = tdProcessRSmaDelete(pVnode->pSma, ver, pRes, pReq, len);
2023-09-20 02:11:03 +00:00
2022-06-28 10:01:56 +00:00
tDecoderClear(pCoder);
taosArrayDestroy(pRes->uidList);
2022-07-26 09:43:22 +00:00
SVDeleteRsp rsp = {.affectedRows = pRes->affectedRows};
2022-08-02 14:23:33 +00:00
int32_t ret = 0;
2022-07-26 09:43:22 +00:00
tEncodeSize(tEncodeSVDeleteRsp, &rsp, pRsp->contLen, ret);
pRsp->pCont = rpcMallocCont(pRsp->contLen);
2022-08-02 14:23:33 +00:00
SEncoder ec = {0};
2022-07-26 09:43:22 +00:00
tEncoderInit(&ec, pRsp->pCont, pRsp->contLen);
2024-07-29 05:54:50 +00:00
code = tEncodeSVDeleteRsp(&ec, &rsp);
if (code) goto _err;
2022-07-26 09:43:22 +00:00
tEncoderClear(&ec);
2022-06-28 10:01:56 +00:00
return code;
_err:
2024-02-21 08:06:45 +00:00
/*
2024-01-24 09:44:44 +00:00
if(code == TSDB_CODE_SUCCESS){
const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2024-01-24 09:44:44 +00:00
pOriginalMsg->info.conn.user, "Success"};
taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
}
else{
const char *batch_sample_labels[] = {VNODE_METRIC_TAG_VALUE_DELETE, pVnode->monitor.strClusterId,
pVnode->monitor.strDnodeId, tsLocalEp, pVnode->monitor.strVgId,
2024-01-24 09:44:44 +00:00
pOriginalMsg->info.conn.user, "Failed"};
taos_counter_inc(pVnode->monitor.insertCounter, batch_sample_labels);
}
2024-02-21 08:06:45 +00:00
*/
2024-01-24 09:44:44 +00:00
2022-06-28 10:01:56 +00:00
return code;
2022-07-07 01:58:47 +00:00
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessCreateIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2023-01-06 13:33:42 +00:00
SVCreateStbReq req = {0};
SDecoder dc = {0};
2024-07-25 03:45:15 +00:00
int32_t code = 0;
2023-01-06 13:33:42 +00:00
pRsp->msgType = TDMT_VND_CREATE_INDEX_RSP;
pRsp->code = TSDB_CODE_SUCCESS;
pRsp->pCont = NULL;
pRsp->contLen = 0;
tDecoderInit(&dc, pReq, len);
// decode req
if (tDecodeSVCreateStbReq(&dc, &req) < 0) {
tDecoderClear(&dc);
2024-07-25 03:45:15 +00:00
return terrno = TSDB_CODE_INVALID_MSG;
2023-01-06 13:33:42 +00:00
}
2024-07-25 03:45:15 +00:00
code = metaAddIndexToSTable(pVnode->pMeta, ver, &req);
if (code) {
pRsp->code = code;
2023-01-06 13:33:42 +00:00
goto _err;
}
tDecoderClear(&dc);
return 0;
2024-07-25 03:45:15 +00:00
2023-01-06 13:33:42 +00:00
_err:
tDecoderClear(&dc);
2024-07-25 03:45:15 +00:00
return code;
2023-01-05 07:39:35 +00:00
}
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessDropIndexReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2023-02-04 13:02:06 +00:00
SDropIndexReq req = {0};
2024-07-25 03:45:15 +00:00
int32_t code = 0;
2023-03-08 08:02:46 +00:00
pRsp->msgType = TDMT_VND_DROP_INDEX_RSP;
2023-02-04 13:02:06 +00:00
pRsp->code = TSDB_CODE_SUCCESS;
pRsp->pCont = NULL;
pRsp->contLen = 0;
2024-07-25 03:45:15 +00:00
if ((code = tDeserializeSDropIdxReq(pReq, len, &req))) {
2024-08-26 09:06:31 +00:00
pRsp->code = code;
2024-07-25 03:45:15 +00:00
return code;
2023-02-04 13:02:06 +00:00
}
2023-03-08 10:16:29 +00:00
2024-07-25 03:45:15 +00:00
code = metaDropIndexFromSTable(pVnode->pMeta, ver, &req);
if (code) {
pRsp->code = code;
return code;
2023-02-04 13:02:06 +00:00
}
2023-01-05 07:39:35 +00:00
return TSDB_CODE_SUCCESS;
}
2024-05-23 09:57:07 +00:00
extern int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp);
2023-05-23 11:10:50 +00:00
static int32_t vnodeProcessCompactVnodeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
if (!pVnode->restored) {
vInfo("vgId:%d, ignore compact req during restoring. ver:%" PRId64, TD_VID(pVnode), ver);
return 0;
}
2024-05-23 09:57:07 +00:00
return vnodeAsyncCompact(pVnode, ver, pReq, len, pRsp);
2023-02-28 11:52:03 +00:00
}
2023-09-01 05:24:47 +00:00
static int32_t vnodeProcessConfigChangeReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) {
2024-09-24 02:06:30 +00:00
if (syncCheckMember(pVnode->sync) != 0) {
vError("vgId:%d, failed to check member", TD_VID(pVnode));
}
2023-09-01 05:24:47 +00:00
pRsp->msgType = TDMT_SYNC_CONFIG_CHANGE_RSP;
pRsp->code = TSDB_CODE_SUCCESS;
pRsp->pCont = NULL;
pRsp->contLen = 0;
return 0;
}
static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
SSyncState syncState = syncGetState(pVnode->sync);
if (syncState.state != TAOS_SYNC_STATE_LEADER) {
2024-07-25 03:45:15 +00:00
return terrno = TSDB_CODE_SYN_NOT_LEADER;
}
char token[TSDB_ARB_TOKEN_SIZE] = {0};
if (vnodeGetArbToken(pVnode, token) != 0) {
2024-07-25 03:45:15 +00:00
return terrno = TSDB_CODE_NOT_FOUND;
}
if (strncmp(token, member0Token, TSDB_ARB_TOKEN_SIZE) != 0 &&
strncmp(token, member1Token, TSDB_ARB_TOKEN_SIZE) != 0) {
2024-07-25 03:45:15 +00:00
return terrno = TSDB_CODE_MND_ARB_TOKEN_MISMATCH;
}
terrno = TSDB_CODE_SUCCESS;
return 0;
}
static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token) {
int32_t code = vnodePreCheckAssignedLogSyncd(pVnode, member0Token, member1Token);
if (code != 0) {
return code;
}
return syncGetAssignedLogSynced(pVnode->sync);
}
static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t len, SRpcMsg *pRsp) {
int32_t code = 0;
SVArbCheckSyncReq syncReq = {0};
2024-07-25 03:45:15 +00:00
code = tDeserializeSVArbCheckSyncReq(pReq, len, &syncReq);
if (code) {
return terrno = code;
}
pRsp->msgType = TDMT_VND_ARB_CHECK_SYNC_RSP;
pRsp->code = TSDB_CODE_SUCCESS;
pRsp->pCont = NULL;
pRsp->contLen = 0;
SVArbCheckSyncRsp syncRsp = {0};
syncRsp.arbToken = syncReq.arbToken;
syncRsp.member0Token = syncReq.member0Token;
syncRsp.member1Token = syncReq.member1Token;
syncRsp.vgId = TD_VID(pVnode);
2024-09-24 02:06:30 +00:00
if (vnodeCheckAssignedLogSyncd(pVnode, syncReq.member0Token, syncReq.member1Token) != 0) {
vError("vgId:%d, failed to check assigned log syncd", TD_VID(pVnode));
}
syncRsp.errCode = terrno;
if (vnodeUpdateArbTerm(pVnode, syncReq.arbTerm) != 0) {
vError("vgId:%d, failed to update arb term", TD_VID(pVnode));
code = -1;
goto _OVER;
}
int32_t contLen = tSerializeSVArbCheckSyncRsp(NULL, 0, &syncRsp);
if (contLen <= 0) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
code = -1;
goto _OVER;
}
void *pHead = rpcMallocCont(contLen);
if (!pHead) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
code = -1;
goto _OVER;
}
if (tSerializeSVArbCheckSyncRsp(pHead, contLen, &syncRsp) <= 0) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
rpcFreeCont(pHead);
code = -1;
goto _OVER;
}
pRsp->pCont = pHead;
pRsp->contLen = contLen;
terrno = TSDB_CODE_SUCCESS;
_OVER:
tFreeSVArbCheckSyncReq(&syncReq);
return code;
}
2023-02-28 11:52:03 +00:00
#ifndef TD_ENTERPRISE
2024-05-28 06:35:58 +00:00
int32_t vnodeAsyncCompact(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { return 0; }
int32_t tsdbAsyncCompact(STsdb *tsdb, const STimeWindow *tw, bool sync) { return 0; }
2023-02-28 11:52:03 +00:00
#endif