From 26f5b0c136797814edf74c66d688f3f1839f7676 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 9 May 2022 16:03:31 +0800 Subject: [PATCH 1/4] feat: make grant revoke work --- include/common/tmsg.h | 10 +- source/dnode/mnode/impl/inc/mndAuth.h | 2 +- source/dnode/mnode/impl/src/mndAuth.c | 17 +--- source/dnode/mnode/impl/src/mndUser.c | 135 ++++++++++++++++---------- 4 files changed, 93 insertions(+), 71 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index e797aff341..9ec80293a8 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -131,12 +131,10 @@ typedef enum _mgmt_table { #define TSDB_ALTER_USER_SUPERUSER 0x2 #define TSDB_ALTER_USER_ADD_READ_DB 0x3 #define TSDB_ALTER_USER_REMOVE_READ_DB 0x4 -#define TSDB_ALTER_USER_CLEAR_READ_DB 0x5 -#define TSDB_ALTER_USER_ADD_WRITE_DB 0x6 -#define TSDB_ALTER_USER_REMOVE_WRITE_DB 0x7 -#define TSDB_ALTER_USER_CLEAR_WRITE_DB 0x8 -#define TSDB_ALTER_USER_ADD_ALL_DB 0x9 -#define TSDB_ALTER_USER_REMOVE_ALL_DB 0xA +#define TSDB_ALTER_USER_ADD_WRITE_DB 0x5 +#define TSDB_ALTER_USER_REMOVE_WRITE_DB 0x6 +#define TSDB_ALTER_USER_ADD_ALL_DB 0x7 +#define TSDB_ALTER_USER_REMOVE_ALL_DB 0x8 #define TSDB_ALTER_USER_PRIVILEGES 0x2 diff --git a/source/dnode/mnode/impl/inc/mndAuth.h b/source/dnode/mnode/impl/inc/mndAuth.h index 890879912f..de59a11cd7 100644 --- a/source/dnode/mnode/impl/inc/mndAuth.h +++ b/source/dnode/mnode/impl/inc/mndAuth.h @@ -26,7 +26,7 @@ int32_t mndInitAuth(SMnode *pMnode); void mndCleanupAuth(SMnode *pMnode); int32_t mndCheckCreateUserAuth(SUserObj *pOperUser); -int32_t mndCheckAlterUserAuth(SUserObj *pOperUser, SUserObj *pUser, SDbObj *pDb, SAlterUserReq *pAlter); +int32_t mndCheckAlterUserAuth(SUserObj *pOperUser, SUserObj *pUser, SAlterUserReq *pAlter); int32_t mndCheckDropUserAuth(SUserObj *pOperUser); int32_t mndCheckNodeAuth(SUserObj *pOperUser); diff --git a/source/dnode/mnode/impl/src/mndAuth.c b/source/dnode/mnode/impl/src/mndAuth.c index 8e5ec40c47..1d89241dd5 100644 --- a/source/dnode/mnode/impl/src/mndAuth.c +++ b/source/dnode/mnode/impl/src/mndAuth.c @@ -79,14 +79,12 @@ int32_t mndCheckCreateUserAuth(SUserObj *pOperUser) { return -1; } -int32_t mndCheckAlterUserAuth(SUserObj *pOperUser, SUserObj *pUser, SDbObj *pDb, SAlterUserReq *pAlter) { +int32_t mndCheckAlterUserAuth(SUserObj *pOperUser, SUserObj *pUser, SAlterUserReq *pAlter) { if (pAlter->alterType == TSDB_ALTER_USER_PASSWD) { if (pOperUser->superUser || strcmp(pUser->user, pOperUser->user) == 0) { return 0; } - } - - if (pAlter->alterType == TSDB_ALTER_USER_SUPERUSER) { + } else if (pAlter->alterType == TSDB_ALTER_USER_SUPERUSER) { if (strcmp(pUser->user, TSDB_DEFAULT_USER) == 0) { terrno = TSDB_CODE_MND_NO_RIGHTS; return -1; @@ -95,21 +93,12 @@ int32_t mndCheckAlterUserAuth(SUserObj *pOperUser, SUserObj *pUser, SDbObj *pDb, if (pOperUser->superUser) { return 0; } - } - - if (pAlter->alterType == TSDB_ALTER_USER_CLEAR_WRITE_DB || pAlter->alterType == TSDB_ALTER_USER_CLEAR_READ_DB) { + } else { if (pOperUser->superUser) { return 0; } } - if (pAlter->alterType == TSDB_ALTER_USER_ADD_READ_DB || pAlter->alterType == TSDB_ALTER_USER_REMOVE_READ_DB || - pAlter->alterType == TSDB_ALTER_USER_ADD_WRITE_DB || pAlter->alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB) { - if (pOperUser->superUser || strcmp(pUser->user, pDb->createUser) == 0) { - return 0; - } - } - terrno = TSDB_CODE_MND_NO_RIGHTS; return -1; } diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index d0af17ff5c..1706820bdc 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -394,6 +394,8 @@ static SHashObj *mndDupDbHash(SHashObj *pOld) { static int32_t mndProcessAlterUserReq(SNodeMsg *pReq) { SMnode *pMnode = pReq->pNode; + SSdb *pSdb = pMnode->pSdb; + void *pIter = NULL; int32_t code = -1; SUserObj *pUser = NULL; SUserObj *pOperUser = NULL; @@ -429,7 +431,13 @@ static int32_t mndProcessAlterUserReq(SNodeMsg *pReq) { goto _OVER; } + if (mndCheckAlterUserAuth(pOperUser, pUser, &alterReq) != 0) { + goto _OVER; + } + memcpy(&newUser, pUser, sizeof(SUserObj)); + newUser.authVersion++; + newUser.updateTime = taosGetTimestampMs(); taosRLockLatch(&pUser->lock); newUser.readDbs = mndDupDbHash(pUser->readDbs); @@ -440,63 +448,90 @@ static int32_t mndProcessAlterUserReq(SNodeMsg *pReq) { goto _OVER; } - int32_t len = strlen(alterReq.dbname) + 1; - SDbObj *pDb = mndAcquireDb(pMnode, alterReq.dbname); - mndReleaseDb(pMnode, pDb); - if (alterReq.alterType == TSDB_ALTER_USER_PASSWD) { char pass[TSDB_PASSWORD_LEN + 1] = {0}; taosEncryptPass_c((uint8_t *)alterReq.pass, strlen(alterReq.pass), pass); memcpy(newUser.pass, pass, TSDB_PASSWORD_LEN); - } else if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) { - newUser.superUser = alterReq.superUser; - } else if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_DB) { - if (pDb == NULL) { - terrno = TSDB_CODE_MND_DB_NOT_EXIST; - goto _OVER; - } - if (taosHashPut(newUser.readDbs, alterReq.dbname, len, alterReq.dbname, TSDB_DB_FNAME_LEN) != 0) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - goto _OVER; - } - newUser.authVersion++; - } else if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_DB) { - if (taosHashRemove(newUser.readDbs, alterReq.dbname, len) != 0) { - terrno = TSDB_CODE_MND_DB_NOT_EXIST; - goto _OVER; - } - newUser.authVersion++; - } else if (alterReq.alterType == TSDB_ALTER_USER_CLEAR_READ_DB) { - taosHashClear(newUser.readDbs); - newUser.authVersion++; - } else if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_DB) { - if (pDb == NULL) { - terrno = TSDB_CODE_MND_DB_NOT_EXIST; - goto _OVER; - } - if (taosHashPut(newUser.writeDbs, alterReq.dbname, len, alterReq.dbname, TSDB_DB_FNAME_LEN) != 0) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - goto _OVER; - } - newUser.authVersion++; - } else if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB) { - if (taosHashRemove(newUser.writeDbs, alterReq.dbname, len) != 0) { - terrno = TSDB_CODE_MND_DB_NOT_EXIST; - goto _OVER; - } - newUser.authVersion++; - } else if (alterReq.alterType == TSDB_ALTER_USER_CLEAR_WRITE_DB) { - taosHashClear(newUser.writeDbs); - newUser.authVersion++; - } else { - terrno = TSDB_CODE_MND_INVALID_ALTER_OPER; - goto _OVER; } - newUser.updateTime = taosGetTimestampMs(); + if (alterReq.alterType == TSDB_ALTER_USER_SUPERUSER) { + newUser.superUser = alterReq.superUser; + } - if (mndCheckAlterUserAuth(pOperUser, pUser, pDb, &alterReq) != 0) { - goto _OVER; + if (alterReq.alterType == TSDB_ALTER_USER_ADD_READ_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) { + if (strcmp(alterReq.dbname, "*") != 0) { + int32_t len = strlen(alterReq.dbname) + 1; + SDbObj *pDb = mndAcquireDb(pMnode, alterReq.dbname); + if (pDb == NULL) { + mndReleaseDb(pMnode, pDb); + goto _OVER; + } + if (taosHashPut(newUser.readDbs, alterReq.dbname, len, alterReq.dbname, TSDB_DB_FNAME_LEN) != 0) { + mndReleaseDb(pMnode, pDb); + goto _OVER; + } + } else { + while (1) { + SDbObj *pDb = NULL; + pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb); + if (pIter == NULL) break; + int32_t len = strlen(pDb->name) + 1; + taosHashPut(newUser.readDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN); + sdbRelease(pSdb, pDb); + } + } + } + + if (alterReq.alterType == TSDB_ALTER_USER_ADD_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_ADD_ALL_DB) { + if (strcmp(alterReq.dbname, "*") != 0) { + int32_t len = strlen(alterReq.dbname) + 1; + SDbObj *pDb = mndAcquireDb(pMnode, alterReq.dbname); + if (pDb == NULL) { + mndReleaseDb(pMnode, pDb); + goto _OVER; + } + if (taosHashPut(newUser.writeDbs, alterReq.dbname, len, alterReq.dbname, TSDB_DB_FNAME_LEN) != 0) { + mndReleaseDb(pMnode, pDb); + goto _OVER; + } + } else { + while (1) { + SDbObj *pDb = NULL; + pIter = sdbFetch(pSdb, SDB_DB, pIter, (void **)&pDb); + if (pIter == NULL) break; + int32_t len = strlen(pDb->name) + 1; + taosHashPut(newUser.writeDbs, pDb->name, len, pDb->name, TSDB_DB_FNAME_LEN); + sdbRelease(pSdb, pDb); + } + } + } + + if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_READ_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) { + if (strcmp(alterReq.dbname, "*") != 0) { + int32_t len = strlen(alterReq.dbname) + 1; + SDbObj *pDb = mndAcquireDb(pMnode, alterReq.dbname); + if (pDb == NULL) { + mndReleaseDb(pMnode, pDb); + goto _OVER; + } + taosHashRemove(newUser.readDbs, alterReq.dbname, len); + } else { + taosHashClear(newUser.readDbs); + } + } + + if (alterReq.alterType == TSDB_ALTER_USER_REMOVE_WRITE_DB || alterReq.alterType == TSDB_ALTER_USER_REMOVE_ALL_DB) { + if (strcmp(alterReq.dbname, "*") != 0) { + int32_t len = strlen(alterReq.dbname) + 1; + SDbObj *pDb = mndAcquireDb(pMnode, alterReq.dbname); + if (pDb == NULL) { + mndReleaseDb(pMnode, pDb); + goto _OVER; + } + taosHashRemove(newUser.writeDbs, alterReq.dbname, len); + } else { + taosHashClear(newUser.writeDbs); + } } code = mndAlterUser(pMnode, pUser, &newUser, pReq); From a83a10f1c5a0179306885eed32deac31ca9d7a63 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 9 May 2022 16:03:50 +0800 Subject: [PATCH 2/4] test: add test case for grant revoke user --- source/dnode/mnode/impl/test/user/user.cpp | 6 +- tests/script/tsim/user/privilege1.sim | 71 ++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tests/script/tsim/user/privilege1.sim diff --git a/source/dnode/mnode/impl/test/user/user.cpp b/source/dnode/mnode/impl/test/user/user.cpp index ee961e9a27..1e03d8ff4a 100644 --- a/source/dnode/mnode/impl/test/user/user.cpp +++ b/source/dnode/mnode/impl/test/user/user.cpp @@ -238,9 +238,10 @@ TEST_F(MndTestUser, 03_Alter_User) { { SAlterUserReq alterReq = {0}; - alterReq.alterType = TSDB_ALTER_USER_CLEAR_WRITE_DB; + alterReq.alterType = TSDB_ALTER_USER_REMOVE_ALL_DB; strcpy(alterReq.user, "u3"); strcpy(alterReq.pass, "1"); + strcpy(alterReq.dbname, "*"); int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq); void* pReq = rpcMallocCont(contLen); @@ -253,9 +254,10 @@ TEST_F(MndTestUser, 03_Alter_User) { { SAlterUserReq alterReq = {0}; - alterReq.alterType = TSDB_ALTER_USER_CLEAR_READ_DB; + alterReq.alterType = TSDB_ALTER_USER_REMOVE_ALL_DB; strcpy(alterReq.user, "u3"); strcpy(alterReq.pass, "1"); + strcpy(alterReq.dbname, "*"); int32_t contLen = tSerializeSAlterUserReq(NULL, 0, &alterReq); void* pReq = rpcMallocCont(contLen); diff --git a/tests/script/tsim/user/privilege1.sim b/tests/script/tsim/user/privilege1.sim new file mode 100644 index 0000000000..a7c5d9d13d --- /dev/null +++ b/tests/script/tsim/user/privilege1.sim @@ -0,0 +1,71 @@ +system sh/stop_dnodes.sh +system sh/deploy.sh -n dnode1 -i 1 +system sh/exec.sh -n dnode1 -s start +sql connect + +print =============== show users +sql create database d1 vgroups 1; +sql create database d2 vgroups 1; +sql create database d3 vgroups 1; +sql show databases +if $rows != 5 then + return -1 +endi + +print =============== create users +sql create user user1 PASS 'user1' +sql create user user2 PASS 'user2' +sql show users +if $rows != 3 then + return -1 +endi + +print =============== test read +sql_error GRANT read ON d1.* to a; +sql_error GRANT read ON d0.* to user1; + +sql GRANT read ON d1.* to user1; +sql GRANT read ON d2.* to user1; +sql GRANT read ON *.* to user1; + +sql REVOKE read ON d1.* from user1; +sql REVOKE read ON d2.* from user1; +sql REVOKE read ON *.* from user1; + +print =============== test write +sql_error GRANT write ON d1.* to a; +sql_error GRANT write ON d0.* to user1; + +sql GRANT write ON d1.* to user1; +sql GRANT write ON d2.* to user1; +sql GRANT write ON *.* to user1; + +sql REVOKE write ON d1.* from user1; +sql REVOKE write ON d2.* from user1; +sql REVOKE write ON *.* from user1; + +print =============== test all +sql_error GRANT all ON d1.* to a; +sql_error GRANT all ON d0.* to user1; + +sql GRANT all ON d1.* to user1; +sql GRANT all ON d2.* to user1; +sql GRANT all ON *.* to user1; + +sql REVOKE all ON d1.* from user1; +sql REVOKE all ON d2.* from user1; +sql REVOKE all ON *.* from user1; + +print =============== test read write +sql_error GRANT read,write ON d1.* to a; +sql_error GRANT read,write ON d0.* to user1; + +sql GRANT read,write ON d1.* to user1; +sql GRANT read,write ON d2.* to user1; +sql GRANT read,write ON *.* to user1; + +sql REVOKE read,write ON d1.* from user1; +sql REVOKE read,write ON d2.* from user1; +sql REVOKE read,write ON *.* from user1; + +system sh/exec.sh -n dnode1 -s stop -x SIGINT From a9f6fa3a8225165083e495f464053db9600c98e2 Mon Sep 17 00:00:00 2001 From: afwerar <1296468573@qq.com> Date: Mon, 9 May 2022 17:56:08 +0800 Subject: [PATCH 3/4] fix/ZhiqiangWang/fix-15189-mv-pipe-file-to-temp --- source/libs/transport/src/transSrv.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/source/libs/transport/src/transSrv.c b/source/libs/transport/src/transSrv.c index 7f8ad150f0..323ef43e25 100644 --- a/source/libs/transport/src/transSrv.c +++ b/source/libs/transport/src/transSrv.c @@ -853,12 +853,13 @@ void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, taosThreadOnce(&transModuleInit, uvInitEnv); transSrvInst++; - char pipeName[64]; assert(0 == uv_pipe_init(srv->loop, &srv->pipeListen, 0)); #ifdef WINDOWS - snprintf(pipeName, sizeof(pipeName), "\\\\?\\pipe\\trans.rpc\\%p-%lu", taosSafeRand(), GetCurrentProcessId()); + char pipeName[64]; + snprintf(pipeName, sizeof(pipeName), "\\\\?\\pipe\\trans.rpc.%p-%lu", taosSafeRand(), GetCurrentProcessId()); #else - snprintf(pipeName, sizeof(pipeName), ".trans.rpc\\%08X-%lu", taosSafeRand(), taosGetSelfPthreadId()); + char pipeName[PATH_MAX] = {0}; + snprintf(pipeName, sizeof(pipeName), "%s%spipe.trans.rpc.%08X-%lu", tsTempDir, TD_DIRSEP, taosSafeRand(), taosGetSelfPthreadId()); #endif assert(0 == uv_pipe_bind(&srv->pipeListen, pipeName)); assert(0 == uv_listen((uv_stream_t*)&srv->pipeListen, SOMAXCONN, uvPipeListenCb)); @@ -871,20 +872,6 @@ void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, thrd->pTransInst = shandle; srv->pipe[i] = (uv_pipe_t*)taosMemoryCalloc(2, sizeof(uv_pipe_t)); - - // #ifdef WINDOWS - // uv_file fds[2]; - // if (uv_pipe(fds, UV_READABLE_PIPE|UV_WRITABLE_PIPE|UV_NONBLOCK_PIPE, UV_READABLE_PIPE|UV_WRITABLE_PIPE|UV_NONBLOCK_PIPE) != 0) { - // #else - // uv_os_sock_t fds[2]; - // if (uv_socketpair(SOCK_STREAM, 0, fds, UV_NONBLOCK_PIPE, UV_NONBLOCK_PIPE) != 0) { - // #endif - // goto End; - // } - // uv_pipe_init(srv->loop, &(srv->pipe[i][0]), 1); - // uv_pipe_open(&(srv->pipe[i][0]), fds[1]); // init write - - // thrd->fd = fds[0]; thrd->pipe = &(srv->pipe[i][1]); // init read if (false == addHandleToWorkloop(thrd,pipeName)) { From a6b258a45fff86a0cc8b075fd39be2d0d4e4fa49 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Tue, 10 May 2022 17:12:11 +0800 Subject: [PATCH 4/4] feat(tmq): change default config --- include/common/tmsg.h | 2 +- include/common/tname.h | 16 ++++----- include/libs/stream/tstream.h | 7 ++++ source/client/src/tmq.c | 2 +- source/common/src/tname.c | 49 ++++++++++++-------------- source/dnode/mnode/impl/src/mndDef.c | 4 ++- source/dnode/mnode/impl/src/mndTopic.c | 10 ++++++ source/dnode/vnode/src/tq/tq.c | 12 ++++--- 8 files changed, 59 insertions(+), 43 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 9ec80293a8..5b0aaece99 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1455,7 +1455,7 @@ typedef struct { static FORCE_INLINE SMqRebInfo* tNewSMqRebSubscribe(const char* key) { SMqRebInfo* pRebInfo = (SMqRebInfo*)taosMemoryCalloc(1, sizeof(SMqRebInfo)); if (pRebInfo == NULL) { - goto _err; + return NULL; } strcpy(pRebInfo->key, key); pRebInfo->lostConsumers = taosArrayInit(0, sizeof(int64_t)); diff --git a/include/common/tname.h b/include/common/tname.h index ae2dc32335..28f97d1028 100644 --- a/include/common/tname.h +++ b/include/common/tname.h @@ -16,8 +16,8 @@ #ifndef _TD_COMMON_NAME_H_ #define _TD_COMMON_NAME_H_ -#include "tdef.h" #include "tarray.h" +#include "tdef.h" #ifdef __cplusplus extern "C" { @@ -65,19 +65,19 @@ bool tNameDBNameEqual(SName* left, SName* right); typedef struct { // input - SArray *tags; // element is SSmlKV - const char *sTableName; // super table name - uint8_t sTableNameLen; // the length of super table name + SArray* tags; // element is SSmlKv + const char* sTableName; // super table name + uint8_t sTableNameLen; // the length of super table name // output - char *childTableName; // must have size of TSDB_TABLE_NAME_LEN; - uint64_t uid; // child table uid, may be useful + char* childTableName; // must have size of TSDB_TABLE_NAME_LEN; + uint64_t uid; // child table uid, may be useful } RandTableName; -void buildChildTableName(RandTableName *rName); +void buildChildTableName(RandTableName* rName); #ifdef __cplusplus } #endif -#endif /*_TD_COMMON_NAME_H_*/ +#endif /*_TD_COMMON_NAME_H_*/ diff --git a/include/libs/stream/tstream.h b/include/libs/stream/tstream.h index e277622c40..56e6a39ce8 100644 --- a/include/libs/stream/tstream.h +++ b/include/libs/stream/tstream.h @@ -16,6 +16,7 @@ #include "tdatablock.h" #include "tmsg.h" #include "tmsgcb.h" +#include "tqueue.h" #include "trpc.h" #ifdef __cplusplus @@ -154,6 +155,10 @@ struct SStreamTask { STaskDispatcherShuffle shuffleDispatcher; }; + // msg buffer + int32_t memUsed; + STaosQueue* inputQ; + // application storage void* ahandle; }; @@ -194,6 +199,8 @@ typedef struct { SArray* res; // SArray } SStreamSinkReq; +int32_t streamEnqueueData(SStreamTask* pTask, const void* input, int32_t inputType); + int32_t streamExecTask(SStreamTask* pTask, SMsgCb* pMsgCb, const void* input, int32_t inputType, int32_t workId); #ifdef __cplusplus diff --git a/source/client/src/tmq.c b/source/client/src/tmq.c index d674b8286b..c768e001c5 100644 --- a/source/client/src/tmq.c +++ b/source/client/src/tmq.c @@ -187,7 +187,7 @@ typedef struct { tmq_conf_t* tmq_conf_new() { tmq_conf_t* conf = taosMemoryCalloc(1, sizeof(tmq_conf_t)); - conf->autoCommit = false; + conf->autoCommit = true; conf->autoCommitInterval = 5000; conf->resetOffset = TMQ_CONF__RESET_OFFSET__EARLIEAST; return conf; diff --git a/source/common/src/tname.c b/source/common/src/tname.c index 62ba4bfb79..56fbfed8ff 100644 --- a/source/common/src/tname.c +++ b/source/common/src/tname.c @@ -18,11 +18,9 @@ #include "tcommon.h" #include "tstrbuild.h" -#define VALID_NAME_TYPE(x) ((x) == TSDB_DB_NAME_T || (x) == TSDB_TABLE_NAME_T) +#define VALID_NAME_TYPE(x) ((x) == TSDB_DB_NAME_T || (x) == TSDB_TABLE_NAME_T) -bool tscValidateTableNameLength(size_t len) { - return len < TSDB_TABLE_NAME_LEN; -} +bool tscValidateTableNameLength(size_t len) { return len < TSDB_TABLE_NAME_LEN; } #if 0 // TODO refactor @@ -95,12 +93,12 @@ int64_t taosGetIntervalStartTimestamp(int64_t startTime, int64_t slidingTime, in * but in case of DST, the start time of one day need to be dynamically decided. */ // todo refactor to extract function that is available for Linux/Windows/Mac platform - #if defined(WINDOWS) && _MSC_VER >= 1900 +#if defined(WINDOWS) && _MSC_VER >= 1900 // see https://docs.microsoft.com/en-us/cpp/c-runtime-library/daylight-dstbias-timezone-and-tzname?view=vs-2019 int64_t timezone = _timezone; int32_t daylight = _daylight; char** tzname = _tzname; - #endif +#endif int64_t t = (precision == TSDB_TIME_PRECISION_MILLI) ? MILLISECOND_PER_SECOND : MILLISECOND_PER_SECOND * 1000L; start += timezone * t; @@ -142,10 +140,10 @@ int32_t tNameExtractFullName(const SName* name, char* dst) { int32_t tNameLen(const SName* name) { assert(name != NULL); - char tmp[12] = {0}; + char tmp[12] = {0}; int32_t len = sprintf(tmp, "%d", name->acctId); - int32_t len1 = (int32_t) strlen(name->dbname); - int32_t len2 = (int32_t) strlen(name->tname); + int32_t len1 = (int32_t)strlen(name->dbname); + int32_t len2 = (int32_t)strlen(name->tname); if (name->type == TSDB_DB_NAME_T) { assert(len2 == 0); @@ -200,9 +198,7 @@ const char* tNameGetTableName(const SName* name) { return &name->tname[0]; } -void tNameAssign(SName* dst, const SName* src) { - memcpy(dst, src, sizeof(SName)); -} +void tNameAssign(SName* dst, const SName* src) { memcpy(dst, src, sizeof(SName)); } int32_t tNameSetDbName(SName* dst, int32_t acct, const char* dbName, size_t nameLen) { assert(dst != NULL && dbName != NULL && nameLen > 0); @@ -244,7 +240,6 @@ bool tNameDBNameEqual(SName* left, SName* right) { return (0 == strcmp(left->dbname, right->dbname)); } - int32_t tNameFromString(SName* dst, const char* str, uint32_t type) { assert(dst != NULL && str != NULL && strlen(str) > 0); @@ -260,14 +255,14 @@ int32_t tNameFromString(SName* dst, const char* str, uint32_t type) { if ((type & T_NAME_DB) == T_NAME_DB) { dst->type = TSDB_DB_NAME_T; - char* start = (char*)((p == NULL)? str:(p+1)); + char* start = (char*)((p == NULL) ? str : (p + 1)); int32_t len = 0; p = strstr(start, TS_PATH_DELIMITER); if (p == NULL) { - len = (int32_t) strlen(start); + len = (int32_t)strlen(start); } else { - len = (int32_t) (p - start); + len = (int32_t)(p - start); } // too long account id or too long db name @@ -275,21 +270,21 @@ int32_t tNameFromString(SName* dst, const char* str, uint32_t type) { return -1; } - memcpy (dst->dbname, start, len); + memcpy(dst->dbname, start, len); dst->dbname[len] = 0; } if ((type & T_NAME_TABLE) == T_NAME_TABLE) { dst->type = TSDB_TABLE_NAME_T; - char* start = (char*) ((p == NULL)? str: (p+1)); + char* start = (char*)((p == NULL) ? str : (p + 1)); // too long account id or too long db name - int32_t len = (int32_t) strlen(start); + int32_t len = (int32_t)strlen(start); if ((len >= tListLen(dst->tname)) || (len <= 0)) { return -1; } - memcpy (dst->tname, start, len); + memcpy(dst->tname, start, len); dst->tname[len] = 0; } @@ -305,14 +300,14 @@ static int compareKv(const void* p1, const void* p2) { if (res != 0) { return res; } else { - return kvLen1-kvLen2; + return kvLen1 - kvLen2; } } /* * use stable name and tags to grearate child table name */ -void buildChildTableName(RandTableName *rName) { +void buildChildTableName(RandTableName* rName) { int32_t size = taosArrayGetSize(rName->tags); ASSERT(size > 0); taosArraySort(rName->tags, compareKv); @@ -320,19 +315,19 @@ void buildChildTableName(RandTableName *rName) { SStringBuilder sb = {0}; taosStringBuilderAppendStringLen(&sb, rName->sTableName, rName->sTableNameLen); for (int j = 0; j < size; ++j) { - SSmlKv *tagKv = taosArrayGetP(rName->tags, j); + SSmlKv* tagKv = taosArrayGetP(rName->tags, j); taosStringBuilderAppendStringLen(&sb, tagKv->key, tagKv->keyLen); taosStringBuilderAppendStringLen(&sb, tagKv->value, tagKv->valueLen); } - size_t len = 0; - char* keyJoined = taosStringBuilderGetResult(&sb, &len); + size_t len = 0; + char* keyJoined = taosStringBuilderGetResult(&sb, &len); T_MD5_CTX context; tMD5Init(&context); - tMD5Update(&context, (uint8_t *)keyJoined, (uint32_t)len); + tMD5Update(&context, (uint8_t*)keyJoined, (uint32_t)len); tMD5Final(&context); uint64_t digest1 = *(uint64_t*)(context.digest); uint64_t digest2 = *(uint64_t*)(context.digest + 8); - snprintf(rName->childTableName, TSDB_TABLE_NAME_LEN, "t_%016"PRIx64"%016"PRIx64, digest1, digest2); + snprintf(rName->childTableName, TSDB_TABLE_NAME_LEN, "t_%016" PRIx64 "%016" PRIx64, digest1, digest2); taosStringBuilderDestroy(&sb); rName->uid = digest1; } diff --git a/source/dnode/mnode/impl/src/mndDef.c b/source/dnode/mnode/impl/src/mndDef.c index a2c628b8a1..8225eca659 100644 --- a/source/dnode/mnode/impl/src/mndDef.c +++ b/source/dnode/mnode/impl/src/mndDef.c @@ -196,7 +196,9 @@ SMqVgEp *tCloneSMqVgEp(const SMqVgEp *pVgEp) { return pVgEpNew; } -void tDeleteSMqVgEp(SMqVgEp *pVgEp) { taosMemoryFree(pVgEp->qmsg); } +void tDeleteSMqVgEp(SMqVgEp *pVgEp) { + if (pVgEp->qmsg) taosMemoryFree(pVgEp->qmsg); +} int32_t tEncodeSMqVgEp(void **buf, const SMqVgEp *pVgEp) { int32_t tlen = 0; diff --git a/source/dnode/mnode/impl/src/mndTopic.c b/source/dnode/mnode/impl/src/mndTopic.c index 01149f793f..b62de0e06e 100644 --- a/source/dnode/mnode/impl/src/mndTopic.c +++ b/source/dnode/mnode/impl/src/mndTopic.c @@ -298,6 +298,8 @@ static int32_t mndCreateTopic(SMnode *pMnode, SNodeMsg *pReq, SCMCreateTopicReq SNode *pAst = NULL; if (nodesStringToNode(pCreate->ast, &pAst) != 0) { + taosMemoryFree(topicObj.ast); + taosMemoryFree(topicObj.sql); mError("topic:%s, failed to create since %s", pCreate->name, terrstr()); return -1; } @@ -307,16 +309,22 @@ static int32_t mndCreateTopic(SMnode *pMnode, SNodeMsg *pReq, SCMCreateTopicReq SPlanContext cxt = {.pAstRoot = pAst, .topicQuery = true}; if (qCreateQueryPlan(&cxt, &pPlan, NULL) != 0) { mError("topic:%s, failed to create since %s", pCreate->name, terrstr()); + taosMemoryFree(topicObj.ast); + taosMemoryFree(topicObj.sql); return -1; } if (qExtractResultSchema(pAst, &topicObj.schema.nCols, &topicObj.schema.pSchema) != 0) { mError("topic:%s, failed to create since %s", pCreate->name, terrstr()); + taosMemoryFree(topicObj.ast); + taosMemoryFree(topicObj.sql); return -1; } if (nodesNodeToString(pPlan, false, &topicObj.physicalPlan, NULL) != 0) { mError("topic:%s, failed to create since %s", pCreate->name, terrstr()); + taosMemoryFree(topicObj.ast); + taosMemoryFree(topicObj.sql); return -1; } } else { @@ -331,6 +339,8 @@ static int32_t mndCreateTopic(SMnode *pMnode, SNodeMsg *pReq, SCMCreateTopicReq STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_TYPE_CREATE_TOPIC, &pReq->rpcMsg); if (pTrans == NULL) { mError("topic:%s, failed to create since %s", pCreate->name, terrstr()); + taosMemoryFreeClear(topicObj.ast); + taosMemoryFreeClear(topicObj.sql); taosMemoryFreeClear(topicObj.physicalPlan); return -1; } diff --git a/source/dnode/vnode/src/tq/tq.c b/source/dnode/vnode/src/tq/tq.c index b8d6e84b1c..48a0f6ad61 100644 --- a/source/dnode/vnode/src/tq/tq.c +++ b/source/dnode/vnode/src/tq/tq.c @@ -233,17 +233,19 @@ int32_t tqPushMsgNew(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_ int tqPushMsg(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t ver) { if (msgType != TDMT_VND_SUBMIT) return 0; + // make sure msgType == TDMT_VND_SUBMIT + if (tsdbUpdateSmaWindow(pTq->pVnode->pTsdb, msg, ver) != 0) { + return -1; + } + + if (taosHashGetSize(pTq->pStreamTasks) == 0) return 0; + void* data = taosMemoryMalloc(msgLen); if (data == NULL) { return -1; } memcpy(data, msg, msgLen); - // make sure msgType == TDMT_VND_SUBMIT - if (tsdbUpdateSmaWindow(pTq->pVnode->pTsdb, msg, ver) != 0) { - return -1; - } - SRpcMsg req = { .msgType = TDMT_VND_STREAM_TRIGGER, .pCont = data,