From 622cdc3907162ed7da57aac5d1d6775f6c03aa90 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 9 Nov 2021 14:27:17 +0800 Subject: [PATCH] add transaction interface --- include/common/taosmsg.h | 2 - include/dnode/mnode/sdb/sdb.h | 4 +- include/dnode/mnode/transaction/trn.h | 46 +++++++++ source/dnode/mnode/impl/CMakeLists.txt | 1 + source/dnode/mnode/impl/inc/mnodeDef.h | 2 +- source/dnode/mnode/impl/inc/mnodeInt.h | 2 + source/dnode/mnode/impl/src/mnodeUser.c | 96 ++++++++++++++++++- source/dnode/mnode/sdb/CMakeLists.txt | 1 - source/dnode/mnode/sdb/src/sdb.c | 2 +- source/dnode/mnode/transaction/CMakeLists.txt | 7 +- source/dnode/mnode/transaction/inc/trnInt.h | 39 ++++++++ source/dnode/mnode/transaction/src/trn.c | 30 ++++++ 12 files changed, 221 insertions(+), 11 deletions(-) create mode 100644 source/dnode/mnode/transaction/inc/trnInt.h diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index 135a79a48e..55378ceeda 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -399,8 +399,6 @@ typedef struct { typedef struct { char user[TSDB_USER_LEN]; char pass[TSDB_KEY_LEN]; - int8_t privilege; - int8_t flag; } SCreateUserMsg, SAlterUserMsg; typedef struct { diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h index 111509677d..f302a21c7a 100644 --- a/include/dnode/mnode/sdb/sdb.h +++ b/include/dnode/mnode/sdb/sdb.h @@ -93,7 +93,7 @@ typedef enum { typedef enum { SDB_ACTION_INSERT = 1, SDB_ACTION_UPDATE = 2, SDB_ACTION_DELETE = 3 } ESdbAction; typedef enum { SDB_KEY_BINARY = 1, SDB_KEY_INT32 = 2, SDB_KEY_INT64 = 3 } EKeyType; -typedef enum { SDB_STATUS_CREATING = 1, SDB_STATUS_READY, SDB_STATUS_DROPPING, SDB_STATUS_DROPPED } ESdbStatus; +typedef enum { SDB_STATUS_CREATING = 1, SDB_STATUS_READY = 2, SDB_STATUS_DROPPING = 3 } ESdbStatus; typedef struct { int8_t type; @@ -136,7 +136,7 @@ int32_t sdbDeploy(); void sdbUnDeploy(); void *sdbAcquire(ESdbType sdb, void *pKey); -void sdbRelease(ESdbType sdb, void *pObj); +void sdbRelease(void *pObj); void *sdbFetch(ESdbType sdb, void *pIter); void sdbCancelFetch(ESdbType sdb, void *pIter); int32_t sdbGetSize(ESdbType sdb); diff --git a/include/dnode/mnode/transaction/trn.h b/include/dnode/mnode/transaction/trn.h index e69de29bb2..3b7e3a5f1f 100644 --- a/include/dnode/mnode/transaction/trn.h +++ b/include/dnode/mnode/transaction/trn.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_TRANSACTION_H_ +#define _TD_TRANSACTION_H_ + +#include "sdb.h" +#include "taosmsg.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { +} STrans; + +int32_t trnInit(); +void trnCleanup(); + +STrans *trnCreate(); +int32_t trnCommit(STrans *); +void trnDrop(STrans *); + +void trnAppendRedoLog(STrans *, SSdbRawData *); +void trnAppendUndoLog(STrans *, SSdbRawData *); +void trnAppendCommitLog(STrans *, SSdbRawData *); +void trnAppendRedoAction(STrans *, SEpSet *, void *pMsg); +void trnAppendUndoAction(STrans *, SEpSet *, void *pMsg); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_TRANSACTION_H_*/ diff --git a/source/dnode/mnode/impl/CMakeLists.txt b/source/dnode/mnode/impl/CMakeLists.txt index 49c9d54513..4c9d44b39e 100644 --- a/source/dnode/mnode/impl/CMakeLists.txt +++ b/source/dnode/mnode/impl/CMakeLists.txt @@ -8,6 +8,7 @@ target_include_directories( target_link_libraries( mnode PRIVATE sdb + PRIVATE transaction PUBLIC transport PUBLIC cjson ) \ No newline at end of file diff --git a/source/dnode/mnode/impl/inc/mnodeDef.h b/source/dnode/mnode/impl/inc/mnodeDef.h index 973e90656f..49c74283ae 100644 --- a/source/dnode/mnode/impl/inc/mnodeDef.h +++ b/source/dnode/mnode/impl/inc/mnodeDef.h @@ -24,7 +24,6 @@ #include "thash.h" #include "cJSON.h" #include "mnode.h" -#include "sdb.h" #ifdef __cplusplus extern "C" { @@ -268,6 +267,7 @@ typedef struct { typedef struct SMnMsg { void (*fp)(SMnMsg *pMsg, int32_t code); + char user[TSDB_USER_LEN]; SUserObj *pUser; int16_t received; int16_t successed; diff --git a/source/dnode/mnode/impl/inc/mnodeInt.h b/source/dnode/mnode/impl/inc/mnodeInt.h index 77ed79f82c..11f8f461ba 100644 --- a/source/dnode/mnode/impl/inc/mnodeInt.h +++ b/source/dnode/mnode/impl/inc/mnodeInt.h @@ -17,6 +17,8 @@ #define _TD_MNODE_INT_H_ #include "mnodeDef.h" +#include "sdb.h" +#include "trn.h" #ifdef __cplusplus extern "C" { diff --git a/source/dnode/mnode/impl/src/mnodeUser.c b/source/dnode/mnode/impl/src/mnodeUser.c index a77419db51..cc321f0ef0 100644 --- a/source/dnode/mnode/impl/src/mnodeUser.c +++ b/source/dnode/mnode/impl/src/mnodeUser.c @@ -95,7 +95,7 @@ static int32_t mnodeUserActionDelete(SUserObj *pUser) { } if (pUser->acct != NULL) { - sdbRelease(SDB_ACCT, pUser->pAcct); + sdbRelease(pUser->pAcct); pUser->pAcct = NULL; } @@ -115,7 +115,7 @@ static int32_t mnodeCreateDefaultUser(char *acct, char *user, char *pass) { tstrncpy(userObj.acct, acct, TSDB_USER_LEN); taosEncryptPass((uint8_t *)pass, strlen(pass), userObj.pass); userObj.createdTime = taosGetTimestampMs(); - userObj.updateTime = taosGetTimestampMs(); + userObj.updateTime = userObj.createdTime; if (strcmp(user, TSDB_DEFAULT_USER) == 0) { userObj.rootAuth = 1; @@ -144,6 +144,98 @@ static int32_t mnodeCreateDefaultUsers() { return code; } +static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnMsg *pMsg) { + int32_t code = 0; + STrans *pTrans = NULL; + SSdbRawData *pUndoRaw = NULL; + SSdbRawData *pRedoRaw = NULL; + + SUserObj userObj = {0}; + tstrncpy(userObj.user, user, TSDB_USER_LEN); + tstrncpy(userObj.acct, acct, TSDB_USER_LEN); + taosEncryptPass((uint8_t *)pass, strlen(pass), userObj.pass); + userObj.createdTime = taosGetTimestampMs(); + userObj.updateTime = userObj.createdTime; + userObj.rootAuth = 0; + + pRedoRaw = mnodeUserActionEncode(&userObj); + if (pRedoRaw == NULL) { + code = terrno; + goto CREATE_USER_OVER; + } + pRedoRaw->status = SDB_STATUS_READY; + pRedoRaw->action = SDB_ACTION_INSERT; + + pUndoRaw = mnodeUserActionEncode(&userObj); + if (pUndoRaw == NULL) { + code = terrno; + goto CREATE_USER_OVER; + } + pUndoRaw->status = SDB_STATUS_DROPPING; + pUndoRaw->action = SDB_ACTION_DELETE; + + pTrans = trnCreate(); + if (pTrans == NULL) { + code = TSDB_CODE_MND_OUT_OF_MEMORY; + goto CREATE_USER_OVER; + } + trnAppendRedoLog(pTrans, pRedoRaw); + trnAppendUndoLog(pTrans, pUndoRaw); + + code = trnCommit(pTrans); + +CREATE_USER_OVER: + if (code != 0) { + trnDrop(pTrans); + free(pRedoRaw); + free(pUndoRaw); + } + + return code; +} + +static int32_t mnodeProcessCreateUserMsg(SMnMsg *pMsg) { + SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont; + int32_t code = TSDB_CODE_SUCCESS; + + if (pCreate->user[0] == 0) { + code = TSDB_CODE_MND_INVALID_USER_FORMAT; + mError("user:%s, failed to create since %s", pCreate->user, tstrerror(code)); + return code; + } + + if (pCreate->pass[0] == 0) { + code = TSDB_CODE_MND_INVALID_PASS_FORMAT; + mError("user:%s, failed to create since %s", pCreate->user, tstrerror(code)); + return code; + } + + SUserObj *pUser = sdbAcquire(SDB_USER, pCreate->user); + if (pUser != NULL) { + sdbRelease(pUser); + code = TSDB_CODE_MND_USER_ALREADY_EXIST; + mError("user:%s, failed to create since %s", pCreate->user, tstrerror(code)); + return code; + } + + SUserObj *pOperUser = sdbAcquire(SDB_USER, pMsg->user); + if (pOperUser == NULL) { + code = TSDB_CODE_MND_NO_USER_FROM_CONN; + mError("user:%s, failed to create since %s", pCreate->user, tstrerror(code)); + return code; + } + + code = mnodeCreateUser(pOperUser->acct, pCreate->user, pCreate->pass, pMsg); + sdbRelease(pOperUser); + + if (code != 0) { + mError("user:%s, failed to create since %s", pCreate->user, tstrerror(code)); + return code; + } + + return TSDB_CODE_MND_ACTION_IN_PROGRESS; +} + int32_t mnodeInitUser() { SSdbDesc desc = {.sdbType = SDB_USER, .keyType = SDB_KEY_BINARY, diff --git a/source/dnode/mnode/sdb/CMakeLists.txt b/source/dnode/mnode/sdb/CMakeLists.txt index 9bbfff3138..c5a0b7299a 100644 --- a/source/dnode/mnode/sdb/CMakeLists.txt +++ b/source/dnode/mnode/sdb/CMakeLists.txt @@ -10,5 +10,4 @@ target_link_libraries( PRIVATE os PRIVATE common PRIVATE util - PRIVATE cjson ) \ No newline at end of file diff --git a/source/dnode/mnode/sdb/src/sdb.c b/source/dnode/mnode/sdb/src/sdb.c index 001b95c5c4..a47ed8ec20 100644 --- a/source/dnode/mnode/sdb/src/sdb.c +++ b/source/dnode/mnode/sdb/src/sdb.c @@ -377,7 +377,7 @@ void *sdbAcquire(ESdbType sdb, void *pKey) { } } -void sdbRelease(ESdbType sdb, void *pObj) { +void sdbRelease(void *pObj) { SSdbRow *pRow = (SSdbRow *)((char *)pObj - sizeof(SSdbRow)); atomic_sub_fetch_32(&pRow->refCount, 1); } diff --git a/source/dnode/mnode/transaction/CMakeLists.txt b/source/dnode/mnode/transaction/CMakeLists.txt index c598b424f2..6b01f5bd18 100644 --- a/source/dnode/mnode/transaction/CMakeLists.txt +++ b/source/dnode/mnode/transaction/CMakeLists.txt @@ -7,5 +7,8 @@ target_include_directories( ) target_link_libraries( transaction - PUBLIC transport -) \ No newline at end of file + PRIVATE os + PRIVATE common + PRIVATE util + PRIVATE sdb +) diff --git a/source/dnode/mnode/transaction/inc/trnInt.h b/source/dnode/mnode/transaction/inc/trnInt.h new file mode 100644 index 0000000000..f39411c768 --- /dev/null +++ b/source/dnode/mnode/transaction/inc/trnInt.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_TRANSACTION_INT_H_ +#define _TD_TRANSACTION_INT_H_ + +#include "os.h" +#include "trn.h" +#include "tglobal.h" +#include "tlog.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define mFatal(...) { if (mDebugFlag & DEBUG_FATAL) { taosPrintLog("MND FATAL ", 255, __VA_ARGS__); }} +#define mError(...) { if (mDebugFlag & DEBUG_ERROR) { taosPrintLog("MND ERROR ", 255, __VA_ARGS__); }} +#define mWarn(...) { if (mDebugFlag & DEBUG_WARN) { taosPrintLog("MND WARN ", 255, __VA_ARGS__); }} +#define mInfo(...) { if (mDebugFlag & DEBUG_INFO) { taosPrintLog("MND ", 255, __VA_ARGS__); }} +#define mDebug(...) { if (mDebugFlag & DEBUG_DEBUG) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }} +#define mTrace(...) { if (mDebugFlag & DEBUG_TRACE) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }} + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_TRANSACTION_INT_H_*/ diff --git a/source/dnode/mnode/transaction/src/trn.c b/source/dnode/mnode/transaction/src/trn.c index e69de29bb2..083f24dc48 100644 --- a/source/dnode/mnode/transaction/src/trn.c +++ b/source/dnode/mnode/transaction/src/trn.c @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#define _DEFAULT_SOURCE +#include "trnInt.h" + +int32_t trnInit() { return 0; } +void trnCleanup(); + +STrans *trnCreate() { return NULL; } +int32_t trnCommit(STrans *pTrans) { return 0; } +void trnDrop(STrans *pTrans) {} + +void trnAppendRedoLog(STrans *pTrans, SSdbRawData *pRaw) {} +void trnAppendUndoLog(STrans *pTrans, SSdbRawData *pRaw) {} +void trnAppendCommitLog(STrans *pTrans, SSdbRawData *pRaw) {} +void trnAppendRedoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) {} +void trnAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) {}