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

1080 lines
34 KiB
C
Raw Normal View History

2022-03-10 09:15:45 +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/>.
*/
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
#include "mndStream.h"
2023-08-31 01:28:43 +00:00
#include "audit.h"
2022-03-10 09:15:45 +00:00
#include "mndDb.h"
2022-06-25 10:14:42 +00:00
#include "mndPrivilege.h"
2022-03-10 09:15:45 +00:00
#include "mndShow.h"
#include "mndStb.h"
#include "mndTrans.h"
2023-11-16 02:22:01 +00:00
#include "osMemory.h"
2022-03-26 08:48:14 +00:00
#include "parser.h"
2024-08-16 08:53:44 +00:00
#include "taoserror.h"
2023-08-01 10:14:58 +00:00
#include "tmisce.h"
2023-08-17 10:52:33 +00:00
#include "tname.h"
2022-03-10 09:15:45 +00:00
2025-06-06 01:09:40 +00:00
#define MND_STREAM_MAX_NUM 100000
2024-02-21 08:17:06 +00:00
typedef struct {
int8_t placeHolder; // // to fix windows compile error, define place holder
} SMStreamNodeCheckMsg;
static int32_t mndNodeCheckSentinel = 0;
2025-05-23 10:14:34 +00:00
SStmRuntime mStreamMgmt = {0};
2023-02-06 02:40:33 +00:00
2022-03-10 09:15:45 +00:00
static int32_t mndStreamActionInsert(SSdb *pSdb, SStreamObj *pStream);
static int32_t mndStreamActionDelete(SSdb *pSdb, SStreamObj *pStream);
static int32_t mndStreamActionUpdate(SSdb *pSdb, SStreamObj *pOldStream, SStreamObj *pNewStream);
2022-06-15 13:13:47 +00:00
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq);
2023-11-24 01:47:35 +00:00
static int32_t mndProcessCreateStreamReqFromMNode(SRpcMsg *pReq);
static int32_t mndProcessDropStreamReqFromMNode(SRpcMsg *pReq);
2022-05-16 06:55:31 +00:00
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
2022-03-10 09:15:45 +00:00
static void mndCancelGetNextStream(SMnode *pMnode, void *pIter);
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
static void mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter);
2025-05-17 02:55:32 +00:00
static int32_t mndProcessStopStreamReq(SRpcMsg *pReq);
static int32_t mndProcessStartStreamReq(SRpcMsg *pReq);
static SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw);
2024-01-15 11:56:38 +00:00
SSdbRaw *mndStreamSeqActionEncode(SStreamObj *pStream);
SSdbRow *mndStreamSeqActionDecode(SSdbRaw *pRaw);
static int32_t mndStreamSeqActionInsert(SSdb *pSdb, SStreamSeq *pStream);
static int32_t mndStreamSeqActionDelete(SSdb *pSdb, SStreamSeq *pStream);
static int32_t mndStreamSeqActionUpdate(SSdb *pSdb, SStreamSeq *pOldStream, SStreamSeq *pNewStream);
2025-04-10 01:09:30 +00:00
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq);
2023-08-23 06:26:45 +00:00
void mndCleanupStream(SMnode *pMnode) {
2025-04-24 01:11:54 +00:00
//STREAMTODO
mDebug("mnd stream runtime info cleanup");
2023-08-23 06:26:45 +00:00
}
2022-03-10 09:15:45 +00:00
SSdbRow *mndStreamActionDecode(SSdbRaw *pRaw) {
2024-09-29 06:32:54 +00:00
int32_t code = 0;
int32_t lino = 0;
SSdbRow *pRow = NULL;
2022-12-01 08:04:39 +00:00
SStreamObj *pStream = NULL;
void *buf = NULL;
2024-01-22 03:05:42 +00:00
int8_t sver = 0;
2024-09-29 06:32:54 +00:00
int32_t tlen;
int32_t dataPos = 0;
2022-03-10 09:15:45 +00:00
2024-09-29 06:32:54 +00:00
code = sdbGetRawSoftVer(pRaw, &sver);
TSDB_CHECK_CODE(code, lino, _over);
2022-03-10 09:15:45 +00:00
2024-02-20 06:49:52 +00:00
if (sver < 1 || sver > MND_STREAM_VER_NUMBER) {
2023-08-17 10:52:33 +00:00
mError("stream read invalid ver, data ver: %d, curr ver: %d", sver, MND_STREAM_VER_NUMBER);
2024-09-29 06:32:54 +00:00
goto _over;
2022-03-10 09:15:45 +00:00
}
2022-12-01 08:04:39 +00:00
pRow = sdbAllocRow(sizeof(SStreamObj));
2024-09-29 06:32:54 +00:00
TSDB_CHECK_NULL(pRow, code, lino, _over, terrno);
2022-03-10 09:15:45 +00:00
2022-12-01 08:04:39 +00:00
pStream = sdbGetRowObj(pRow);
2024-09-29 06:32:54 +00:00
TSDB_CHECK_NULL(pStream, code, lino, _over, terrno);
2022-03-10 09:15:45 +00:00
2024-09-29 06:32:54 +00:00
SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
2024-01-22 03:05:42 +00:00
2022-03-25 16:29:53 +00:00
buf = taosMemoryMalloc(tlen + 1);
2024-09-29 06:32:54 +00:00
TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
2024-01-22 03:05:42 +00:00
2024-09-29 06:32:54 +00:00
SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
2022-03-10 09:15:45 +00:00
2022-05-07 10:03:06 +00:00
SDecoder decoder;
tDecoderInit(&decoder, buf, tlen + 1);
2024-09-29 06:32:54 +00:00
code = tDecodeSStreamObj(&decoder, pStream, sver);
2022-10-21 03:42:30 +00:00
tDecoderClear(&decoder);
2022-03-10 09:15:45 +00:00
2024-09-29 06:32:54 +00:00
if (code < 0) {
2024-09-29 06:18:24 +00:00
tFreeStreamObj(pStream);
2022-03-10 09:15:45 +00:00
}
2024-09-29 06:32:54 +00:00
_over:
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear(buf);
2024-09-29 06:32:54 +00:00
if (code != TSDB_CODE_SUCCESS) {
2025-04-24 01:11:54 +00:00
char *p = (pStream == NULL) ? "null" : pStream->pCreate->name;
2024-09-29 06:32:54 +00:00
mError("stream:%s, failed to decode from raw:%p since %s at:%d", p, pRaw, tstrerror(code), lino);
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear(pRow);
2024-09-29 06:32:54 +00:00
terrno = code;
2022-03-10 09:15:45 +00:00
return NULL;
2024-09-29 06:32:54 +00:00
} else {
2025-04-24 01:11:54 +00:00
mTrace("stream:%s, decode from raw:%p, row:%p", pStream->pCreate->name, pRaw, pStream);
2022-03-10 09:15:45 +00:00
2024-09-29 06:32:54 +00:00
terrno = 0;
return pRow;
}
2022-03-10 09:15:45 +00:00
}
static int32_t mndStreamActionInsert(SSdb *pSdb, SStreamObj *pStream) {
2025-04-24 01:11:54 +00:00
mTrace("stream:%s, perform insert action", pStream->pCreate->name);
2022-03-10 09:15:45 +00:00
return 0;
}
static int32_t mndStreamActionDelete(SSdb *pSdb, SStreamObj *pStream) {
2025-04-24 01:11:54 +00:00
mInfo("stream:%s, perform delete action", pStream->pCreate->name);
2022-08-18 06:26:11 +00:00
tFreeStreamObj(pStream);
2022-03-10 09:15:45 +00:00
return 0;
}
static int32_t mndStreamActionUpdate(SSdb *pSdb, SStreamObj *pOldStream, SStreamObj *pNewStream) {
2025-04-24 01:11:54 +00:00
mTrace("stream:%s, perform update action", pOldStream->pCreate->name);
2022-03-10 09:15:45 +00:00
2025-05-17 08:36:35 +00:00
atomic_store_32(&pOldStream->mainSnodeId, pNewStream->mainSnodeId);
2025-04-30 10:39:59 +00:00
atomic_store_8(&pOldStream->userStopped, atomic_load_8(&pNewStream->userStopped));
2023-08-03 10:09:36 +00:00
pOldStream->updateTime = pNewStream->updateTime;
2025-04-24 01:11:54 +00:00
2022-03-10 09:15:45 +00:00
return 0;
}
2024-07-22 02:07:18 +00:00
int32_t mndAcquireStream(SMnode *pMnode, char *streamName, SStreamObj **pStream) {
2024-07-23 11:16:06 +00:00
int32_t code = 0;
SSdb *pSdb = pMnode->pSdb;
2024-07-22 02:07:18 +00:00
(*pStream) = sdbAcquire(pSdb, SDB_STREAM, streamName);
if ((*pStream) == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
2024-07-23 11:16:06 +00:00
code = TSDB_CODE_MND_STREAM_NOT_EXIST;
2022-03-10 09:15:45 +00:00
}
2024-07-23 11:16:06 +00:00
return code;
2022-03-10 09:15:45 +00:00
}
2025-05-27 11:08:07 +00:00
static bool mndStreamGetNameFromId(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
SStreamObj* pStream = pObj;
if (pStream->pCreate->streamId == *(int64_t*)p1) {
strncpy((char*)p2, pStream->name, TSDB_STREAM_NAME_LEN);
return false;
}
return true;
}
int32_t mndAcquireStreamById(SMnode *pMnode, int64_t streamId, SStreamObj **pStream) {
int32_t code = 0;
SSdb *pSdb = pMnode->pSdb;
char streamName[TSDB_STREAM_NAME_LEN];
streamName[0] = 0;
sdbTraverse(pSdb, SDB_STREAM, mndStreamGetNameFromId, &streamId, streamName, NULL);
if (streamName[0]) {
(*pStream) = sdbAcquire(pSdb, SDB_STREAM, streamName);
if ((*pStream) == NULL && terrno == TSDB_CODE_SDB_OBJ_NOT_THERE) {
code = TSDB_CODE_MND_STREAM_NOT_EXIST;
}
}
return code;
}
2022-03-10 09:15:45 +00:00
void mndReleaseStream(SMnode *pMnode, SStreamObj *pStream) {
SSdb *pSdb = pMnode->pSdb;
sdbRelease(pSdb, pStream);
}
SSdbRaw *mndStreamSeqActionEncode(SStreamObj *pStream) { return NULL; }
SSdbRow *mndStreamSeqActionDecode(SSdbRaw *pRaw) { return NULL; }
int32_t mndStreamSeqActionInsert(SSdb *pSdb, SStreamSeq *pStream) { return 0; }
int32_t mndStreamSeqActionDelete(SSdb *pSdb, SStreamSeq *pStream) { return 0; }
int32_t mndStreamSeqActionUpdate(SSdb *pSdb, SStreamSeq *pOldStream, SStreamSeq *pNewStream) { return 0; }
2023-11-08 11:54:57 +00:00
2025-05-17 02:55:32 +00:00
static int32_t mndStreamBuildObj(SMnode *pMnode, SStreamObj *pObj, SCMCreateStreamReq *pCreate, int32_t snodeId) {
2024-08-01 09:17:32 +00:00
int32_t code = 0;
2022-06-16 08:01:22 +00:00
2025-04-10 11:41:37 +00:00
pObj->pCreate = pCreate;
2025-04-28 08:47:32 +00:00
strncpy(pObj->name, pCreate->name, TSDB_STREAM_NAME_LEN);
2025-05-17 02:55:32 +00:00
pObj->mainSnodeId = snodeId;
2025-05-06 02:27:12 +00:00
pObj->userDropped = 0;
2025-04-30 10:39:59 +00:00
pObj->userStopped = 0;
2025-04-25 06:59:41 +00:00
2022-06-16 08:01:22 +00:00
pObj->createTime = taosGetTimestampMs();
pObj->updateTime = pObj->createTime;
2024-08-01 09:17:32 +00:00
2025-06-24 05:21:01 +00:00
mstLogSStreamObj("create stream", pObj);
2025-05-19 09:46:36 +00:00
2024-08-01 09:17:32 +00:00
return code;
2022-06-16 08:01:22 +00:00
}
2025-04-24 01:11:54 +00:00
static int32_t mndStreamCreateOutStb(SMnode *pMnode, STrans *pTrans, const SCMCreateStreamReq *pStream, const char *user) {
2022-06-16 02:29:30 +00:00
SStbObj *pStb = NULL;
2024-01-15 11:56:38 +00:00
SDbObj *pDb = NULL;
int32_t code = 0;
int32_t lino = 0;
2022-05-06 17:47:45 +00:00
SMCreateStbReq createReq = {0};
TAOS_STRNCAT(createReq.name, pStream->outDB, TSDB_DB_FNAME_LEN);
TAOS_STRNCAT(createReq.name, ".", 2);
TAOS_STRNCAT(createReq.name, pStream->outTblName, TSDB_TABLE_NAME_LEN);
2025-04-24 01:11:54 +00:00
createReq.numOfColumns = taosArrayGetSize(pStream->outCols);
createReq.numOfTags = pStream->outTags ? taosArrayGetSize(pStream->outTags) : 1;
2024-03-31 00:08:23 +00:00
createReq.pColumns = taosArrayInit_s(sizeof(SFieldWithOptions), createReq.numOfColumns);
2024-08-01 09:17:32 +00:00
TSDB_CHECK_NULL(createReq.pColumns, code, lino, _OVER, terrno);
2022-05-06 17:47:45 +00:00
// build fields
2022-05-07 02:21:51 +00:00
for (int32_t i = 0; i < createReq.numOfColumns; i++) {
2024-03-31 00:08:23 +00:00
SFieldWithOptions *pField = taosArrayGet(createReq.pColumns, i);
2024-08-02 03:38:30 +00:00
TSDB_CHECK_NULL(pField, code, lino, _OVER, terrno);
2025-04-24 01:11:54 +00:00
TAOS_FIELD_E *pSrc = taosArrayGet(pStream->outCols, i);
2024-08-02 03:38:30 +00:00
2025-04-24 01:11:54 +00:00
tstrncpy(pField->name, pSrc->name, TSDB_COL_NAME_LEN);
pField->flags = 0;
pField->type = pSrc->type;
pField->bytes = pSrc->bytes;
2024-03-31 00:08:23 +00:00
pField->compress = createDefaultColCmprByType(pField->type);
feat(decimal): support decimal data type (#30060) * decimal: create table * decimal: add test case decimal.py * decimal: add decimal.c * support input decimal * decimal test * refactor svalue * fix test cases * add decimal unit test * add decimal test cmake * support insert and query decimal type * define wide integer, support decimal128 * support decimal128 divide * set decimal type expr res types * scalar decimal * convert to decimal * fix decimal64/128 from str and to str * fix decimal from str and decimal to str * decimal simple conversion * unit test for decimal * decimal conversion and unit tests * decimal + - * / * decimal scalar ops and comparision * start to refactor GET_TYPED_DATA * support decimal max func, cast func * refactor GET_TYPED_DATA interface * decimal scalar comparision * start to implement sum for decimal * support sum and avg for decimal type * decimal tests * add decimal test * decimal add test cases * decimal use int256/int128 * decimal testing * fix decimal table meta and add tests for decimal col streams * fix create stream and create tsma * test insert decimal values * decimal from str * test decimal input * test parse decimal from string * add taos_fetch_field_e api * decimal insert tests * test decimal operators * decimal operator test * feat:support decimal in raw block * decimal operator tests * decimal test * feat:support decimal in raw block * feat:support decimal in raw block * feat:add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * decimal test operators * decimal operator test * test decimal operators * test decimal compare operators * decimal unary operator test * decimal col with decimal col oper test * test decimal col filtering * fix decimal float operator test * decimal test where filtering * fix decimal filtering * fix decimal order by * fix decimal op test * test decimal agg funcs * test decimal functions * remove assert * fix ci build for ret check * fix decimal windows build * fix ci ret check * skip decimal ret check * skip decimal ret check * fix decimal tests * fix decimal ci test * decimal test * fix(tmq): heap user after free * fix(tmq): double free * fix(tmq): double free * fix decimal tests * fix(decimal): decimal test ci build * fix(decimal): windows build * fix(decimal): decimal test build * fix(decimal): fix decimal build and tests * fix(decimal): fix decimal tests * fix(decimal): fix taos_fetch_fields_e api * fix(decimal): fix decimal taos_fetch_fields_e api * fix(decimal): rebase 3.0 * fix(decimal): fix decimal functions * fix(decimal): fix decimal test case memory leak * fix(decimal): fix decimal tests * fix(decimal): fix decimal test case * fix(decimal): fix decimal tests * feat(decimal): fix unit tests * feat(decimal): fix deicmal unit test --------- Co-authored-by: wangmm0220 <wangmm0220@gmail.com> Co-authored-by: yihaoDeng <yhdeng@taosdata.com>
2025-03-14 10:08:07 +00:00
if (IS_DECIMAL_TYPE(pField->type)) {
2025-04-24 01:11:54 +00:00
pField->typeMod = decimalCalcTypeMod(pSrc->precision, pSrc->scale);
feat(decimal): support decimal data type (#30060) * decimal: create table * decimal: add test case decimal.py * decimal: add decimal.c * support input decimal * decimal test * refactor svalue * fix test cases * add decimal unit test * add decimal test cmake * support insert and query decimal type * define wide integer, support decimal128 * support decimal128 divide * set decimal type expr res types * scalar decimal * convert to decimal * fix decimal64/128 from str and to str * fix decimal from str and decimal to str * decimal simple conversion * unit test for decimal * decimal conversion and unit tests * decimal + - * / * decimal scalar ops and comparision * start to refactor GET_TYPED_DATA * support decimal max func, cast func * refactor GET_TYPED_DATA interface * decimal scalar comparision * start to implement sum for decimal * support sum and avg for decimal type * decimal tests * add decimal test * decimal add test cases * decimal use int256/int128 * decimal testing * fix decimal table meta and add tests for decimal col streams * fix create stream and create tsma * test insert decimal values * decimal from str * test decimal input * test parse decimal from string * add taos_fetch_field_e api * decimal insert tests * test decimal operators * decimal operator test * feat:support decimal in raw block * decimal operator tests * decimal test * feat:support decimal in raw block * feat:support decimal in raw block * feat:add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * decimal test operators * decimal operator test * test decimal operators * test decimal compare operators * decimal unary operator test * decimal col with decimal col oper test * test decimal col filtering * fix decimal float operator test * decimal test where filtering * fix decimal filtering * fix decimal order by * fix decimal op test * test decimal agg funcs * test decimal functions * remove assert * fix ci build for ret check * fix decimal windows build * fix ci ret check * skip decimal ret check * skip decimal ret check * fix decimal tests * fix decimal ci test * decimal test * fix(tmq): heap user after free * fix(tmq): double free * fix(tmq): double free * fix decimal tests * fix(decimal): decimal test ci build * fix(decimal): windows build * fix(decimal): decimal test build * fix(decimal): fix decimal build and tests * fix(decimal): fix decimal tests * fix(decimal): fix taos_fetch_fields_e api * fix(decimal): fix decimal taos_fetch_fields_e api * fix(decimal): rebase 3.0 * fix(decimal): fix decimal functions * fix(decimal): fix decimal test case memory leak * fix(decimal): fix decimal tests * fix(decimal): fix decimal test case * fix(decimal): fix decimal tests * feat(decimal): fix unit tests * feat(decimal): fix deicmal unit test --------- Co-authored-by: wangmm0220 <wangmm0220@gmail.com> Co-authored-by: yihaoDeng <yhdeng@taosdata.com>
2025-03-14 10:08:07 +00:00
}
2022-05-07 02:21:51 +00:00
}
2023-01-10 02:13:56 +00:00
2025-04-24 01:11:54 +00:00
if (NULL == pStream->outTags) {
2023-01-10 02:13:56 +00:00
createReq.numOfTags = 1;
2023-02-23 06:58:22 +00:00
createReq.pTags = taosArrayInit_s(sizeof(SField), 1);
2024-08-01 09:17:32 +00:00
TSDB_CHECK_NULL(createReq.pTags, code, lino, _OVER, terrno);
2023-01-10 02:13:56 +00:00
// build tags
SField *pField = taosArrayGet(createReq.pTags, 0);
2024-08-02 03:38:30 +00:00
TSDB_CHECK_NULL(pField, code, lino, _OVER, terrno);
tstrncpy(pField->name, "group_id", sizeof(pField->name));
2023-01-10 02:13:56 +00:00
pField->type = TSDB_DATA_TYPE_UBIGINT;
pField->flags = 0;
pField->bytes = 8;
} else {
2025-04-24 01:11:54 +00:00
createReq.numOfTags = taosArrayGetSize(pStream->outTags);
2023-02-23 06:58:22 +00:00
createReq.pTags = taosArrayInit_s(sizeof(SField), createReq.numOfTags);
2024-08-01 09:17:32 +00:00
TSDB_CHECK_NULL(createReq.pTags, code, lino, _OVER, terrno);
2023-01-10 02:13:56 +00:00
for (int32_t i = 0; i < createReq.numOfTags; i++) {
SField *pField = taosArrayGet(createReq.pTags, i);
2024-08-02 03:38:30 +00:00
if (pField == NULL) {
continue;
}
2025-04-24 01:11:54 +00:00
TAOS_FIELD_E *pSrc = taosArrayGet(pStream->outTags, i);
pField->bytes = pSrc->bytes;
pField->flags = 0;
pField->type = pSrc->type;
tstrncpy(pField->name, pSrc->name, TSDB_COL_NAME_LEN);
2023-01-10 02:13:56 +00:00
}
}
2022-05-06 17:47:45 +00:00
if ((code = mndCheckCreateStbReq(&createReq)) != 0) {
2022-05-06 17:47:45 +00:00
goto _OVER;
}
pStb = mndAcquireStb(pMnode, createReq.name);
if (pStb != NULL) {
code = TSDB_CODE_MND_STB_ALREADY_EXIST;
2022-05-06 17:47:45 +00:00
goto _OVER;
}
pDb = mndAcquireDbByStb(pMnode, createReq.name);
if (pDb == NULL) {
code = TSDB_CODE_MND_DB_NOT_SELECTED;
2022-05-06 17:47:45 +00:00
goto _OVER;
}
int32_t numOfStbs = -1;
2022-05-07 04:07:45 +00:00
if (mndGetNumOfStbs(pMnode, pDb->name, &numOfStbs) != 0) {
goto _OVER;
}
2022-05-06 17:47:45 +00:00
if (pDb->cfg.numOfStables == 1 && numOfStbs != 0) {
code = TSDB_CODE_MND_SINGLE_STB_MODE_DB;
2022-05-06 17:47:45 +00:00
goto _OVER;
}
SStbObj stbObj = {0};
if (mndBuildStbFromReq(pMnode, &stbObj, &createReq, pDb) != 0) {
goto _OVER;
}
2025-04-24 01:11:54 +00:00
stbObj.uid = pStream->outStbUid;
2022-05-07 15:19:05 +00:00
2022-08-18 06:26:11 +00:00
if (mndAddStbToTrans(pMnode, pTrans, pDb, &stbObj) < 0) {
mndFreeStb(&stbObj);
goto _OVER;
}
2025-04-24 01:11:54 +00:00
mDebug("stream:%s create dst stable:%s, cols:%d", pStream->name, pStream->outTblName, createReq.numOfColumns);
2022-08-18 06:26:11 +00:00
tFreeSMCreateStbReq(&createReq);
mndFreeStb(&stbObj);
2022-11-27 14:23:40 +00:00
mndReleaseStb(pMnode, pStb);
2022-11-16 02:55:19 +00:00
mndReleaseDb(pMnode, pDb);
2024-08-01 09:17:32 +00:00
return code;
2022-05-06 17:47:45 +00:00
_OVER:
2022-08-18 06:26:11 +00:00
tFreeSMCreateStbReq(&createReq);
2022-05-06 17:47:45 +00:00
mndReleaseStb(pMnode, pStb);
mndReleaseDb(pMnode, pDb);
2025-04-24 01:11:54 +00:00
mDebug("stream:%s failed to create dst stable:%s, line:%d code:%s", pStream->name, pStream->outTblName, lino,
tstrerror(code));
2024-08-01 09:17:32 +00:00
return code;
2022-05-06 17:47:45 +00:00
}
2025-04-10 11:41:37 +00:00
static int32_t mndStreamValidateCreate(SMnode *pMnode, char* pUser, SCMCreateStreamReq* pCreate) {
int32_t code = 0, lino = 0;
int64_t streamId = pCreate->streamId;
2025-06-06 01:09:40 +00:00
if (pCreate->streamDB) {
code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_WRITE_DB, pCreate->streamDB);
if (code) {
mstsError("user %s failed to create stream %s in db %s since %s", pUser, pCreate->name, pCreate->streamDB, tstrerror(code));
}
TSDB_CHECK_CODE(code, lino, _OVER);
}
2025-04-10 11:41:37 +00:00
if (pCreate->triggerDB) {
code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_READ_DB, pCreate->triggerDB);
if (code) {
2025-05-23 10:14:34 +00:00
mstsError("user %s failed to create stream %s using trigger db %s since %s", pUser, pCreate->name, pCreate->triggerDB, tstrerror(code));
}
2025-04-10 11:41:37 +00:00
TSDB_CHECK_CODE(code, lino, _OVER);
}
2025-06-06 01:09:40 +00:00
if (pCreate->calcDB) {
int32_t dbNum = taosArrayGetSize(pCreate->calcDB);
for (int32_t i = 0; i < dbNum; ++i) {
char* calcDB = taosArrayGetP(pCreate->calcDB, i);
code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_READ_DB, calcDB);
if (code) {
mstsError("user %s failed to create stream %s using calcDB %s since %s", pUser, pCreate->name, calcDB, tstrerror(code));
}
TSDB_CHECK_CODE(code, lino, _OVER);
}
2025-04-10 11:41:37 +00:00
}
2025-04-10 11:41:37 +00:00
if (pCreate->outDB) {
code = mndCheckDbPrivilegeByName(pMnode, pUser, MND_OPER_WRITE_DB, pCreate->outDB);
if (code) {
2025-05-23 10:14:34 +00:00
mstsError("user %s failed to create stream %s using out db %s since %s", pUser, pCreate->name, pCreate->outDB, tstrerror(code));
}
2025-04-10 11:41:37 +00:00
TSDB_CHECK_CODE(code, lino, _OVER);
}
2025-04-10 11:41:37 +00:00
int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
if (streamNum > MND_STREAM_MAX_NUM) {
code = TSDB_CODE_MND_TOO_MANY_STREAMS;
2025-05-23 10:14:34 +00:00
mstsError("failed to create stream %s since %s, stream number:%d", pCreate->name, tstrerror(code), streamNum);
2025-04-10 11:41:37 +00:00
return code;
}
_OVER:
return code;
}
2022-06-16 08:01:22 +00:00
int32_t mndDropStreamByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
2024-08-02 03:38:30 +00:00
SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL;
int32_t code = 0;
2022-06-16 08:01:22 +00:00
while (1) {
2022-07-02 03:55:59 +00:00
SStreamObj *pStream = NULL;
2022-06-16 08:01:22 +00:00
pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
if (pIter == NULL) break;
2025-06-06 01:09:40 +00:00
if (0 == strcmp(pStream->pCreate->streamDB, pDb->name)) {
mInfo("start to drop stream %s in db %s", pStream->pCreate->name, pDb->name);
pStream->updateTime = taosGetTimestampMs();
atomic_store_8(&pStream->userDropped, 1);
MND_STREAM_SET_LAST_TS(STM_EVENT_DROP_STREAM, pStream->updateTime);
msmUndeployStream(pMnode, pStream->pCreate->streamId, pStream->pCreate->name);
// drop stream
code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_DROPPED);
if (code) {
mError("drop db trans:%d failed to append drop stream trans since %s", pTrans->id, tstrerror(code));
2022-06-16 08:01:22 +00:00
sdbRelease(pSdb, pStream);
2025-06-06 01:09:40 +00:00
TAOS_RETURN(code);
2022-06-16 08:01:22 +00:00
}
}
sdbRelease(pSdb, pStream);
}
return 0;
}
2022-05-16 06:55:31 +00:00
static int32_t mndRetrieveStream(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
2024-01-15 11:56:38 +00:00
SMnode *pMnode = pReq->info.node;
SSdb *pSdb = pMnode->pSdb;
2022-03-10 09:15:45 +00:00
int32_t numOfRows = 0;
SStreamObj *pStream = NULL;
int32_t code = 0;
2022-03-10 09:15:45 +00:00
while (numOfRows < rows) {
2022-04-28 08:31:35 +00:00
pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
2022-03-10 09:15:45 +00:00
if (pShow->pIter == NULL) break;
2025-06-17 08:55:32 +00:00
code = mstSetStreamAttrResBlock(pMnode, pStream, pBlock, numOfRows);
if (code == 0) {
numOfRows++;
}
sdbRelease(pSdb, pStream);
}
2022-03-10 09:15:45 +00:00
pShow->numOfRows += numOfRows;
return numOfRows;
}
2022-03-10 09:15:45 +00:00
static void mndCancelGetNextStream(SMnode *pMnode, void *pIter) {
SSdb *pSdb = pMnode->pSdb;
sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
}
2022-03-10 09:15:45 +00:00
static int32_t mndRetrieveStreamTask(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
2024-01-15 11:56:38 +00:00
SMnode *pMnode = pReq->info.node;
SSdb *pSdb = pMnode->pSdb;
int32_t numOfRows = 0;
SStreamObj *pStream = NULL;
int32_t code = 0;
2025-06-17 08:55:32 +00:00
while (numOfRows < rowsCapacity) {
pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
if (pShow->pIter == NULL) {
break;
}
code = mstSetStreamTasksResBlock(pStream, pBlock, &numOfRows, rowsCapacity);
sdbRelease(pSdb, pStream);
}
pShow->numOfRows += numOfRows;
return numOfRows;
}
static void mndCancelGetNextStreamTask(SMnode *pMnode, void *pIter) {
SSdb *pSdb = pMnode->pSdb;
sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
}
2023-03-28 11:39:38 +00:00
2025-06-24 10:06:20 +00:00
static int32_t mndRetrieveStreamRecalculates(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rowsCapacity) {
SMnode *pMnode = pReq->info.node;
SSdb *pSdb = pMnode->pSdb;
int32_t numOfRows = 0;
SStreamObj *pStream = NULL;
int32_t code = 0;
while (numOfRows < rowsCapacity) {
pShow->pIter = sdbFetch(pSdb, SDB_STREAM, pShow->pIter, (void **)&pStream);
if (pShow->pIter == NULL) {
break;
}
code = mstSetStreamRecalculatesResBlock(pStream, pBlock, &numOfRows, rowsCapacity);
sdbRelease(pSdb, pStream);
}
pShow->numOfRows += numOfRows;
return numOfRows;
}
static void mndCancelGetNextStreamRecalculates(SMnode *pMnode, void *pIter) {
SSdb *pSdb = pMnode->pSdb;
sdbCancelFetchByType(pSdb, pIter, SDB_STREAM);
}
2025-06-06 01:09:40 +00:00
static bool mndStreamUpdateTagsFlag(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
SStreamObj *pStream = pObj;
if (atomic_load_8(&pStream->userDropped)) {
return true;
}
if (TSDB_SUPER_TABLE != pStream->pCreate->triggerTblType &&
TSDB_CHILD_TABLE != pStream->pCreate->triggerTblType &&
TSDB_VIRTUAL_CHILD_TABLE != pStream->pCreate->triggerTblType) {
return true;
}
if (pStream->pCreate->triggerTblSuid != *(uint64_t*)p1) {
return true;
}
if (NULL == pStream->pCreate->partitionCols) {
return true;
}
SNodeList* pList = NULL;
int32_t code = nodesStringToList(pStream->pCreate->partitionCols, &pList);
if (code) {
mstError("partitionCols [%s] nodesStringToList failed with error:%s", (char*)pStream->pCreate->partitionCols, tstrerror(code));
return true;
}
SSchema* pTags = (SSchema*)p2;
int32_t* tagNum = (int32_t*)p3;
SNode* pNode = NULL;
FOREACH(pNode, pList) {
SColumnNode* pCol = (SColumnNode*)pNode;
for (int32_t i = 0; i < *tagNum; ++i) {
if (pCol->colId == pTags[i].colId) {
2025-06-16 07:19:05 +00:00
pTags[i].flags |= COL_REF_BY_STM;
2025-06-06 01:09:40 +00:00
break;
}
}
}
return true;
}
void mndStreamUpdateTagsRefFlag(SMnode *pMnode, int64_t suid, SSchema* pTags, int32_t tagNum) {
2025-06-06 01:09:40 +00:00
int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
if (streamNum <= 0) {
return;
2025-06-06 01:09:40 +00:00
}
sdbTraverse(pMnode->pSdb, SDB_STREAM, mndStreamUpdateTagsFlag, &suid, pTags, &tagNum);
}
2025-05-17 02:55:32 +00:00
static int32_t mndProcessStopStreamReq(SRpcMsg *pReq) {
2025-04-10 01:09:30 +00:00
SMnode *pMnode = pReq->info.node;
SStreamObj *pStream = NULL;
2025-04-10 11:41:37 +00:00
int32_t code = 0;
2025-04-10 11:41:37 +00:00
SMPauseStreamReq pauseReq = {0};
if (tDeserializeSMPauseStreamReq(pReq->pCont, pReq->contLen, &pauseReq) < 0) {
TAOS_RETURN(TSDB_CODE_INVALID_MSG);
}
2024-07-22 05:31:57 +00:00
2025-04-10 11:41:37 +00:00
code = mndAcquireStream(pMnode, pauseReq.name, &pStream);
if (pStream == NULL || code != 0) {
if (pauseReq.igNotExists) {
2025-06-06 01:09:40 +00:00
mInfo("stream:%s, not exist, not stop stream", pauseReq.name);
2025-04-10 11:41:37 +00:00
return 0;
} else {
2025-06-06 01:09:40 +00:00
mError("stream:%s not exist, failed to stop stream", pauseReq.name);
2025-04-10 11:41:37 +00:00
TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
}
}
2025-04-10 11:41:37 +00:00
int64_t streamId = pStream->pCreate->streamId;
2025-04-10 01:09:30 +00:00
2025-06-06 01:09:40 +00:00
mstsInfo("start to stop stream %s", pauseReq.name);
2025-04-10 11:41:37 +00:00
code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
if (code != TSDB_CODE_SUCCESS) {
2025-06-06 01:09:40 +00:00
mstsError("user %s failed to stop stream %s since %s", pReq->info.conn.user, pauseReq.name, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
return code;
}
2025-06-24 10:06:20 +00:00
if (atomic_load_8(&pStream->userDropped)) {
code = TSDB_CODE_MND_STREAM_DROPPING;
mstsError("user %s failed to stop stream %s since %s", pReq->info.conn.user, pauseReq.name, tstrerror(code));
sdbRelease(pMnode->pSdb, pStream);
return code;
}
2025-04-10 11:41:37 +00:00
STrans *pTrans = NULL;
2025-05-17 02:55:32 +00:00
code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_STOP_NAME, &pTrans);
2025-04-10 11:41:37 +00:00
if (pTrans == NULL || code) {
2025-06-06 01:09:40 +00:00
mstsError("failed to stop stream %s since %s", pauseReq.name, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
return code;
}
2025-05-28 10:37:21 +00:00
pStream->updateTime = taosGetTimestampMs();
2025-04-30 10:39:59 +00:00
atomic_store_8(&pStream->userStopped, 1);
2025-04-10 11:41:37 +00:00
2025-06-06 01:09:40 +00:00
MND_STREAM_SET_LAST_TS(STM_EVENT_STOP_STREAM, pStream->updateTime);
2025-05-30 00:56:34 +00:00
2025-05-19 01:54:46 +00:00
msmUndeployStream(pMnode, streamId, pStream->pCreate->name);
// stop stream
2025-04-10 11:41:37 +00:00
code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
if (code != TSDB_CODE_SUCCESS) {
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
return code;
}
code = mndTransPrepare(pMnode, pTrans);
if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2025-06-06 01:09:40 +00:00
mError("trans:%d, failed to prepare stop stream trans since %s", pTrans->id, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
return code;
}
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
return TSDB_CODE_ACTION_IN_PROGRESS;
}
2025-05-17 02:55:32 +00:00
static int32_t mndProcessStartStreamReq(SRpcMsg *pReq) {
2025-04-10 11:41:37 +00:00
SMnode *pMnode = pReq->info.node;
SStreamObj *pStream = NULL;
int32_t code = 0;
if ((code = grantCheckExpire(TSDB_GRANT_STREAMS)) < 0) {
return code;
}
SMResumeStreamReq resumeReq = {0};
if (tDeserializeSMResumeStreamReq(pReq->pCont, pReq->contLen, &resumeReq) < 0) {
TAOS_RETURN(TSDB_CODE_INVALID_MSG);
}
code = mndAcquireStream(pMnode, resumeReq.name, &pStream);
if (pStream == NULL || code != 0) {
if (resumeReq.igNotExists) {
2025-06-06 01:09:40 +00:00
mInfo("stream:%s not exist, not start stream", resumeReq.name);
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
return 0;
} else {
2025-06-06 01:09:40 +00:00
mError("stream:%s not exist, failed to start stream", resumeReq.name);
2025-04-10 11:41:37 +00:00
TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
2025-04-10 01:09:30 +00:00
}
}
2025-04-10 11:41:37 +00:00
int64_t streamId = pStream->pCreate->streamId;
2025-04-24 01:11:54 +00:00
2025-05-23 10:14:34 +00:00
mstsInfo("start to start stream %s from stopped", resumeReq.name);
2025-04-10 11:41:37 +00:00
code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
if (code != TSDB_CODE_SUCCESS) {
2025-06-06 01:09:40 +00:00
mstsError("user %s failed to start stream %s since %s", pReq->info.conn.user, resumeReq.name, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
return code;
2025-04-10 01:09:30 +00:00
}
2025-06-24 10:06:20 +00:00
if (atomic_load_8(&pStream->userDropped)) {
code = TSDB_CODE_MND_STREAM_DROPPING;
mstsError("user %s failed to start stream %s since %s", pReq->info.conn.user, resumeReq.name, tstrerror(code));
sdbRelease(pMnode->pSdb, pStream);
return code;
}
2025-05-30 00:56:34 +00:00
2025-06-24 10:06:20 +00:00
if (0 == atomic_load_8(&pStream->userStopped)) {
code = TSDB_CODE_MND_STREAM_NOT_STOPPED;
mstsError("user %s failed to start stream %s since %s", pReq->info.conn.user, resumeReq.name, tstrerror(code));
sdbRelease(pMnode->pSdb, pStream);
return code;
}
2025-05-30 00:56:34 +00:00
atomic_store_8(&pStream->userStopped, 0);
2025-06-24 10:06:20 +00:00
pStream->updateTime = taosGetTimestampMs();
2025-06-06 01:09:40 +00:00
MND_STREAM_SET_LAST_TS(STM_EVENT_START_STREAM, pStream->updateTime);
2025-05-30 00:56:34 +00:00
2025-04-10 11:41:37 +00:00
STrans *pTrans = NULL;
2025-05-17 02:55:32 +00:00
code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_START_NAME, &pTrans);
2025-04-10 11:41:37 +00:00
if (pTrans == NULL || code) {
2025-06-06 01:09:40 +00:00
mstsError("failed to start stream %s since %s", resumeReq.name, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
return code;
}
code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
if (code != TSDB_CODE_SUCCESS) {
2025-06-06 01:09:40 +00:00
mstsError("failed to start stream %s since %s", resumeReq.name, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
return code;
}
code = mndTransPrepare(pMnode, pTrans);
if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2025-06-06 01:09:40 +00:00
mstsError("trans:%d, failed to prepare start stream %s trans since %s", pTrans->id, resumeReq.name, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
return code;
}
2025-06-24 10:06:20 +00:00
mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, true, STREAM_ACT_DEPLOY);
2025-04-14 05:20:57 +00:00
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
return TSDB_CODE_ACTION_IN_PROGRESS;
}
static int32_t mndProcessDropStreamReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
SStreamObj *pStream = NULL;
int32_t code = 0;
SMDropStreamReq dropReq = {0};
if (tDeserializeSMDropStreamReq(pReq->pCont, pReq->contLen, &dropReq) < 0) {
mError("invalid drop stream msg recv, discarded");
code = TSDB_CODE_INVALID_MSG;
TAOS_RETURN(code);
}
mDebug("recv drop stream:%s msg", dropReq.name);
code = mndAcquireStream(pMnode, dropReq.name, &pStream);
if (pStream == NULL || code != 0) {
if (dropReq.igNotExists) {
mInfo("stream:%s not exist, ignore not exist is set, drop stream exec done with success", dropReq.name);
sdbRelease(pMnode->pSdb, pStream);
tFreeMDropStreamReq(&dropReq);
return 0;
} else {
mError("stream:%s not exist failed to drop it", dropReq.name);
tFreeMDropStreamReq(&dropReq);
TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
}
}
2025-04-10 11:41:37 +00:00
int64_t streamId = pStream->pCreate->streamId;
code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
2025-04-10 01:09:30 +00:00
if (code != 0) {
2025-05-23 10:14:34 +00:00
mstsError("user %s failed to drop stream %s since %s", pReq->info.conn.user, dropReq.name, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
tFreeMDropStreamReq(&dropReq);
return code;
}
2025-04-10 11:41:37 +00:00
if (pStream->pCreate->tsmaId != 0) {
2025-05-23 10:14:34 +00:00
mstsDebug("try to drop tsma related stream, tsmaId:%" PRIx64, pStream->pCreate->tsmaId);
2025-04-10 11:41:37 +00:00
void *pIter = NULL;
SSmaObj *pSma = NULL;
pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
while (pIter) {
if (pSma && pSma->uid == pStream->pCreate->tsmaId) {
sdbRelease(pMnode->pSdb, pSma);
sdbRelease(pMnode->pSdb, pStream);
sdbCancelFetch(pMnode->pSdb, pIter);
tFreeMDropStreamReq(&dropReq);
code = TSDB_CODE_TSMA_MUST_BE_DROPPED;
2025-05-23 10:14:34 +00:00
mstsError("refused to drop tsma-related stream %s since tsma still exists", dropReq.name);
2025-04-10 11:41:37 +00:00
TAOS_RETURN(code);
}
if (pSma) {
sdbRelease(pMnode->pSdb, pSma);
}
pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSma);
}
2025-04-10 01:09:30 +00:00
}
2025-05-23 10:14:34 +00:00
mstsInfo("start to drop stream %s", pStream->pCreate->name);
2025-05-06 02:27:12 +00:00
2025-05-28 10:37:21 +00:00
pStream->updateTime = taosGetTimestampMs();
2025-05-06 02:27:12 +00:00
atomic_store_8(&pStream->userDropped, 1);
2025-04-30 10:39:59 +00:00
2025-06-06 01:09:40 +00:00
MND_STREAM_SET_LAST_TS(STM_EVENT_DROP_STREAM, pStream->updateTime);
2025-05-30 00:56:34 +00:00
2025-04-30 10:39:59 +00:00
msmUndeployStream(pMnode, streamId, pStream->pCreate->name);
2025-04-14 05:20:57 +00:00
2025-04-10 11:41:37 +00:00
STrans *pTrans = NULL;
code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_DROP_NAME, &pTrans);
if (pTrans == NULL || code) {
2025-05-23 10:14:34 +00:00
mstsError("failed to drop stream %s since %s", dropReq.name, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
tFreeMDropStreamReq(&dropReq);
TAOS_RETURN(code);
}
2025-04-10 11:41:37 +00:00
// drop stream
code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_DROPPED);
if (code) {
2025-05-23 10:14:34 +00:00
mstsError("trans:%d, failed to append drop stream trans since %s", pTrans->id, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
tFreeMDropStreamReq(&dropReq);
TAOS_RETURN(code);
}
2025-04-10 11:41:37 +00:00
code = mndTransPrepare(pMnode, pTrans);
if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2025-05-23 10:14:34 +00:00
mstsError("trans:%d, failed to prepare drop stream trans since %s", pTrans->id, tstrerror(code));
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
tFreeMDropStreamReq(&dropReq);
TAOS_RETURN(code);
}
2025-04-29 11:22:25 +00:00
auditRecord(pReq, pMnode->clusterId, "dropStream", "", pStream->pCreate->streamDB, NULL, 0);
2025-04-10 11:41:37 +00:00
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
2025-05-23 10:14:34 +00:00
mstsDebug("drop stream %s half completed", dropReq.name);
2025-04-10 11:41:37 +00:00
code = TSDB_CODE_ACTION_IN_PROGRESS;
tFreeMDropStreamReq(&dropReq);
TAOS_RETURN(code);
}
static int32_t mndProcessCreateStreamReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
SStreamObj *pStream = NULL;
SStreamObj streamObj = {0};
int32_t code = TSDB_CODE_SUCCESS;
int32_t lino = 0;
STrans *pTrans = NULL;
SCMCreateStreamReq* pCreate = taosMemoryCalloc(1, sizeof(SCMCreateStreamReq));
TSDB_CHECK_NULL(pCreate, code, lino, _OVER, terrno);
code = tDeserializeSCMCreateStreamReq(pReq->pCont, pReq->contLen, pCreate);
2025-04-10 01:09:30 +00:00
TSDB_CHECK_CODE(code, lino, _OVER);
2025-04-10 11:41:37 +00:00
#ifdef WINDOWS
code = TSDB_CODE_MND_INVALID_PLATFORM;
goto _OVER;
#endif
uint64_t streamId = pCreate->streamId;
2025-04-10 01:09:30 +00:00
2025-05-23 10:14:34 +00:00
mstsInfo("start to create stream %s, sql:%s", pCreate->name, pCreate->sql);
2025-04-10 11:41:37 +00:00
2025-05-17 02:55:32 +00:00
int32_t snodeId = msmAssignRandomSnodeId(pMnode, streamId);
2025-06-06 01:09:40 +00:00
if (!GOT_SNODE(snodeId)) {
2025-05-17 02:55:32 +00:00
code = terrno;
TSDB_CHECK_CODE(code, lino, _OVER);
}
2025-04-10 11:41:37 +00:00
code = mndAcquireStream(pMnode, pCreate->name, &pStream);
if (pStream != NULL && code == 0) {
if (pCreate->igExists) {
2025-05-23 10:14:34 +00:00
mstsInfo("stream %s already exist, ignore exist is set", pCreate->name);
2025-04-10 11:41:37 +00:00
} else {
code = TSDB_CODE_MND_STREAM_ALREADY_EXIST;
2025-04-10 01:09:30 +00:00
}
2024-08-02 02:47:06 +00:00
2025-04-10 11:41:37 +00:00
mndReleaseStream(pMnode, pStream);
goto _OVER;
} else if (code != TSDB_CODE_MND_STREAM_NOT_EXIST) {
goto _OVER;
}
if ((code = grantCheck(TSDB_GRANT_STREAMS)) < 0) {
goto _OVER;
}
2025-04-25 06:59:41 +00:00
code = mndStreamValidateCreate(pMnode, pReq->info.conn.user, pCreate);
TSDB_CHECK_CODE(code, lino, _OVER);
2025-05-17 02:55:32 +00:00
code = mndStreamBuildObj(pMnode, &streamObj, pCreate, snodeId);
2025-04-10 11:41:37 +00:00
TSDB_CHECK_CODE(code, lino, _OVER);
pStream = &streamObj;
code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_DB, MND_STREAM_CREATE_NAME, &pTrans);
2025-04-10 01:09:30 +00:00
if (pTrans == NULL || code) {
goto _OVER;
}
2025-04-10 01:09:30 +00:00
// create stb for stream
2025-04-24 01:11:54 +00:00
if (TSDB_SUPER_TABLE == pCreate->outTblType && !pCreate->outStbExists) {
pCreate->outStbUid = mndGenerateUid(pCreate->outTblName, strlen(pCreate->outTblName));
code = mndStreamCreateOutStb(pMnode, pTrans, pStream->pCreate, pReq->info.conn.user);
2025-04-10 11:41:37 +00:00
TSDB_CHECK_CODE(code, lino, _OVER);
}
2025-04-10 01:09:30 +00:00
// add stream to trans
2025-04-10 11:41:37 +00:00
code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
2025-04-10 01:09:30 +00:00
if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2025-05-23 10:14:34 +00:00
mstsError("failed to persist stream %s since %s", pCreate->name, tstrerror(code));
2025-04-10 01:09:30 +00:00
goto _OVER;
}
2025-04-10 01:09:30 +00:00
// execute creation
code = mndTransPrepare(pMnode, pTrans);
if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2025-05-23 10:14:34 +00:00
mstsError("trans:%d, failed to prepare since %s", pTrans->id, tstrerror(code));
2025-04-10 01:09:30 +00:00
goto _OVER;
}
2025-04-10 11:41:37 +00:00
auditRecord(pReq, pMnode->clusterId, "createStream", pCreate->streamDB, pCreate->name, pCreate->sql, strlen(pCreate->sql));
2025-06-06 01:09:40 +00:00
MND_STREAM_SET_LAST_TS(STM_EVENT_CREATE_STREAM, taosGetTimestampMs());
2025-05-30 00:56:34 +00:00
2025-06-24 10:06:20 +00:00
mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, true, STREAM_ACT_DEPLOY);
2025-04-10 01:09:30 +00:00
_OVER:
2025-04-10 11:41:37 +00:00
2025-04-10 01:09:30 +00:00
if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
2025-05-23 10:14:34 +00:00
mstsError("failed to create stream %s at line:%d since %s", pCreate->name, lino, tstrerror(code));
2025-04-10 01:09:30 +00:00
} else {
2025-05-23 10:14:34 +00:00
mstsDebug("create stream %s half completed", pCreate->name);
2025-04-10 01:09:30 +00:00
code = TSDB_CODE_ACTION_IN_PROGRESS;
}
2024-08-02 02:47:06 +00:00
mndTransDrop(pTrans);
2025-04-10 01:09:30 +00:00
tFreeStreamObj(&streamObj);
2024-08-02 02:47:06 +00:00
return code;
2024-08-16 08:53:44 +00:00
}
2025-04-10 01:09:30 +00:00
2025-06-21 10:50:48 +00:00
static int32_t mndProcessRecalcStreamReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node;
SStreamObj *pStream = NULL;
int32_t code = 0;
SMRecalcStreamReq recalcReq = {0};
if (tDeserializeSMRecalcStreamReq(pReq->pCont, pReq->contLen, &recalcReq) < 0) {
TAOS_RETURN(TSDB_CODE_INVALID_MSG);
}
code = mndAcquireStream(pMnode, recalcReq.name, &pStream);
if (pStream == NULL || code != 0) {
mError("stream:%s not exist, failed to recalc stream", recalcReq.name);
TAOS_RETURN(TSDB_CODE_MND_STREAM_NOT_EXIST);
}
int64_t streamId = pStream->pCreate->streamId;
mstsInfo("start to recalc stream %s", recalcReq.name);
code = mndCheckDbPrivilegeByName(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pStream->pCreate->streamDB);
if (code != TSDB_CODE_SUCCESS) {
mstsError("user %s failed to recalc stream %s since %s", pReq->info.conn.user, recalcReq.name, tstrerror(code));
sdbRelease(pMnode->pSdb, pStream);
return code;
}
2025-06-24 10:06:20 +00:00
if (atomic_load_8(&pStream->userDropped)) {
code = TSDB_CODE_MND_STREAM_DROPPING;
mstsError("user %s failed to recalc stream %s since %s", pReq->info.conn.user, recalcReq.name, tstrerror(code));
sdbRelease(pMnode->pSdb, pStream);
return code;
}
if (atomic_load_8(&pStream->userStopped)) {
code = TSDB_CODE_MND_STREAM_STOPPED;
mstsError("user %s failed to recalc stream %s since %s", pReq->info.conn.user, recalcReq.name, tstrerror(code));
sdbRelease(pMnode->pSdb, pStream);
return code;
}
2025-06-21 10:50:48 +00:00
if (WINDOW_TYPE_PERIOD == pStream->pCreate->triggerType) {
code = TSDB_CODE_OPS_NOT_SUPPORT;
mstsError("failed to recalc stream %s since %s", recalcReq.name, tstrerror(code));
sdbRelease(pMnode->pSdb, pStream);
return code;
}
2025-06-24 05:21:01 +00:00
/*
pStream->updateTime = taosGetTimestampMs();
2025-06-21 10:50:48 +00:00
STrans *pTrans = NULL;
code = mndStreamCreateTrans(pMnode, pStream, pReq, TRN_CONFLICT_NOTHING, MND_STREAM_RECALC_NAME, &pTrans);
if (pTrans == NULL || code) {
mstsError("failed to recalc stream %s since %s", recalcReq.name, tstrerror(code));
sdbRelease(pMnode->pSdb, pStream);
return code;
}
// stop stream
code = mndStreamTransAppend(pStream, pTrans, SDB_STATUS_READY);
if (code != TSDB_CODE_SUCCESS) {
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
return code;
}
code = mndTransPrepare(pMnode, pTrans);
if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
mError("trans:%d, failed to prepare stop stream trans since %s", pTrans->id, tstrerror(code));
sdbRelease(pMnode->pSdb, pStream);
mndTransDrop(pTrans);
return code;
}
2025-06-24 05:21:01 +00:00
*/
code = msmRecalcStream(pMnode, pStream->pCreate->streamId, &recalcReq.timeRange);
if (code != TSDB_CODE_SUCCESS) {
sdbRelease(pMnode->pSdb, pStream);
return code;
}
char buf[128];
snprintf(buf, sizeof(buf), "start:%" PRId64 ", end:%" PRId64, recalcReq.timeRange.skey, recalcReq.timeRange.ekey);
auditRecord(pReq, pMnode->clusterId, "recalcStream", pStream->name, recalcReq.name, buf, strlen(buf));
2025-06-21 10:50:48 +00:00
sdbRelease(pMnode->pSdb, pStream);
2025-06-24 05:21:01 +00:00
// mndTransDrop(pTrans);
2025-06-21 10:50:48 +00:00
return TSDB_CODE_ACTION_IN_PROGRESS;
}
2025-04-10 11:41:37 +00:00
int32_t mndInitStream(SMnode *pMnode) {
SSdbTable table = {
.sdbType = SDB_STREAM,
.keyType = SDB_KEY_BINARY,
.encodeFp = (SdbEncodeFp)mndStreamActionEncode,
.decodeFp = (SdbDecodeFp)mndStreamActionDecode,
.insertFp = (SdbInsertFp)mndStreamActionInsert,
.updateFp = (SdbUpdateFp)mndStreamActionUpdate,
.deleteFp = (SdbDeleteFp)mndStreamActionDelete,
};
2025-04-28 08:47:32 +00:00
/*
2025-04-10 11:41:37 +00:00
SSdbTable tableSeq = {
.sdbType = SDB_STREAM_SEQ,
.keyType = SDB_KEY_BINARY,
.encodeFp = (SdbEncodeFp)mndStreamSeqActionEncode,
.decodeFp = (SdbDecodeFp)mndStreamSeqActionDecode,
.insertFp = (SdbInsertFp)mndStreamSeqActionInsert,
.updateFp = (SdbUpdateFp)mndStreamSeqActionUpdate,
.deleteFp = (SdbDeleteFp)mndStreamSeqActionDelete,
};
2025-04-28 08:47:32 +00:00
*/
2025-04-10 11:41:37 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_CREATE_STREAM, mndProcessCreateStreamReq);
mndSetMsgHandle(pMnode, TDMT_MND_DROP_STREAM, mndProcessDropStreamReq);
2025-05-17 02:55:32 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_START_STREAM, mndProcessStartStreamReq);
mndSetMsgHandle(pMnode, TDMT_MND_STOP_STREAM, mndProcessStopStreamReq);
2025-04-14 05:20:57 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_STREAM_HEARTBEAT, mndProcessStreamHb);
2025-06-21 10:50:48 +00:00
mndSetMsgHandle(pMnode, TDMT_MND_RECALC_STREAM, mndProcessRecalcStreamReq);
2025-04-10 11:41:37 +00:00
mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndRetrieveStream);
mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAMS, mndCancelGetNextStream);
mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndRetrieveStreamTask);
mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_TASKS, mndCancelGetNextStreamTask);
2025-06-24 10:06:20 +00:00
mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_STREAM_RECALCULATES, mndRetrieveStreamRecalculates);
mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_STREAM_RECALCULATES, mndCancelGetNextStreamRecalculates);
2025-04-10 11:41:37 +00:00
2025-05-23 10:14:34 +00:00
int32_t code = sdbSetTable(pMnode->pSdb, table);
2025-04-10 11:41:37 +00:00
if (code) {
return code;
}
2025-04-28 08:47:32 +00:00
//code = sdbSetTable(pMnode->pSdb, tableSeq);
2025-04-10 11:41:37 +00:00
return code;
}