TDengine/source/dnode/mnode/impl/src/mndStreamTrans.c

139 lines
4.3 KiB
C
Raw Normal View History

2023-11-13 15:59:05 +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 "mndStream.h"
#include "mndTrans.h"
2023-11-13 15:59:05 +00:00
fix(stream): reduce the consensus checkpoint id trans. (#30105) * fix(stream): reduce the consensus checkpoint id trans. * refactor(stream): add some logs. * refactor(stream): set the max checkpoint exec time 30min. * refactor(stream): add checkpoint-consensus trans conflict check. * refactor(stream): remove unused local variables. * fix(stream): fix syntax error. * fix(stream): 1. fix free memory error 2. continue if put result into dst hashmap failed. * fix issue * fix issue * fix(mnd): follower mnode not processes the timer event. * fix(stream): print correct error msg. * fix(stream): add some logs. * fix(stream): add some logs. * fix(stream): add some logs. * fix(stream): add some logs. * fix(stream): add some logs. * fix(stream): add some logs. * fix(stream): add some logs. * fix(stream): add some logs. * fix(stream): truncate long subtable name * fix(stream): add buffer len. * refactor(stream): update some logs. * fix issue * refactor(stream): update some logs. * refactor(stream): update some logs. * fix(stream): check return value. * fix(stream): fix syntax error. * fix(stream): check return value. * fix(stream): update the timer check in mnode. * fix(stream): add restart stage tracking. * fix(stream): track the start task stage for meta. * fix(stream): fix error in log. * refactor(stream): adjust log info. * fix mem issue * fix(stream): check the number of required tasks for consensus checkpointId. * fix(stream): lock the whole start procedure. * fix(stream): add lock during start all tasks. * fix(stream): update logs. * fix(stream): update logs. * fix(stream): update logs. * fix(stream): fix dead-lock. * fix(stream): fix syntax error. * fix(stream): not drop the scan-history task. * fix(stream): fix syntax error. * fix(stream): wait for executor stop before restarting. * fix(stream): add some logs. * fix(stream): add some logs. * fix(stream): add some logs. * fix(stream): disable some logs. * fix(stream): reset the start info if no task left. --------- Co-authored-by: 54liuyao <54liuyao@163.com> Co-authored-by: Jinqing Kuang <kuangjinqingcn@gmail.com>
2025-03-17 02:20:17 +00:00
#define MAX_CHKPT_EXEC_ELAPSED (600*1000*3) // 600s
2023-11-13 15:59:05 +00:00
typedef struct SKeyInfo {
2024-06-26 02:32:15 +00:00
void *pKey;
2023-11-13 15:59:05 +00:00
int32_t keyLen;
} SKeyInfo;
2024-09-19 10:13:46 +00:00
static bool identicalName(const char *pDb, const char *pParam, int32_t len) {
return (strlen(pDb) == len) && (strncmp(pDb, pParam, len) == 0);
}
2025-04-10 11:41:37 +00:00
int32_t mndStreamCreateTrans(SMnode *pMnode, SStreamObj *pStream, SRpcMsg *pReq, ETrnConflct conflict, const char *name, STrans **ppTrans) {
int64_t streamId = pStream->pCreate->streamId;
2024-08-02 03:49:21 +00:00
int32_t code = 0;
2025-04-10 11:41:37 +00:00
2024-07-22 05:31:57 +00:00
STrans *p = mndTransCreate(pMnode, TRN_POLICY_RETRY, conflict, pReq, name);
if (p == NULL) {
2025-04-10 11:41:37 +00:00
mstError("failed to build trans:%s, reason: %s", name, tstrerror(terrno));
2024-07-22 05:31:57 +00:00
return terrno;
2024-01-25 08:53:30 +00:00
}
2025-04-10 11:41:37 +00:00
mstInfo("start to build trans %s, transId:%d", name, p->id);
2024-01-25 08:53:30 +00:00
2025-04-10 11:41:37 +00:00
mndTransSetDbName(p, pStream->pCreate->streamDB, pStream->pCreate->outTblName);
2024-08-02 03:49:21 +00:00
if ((code = mndTransCheckConflict(pMnode, p)) != 0) {
2025-04-10 11:41:37 +00:00
mstError("failed to build trans:%s for stream, code:%s", name, tstrerror(terrno));
2024-07-22 05:31:57 +00:00
mndTransDrop(p);
2024-08-02 03:49:21 +00:00
return code;
2024-01-25 08:53:30 +00:00
}
2025-04-10 11:41:37 +00:00
*ppTrans = p;
2024-08-02 03:49:21 +00:00
return code;
2024-01-25 08:53:30 +00:00
}
SSdbRaw *mndStreamActionEncode(SStreamObj *pStream) {
2024-07-22 08:33:42 +00:00
int32_t code = 0;
int32_t lino = 0;
void *buf = NULL;
2025-04-10 11:41:37 +00:00
int64_t streamId = pStream->pCreate->streamId;
2024-01-25 08:53:30 +00:00
SEncoder encoder;
tEncoderInit(&encoder, NULL, 0);
if ((code = tEncodeSStreamObj(&encoder, pStream)) < 0) {
2024-01-25 08:53:30 +00:00
tEncoderClear(&encoder);
TSDB_CHECK_CODE(code, lino, _over);
2024-01-25 08:53:30 +00:00
}
2024-01-25 08:53:30 +00:00
int32_t tlen = encoder.pos;
tEncoderClear(&encoder);
int32_t size = sizeof(int32_t) + tlen + MND_STREAM_RESERVE_SIZE;
SSdbRaw *pRaw = sdbAllocRaw(SDB_STREAM, MND_STREAM_VER_NUMBER, size);
TSDB_CHECK_NULL(pRaw, code, lino, _over, terrno);
2024-01-25 08:53:30 +00:00
buf = taosMemoryMalloc(tlen);
TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
2024-01-25 08:53:30 +00:00
tEncoderInit(&encoder, buf, tlen);
if ((code = tEncodeSStreamObj(&encoder, pStream)) < 0) {
2024-01-25 08:53:30 +00:00
tEncoderClear(&encoder);
TSDB_CHECK_CODE(code, lino, _over);
2024-01-25 08:53:30 +00:00
}
2024-01-25 08:53:30 +00:00
tEncoderClear(&encoder);
int32_t dataPos = 0;
SDB_SET_INT32(pRaw, dataPos, tlen, _over);
SDB_SET_BINARY(pRaw, dataPos, buf, tlen, _over);
SDB_SET_DATALEN(pRaw, dataPos, _over);
2024-01-25 08:53:30 +00:00
_over:
2025-04-10 11:41:37 +00:00
2024-01-25 08:53:30 +00:00
taosMemoryFreeClear(buf);
if (code != TSDB_CODE_SUCCESS) {
2025-04-10 11:41:37 +00:00
mstError("failed to encode stream %s to raw:%p at line:%d since %s", pStream->pCreate->name, pRaw, lino, tstrerror(code));
2024-01-25 08:53:30 +00:00
sdbFreeRaw(pRaw);
terrno = code;
2024-01-25 08:53:30 +00:00
return NULL;
}
2025-04-10 11:41:37 +00:00
mstTrace("stream %s encoded to raw:%p", pStream->pCreate->name, pRaw);
2024-01-25 08:53:30 +00:00
return pRaw;
}
2025-04-10 11:41:37 +00:00
int32_t mndStreamTransAppend(SStreamObj *pStream, STrans *pTrans, int32_t status) {
2025-04-24 01:11:54 +00:00
int64_t streamId = pStream->pCreate->streamId;
2024-01-25 08:53:30 +00:00
SSdbRaw *pCommitRaw = mndStreamActionEncode(pStream);
if (pCommitRaw == NULL) {
2025-04-10 11:41:37 +00:00
mstError("failed to encode stream since %s", terrstr());
2024-01-25 08:53:30 +00:00
mndTransDrop(pTrans);
return terrno;
2024-01-25 08:53:30 +00:00
}
if (mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
2025-04-10 11:41:37 +00:00
mstError("stream trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
2024-01-25 08:53:30 +00:00
sdbFreeRaw(pCommitRaw);
mndTransDrop(pTrans);
return terrno;
2024-01-25 08:53:30 +00:00
}
if (sdbSetRawStatus(pCommitRaw, status) != 0) {
2025-04-10 11:41:37 +00:00
mstError("stream trans:%d failed to set raw status:%d since %s", pTrans->id, status, terrstr());
2024-01-25 08:53:30 +00:00
sdbFreeRaw(pCommitRaw);
mndTransDrop(pTrans);
return terrno;
2024-01-25 08:53:30 +00:00
}
return 0;
}
2024-01-26 02:03:54 +00:00
int32_t setTransAction(STrans *pTrans, void *pCont, int32_t contLen, int32_t msgType, const SEpSet *pEpset,
int32_t retryCode, int32_t acceptCode) {
2024-06-26 02:32:15 +00:00
STransAction action = {.epSet = *pEpset,
.contLen = contLen,
.pCont = pCont,
.msgType = msgType,
.retryCode = retryCode,
.acceptableCode = acceptCode};
2024-01-26 02:03:54 +00:00
return mndTransAppendRedoAction(pTrans, &action);
}