From 82ad9a911b92bf1458deec0340b38e1fc2dd7b14 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 9 Nov 2021 15:33:07 +0800 Subject: [PATCH] transaction object --- include/dnode/mnode/transaction/trn.h | 13 ++-- source/dnode/mnode/transaction/inc/trnInt.h | 9 +++ source/dnode/mnode/transaction/src/trn.c | 75 +++++++++++++++++++-- 3 files changed, 84 insertions(+), 13 deletions(-) diff --git a/include/dnode/mnode/transaction/trn.h b/include/dnode/mnode/transaction/trn.h index 3b7e3a5f1f..62aa326682 100644 --- a/include/dnode/mnode/transaction/trn.h +++ b/include/dnode/mnode/transaction/trn.h @@ -23,8 +23,7 @@ extern "C" { #endif -typedef struct { -} STrans; +typedef struct STrans STrans; int32_t trnInit(); void trnCleanup(); @@ -33,11 +32,11 @@ 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); +int32_t trnAppendRedoLog(STrans *, SSdbRawData *); +int32_t trnAppendUndoLog(STrans *, SSdbRawData *); +int32_t trnAppendCommitLog(STrans *, SSdbRawData *); +int32_t trnAppendRedoAction(STrans *, SEpSet *, void *pMsg); +int32_t trnAppendUndoAction(STrans *, SEpSet *, void *pMsg); #ifdef __cplusplus } diff --git a/source/dnode/mnode/transaction/inc/trnInt.h b/source/dnode/mnode/transaction/inc/trnInt.h index f39411c768..c5d57d1020 100644 --- a/source/dnode/mnode/transaction/inc/trnInt.h +++ b/source/dnode/mnode/transaction/inc/trnInt.h @@ -19,6 +19,7 @@ #include "os.h" #include "trn.h" #include "tglobal.h" +#include "tarray.h" #include "tlog.h" #ifdef __cplusplus @@ -32,6 +33,14 @@ extern "C" { #define mDebug(...) { if (mDebugFlag & DEBUG_DEBUG) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }} #define mTrace(...) { if (mDebugFlag & DEBUG_TRACE) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }} +typedef struct STrans { + SArray *redoLogs; + SArray *undoLogs; + SArray *commitLogs; + SArray *redoActions; + SArray *undoActions; +} STrans; + #ifdef __cplusplus } #endif diff --git a/source/dnode/mnode/transaction/src/trn.c b/source/dnode/mnode/transaction/src/trn.c index 083f24dc48..850880d16d 100644 --- a/source/dnode/mnode/transaction/src/trn.c +++ b/source/dnode/mnode/transaction/src/trn.c @@ -16,15 +16,78 @@ #define _DEFAULT_SOURCE #include "trnInt.h" +#define TRN_DEFAULT_ARRAY_SIZE 8 + int32_t trnInit() { return 0; } void trnCleanup(); -STrans *trnCreate() { return NULL; } +STrans *trnCreate() { + STrans *pTrans = calloc(1, sizeof(STrans)); + if (pTrans == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + pTrans->redoLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *)); + pTrans->undoLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *)); + pTrans->commitLogs = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *)); + pTrans->redoActions = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *)); + pTrans->undoActions = taosArrayInit(TRN_DEFAULT_ARRAY_SIZE, sizeof(void *)); + + if (pTrans->redoLogs == NULL || pTrans->undoLogs == NULL || pTrans->commitLogs == NULL || + pTrans->redoActions == NULL || pTrans->undoActions == NULL) { + taosArrayDestroy(pTrans->redoLogs); + taosArrayDestroy(pTrans->undoLogs); + taosArrayDestroy(pTrans->commitLogs); + taosArrayDestroy(pTrans->redoActions); + taosArrayDestroy(pTrans->undoActions); + free(pTrans); + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + return pTrans; +} + 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) {} +int32_t trnAppendRedoLog(STrans *pTrans, SSdbRawData *pRaw) { + void *ptr = taosArrayPush(pTrans->redoLogs, &pRaw); + if (ptr == NULL) { + return TSDB_CODE_MND_OUT_OF_MEMORY; + } + return 0; +} + +int32_t trnAppendUndoLog(STrans *pTrans, SSdbRawData *pRaw) { + void *ptr = taosArrayPush(pTrans->undoLogs, &pRaw); + if (ptr == NULL) { + return TSDB_CODE_MND_OUT_OF_MEMORY; + } + return 0; +} + +int32_t trnAppendCommitLog(STrans *pTrans, SSdbRawData *pRaw) { + void *ptr = taosArrayPush(pTrans->commitLogs, &pRaw); + if (ptr == NULL) { + return TSDB_CODE_MND_OUT_OF_MEMORY; + } + return 0; +} + +int32_t trnAppendRedoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) { + void *ptr = taosArrayPush(pTrans->redoActions, &pMsg); + if (ptr == NULL) { + return TSDB_CODE_MND_OUT_OF_MEMORY; + } + return 0; +} + +int32_t trnAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) { + void *ptr = taosArrayPush(pTrans->undoActions, &pMsg); + if (ptr == NULL) { + return TSDB_CODE_MND_OUT_OF_MEMORY; + } + return 0; +}