From 3536ead6ff9d5b9a106dbcb10ee7b7e0dd82dda2 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 5 Nov 2021 19:23:28 +0800 Subject: [PATCH 01/17] change source directory --- include/{server => dnode}/mnode/mnode.h | 0 .../dnode/mnode/sdb/sdb.h | 0 include/dnode/mnode/transaction/trn.h | 0 include/{server => dnode}/qnode/qnode.h | 0 include/{server => dnode}/vnode/meta/meta.h | 0 include/{server => dnode}/vnode/tq/tq.h | 0 .../{server => dnode}/vnode/tsdb/impl/tsdbImpl.h | 0 include/{server => dnode}/vnode/tsdb/tsdb.h | 0 include/{server => dnode}/vnode/vnode.h | 0 source/dnode/mgmt/CMakeLists.txt | 2 +- source/dnode/mnode/CMakeLists.txt | 15 +++------------ source/dnode/mnode/impl/CMakeLists.txt | 12 ++++++++++++ source/dnode/mnode/{ => impl}/inc/mnodeAcct.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeAuth.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeBalance.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeCluster.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeDb.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeDef.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeDnode.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeFunc.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeInt.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeMnode.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeOper.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeProfile.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeSdb.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeShow.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeStable.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeSync.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeTelem.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeUser.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeVgroup.h | 0 source/dnode/mnode/{ => impl}/inc/mnodeWorker.h | 0 source/dnode/mnode/{ => impl}/src/mnodeAcct.c | 0 source/dnode/mnode/{ => impl}/src/mnodeAuth.c | 0 source/dnode/mnode/{ => impl}/src/mnodeBalance.c | 0 source/dnode/mnode/{ => impl}/src/mnodeCluster.c | 0 source/dnode/mnode/{ => impl}/src/mnodeDb.c | 0 source/dnode/mnode/{ => impl}/src/mnodeDnode.c | 0 source/dnode/mnode/{ => impl}/src/mnodeFunc.c | 0 source/dnode/mnode/{ => impl}/src/mnodeMnode.c | 0 source/dnode/mnode/{ => impl}/src/mnodeOper.c | 0 source/dnode/mnode/{ => impl}/src/mnodeProfile.c | 0 source/dnode/mnode/{ => impl}/src/mnodeSdb.c | 0 source/dnode/mnode/{ => impl}/src/mnodeShow.c | 0 source/dnode/mnode/{ => impl}/src/mnodeStable.c | 0 source/dnode/mnode/{ => impl}/src/mnodeSync.c | 0 source/dnode/mnode/{ => impl}/src/mnodeTelem.c | 0 source/dnode/mnode/{ => impl}/src/mnodeUser.c | 0 source/dnode/mnode/{ => impl}/src/mnodeVgroup.c | 0 source/dnode/mnode/{ => impl}/src/mnodeWorker.c | 0 source/dnode/mnode/{ => impl}/src/mondeInt.c | 0 source/dnode/mnode/impl/test/mnodeTests.cpp | 0 source/dnode/mnode/sdb/CMakeLists.txt | 11 +++++++++++ source/dnode/mnode/sdb/src/sdb.c | 0 source/dnode/mnode/transaction/CMakeLists.txt | 11 +++++++++++ source/dnode/mnode/transaction/src/trn.c | 0 source/dnode/qnode/CMakeLists.txt | 2 +- source/dnode/vnode/impl/CMakeLists.txt | 2 +- source/dnode/vnode/meta/CMakeLists.txt | 2 +- source/dnode/vnode/meta/test/CMakeLists.txt | 2 +- source/dnode/vnode/tq/CMakeLists.txt | 2 +- source/dnode/vnode/tsdb/CMakeLists.txt | 2 +- 62 files changed, 44 insertions(+), 19 deletions(-) rename include/{server => dnode}/mnode/mnode.h (100%) rename source/dnode/mnode/test/mnodeTests.cpp => include/dnode/mnode/sdb/sdb.h (100%) create mode 100644 include/dnode/mnode/transaction/trn.h rename include/{server => dnode}/qnode/qnode.h (100%) rename include/{server => dnode}/vnode/meta/meta.h (100%) rename include/{server => dnode}/vnode/tq/tq.h (100%) rename include/{server => dnode}/vnode/tsdb/impl/tsdbImpl.h (100%) rename include/{server => dnode}/vnode/tsdb/tsdb.h (100%) rename include/{server => dnode}/vnode/vnode.h (100%) create mode 100644 source/dnode/mnode/impl/CMakeLists.txt rename source/dnode/mnode/{ => impl}/inc/mnodeAcct.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeAuth.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeBalance.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeCluster.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeDb.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeDef.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeDnode.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeFunc.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeInt.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeMnode.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeOper.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeProfile.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeSdb.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeShow.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeStable.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeSync.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeTelem.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeUser.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeVgroup.h (100%) rename source/dnode/mnode/{ => impl}/inc/mnodeWorker.h (100%) rename source/dnode/mnode/{ => impl}/src/mnodeAcct.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeAuth.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeBalance.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeCluster.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeDb.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeDnode.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeFunc.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeMnode.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeOper.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeProfile.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeSdb.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeShow.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeStable.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeSync.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeTelem.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeUser.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeVgroup.c (100%) rename source/dnode/mnode/{ => impl}/src/mnodeWorker.c (100%) rename source/dnode/mnode/{ => impl}/src/mondeInt.c (100%) create mode 100644 source/dnode/mnode/impl/test/mnodeTests.cpp create mode 100644 source/dnode/mnode/sdb/CMakeLists.txt create mode 100644 source/dnode/mnode/sdb/src/sdb.c create mode 100644 source/dnode/mnode/transaction/CMakeLists.txt create mode 100644 source/dnode/mnode/transaction/src/trn.c diff --git a/include/server/mnode/mnode.h b/include/dnode/mnode/mnode.h similarity index 100% rename from include/server/mnode/mnode.h rename to include/dnode/mnode/mnode.h diff --git a/source/dnode/mnode/test/mnodeTests.cpp b/include/dnode/mnode/sdb/sdb.h similarity index 100% rename from source/dnode/mnode/test/mnodeTests.cpp rename to include/dnode/mnode/sdb/sdb.h diff --git a/include/dnode/mnode/transaction/trn.h b/include/dnode/mnode/transaction/trn.h new file mode 100644 index 0000000000..e69de29bb2 diff --git a/include/server/qnode/qnode.h b/include/dnode/qnode/qnode.h similarity index 100% rename from include/server/qnode/qnode.h rename to include/dnode/qnode/qnode.h diff --git a/include/server/vnode/meta/meta.h b/include/dnode/vnode/meta/meta.h similarity index 100% rename from include/server/vnode/meta/meta.h rename to include/dnode/vnode/meta/meta.h diff --git a/include/server/vnode/tq/tq.h b/include/dnode/vnode/tq/tq.h similarity index 100% rename from include/server/vnode/tq/tq.h rename to include/dnode/vnode/tq/tq.h diff --git a/include/server/vnode/tsdb/impl/tsdbImpl.h b/include/dnode/vnode/tsdb/impl/tsdbImpl.h similarity index 100% rename from include/server/vnode/tsdb/impl/tsdbImpl.h rename to include/dnode/vnode/tsdb/impl/tsdbImpl.h diff --git a/include/server/vnode/tsdb/tsdb.h b/include/dnode/vnode/tsdb/tsdb.h similarity index 100% rename from include/server/vnode/tsdb/tsdb.h rename to include/dnode/vnode/tsdb/tsdb.h diff --git a/include/server/vnode/vnode.h b/include/dnode/vnode/vnode.h similarity index 100% rename from include/server/vnode/vnode.h rename to include/dnode/vnode/vnode.h diff --git a/source/dnode/mgmt/CMakeLists.txt b/source/dnode/mgmt/CMakeLists.txt index 74ee422bb0..194c317991 100644 --- a/source/dnode/mgmt/CMakeLists.txt +++ b/source/dnode/mgmt/CMakeLists.txt @@ -11,6 +11,6 @@ target_link_libraries( ) target_include_directories( taosd - PUBLIC "${CMAKE_SOURCE_DIR}/include/server/dnode" + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode" private "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) diff --git a/source/dnode/mnode/CMakeLists.txt b/source/dnode/mnode/CMakeLists.txt index bf35b381af..6de5f06476 100644 --- a/source/dnode/mnode/CMakeLists.txt +++ b/source/dnode/mnode/CMakeLists.txt @@ -1,12 +1,3 @@ -aux_source_directory(src MNODE_SRC) -add_library(mnode ${MNODE_SRC}) -target_include_directories( - mnode - PUBLIC "${CMAKE_SOURCE_DIR}/include/server/mnode" - private "${CMAKE_CURRENT_SOURCE_DIR}/inc" -) -target_link_libraries( - mnode - PUBLIC transport - PUBLIC cjson -) \ No newline at end of file +add_subdirectory(impl) +add_subdirectory(sdb) +add_subdirectory(transaction) diff --git a/source/dnode/mnode/impl/CMakeLists.txt b/source/dnode/mnode/impl/CMakeLists.txt new file mode 100644 index 0000000000..7c9e33c5b5 --- /dev/null +++ b/source/dnode/mnode/impl/CMakeLists.txt @@ -0,0 +1,12 @@ +aux_source_directory(src MNODE_SRC) +add_library(mnode ${MNODE_SRC}) +target_include_directories( + mnode + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/mnode" + private "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + mnode + PUBLIC transport + PUBLIC cjson +) \ No newline at end of file diff --git a/source/dnode/mnode/inc/mnodeAcct.h b/source/dnode/mnode/impl/inc/mnodeAcct.h similarity index 100% rename from source/dnode/mnode/inc/mnodeAcct.h rename to source/dnode/mnode/impl/inc/mnodeAcct.h diff --git a/source/dnode/mnode/inc/mnodeAuth.h b/source/dnode/mnode/impl/inc/mnodeAuth.h similarity index 100% rename from source/dnode/mnode/inc/mnodeAuth.h rename to source/dnode/mnode/impl/inc/mnodeAuth.h diff --git a/source/dnode/mnode/inc/mnodeBalance.h b/source/dnode/mnode/impl/inc/mnodeBalance.h similarity index 100% rename from source/dnode/mnode/inc/mnodeBalance.h rename to source/dnode/mnode/impl/inc/mnodeBalance.h diff --git a/source/dnode/mnode/inc/mnodeCluster.h b/source/dnode/mnode/impl/inc/mnodeCluster.h similarity index 100% rename from source/dnode/mnode/inc/mnodeCluster.h rename to source/dnode/mnode/impl/inc/mnodeCluster.h diff --git a/source/dnode/mnode/inc/mnodeDb.h b/source/dnode/mnode/impl/inc/mnodeDb.h similarity index 100% rename from source/dnode/mnode/inc/mnodeDb.h rename to source/dnode/mnode/impl/inc/mnodeDb.h diff --git a/source/dnode/mnode/inc/mnodeDef.h b/source/dnode/mnode/impl/inc/mnodeDef.h similarity index 100% rename from source/dnode/mnode/inc/mnodeDef.h rename to source/dnode/mnode/impl/inc/mnodeDef.h diff --git a/source/dnode/mnode/inc/mnodeDnode.h b/source/dnode/mnode/impl/inc/mnodeDnode.h similarity index 100% rename from source/dnode/mnode/inc/mnodeDnode.h rename to source/dnode/mnode/impl/inc/mnodeDnode.h diff --git a/source/dnode/mnode/inc/mnodeFunc.h b/source/dnode/mnode/impl/inc/mnodeFunc.h similarity index 100% rename from source/dnode/mnode/inc/mnodeFunc.h rename to source/dnode/mnode/impl/inc/mnodeFunc.h diff --git a/source/dnode/mnode/inc/mnodeInt.h b/source/dnode/mnode/impl/inc/mnodeInt.h similarity index 100% rename from source/dnode/mnode/inc/mnodeInt.h rename to source/dnode/mnode/impl/inc/mnodeInt.h diff --git a/source/dnode/mnode/inc/mnodeMnode.h b/source/dnode/mnode/impl/inc/mnodeMnode.h similarity index 100% rename from source/dnode/mnode/inc/mnodeMnode.h rename to source/dnode/mnode/impl/inc/mnodeMnode.h diff --git a/source/dnode/mnode/inc/mnodeOper.h b/source/dnode/mnode/impl/inc/mnodeOper.h similarity index 100% rename from source/dnode/mnode/inc/mnodeOper.h rename to source/dnode/mnode/impl/inc/mnodeOper.h diff --git a/source/dnode/mnode/inc/mnodeProfile.h b/source/dnode/mnode/impl/inc/mnodeProfile.h similarity index 100% rename from source/dnode/mnode/inc/mnodeProfile.h rename to source/dnode/mnode/impl/inc/mnodeProfile.h diff --git a/source/dnode/mnode/inc/mnodeSdb.h b/source/dnode/mnode/impl/inc/mnodeSdb.h similarity index 100% rename from source/dnode/mnode/inc/mnodeSdb.h rename to source/dnode/mnode/impl/inc/mnodeSdb.h diff --git a/source/dnode/mnode/inc/mnodeShow.h b/source/dnode/mnode/impl/inc/mnodeShow.h similarity index 100% rename from source/dnode/mnode/inc/mnodeShow.h rename to source/dnode/mnode/impl/inc/mnodeShow.h diff --git a/source/dnode/mnode/inc/mnodeStable.h b/source/dnode/mnode/impl/inc/mnodeStable.h similarity index 100% rename from source/dnode/mnode/inc/mnodeStable.h rename to source/dnode/mnode/impl/inc/mnodeStable.h diff --git a/source/dnode/mnode/inc/mnodeSync.h b/source/dnode/mnode/impl/inc/mnodeSync.h similarity index 100% rename from source/dnode/mnode/inc/mnodeSync.h rename to source/dnode/mnode/impl/inc/mnodeSync.h diff --git a/source/dnode/mnode/inc/mnodeTelem.h b/source/dnode/mnode/impl/inc/mnodeTelem.h similarity index 100% rename from source/dnode/mnode/inc/mnodeTelem.h rename to source/dnode/mnode/impl/inc/mnodeTelem.h diff --git a/source/dnode/mnode/inc/mnodeUser.h b/source/dnode/mnode/impl/inc/mnodeUser.h similarity index 100% rename from source/dnode/mnode/inc/mnodeUser.h rename to source/dnode/mnode/impl/inc/mnodeUser.h diff --git a/source/dnode/mnode/inc/mnodeVgroup.h b/source/dnode/mnode/impl/inc/mnodeVgroup.h similarity index 100% rename from source/dnode/mnode/inc/mnodeVgroup.h rename to source/dnode/mnode/impl/inc/mnodeVgroup.h diff --git a/source/dnode/mnode/inc/mnodeWorker.h b/source/dnode/mnode/impl/inc/mnodeWorker.h similarity index 100% rename from source/dnode/mnode/inc/mnodeWorker.h rename to source/dnode/mnode/impl/inc/mnodeWorker.h diff --git a/source/dnode/mnode/src/mnodeAcct.c b/source/dnode/mnode/impl/src/mnodeAcct.c similarity index 100% rename from source/dnode/mnode/src/mnodeAcct.c rename to source/dnode/mnode/impl/src/mnodeAcct.c diff --git a/source/dnode/mnode/src/mnodeAuth.c b/source/dnode/mnode/impl/src/mnodeAuth.c similarity index 100% rename from source/dnode/mnode/src/mnodeAuth.c rename to source/dnode/mnode/impl/src/mnodeAuth.c diff --git a/source/dnode/mnode/src/mnodeBalance.c b/source/dnode/mnode/impl/src/mnodeBalance.c similarity index 100% rename from source/dnode/mnode/src/mnodeBalance.c rename to source/dnode/mnode/impl/src/mnodeBalance.c diff --git a/source/dnode/mnode/src/mnodeCluster.c b/source/dnode/mnode/impl/src/mnodeCluster.c similarity index 100% rename from source/dnode/mnode/src/mnodeCluster.c rename to source/dnode/mnode/impl/src/mnodeCluster.c diff --git a/source/dnode/mnode/src/mnodeDb.c b/source/dnode/mnode/impl/src/mnodeDb.c similarity index 100% rename from source/dnode/mnode/src/mnodeDb.c rename to source/dnode/mnode/impl/src/mnodeDb.c diff --git a/source/dnode/mnode/src/mnodeDnode.c b/source/dnode/mnode/impl/src/mnodeDnode.c similarity index 100% rename from source/dnode/mnode/src/mnodeDnode.c rename to source/dnode/mnode/impl/src/mnodeDnode.c diff --git a/source/dnode/mnode/src/mnodeFunc.c b/source/dnode/mnode/impl/src/mnodeFunc.c similarity index 100% rename from source/dnode/mnode/src/mnodeFunc.c rename to source/dnode/mnode/impl/src/mnodeFunc.c diff --git a/source/dnode/mnode/src/mnodeMnode.c b/source/dnode/mnode/impl/src/mnodeMnode.c similarity index 100% rename from source/dnode/mnode/src/mnodeMnode.c rename to source/dnode/mnode/impl/src/mnodeMnode.c diff --git a/source/dnode/mnode/src/mnodeOper.c b/source/dnode/mnode/impl/src/mnodeOper.c similarity index 100% rename from source/dnode/mnode/src/mnodeOper.c rename to source/dnode/mnode/impl/src/mnodeOper.c diff --git a/source/dnode/mnode/src/mnodeProfile.c b/source/dnode/mnode/impl/src/mnodeProfile.c similarity index 100% rename from source/dnode/mnode/src/mnodeProfile.c rename to source/dnode/mnode/impl/src/mnodeProfile.c diff --git a/source/dnode/mnode/src/mnodeSdb.c b/source/dnode/mnode/impl/src/mnodeSdb.c similarity index 100% rename from source/dnode/mnode/src/mnodeSdb.c rename to source/dnode/mnode/impl/src/mnodeSdb.c diff --git a/source/dnode/mnode/src/mnodeShow.c b/source/dnode/mnode/impl/src/mnodeShow.c similarity index 100% rename from source/dnode/mnode/src/mnodeShow.c rename to source/dnode/mnode/impl/src/mnodeShow.c diff --git a/source/dnode/mnode/src/mnodeStable.c b/source/dnode/mnode/impl/src/mnodeStable.c similarity index 100% rename from source/dnode/mnode/src/mnodeStable.c rename to source/dnode/mnode/impl/src/mnodeStable.c diff --git a/source/dnode/mnode/src/mnodeSync.c b/source/dnode/mnode/impl/src/mnodeSync.c similarity index 100% rename from source/dnode/mnode/src/mnodeSync.c rename to source/dnode/mnode/impl/src/mnodeSync.c diff --git a/source/dnode/mnode/src/mnodeTelem.c b/source/dnode/mnode/impl/src/mnodeTelem.c similarity index 100% rename from source/dnode/mnode/src/mnodeTelem.c rename to source/dnode/mnode/impl/src/mnodeTelem.c diff --git a/source/dnode/mnode/src/mnodeUser.c b/source/dnode/mnode/impl/src/mnodeUser.c similarity index 100% rename from source/dnode/mnode/src/mnodeUser.c rename to source/dnode/mnode/impl/src/mnodeUser.c diff --git a/source/dnode/mnode/src/mnodeVgroup.c b/source/dnode/mnode/impl/src/mnodeVgroup.c similarity index 100% rename from source/dnode/mnode/src/mnodeVgroup.c rename to source/dnode/mnode/impl/src/mnodeVgroup.c diff --git a/source/dnode/mnode/src/mnodeWorker.c b/source/dnode/mnode/impl/src/mnodeWorker.c similarity index 100% rename from source/dnode/mnode/src/mnodeWorker.c rename to source/dnode/mnode/impl/src/mnodeWorker.c diff --git a/source/dnode/mnode/src/mondeInt.c b/source/dnode/mnode/impl/src/mondeInt.c similarity index 100% rename from source/dnode/mnode/src/mondeInt.c rename to source/dnode/mnode/impl/src/mondeInt.c diff --git a/source/dnode/mnode/impl/test/mnodeTests.cpp b/source/dnode/mnode/impl/test/mnodeTests.cpp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/source/dnode/mnode/sdb/CMakeLists.txt b/source/dnode/mnode/sdb/CMakeLists.txt new file mode 100644 index 0000000000..a441e38118 --- /dev/null +++ b/source/dnode/mnode/sdb/CMakeLists.txt @@ -0,0 +1,11 @@ +aux_source_directory(src MNODE_SRC) +add_library(sdb ${MNODE_SRC}) +target_include_directories( + sdb + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/mnode/sdb/inc" + private "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + sdb + PUBLIC transport +) \ No newline at end of file diff --git a/source/dnode/mnode/sdb/src/sdb.c b/source/dnode/mnode/sdb/src/sdb.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/source/dnode/mnode/transaction/CMakeLists.txt b/source/dnode/mnode/transaction/CMakeLists.txt new file mode 100644 index 0000000000..59be0528e4 --- /dev/null +++ b/source/dnode/mnode/transaction/CMakeLists.txt @@ -0,0 +1,11 @@ +aux_source_directory(src MNODE_SRC) +add_library(transaction ${MNODE_SRC}) +target_include_directories( + transaction + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/mnode/transaction/inc" + private "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + transaction + PUBLIC transport +) \ No newline at end of file diff --git a/source/dnode/mnode/transaction/src/trn.c b/source/dnode/mnode/transaction/src/trn.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/source/dnode/qnode/CMakeLists.txt b/source/dnode/qnode/CMakeLists.txt index 4d4e764d41..d9f932a631 100644 --- a/source/dnode/qnode/CMakeLists.txt +++ b/source/dnode/qnode/CMakeLists.txt @@ -2,6 +2,6 @@ aux_source_directory(src QNODE_SRC) add_library(qnode ${QNODE_SRC}) target_include_directories( qnode - PUBLIC "${CMAKE_SOURCE_DIR}/include/server/qnode" + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/qnode" private "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) \ No newline at end of file diff --git a/source/dnode/vnode/impl/CMakeLists.txt b/source/dnode/vnode/impl/CMakeLists.txt index 81744df79c..75e6e822bb 100644 --- a/source/dnode/vnode/impl/CMakeLists.txt +++ b/source/dnode/vnode/impl/CMakeLists.txt @@ -2,7 +2,7 @@ aux_source_directory(src VNODE_SRC) add_library(vnode STATIC ${VNODE_SRC}) target_include_directories( vnode - PUBLIC "${CMAKE_SOURCE_DIR}/include/server/vnode" + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/vnode" private "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( diff --git a/source/dnode/vnode/meta/CMakeLists.txt b/source/dnode/vnode/meta/CMakeLists.txt index 0de78074ee..8d37f5842e 100644 --- a/source/dnode/vnode/meta/CMakeLists.txt +++ b/source/dnode/vnode/meta/CMakeLists.txt @@ -2,7 +2,7 @@ aux_source_directory(src META_SRC) add_library(meta STATIC ${META_SRC}) target_include_directories( meta - PUBLIC "${CMAKE_SOURCE_DIR}/include/server/vnode/meta" + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/vnode/meta" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( diff --git a/source/dnode/vnode/meta/test/CMakeLists.txt b/source/dnode/vnode/meta/test/CMakeLists.txt index b37ba6abd4..095a78383c 100644 --- a/source/dnode/vnode/meta/test/CMakeLists.txt +++ b/source/dnode/vnode/meta/test/CMakeLists.txt @@ -7,7 +7,7 @@ target_sources(metaTest ) target_include_directories(metaTest PUBLIC - "${CMAKE_SOURCE_DIR}/include/server/vnode/meta" + "${CMAKE_SOURCE_DIR}/include/dnode/vnode/meta" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_link_libraries(metaTest diff --git a/source/dnode/vnode/tq/CMakeLists.txt b/source/dnode/vnode/tq/CMakeLists.txt index 441fe46244..205082ed2d 100644 --- a/source/dnode/vnode/tq/CMakeLists.txt +++ b/source/dnode/vnode/tq/CMakeLists.txt @@ -2,7 +2,7 @@ aux_source_directory(src TQ_SRC) add_library(tq ${TQ_SRC}) target_include_directories( tq - PUBLIC "${CMAKE_SOURCE_DIR}/include/server/vnode/tq" + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/vnode/tq" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) diff --git a/source/dnode/vnode/tsdb/CMakeLists.txt b/source/dnode/vnode/tsdb/CMakeLists.txt index 339971431b..870d75cd20 100644 --- a/source/dnode/vnode/tsdb/CMakeLists.txt +++ b/source/dnode/vnode/tsdb/CMakeLists.txt @@ -2,7 +2,7 @@ aux_source_directory(src TSDB_SRC) add_library(tsdb ${TSDB_SRC}) target_include_directories( tsdb - PUBLIC "${CMAKE_SOURCE_DIR}/include/server/vnode/tsdb" + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/vnode/tsdb" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( From 9b790c7b3a165bb9a5c752e351d5cdabe2e0437e Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 5 Nov 2021 19:26:12 +0800 Subject: [PATCH 02/17] minor changes --- include/dnode/mnode/sdb/sdb.h | 0 source/dnode/mnode/sdb/src/sdb.c | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 include/dnode/mnode/sdb/sdb.h delete mode 100644 source/dnode/mnode/sdb/src/sdb.c diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/source/dnode/mnode/sdb/src/sdb.c b/source/dnode/mnode/sdb/src/sdb.c deleted file mode 100644 index e69de29bb2..0000000000 From 826e3c587a483731b6d9fdbcdb0a52887f4c0a8e Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 5 Nov 2021 19:26:47 +0800 Subject: [PATCH 03/17] minor changes --- .../mnode/impl/inc/mnodeSdb.h => include/dnode/mnode/sdb/sdb.h | 0 source/dnode/mnode/{impl/src/mnodeSdb.c => sdb/src/sdb.c} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename source/dnode/mnode/impl/inc/mnodeSdb.h => include/dnode/mnode/sdb/sdb.h (100%) rename source/dnode/mnode/{impl/src/mnodeSdb.c => sdb/src/sdb.c} (100%) diff --git a/source/dnode/mnode/impl/inc/mnodeSdb.h b/include/dnode/mnode/sdb/sdb.h similarity index 100% rename from source/dnode/mnode/impl/inc/mnodeSdb.h rename to include/dnode/mnode/sdb/sdb.h diff --git a/source/dnode/mnode/impl/src/mnodeSdb.c b/source/dnode/mnode/sdb/src/sdb.c similarity index 100% rename from source/dnode/mnode/impl/src/mnodeSdb.c rename to source/dnode/mnode/sdb/src/sdb.c From 7444bf5098bcda9e01d9db35e40bd77a7af43bec Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 5 Nov 2021 19:35:21 +0800 Subject: [PATCH 04/17] minor changes --- include/dnode/mnode/sdb/sdb.h | 30 ++++++++++++++++++- source/dnode/mnode/impl/CMakeLists.txt | 1 + source/dnode/mnode/impl/inc/mnodeDef.h | 27 +---------------- source/dnode/mnode/impl/src/mnodeAcct.c | 2 +- source/dnode/mnode/impl/src/mnodeUser.c | 2 +- source/dnode/mnode/impl/src/mnodeWorker.c | 2 +- source/dnode/mnode/impl/src/mondeInt.c | 2 +- source/dnode/mnode/sdb/CMakeLists.txt | 7 +++-- source/dnode/mnode/sdb/src/sdb.c | 16 +++++++++- source/dnode/mnode/transaction/CMakeLists.txt | 2 +- 10 files changed, 56 insertions(+), 35 deletions(-) diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h index cba9538ac2..678052e615 100644 --- a/include/dnode/mnode/sdb/sdb.h +++ b/include/dnode/mnode/sdb/sdb.h @@ -16,12 +16,40 @@ #ifndef _TD_MNODE_SDB_H_ #define _TD_MNODE_SDB_H_ -#include "mnodeInt.h" +#include "cJSON.h" #ifdef __cplusplus extern "C" { #endif +typedef enum { + MN_SDB_START = 0, + MN_SDB_CLUSTER = 1, + MN_SDB_DNODE = 2, + MN_SDB_MNODE = 3, + MN_SDB_ACCT = 4, + MN_SDB_AUTH = 5, + MN_SDB_USER = 6, + MN_SDB_DB = 7, + MN_SDB_VGROUP = 8, + MN_SDB_STABLE = 9, + MN_SDB_FUNC = 10, + MN_SDB_OPER = 11, + MN_SDB_MAX = 12 +} EMnSdb; + +typedef enum { MN_OP_START = 0, MN_OP_INSERT = 1, MN_OP_UPDATE = 2, MN_OP_DELETE = 3, MN_OP_MAX = 4 } EMnOp; + +typedef enum { MN_KEY_START = 0, MN_KEY_BINARY = 1, MN_KEY_INT32 = 2, MN_KEY_INT64 = 3, MN_KEY_MAX } EMnKey; + +typedef enum { MN_SDB_STAT_AVAIL = 0, MN_SDB_STAT_DROPPED = 1 } EMnSdbStat; + +typedef struct { + int8_t type; + int8_t status; + int8_t align[6]; +} SdbHead; + typedef void (*SdbDeployFp)(); typedef void *(*SdbDecodeFp)(cJSON *root); typedef int32_t (*SdbEncodeFp)(void *pHead, char *buf, int32_t maxLen); diff --git a/source/dnode/mnode/impl/CMakeLists.txt b/source/dnode/mnode/impl/CMakeLists.txt index 7c9e33c5b5..49c9d54513 100644 --- a/source/dnode/mnode/impl/CMakeLists.txt +++ b/source/dnode/mnode/impl/CMakeLists.txt @@ -7,6 +7,7 @@ target_include_directories( ) target_link_libraries( mnode + PRIVATE sdb 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 0825815bc7..d3ddba332e 100644 --- a/source/dnode/mnode/impl/inc/mnodeDef.h +++ b/source/dnode/mnode/impl/inc/mnodeDef.h @@ -24,6 +24,7 @@ #include "thash.h" #include "cJSON.h" #include "mnode.h" +#include "sdb.h" #ifdef __cplusplus extern "C" { @@ -59,26 +60,6 @@ typedef struct SFuncObj SFuncObj; typedef struct SOperObj SOperObj; typedef struct SMnMsg SMnMsg; -typedef enum { - MN_SDB_START = 0, - MN_SDB_CLUSTER = 1, - MN_SDB_DNODE = 2, - MN_SDB_MNODE = 3, - MN_SDB_ACCT = 4, - MN_SDB_AUTH = 5, - MN_SDB_USER = 6, - MN_SDB_DB = 7, - MN_SDB_VGROUP = 8, - MN_SDB_STABLE = 9, - MN_SDB_FUNC = 10, - MN_SDB_OPER = 11, - MN_SDB_MAX = 12 -} EMnSdb; - -typedef enum { MN_OP_START = 0, MN_OP_INSERT = 1, MN_OP_UPDATE = 2, MN_OP_DELETE = 3, MN_OP_MAX = 4 } EMnOp; - -typedef enum { MN_KEY_START = 0, MN_KEY_BINARY = 1, MN_KEY_INT32 = 2, MN_KEY_INT64 = 3, MN_KEY_MAX } EMnKey; - typedef enum { MN_AUTH_ACCT_START = 0, MN_AUTH_ACCT_USER, @@ -97,13 +78,7 @@ typedef enum { MN_AUTH_MAX } EMnAuthOp; -typedef enum { MN_SDB_STAT_AVAIL = 0, MN_SDB_STAT_DROPPED = 1 } EMnSdbStat; -typedef struct { - int8_t type; - int8_t status; - int8_t align[6]; -} SdbHead; typedef struct SClusterObj { SdbHead head; diff --git a/source/dnode/mnode/impl/src/mnodeAcct.c b/source/dnode/mnode/impl/src/mnodeAcct.c index 39db95b6d1..ae9d666da4 100644 --- a/source/dnode/mnode/impl/src/mnodeAcct.c +++ b/source/dnode/mnode/impl/src/mnodeAcct.c @@ -15,7 +15,7 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "mnodeSdb.h" +#include "mnodeInt.h" static void mnodeCreateDefaultAcct() { int32_t code = TSDB_CODE_SUCCESS; diff --git a/source/dnode/mnode/impl/src/mnodeUser.c b/source/dnode/mnode/impl/src/mnodeUser.c index 381a16ae26..4a4c086422 100644 --- a/source/dnode/mnode/impl/src/mnodeUser.c +++ b/source/dnode/mnode/impl/src/mnodeUser.c @@ -17,7 +17,7 @@ #include "os.h" #include "tkey.h" #include "tglobal.h" -#include "mnodeSdb.h" +#include "mnodeInt.h" static int32_t mnodeCreateDefaultUser(char *acct, char *user, char *pass) { int32_t code = TSDB_CODE_SUCCESS; diff --git a/source/dnode/mnode/impl/src/mnodeWorker.c b/source/dnode/mnode/impl/src/mnodeWorker.c index bdf0e869fc..4b0676f756 100644 --- a/source/dnode/mnode/impl/src/mnodeWorker.c +++ b/source/dnode/mnode/impl/src/mnodeWorker.c @@ -18,7 +18,7 @@ #include "tworker.h" #include "tglobal.h" #include "mnodeMnode.h" -#include "mnodeSdb.h" +#include "mnodeInt.h" #include "mnodeShow.h" #include "mnodeSync.h" #include "mnodeWorker.h" diff --git a/source/dnode/mnode/impl/src/mondeInt.c b/source/dnode/mnode/impl/src/mondeInt.c index eed710a1bd..d92c52e6c8 100644 --- a/source/dnode/mnode/impl/src/mondeInt.c +++ b/source/dnode/mnode/impl/src/mondeInt.c @@ -27,7 +27,7 @@ #include "mnodeMnode.h" #include "mnodeOper.h" #include "mnodeProfile.h" -#include "mnodeSdb.h" +#include "mnodeInt.h" #include "mnodeShow.h" #include "mnodeStable.h" #include "mnodeSync.h" diff --git a/source/dnode/mnode/sdb/CMakeLists.txt b/source/dnode/mnode/sdb/CMakeLists.txt index a441e38118..9bbfff3138 100644 --- a/source/dnode/mnode/sdb/CMakeLists.txt +++ b/source/dnode/mnode/sdb/CMakeLists.txt @@ -2,10 +2,13 @@ aux_source_directory(src MNODE_SRC) add_library(sdb ${MNODE_SRC}) target_include_directories( sdb - PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/mnode/sdb/inc" + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/mnode/sdb" private "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( sdb - PUBLIC transport + 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 f676729469..f92c487c7d 100644 --- a/source/dnode/mnode/sdb/src/sdb.c +++ b/source/dnode/mnode/sdb/src/sdb.c @@ -18,7 +18,21 @@ #include "thash.h" #include "tglobal.h" #include "cJSON.h" -#include "mnodeSdb.h" +#include "sdb.h" + +#include "os.h" +#include "taosmsg.h" +#include "tlog.h" +extern int32_t mDebugFlag; + +// mnode log function +#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__); }} + static struct { char currDir[PATH_MAX]; diff --git a/source/dnode/mnode/transaction/CMakeLists.txt b/source/dnode/mnode/transaction/CMakeLists.txt index 59be0528e4..c598b424f2 100644 --- a/source/dnode/mnode/transaction/CMakeLists.txt +++ b/source/dnode/mnode/transaction/CMakeLists.txt @@ -2,7 +2,7 @@ aux_source_directory(src MNODE_SRC) add_library(transaction ${MNODE_SRC}) target_include_directories( transaction - PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/mnode/transaction/inc" + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/mnode/transaction" private "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) target_link_libraries( From 6d6b55ea9340d536f88ac56d17599591f9de9cee Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 5 Nov 2021 19:42:45 +0800 Subject: [PATCH 05/17] minor changes --- include/dnode/mnode/sdb/sdb.h | 6 ++--- source/dnode/mnode/sdb/inc/sdbInt.h | 42 +++++++++++++++++++++++++++++ source/dnode/mnode/sdb/src/sdb.c | 19 +------------ 3 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 source/dnode/mnode/sdb/inc/sdbInt.h diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h index 678052e615..7a029b8f10 100644 --- a/include/dnode/mnode/sdb/sdb.h +++ b/include/dnode/mnode/sdb/sdb.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_MNODE_SDB_H_ -#define _TD_MNODE_SDB_H_ +#ifndef _TD_SDB_H_ +#define _TD_SDB_H_ #include "cJSON.h" @@ -77,4 +77,4 @@ void sdbSetFp(EMnSdb, EMnKey, SdbDeployFp, SdbEncodeFp, SdbDecodeFp, int32_t dat } #endif -#endif /*_TD_MNODE_INT_H_*/ +#endif /*_TD_SDB_H_*/ diff --git a/source/dnode/mnode/sdb/inc/sdbInt.h b/source/dnode/mnode/sdb/inc/sdbInt.h new file mode 100644 index 0000000000..1eafb0518b --- /dev/null +++ b/source/dnode/mnode/sdb/inc/sdbInt.h @@ -0,0 +1,42 @@ +/* + * 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_SDB_INT_H_ +#define _TD_SDB_INT_H_ + +#include "os.h" +#include "taosmsg.h" +#include "tlog.h" +#include "thash.h" +#include "tglobal.h" +#include "sdb.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// mnode log function +#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_SDB_INT_H_*/ diff --git a/source/dnode/mnode/sdb/src/sdb.c b/source/dnode/mnode/sdb/src/sdb.c index f92c487c7d..145895798e 100644 --- a/source/dnode/mnode/sdb/src/sdb.c +++ b/source/dnode/mnode/sdb/src/sdb.c @@ -14,25 +14,8 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "thash.h" -#include "tglobal.h" #include "cJSON.h" -#include "sdb.h" - -#include "os.h" -#include "taosmsg.h" -#include "tlog.h" -extern int32_t mDebugFlag; - -// mnode log function -#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__); }} - +#include "sdbInt.h" static struct { char currDir[PATH_MAX]; From 79519165a9f0305b0ade2bca76b3e2a87598b733 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 9 Nov 2021 11:37:58 +0800 Subject: [PATCH 06/17] add sdb interface --- include/dnode/mnode/sdb/sdb.h | 142 +++++-- include/util/taoserror.h | 19 +- source/dnode/mnode/impl/inc/mnodeDef.h | 14 +- source/dnode/mnode/impl/src/mnodeAcct.c | 204 +++++------ source/dnode/mnode/impl/src/mnodeUser.c | 195 +++++----- source/dnode/mnode/impl/src/mnodeWorker.c | 10 +- source/dnode/mnode/impl/src/mondeInt.c | 4 +- source/dnode/mnode/sdb/inc/sdbInt.h | 36 +- source/dnode/mnode/sdb/src/sdb.c | 428 +++++++++++----------- source/libs/transport/src/rpcMain.c | 2 +- source/util/src/terror.c | 19 +- 11 files changed, 598 insertions(+), 475 deletions(-) diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h index 7a029b8f10..111509677d 100644 --- a/include/dnode/mnode/sdb/sdb.h +++ b/include/dnode/mnode/sdb/sdb.h @@ -16,62 +16,130 @@ #ifndef _TD_SDB_H_ #define _TD_SDB_H_ -#include "cJSON.h" - #ifdef __cplusplus extern "C" { #endif +#define SDB_GET_BINARY_VAL(pData, dataLen, val, valLen, code) \ + { \ + if ((dataLen) >= (valLen)) { \ + memcpy((val), (char *)(pData), (valLen)); \ + (dataLen) -= (valLen); \ + (pData) = (char *)(pData) + (valLen); \ + } else { \ + code = TSDB_CODE_SDB_INVAID_RAW_DATA_LEN; \ + } \ + } + +#define SDB_GET_INT32_VAL(pData, dataLen, val, code) \ + { \ + if (dataLen >= sizeof(int32_t)) { \ + *(int32_t *)(pData) = (int32_t)(val); \ + (dataLen) -= sizeof(int32_t); \ + (pData) = (char *)(pData) + sizeof(int32_t); \ + } else { \ + code = TSDB_CODE_SDB_INVAID_RAW_DATA_LEN; \ + } \ + } + +#define SDB_GET_INT64_VAL(pData, dataLen, val, code) \ + { \ + if (dataLen >= sizeof(int64_t)) { \ + *(int64_t *)(pData) = (int64_t)(val); \ + (dataLen) -= sizeof(int64_t); \ + (pData) = (char *)(pData) + sizeof(int64_t); \ + } else { \ + code = TSDB_CODE_SDB_INVAID_RAW_DATA_LEN; \ + } \ + } + +#define SDB_SET_INT64_VAL(pData, dataLen, val) \ + { \ + *(int64_t *)(pData) = (int64_t)(val); \ + (dataLen) += sizeof(int64_t); \ + (pData) += sizeof(int64_t); \ + } + +#define SDB_SET_INT32_VAL(pData, dataLen, val) \ + { \ + *(int32_t *)(pData) = (int32_t)(val); \ + (dataLen) += sizeof(int32_t); \ + (pData) += sizeof(int32_t); \ + } + +#define SDB_SET_BINARY_VAL(pData, dataLen, val, valLen) \ + { \ + memcpy((char *)(pData), (val), (valLen)); \ + (dataLen) += (valLen); \ + (pData) += (valLen); \ + } + typedef enum { - MN_SDB_START = 0, - MN_SDB_CLUSTER = 1, - MN_SDB_DNODE = 2, - MN_SDB_MNODE = 3, - MN_SDB_ACCT = 4, - MN_SDB_AUTH = 5, - MN_SDB_USER = 6, - MN_SDB_DB = 7, - MN_SDB_VGROUP = 8, - MN_SDB_STABLE = 9, - MN_SDB_FUNC = 10, - MN_SDB_OPER = 11, - MN_SDB_MAX = 12 -} EMnSdb; + SDB_START = 0, + SDB_VERSION = 1, + SDB_CLUSTER = 2, + SDB_DNODE = 3, + SDB_MNODE = 4, + SDB_ACCT = 5, + SDB_AUTH = 6, + SDB_USER = 7, + SDB_DB = 8, + SDB_VGROUP = 9, + SDB_STABLE = 10, + SDB_FUNC = 11, + SDB_OPER = 12, + SDB_MAX = 13 +} ESdbType; -typedef enum { MN_OP_START = 0, MN_OP_INSERT = 1, MN_OP_UPDATE = 2, MN_OP_DELETE = 3, MN_OP_MAX = 4 } EMnOp; - -typedef enum { MN_KEY_START = 0, MN_KEY_BINARY = 1, MN_KEY_INT32 = 2, MN_KEY_INT64 = 3, MN_KEY_MAX } EMnKey; - -typedef enum { MN_SDB_STAT_AVAIL = 0, MN_SDB_STAT_DROPPED = 1 } EMnSdbStat; +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 struct { - int8_t type; - int8_t status; - int8_t align[6]; -} SdbHead; + int8_t type; + int8_t sver; + int8_t status; + int8_t action; + int8_t reserved[4]; + int32_t cksum; + int32_t dataLen; + char data[]; +} SSdbRawData; -typedef void (*SdbDeployFp)(); -typedef void *(*SdbDecodeFp)(cJSON *root); -typedef int32_t (*SdbEncodeFp)(void *pHead, char *buf, int32_t maxLen); +typedef int32_t (*SdbInsertFp)(void *pObj); +typedef int32_t (*SdbUpdateFp)(void *pSrcObj, void *pDstObj); +typedef int32_t (*SdbDeleteFp)(void *pObj); +typedef int32_t (*SdbDeployFp)(); +typedef void *(*SdbDecodeFp)(SSdbRawData *pRaw); +typedef SSdbRawData *(*SdbEncodeFp)(void *pObj); + +typedef struct { + ESdbType sdbType; + EKeyType keyType; + SdbDeployFp deployFp; + SdbEncodeFp encodeFp; + SdbDecodeFp decodeFp; + SdbInsertFp insertFp; + SdbUpdateFp updateFp; + SdbDeleteFp deleteFp; +} SSdbDesc; int32_t sdbInit(); void sdbCleanup(); +void sdbSetHandler(SSdbDesc desc); int32_t sdbRead(); +int32_t sdbWrite(SSdbRawData *pRawData); int32_t sdbCommit(); int32_t sdbDeploy(); void sdbUnDeploy(); -void *sdbInsertRow(EMnSdb sdb, void *pObj); -void sdbDeleteRow(EMnSdb sdb, void *pHead); -void *sdbUpdateRow(EMnSdb sdb, void *pHead); -void *sdbGetRow(EMnSdb sdb, void *pKey); -void *sdbFetchRow(EMnSdb sdb, void *pIter); -void sdbCancelFetch(EMnSdb sdb, void *pIter); -int32_t sdbGetCount(EMnSdb sdb); - -void sdbSetFp(EMnSdb, EMnKey, SdbDeployFp, SdbEncodeFp, SdbDecodeFp, int32_t dataSize); +void *sdbAcquire(ESdbType sdb, void *pKey); +void sdbRelease(ESdbType sdb, void *pObj); +void *sdbFetch(ESdbType sdb, void *pIter); +void sdbCancelFetch(ESdbType sdb, void *pIter); +int32_t sdbGetSize(ESdbType sdb); #ifdef __cplusplus } diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 6c46a4c89f..ba3dad63af 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -131,12 +131,15 @@ int32_t* taosGetErrno(); #define TSDB_CODE_MND_FAILED_TO_CREATE_DIR TAOS_DEF_ERROR_CODE(0, 0x0313) //"failed to create mnode dir") #define TSDB_CODE_MND_FAILED_TO_INIT_STEP TAOS_DEF_ERROR_CODE(0, 0x0314) //"failed to init components") -#define TSDB_CODE_MND_SDB_OBJ_ALREADY_THERE TAOS_DEF_ERROR_CODE(0, 0x0320) //"Object already there") -#define TSDB_CODE_MND_SDB_ERROR TAOS_DEF_ERROR_CODE(0, 0x0321) //"Unexpected generic error in sdb") -#define TSDB_CODE_MND_SDB_INVALID_TABLE_TYPE TAOS_DEF_ERROR_CODE(0, 0x0322) //"Invalid table type") -#define TSDB_CODE_MND_SDB_OBJ_NOT_THERE TAOS_DEF_ERROR_CODE(0, 0x0323) //"Object not there") -#define TSDB_CODE_MND_SDB_INVAID_META_ROW TAOS_DEF_ERROR_CODE(0, 0x0324) //"Invalid meta row") -#define TSDB_CODE_MND_SDB_INVAID_KEY_TYPE TAOS_DEF_ERROR_CODE(0, 0x0325) //"Invalid key type") +#define TSDB_CODE_SDB_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x0320) +#define TSDB_CODE_SDB_OUT_OF_MEMORY TAOS_DEF_ERROR_CODE(0, 0x0321) +#define TSDB_CODE_SDB_OBJ_ALREADY_THERE TAOS_DEF_ERROR_CODE(0, 0x0322) +#define TSDB_CODE_SDB_OBJ_NOT_THERE TAOS_DEF_ERROR_CODE(0, 0x0323) +#define TSDB_CODE_SDB_INVAID_RAW_DATA_VER TAOS_DEF_ERROR_CODE(0, 0x0324) +#define TSDB_CODE_SDB_INVAID_RAW_DATA_LEN TAOS_DEF_ERROR_CODE(0, 0x0325) +#define TSDB_CODE_SDB_INVALID_TABLE_TYPE TAOS_DEF_ERROR_CODE(0, 0x0326) +#define TSDB_CODE_SDB_INVAID_META_ROW TAOS_DEF_ERROR_CODE(0, 0x0327) +#define TSDB_CODE_SDB_INVAID_KEY_TYPE TAOS_DEF_ERROR_CODE(0, 0x0328) #define TSDB_CODE_MND_DNODE_ALREADY_EXIST TAOS_DEF_ERROR_CODE(0, 0x0330) //"DNode already exists") #define TSDB_CODE_MND_DNODE_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x0331) //"DNode does not exist") @@ -155,12 +158,12 @@ int32_t* taosGetErrno(); #define TSDB_CODE_MND_DNODE_EP_NOT_CONFIGURED TAOS_DEF_ERROR_CODE(0, 0x033E) //"Dnode Ep not configured") #define TSDB_CODE_MND_ACCT_ALREADY_EXIST TAOS_DEF_ERROR_CODE(0, 0x0340) //"Account already exists") -#define TSDB_CODE_MND_INVALID_ACCT TAOS_DEF_ERROR_CODE(0, 0x0341) //"Invalid account") +#define TSDB_CODE_MND_ACCT_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x0341) //"Invalid account") #define TSDB_CODE_MND_INVALID_ACCT_OPTION TAOS_DEF_ERROR_CODE(0, 0x0342) //"Invalid account options") #define TSDB_CODE_MND_ACCT_EXPIRED TAOS_DEF_ERROR_CODE(0, 0x0343) //"Account authorization has expired") #define TSDB_CODE_MND_USER_ALREADY_EXIST TAOS_DEF_ERROR_CODE(0, 0x0350) //"User already exists") -#define TSDB_CODE_MND_INVALID_USER TAOS_DEF_ERROR_CODE(0, 0x0351) //"Invalid user") +#define TSDB_CODE_MND_USER_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x0351) //"Invalid user") #define TSDB_CODE_MND_INVALID_USER_FORMAT TAOS_DEF_ERROR_CODE(0, 0x0352) //"Invalid user format") #define TSDB_CODE_MND_INVALID_PASS_FORMAT TAOS_DEF_ERROR_CODE(0, 0x0353) //"Invalid password format") #define TSDB_CODE_MND_NO_USER_FROM_CONN TAOS_DEF_ERROR_CODE(0, 0x0354) //"Can not get user from conn") diff --git a/source/dnode/mnode/impl/inc/mnodeDef.h b/source/dnode/mnode/impl/inc/mnodeDef.h index d3ddba332e..973e90656f 100644 --- a/source/dnode/mnode/impl/inc/mnodeDef.h +++ b/source/dnode/mnode/impl/inc/mnodeDef.h @@ -81,7 +81,6 @@ typedef enum { typedef struct SClusterObj { - SdbHead head; int64_t id; char uid[TSDB_CLUSTER_ID_LEN]; int64_t createdTime; @@ -89,7 +88,6 @@ typedef struct SClusterObj { } SClusterObj; typedef struct SDnodeObj { - SdbHead head; int32_t id; int32_t vnodes; int64_t createdTime; @@ -106,7 +104,6 @@ typedef struct SDnodeObj { } SDnodeObj; typedef struct SMnodeObj { - SdbHead head; int32_t id; int8_t status; int8_t role; @@ -122,8 +119,8 @@ typedef struct { int32_t maxDbs; int32_t maxTimeSeries; int32_t maxStreams; - int64_t maxStorage; // In unit of GB - int8_t accessState; // Configured only by command + int64_t maxStorage; // In unit of GB + int32_t accessState; // Configured only by command } SAcctCfg; typedef struct { @@ -136,18 +133,16 @@ typedef struct { } SAcctInfo; typedef struct SAcctObj { - SdbHead head; char acct[TSDB_USER_LEN]; int64_t createdTime; int64_t updateTime; int32_t acctId; - int8_t status; + int32_t status; SAcctCfg cfg; SAcctInfo info; } SAcctObj; typedef struct SUserObj { - SdbHead head; char user[TSDB_USER_LEN]; char pass[TSDB_KEY_LEN]; char acct[TSDB_USER_LEN]; @@ -182,7 +177,6 @@ typedef struct { } SDbCfg; typedef struct SDbObj { - SdbHead head; char name[TSDB_FULL_DB_NAME_LEN]; char acct[TSDB_USER_LEN]; int64_t createdTime; @@ -226,7 +220,6 @@ typedef struct SVgObj { } SVgObj; typedef struct SSTableObj { - SdbHead head; char tableId[TSDB_TABLE_NAME_LEN]; uint64_t uid; int64_t createdTime; @@ -237,7 +230,6 @@ typedef struct SSTableObj { } SSTableObj; typedef struct SFuncObj { - SdbHead head; char name[TSDB_FUNC_NAME_LEN]; char path[128]; int32_t contLen; diff --git a/source/dnode/mnode/impl/src/mnodeAcct.c b/source/dnode/mnode/impl/src/mnodeAcct.c index ae9d666da4..0811a64e92 100644 --- a/source/dnode/mnode/impl/src/mnodeAcct.c +++ b/source/dnode/mnode/impl/src/mnodeAcct.c @@ -14,133 +14,117 @@ */ #define _DEFAULT_SOURCE -#include "os.h" #include "mnodeInt.h" -static void mnodeCreateDefaultAcct() { - int32_t code = TSDB_CODE_SUCCESS; +#define ACCT_VER 1 + +static SSdbRawData *mnodeAcctActionEncode(SAcctObj *pAcct) { + SSdbRawData *pRaw = calloc(1, sizeof(SAcctObj) + sizeof(SSdbRawData)); + if (pRaw == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + int32_t dataLen = 0; + char *pData = pRaw->data; + SDB_SET_BINARY_VAL(pData, dataLen, pAcct->acct, TSDB_USER_LEN) + SDB_SET_INT64_VAL(pData, dataLen, pAcct->createdTime) + SDB_SET_INT64_VAL(pData, dataLen, pAcct->updateTime) + SDB_SET_INT32_VAL(pData, dataLen, pAcct->acctId) + SDB_SET_INT32_VAL(pData, dataLen, pAcct->status) + SDB_SET_INT32_VAL(pData, dataLen, pAcct->cfg.maxUsers) + SDB_SET_INT32_VAL(pData, dataLen, pAcct->cfg.maxDbs) + SDB_SET_INT32_VAL(pData, dataLen, pAcct->cfg.maxTimeSeries) + SDB_SET_INT32_VAL(pData, dataLen, pAcct->cfg.maxStreams) + SDB_SET_INT64_VAL(pData, dataLen, pAcct->cfg.maxStorage) + SDB_SET_INT32_VAL(pData, dataLen, pAcct->cfg.accessState) + + pRaw->dataLen = dataLen; + pRaw->type = SDB_ACCT; + pRaw->sver = ACCT_VER; + return pRaw; +} + +static SAcctObj *mnodeAcctActionDecode(SSdbRawData *pRaw) { + if (pRaw->sver != ACCT_VER) { + terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER; + return NULL; + } + + SAcctObj *pAcct = calloc(1, sizeof(SAcctObj)); + if (pAcct == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + int32_t code = 0; + int32_t dataLen = pRaw->dataLen; + char *pData = pRaw->data; + SDB_GET_BINARY_VAL(pData, dataLen, pAcct->acct, TSDB_USER_LEN, code) + SDB_GET_INT64_VAL(pData, dataLen, pAcct->createdTime, code) + SDB_GET_INT64_VAL(pData, dataLen, pAcct->updateTime, code) + SDB_GET_INT32_VAL(pData, dataLen, pAcct->acctId, code) + SDB_GET_INT32_VAL(pData, dataLen, pAcct->status, code) + SDB_GET_INT32_VAL(pData, dataLen, pAcct->cfg.maxUsers, code) + SDB_GET_INT32_VAL(pData, dataLen, pAcct->cfg.maxDbs, code) + SDB_GET_INT32_VAL(pData, dataLen, pAcct->cfg.maxTimeSeries, code) + SDB_GET_INT32_VAL(pData, dataLen, pAcct->cfg.maxStreams, code) + SDB_GET_INT64_VAL(pData, dataLen, pAcct->cfg.maxStorage, code) + SDB_GET_INT32_VAL(pData, dataLen, pAcct->cfg.accessState, code) + + if (code != 0) { + tfree(pAcct); + terrno = code; + return NULL; + } + + return pAcct; +} + +static int32_t mnodeAcctActionInsert(SAcctObj *pAcct) { return 0; } + +static int32_t mnodeAcctActionDelete(SAcctObj *pAcct) { return 0; } + +static int32_t mnodeAcctActionUpdate(SAcctObj *pSrcAcct, SAcctObj *pDstAcct) { + memcpy(pDstAcct, pSrcAcct, (int32_t)((char *)&pDstAcct->info - (char *)&pDstAcct)); + return 0; +} + +static int32_t mnodeCreateDefaultAcct() { + int32_t code = 0; SAcctObj acctObj = {0}; tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN); + acctObj.createdTime = taosGetTimestampMs(); + acctObj.updateTime = taosGetTimestampMs(); + acctObj.acctId = 1; acctObj.cfg = (SAcctCfg){.maxUsers = 128, .maxDbs = 128, .maxTimeSeries = INT32_MAX, .maxStreams = 1000, .maxStorage = INT64_MAX, .accessState = TSDB_VN_ALL_ACCCESS}; - acctObj.acctId = 1; - acctObj.createdTime = taosGetTimestampMs(); - acctObj.updateTime = taosGetTimestampMs(); - sdbInsertRow(MN_SDB_ACCT, &acctObj); -} - -int32_t mnodeEncodeAcct(SAcctObj *pAcct, char *buf, int32_t maxLen) { - int32_t len = 0; - - len += snprintf(buf + len, maxLen - len, "{\"type\":%d, ", MN_SDB_ACCT); - len += snprintf(buf + len, maxLen - len, "\"acct\":\"%s\", ", pAcct->acct); - len += snprintf(buf + len, maxLen - len, "\"acctId\":\"%d\", ", pAcct->acctId); - len += snprintf(buf + len, maxLen - len, "\"maxUsers\":\"%d\", ", pAcct->cfg.maxUsers); - len += snprintf(buf + len, maxLen - len, "\"maxDbs\":\"%d\", ", pAcct->cfg.maxDbs); - len += snprintf(buf + len, maxLen - len, "\"maxTimeSeries\":\"%d\", ", pAcct->cfg.maxTimeSeries); - len += snprintf(buf + len, maxLen - len, "\"maxStreams\":\"%d\", ", pAcct->cfg.maxStreams); - len += snprintf(buf + len, maxLen - len, "\"maxStorage\":\"%" PRIu64 "\", ", pAcct->cfg.maxStorage); - len += snprintf(buf + len, maxLen - len, "\"accessState\":\"%d\", ", pAcct->cfg.accessState); - len += snprintf(buf + len, maxLen - len, "\"createdTime\":\"%" PRIu64 "\", ", pAcct->createdTime); - len += snprintf(buf + len, maxLen - len, "\"updateTime\":\"%" PRIu64 "\"}\n", pAcct->updateTime); - - return len; -} - -SAcctObj *mnodeDecodeAcct(cJSON *root) { - int32_t code = -1; - SAcctObj *pAcct = calloc(1, sizeof(SAcctObj)); - - cJSON *acct = cJSON_GetObjectItem(root, "acct"); - if (!acct || acct->type != cJSON_String) { - mError("failed to parse acct since acct not found"); - goto DECODE_ACCT_OVER; + SSdbRawData *pRaw = mnodeAcctActionEncode(&acctObj); + if (pRaw != NULL) { + code = sdbWrite(pRaw); + } else { + code = terrno; } - tstrncpy(pAcct->acct, acct->valuestring, TSDB_USER_LEN); - cJSON *acctId = cJSON_GetObjectItem(root, "acctId"); - if (!acctId || acctId->type != cJSON_String) { - mError("acct:%s, failed to parse since acctId not found", pAcct->acct); - goto DECODE_ACCT_OVER; - } - pAcct->acctId = atol(acctId->valuestring); - - cJSON *maxUsers = cJSON_GetObjectItem(root, "maxUsers"); - if (!maxUsers || maxUsers->type != cJSON_String) { - mError("acct:%s, failed to parse since maxUsers not found", pAcct->acct); - goto DECODE_ACCT_OVER; - } - pAcct->cfg.maxUsers = atol(maxUsers->valuestring); - - cJSON *maxDbs = cJSON_GetObjectItem(root, "maxDbs"); - if (!maxDbs || maxDbs->type != cJSON_String) { - mError("acct:%s, failed to parse since maxDbs not found", pAcct->acct); - goto DECODE_ACCT_OVER; - } - pAcct->cfg.maxDbs = atol(maxDbs->valuestring); - - cJSON *maxTimeSeries = cJSON_GetObjectItem(root, "maxTimeSeries"); - if (!maxTimeSeries || maxTimeSeries->type != cJSON_String) { - mError("acct:%s, failed to parse since maxTimeSeries not found", pAcct->acct); - goto DECODE_ACCT_OVER; - } - pAcct->cfg.maxTimeSeries = atol(maxTimeSeries->valuestring); - - cJSON *maxStreams = cJSON_GetObjectItem(root, "maxStreams"); - if (!maxStreams || maxStreams->type != cJSON_String) { - mError("acct:%s, failed to parse since maxStreams not found", pAcct->acct); - goto DECODE_ACCT_OVER; - } - pAcct->cfg.maxStreams = atol(maxStreams->valuestring); - - cJSON *maxStorage = cJSON_GetObjectItem(root, "maxStorage"); - if (!maxStorage || maxStorage->type != cJSON_String) { - mError("acct:%s, failed to parse since maxStorage not found", pAcct->acct); - goto DECODE_ACCT_OVER; - } - pAcct->cfg.maxStorage = atoll(maxStorage->valuestring); - - cJSON *accessState = cJSON_GetObjectItem(root, "accessState"); - if (!accessState || accessState->type != cJSON_String) { - mError("acct:%s, failed to parse since accessState not found", pAcct->acct); - goto DECODE_ACCT_OVER; - } - pAcct->cfg.accessState = atol(accessState->valuestring); - - cJSON *createdTime = cJSON_GetObjectItem(root, "createdTime"); - if (!createdTime || createdTime->type != cJSON_String) { - mError("acct:%s, failed to parse since createdTime not found", pAcct->acct); - goto DECODE_ACCT_OVER; - } - pAcct->createdTime = atol(createdTime->valuestring); - - cJSON *updateTime = cJSON_GetObjectItem(root, "updateTime"); - if (!updateTime || updateTime->type != cJSON_String) { - mError("acct:%s, failed to parse since updateTime not found", pAcct->acct); - goto DECODE_ACCT_OVER; - } - pAcct->updateTime = atol(updateTime->valuestring); - - code = 0; - mTrace("acct:%s, parse success", pAcct->acct); - -DECODE_ACCT_OVER: - if (code != 0) { - free(pAcct); - pAcct = NULL; - } - return pAcct; + return code; } int32_t mnodeInitAcct() { - sdbSetFp(MN_SDB_ACCT, MN_KEY_BINARY, mnodeCreateDefaultAcct, (SdbEncodeFp)mnodeEncodeAcct, - (SdbDecodeFp)(mnodeDecodeAcct), sizeof(SAcctObj)); + SSdbDesc desc = {.sdbType = SDB_ACCT, + .keyType = SDB_KEY_BINARY, + .deployFp = (SdbDeployFp)mnodeCreateDefaultAcct, + .encodeFp = (SdbEncodeFp)mnodeAcctActionEncode, + .decodeFp = (SdbDecodeFp)mnodeAcctActionDecode, + .insertFp = (SdbInsertFp)mnodeAcctActionInsert, + .updateFp = (SdbUpdateFp)mnodeAcctActionUpdate, + .deleteFp = (SdbDeleteFp)mnodeAcctActionDelete}; + sdbSetHandler(desc); return 0; } diff --git a/source/dnode/mnode/impl/src/mnodeUser.c b/source/dnode/mnode/impl/src/mnodeUser.c index 4a4c086422..a77419db51 100644 --- a/source/dnode/mnode/impl/src/mnodeUser.c +++ b/source/dnode/mnode/impl/src/mnodeUser.c @@ -19,8 +19,96 @@ #include "tglobal.h" #include "mnodeInt.h" +#define USER_VER 1 + +static SSdbRawData *mnodeUserActionEncode(SUserObj *pUser) { + SSdbRawData *pRaw = calloc(1, sizeof(SUserObj) + sizeof(SSdbRawData)); + if (pRaw == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + int32_t dataLen = 0; + char *pData = pRaw->data; + SDB_SET_BINARY_VAL(pData, dataLen, pUser->user, TSDB_USER_LEN) + SDB_SET_BINARY_VAL(pData, dataLen, pUser->pass, TSDB_KEY_LEN) + SDB_SET_BINARY_VAL(pData, dataLen, pUser->acct, TSDB_KEY_LEN) + SDB_SET_INT64_VAL(pData, dataLen, pUser->createdTime) + SDB_SET_INT64_VAL(pData, dataLen, pUser->updateTime) + SDB_SET_INT32_VAL(pData, dataLen, pUser->rootAuth) + + pRaw->dataLen = dataLen; + pRaw->type = SDB_USER; + pRaw->sver = USER_VER; + return pRaw; +} + +static SUserObj *mnodeUserActionDecode(SSdbRawData *pRaw) { + if (pRaw->sver != USER_VER) { + terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER; + return NULL; + } + + SUserObj *pUser = calloc(1, sizeof(SUserObj)); + if (pUser == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + int32_t code = 0; + int32_t dataLen = pRaw->dataLen; + char *pData = pRaw->data; + SDB_GET_BINARY_VAL(pData, dataLen, pUser->user, TSDB_USER_LEN, code) + SDB_GET_BINARY_VAL(pData, dataLen, pUser->pass, TSDB_KEY_LEN, code) + SDB_GET_BINARY_VAL(pData, dataLen, pUser->acct, TSDB_USER_LEN, code) + SDB_GET_INT64_VAL(pData, dataLen, pUser->createdTime, code) + SDB_GET_INT64_VAL(pData, dataLen, pUser->updateTime, code) + SDB_GET_INT32_VAL(pData, dataLen, pUser->rootAuth, code) + + if (code != 0) { + tfree(pUser); + terrno = code; + return NULL; + } + + return pUser; +} + +static int32_t mnodeUserActionInsert(SUserObj *pUser) { + pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); + if (pUser->prohibitDbHash == NULL) { + return TSDB_CODE_MND_OUT_OF_MEMORY; + } + + pUser->pAcct = sdbAcquire(SDB_ACCT, pUser->acct); + if (pUser->pAcct == NULL) { + return TSDB_CODE_MND_ACCT_NOT_EXIST; + } + + return 0; +} + +static int32_t mnodeUserActionDelete(SUserObj *pUser) { + if (pUser->prohibitDbHash) { + taosHashCleanup(pUser->prohibitDbHash); + pUser->prohibitDbHash = NULL; + } + + if (pUser->acct != NULL) { + sdbRelease(SDB_ACCT, pUser->pAcct); + pUser->pAcct = NULL; + } + + return 0; +} + +static int32_t mnodeUserActionUpdate(SUserObj *pSrcUser, SUserObj *pDstUser) { + memcpy(pDstUser, pSrcUser, (int32_t)((char *)&pDstUser->prohibitDbHash - (char *)&pDstUser)); + return 0; +} + static int32_t mnodeCreateDefaultUser(char *acct, char *user, char *pass) { - int32_t code = TSDB_CODE_SUCCESS; + int32_t code = 0; SUserObj userObj = {0}; tstrncpy(userObj.user, user, TSDB_USER_LEN); @@ -33,97 +121,40 @@ static int32_t mnodeCreateDefaultUser(char *acct, char *user, char *pass) { userObj.rootAuth = 1; } - sdbInsertRow(MN_SDB_USER, &userObj); -} - -static void mnodeCreateDefaultUsers() { - mnodeCreateDefaultUser(TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS); - mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "monitor", tsInternalPass); - mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, tsInternalPass); -} - -int32_t mnodeEncodeUser(SUserObj *pUser, char *buf, int32_t maxLen) { - int32_t len = 0; - char *base64 = base64_encode((const unsigned char *)pUser->pass, TSDB_KEY_LEN); - - len += snprintf(buf + len, maxLen - len, "{\"type\":%d, ", MN_SDB_USER); - len += snprintf(buf + len, maxLen - len, "\"user\":\"%s\", ", pUser->user); - len += snprintf(buf + len, maxLen - len, "\"auth\":\"%24s\", ", base64); - len += snprintf(buf + len, maxLen - len, "\"acct\":\"%s\", ", pUser->acct); - len += snprintf(buf + len, maxLen - len, "\"createdTime\":\"%" PRIu64 "\", ", pUser->createdTime); - len += snprintf(buf + len, maxLen - len, "\"updateTime\":\"%" PRIu64 "\"}\n", pUser->updateTime); - - free(base64); - return len; -} - -SUserObj *mnodeDecodeUser(cJSON *root) { - int32_t code = -1; - SUserObj *pUser = calloc(1, sizeof(SUserObj)); - - cJSON *user = cJSON_GetObjectItem(root, "user"); - if (!user || user->type != cJSON_String) { - mError("failed to parse user since user not found"); - goto DECODE_USER_OVER; - } - tstrncpy(pUser->user, user->valuestring, TSDB_USER_LEN); - - if (strcmp(pUser->user, TSDB_DEFAULT_USER) == 0) { - pUser->rootAuth = 1; - } - - cJSON *pass = cJSON_GetObjectItem(root, "auth"); - if (!pass || pass->type != cJSON_String) { - mError("user:%s, failed to parse since auth not found", pUser->user); - goto DECODE_USER_OVER; - } - - int32_t outlen = 0; - char *base64 = (char *)base64_decode(pass->valuestring, strlen(pass->valuestring), &outlen); - if (outlen != TSDB_KEY_LEN) { - mError("user:%s, failed to parse since invalid auth format", pUser->user); - free(base64); - goto DECODE_USER_OVER; + SSdbRawData *pRaw = mnodeUserActionEncode(&userObj); + if (pRaw != NULL) { + code = sdbWrite(pRaw); } else { - memcpy(pUser->pass, base64, outlen); - free(base64); + code = terrno; } - cJSON *acct = cJSON_GetObjectItem(root, "acct"); - if (!acct || acct->type != cJSON_String) { - mError("user:%s, failed to parse since acct not found", pUser->user); - goto DECODE_USER_OVER; - } - tstrncpy(pUser->acct, acct->valuestring, TSDB_USER_LEN); + return code; +} - cJSON *createdTime = cJSON_GetObjectItem(root, "createdTime"); - if (!createdTime || createdTime->type != cJSON_String) { - mError("user:%s, failed to parse since createdTime not found", pUser->user); - goto DECODE_USER_OVER; - } - pUser->createdTime = atol(createdTime->valuestring); +static int32_t mnodeCreateDefaultUsers() { + int32_t code = mnodeCreateDefaultUser(TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS); + if (code != 0) return code; - cJSON *updateTime = cJSON_GetObjectItem(root, "updateTime"); - if (!updateTime || updateTime->type != cJSON_String) { - mError("user:%s, failed to parse since updateTime not found", pUser->user); - goto DECODE_USER_OVER; - } - pUser->updateTime = atol(updateTime->valuestring); + code = mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "monitor", tsInternalPass); + if (code != 0) return code; - code = 0; - mTrace("user:%s, parse success", pUser->user); + code = mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, tsInternalPass); + if (code != 0) return code; -DECODE_USER_OVER: - if (code != 0) { - free(pUser); - pUser = NULL; - } - return pUser; + return code; } int32_t mnodeInitUser() { - sdbSetFp(MN_SDB_USER, MN_KEY_BINARY, mnodeCreateDefaultUsers, (SdbEncodeFp)mnodeEncodeUser, - (SdbDecodeFp)(mnodeDecodeUser), sizeof(SUserObj)); + SSdbDesc desc = {.sdbType = SDB_USER, + .keyType = SDB_KEY_BINARY, + .deployFp = (SdbDeployFp)mnodeCreateDefaultUsers, + .encodeFp = (SdbEncodeFp)mnodeUserActionEncode, + .decodeFp = (SdbDecodeFp)mnodeUserActionDecode, + .insertFp = (SdbInsertFp)mnodeUserActionInsert, + .updateFp = (SdbUpdateFp)mnodeUserActionUpdate, + .deleteFp = (SdbDeleteFp)mnodeUserActionDelete}; + sdbSetHandler(desc); + return 0; } diff --git a/source/dnode/mnode/impl/src/mnodeWorker.c b/source/dnode/mnode/impl/src/mnodeWorker.c index 4b0676f756..cf2f415b4d 100644 --- a/source/dnode/mnode/impl/src/mnodeWorker.c +++ b/source/dnode/mnode/impl/src/mnodeWorker.c @@ -50,7 +50,7 @@ static SMnMsg *mnodeInitMsg2(SRpcMsg *pRpcMsg) { SRpcConnInfo connInfo = {0}; if (rpcGetConnInfo(pMsg->rpcMsg.handle, &connInfo) == 0) { - pMsg->pUser = sdbGetRow(MN_SDB_USER, connInfo.user); + pMsg->pUser = sdbAcquire(SDB_USER, connInfo.user); } if (pMsg->pUser == NULL) { @@ -77,7 +77,7 @@ static void mnodeDispatchToWriteQueue(SRpcMsg *pRpcMsg) { } else { SMnMsg *pMsg = mnodeInitMsg2(pRpcMsg); if (pMsg == NULL) { - SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_INVALID_USER}; + SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_USER_NOT_EXIST}; rpcSendResponse(&rpcRsp); } else { mTrace("msg:%p, app:%p type:%s is put into wqueue", pMsg, pMsg->rpcMsg.ahandle, taosMsg[pMsg->rpcMsg.msgType]); @@ -103,7 +103,7 @@ static void mnodeDispatchToReadQueue(SRpcMsg *pRpcMsg) { } else { SMnMsg *pMsg = mnodeInitMsg2(pRpcMsg); if (pMsg == NULL) { - SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_INVALID_USER}; + SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_USER_NOT_EXIST}; rpcSendResponse(&rpcRsp); } else { mTrace("msg:%p, app:%p type:%s is put into rqueue", pMsg, pMsg->rpcMsg.ahandle, taosMsg[pMsg->rpcMsg.msgType]); @@ -120,7 +120,7 @@ static void mnodeDispatchToPeerQueue(SRpcMsg *pRpcMsg) { } else { SMnMsg *pMsg = mnodeInitMsg2(pRpcMsg); if (pMsg == NULL) { - SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_INVALID_USER}; + SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_USER_NOT_EXIST}; rpcSendResponse(&rpcRsp); } else { mTrace("msg:%p, app:%p type:%s is put into peer req queue", pMsg, pMsg->rpcMsg.ahandle, @@ -135,7 +135,7 @@ static void mnodeDispatchToPeerQueue(SRpcMsg *pRpcMsg) { void mnodeDispatchToPeerRspQueue(SRpcMsg *pRpcMsg) { SMnMsg *pMsg = mnodeInitMsg2(pRpcMsg); if (pMsg == NULL) { - SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_INVALID_USER}; + SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_USER_NOT_EXIST}; rpcSendResponse(&rpcRsp); } else { mTrace("msg:%p, app:%p type:%s is put into peer rsp queue", pMsg, pMsg->rpcMsg.ahandle, diff --git a/source/dnode/mnode/impl/src/mondeInt.c b/source/dnode/mnode/impl/src/mondeInt.c index d92c52e6c8..a7c76360e2 100644 --- a/source/dnode/mnode/impl/src/mondeInt.c +++ b/source/dnode/mnode/impl/src/mondeInt.c @@ -161,7 +161,7 @@ int32_t mnodeDeploy(char *path, SMnodeCfg *pCfg) { if (code != 0) { mError("failed to deploy mnode since init step1 error"); tsMint.state = MN_STATUS_UNINIT; - return TSDB_CODE_MND_SDB_ERROR; + return TSDB_CODE_MND_APP_ERROR; } code = mnodeInitStep2(); @@ -169,7 +169,7 @@ int32_t mnodeDeploy(char *path, SMnodeCfg *pCfg) { mnodeCleanupStep1(); mError("failed to deploy mnode since init step2 error"); tsMint.state = MN_STATUS_UNINIT; - return TSDB_CODE_MND_SDB_ERROR; + return TSDB_CODE_MND_APP_ERROR; } mDebug("mnode is deployed and waiting for raft to confirm"); diff --git a/source/dnode/mnode/sdb/inc/sdbInt.h b/source/dnode/mnode/sdb/inc/sdbInt.h index 1eafb0518b..3493e97ef2 100644 --- a/source/dnode/mnode/sdb/inc/sdbInt.h +++ b/source/dnode/mnode/sdb/inc/sdbInt.h @@ -17,17 +17,17 @@ #define _TD_SDB_INT_H_ #include "os.h" -#include "taosmsg.h" -#include "tlog.h" -#include "thash.h" -#include "tglobal.h" #include "sdb.h" +#include "taosmsg.h" +#include "tglobal.h" +#include "thash.h" +#include "tlockfree.h" +#include "tlog.h" #ifdef __cplusplus extern "C" { #endif -// mnode log function #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__); }} @@ -35,6 +35,32 @@ extern "C" { #define mDebug(...) { if (mDebugFlag & DEBUG_DEBUG) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }} #define mTrace(...) { if (mDebugFlag & DEBUG_TRACE) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }} +#define SDB_MAX_SIZE (32*1024) + +typedef struct { + char *currDir; + char *syncDir; + char *tmpDir; + int64_t lastCommitVer; + int64_t curVer; + EKeyType keyTypes[SDB_MAX]; + SHashObj *hashObjs[SDB_MAX]; + SRWLatch locks[SDB_MAX]; + SdbInsertFp insertFps[SDB_MAX]; + SdbUpdateFp updateFps[SDB_MAX]; + SdbDeleteFp deleteFps[SDB_MAX]; + SdbDeployFp deployFps[SDB_MAX]; + SdbEncodeFp encodeFps[SDB_MAX]; + SdbDecodeFp decodeFps[SDB_MAX]; +} SSdbObj; + +typedef struct { + ESdbStatus status; + int32_t refCount; + int32_t dataLen; + char *data[]; +} SSdbRow; + #ifdef __cplusplus } #endif diff --git a/source/dnode/mnode/sdb/src/sdb.c b/source/dnode/mnode/sdb/src/sdb.c index 145895798e..001b95c5c4 100644 --- a/source/dnode/mnode/sdb/src/sdb.c +++ b/source/dnode/mnode/sdb/src/sdb.c @@ -14,21 +14,9 @@ */ #define _DEFAULT_SOURCE -#include "cJSON.h" #include "sdbInt.h" -static struct { - char currDir[PATH_MAX]; - char backDir[PATH_MAX]; - char tmpDir[PATH_MAX]; - int64_t version; - EMnKey hashKey[MN_SDB_MAX]; - int32_t dataSize[MN_SDB_MAX]; - SHashObj *hashObj[MN_SDB_MAX]; - SdbDeployFp deployFp[MN_SDB_MAX]; - SdbEncodeFp encodeFp[MN_SDB_MAX]; - SdbDecodeFp decodeFp[MN_SDB_MAX]; -} tsSdb = {0}; +static SSdbObj tsSdb = {0}; static int32_t sdbCreateDir() { if (!taosMkDir(tsSdb.currDir)) { @@ -36,8 +24,8 @@ static int32_t sdbCreateDir() { return TAOS_SYSTEM_ERROR(errno); } - if (!taosMkDir(tsSdb.backDir)) { - mError("failed to create dir:%s", tsSdb.backDir); + if (!taosMkDir(tsSdb.syncDir)) { + mError("failed to create dir:%s", tsSdb.syncDir); return -1; } @@ -50,8 +38,8 @@ static int32_t sdbCreateDir() { } static int32_t sdbRunDeployFp() { - for (int32_t i = MN_SDB_START; i < MN_SDB_MAX; ++i) { - SdbDeployFp fp = tsSdb.deployFp[i]; + for (int32_t i = SDB_START; i < SDB_MAX; ++i) { + SdbDeployFp fp = tsSdb.deployFps[i]; if (fp) { (*fp)(); } @@ -60,152 +48,147 @@ static int32_t sdbRunDeployFp() { return 0; } -static int32_t sdbReadVersion(cJSON *root) { - cJSON *ver = cJSON_GetObjectItem(root, "version"); - if (!ver || ver->type != cJSON_String) { - mError("failed to parse version since version not found"); - return -1; +static SHashObj *sdbGetHash(int32_t sdb) { + if (sdb >= SDB_MAX || sdb <= SDB_START) { + return NULL; } - tsSdb.version = (int64_t)atoll(ver->valuestring); - mTrace("parse version success, version:%" PRIu64, tsSdb.version); + SHashObj *hash = tsSdb.hashObjs[sdb]; + if (hash == NULL) { + return NULL; + } + + return hash; +} + +int32_t sdbWrite(SSdbRawData *pRaw) { + SHashObj *hash = sdbGetHash(pRaw->type); + switch (pRaw->action) { + case SDB_ACTION_INSERT: + break; + case SDB_ACTION_UPDATE: + break; + case SDB_ACTION_DELETE: + break; + + default: + break; + } return 0; } -static void sdbWriteVersion(FileFd fd) { - char content[128]; - int32_t len = - snprintf(content, sizeof(content), "{\"type\":0, \"version\":\"%" PRIu64 "\", \"updateTime\":\"%" PRIu64 "\"}\n", - tsSdb.version, taosGetTimestampMs()); - taosWriteFile(fd, content, len); -} +static int32_t sdbWriteVersion(FileFd fd) { return 0; } + +static int32_t sdbReadVersion(FileFd fd) { return 0; } static int32_t sdbReadDataFile() { - ssize_t _bytes = 0; - size_t len = 4096; - char *line = calloc(1, len); - int32_t code = -1; - FILE *fp = NULL; - cJSON *root = NULL; + int32_t code = 0; - char file[PATH_MAX + 20]; - snprintf(file, sizeof(file), "%ssdb.data", tsSdb.currDir); - fp = fopen(file, "r"); - if (!fp) { - mDebug("failed to open file:%s for read since %s", file, strerror(errno)); - goto PARSE_SDB_DATA_ERROR; + SSdbRawData *pRaw = malloc(SDB_MAX_SIZE); + if (pRaw == NULL) { + return TSDB_CODE_MND_OUT_OF_MEMORY; } - while (!feof(fp)) { - memset(line, 0, len); - _bytes = tgetline(&line, &len, fp); - if (_bytes < 0) { + char file[PATH_MAX] = {0}; + snprintf(file, sizeof(file), "%ssdb.data", tsSdb.currDir); + FileFd fd = taosOpenFileCreateWrite(file); + if (fd <= 0) { + code = TAOS_SYSTEM_ERROR(errno); + mError("failed to open file:%s for read since %s", file, tstrerror(code)); + return code; + } + + int64_t offset = 0; + while (1) { + int32_t ret = (int32_t)taosReadFile(fd, pRaw, sizeof(SSdbRawData)); + if (ret == 0) break; + + if (ret < 0) { + code = TAOS_SYSTEM_ERROR(errno); + mError("failed to read file:%s since %s", file, tstrerror(code)); break; } - line[len - 1] = 0; - if (len <= 10) continue; + if (ret < sizeof(SSdbRawData)) { + code = TSDB_CODE_SDB_INTERNAL_ERROR; + mError("failed to read file:%s since %s", file, tstrerror(code)); + break; + } - root = cJSON_Parse(line); - if (root == NULL) { - mError("failed to parse since invalid json format, %s", line); + code = sdbWrite(pRaw); + if (code != 0) { + mError("failed to read file:%s since %s", file, tstrerror(code)); goto PARSE_SDB_DATA_ERROR; } - - cJSON *type = cJSON_GetObjectItem(root, "type"); - if (!type || type->type != cJSON_Number) { - mError("failed to parse since invalid type not found, %s", line); - goto PARSE_SDB_DATA_ERROR; - } - - if (type->valueint >= MN_SDB_MAX || type->valueint < MN_SDB_START) { - mError("failed to parse since invalid type, %s", line); - goto PARSE_SDB_DATA_ERROR; - } - - if (type->valueint == MN_SDB_START) { - if (sdbReadVersion(root) != 0) { - mError("failed to parse version, %s", line); - goto PARSE_SDB_DATA_ERROR; - } - cJSON_Delete(root); - root = NULL; - continue; - } - - SdbDecodeFp decodeFp = tsSdb.decodeFp[type->valueint]; - SdbHead *pHead = (*decodeFp)(root); - if (pHead == NULL) { - mError("failed to parse since decode error, %s", line); - goto PARSE_SDB_DATA_ERROR; - } - - pHead->type = type->valueint; - pHead->status = MN_SDB_STAT_AVAIL; - - sdbInsertRow(pHead->type, pHead); - free(pHead); - cJSON_Delete(root); - root = NULL; } code = 0; PARSE_SDB_DATA_ERROR: - if (line) free(line); - if (fp) fclose(fp); - if (root) cJSON_Delete(root); - + taosCloseFile(fd); return code; } static int32_t sdbWriteDataFile() { - char file[PATH_MAX + 20] = {0}; - snprintf(file, sizeof(file), "%ssdb.data", tsSdb.currDir); - FileFd fd = taosOpenFileCreateWrite(file); + int32_t code = 0; + + char tmpfile[PATH_MAX] = {0}; + snprintf(tmpfile, sizeof(tmpfile), "%ssdb.data", tsSdb.tmpDir); + + FileFd fd = taosOpenFileCreateWrite(tmpfile); if (fd <= 0) { - mError("failed to open file:%s for write since %s", file, strerror(errno)); - return -1; - } - - int32_t len; - int32_t maxLen = 10240; - char *buf = malloc(maxLen); - - for (int32_t i = MN_SDB_START; i < MN_SDB_MAX; ++i) { - SHashObj *hash = tsSdb.hashObj[i]; - if (!hash) continue; - - SdbEncodeFp encodeFp = tsSdb.encodeFp[i]; - if (!encodeFp) continue; - - SdbHead *pHead = taosHashIterate(hash, NULL); - while (pHead != NULL) { - len = (*encodeFp)(pHead, buf, maxLen); - if (len >= 0) { - taosWriteFile(fd, buf, len); - } - - pHead = taosHashIterate(hash, pHead); - } - } - - sdbWriteVersion(fd); - taosFsyncFile(fd); - taosCloseFile(fd); - - mInfo("write file:%s successfully", file); - return 0; -} - -int32_t sdbCommit() { - int32_t code = sdbWriteDataFile(); - if (code != 0) { + code = TAOS_SYSTEM_ERROR(errno); + mError("failed to open file:%s for write since %s", tmpfile, tstrerror(code)); return code; } - return 0; + for (int32_t i = SDB_START; i < SDB_MAX; ++i) { + SHashObj *hash = tsSdb.hashObjs[i]; + if (!hash) continue; + + SdbEncodeFp encodeFp = tsSdb.encodeFps[i]; + if (!encodeFp) continue; + + SSdbRow *pRow = taosHashIterate(hash, NULL); + while (pRow != NULL) { + if (pRow->status == SDB_STATUS_READY) continue; + SSdbRawData *pRaw = (*encodeFp)(pRow->data); + if (pRaw != NULL) { + taosWriteFile(fd, pRaw, sizeof(SSdbRawData) + pRaw->dataLen); + } else { + taosHashCancelIterate(hash, pRow); + code = TSDB_CODE_SDB_INTERNAL_ERROR; + break; + } + + pRow = taosHashIterate(hash, pRow); + } + } + + if (code == 0) { + code = sdbWriteVersion(fd); + } + + taosCloseFile(fd); + + if (code == 0) { + code = taosFsyncFile(fd); + } + + if (code != 0) { + char curfile[PATH_MAX] = {0}; + snprintf(curfile, sizeof(curfile), "%ssdb.data", tsSdb.currDir); + code = taosRenameFile(tmpfile, curfile); + } + + if (code != 0) { + mError("failed to write sdb file since %s", tstrerror(code)); + } else { + mInfo("write sdb file successfully"); + } + + return code; } int32_t sdbRead() { @@ -218,6 +201,15 @@ int32_t sdbRead() { return -1; } +int32_t sdbCommit() { + int32_t code = sdbWriteDataFile(); + if (code != 0) { + return code; + } + + return 0; +} + int32_t sdbDeploy() { if (sdbCreateDir() != 0) { return -1; @@ -231,38 +223,32 @@ int32_t sdbDeploy() { return -1; } - // if (!taosMkDir()) - // if (pMinfos == NULL) { // first deploy - // tsMint.dnodeId = 1; - // bool getuid = taosGetSystemUid(tsMint.clusterId); - // if (!getuid) { - // strcpy(tsMint.clusterId, "tdengine3.0"); - // mError("deploy new mnode but failed to get uid, set to default val %s", tsMint.clusterId); - // } else { - // mDebug("deploy new mnode and uid is %s", tsMint.clusterId); - // } - // } else { // todo - // } - - // if (mkdir(tsMnodeDir, 0755) != 0 && errno != EEXIST) { - // mError("failed to init mnode dir:%s, reason:%s", tsMnodeDir, strerror(errno)); - // return -1; - // } return 0; } void sdbUnDeploy() {} int32_t sdbInit() { - snprintf(tsSdb.currDir, PATH_MAX, "%s%scurrent%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP); - snprintf(tsSdb.backDir, PATH_MAX, "%s%sbackup%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP); - snprintf(tsSdb.tmpDir, PATH_MAX, "%s%stmp%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP); + char path[PATH_MAX + 100]; - for (int32_t i = 0; i < MN_SDB_MAX; ++i) { + snprintf(path, PATH_MAX + 100, "%s%scurrent%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP); + tsSdb.currDir = strdup(path); + + snprintf(path, PATH_MAX + 100, "%s%ssync%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP); + tsSdb.syncDir = strdup(path); + + snprintf(path, PATH_MAX + 100, "%s%stmp%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP); + tsSdb.tmpDir = strdup(path); + + if (tsSdb.currDir == NULL || tsSdb.currDir == NULL || tsSdb.currDir == NULL) { + return TSDB_CODE_MND_OUT_OF_MEMORY; + } + + for (int32_t i = 0; i < SDB_MAX; ++i) { int32_t type; - if (tsSdb.hashKey[i] == MN_KEY_INT32) { + if (tsSdb.keyTypes[i] == SDB_KEY_INT32) { type = TSDB_DATA_TYPE_INT; - } else if (tsSdb.hashKey[i] == MN_KEY_INT64) { + } else if (tsSdb.keyTypes[i] == SDB_KEY_INT64) { type = TSDB_DATA_TYPE_BIGINT; } else { type = TSDB_DATA_TYPE_BINARY; @@ -270,65 +256,72 @@ int32_t sdbInit() { SHashObj *hash = taosHashInit(128, taosGetDefaultHashFunction(type), true, HASH_NO_LOCK); if (hash == NULL) { - return -1; + return TSDB_CODE_MND_OUT_OF_MEMORY; } - tsSdb.hashObj[i] = hash; + tsSdb.hashObjs[i] = hash; + taosInitRWLatch(&tsSdb.locks[i]); } return 0; } void sdbCleanup() { - for (int32_t i = 0; i < MN_SDB_MAX; ++i) { - SHashObj *hash = tsSdb.hashObj[i]; + if (tsSdb.curVer != tsSdb.lastCommitVer) { + sdbCommit(); + } + + if (tsSdb.currDir != NULL) { + tfree(tsSdb.currDir); + } + + if (tsSdb.syncDir != NULL) { + tfree(tsSdb.syncDir); + } + + if (tsSdb.tmpDir != NULL) { + tfree(tsSdb.tmpDir); + } + + for (int32_t i = 0; i < SDB_MAX; ++i) { + SHashObj *hash = tsSdb.hashObjs[i]; if (hash != NULL) { taosHashCleanup(hash); } - tsSdb.hashObj[i] = NULL; + tsSdb.hashObjs[i] = NULL; } } -void sdbSetFp(EMnSdb sdb, EMnKey keyType, SdbDeployFp deployFp, SdbEncodeFp encodeFp, SdbDecodeFp decodeFp, - int32_t dataSize) { - tsSdb.deployFp[sdb] = deployFp; - tsSdb.encodeFp[sdb] = encodeFp; - tsSdb.decodeFp[sdb] = decodeFp; - tsSdb.dataSize[sdb] = dataSize; - tsSdb.hashKey[sdb] = keyType; +void sdbSetHandler(SSdbDesc desc) { + ESdbType sdb = desc.sdbType; + tsSdb.keyTypes[sdb] = desc.keyType; + tsSdb.insertFps[sdb] = desc.insertFp; + tsSdb.updateFps[sdb] = desc.updateFp; + tsSdb.deleteFps[sdb] = desc.deleteFp; + tsSdb.deployFps[sdb] = desc.deployFp; + tsSdb.encodeFps[sdb] = desc.encodeFp; + tsSdb.decodeFps[sdb] = desc.decodeFp; } -static SHashObj *sdbGetHash(int32_t sdb) { - if (sdb >= MN_SDB_MAX || sdb <= MN_SDB_START) { - return NULL; - } - - SHashObj *hash = tsSdb.hashObj[sdb]; - if (hash == NULL) { - return NULL; - } - - return hash; -} - -void *sdbInsertRow(EMnSdb sdb, void *p) { +#if 0 +void *sdbInsertRow(ESdbType sdb, void *p) { SdbHead *pHead = p; pHead->type = sdb; - pHead->status = MN_SDB_STAT_AVAIL; + pHead->status = SDB_AVAIL; - char *pKey = (char *)pHead + sizeof(pHead); - int32_t keySize; - EMnKey keyType = tsSdb.hashKey[pHead->type]; - int32_t dataSize = tsSdb.dataSize[pHead->type]; + char *pKey = (char *)pHead + sizeof(pHead); + int32_t keySize; + EKeyType keyType = tsSdb.keyTypes[pHead->type]; + int32_t dataSize = tsSdb.dataSize[pHead->type]; SHashObj *hash = sdbGetHash(pHead->type); if (hash == NULL) { return NULL; } - if (keyType == MN_KEY_INT32) { + if (keyType == SDBINT32) { keySize = sizeof(int32_t); - } else if (keyType == MN_KEY_BINARY) { + } else if (keyType == SDB_KEY_BINARY) { keySize = strlen(pKey) + 1; } else { keySize = sizeof(int64_t); @@ -338,34 +331,58 @@ void *sdbInsertRow(EMnSdb sdb, void *p) { return taosHashGet(hash, pKey, keySize); } -void sdbDeleteRow(EMnSdb sdb, void *p) { +void sdbDeleteRow(ESdbType sdb, void *p) { SdbHead *pHead = p; - pHead->status = MN_SDB_STAT_DROPPED; + pHead->status = SDB_STATUS_DROPPED; } -void *sdbUpdateRow(EMnSdb sdb, void *pHead) { return sdbInsertRow(sdb, pHead); } +void *sdbUpdateRow(ESdbType sdb, void *pHead) { return sdbInsertRow(sdb, pHead); } + +#endif + +void *sdbAcquire(ESdbType sdb, void *pKey) { + terrno = 0; -void *sdbGetRow(EMnSdb sdb, void *pKey) { SHashObj *hash = sdbGetHash(sdb); if (hash == NULL) { return NULL; } - int32_t keySize; - EMnKey keyType = tsSdb.hashKey[sdb]; + int32_t keySize; + EKeyType keyType = tsSdb.keyTypes[sdb]; - if (keyType == MN_KEY_INT32) { - keySize = sizeof(int32_t); - } else if (keyType == MN_KEY_BINARY) { - keySize = strlen(pKey) + 1; - } else { - keySize = sizeof(int64_t); + switch (keyType) { + case SDB_KEY_INT32: + keySize = sizeof(int32_t); + break; + case SDB_KEY_INT64: + keySize = sizeof(int64_t); + break; + case SDB_KEY_BINARY: + keySize = strlen(pKey) + 1; + break; + default: + keySize = sizeof(int32_t); } - return taosHashGet(hash, pKey, keySize); + SSdbRow *pRow = taosHashGet(hash, pKey, keySize); + if (pRow == NULL) return NULL; + + if (pRow->status == SDB_STATUS_READY) { + atomic_add_fetch_32(&pRow->refCount, 1); + return pRow->data; + } else { + terrno = -1; // todo + return NULL; + } } -void *sdbFetchRow(EMnSdb sdb, void *pIter) { +void sdbRelease(ESdbType sdb, void *pObj) { + SSdbRow *pRow = (SSdbRow *)((char *)pObj - sizeof(SSdbRow)); + atomic_sub_fetch_32(&pRow->refCount, 1); +} + +void *sdbFetchRow(ESdbType sdb, void *pIter) { SHashObj *hash = sdbGetHash(sdb); if (hash == NULL) { return NULL; @@ -374,16 +391,15 @@ void *sdbFetchRow(EMnSdb sdb, void *pIter) { return taosHashIterate(hash, pIter); } -void sdbCancelFetch(EMnSdb sdb, void *pIter) { +void sdbCancelFetch(ESdbType sdb, void *pIter) { SHashObj *hash = sdbGetHash(sdb); if (hash == NULL) { return; } - taosHashCancelIterate(hash, pIter); } -int32_t sdbGetCount(EMnSdb sdb) { +int32_t sdbGetSize(ESdbType sdb) { SHashObj *hash = sdbGetHash(sdb); if (hash == NULL) { return 0; diff --git a/source/libs/transport/src/rpcMain.c b/source/libs/transport/src/rpcMain.c index fa49c20c77..1aafc880be 100644 --- a/source/libs/transport/src/rpcMain.c +++ b/source/libs/transport/src/rpcMain.c @@ -1582,7 +1582,7 @@ static int rpcCheckAuthentication(SRpcConn *pConn, char *msg, int msgLen) { code = htonl(pHead->code); if (code == TSDB_CODE_RPC_INVALID_TIME_STAMP || code == TSDB_CODE_RPC_AUTH_FAILURE || code == TSDB_CODE_RPC_INVALID_VERSION || - code == TSDB_CODE_RPC_AUTH_REQUIRED || code == TSDB_CODE_MND_INVALID_USER || code == TSDB_CODE_RPC_NOT_READY) { + code == TSDB_CODE_RPC_AUTH_REQUIRED || code == TSDB_CODE_MND_USER_NOT_EXIST || code == TSDB_CODE_RPC_NOT_READY) { pHead->msgLen = (int32_t)htonl((uint32_t)pHead->msgLen); // tTrace("%s, dont check authentication since code is:0x%x", pConn->info, code); return 0; diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 324f78ad79..2003270b21 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -142,12 +142,15 @@ TAOS_DEFINE_ERROR(TSDB_CODE_MND_FAILED_TO_START_SYNC, "failed to start sync" TAOS_DEFINE_ERROR(TSDB_CODE_MND_FAILED_TO_CREATE_DIR, "failed to create mnode dir") TAOS_DEFINE_ERROR(TSDB_CODE_MND_FAILED_TO_INIT_STEP, "failed to init components") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_SDB_OBJ_ALREADY_THERE, "Object already there") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_SDB_ERROR, "Unexpected generic error in sdb") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_SDB_INVALID_TABLE_TYPE, "Invalid table type") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_SDB_OBJ_NOT_THERE, "Object not there") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_SDB_INVAID_META_ROW, "Invalid meta row") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_SDB_INVAID_KEY_TYPE, "Invalid key type") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INTERNAL_ERROR, "Unexpected generic error in sdb") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_OUT_OF_MEMORY, "Out of memory in sdb") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_OBJ_ALREADY_THERE, "Object already there") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_OBJ_NOT_THERE, "Object not there") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVAID_RAW_DATA_VER, "Invalid raw data version") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVAID_RAW_DATA_LEN, "Invalid raw data len") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVALID_TABLE_TYPE, "Invalid table type") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVAID_META_ROW, "Invalid meta row") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVAID_KEY_TYPE, "Invalid key type") TAOS_DEFINE_ERROR(TSDB_CODE_MND_DNODE_ALREADY_EXIST, "DNode already exists") TAOS_DEFINE_ERROR(TSDB_CODE_MND_DNODE_NOT_EXIST, "DNode does not exist") @@ -166,12 +169,12 @@ TAOS_DEFINE_ERROR(TSDB_CODE_MND_DNODE_ID_NOT_CONFIGURED, "Dnode Id not configur TAOS_DEFINE_ERROR(TSDB_CODE_MND_DNODE_EP_NOT_CONFIGURED, "Dnode Ep not configured") TAOS_DEFINE_ERROR(TSDB_CODE_MND_ACCT_ALREADY_EXIST, "Account already exists") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_ACCT, "Invalid account") +TAOS_DEFINE_ERROR(TSDB_CODE_MND_ACCT_NOT_EXIST, "Invalid account") TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_ACCT_OPTION, "Invalid account options") TAOS_DEFINE_ERROR(TSDB_CODE_MND_ACCT_EXPIRED, "Account authorization has expired") TAOS_DEFINE_ERROR(TSDB_CODE_MND_USER_ALREADY_EXIST, "User already exists") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_USER, "Invalid user") +TAOS_DEFINE_ERROR(TSDB_CODE_MND_USER_NOT_EXIST, "Invalid user") TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_USER_FORMAT, "Invalid user format") TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_PASS_FORMAT, "Invalid password format") TAOS_DEFINE_ERROR(TSDB_CODE_MND_NO_USER_FROM_CONN, "Can not get user from conn") From 622cdc3907162ed7da57aac5d1d6775f6c03aa90 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 9 Nov 2021 14:27:17 +0800 Subject: [PATCH 07/17] 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) {} From 82ad9a911b92bf1458deec0340b38e1fc2dd7b14 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 9 Nov 2021 15:33:07 +0800 Subject: [PATCH 08/17] 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; +} From 38085942c6da37086fb7a4547ef0d907ab37ed7f Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 9 Nov 2021 17:03:15 +0800 Subject: [PATCH 09/17] transaction thread --- include/dnode/mnode/sdb/sdb.h | 8 +- include/dnode/mnode/transaction/trn.h | 2 + source/dnode/mnode/transaction/inc/trnInt.h | 1 + source/dnode/mnode/transaction/src/trn.c | 160 ++++++++++++++++-- .../dnode/mnode/transaction/src/trnThread.c | 46 +++++ 5 files changed, 203 insertions(+), 14 deletions(-) create mode 100644 source/dnode/mnode/transaction/src/trnThread.c diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h index f302a21c7a..6e4e476b3e 100644 --- a/include/dnode/mnode/sdb/sdb.h +++ b/include/dnode/mnode/sdb/sdb.h @@ -21,7 +21,7 @@ extern "C" { #endif #define SDB_GET_BINARY_VAL(pData, dataLen, val, valLen, code) \ - { \ + if (code == 0) { \ if ((dataLen) >= (valLen)) { \ memcpy((val), (char *)(pData), (valLen)); \ (dataLen) -= (valLen); \ @@ -32,7 +32,7 @@ extern "C" { } #define SDB_GET_INT32_VAL(pData, dataLen, val, code) \ - { \ + if (code == 0) { \ if (dataLen >= sizeof(int32_t)) { \ *(int32_t *)(pData) = (int32_t)(val); \ (dataLen) -= sizeof(int32_t); \ @@ -43,7 +43,7 @@ extern "C" { } #define SDB_GET_INT64_VAL(pData, dataLen, val, code) \ - { \ + if (code == 0) { \ if (dataLen >= sizeof(int64_t)) { \ *(int64_t *)(pData) = (int64_t)(val); \ (dataLen) -= sizeof(int64_t); \ @@ -87,7 +87,7 @@ typedef enum { SDB_VGROUP = 9, SDB_STABLE = 10, SDB_FUNC = 11, - SDB_OPER = 12, + SDB_TRANS = 12, SDB_MAX = 13 } ESdbType; diff --git a/include/dnode/mnode/transaction/trn.h b/include/dnode/mnode/transaction/trn.h index 62aa326682..47ac068b54 100644 --- a/include/dnode/mnode/transaction/trn.h +++ b/include/dnode/mnode/transaction/trn.h @@ -29,7 +29,9 @@ int32_t trnInit(); void trnCleanup(); STrans *trnCreate(); +int32_t trnPrepare(STrans *); int32_t trnCommit(STrans *); +int32_t trnExecute(STrans *); void trnDrop(STrans *); int32_t trnAppendRedoLog(STrans *, SSdbRawData *); diff --git a/source/dnode/mnode/transaction/inc/trnInt.h b/source/dnode/mnode/transaction/inc/trnInt.h index c5d57d1020..74c64f673f 100644 --- a/source/dnode/mnode/transaction/inc/trnInt.h +++ b/source/dnode/mnode/transaction/inc/trnInt.h @@ -41,6 +41,7 @@ typedef struct STrans { 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 850880d16d..c0c0deae7c 100644 --- a/source/dnode/mnode/transaction/src/trn.c +++ b/source/dnode/mnode/transaction/src/trn.c @@ -16,11 +16,9 @@ #define _DEFAULT_SOURCE #include "trnInt.h" +#define TRN_VER 1 #define TRN_DEFAULT_ARRAY_SIZE 8 -int32_t trnInit() { return 0; } -void trnCleanup(); - STrans *trnCreate() { STrans *pTrans = calloc(1, sizeof(STrans)); if (pTrans == NULL) { @@ -36,12 +34,7 @@ STrans *trnCreate() { 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); + trnDrop(pTrans); terrno = TSDB_CODE_MND_OUT_OF_MEMORY; return NULL; } @@ -50,7 +43,22 @@ STrans *trnCreate() { } int32_t trnCommit(STrans *pTrans) { return 0; } -void trnDrop(STrans *pTrans) {} + +static void trnDropLogs(SArray *pArray) { + for (int32_t index = 0; index < pArray->size; ++index) { + SSdbRawData *pRaw = taosArrayGetP(pArray, index); + free(pRaw); + } + + taosArrayDestroy(pArray); +} + +void trnDrop(STrans *pTrans) { + trnDropLogs(pTrans->redoLogs); + trnDropLogs(pTrans->undoLogs); + trnDropLogs(pTrans->commitLogs); + free(pTrans); +} int32_t trnAppendRedoLog(STrans *pTrans, SSdbRawData *pRaw) { void *ptr = taosArrayPush(pTrans->redoLogs, &pRaw); @@ -91,3 +99,135 @@ int32_t trnAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) { } return 0; } + +static SSdbRawData *trnActionEncode(STrans *pTrans) { + int32_t rawDataLen = 5 * sizeof(int32_t); + int32_t redoLogNum = taosArrayGetSize(pTrans->redoLogs); + int32_t undoLogNum = taosArrayGetSize(pTrans->undoLogs); + int32_t commitLogNum = taosArrayGetSize(pTrans->commitLogs); + int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions); + int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions); + + for (int32_t index = 0; index < redoLogNum; ++index) { + SSdbRawData *pRawData = taosArrayGet(pTrans->redoLogs, index); + rawDataLen += (sizeof(SSdbRawData) + pRawData->dataLen); + } + + for (int32_t index = 0; index < undoLogNum; ++index) { + SSdbRawData *pRawData = taosArrayGet(pTrans->undoLogs, index); + rawDataLen += (sizeof(SSdbRawData) + pRawData->dataLen); + } + + for (int32_t index = 0; index < commitLogNum; ++index) { + SSdbRawData *pRawData = taosArrayGet(pTrans->commitLogs, index); + rawDataLen += (sizeof(SSdbRawData) + pRawData->dataLen); + } + + SSdbRawData *pRaw = calloc(1, rawDataLen + sizeof(SSdbRawData)); + if (pRaw == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + int32_t dataLen = 0; + char *pData = pRaw->data; + SDB_SET_INT32_VAL(pData, dataLen, redoLogNum) + SDB_SET_INT32_VAL(pData, dataLen, undoLogNum) + SDB_SET_INT32_VAL(pData, dataLen, commitLogNum) + SDB_SET_INT32_VAL(pData, dataLen, redoActionNum) + SDB_SET_INT32_VAL(pData, dataLen, undoActionNum) + + pRaw->dataLen = dataLen; + pRaw->type = SDB_TRANS; + pRaw->sver = TRN_VER; + return pRaw; +} + +static STrans *trnActionDecode(SSdbRawData *pRaw) { + if (pRaw->sver != TRN_VER) { + terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER; + return NULL; + } + + STrans *pTrans = trnCreate(); + if (pTrans == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + int32_t redoLogNum = 0; + int32_t undoLogNum = 0; + int32_t commitLogNum = 0; + int32_t redoActionNum = 0; + int32_t undoActionNum = 0; + SSdbRawData *pTmp = malloc(sizeof(SSdbRawData)); + + int32_t code = 0; + int32_t dataLen = pRaw->dataLen; + char *pData = pRaw->data; + SDB_GET_INT32_VAL(pData, dataLen, redoLogNum, code) + SDB_GET_INT32_VAL(pData, dataLen, undoLogNum, code) + SDB_GET_INT32_VAL(pData, dataLen, commitLogNum, code) + SDB_GET_INT32_VAL(pData, dataLen, redoActionNum, code) + SDB_GET_INT32_VAL(pData, dataLen, undoActionNum, code) + + for (int32_t index = 0; index < redoLogNum; ++index) { + SDB_GET_BINARY_VAL(pData, dataLen, pTmp, sizeof(SSdbRawData), code); + if (code == 0 && pTmp->dataLen > 0) { + SSdbRawData *pRead = malloc(sizeof(SSdbRawData) + pTmp->dataLen); + if (pRead == NULL) { + code = TSDB_CODE_MND_OUT_OF_MEMORY; + break; + } + memcpy(pRead, pTmp, sizeof(SSdbRawData)); + SDB_GET_BINARY_VAL(pData, dataLen, pRead->data, pRead->dataLen, code); + void *ret = taosArrayPush(pTrans->redoLogs, &pRead); + if (ret == NULL) { + code = TSDB_CODE_MND_OUT_OF_MEMORY; + break; + } + } + } + + if (code != 0) { + trnDrop(pTrans); + terrno = code; + return NULL; + } + + return pTrans; +} + +static int32_t trnActionInsert(STrans *pTrans) { + SArray *pArray = pTrans->redoLogs; + int32_t arraySize = taosArrayGetSize(pArray); + + for (int32_t index = 0; index < arraySize; ++index) { + SSdbRawData *pRaw = taosArrayGetP(pArray, index); + int32_t code = sdbWrite(pRaw); + if (code != 0) { + return code; + } + } + + return 0; +} + +static int32_t trnActionDelete(STrans *pTrans) { + SArray *pArray = pTrans->redoLogs; + int32_t arraySize = taosArrayGetSize(pArray); + + for (int32_t index = 0; index < arraySize; ++index) { + SSdbRawData *pRaw = taosArrayGetP(pArray, index); + int32_t code = sdbWrite(pRaw); + if (code != 0) { + return code; + } + } + + return 0; +} + +static int32_t trnActionUpdate(STrans *pSrcUser, STrans *pDstUser) { + return 0; +} diff --git a/source/dnode/mnode/transaction/src/trnThread.c b/source/dnode/mnode/transaction/src/trnThread.c new file mode 100644 index 0000000000..6340f401b1 --- /dev/null +++ b/source/dnode/mnode/transaction/src/trnThread.c @@ -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 . + */ + +#define _DEFAULT_SOURCE +#include "trnInt.h" +#include "tthread.h" + +static struct { + pthread_t *threadId; + bool threadRunning; +} tsTrn; + +static void *trnThreadFunc(void *param) { + while (1) { + pthread_testcancel(); + } + return NULL; +} + +int32_t trnInit() { + tsTrn.threadId = taosCreateThread(trnThreadFunc, NULL); + if (tsTrn.threadId == NULL) { + return TSDB_CODE_MND_OUT_OF_MEMORY; + } + + return 0; +} + +void trnCleanup() { + if (tsTrn.threadId) { + taosDestoryThread(tsTrn.threadId); + tsTrn.threadId = NULL; + } +} From ff4ad0f43df722ffcf16c1deb2b356239a755bc0 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 9 Nov 2021 17:03:46 +0800 Subject: [PATCH 10/17] rename file --- source/dnode/mnode/transaction/src/{trn.c => trnMain.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename source/dnode/mnode/transaction/src/{trn.c => trnMain.c} (100%) diff --git a/source/dnode/mnode/transaction/src/trn.c b/source/dnode/mnode/transaction/src/trnMain.c similarity index 100% rename from source/dnode/mnode/transaction/src/trn.c rename to source/dnode/mnode/transaction/src/trnMain.c From 8668733880bcbfd9b3a60034165aa4096e8f7e2d Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 10 Nov 2021 23:31:35 +0800 Subject: [PATCH 11/17] [td-10564] fix compiler error. --- source/libs/executor/src/executorimpl.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 3b22fd3fea..d70b61bb4a 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -3956,7 +3956,7 @@ static void toSSDataBlock(SGroupResInfo *pGroupResInfo, SQueryRuntimeEnv* pRunti } SQueryAttr* pQueryAttr = pRuntimeEnv->pQueryAttr; - int32_t orderType = (pQueryAttr->pGroupbyExpr != NULL) ? pQueryAttr->pGroupbyExpr->orderType : TSDB_ORDER_ASC; + int32_t orderType = TSDB_ORDER_ASC;//(pQueryAttr->pGroupbyExpr != NULL) ? pQueryAttr->pGroupbyExpr->orderType : TSDB_ORDER_ASC; doCopyToSDataBlock(pRuntimeEnv, pGroupResInfo, orderType, pBlock); // refactor : extract method @@ -7090,16 +7090,16 @@ static bool validateQueryMsg(SQueryTableMsg *pQueryMsg) { return false; } - if (pQueryMsg->sw.gap < 0 || pQueryMsg->sw.primaryColId != PRIMARYKEY_TIMESTAMP_COL_ID) { +// if (pQueryMsg->sw.gap < 0 || pQueryMsg->sw.primaryColId != PRIMARYKEY_TIMESTAMP_COL_ID) { //qError("qmsg:%p illegal value of session window time %" PRId64, pQueryMsg, pQueryMsg->sw.gap); - return false; - } +// return false; +// } - if (pQueryMsg->sw.gap > 0 && pQueryMsg->interval.interval > 0) { +// if (pQueryMsg->sw.gap > 0 && pQueryMsg->interval.interval > 0) { //qError("qmsg:%p illegal value of session window time %" PRId64" and interval value %"PRId64, pQueryMsg, // pQueryMsg->sw.gap, pQueryMsg->interval.interval); - return false; - } +// return false; +// } if (pQueryMsg->numOfTables <= 0) { //qError("qmsg:%p illegal value of numOfTables %d", pQueryMsg, pQueryMsg->numOfTables); @@ -7245,8 +7245,8 @@ int32_t convertQueryMsg(SQueryTableMsg *pQueryMsg, SQueryParam* param) { pQueryMsg->secondStageOutput = htonl(pQueryMsg->secondStageOutput); pQueryMsg->sqlstrLen = htonl(pQueryMsg->sqlstrLen); pQueryMsg->prevResultLen = htonl(pQueryMsg->prevResultLen); - pQueryMsg->sw.gap = htobe64(pQueryMsg->sw.gap); - pQueryMsg->sw.primaryColId = htonl(pQueryMsg->sw.primaryColId); +// pQueryMsg->sw.gap = htobe64(pQueryMsg->sw.gap); +// pQueryMsg->sw.primaryColId = htonl(pQueryMsg->sw.primaryColId); pQueryMsg->tableScanOperator = htonl(pQueryMsg->tableScanOperator); pQueryMsg->numOfOperator = htonl(pQueryMsg->numOfOperator); pQueryMsg->udfContentOffset = htonl(pQueryMsg->udfContentOffset); @@ -8045,7 +8045,7 @@ SQInfo* createQInfoImpl(SQueryTableMsg* pQueryMsg, SGroupbyExpr* pGroupbyExpr, S pQueryAttr->tagColList = pTagCols; pQueryAttr->prjInfo.vgroupLimit = pQueryMsg->vgroupLimit; pQueryAttr->prjInfo.ts = (pQueryMsg->order == TSDB_ORDER_ASC)? INT64_MIN:INT64_MAX; - pQueryAttr->sw = pQueryMsg->sw; +// pQueryAttr->sw = pQueryMsg->sw; pQueryAttr->vgId = vgId; pQueryAttr->stableQuery = pQueryMsg->stableQuery; From d92a150c7203ba142a0acf4d94bd87ebc18ff7bc Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 11 Nov 2021 11:11:14 +0800 Subject: [PATCH 12/17] interface for transaction --- include/dnode/mnode/sdb/sdb.h | 32 +-- include/dnode/mnode/transaction/trn.h | 22 +- include/util/taoserror.h | 1 + source/dnode/mnode/impl/inc/mnodeSync.h | 1 + source/dnode/mnode/impl/src/mnodeAcct.c | 22 +- source/dnode/mnode/impl/src/mnodeSync.c | 6 + source/dnode/mnode/impl/src/mnodeUser.c | 131 +++++---- source/dnode/mnode/sdb/src/sdb.c | 14 +- source/dnode/mnode/transaction/CMakeLists.txt | 1 + source/dnode/mnode/transaction/inc/trnInt.h | 31 ++- source/dnode/mnode/transaction/src/trnInt.c | 164 +++++++++++ source/dnode/mnode/transaction/src/trnMain.c | 262 ++++++++---------- .../dnode/mnode/transaction/src/trnThread.c | 46 --- source/util/src/terror.c | 2 + 14 files changed, 423 insertions(+), 312 deletions(-) create mode 100644 source/dnode/mnode/transaction/src/trnInt.c delete mode 100644 source/dnode/mnode/transaction/src/trnThread.c diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h index 6e4e476b3e..344dc6922d 100644 --- a/include/dnode/mnode/sdb/sdb.h +++ b/include/dnode/mnode/sdb/sdb.h @@ -76,18 +76,18 @@ extern "C" { typedef enum { SDB_START = 0, - SDB_VERSION = 1, - SDB_CLUSTER = 2, - SDB_DNODE = 3, - SDB_MNODE = 4, - SDB_ACCT = 5, - SDB_AUTH = 6, - SDB_USER = 7, - SDB_DB = 8, - SDB_VGROUP = 9, - SDB_STABLE = 10, - SDB_FUNC = 11, - SDB_TRANS = 12, + SDB_TRANS = 1, + SDB_VERSION = 2, + SDB_CLUSTER = 3, + SDB_DNODE = 4, + SDB_MNODE = 5, + SDB_ACCT = 6, + SDB_AUTH = 7, + SDB_USER = 8, + SDB_DB = 9, + SDB_VGROUP = 10, + SDB_STABLE = 11, + SDB_FUNC = 12, SDB_MAX = 13 } ESdbType; @@ -104,14 +104,14 @@ typedef struct { int32_t cksum; int32_t dataLen; char data[]; -} SSdbRawData; +} SSdbRaw; typedef int32_t (*SdbInsertFp)(void *pObj); typedef int32_t (*SdbUpdateFp)(void *pSrcObj, void *pDstObj); typedef int32_t (*SdbDeleteFp)(void *pObj); typedef int32_t (*SdbDeployFp)(); -typedef void *(*SdbDecodeFp)(SSdbRawData *pRaw); -typedef SSdbRawData *(*SdbEncodeFp)(void *pObj); +typedef void *(*SdbDecodeFp)(SSdbRaw *pRaw); +typedef SSdbRaw *(*SdbEncodeFp)(void *pObj); typedef struct { ESdbType sdbType; @@ -129,7 +129,7 @@ void sdbCleanup(); void sdbSetHandler(SSdbDesc desc); int32_t sdbRead(); -int32_t sdbWrite(SSdbRawData *pRawData); +int32_t sdbWrite(SSdbRaw *pRaw); int32_t sdbCommit(); int32_t sdbDeploy(); diff --git a/include/dnode/mnode/transaction/trn.h b/include/dnode/mnode/transaction/trn.h index 47ac068b54..8ba043de12 100644 --- a/include/dnode/mnode/transaction/trn.h +++ b/include/dnode/mnode/transaction/trn.h @@ -24,21 +24,23 @@ extern "C" { #endif typedef struct STrans STrans; +typedef enum { TRN_POLICY_ROLLBACK = 1, TRN_POLICY_RETRY = 2 } ETrnPolicy; int32_t trnInit(); void trnCleanup(); -STrans *trnCreate(); -int32_t trnPrepare(STrans *); -int32_t trnCommit(STrans *); -int32_t trnExecute(STrans *); -void trnDrop(STrans *); +STrans *trnCreate(ETrnPolicy); +void trnDrop(STrans *pTrans); +void trnSetRpcHandle(STrans *pTrans, void *rpcHandle); +int32_t trnAppendRedoLog(STrans *pTrans, SSdbRaw *pRaw); +int32_t trnAppendUndoLog(STrans *pTrans, SSdbRaw *pRaw); +int32_t trnAppendCommitLog(STrans *pTrans, SSdbRaw *pRaw); +int32_t trnAppendRedoAction(STrans *pTrans, SEpSet *, void *pMsg); +int32_t trnAppendUndoAction(STrans *pTrans, 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); +int32_t trnPrepare(STrans *pTrans, int32_t (*syncfp)(SSdbRaw *pRaw, void *pData)); +int32_t trnApply(SSdbRaw *pRaw, void *pData, int32_t code); +int32_t trnExecute(int32_t tranId); #ifdef __cplusplus } diff --git a/include/util/taoserror.h b/include/util/taoserror.h index ba3dad63af..8c4a726bf6 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -27,6 +27,7 @@ extern "C" { #define TAOS_FAILED(err) ((err) < 0) const char* tstrerror(int32_t err); +const char* terrstr(); int32_t* taosGetErrno(); #define terrno (*taosGetErrno()) diff --git a/source/dnode/mnode/impl/inc/mnodeSync.h b/source/dnode/mnode/impl/inc/mnodeSync.h index 714d6ed1a8..380ad36e23 100644 --- a/source/dnode/mnode/impl/inc/mnodeSync.h +++ b/source/dnode/mnode/impl/inc/mnodeSync.h @@ -24,6 +24,7 @@ extern "C" { int32_t mnodeInitSync(); void mnodeCleanUpSync(); +int32_t mnodeSyncPropose(SSdbRaw *pRaw, void *pData); bool mnodeIsMaster(); diff --git a/source/dnode/mnode/impl/src/mnodeAcct.c b/source/dnode/mnode/impl/src/mnodeAcct.c index 0811a64e92..e4538fa391 100644 --- a/source/dnode/mnode/impl/src/mnodeAcct.c +++ b/source/dnode/mnode/impl/src/mnodeAcct.c @@ -18,8 +18,8 @@ #define ACCT_VER 1 -static SSdbRawData *mnodeAcctActionEncode(SAcctObj *pAcct) { - SSdbRawData *pRaw = calloc(1, sizeof(SAcctObj) + sizeof(SSdbRawData)); +static SSdbRaw *mnodeAcctActionEncode(SAcctObj *pAcct) { + SSdbRaw *pRaw = calloc(1, sizeof(SAcctObj) + sizeof(SSdbRaw)); if (pRaw == NULL) { terrno = TSDB_CODE_MND_OUT_OF_MEMORY; return NULL; @@ -45,7 +45,7 @@ static SSdbRawData *mnodeAcctActionEncode(SAcctObj *pAcct) { return pRaw; } -static SAcctObj *mnodeAcctActionDecode(SSdbRawData *pRaw) { +static SAcctObj *mnodeAcctActionDecode(SSdbRaw *pRaw) { if (pRaw->sver != ACCT_VER) { terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER; return NULL; @@ -96,23 +96,21 @@ static int32_t mnodeCreateDefaultAcct() { SAcctObj acctObj = {0}; tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN); acctObj.createdTime = taosGetTimestampMs(); - acctObj.updateTime = taosGetTimestampMs(); + acctObj.updateTime = acctObj.createdTime; acctObj.acctId = 1; - acctObj.cfg = (SAcctCfg){.maxUsers = 128, - .maxDbs = 128, + acctObj.cfg = (SAcctCfg){.maxUsers = 1024, + .maxDbs = 1024, .maxTimeSeries = INT32_MAX, - .maxStreams = 1000, + .maxStreams = 8092, .maxStorage = INT64_MAX, .accessState = TSDB_VN_ALL_ACCCESS}; - SSdbRawData *pRaw = mnodeAcctActionEncode(&acctObj); + SSdbRaw *pRaw = mnodeAcctActionEncode(&acctObj); if (pRaw != NULL) { - code = sdbWrite(pRaw); - } else { - code = terrno; + return -1; } - return code; + return sdbWrite(pRaw); } int32_t mnodeInitAcct() { diff --git a/source/dnode/mnode/impl/src/mnodeSync.c b/source/dnode/mnode/impl/src/mnodeSync.c index c161bb971a..7541ab6b59 100644 --- a/source/dnode/mnode/impl/src/mnodeSync.c +++ b/source/dnode/mnode/impl/src/mnodeSync.c @@ -20,4 +20,10 @@ int32_t mnodeInitSync() { return 0; } void mnodeCleanUpSync() {} +int32_t mnodeSyncPropose(SSdbRaw *pRaw, void *pData) { + trnApply(pRaw, pData, 0); + free(pRaw); + return 0; +} + bool mnodeIsMaster() { return true; } \ No newline at end of file diff --git a/source/dnode/mnode/impl/src/mnodeUser.c b/source/dnode/mnode/impl/src/mnodeUser.c index cc321f0ef0..3b5d40d807 100644 --- a/source/dnode/mnode/impl/src/mnodeUser.c +++ b/source/dnode/mnode/impl/src/mnodeUser.c @@ -14,15 +14,15 @@ */ #define _DEFAULT_SOURCE +#include "mnodeSync.h" #include "os.h" -#include "tkey.h" #include "tglobal.h" -#include "mnodeInt.h" +#include "tkey.h" #define USER_VER 1 -static SSdbRawData *mnodeUserActionEncode(SUserObj *pUser) { - SSdbRawData *pRaw = calloc(1, sizeof(SUserObj) + sizeof(SSdbRawData)); +static SSdbRaw *mnodeUserActionEncode(SUserObj *pUser) { + SSdbRaw *pRaw = calloc(1, sizeof(SUserObj) + sizeof(SSdbRaw)); if (pRaw == NULL) { terrno = TSDB_CODE_MND_OUT_OF_MEMORY; return NULL; @@ -43,7 +43,7 @@ static SSdbRawData *mnodeUserActionEncode(SUserObj *pUser) { return pRaw; } -static SUserObj *mnodeUserActionDecode(SSdbRawData *pRaw) { +static SUserObj *mnodeUserActionDecode(SSdbRaw *pRaw) { if (pRaw->sver != USER_VER) { terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER; return NULL; @@ -77,12 +77,14 @@ static SUserObj *mnodeUserActionDecode(SSdbRawData *pRaw) { static int32_t mnodeUserActionInsert(SUserObj *pUser) { pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); if (pUser->prohibitDbHash == NULL) { - return TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return -1; } pUser->pAcct = sdbAcquire(SDB_ACCT, pUser->acct); if (pUser->pAcct == NULL) { - return TSDB_CODE_MND_ACCT_NOT_EXIST; + terrno = TSDB_CODE_MND_ACCT_NOT_EXIST; + return -1; } return 0; @@ -108,8 +110,6 @@ static int32_t mnodeUserActionUpdate(SUserObj *pSrcUser, SUserObj *pDstUser) { } static int32_t mnodeCreateDefaultUser(char *acct, char *user, char *pass) { - int32_t code = 0; - SUserObj userObj = {0}; tstrncpy(userObj.user, user, TSDB_USER_LEN); tstrncpy(userObj.acct, acct, TSDB_USER_LEN); @@ -121,35 +121,31 @@ static int32_t mnodeCreateDefaultUser(char *acct, char *user, char *pass) { userObj.rootAuth = 1; } - SSdbRawData *pRaw = mnodeUserActionEncode(&userObj); - if (pRaw != NULL) { - code = sdbWrite(pRaw); - } else { - code = terrno; + SSdbRaw *pRaw = mnodeUserActionEncode(&userObj); + if (pRaw == NULL) { + return -1; } - return code; + return sdbWrite(pRaw); } static int32_t mnodeCreateDefaultUsers() { - int32_t code = mnodeCreateDefaultUser(TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS); - if (code != 0) return code; + if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) { + return -1; + } - code = mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "monitor", tsInternalPass); - if (code != 0) return code; + if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "monitor", tsInternalPass) != 0) { + return -1; + } - code = mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, tsInternalPass); - if (code != 0) return code; + if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, tsInternalPass) != 0) { + return -1; + } - return code; + return 0; } 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); @@ -158,79 +154,80 @@ static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnMsg *pMsg) userObj.updateTime = userObj.createdTime; userObj.rootAuth = 0; - pRedoRaw = mnodeUserActionEncode(&userObj); - if (pRedoRaw == NULL) { - code = terrno; - goto CREATE_USER_OVER; + STrans *pTrans = trnCreate(TRN_POLICY_ROLLBACK); + if (pTrans == NULL) return -1; + + SSdbRaw *pRedoRaw = mnodeUserActionEncode(&userObj); + if (pRedoRaw == NULL || trnAppendRedoLog(pTrans, pRedoRaw) != 0) { + trnDrop(pTrans); + return -1; } - pRedoRaw->status = SDB_STATUS_READY; + pRedoRaw->status = SDB_STATUS_CREATING; pRedoRaw->action = SDB_ACTION_INSERT; - pUndoRaw = mnodeUserActionEncode(&userObj); - if (pUndoRaw == NULL) { - code = terrno; - goto CREATE_USER_OVER; + SSdbRaw *pUndoRaw = mnodeUserActionEncode(&userObj); + if (pUndoRaw == NULL || trnAppendUndoLog(pTrans, pUndoRaw) != 0) { + trnDrop(pTrans); + return -1; } 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) { + SSdbRaw *pCommitRaw = mnodeUserActionEncode(&userObj); + if (pCommitRaw == NULL || trnAppendCommitLog(pTrans, pCommitRaw) != 0) { trnDrop(pTrans); - free(pRedoRaw); - free(pUndoRaw); + return -1; + } + pCommitRaw->status = SDB_STATUS_READY; + pCommitRaw->action = SDB_ACTION_UPDATE; + + trnSetRpcHandle(pTrans, pMsg->rpcMsg.handle); + + if (trnPrepare(pTrans, mnodeSyncPropose) != 0) { + trnDrop(pTrans); + return -1; } - return code; + trnDrop(pTrans); + return 0; } 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; + terrno = TSDB_CODE_MND_INVALID_USER_FORMAT; + mError("user:%s, failed to create since %s", pCreate->user, terrstr()); + return -1; } 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; + terrno = TSDB_CODE_MND_INVALID_PASS_FORMAT; + mError("user:%s, failed to create since %s", pCreate->user, terrstr()); + return -1; } 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; + terrno = TSDB_CODE_MND_USER_ALREADY_EXIST; + mError("user:%s, failed to create since %s", pCreate->user, terrstr()); + return -1; } 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; + terrno = TSDB_CODE_MND_NO_USER_FROM_CONN; + mError("user:%s, failed to create since %s", pCreate->user, terrstr()); + return -1; } - code = mnodeCreateUser(pOperUser->acct, pCreate->user, pCreate->pass, pMsg); + int32_t 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; + mError("user:%s, failed to create since %s", pCreate->user, terrstr()); + return -1; } return TSDB_CODE_MND_ACTION_IN_PROGRESS; diff --git a/source/dnode/mnode/sdb/src/sdb.c b/source/dnode/mnode/sdb/src/sdb.c index a47ed8ec20..d3b825e190 100644 --- a/source/dnode/mnode/sdb/src/sdb.c +++ b/source/dnode/mnode/sdb/src/sdb.c @@ -61,7 +61,7 @@ static SHashObj *sdbGetHash(int32_t sdb) { return hash; } -int32_t sdbWrite(SSdbRawData *pRaw) { +int32_t sdbWrite(SSdbRaw *pRaw) { SHashObj *hash = sdbGetHash(pRaw->type); switch (pRaw->action) { case SDB_ACTION_INSERT: @@ -85,7 +85,7 @@ static int32_t sdbReadVersion(FileFd fd) { return 0; } static int32_t sdbReadDataFile() { int32_t code = 0; - SSdbRawData *pRaw = malloc(SDB_MAX_SIZE); + SSdbRaw *pRaw = malloc(SDB_MAX_SIZE); if (pRaw == NULL) { return TSDB_CODE_MND_OUT_OF_MEMORY; } @@ -101,7 +101,7 @@ static int32_t sdbReadDataFile() { int64_t offset = 0; while (1) { - int32_t ret = (int32_t)taosReadFile(fd, pRaw, sizeof(SSdbRawData)); + int32_t ret = (int32_t)taosReadFile(fd, pRaw, sizeof(SSdbRaw)); if (ret == 0) break; if (ret < 0) { @@ -110,7 +110,7 @@ static int32_t sdbReadDataFile() { break; } - if (ret < sizeof(SSdbRawData)) { + if (ret < sizeof(SSdbRaw)) { code = TSDB_CODE_SDB_INTERNAL_ERROR; mError("failed to read file:%s since %s", file, tstrerror(code)); break; @@ -143,7 +143,7 @@ static int32_t sdbWriteDataFile() { return code; } - for (int32_t i = SDB_START; i < SDB_MAX; ++i) { + for (int32_t i = SDB_MAX - 1; i > SDB_START; --i) { SHashObj *hash = tsSdb.hashObjs[i]; if (!hash) continue; @@ -153,9 +153,9 @@ static int32_t sdbWriteDataFile() { SSdbRow *pRow = taosHashIterate(hash, NULL); while (pRow != NULL) { if (pRow->status == SDB_STATUS_READY) continue; - SSdbRawData *pRaw = (*encodeFp)(pRow->data); + SSdbRaw *pRaw = (*encodeFp)(pRow->data); if (pRaw != NULL) { - taosWriteFile(fd, pRaw, sizeof(SSdbRawData) + pRaw->dataLen); + taosWriteFile(fd, pRaw, sizeof(SSdbRaw) + pRaw->dataLen); } else { taosHashCancelIterate(hash, pRow); code = TSDB_CODE_SDB_INTERNAL_ERROR; diff --git a/source/dnode/mnode/transaction/CMakeLists.txt b/source/dnode/mnode/transaction/CMakeLists.txt index 6b01f5bd18..d35a8c9b3f 100644 --- a/source/dnode/mnode/transaction/CMakeLists.txt +++ b/source/dnode/mnode/transaction/CMakeLists.txt @@ -11,4 +11,5 @@ target_link_libraries( PRIVATE common PRIVATE util PRIVATE sdb + PRIVATE transport ) diff --git a/source/dnode/mnode/transaction/inc/trnInt.h b/source/dnode/mnode/transaction/inc/trnInt.h index 74c64f673f..03860734ee 100644 --- a/source/dnode/mnode/transaction/inc/trnInt.h +++ b/source/dnode/mnode/transaction/inc/trnInt.h @@ -33,14 +33,35 @@ extern "C" { #define mDebug(...) { if (mDebugFlag & DEBUG_DEBUG) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }} #define mTrace(...) { if (mDebugFlag & DEBUG_TRACE) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }} +#define TRN_VER 1 +#define TRN_DEFAULT_ARRAY_SIZE 8 + +typedef enum { + TRN_STAGE_PREPARE = 1, + TRN_STAGE_EXECUTE = 2, + TRN_STAGE_COMMIT = 3, + TRN_STAGE_ROLLBACK = 4, + TRN_STAGE_RETRY = 5 +} ETrnStage; + typedef struct STrans { - SArray *redoLogs; - SArray *undoLogs; - SArray *commitLogs; - SArray *redoActions; - SArray *undoActions; + int32_t id; + ETrnStage stage; + ETrnPolicy policy; + void *rpcHandle; + SArray *redoLogs; + SArray *undoLogs; + SArray *commitLogs; + SArray *redoActions; + SArray *undoActions; } STrans; +SSdbRaw *trnActionEncode(STrans *pTrans); +STrans *trnActionDecode(SSdbRaw *pRaw); +int32_t trnActionInsert(STrans *pTrans); +int32_t trnActionDelete(STrans *pTrans); +int32_t trnActionUpdate(STrans *pSrcTrans, STrans *pDstTrans); +int32_t trnGenerateTransId(); #ifdef __cplusplus } diff --git a/source/dnode/mnode/transaction/src/trnInt.c b/source/dnode/mnode/transaction/src/trnInt.c new file mode 100644 index 0000000000..4a69007de5 --- /dev/null +++ b/source/dnode/mnode/transaction/src/trnInt.c @@ -0,0 +1,164 @@ +/* + * 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" + +SSdbRaw *trnActionEncode(STrans *pTrans) { + int32_t rawDataLen = 5 * sizeof(int32_t); + int32_t redoLogNum = taosArrayGetSize(pTrans->redoLogs); + int32_t undoLogNum = taosArrayGetSize(pTrans->undoLogs); + int32_t commitLogNum = taosArrayGetSize(pTrans->commitLogs); + int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions); + int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions); + + for (int32_t index = 0; index < redoLogNum; ++index) { + SSdbRaw *pRawData = taosArrayGet(pTrans->redoLogs, index); + rawDataLen += (sizeof(SSdbRaw) + pRawData->dataLen); + } + + for (int32_t index = 0; index < undoLogNum; ++index) { + SSdbRaw *pRawData = taosArrayGet(pTrans->undoLogs, index); + rawDataLen += (sizeof(SSdbRaw) + pRawData->dataLen); + } + + for (int32_t index = 0; index < commitLogNum; ++index) { + SSdbRaw *pRawData = taosArrayGet(pTrans->commitLogs, index); + rawDataLen += (sizeof(SSdbRaw) + pRawData->dataLen); + } + + SSdbRaw *pRaw = calloc(1, rawDataLen + sizeof(SSdbRaw)); + if (pRaw == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + int32_t dataLen = 0; + char *pData = pRaw->data; + SDB_SET_INT32_VAL(pData, dataLen, redoLogNum) + SDB_SET_INT32_VAL(pData, dataLen, undoLogNum) + SDB_SET_INT32_VAL(pData, dataLen, commitLogNum) + SDB_SET_INT32_VAL(pData, dataLen, redoActionNum) + SDB_SET_INT32_VAL(pData, dataLen, undoActionNum) + + pRaw->dataLen = dataLen; + pRaw->type = SDB_TRANS; + pRaw->sver = TRN_VER; + return pRaw; +} + +STrans *trnActionDecode(SSdbRaw *pRaw) { + if (pRaw->sver != TRN_VER) { + terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER; + return NULL; + } + + STrans *pTrans = NULL; + if (pTrans == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + int32_t redoLogNum = 0; + int32_t undoLogNum = 0; + int32_t commitLogNum = 0; + int32_t redoActionNum = 0; + int32_t undoActionNum = 0; + SSdbRaw *pTmp = malloc(sizeof(SSdbRaw)); + + int32_t code = 0; + int32_t dataLen = pRaw->dataLen; + char *pData = pRaw->data; + SDB_GET_INT32_VAL(pData, dataLen, redoLogNum, code) + SDB_GET_INT32_VAL(pData, dataLen, undoLogNum, code) + SDB_GET_INT32_VAL(pData, dataLen, commitLogNum, code) + SDB_GET_INT32_VAL(pData, dataLen, redoActionNum, code) + SDB_GET_INT32_VAL(pData, dataLen, undoActionNum, code) + + for (int32_t index = 0; index < redoLogNum; ++index) { + SDB_GET_BINARY_VAL(pData, dataLen, pTmp, sizeof(SSdbRaw), code); + if (code == 0 && pTmp->dataLen > 0) { + SSdbRaw *pRead = malloc(sizeof(SSdbRaw) + pTmp->dataLen); + if (pRead == NULL) { + code = TSDB_CODE_MND_OUT_OF_MEMORY; + break; + } + memcpy(pRead, pTmp, sizeof(SSdbRaw)); + SDB_GET_BINARY_VAL(pData, dataLen, pRead->data, pRead->dataLen, code); + void *ret = taosArrayPush(pTrans->redoLogs, &pRead); + if (ret == NULL) { + code = TSDB_CODE_MND_OUT_OF_MEMORY; + break; + } + } + } + + if (code != 0) { + trnDrop(pTrans); + terrno = code; + return NULL; + } + + return pTrans; +} + +int32_t trnActionInsert(STrans *pTrans) { + SArray *pArray = pTrans->redoLogs; + int32_t arraySize = taosArrayGetSize(pArray); + + for (int32_t index = 0; index < arraySize; ++index) { + SSdbRaw *pRaw = taosArrayGetP(pArray, index); + int32_t code = sdbWrite(pRaw); + if (code != 0) { + return code; + } + } + + return 0; +} + +int32_t trnActionDelete(STrans *pTrans) { + SArray *pArray = pTrans->redoLogs; + int32_t arraySize = taosArrayGetSize(pArray); + + for (int32_t index = 0; index < arraySize; ++index) { + SSdbRaw *pRaw = taosArrayGetP(pArray, index); + int32_t code = sdbWrite(pRaw); + if (code != 0) { + return code; + } + } + + return 0; +} + +int32_t trnActionUpdate(STrans *pSrcTrans, STrans *pDstTrans) { return 0; } + +int32_t trnGenerateTransId() { return 1; } + +int32_t trnInit() { + SSdbDesc desc = {.sdbType = SDB_TRANS, + .keyType = SDB_KEY_INT32, + .encodeFp = (SdbEncodeFp)trnActionEncode, + .decodeFp = (SdbDecodeFp)trnActionDecode, + .insertFp = (SdbInsertFp)trnActionInsert, + .updateFp = (SdbUpdateFp)trnActionUpdate, + .deleteFp = (SdbDeleteFp)trnActionDelete}; + sdbSetHandler(desc); + + return 0; +} + +void trnCleanup() {} diff --git a/source/dnode/mnode/transaction/src/trnMain.c b/source/dnode/mnode/transaction/src/trnMain.c index c0c0deae7c..3ff991a676 100644 --- a/source/dnode/mnode/transaction/src/trnMain.c +++ b/source/dnode/mnode/transaction/src/trnMain.c @@ -15,17 +15,18 @@ #define _DEFAULT_SOURCE #include "trnInt.h" +#include "trpc.h" -#define TRN_VER 1 -#define TRN_DEFAULT_ARRAY_SIZE 8 - -STrans *trnCreate() { +STrans *trnCreate(ETrnPolicy policy) { STrans *pTrans = calloc(1, sizeof(STrans)); if (pTrans == NULL) { terrno = TSDB_CODE_MND_OUT_OF_MEMORY; return NULL; } + pTrans->id = trnGenerateTransId(); + pTrans->stage = TRN_STAGE_PREPARE; + pTrans->policy = policy; 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 *)); @@ -34,7 +35,6 @@ STrans *trnCreate() { if (pTrans->redoLogs == NULL || pTrans->undoLogs == NULL || pTrans->commitLogs == NULL || pTrans->redoActions == NULL || pTrans->undoActions == NULL) { - trnDrop(pTrans); terrno = TSDB_CODE_MND_OUT_OF_MEMORY; return NULL; } @@ -42,192 +42,156 @@ STrans *trnCreate() { return pTrans; } -int32_t trnCommit(STrans *pTrans) { return 0; } - -static void trnDropLogs(SArray *pArray) { +static void trnDropArray(SArray *pArray) { for (int32_t index = 0; index < pArray->size; ++index) { - SSdbRawData *pRaw = taosArrayGetP(pArray, index); - free(pRaw); + SSdbRaw *pRaw = taosArrayGetP(pArray, index); + tfree(pRaw); } taosArrayDestroy(pArray); } void trnDrop(STrans *pTrans) { - trnDropLogs(pTrans->redoLogs); - trnDropLogs(pTrans->undoLogs); - trnDropLogs(pTrans->commitLogs); - free(pTrans); + trnDropArray(pTrans->redoLogs); + trnDropArray(pTrans->undoLogs); + trnDropArray(pTrans->commitLogs); + trnDropArray(pTrans->redoActions); + trnDropArray(pTrans->undoActions); + tfree(pTrans); } -int32_t trnAppendRedoLog(STrans *pTrans, SSdbRawData *pRaw) { - void *ptr = taosArrayPush(pTrans->redoLogs, &pRaw); - if (ptr == NULL) { - return TSDB_CODE_MND_OUT_OF_MEMORY; +void trnSetRpcHandle(STrans *pTrans, void *rpcHandle) { + if (pTrans != NULL) { + pTrans->rpcHandle = rpcHandle; } +} + +static int32_t trnAppendArray(SArray *pArray, SSdbRaw *pRaw) { + if (pArray == NULL || pRaw == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return -1; + } + + void *ptr = taosArrayPush(pArray, &pRaw); + if (ptr == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return -1; + } + 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 trnAppendRedoLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->redoLogs, pRaw); } -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 trnAppendUndoLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->undoLogs, pRaw); } + +int32_t trnAppendCommitLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->commitLogs, pRaw); } 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; + return trnAppendArray(pTrans->redoActions, pMsg); } 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; + return trnAppendArray(pTrans->undoActions, pMsg); } -static SSdbRawData *trnActionEncode(STrans *pTrans) { - int32_t rawDataLen = 5 * sizeof(int32_t); - int32_t redoLogNum = taosArrayGetSize(pTrans->redoLogs); - int32_t undoLogNum = taosArrayGetSize(pTrans->undoLogs); - int32_t commitLogNum = taosArrayGetSize(pTrans->commitLogs); - int32_t redoActionNum = taosArrayGetSize(pTrans->redoActions); - int32_t undoActionNum = taosArrayGetSize(pTrans->undoActions); +int32_t trnPrepare(STrans *pTrans, int32_t (*syncfp)(SSdbRaw *pRaw, void *pData)) { + if (syncfp == NULL) return -1; - for (int32_t index = 0; index < redoLogNum; ++index) { - SSdbRawData *pRawData = taosArrayGet(pTrans->redoLogs, index); - rawDataLen += (sizeof(SSdbRawData) + pRawData->dataLen); - } - - for (int32_t index = 0; index < undoLogNum; ++index) { - SSdbRawData *pRawData = taosArrayGet(pTrans->undoLogs, index); - rawDataLen += (sizeof(SSdbRawData) + pRawData->dataLen); - } - - for (int32_t index = 0; index < commitLogNum; ++index) { - SSdbRawData *pRawData = taosArrayGet(pTrans->commitLogs, index); - rawDataLen += (sizeof(SSdbRawData) + pRawData->dataLen); - } - - SSdbRawData *pRaw = calloc(1, rawDataLen + sizeof(SSdbRawData)); + SSdbRaw *pRaw = trnActionEncode(pTrans); if (pRaw == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; - return NULL; + mError("tranId:%d, failed to decode trans since %s", pTrans->id, terrstr()); + return -1; } - int32_t dataLen = 0; - char *pData = pRaw->data; - SDB_SET_INT32_VAL(pData, dataLen, redoLogNum) - SDB_SET_INT32_VAL(pData, dataLen, undoLogNum) - SDB_SET_INT32_VAL(pData, dataLen, commitLogNum) - SDB_SET_INT32_VAL(pData, dataLen, redoActionNum) - SDB_SET_INT32_VAL(pData, dataLen, undoActionNum) + if (sdbWrite(pRaw) != 0) { + mError("tranId:%d, failed to write trans since %s", pTrans->id, terrstr()); + return -1; + } - pRaw->dataLen = dataLen; - pRaw->type = SDB_TRANS; - pRaw->sver = TRN_VER; - return pRaw; + if ((*syncfp)(pRaw, pTrans->rpcHandle) != 0) { + mError("tranId:%d, failed to sync trans since %s", pTrans->id, terrstr()); + return -1; + } + + return 0; } -static STrans *trnActionDecode(SSdbRawData *pRaw) { - if (pRaw->sver != TRN_VER) { - terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER; - return NULL; - } - - STrans *pTrans = trnCreate(); - if (pTrans == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; - return NULL; - } - - int32_t redoLogNum = 0; - int32_t undoLogNum = 0; - int32_t commitLogNum = 0; - int32_t redoActionNum = 0; - int32_t undoActionNum = 0; - SSdbRawData *pTmp = malloc(sizeof(SSdbRawData)); - - int32_t code = 0; - int32_t dataLen = pRaw->dataLen; - char *pData = pRaw->data; - SDB_GET_INT32_VAL(pData, dataLen, redoLogNum, code) - SDB_GET_INT32_VAL(pData, dataLen, undoLogNum, code) - SDB_GET_INT32_VAL(pData, dataLen, commitLogNum, code) - SDB_GET_INT32_VAL(pData, dataLen, redoActionNum, code) - SDB_GET_INT32_VAL(pData, dataLen, undoActionNum, code) - - for (int32_t index = 0; index < redoLogNum; ++index) { - SDB_GET_BINARY_VAL(pData, dataLen, pTmp, sizeof(SSdbRawData), code); - if (code == 0 && pTmp->dataLen > 0) { - SSdbRawData *pRead = malloc(sizeof(SSdbRawData) + pTmp->dataLen); - if (pRead == NULL) { - code = TSDB_CODE_MND_OUT_OF_MEMORY; - break; - } - memcpy(pRead, pTmp, sizeof(SSdbRawData)); - SDB_GET_BINARY_VAL(pData, dataLen, pRead->data, pRead->dataLen, code); - void *ret = taosArrayPush(pTrans->redoLogs, &pRead); - if (ret == NULL) { - code = TSDB_CODE_MND_OUT_OF_MEMORY; - break; - } - } +static void trnSendRpcRsp(void *rpcHandle, int32_t code) { + if (rpcHandle != NULL) { + SRpcMsg rspMsg = {.handle = rpcHandle, .code = terrno}; + rpcSendResponse(&rspMsg); } +} +int32_t trnApply(SSdbRaw *pRaw, void *pData, int32_t code) { if (code != 0) { - trnDrop(pTrans); + trnSendRpcRsp(pData, terrno); + return 0; + } + + if (sdbWrite(pRaw) != 0) { + code = terrno; + trnSendRpcRsp(pData, code); terrno = code; - return NULL; + return -1; } - return pTrans; + return 0; } -static int32_t trnActionInsert(STrans *pTrans) { - SArray *pArray = pTrans->redoLogs; - int32_t arraySize = taosArrayGetSize(pArray); +int32_t trnExecuteRedoLogs(STrans *pTrans) {return 0;} +int32_t trnExecuteUndoLogs(STrans *pTrans) {return 0;} +int32_t trnExecuteCommitLogs(STrans *pTrans) {return 0;} +int32_t trnExecuteRedoActions(STrans *pTrans) {return 0;} +int32_t trnExecuteUndoActions(STrans *pTrans) {return 0;} +static int32_t trnPerfomRollbackStage(STrans *pTrans) { return 0; } - for (int32_t index = 0; index < arraySize; ++index) { - SSdbRawData *pRaw = taosArrayGetP(pArray, index); - int32_t code = sdbWrite(pRaw); - if (code != 0) { - return code; +int32_t trnExecute(int32_t tranId) { + int32_t code = 0; + + STrans *pTrans = sdbAcquire(SDB_TRANS, &tranId); + if (pTrans == NULL) { + code = terrno; + return code; + } + + if (pTrans->stage == TRN_STAGE_PREPARE) { + code = trnExecuteRedoLogs(pTrans); + if (code == 0) { + pTrans->stage = TRN_STAGE_EXECUTE; + } else { + pTrans->stage = TRN_STAGE_ROLLBACK; } } - return 0; -} - -static int32_t trnActionDelete(STrans *pTrans) { - SArray *pArray = pTrans->redoLogs; - int32_t arraySize = taosArrayGetSize(pArray); - - for (int32_t index = 0; index < arraySize; ++index) { - SSdbRawData *pRaw = taosArrayGetP(pArray, index); - int32_t code = sdbWrite(pRaw); - if (code != 0) { - return code; + if (pTrans->stage == TRN_STAGE_EXECUTE) { + code = trnExecuteRedoActions(pTrans); + if (code == 0) { + pTrans->stage = TRN_STAGE_COMMIT; + } else if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) { + // do nothing + } else { + if (pTrans->policy == TRN_POLICY_RETRY) { + pTrans->stage = TRN_STAGE_RETRY; + } else { + pTrans->stage = TRN_STAGE_ROLLBACK; + } } } - return 0; -} + if (pTrans->stage == TRN_STAGE_COMMIT) { + code = trnExecuteCommitLogs(pTrans); + if (code == 0) { + trnDrop(pTrans); + } + } + + if (pTrans->stage == TRN_STAGE_ROLLBACK) { + } + + if (pTrans->stage == TRN_STAGE_RETRY) { + } -static int32_t trnActionUpdate(STrans *pSrcUser, STrans *pDstUser) { return 0; -} +} \ No newline at end of file diff --git a/source/dnode/mnode/transaction/src/trnThread.c b/source/dnode/mnode/transaction/src/trnThread.c deleted file mode 100644 index 6340f401b1..0000000000 --- a/source/dnode/mnode/transaction/src/trnThread.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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" -#include "tthread.h" - -static struct { - pthread_t *threadId; - bool threadRunning; -} tsTrn; - -static void *trnThreadFunc(void *param) { - while (1) { - pthread_testcancel(); - } - return NULL; -} - -int32_t trnInit() { - tsTrn.threadId = taosCreateThread(trnThreadFunc, NULL); - if (tsTrn.threadId == NULL) { - return TSDB_CODE_MND_OUT_OF_MEMORY; - } - - return 0; -} - -void trnCleanup() { - if (tsTrn.threadId) { - taosDestoryThread(tsTrn.threadId); - tsTrn.threadId = NULL; - } -} diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 2003270b21..4151178e2e 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -524,3 +524,5 @@ const char* tstrerror(int32_t err) { return ""; } + +const char* terrstr() { return tstrerror(terrno); } \ No newline at end of file From ffd3425969981868be092a2463bc637bb7f7a861 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 11 Nov 2021 11:24:00 +0800 Subject: [PATCH 13/17] minor changes --- source/dnode/mnode/transaction/src/trnInt.c | 72 ++++++++++++++++++++ source/dnode/mnode/transaction/src/trnMain.c | 71 ------------------- 2 files changed, 72 insertions(+), 71 deletions(-) diff --git a/source/dnode/mnode/transaction/src/trnInt.c b/source/dnode/mnode/transaction/src/trnInt.c index 4a69007de5..03c1a506b9 100644 --- a/source/dnode/mnode/transaction/src/trnInt.c +++ b/source/dnode/mnode/transaction/src/trnInt.c @@ -148,6 +148,78 @@ int32_t trnActionUpdate(STrans *pSrcTrans, STrans *pDstTrans) { return 0; } int32_t trnGenerateTransId() { return 1; } +STrans *trnCreate(ETrnPolicy policy) { + STrans *pTrans = calloc(1, sizeof(STrans)); + if (pTrans == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + pTrans->id = trnGenerateTransId(); + pTrans->stage = TRN_STAGE_PREPARE; + pTrans->policy = policy; + 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) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return NULL; + } + + return pTrans; +} + +static void trnDropArray(SArray *pArray) { + for (int32_t index = 0; index < pArray->size; ++index) { + SSdbRaw *pRaw = taosArrayGetP(pArray, index); + tfree(pRaw); + } + + taosArrayDestroy(pArray); +} + +void trnDrop(STrans *pTrans) { + trnDropArray(pTrans->redoLogs); + trnDropArray(pTrans->undoLogs); + trnDropArray(pTrans->commitLogs); + trnDropArray(pTrans->redoActions); + trnDropArray(pTrans->undoActions); + tfree(pTrans); +} + +static int32_t trnAppendArray(SArray *pArray, SSdbRaw *pRaw) { + if (pArray == NULL || pRaw == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return -1; + } + + void *ptr = taosArrayPush(pArray, &pRaw); + if (ptr == NULL) { + terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + return -1; + } + + return 0; +} + +int32_t trnAppendRedoLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->redoLogs, pRaw); } + +int32_t trnAppendUndoLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->undoLogs, pRaw); } + +int32_t trnAppendCommitLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->commitLogs, pRaw); } + +int32_t trnAppendRedoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) { + return trnAppendArray(pTrans->redoActions, pMsg); +} + +int32_t trnAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) { + return trnAppendArray(pTrans->undoActions, pMsg); +} + int32_t trnInit() { SSdbDesc desc = {.sdbType = SDB_TRANS, .keyType = SDB_KEY_INT32, diff --git a/source/dnode/mnode/transaction/src/trnMain.c b/source/dnode/mnode/transaction/src/trnMain.c index 3ff991a676..1f8da2689e 100644 --- a/source/dnode/mnode/transaction/src/trnMain.c +++ b/source/dnode/mnode/transaction/src/trnMain.c @@ -17,83 +17,12 @@ #include "trnInt.h" #include "trpc.h" -STrans *trnCreate(ETrnPolicy policy) { - STrans *pTrans = calloc(1, sizeof(STrans)); - if (pTrans == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; - return NULL; - } - - pTrans->id = trnGenerateTransId(); - pTrans->stage = TRN_STAGE_PREPARE; - pTrans->policy = policy; - 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) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; - return NULL; - } - - return pTrans; -} - -static void trnDropArray(SArray *pArray) { - for (int32_t index = 0; index < pArray->size; ++index) { - SSdbRaw *pRaw = taosArrayGetP(pArray, index); - tfree(pRaw); - } - - taosArrayDestroy(pArray); -} - -void trnDrop(STrans *pTrans) { - trnDropArray(pTrans->redoLogs); - trnDropArray(pTrans->undoLogs); - trnDropArray(pTrans->commitLogs); - trnDropArray(pTrans->redoActions); - trnDropArray(pTrans->undoActions); - tfree(pTrans); -} - void trnSetRpcHandle(STrans *pTrans, void *rpcHandle) { if (pTrans != NULL) { pTrans->rpcHandle = rpcHandle; } } -static int32_t trnAppendArray(SArray *pArray, SSdbRaw *pRaw) { - if (pArray == NULL || pRaw == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; - return -1; - } - - void *ptr = taosArrayPush(pArray, &pRaw); - if (ptr == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; - return -1; - } - - return 0; -} - -int32_t trnAppendRedoLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->redoLogs, pRaw); } - -int32_t trnAppendUndoLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->undoLogs, pRaw); } - -int32_t trnAppendCommitLog(STrans *pTrans, SSdbRaw *pRaw) { return trnAppendArray(pTrans->commitLogs, pRaw); } - -int32_t trnAppendRedoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) { - return trnAppendArray(pTrans->redoActions, pMsg); -} - -int32_t trnAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) { - return trnAppendArray(pTrans->undoActions, pMsg); -} int32_t trnPrepare(STrans *pTrans, int32_t (*syncfp)(SSdbRaw *pRaw, void *pData)) { if (syncfp == NULL) return -1; From 419323274ca8bc466eb450c9928b5165bff071c9 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 11 Nov 2021 14:23:12 +0800 Subject: [PATCH 14/17] minor changes --- source/dnode/mnode/transaction/src/trnInt.c | 2 + source/dnode/mnode/transaction/src/trnMain.c | 131 ++++++++++++++----- 2 files changed, 99 insertions(+), 34 deletions(-) diff --git a/source/dnode/mnode/transaction/src/trnInt.c b/source/dnode/mnode/transaction/src/trnInt.c index 03c1a506b9..f7463ec369 100644 --- a/source/dnode/mnode/transaction/src/trnInt.c +++ b/source/dnode/mnode/transaction/src/trnInt.c @@ -191,6 +191,8 @@ void trnDrop(STrans *pTrans) { tfree(pTrans); } +void trnSetRpcHandle(STrans *pTrans, void *rpcHandle) { pTrans->rpcHandle = rpcHandle; } + static int32_t trnAppendArray(SArray *pArray, SSdbRaw *pRaw) { if (pArray == NULL || pRaw == NULL) { terrno = TSDB_CODE_MND_OUT_OF_MEMORY; diff --git a/source/dnode/mnode/transaction/src/trnMain.c b/source/dnode/mnode/transaction/src/trnMain.c index 1f8da2689e..6bb46f1283 100644 --- a/source/dnode/mnode/transaction/src/trnMain.c +++ b/source/dnode/mnode/transaction/src/trnMain.c @@ -17,13 +17,6 @@ #include "trnInt.h" #include "trpc.h" -void trnSetRpcHandle(STrans *pTrans, void *rpcHandle) { - if (pTrans != NULL) { - pTrans->rpcHandle = rpcHandle; - } -} - - int32_t trnPrepare(STrans *pTrans, int32_t (*syncfp)(SSdbRaw *pRaw, void *pData)) { if (syncfp == NULL) return -1; @@ -69,58 +62,128 @@ int32_t trnApply(SSdbRaw *pRaw, void *pData, int32_t code) { return 0; } -int32_t trnExecuteRedoLogs(STrans *pTrans) {return 0;} -int32_t trnExecuteUndoLogs(STrans *pTrans) {return 0;} -int32_t trnExecuteCommitLogs(STrans *pTrans) {return 0;} -int32_t trnExecuteRedoActions(STrans *pTrans) {return 0;} -int32_t trnExecuteUndoActions(STrans *pTrans) {return 0;} -static int32_t trnPerfomRollbackStage(STrans *pTrans) { return 0; } +static int32_t trnExecuteArray(SArray *pArray) { + for (int32_t index = 0; index < pArray->size; ++index) { + SSdbRaw *pRaw = taosArrayGetP(pArray, index); + if (sdbWrite(pRaw) != 0) { + return -1; + } + } + + return 0; +} + +static int32_t trnExecuteRedoLogs(STrans *pTrans) { return trnExecuteArray(pTrans->redoLogs); } + +static int32_t trnExecuteUndoLogs(STrans *pTrans) { return trnExecuteArray(pTrans->undoLogs); } + +static int32_t trnExecuteCommitLogs(STrans *pTrans) { return trnExecuteArray(pTrans->commitLogs); } + +static int32_t trnExecuteRedoActions(STrans *pTrans) { return trnExecuteArray(pTrans->redoActions); } + +static int32_t trnExecuteUndoActions(STrans *pTrans) { return trnExecuteArray(pTrans->undoActions); } + +static int32_t trnPerformPrepareStage(STrans *pTrans) { + if (trnExecuteRedoLogs(pTrans) == 0) { + pTrans->stage = TRN_STAGE_EXECUTE; + return 0; + } else { + pTrans->stage = TRN_STAGE_ROLLBACK; + return -1; + } +} + +static int32_t trnPerformExecuteStage(STrans *pTrans) { + int32_t code = trnExecuteRedoActions(pTrans); + + if (code == 0) { + pTrans->stage = TRN_STAGE_COMMIT; + return 0; + } else if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) { + return -1; + } else { + if (pTrans->policy == TRN_POLICY_RETRY) { + pTrans->stage = TRN_STAGE_RETRY; + } else { + pTrans->stage = TRN_STAGE_ROLLBACK; + } + return 0; + } +} + +static int32_t trnPerformCommitStage(STrans *pTrans) { + if (trnExecuteCommitLogs(pTrans) == 0) { + pTrans->stage = TRN_STAGE_EXECUTE; + return 0; + } else { + pTrans->stage = TRN_STAGE_ROLLBACK; + return -1; + } +} + +static int32_t trnPerformRollbackStage(STrans *pTrans) { + if (trnExecuteCommitLogs(pTrans) == 0) { + pTrans->stage = TRN_STAGE_EXECUTE; + return 0; + } else { + pTrans->stage = TRN_STAGE_ROLLBACK; + return -1; + } +} + +static int32_t trnPerformRetryStage(STrans *pTrans) { + if (trnExecuteCommitLogs(pTrans) == 0) { + pTrans->stage = TRN_STAGE_EXECUTE; + return 0; + } else { + pTrans->stage = TRN_STAGE_ROLLBACK; + return -1; + } +} int32_t trnExecute(int32_t tranId) { int32_t code = 0; STrans *pTrans = sdbAcquire(SDB_TRANS, &tranId); if (pTrans == NULL) { - code = terrno; - return code; + return -1; } if (pTrans->stage == TRN_STAGE_PREPARE) { - code = trnExecuteRedoLogs(pTrans); - if (code == 0) { - pTrans->stage = TRN_STAGE_EXECUTE; - } else { - pTrans->stage = TRN_STAGE_ROLLBACK; + if (trnPerformPrepareStage(pTrans) != 0) { + sdbRelease(pTrans); + return -1; } } if (pTrans->stage == TRN_STAGE_EXECUTE) { - code = trnExecuteRedoActions(pTrans); - if (code == 0) { - pTrans->stage = TRN_STAGE_COMMIT; - } else if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) { - // do nothing - } else { - if (pTrans->policy == TRN_POLICY_RETRY) { - pTrans->stage = TRN_STAGE_RETRY; - } else { - pTrans->stage = TRN_STAGE_ROLLBACK; - } + if (trnPerformExecuteStage(pTrans) != 0) { + sdbRelease(pTrans); + return -1; } } if (pTrans->stage == TRN_STAGE_COMMIT) { - code = trnExecuteCommitLogs(pTrans); - if (code == 0) { - trnDrop(pTrans); + if (trnPerformCommitStage(pTrans) != 0) { + sdbRelease(pTrans); + return -1; } } if (pTrans->stage == TRN_STAGE_ROLLBACK) { + if (trnPerformRollbackStage(pTrans) != 0) { + sdbRelease(pTrans); + return -1; + } } if (pTrans->stage == TRN_STAGE_RETRY) { + if (trnPerformRetryStage(pTrans) != 0) { + sdbRelease(pTrans); + return -1; + } } + sdbRelease(pTrans); return 0; } \ No newline at end of file From c7f4f9d4e78cc5ffedc1e8dd1e97d652b6802590 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 11 Nov 2021 17:31:52 +0800 Subject: [PATCH 15/17] minor changes in mnode --- include/dnode/mnode/mnode.h | 3 +- include/dnode/mnode/sdb/sdb.h | 23 +- include/util/taoserror.h | 26 +- source/common/src/tglobal.c | 2 +- source/dnode/mgmt/src/dnodeDnode.c | 7 + source/dnode/mgmt/src/dnodeInt.c | 6 +- source/dnode/mgmt/src/dnodeMnode.c | 7 +- source/dnode/mnode/impl/inc/mnodeDef.h | 9 +- source/dnode/mnode/impl/inc/mnodeInt.h | 11 +- source/dnode/mnode/impl/inc/mnodeWorker.h | 34 -- source/dnode/mnode/impl/src/mnode.c | 392 ++++++++++++++++ source/dnode/mnode/impl/src/mnodeAcct.c | 4 +- source/dnode/mnode/impl/src/mnodeTelem.c | 3 +- source/dnode/mnode/impl/src/mnodeUser.c | 12 +- source/dnode/mnode/impl/src/mnodeWorker.c | 494 -------------------- source/dnode/mnode/impl/src/mondeInt.c | 250 ---------- source/dnode/mnode/sdb/src/sdb.c | 6 +- source/dnode/mnode/transaction/src/trnInt.c | 16 +- source/util/src/terror.c | 14 +- source/util/src/tpagedfile.c | 2 +- source/util/src/tqueue.c | 4 +- source/util/src/tstep.c | 18 +- source/util/src/ttimer.c | 2 + 23 files changed, 490 insertions(+), 855 deletions(-) delete mode 100644 source/dnode/mnode/impl/inc/mnodeWorker.h create mode 100644 source/dnode/mnode/impl/src/mnode.c delete mode 100644 source/dnode/mnode/impl/src/mnodeWorker.c delete mode 100644 source/dnode/mnode/impl/src/mondeInt.c diff --git a/include/dnode/mnode/mnode.h b/include/dnode/mnode/mnode.h index 09dd4a3f2d..20de27e59d 100644 --- a/include/dnode/mnode/mnode.h +++ b/include/dnode/mnode/mnode.h @@ -64,8 +64,7 @@ void mnodeStop(); int32_t mnodeGetLoad(SMnodeLoad *pLoad); int32_t mnodeRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey); -SMnodeMsg *mnodeInitMsg(int32_t msgNum); -int32_t mnodeAppendMsg(SMnodeMsg *pMsg, SRpcMsg *pRpcMsg); +SMnodeMsg *mnodeInitMsg(SRpcMsg *pRpcMsg); void mnodeCleanupMsg(SMnodeMsg *pMsg); void mnodeProcessMsg(SMnodeMsg *pMsg, EMnMsgType msgType); diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h index 344dc6922d..eef79fbc09 100644 --- a/include/dnode/mnode/sdb/sdb.h +++ b/include/dnode/mnode/sdb/sdb.h @@ -77,18 +77,17 @@ extern "C" { typedef enum { SDB_START = 0, SDB_TRANS = 1, - SDB_VERSION = 2, - SDB_CLUSTER = 3, - SDB_DNODE = 4, - SDB_MNODE = 5, - SDB_ACCT = 6, - SDB_AUTH = 7, - SDB_USER = 8, - SDB_DB = 9, - SDB_VGROUP = 10, - SDB_STABLE = 11, - SDB_FUNC = 12, - SDB_MAX = 13 + SDB_CLUSTER = 2, + SDB_DNODE = 3, + SDB_MNODE = 4, + SDB_ACCT = 5, + SDB_AUTH = 6, + SDB_USER = 7, + SDB_DB = 8, + SDB_VGROUP = 9, + SDB_STABLE = 10, + SDB_FUNC = 11, + SDB_MAX = 12 } ESdbType; typedef enum { SDB_ACTION_INSERT = 1, SDB_ACTION_UPDATE = 2, SDB_ACTION_DELETE = 3 } ESdbAction; diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 8c4a726bf6..9669ccb0bf 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -60,17 +60,19 @@ int32_t* taosGetErrno(); #define TSDB_CODE_RPC_INVALID_VERSION TAOS_DEF_ERROR_CODE(0, 0x0016) //"Invalid app version") //common & util -#define TSDB_CODE_COM_OPS_NOT_SUPPORT TAOS_DEF_ERROR_CODE(0, 0x0100) //"Operation not supported") -#define TSDB_CODE_COM_MEMORY_CORRUPTED TAOS_DEF_ERROR_CODE(0, 0x0101) //"Memory corrupted") -#define TSDB_CODE_COM_OUT_OF_MEMORY TAOS_DEF_ERROR_CODE(0, 0x0102) //"Out of memory") -#define TSDB_CODE_COM_INVALID_CFG_MSG TAOS_DEF_ERROR_CODE(0, 0x0103) //"Invalid config message") -#define TSDB_CODE_COM_FILE_CORRUPTED TAOS_DEF_ERROR_CODE(0, 0x0104) //"Data file corrupted") -#define TSDB_CODE_REF_NO_MEMORY TAOS_DEF_ERROR_CODE(0, 0x0105) //"Ref out of memory") -#define TSDB_CODE_REF_FULL TAOS_DEF_ERROR_CODE(0, 0x0106) //"too many Ref Objs") -#define TSDB_CODE_REF_ID_REMOVED TAOS_DEF_ERROR_CODE(0, 0x0107) //"Ref ID is removed") -#define TSDB_CODE_REF_INVALID_ID TAOS_DEF_ERROR_CODE(0, 0x0108) //"Invalid Ref ID") -#define TSDB_CODE_REF_ALREADY_EXIST TAOS_DEF_ERROR_CODE(0, 0x0109) //"Ref is already there") -#define TSDB_CODE_REF_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x010A) //"Ref is not there") +#define TSDB_CODE_OPS_NOT_SUPPORT TAOS_DEF_ERROR_CODE(0, 0x0100) +#define TSDB_CODE_OUT_OF_MEMORY TAOS_DEF_ERROR_CODE(0, 0x0101) +#define TSDB_CODE_OUT_OF_RANGE TAOS_DEF_ERROR_CODE(0, 0x0102) +#define TSDB_CODE_INVALID_PTR TAOS_DEF_ERROR_CODE(0, 0x0103) +#define TSDB_CODE_MEMORY_CORRUPTED TAOS_DEF_ERROR_CODE(0, 0x0104) +#define TSDB_CODE_FILE_CORRUPTED TAOS_DEF_ERROR_CODE(0, 0x0106) +#define TSDB_CODE_INVALID_MSG TAOS_DEF_ERROR_CODE(0, 0x0107) +#define TSDB_CODE_REF_NO_MEMORY TAOS_DEF_ERROR_CODE(0, 0x0108) +#define TSDB_CODE_REF_FULL TAOS_DEF_ERROR_CODE(0, 0x0109) +#define TSDB_CODE_REF_ID_REMOVED TAOS_DEF_ERROR_CODE(0, 0x010A) +#define TSDB_CODE_REF_INVALID_ID TAOS_DEF_ERROR_CODE(0, 0x010B) +#define TSDB_CODE_REF_ALREADY_EXIST TAOS_DEF_ERROR_CODE(0, 0x010C) +#define TSDB_CODE_REF_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x010D) //client #define TSDB_CODE_TSC_INVALID_OPERATION TAOS_DEF_ERROR_CODE(0, 0x0200) //"Invalid Operation") @@ -121,7 +123,6 @@ int32_t* taosGetErrno(); #define TSDB_CODE_MND_INVALID_MSG_LEN TAOS_DEF_ERROR_CODE(0, 0x0307) //"Invalid message length") #define TSDB_CODE_MND_INVALID_MSG_TYPE TAOS_DEF_ERROR_CODE(0, 0x0308) //"Invalid message type") #define TSDB_CODE_MND_TOO_MANY_SHELL_CONNS TAOS_DEF_ERROR_CODE(0, 0x0309) //"Too many connections") -#define TSDB_CODE_MND_OUT_OF_MEMORY TAOS_DEF_ERROR_CODE(0, 0x030A) //"Out of memory in mnode") #define TSDB_CODE_MND_INVALID_SHOWOBJ TAOS_DEF_ERROR_CODE(0, 0x030B) //"Data expired") #define TSDB_CODE_MND_INVALID_QUERY_ID TAOS_DEF_ERROR_CODE(0, 0x030C) //"Invalid query id") #define TSDB_CODE_MND_INVALID_STREAM_ID TAOS_DEF_ERROR_CODE(0, 0x030D) //"Invalid stream id") @@ -132,6 +133,7 @@ int32_t* taosGetErrno(); #define TSDB_CODE_MND_FAILED_TO_CREATE_DIR TAOS_DEF_ERROR_CODE(0, 0x0313) //"failed to create mnode dir") #define TSDB_CODE_MND_FAILED_TO_INIT_STEP TAOS_DEF_ERROR_CODE(0, 0x0314) //"failed to init components") + #define TSDB_CODE_SDB_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x0320) #define TSDB_CODE_SDB_OUT_OF_MEMORY TAOS_DEF_ERROR_CODE(0, 0x0321) #define TSDB_CODE_SDB_OBJ_ALREADY_THERE TAOS_DEF_ERROR_CODE(0, 0x0322) diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 4f30327c06..a19f6ec04b 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -40,7 +40,7 @@ uint16_t tsArbitratorPort = 6042; int32_t tsStatusInterval = 1; // second int32_t tsNumOfMnodes = 1; int8_t tsEnableVnodeBak = 1; -int8_t tsEnableTelemetryReporting = 1; +int8_t tsEnableTelemetryReporting = 0; int8_t tsArbOnline = 0; int64_t tsArbOnlineTimestamp = TSDB_ARB_DUMMY_TIME; char tsEmail[TSDB_FQDN_LEN] = {0}; diff --git a/source/dnode/mgmt/src/dnodeDnode.c b/source/dnode/mgmt/src/dnodeDnode.c index 63de2b940d..7843075b30 100644 --- a/source/dnode/mgmt/src/dnodeDnode.c +++ b/source/dnode/mgmt/src/dnodeDnode.c @@ -302,6 +302,13 @@ PRASE_DNODE_OVER: return -1; } + if (tsDnode.dnodeEps == NULL) { + tsDnode.dnodeEps = calloc(1, sizeof(SDnodeEps) + sizeof(SDnodeEp)); + tsDnode.dnodeEps->dnodeNum = 1; + tsDnode.dnodeEps->dnodeEps[0].dnodePort = tsServerPort; + tstrncpy(tsDnode.dnodeEps->dnodeEps[0].dnodeFqdn, tsLocalFqdn, TSDB_FQDN_LEN); + } + dnodeResetDnodes(tsDnode.dnodeEps); terrno = 0; diff --git a/source/dnode/mgmt/src/dnodeInt.c b/source/dnode/mgmt/src/dnodeInt.c index e7018f4265..2674e107fd 100644 --- a/source/dnode/mgmt/src/dnodeInt.c +++ b/source/dnode/mgmt/src/dnodeInt.c @@ -135,7 +135,7 @@ static int32_t dnodeInitMain() { dnodeInitDir(); - return -1; + return 0; } static void dnodeCleanupMain() { @@ -145,14 +145,14 @@ static void dnodeCleanupMain() { } int32_t dnodeInit() { - SSteps *steps = taosStepInit(24, dnodeReportStartup); + SSteps *steps = taosStepInit(10, dnodeReportStartup); if (steps == NULL) return -1; taosStepAdd(steps, "dnode-main", dnodeInitMain, dnodeCleanupMain); taosStepAdd(steps, "dnode-rpc", rpcInit, rpcCleanup); taosStepAdd(steps, "dnode-tfs", NULL, NULL); taosStepAdd(steps, "dnode-wal", walInit, walCleanUp); - taosStepAdd(steps, "dnode-sync", syncInit, syncCleanUp); + //taosStepAdd(steps, "dnode-sync", syncInit, syncCleanUp); taosStepAdd(steps, "dnode-dnode", dnodeInitDnode, dnodeCleanupDnode); taosStepAdd(steps, "dnode-vnodes", dnodeInitVnodes, dnodeCleanupVnodes); taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, dnodeCleanupMnode); diff --git a/source/dnode/mgmt/src/dnodeMnode.c b/source/dnode/mgmt/src/dnodeMnode.c index e5a758899e..f6726bf981 100644 --- a/source/dnode/mgmt/src/dnodeMnode.c +++ b/source/dnode/mgmt/src/dnodeMnode.c @@ -335,12 +335,9 @@ static int32_t dnodeWriteMnodeMsgToQueue(taos_queue pQueue, SRpcMsg *pRpcMsg) { if (pQueue == NULL) { code = TSDB_CODE_DND_MSG_NOT_PROCESSED; } else { - SMnodeMsg *pMsg = mnodeInitMsg(1); + SMnodeMsg *pMsg = mnodeInitMsg(pRpcMsg); if (pMsg == NULL) { - code = TSDB_CODE_DND_OUT_OF_MEMORY; - } else { - mnodeAppendMsg(pMsg, pRpcMsg); - code = taosWriteQitem(pQueue, pMsg); + code = terrno; } } diff --git a/source/dnode/mnode/impl/inc/mnodeDef.h b/source/dnode/mnode/impl/inc/mnodeDef.h index 49c74283ae..b6449ecfe7 100644 --- a/source/dnode/mnode/impl/inc/mnodeDef.h +++ b/source/dnode/mnode/impl/inc/mnodeDef.h @@ -57,7 +57,6 @@ typedef struct SVgObj SVgObj; typedef struct SSTableObj SSTableObj; typedef struct SFuncObj SFuncObj; typedef struct SOperObj SOperObj; -typedef struct SMnMsg SMnMsg; typedef enum { MN_AUTH_ACCT_START = 0, @@ -265,9 +264,9 @@ typedef struct { void *rsp; } SMnRsp; -typedef struct SMnMsg { - void (*fp)(SMnMsg *pMsg, int32_t code); - char user[TSDB_USER_LEN]; +typedef struct SMnodeMsg { + void (*fp)(SMnodeMsg *pMsg, int32_t code); + SRpcConnInfo conn; SUserObj *pUser; int16_t received; int16_t successed; @@ -278,7 +277,7 @@ typedef struct SMnMsg { SMnRsp rpcRsp; SRpcMsg rpcMsg; char pCont[]; -} SMnReq; +} SMnodeMsg; #ifdef __cplusplus } diff --git a/source/dnode/mnode/impl/inc/mnodeInt.h b/source/dnode/mnode/impl/inc/mnodeInt.h index 11f8f461ba..95f0acf52f 100644 --- a/source/dnode/mnode/impl/inc/mnodeInt.h +++ b/source/dnode/mnode/impl/inc/mnodeInt.h @@ -24,17 +24,18 @@ extern "C" { #endif -typedef enum { MN_STATUS_UNINIT = 0, MN_STATUS_INIT = 1, MN_STATUS_READY = 2, MN_STATUS_CLOSING = 3 } EMnStatus; +typedef void (*MnodeRpcFp[TSDB_MSG_TYPE_MAX])(SMnodeMsg *pMsg); -tmr_h mnodeGetTimer(); -int32_t mnodeGetDnodeId(); -int64_t mnodeGetClusterId(); -EMnStatus mnodeGetStatus(); +tmr_h mnodeGetTimer(); +int32_t mnodeGetDnodeId(); +int64_t mnodeGetClusterId(); void mnodeSendMsgToDnode(struct SEpSet *epSet, struct SRpcMsg *rpcMsg); void mnodeSendMsgToMnode(struct SRpcMsg *rpcMsg); void mnodeSendRedirectMsg(struct SRpcMsg *rpcMsg, bool forShell); +void mnodeSetMsgFp(int32_t msgType, MnodeRpcFp fp); + #ifdef __cplusplus } #endif diff --git a/source/dnode/mnode/impl/inc/mnodeWorker.h b/source/dnode/mnode/impl/inc/mnodeWorker.h deleted file mode 100644 index 8477af6b72..0000000000 --- a/source/dnode/mnode/impl/inc/mnodeWorker.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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_MNODE_WORKER_H_ -#define _TD_MNODE_WORKER_H_ - -#include "mnodeInt.h" - -#ifdef __cplusplus -extern "C" { -#endif - -int32_t mnodeInitWorker(); -void mnodeCleanupWorker(); -void mnodeSendRsp(SMnMsg *pMsg, int32_t code); -void mnodeReDispatchToWriteQueue(SMnMsg *pMsg); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_MNODE_WORKER_H_*/ diff --git a/source/dnode/mnode/impl/src/mnode.c b/source/dnode/mnode/impl/src/mnode.c new file mode 100644 index 0000000000..93cd85a888 --- /dev/null +++ b/source/dnode/mnode/impl/src/mnode.c @@ -0,0 +1,392 @@ +/* + * 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 "os.h" +#include "tglobal.h" +#include "tstep.h" +#include "tqueue.h" +#include "mnodeAcct.h" +#include "mnodeAuth.h" +#include "mnodeBalance.h" +#include "mnodeCluster.h" +#include "mnodeDb.h" +#include "mnodeDnode.h" +#include "mnodeFunc.h" +#include "mnodeMnode.h" +#include "mnodeOper.h" +#include "mnodeProfile.h" +#include "mnodeShow.h" +#include "mnodeStable.h" +#include "mnodeSync.h" +#include "mnodeTelem.h" +#include "mnodeUser.h" +#include "mnodeVgroup.h" + +static struct { + int32_t dnodeId; + int64_t clusterId; + tmr_h timer; + SSteps *pInitSteps; + SSteps *pStartSteps; + SMnodePara para; + MnodeRpcFp msgFp; +} tsMint; + +int32_t mnodeGetDnodeId() { return tsMint.para.dnodeId; } + +int64_t mnodeGetClusterId() { return tsMint.para.clusterId; } + +void mnodeSendMsgToDnode(struct SEpSet *epSet, struct SRpcMsg *rpcMsg) { (*tsMint.para.SendMsgToDnode)(epSet, rpcMsg); } + +void mnodeSendMsgToMnode(struct SRpcMsg *rpcMsg) { return (*tsMint.para.SendMsgToMnode)(rpcMsg); } + +void mnodeSendRedirectMsg(struct SRpcMsg *rpcMsg, bool forShell) { (*tsMint.para.SendRedirectMsg)(rpcMsg, forShell); } + +static int32_t mnodeInitTimer() { + if (tsMint.timer == NULL) { + tsMint.timer = taosTmrInit(tsMaxShellConns, 200, 3600000, "MND"); + } + + if (tsMint.timer == NULL) { + return -1; + } + + return 0; +} + +static void mnodeCleanupTimer() { + if (tsMint.timer != NULL) { + taosTmrCleanUp(tsMint.timer); + tsMint.timer = NULL; + } +} + +tmr_h mnodeGetTimer() { return tsMint.timer; } + +static int32_t mnodeSetPara(SMnodePara para) { + tsMint.para = para; + + if (tsMint.para.SendMsgToDnode == NULL) { + terrno = TSDB_CODE_MND_APP_ERROR; + return -1; + } + + if (tsMint.para.SendMsgToMnode == NULL) { + terrno = TSDB_CODE_MND_APP_ERROR; + return -1; + } + + if (tsMint.para.SendRedirectMsg == NULL) { + terrno = TSDB_CODE_MND_APP_ERROR; + return -1; + } + + if (tsMint.para.PutMsgIntoApplyQueue == NULL) { + terrno = TSDB_CODE_MND_APP_ERROR; + return -1; + } + + if (tsMint.para.dnodeId < 0) { + terrno = TSDB_CODE_MND_APP_ERROR; + return -1; + } + + if (tsMint.para.clusterId < 0) { + terrno = TSDB_CODE_MND_APP_ERROR; + return -1; + } + + return 0; +} + +static int32_t mnodeAllocInitSteps() { + struct SSteps *steps = taosStepInit(16, NULL); + if (steps == NULL) return -1; + + if (taosStepAdd(steps, "mnode-trans", trnInit, trnCleanup) != 0) return -1; + if (taosStepAdd(steps, "mnode-cluster", mnodeInitCluster, mnodeCleanupCluster) != 0) return -1; + if (taosStepAdd(steps, "mnode-dnode", mnodeInitDnode, mnodeCleanupDnode) != 0) return -1; + if (taosStepAdd(steps, "mnode-mnode", mnodeInitMnode, mnodeCleanupMnode) != 0) return -1; + if (taosStepAdd(steps, "mnode-acct", mnodeInitAcct, mnodeCleanupAcct) != 0) return -1; + if (taosStepAdd(steps, "mnode-auth", mnodeInitAuth, mnodeCleanupAuth) != 0) return -1; + if (taosStepAdd(steps, "mnode-user", mnodeInitUser, mnodeCleanupUser) != 0) return -1; + if (taosStepAdd(steps, "mnode-db", mnodeInitDb, mnodeCleanupDb) != 0) return -1; + if (taosStepAdd(steps, "mnode-vgroup", mnodeInitVgroup, mnodeCleanupVgroup) != 0) return -1; + if (taosStepAdd(steps, "mnode-stable", mnodeInitStable, mnodeCleanupStable) != 0) return -1; + if (taosStepAdd(steps, "mnode-func", mnodeInitFunc, mnodeCleanupFunc) != 0) return -1; + if (taosStepAdd(steps, "mnode-sdb", sdbInit, sdbCleanup) != 0) return -1; + + tsMint.pInitSteps = steps; + return 0; +} + +static int32_t mnodeAllocStartSteps() { + struct SSteps *steps = taosStepInit(7, NULL); + if (steps == NULL) return -1; + + taosStepAdd(steps, "mnode-timer", mnodeInitTimer, NULL); + taosStepAdd(steps, "mnode-balance", mnodeInitBalance, mnodeCleanupBalance); + taosStepAdd(steps, "mnode-profile", mnodeInitProfile, mnodeCleanupProfile); + taosStepAdd(steps, "mnode-show", mnodeInitShow, mnodeCleanUpShow); + taosStepAdd(steps, "mnode-sync", mnodeInitSync, mnodeCleanUpSync); + taosStepAdd(steps, "mnode-telem", mnodeInitTelem, mnodeCleanupTelem); + taosStepAdd(steps, "mnode-timer", NULL, mnodeCleanupTimer); + + tsMint.pStartSteps = steps; + return 0; +} + +int32_t mnodeInit(SMnodePara para) { + if (mnodeSetPara(para) != 0) { + mError("failed to init mnode para since %s", terrstr()); + return -1; + } + + if (mnodeAllocInitSteps() != 0) { + mError("failed to alloc init steps since %s", terrstr()); + return -1; + } + + if (mnodeAllocStartSteps() != 0) { + mError("failed to alloc start steps since %s", terrstr()); + return -1; + } + + return taosStepExec(tsMint.pInitSteps); +} + +void mnodeCleanup() { taosStepCleanup(tsMint.pInitSteps); } + +int32_t mnodeDeploy(char *path, SMnodeCfg *pCfg) { + if (tsMint.para.dnodeId <= 0 && tsMint.para.clusterId <= 0) { + if (sdbDeploy() != 0) { + mError("failed to deploy sdb since %s", terrstr()); + return -1; + } + } + + mDebug("mnode is deployed"); + return 0; +} + +void mnodeUnDeploy(char *path) { sdbUnDeploy(); } + +int32_t mnodeStart(char *path, SMnodeCfg *pCfg) { return taosStepExec(tsMint.pStartSteps); } + +int32_t mnodeAlter(SMnodeCfg *pCfg) { return 0; } + +void mnodeStop() { taosStepCleanup(tsMint.pStartSteps); } + +int32_t mnodeGetLoad(SMnodeLoad *pLoad) { return 0; } + +SMnodeMsg *mnodeInitMsg(SRpcMsg *pRpcMsg) { + SMnodeMsg *pMsg = taosAllocateQitem(sizeof(SMnodeMsg)); + if (pMsg == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } + + if (rpcGetConnInfo(pRpcMsg->handle, &pMsg->conn) != 0) { + mnodeCleanupMsg(pMsg); + mError("can not get user from conn:%p", pMsg->rpcMsg.handle); + terrno = TSDB_CODE_MND_NO_USER_FROM_CONN; + return NULL; + } + + pMsg->rpcMsg = *pRpcMsg; + pMsg->createdTime = taosGetTimestampSec(); + + return pMsg; +} + +void mnodeCleanupMsg(SMnodeMsg *pMsg) { + if (pMsg->pUser != NULL) { + sdbRelease(pMsg->pUser); + } + + taosFreeQitem(pMsg); +} + +static void mnodeProcessRpcMsg(SMnodeMsg *pMsg) { + int32_t msgType = pMsg->rpcMsg.msgType; + + if (tsMint.msgFp[msgType] == NULL) { + } + + (*tsMint.msgFp[msgType])(pMsg); +} + +void mnodeSetMsgFp(int32_t msgType, MnodeRpcFp fp) { + if (msgType > 0 || msgType < TSDB_MSG_TYPE_MAX) { + tsMint.msgFp[msgType] = fp; + } +} + +void mnodeProcessMsg(SMnodeMsg *pMsg, EMnMsgType msgType) { + if (!mnodeIsMaster()) { + mnodeSendRedirectMsg(&pMsg->rpcMsg, true); + mnodeCleanupMsg(pMsg); + return; + } + + switch (msgType) { + case MN_MSG_TYPE_READ: + case MN_MSG_TYPE_WRITE: + case MN_MSG_TYPE_SYNC: + mnodeProcessRpcMsg(pMsg); + break; + case MN_MSG_TYPE_APPLY: + break; + default: + break; + } +} + +#if 0 + +static void mnodeProcessWriteReq(SMnodeMsg *pMsg, void *unused) { + int32_t msgType = pMsg->rpcMsg.msgType; + void *ahandle = pMsg->rpcMsg.ahandle; + int32_t code = 0; + + if (pMsg->rpcMsg.pCont == NULL) { + mError("msg:%p, app:%p type:%s content is null", pMsg, ahandle, taosMsg[msgType]); + code = TSDB_CODE_MND_INVALID_MSG_LEN; + goto PROCESS_WRITE_REQ_END; + } + + if (!mnodeIsMaster()) { + SMnRsp *rpcRsp = &pMsg->rpcRsp; + SEpSet *epSet = rpcMallocCont(sizeof(SEpSet)); + mnodeGetMnodeEpSetForShell(epSet, true); + rpcRsp->rsp = epSet; + rpcRsp->len = sizeof(SEpSet); + + mDebug("msg:%p, app:%p type:%s in write queue, is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, + taosMsg[msgType], epSet->numOfEps, epSet->inUse); + + code = TSDB_CODE_RPC_REDIRECT; + goto PROCESS_WRITE_REQ_END; + } + + if (tsMworker.writeMsgFp[msgType] == NULL) { + mError("msg:%p, app:%p type:%s not processed", pMsg, ahandle, taosMsg[msgType]); + code = TSDB_CODE_MND_MSG_NOT_PROCESSED; + goto PROCESS_WRITE_REQ_END; + } + + code = (*tsMworker.writeMsgFp[msgType])(pMsg); + +PROCESS_WRITE_REQ_END: + mnodeSendRsp(pMsg, code); +} + +static void mnodeProcessReadReq(SMnodeMsg *pMsg, void *unused) { + int32_t msgType = pMsg->rpcMsg.msgType; + void *ahandle = pMsg->rpcMsg.ahandle; + int32_t code = 0; + + if (pMsg->rpcMsg.pCont == NULL) { + mError("msg:%p, app:%p type:%s in mread queue, content is null", pMsg, ahandle, taosMsg[msgType]); + code = TSDB_CODE_MND_INVALID_MSG_LEN; + goto PROCESS_READ_REQ_END; + } + + if (!mnodeIsMaster()) { + SMnRsp *rpcRsp = &pMsg->rpcRsp; + SEpSet *epSet = rpcMallocCont(sizeof(SEpSet)); + if (!epSet) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto PROCESS_READ_REQ_END; + } + mnodeGetMnodeEpSetForShell(epSet, true); + rpcRsp->rsp = epSet; + rpcRsp->len = sizeof(SEpSet); + + mDebug("msg:%p, app:%p type:%s in mread queue is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, taosMsg[msgType], + epSet->numOfEps, epSet->inUse); + code = TSDB_CODE_RPC_REDIRECT; + goto PROCESS_READ_REQ_END; + } + + if (tsMworker.readMsgFp[msgType] == NULL) { + mError("msg:%p, app:%p type:%s in mread queue, not processed", pMsg, ahandle, taosMsg[msgType]); + code = TSDB_CODE_MND_MSG_NOT_PROCESSED; + goto PROCESS_READ_REQ_END; + } + + mTrace("msg:%p, app:%p type:%s will be processed in mread queue", pMsg, ahandle, taosMsg[msgType]); + code = (*tsMworker.readMsgFp[msgType])(pMsg); + +PROCESS_READ_REQ_END: + mnodeSendRsp(pMsg, code); +} + +static void mnodeProcessPeerReq(SMnodeMsg *pMsg, void *unused) { + int32_t msgType = pMsg->rpcMsg.msgType; + void *ahandle = pMsg->rpcMsg.ahandle; + int32_t code = 0; + + if (pMsg->rpcMsg.pCont == NULL) { + mError("msg:%p, ahandle:%p type:%s in mpeer queue, content is null", pMsg, ahandle, taosMsg[msgType]); + code = TSDB_CODE_MND_INVALID_MSG_LEN; + goto PROCESS_PEER_REQ_END; + } + + if (!mnodeIsMaster()) { + SMnRsp *rpcRsp = &pMsg->rpcRsp; + SEpSet *epSet = rpcMallocCont(sizeof(SEpSet)); + mnodeGetMnodeEpSetForPeer(epSet, true); + rpcRsp->rsp = epSet; + rpcRsp->len = sizeof(SEpSet); + + mDebug("msg:%p, ahandle:%p type:%s in mpeer queue is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, + taosMsg[msgType], epSet->numOfEps, epSet->inUse); + + code = TSDB_CODE_RPC_REDIRECT; + goto PROCESS_PEER_REQ_END; + } + + if (tsMworker.peerReqFp[msgType] == NULL) { + mError("msg:%p, ahandle:%p type:%s in mpeer queue, not processed", pMsg, ahandle, taosMsg[msgType]); + code = TSDB_CODE_MND_MSG_NOT_PROCESSED; + goto PROCESS_PEER_REQ_END; + } + + code = (*tsMworker.peerReqFp[msgType])(pMsg); + +PROCESS_PEER_REQ_END: + mnodeSendRsp(pMsg, code); +} + +static void mnodeProcessPeerRsp(SMnodeMsg *pMsg, void *unused) { + int32_t msgType = pMsg->rpcMsg.msgType; + SRpcMsg *pRpcMsg = &pMsg->rpcMsg; + + if (!mnodeIsMaster()) { + mError("msg:%p, ahandle:%p type:%s not processed for not master", pRpcMsg, pRpcMsg->ahandle, taosMsg[msgType]); + mnodeCleanupMsg2(pMsg); + } + + if (tsMworker.peerRspFp[msgType]) { + (*tsMworker.peerRspFp[msgType])(pRpcMsg); + } else { + mError("msg:%p, ahandle:%p type:%s is not processed", pRpcMsg, pRpcMsg->ahandle, taosMsg[msgType]); + } + + mnodeCleanupMsg2(pMsg); +} +#endif \ No newline at end of file diff --git a/source/dnode/mnode/impl/src/mnodeAcct.c b/source/dnode/mnode/impl/src/mnodeAcct.c index e4538fa391..251fcf3edc 100644 --- a/source/dnode/mnode/impl/src/mnodeAcct.c +++ b/source/dnode/mnode/impl/src/mnodeAcct.c @@ -21,7 +21,7 @@ static SSdbRaw *mnodeAcctActionEncode(SAcctObj *pAcct) { SSdbRaw *pRaw = calloc(1, sizeof(SAcctObj) + sizeof(SSdbRaw)); if (pRaw == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -53,7 +53,7 @@ static SAcctObj *mnodeAcctActionDecode(SSdbRaw *pRaw) { SAcctObj *pAcct = calloc(1, sizeof(SAcctObj)); if (pAcct == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } diff --git a/source/dnode/mnode/impl/src/mnodeTelem.c b/source/dnode/mnode/impl/src/mnodeTelem.c index a3977f5b17..ef1ac10eb6 100644 --- a/source/dnode/mnode/impl/src/mnodeTelem.c +++ b/source/dnode/mnode/impl/src/mnodeTelem.c @@ -17,6 +17,7 @@ #include "mnodeTelem.h" #include "tbuffer.h" #include "tglobal.h" +#include "mnodeSync.h" #define TELEMETRY_SERVER "telemetry.taosdata.com" #define TELEMETRY_PORT 80 @@ -255,7 +256,7 @@ static void* mnodeTelemThreadFp(void* param) { if (r == 0) break; if (r != ETIMEDOUT) continue; - if (mnodeGetStatus() == MN_STATUS_READY) { + if (mnodeIsMaster()) { mnodeSendTelemetryReport(); } end.tv_sec += REPORT_INTERVAL; diff --git a/source/dnode/mnode/impl/src/mnodeUser.c b/source/dnode/mnode/impl/src/mnodeUser.c index 3b5d40d807..e7b03b8372 100644 --- a/source/dnode/mnode/impl/src/mnodeUser.c +++ b/source/dnode/mnode/impl/src/mnodeUser.c @@ -24,7 +24,7 @@ static SSdbRaw *mnodeUserActionEncode(SUserObj *pUser) { SSdbRaw *pRaw = calloc(1, sizeof(SUserObj) + sizeof(SSdbRaw)); if (pRaw == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -51,7 +51,7 @@ static SUserObj *mnodeUserActionDecode(SSdbRaw *pRaw) { SUserObj *pUser = calloc(1, sizeof(SUserObj)); if (pUser == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -77,7 +77,7 @@ static SUserObj *mnodeUserActionDecode(SSdbRaw *pRaw) { static int32_t mnodeUserActionInsert(SUserObj *pUser) { pUser->prohibitDbHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); if (pUser->prohibitDbHash == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } @@ -145,7 +145,7 @@ static int32_t mnodeCreateDefaultUsers() { return 0; } -static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnMsg *pMsg) { +static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnodeMsg *pMsg) { SUserObj userObj = {0}; tstrncpy(userObj.user, user, TSDB_USER_LEN); tstrncpy(userObj.acct, acct, TSDB_USER_LEN); @@ -192,7 +192,7 @@ static int32_t mnodeCreateUser(char *acct, char *user, char *pass, SMnMsg *pMsg) return 0; } -static int32_t mnodeProcessCreateUserMsg(SMnMsg *pMsg) { +static int32_t mnodeProcessCreateUserMsg(SMnodeMsg *pMsg) { SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont; if (pCreate->user[0] == 0) { @@ -215,7 +215,7 @@ static int32_t mnodeProcessCreateUserMsg(SMnMsg *pMsg) { return -1; } - SUserObj *pOperUser = sdbAcquire(SDB_USER, pMsg->user); + SUserObj *pOperUser = sdbAcquire(SDB_USER, pMsg->conn.user); if (pOperUser == NULL) { terrno = TSDB_CODE_MND_NO_USER_FROM_CONN; mError("user:%s, failed to create since %s", pCreate->user, terrstr()); diff --git a/source/dnode/mnode/impl/src/mnodeWorker.c b/source/dnode/mnode/impl/src/mnodeWorker.c deleted file mode 100644 index cf2f415b4d..0000000000 --- a/source/dnode/mnode/impl/src/mnodeWorker.c +++ /dev/null @@ -1,494 +0,0 @@ -/* - * 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 "os.h" -#include "tworker.h" -#include "tglobal.h" -#include "mnodeMnode.h" -#include "mnodeInt.h" -#include "mnodeShow.h" -#include "mnodeSync.h" -#include "mnodeWorker.h" - -static struct { - SWorkerPool read; - SWorkerPool write; - SWorkerPool peerReq; - SWorkerPool peerRsp; - taos_queue readQ; - taos_queue writeQ; - taos_queue peerReqQ; - taos_queue peerRspQ; - int32_t (*writeMsgFp[TSDB_MSG_TYPE_MAX])(SMnMsg *); - int32_t (*readMsgFp[TSDB_MSG_TYPE_MAX])(SMnMsg *); - int32_t (*peerReqFp[TSDB_MSG_TYPE_MAX])(SMnMsg *); - void (*peerRspFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *); - void (*msgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *pMsg); -} tsMworker = {0}; - -static SMnMsg *mnodeInitMsg2(SRpcMsg *pRpcMsg) { - int32_t size = sizeof(SMnMsg) + pRpcMsg->contLen; - SMnMsg *pMsg = taosAllocateQitem(size); - - pMsg->rpcMsg = *pRpcMsg; - pMsg->rpcMsg.pCont = pMsg->pCont; - pMsg->createdTime = taosGetTimestampSec(); - memcpy(pMsg->pCont, pRpcMsg->pCont, pRpcMsg->contLen); - - SRpcConnInfo connInfo = {0}; - if (rpcGetConnInfo(pMsg->rpcMsg.handle, &connInfo) == 0) { - pMsg->pUser = sdbAcquire(SDB_USER, connInfo.user); - } - - if (pMsg->pUser == NULL) { - mError("can not get user from conn:%p", pMsg->rpcMsg.handle); - taosFreeQitem(pMsg); - return NULL; - } - - return pMsg; -} - -static void mnodeCleanupMsg2(SMnMsg *pMsg) { - if (pMsg == NULL) return; - if (pMsg->rpcMsg.pCont != pMsg->pCont) { - tfree(pMsg->rpcMsg.pCont); - } - - taosFreeQitem(pMsg); -} - -static void mnodeDispatchToWriteQueue(SRpcMsg *pRpcMsg) { - if (mnodeGetStatus() != MN_STATUS_READY || tsMworker.writeQ == NULL) { - mnodeSendRedirectMsg(pRpcMsg, true); - } else { - SMnMsg *pMsg = mnodeInitMsg2(pRpcMsg); - if (pMsg == NULL) { - SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_USER_NOT_EXIST}; - rpcSendResponse(&rpcRsp); - } else { - mTrace("msg:%p, app:%p type:%s is put into wqueue", pMsg, pMsg->rpcMsg.ahandle, taosMsg[pMsg->rpcMsg.msgType]); - taosWriteQitem(tsMworker.writeQ, pMsg); - } - } - - rpcFreeCont(pRpcMsg->pCont); -} - -void mnodeReDispatchToWriteQueue(SMnMsg *pMsg) { - if (mnodeGetStatus() != MN_STATUS_READY || tsMworker.writeQ == NULL) { - mnodeSendRedirectMsg(&pMsg->rpcMsg, true); - mnodeCleanupMsg2(pMsg); - } else { - taosWriteQitem(tsMworker.writeQ, pMsg); - } -} - -static void mnodeDispatchToReadQueue(SRpcMsg *pRpcMsg) { - if (mnodeGetStatus() != MN_STATUS_READY || tsMworker.readQ == NULL) { - mnodeSendRedirectMsg(pRpcMsg, true); - } else { - SMnMsg *pMsg = mnodeInitMsg2(pRpcMsg); - if (pMsg == NULL) { - SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_USER_NOT_EXIST}; - rpcSendResponse(&rpcRsp); - } else { - mTrace("msg:%p, app:%p type:%s is put into rqueue", pMsg, pMsg->rpcMsg.ahandle, taosMsg[pMsg->rpcMsg.msgType]); - taosWriteQitem(tsMworker.readQ, pMsg); - } - } - - rpcFreeCont(pRpcMsg->pCont); -} - -static void mnodeDispatchToPeerQueue(SRpcMsg *pRpcMsg) { - if (mnodeGetStatus() != MN_STATUS_READY || tsMworker.peerReqQ == NULL) { - mnodeSendRedirectMsg(pRpcMsg, false); - } else { - SMnMsg *pMsg = mnodeInitMsg2(pRpcMsg); - if (pMsg == NULL) { - SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_USER_NOT_EXIST}; - rpcSendResponse(&rpcRsp); - } else { - mTrace("msg:%p, app:%p type:%s is put into peer req queue", pMsg, pMsg->rpcMsg.ahandle, - taosMsg[pMsg->rpcMsg.msgType]); - taosWriteQitem(tsMworker.peerReqQ, pMsg); - } - } - - rpcFreeCont(pRpcMsg->pCont); -} - -void mnodeDispatchToPeerRspQueue(SRpcMsg *pRpcMsg) { - SMnMsg *pMsg = mnodeInitMsg2(pRpcMsg); - if (pMsg == NULL) { - SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = TSDB_CODE_MND_USER_NOT_EXIST}; - rpcSendResponse(&rpcRsp); - } else { - mTrace("msg:%p, app:%p type:%s is put into peer rsp queue", pMsg, pMsg->rpcMsg.ahandle, - taosMsg[pMsg->rpcMsg.msgType]); - taosWriteQitem(tsMworker.peerRspQ, pMsg); - } - - // rpcFreeCont(pRpcMsg->pCont); -} - -void mnodeSendRsp(SMnMsg *pMsg, int32_t code) { - if (pMsg == NULL) return; - if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) return; - if (code == TSDB_CODE_MND_ACTION_NEED_REPROCESSED) { - mnodeReDispatchToWriteQueue(pMsg); - return; - } - - SRpcMsg rpcRsp = { - .handle = pMsg->rpcMsg.handle, - .pCont = pMsg->rpcRsp.rsp, - .contLen = pMsg->rpcRsp.len, - .code = code, - }; - - rpcSendResponse(&rpcRsp); - mnodeCleanupMsg2(pMsg); -} - -static void mnodeInitMsgFp() { -// // peer req -// tsMworker.msgFp[TSDB_MSG_TYPE_DM_CONFIG_TABLE] = mnodeDispatchToPeerQueue; -// tsMworker.peerReqFp[TSDB_MSG_TYPE_DM_CONFIG_TABLE] = mnodeProcessTableCfgMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_DM_CONFIG_VNODE] = mnodeDispatchToPeerQueue; -// tsMworker.peerReqFp[TSDB_MSG_TYPE_DM_CONFIG_VNODE] = mnodeProcessVnodeCfgMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_AUTH] = mnodeDispatchToPeerQueue; -// tsMworker.peerReqFp[TSDB_MSG_TYPE_AUTH] = mnodeProcessAuthMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_GRANT] = mnodeDispatchToPeerQueue; -// // tsMworker.peerReqFp[TSDB_MSG_TYPE_GRANT] = grantProcessMsgInMgmt; -// tsMworker.msgFp[TSDB_MSG_TYPE_STATUS] = mnodeDispatchToPeerQueue; -// tsMworker.peerReqFp[TSDB_MSG_TYPE_STATUS] = mnodeProcessDnodeStatusMsg; - -// // peer rsp -// tsMworker.msgFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP] = mnodeDispatchToPeerRspQueue; -// tsMworker.peerRspFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP] = mnodeProcessCfgDnodeMsgRsp; - -// tsMworker.msgFp[TSDB_MSG_TYPE_MD_DROP_STABLE_RSP] = mnodeDispatchToPeerRspQueue; -// tsMworker.peerRspFp[TSDB_MSG_TYPE_MD_DROP_STABLE_RSP] = mnodeProcessDropSuperTableRsp; -// tsMworker.msgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE_RSP] = mnodeDispatchToPeerRspQueue; -// tsMworker.peerRspFp[TSDB_MSG_TYPE_MD_CREATE_TABLE_RSP] = mnodeProcessCreateChildTableRsp; -// tsMworker.msgFp[TSDB_MSG_TYPE_MD_DROP_TABLE_RSP] = mnodeDispatchToPeerRspQueue; -// tsMworker.peerRspFp[TSDB_MSG_TYPE_MD_DROP_TABLE_RSP] = mnodeProcessDropChildTableRsp; -// tsMworker.msgFp[TSDB_MSG_TYPE_MD_ALTER_TABLE_RSP] = mnodeDispatchToPeerRspQueue; -// tsMworker.peerRspFp[TSDB_MSG_TYPE_MD_ALTER_TABLE_RSP] = mnodeProcessAlterTableRsp; - -// tsMworker.msgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE_RSP] = mnodeDispatchToPeerRspQueue; -// tsMworker.peerRspFp[TSDB_MSG_TYPE_MD_CREATE_VNODE_RSP] = mnodeProcessCreateVnodeRsp; -// tsMworker.msgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE_RSP] = mnodeDispatchToPeerRspQueue; -// tsMworker.peerRspFp[TSDB_MSG_TYPE_MD_ALTER_VNODE_RSP] = mnodeProcessAlterVnodeRsp; -// tsMworker.msgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE_RSP] = mnodeDispatchToPeerRspQueue; -// tsMworker.peerRspFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE_RSP] = mnodeProcessCompactVnodeRsp; -// tsMworker.msgFp[TSDB_MSG_TYPE_MD_DROP_VNODE_RSP] = mnodeDispatchToPeerRspQueue; -// tsMworker.peerRspFp[TSDB_MSG_TYPE_MD_DROP_VNODE_RSP] = mnodeProcessDropVnodeRsp; - -// // read msg -// tsMworker.msgFp[TSDB_MSG_TYPE_HEARTBEAT] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_HEARTBEAT] = mnodeProcessHeartBeatMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CONNECT] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_CONNECT] = mnodeProcessConnectMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_USE_DB] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_USE_DB] = mnodeProcessUseMsg; - -// tsMworker.msgFp[TSDB_MSG_TYPE_TABLE_META] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_TABLE_META] = mnodeProcessTableMetaMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_TABLES_META] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_TABLES_META] = mnodeProcessMultiTableMetaMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = mnodeProcessSuperTableVgroupMsg; - -// tsMworker.msgFp[TSDB_MSG_TYPE_SHOW] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_SHOW] = mnodeProcessShowMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE] = mnodeProcessRetrieveMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_RETRIEVE_FUNC] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_RETRIEVE_FUNC] = mnodeProcessRetrieveFuncReq; - -// // tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_ACCT] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_CREATE_ACCT] = acctProcessCreateAcctMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_ACCT] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_ALTER_ACCT] = acctProcessDropAcctMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_DROP_ACCT] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_DROP_ACCT] = acctProcessAlterAcctMsg; - -// // write msg -// tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_USER] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CREATE_USER] = mnodeProcessCreateUserMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_USER] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_ALTER_USER] = mnodeProcessAlterUserMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_DROP_USER] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_DROP_USER] = mnodeProcessDropUserMsg; - -// tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_DNODE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CREATE_DNODE] = mnodeProcessCreateDnodeMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_DROP_DNODE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_DROP_DNODE] = mnodeProcessDropDnodeMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CONFIG_DNODE] = mnodeProcessCfgDnodeMsg; - -// tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_DB] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CREATE_DB] = mnodeProcessCreateDbMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_DB] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_ALTER_DB] = mnodeProcessAlterDbMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_DROP_DB] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_DROP_DB] = mnodeProcessDropDbMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_SYNC_DB] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_SYNC_DB] = mnodeProcessSyncDbMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_COMPACT_VNODE] = mnodeProcessCompactMsg; - -// tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_FUNCTION] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CREATE_FUNCTION] = mnodeProcessCreateFuncMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_DROP_FUNCTION] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_DROP_FUNCTION] = mnodeProcessDropFuncMsg; - -// // tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_TP] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_CREATE_TP] = tpProcessCreateTpMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_DROP_TP] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_DROP_TP] = tpProcessAlterTpMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_TP] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_ALTER_TP] = tpProcessDropTpMsg; - -// tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_TABLE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CREATE_TABLE] = mnodeProcessCreateTableMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_DROP_TABLE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_DROP_TABLE] = mnodeProcessDropTableMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_TABLE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_ALTER_TABLE] = mnodeProcessAlterTableMsg; - -// tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_STREAM] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_ALTER_STREAM] = NULL; -// tsMworker.msgFp[TSDB_MSG_TYPE_KILL_QUERY] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_KILL_QUERY] = mnodeProcessKillQueryMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_KILL_STREAM] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_KILL_STREAM] = mnodeProcessKillStreamMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_KILL_CONN] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_KILL_CONN] = mnodeProcessKillConnectionMsg; -} - -static void mnodeProcessWriteReq(SMnMsg *pMsg, void *unused) { - int32_t msgType = pMsg->rpcMsg.msgType; - void *ahandle = pMsg->rpcMsg.ahandle; - int32_t code = 0; - - if (pMsg->rpcMsg.pCont == NULL) { - mError("msg:%p, app:%p type:%s content is null", pMsg, ahandle, taosMsg[msgType]); - code = TSDB_CODE_MND_INVALID_MSG_LEN; - goto PROCESS_WRITE_REQ_END; - } - - if (!mnodeIsMaster()) { - SMnRsp *rpcRsp = &pMsg->rpcRsp; - SEpSet *epSet = rpcMallocCont(sizeof(SEpSet)); - mnodeGetMnodeEpSetForShell(epSet, true); - rpcRsp->rsp = epSet; - rpcRsp->len = sizeof(SEpSet); - - mDebug("msg:%p, app:%p type:%s in write queue, is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, - taosMsg[msgType], epSet->numOfEps, epSet->inUse); - - code = TSDB_CODE_RPC_REDIRECT; - goto PROCESS_WRITE_REQ_END; - } - - if (tsMworker.writeMsgFp[msgType] == NULL) { - mError("msg:%p, app:%p type:%s not processed", pMsg, ahandle, taosMsg[msgType]); - code = TSDB_CODE_MND_MSG_NOT_PROCESSED; - goto PROCESS_WRITE_REQ_END; - } - - code = (*tsMworker.writeMsgFp[msgType])(pMsg); - -PROCESS_WRITE_REQ_END: - mnodeSendRsp(pMsg, code); -} - -static void mnodeProcessReadReq(SMnMsg *pMsg, void *unused) { - int32_t msgType = pMsg->rpcMsg.msgType; - void *ahandle = pMsg->rpcMsg.ahandle; - int32_t code = 0; - - if (pMsg->rpcMsg.pCont == NULL) { - mError("msg:%p, app:%p type:%s in mread queue, content is null", pMsg, ahandle, taosMsg[msgType]); - code = TSDB_CODE_MND_INVALID_MSG_LEN; - goto PROCESS_READ_REQ_END; - } - - if (!mnodeIsMaster()) { - SMnRsp *rpcRsp = &pMsg->rpcRsp; - SEpSet *epSet = rpcMallocCont(sizeof(SEpSet)); - if (!epSet) { - code = TSDB_CODE_MND_OUT_OF_MEMORY; - goto PROCESS_READ_REQ_END; - } - mnodeGetMnodeEpSetForShell(epSet, true); - rpcRsp->rsp = epSet; - rpcRsp->len = sizeof(SEpSet); - - mDebug("msg:%p, app:%p type:%s in mread queue is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, taosMsg[msgType], - epSet->numOfEps, epSet->inUse); - code = TSDB_CODE_RPC_REDIRECT; - goto PROCESS_READ_REQ_END; - } - - if (tsMworker.readMsgFp[msgType] == NULL) { - mError("msg:%p, app:%p type:%s in mread queue, not processed", pMsg, ahandle, taosMsg[msgType]); - code = TSDB_CODE_MND_MSG_NOT_PROCESSED; - goto PROCESS_READ_REQ_END; - } - - mTrace("msg:%p, app:%p type:%s will be processed in mread queue", pMsg, ahandle, taosMsg[msgType]); - code = (*tsMworker.readMsgFp[msgType])(pMsg); - -PROCESS_READ_REQ_END: - mnodeSendRsp(pMsg, code); -} - -static void mnodeProcessPeerReq(SMnMsg *pMsg, void *unused) { - int32_t msgType = pMsg->rpcMsg.msgType; - void *ahandle = pMsg->rpcMsg.ahandle; - int32_t code = 0; - - if (pMsg->rpcMsg.pCont == NULL) { - mError("msg:%p, ahandle:%p type:%s in mpeer queue, content is null", pMsg, ahandle, taosMsg[msgType]); - code = TSDB_CODE_MND_INVALID_MSG_LEN; - goto PROCESS_PEER_REQ_END; - } - - if (!mnodeIsMaster()) { - SMnRsp *rpcRsp = &pMsg->rpcRsp; - SEpSet *epSet = rpcMallocCont(sizeof(SEpSet)); - mnodeGetMnodeEpSetForPeer(epSet, true); - rpcRsp->rsp = epSet; - rpcRsp->len = sizeof(SEpSet); - - mDebug("msg:%p, ahandle:%p type:%s in mpeer queue is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, - taosMsg[msgType], epSet->numOfEps, epSet->inUse); - - code = TSDB_CODE_RPC_REDIRECT; - goto PROCESS_PEER_REQ_END; - } - - if (tsMworker.peerReqFp[msgType] == NULL) { - mError("msg:%p, ahandle:%p type:%s in mpeer queue, not processed", pMsg, ahandle, taosMsg[msgType]); - code = TSDB_CODE_MND_MSG_NOT_PROCESSED; - goto PROCESS_PEER_REQ_END; - } - - code = (*tsMworker.peerReqFp[msgType])(pMsg); - -PROCESS_PEER_REQ_END: - mnodeSendRsp(pMsg, code); -} - -static void mnodeProcessPeerRsp(SMnMsg *pMsg, void *unused) { - int32_t msgType = pMsg->rpcMsg.msgType; - SRpcMsg *pRpcMsg = &pMsg->rpcMsg; - - if (!mnodeIsMaster()) { - mError("msg:%p, ahandle:%p type:%s not processed for not master", pRpcMsg, pRpcMsg->ahandle, taosMsg[msgType]); - mnodeCleanupMsg2(pMsg); - } - - if (tsMworker.peerRspFp[msgType]) { - (*tsMworker.peerRspFp[msgType])(pRpcMsg); - } else { - mError("msg:%p, ahandle:%p type:%s is not processed", pRpcMsg, pRpcMsg->ahandle, taosMsg[msgType]); - } - - mnodeCleanupMsg2(pMsg); -} - -int32_t mnodeInitWorker() { - mnodeInitMsgFp(); - - SWorkerPool *pPool = &tsMworker.write; - pPool->name = "mnode-write"; - pPool->min = 1; - pPool->max = 1; - if (tWorkerInit(pPool) != 0) { - return TSDB_CODE_MND_OUT_OF_MEMORY; - } else { - tsMworker.writeQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)mnodeProcessWriteReq); - } - - pPool = &tsMworker.read; - pPool->name = "mnode-read"; - pPool->min = 2; - pPool->max = (int32_t)(tsNumOfCores * tsNumOfThreadsPerCore / 2); - pPool->max = MAX(2, pPool->max); - pPool->max = MIN(4, pPool->max); - if (tWorkerInit(pPool) != 0) { - return TSDB_CODE_MND_OUT_OF_MEMORY; - } else { - tsMworker.readQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)mnodeProcessReadReq); - } - - pPool = &tsMworker.peerReq; - pPool->name = "mnode-peer-req"; - pPool->min = 1; - pPool->max = 1; - if (tWorkerInit(pPool) != 0) { - return TSDB_CODE_MND_OUT_OF_MEMORY; - } else { - tsMworker.peerReqQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)mnodeProcessPeerReq); - } - - pPool = &tsMworker.peerRsp; - pPool->name = "mnode-peer-rsp"; - pPool->min = 1; - pPool->max = 1; - if (tWorkerInit(pPool) != 0) { - return TSDB_CODE_MND_OUT_OF_MEMORY; - } else { - tsMworker.peerRspQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)mnodeProcessPeerRsp); - } - - mInfo("mnode worker is initialized"); - return 0; -} - -void mnodeCleanupWorker() { - tWorkerFreeQueue(&tsMworker.write, tsMworker.writeQ); - tWorkerCleanup(&tsMworker.write); - tsMworker.writeQ = NULL; - - tWorkerFreeQueue(&tsMworker.read, tsMworker.readQ); - tWorkerCleanup(&tsMworker.read); - tsMworker.readQ = NULL; - - tWorkerFreeQueue(&tsMworker.peerReq, tsMworker.peerReqQ); - tWorkerCleanup(&tsMworker.peerReq); - tsMworker.peerReqQ = NULL; - - tWorkerFreeQueue(&tsMworker.peerRsp, tsMworker.peerRspQ); - tWorkerCleanup(&tsMworker.peerRsp); - tsMworker.peerRspQ = NULL; - - mInfo("mnode worker is closed"); -} - -SMnodeMsg *mnodeInitMsg(int32_t msgNum) { return NULL; } - -int32_t mnodeAppendMsg(SMnodeMsg *pMsg, SRpcMsg *pRpcMsg) { return 0; } - -void mnodeCleanupMsg(SMnodeMsg *pMsg) {} -void mnodeProcessMsg(SMnodeMsg *pMsg, EMnMsgType msgType) {} diff --git a/source/dnode/mnode/impl/src/mondeInt.c b/source/dnode/mnode/impl/src/mondeInt.c deleted file mode 100644 index a7c76360e2..0000000000 --- a/source/dnode/mnode/impl/src/mondeInt.c +++ /dev/null @@ -1,250 +0,0 @@ -/* - * 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 "os.h" -#include "tglobal.h" -#include "tstep.h" -#include "mnodeAcct.h" -#include "mnodeAuth.h" -#include "mnodeBalance.h" -#include "mnodeCluster.h" -#include "mnodeDb.h" -#include "mnodeDnode.h" -#include "mnodeFunc.h" -#include "mnodeMnode.h" -#include "mnodeOper.h" -#include "mnodeProfile.h" -#include "mnodeInt.h" -#include "mnodeShow.h" -#include "mnodeStable.h" -#include "mnodeSync.h" -#include "mnodeUser.h" -#include "mnodeVgroup.h" -#include "mnodeWorker.h" -#include "mnodeTelem.h" - -static struct { - int32_t state; - int32_t dnodeId; - int64_t clusterId; - tmr_h timer; - SSteps *steps1; - SSteps *steps2; - SMnodePara para; -} tsMint; - -tmr_h mnodeGetTimer() { return tsMint.timer; } - -int32_t mnodeGetDnodeId() { return tsMint.para.dnodeId; } - -int64_t mnodeGetClusterId() { return tsMint.para.clusterId; } - -EMnStatus mnodeGetStatus() { return tsMint.state; } - -void mnodeSendMsgToDnode(struct SEpSet *epSet, struct SRpcMsg *rpcMsg) { - (*tsMint.para.SendMsgToDnode)(epSet, rpcMsg); -} - -void mnodeSendMsgToMnode(struct SRpcMsg *rpcMsg) { return (*tsMint.para.SendMsgToMnode)(rpcMsg); } - -void mnodeSendRedirectMsg(struct SRpcMsg *rpcMsg, bool forShell) { (*tsMint.para.SendRedirectMsg)(rpcMsg, forShell); } - -int32_t mnodeGetLoad(SMnodeLoad *pLoad) { return 0; } - -static int32_t mnodeSetPara(SMnodePara para) { - tsMint.para = para; - - if (tsMint.para.SendMsgToDnode == NULL) return -1; - if (tsMint.para.SendMsgToMnode == NULL) return -1; - if (tsMint.para.SendRedirectMsg == NULL) return -1; - if (tsMint.para.PutMsgIntoApplyQueue == NULL) return -1; - if (tsMint.para.dnodeId < 0) return -1; - if (tsMint.para.clusterId < 0) return -1; - - return 0; -} - -static int32_t mnodeInitTimer() { - if (tsMint.timer == NULL) { - tsMint.timer = taosTmrInit(tsMaxShellConns, 200, 3600000, "MND"); - } - - return 0; -} - -static void mnodeCleanupTimer() { - if (tsMint.timer != NULL) { - taosTmrCleanUp(tsMint.timer); - tsMint.timer = NULL; - } -} - -static int32_t mnodeInitStep1() { - struct SSteps *steps = taosStepInit(16, NULL); - if (steps == NULL) return -1; - - taosStepAdd(steps, "mnode-sdb", sdbInit, sdbCleanup); - taosStepAdd(steps, "mnode-cluster", mnodeInitCluster, mnodeCleanupCluster); - taosStepAdd(steps, "mnode-dnode", mnodeInitDnode, mnodeCleanupDnode); - taosStepAdd(steps, "mnode-mnode", mnodeInitMnode, mnodeCleanupMnode); - taosStepAdd(steps, "mnode-acct", mnodeInitAcct, mnodeCleanupAcct); - taosStepAdd(steps, "mnode-auth", mnodeInitAuth, mnodeCleanupAuth); - taosStepAdd(steps, "mnode-user", mnodeInitUser, mnodeCleanupUser); - taosStepAdd(steps, "mnode-db", mnodeInitDb, mnodeCleanupDb); - taosStepAdd(steps, "mnode-vgroup", mnodeInitVgroup, mnodeCleanupVgroup); - taosStepAdd(steps, "mnode-stable", mnodeInitStable, mnodeCleanupStable); - taosStepAdd(steps, "mnode-func", mnodeInitFunc, mnodeCleanupFunc); - taosStepAdd(steps, "mnode-oper", mnodeInitOper, mnodeCleanupOper); - - tsMint.steps1 = steps; - return taosStepExec(tsMint.steps1); -} - -static int32_t mnodeInitStep2() { - struct SSteps *steps = taosStepInit(12, NULL); - if (steps == NULL) return -1; - - taosStepAdd(steps, "mnode-timer", mnodeInitTimer, NULL); - taosStepAdd(steps, "mnode-worker", mnodeInitWorker, NULL); - taosStepAdd(steps, "mnode-balance", mnodeInitBalance, mnodeCleanupBalance); - taosStepAdd(steps, "mnode-profile", mnodeInitProfile, mnodeCleanupProfile); - taosStepAdd(steps, "mnode-show", mnodeInitShow, mnodeCleanUpShow); - taosStepAdd(steps, "mnode-sync", mnodeInitSync, mnodeCleanUpSync); - taosStepAdd(steps, "mnode-worker", NULL, mnodeCleanupWorker); - taosStepAdd(steps, "mnode-telem", mnodeInitTelem, mnodeCleanupTelem); - taosStepAdd(steps, "mnode-timer", NULL, mnodeCleanupTimer); - - tsMint.steps2 = steps; - return taosStepExec(tsMint.steps2); -} - -static void mnodeCleanupStep1() { taosStepCleanup(tsMint.steps1); } - -static void mnodeCleanupStep2() { taosStepCleanup(tsMint.steps2); } - -static bool mnodeNeedDeploy() { - if (tsMint.para.dnodeId > 0) return false; - if (tsMint.para.clusterId > 0) return false; - if (strcmp(tsFirst, tsLocalEp) != 0) return false; - return true; -} - -int32_t mnodeDeploy(char *path, SMnodeCfg *pCfg) { - if (tsMint.state != MN_STATUS_UNINIT) { - mError("failed to deploy mnode since its deployed"); - return 0; - } else { - tsMint.state = MN_STATUS_INIT; - } - - if (tsMint.para.dnodeId <= 0 || tsMint.para.clusterId <= 0) { - mError("failed to deploy mnode since cluster not ready"); - return TSDB_CODE_MND_NOT_READY; - } - - mInfo("starting to deploy mnode"); - - int32_t code = mnodeInitStep1(); - if (code != 0) { - mError("failed to deploy mnode since init step1 error"); - tsMint.state = MN_STATUS_UNINIT; - return TSDB_CODE_MND_APP_ERROR; - } - - code = mnodeInitStep2(); - if (code != 0) { - mnodeCleanupStep1(); - mError("failed to deploy mnode since init step2 error"); - tsMint.state = MN_STATUS_UNINIT; - return TSDB_CODE_MND_APP_ERROR; - } - - mDebug("mnode is deployed and waiting for raft to confirm"); - tsMint.state = MN_STATUS_READY; - return 0; -} - -void mnodeUnDeploy(char *path) { - sdbUnDeploy(); - mnodeCleanup(); -} - -int32_t mnodeInit(SMnodePara para) { - mDebugFlag = 207; - if (tsMint.state != MN_STATUS_UNINIT) { - return 0; - } else { - tsMint.state = MN_STATUS_INIT; - } - - mInfo("starting to initialize mnode ..."); - - int32_t code = mnodeSetPara(para); - if (code != 0) { - tsMint.state = MN_STATUS_UNINIT; - return code; - } - - code = mnodeInitStep1(); - if (code != 0) { - tsMint.state = MN_STATUS_UNINIT; - return -1; - } - - code = sdbRead(); - if (code != 0) { - if (mnodeNeedDeploy()) { - code = sdbDeploy(); - if (code != 0) { - mnodeCleanupStep1(); - tsMint.state = MN_STATUS_UNINIT; - return -1; - } - } else { - mnodeCleanupStep1(); - tsMint.state = MN_STATUS_UNINIT; - return -1; - } - } - - code = mnodeInitStep2(); - if (code != 0) { - mnodeCleanupStep1(); - tsMint.state = MN_STATUS_UNINIT; - return -1; - } - - tsMint.state = MN_STATUS_READY; - mInfo("mnode is initialized successfully"); - return 0; -} - -void mnodeCleanup() { - if (tsMint.state != MN_STATUS_UNINIT && tsMint.state != MN_STATUS_CLOSING) { - mInfo("starting to clean up mnode"); - tsMint.state = MN_STATUS_CLOSING; - - mnodeCleanupStep2(); - mnodeCleanupStep1(); - - tsMint.state = MN_STATUS_UNINIT; - mInfo("mnode is cleaned up"); - } -} - -int32_t mnodeStart(char *path, SMnodeCfg *pCfg) { return 0; } -int32_t mnodeAlter(SMnodeCfg *pCfg) { return 0; } -void mnodeStop() {} \ 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 d3b825e190..119f614db5 100644 --- a/source/dnode/mnode/sdb/src/sdb.c +++ b/source/dnode/mnode/sdb/src/sdb.c @@ -87,7 +87,7 @@ static int32_t sdbReadDataFile() { SSdbRaw *pRaw = malloc(SDB_MAX_SIZE); if (pRaw == NULL) { - return TSDB_CODE_MND_OUT_OF_MEMORY; + return TSDB_CODE_OUT_OF_MEMORY; } char file[PATH_MAX] = {0}; @@ -241,7 +241,7 @@ int32_t sdbInit() { tsSdb.tmpDir = strdup(path); if (tsSdb.currDir == NULL || tsSdb.currDir == NULL || tsSdb.currDir == NULL) { - return TSDB_CODE_MND_OUT_OF_MEMORY; + return TSDB_CODE_OUT_OF_MEMORY; } for (int32_t i = 0; i < SDB_MAX; ++i) { @@ -256,7 +256,7 @@ int32_t sdbInit() { SHashObj *hash = taosHashInit(128, taosGetDefaultHashFunction(type), true, HASH_NO_LOCK); if (hash == NULL) { - return TSDB_CODE_MND_OUT_OF_MEMORY; + return TSDB_CODE_OUT_OF_MEMORY; } tsSdb.hashObjs[i] = hash; diff --git a/source/dnode/mnode/transaction/src/trnInt.c b/source/dnode/mnode/transaction/src/trnInt.c index f7463ec369..48f6175c62 100644 --- a/source/dnode/mnode/transaction/src/trnInt.c +++ b/source/dnode/mnode/transaction/src/trnInt.c @@ -41,7 +41,7 @@ SSdbRaw *trnActionEncode(STrans *pTrans) { SSdbRaw *pRaw = calloc(1, rawDataLen + sizeof(SSdbRaw)); if (pRaw == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -67,7 +67,7 @@ STrans *trnActionDecode(SSdbRaw *pRaw) { STrans *pTrans = NULL; if (pTrans == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -92,14 +92,14 @@ STrans *trnActionDecode(SSdbRaw *pRaw) { if (code == 0 && pTmp->dataLen > 0) { SSdbRaw *pRead = malloc(sizeof(SSdbRaw) + pTmp->dataLen); if (pRead == NULL) { - code = TSDB_CODE_MND_OUT_OF_MEMORY; + code = TSDB_CODE_OUT_OF_MEMORY; break; } memcpy(pRead, pTmp, sizeof(SSdbRaw)); SDB_GET_BINARY_VAL(pData, dataLen, pRead->data, pRead->dataLen, code); void *ret = taosArrayPush(pTrans->redoLogs, &pRead); if (ret == NULL) { - code = TSDB_CODE_MND_OUT_OF_MEMORY; + code = TSDB_CODE_OUT_OF_MEMORY; break; } } @@ -151,7 +151,7 @@ int32_t trnGenerateTransId() { return 1; } STrans *trnCreate(ETrnPolicy policy) { STrans *pTrans = calloc(1, sizeof(STrans)); if (pTrans == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -166,7 +166,7 @@ STrans *trnCreate(ETrnPolicy policy) { if (pTrans->redoLogs == NULL || pTrans->undoLogs == NULL || pTrans->commitLogs == NULL || pTrans->redoActions == NULL || pTrans->undoActions == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -195,13 +195,13 @@ void trnSetRpcHandle(STrans *pTrans, void *rpcHandle) { pTrans->rpcHandle = rpcH static int32_t trnAppendArray(SArray *pArray, SSdbRaw *pRaw) { if (pArray == NULL || pRaw == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } void *ptr = taosArrayPush(pArray, &pRaw); if (ptr == NULL) { - terrno = TSDB_CODE_MND_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 4151178e2e..7f396f4a53 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -70,11 +70,13 @@ TAOS_DEFINE_ERROR(TSDB_CODE_RPC_FQDN_ERROR, "Unable to resolve FQD TAOS_DEFINE_ERROR(TSDB_CODE_RPC_INVALID_VERSION, "Invalid app version") //common & util -TAOS_DEFINE_ERROR(TSDB_CODE_COM_OPS_NOT_SUPPORT, "Operation not supported") -TAOS_DEFINE_ERROR(TSDB_CODE_COM_MEMORY_CORRUPTED, "Memory corrupted") -TAOS_DEFINE_ERROR(TSDB_CODE_COM_OUT_OF_MEMORY, "Out of memory") -TAOS_DEFINE_ERROR(TSDB_CODE_COM_INVALID_CFG_MSG, "Invalid config message") -TAOS_DEFINE_ERROR(TSDB_CODE_COM_FILE_CORRUPTED, "Data file corrupted") +TAOS_DEFINE_ERROR(TSDB_CODE_OPS_NOT_SUPPORT, "Operation not supported") +TAOS_DEFINE_ERROR(TSDB_CODE_OUT_OF_MEMORY, "Out of Memory") +TAOS_DEFINE_ERROR(TSDB_CODE_OUT_OF_RANGE, "Out of range") +TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_PTR, "Invalid pointer") +TAOS_DEFINE_ERROR(TSDB_CODE_MEMORY_CORRUPTED, "Memory corrupted") +TAOS_DEFINE_ERROR(TSDB_CODE_FILE_CORRUPTED, "Data file corrupted") +TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_MSG, "Invalid config message") TAOS_DEFINE_ERROR(TSDB_CODE_REF_NO_MEMORY, "Ref out of memory") TAOS_DEFINE_ERROR(TSDB_CODE_REF_FULL, "too many Ref Objs") TAOS_DEFINE_ERROR(TSDB_CODE_REF_ID_REMOVED, "Ref ID is removed") @@ -82,6 +84,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_REF_INVALID_ID, "Invalid Ref ID") TAOS_DEFINE_ERROR(TSDB_CODE_REF_ALREADY_EXIST, "Ref is already there") TAOS_DEFINE_ERROR(TSDB_CODE_REF_NOT_EXIST, "Ref is not there") + //client TAOS_DEFINE_ERROR(TSDB_CODE_TSC_INVALID_OPERATION, "Invalid operation") TAOS_DEFINE_ERROR(TSDB_CODE_TSC_INVALID_QHANDLE, "Invalid qhandle") @@ -131,7 +134,6 @@ TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_MSG_VERSION, "Incompatible protocol TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_MSG_LEN, "Invalid message length") TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_MSG_TYPE, "Invalid message type") TAOS_DEFINE_ERROR(TSDB_CODE_MND_TOO_MANY_SHELL_CONNS, "Too many connections") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_OUT_OF_MEMORY, "Out of memory in mnode") TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_SHOWOBJ, "Data expired") TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_QUERY_ID, "Invalid query id") TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_STREAM_ID, "Invalid stream id") diff --git a/source/util/src/tpagedfile.c b/source/util/src/tpagedfile.c index fcd4f2b155..3373d09876 100644 --- a/source/util/src/tpagedfile.c +++ b/source/util/src/tpagedfile.c @@ -12,7 +12,7 @@ int32_t createDiskbasedResultBuffer(SDiskbasedResultBuf** pResultBuf, int32_t pa SDiskbasedResultBuf* pResBuf = *pResultBuf; if (pResBuf == NULL) { - return TSDB_CODE_COM_OUT_OF_MEMORY; + return TSDB_CODE_OUT_OF_MEMORY; } pResBuf->pageSize = pagesize; diff --git a/source/util/src/tqueue.c b/source/util/src/tqueue.c index 5d6a507172..93008f7114 100644 --- a/source/util/src/tqueue.c +++ b/source/util/src/tqueue.c @@ -55,7 +55,7 @@ typedef struct STaosQall { taos_queue taosOpenQueue() { STaosQueue *queue = (STaosQueue *)calloc(sizeof(STaosQueue), 1); if (queue == NULL) { - terrno = TSDB_CODE_COM_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -244,7 +244,7 @@ void taosResetQitems(taos_qall param) { taos_qset taosOpenQset() { STaosQset *qset = (STaosQset *)calloc(sizeof(STaosQset), 1); if (qset == NULL) { - terrno = TSDB_CODE_COM_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } diff --git a/source/util/src/tstep.c b/source/util/src/tstep.c index 8162fb76ed..d840b119fb 100644 --- a/source/util/src/tstep.c +++ b/source/util/src/tstep.c @@ -16,6 +16,7 @@ #define _DEFAULT_SOURCE #include "os.h" #include "ulog.h" +#include "taoserror.h" #include "tstep.h" typedef struct { @@ -33,7 +34,10 @@ typedef struct SSteps { SSteps *taosStepInit(int32_t maxsize, ReportFp fp) { SSteps *steps = calloc(1, sizeof(SSteps)); - if (steps == NULL) return NULL; + if (steps == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } steps->maxsize = maxsize; steps->cursize = 0; @@ -44,9 +48,14 @@ SSteps *taosStepInit(int32_t maxsize, ReportFp fp) { } int32_t taosStepAdd(struct SSteps *steps, char *name, InitFp initFp, CleanupFp cleanupFp) { - if (steps == NULL) return -1; + if (steps == NULL) { + terrno = TSDB_CODE_INVALID_PTR; + return -1; + } + if (steps->cursize >= steps->maxsize) { uError("failed to add step since up to the maxsize"); + terrno = TSDB_CODE_OUT_OF_RANGE; return -1; } @@ -66,7 +75,10 @@ static void taosStepCleanupImp(SSteps *steps, int32_t pos) { } int32_t taosStepExec(SSteps *steps) { - if (steps == NULL) return -1; + if (steps == NULL) { + terrno = TSDB_CODE_INVALID_PTR; + return -1; + } for (int32_t s = 0; s < steps->cursize; s++) { SStep *step = steps->steps + s; diff --git a/source/util/src/ttimer.c b/source/util/src/ttimer.c index 4aac7b7abf..56186d9b24 100644 --- a/source/util/src/ttimer.c +++ b/source/util/src/ttimer.c @@ -18,6 +18,7 @@ #include "tsched.h" #include "ttimer.h" #include "tutil.h" +#include "taoserror.h" extern int8_t tscEmbedded; @@ -547,6 +548,7 @@ void* taosTmrInit(int maxNumOfTmrs, int resolution, int longest, const char* lab if (ctrl == NULL) { tmrError("%s too many timer controllers, failed to create timer controller.", label); + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } From 2bdccc20ae1c911c8eff44e87e2c0d52ab48d684 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 11 Nov 2021 18:45:25 +0800 Subject: [PATCH 16/17] minor changes --- include/dnode/mnode/sdb/sdb.h | 4 +-- source/dnode/mnode/impl/inc/mnodeInt.h | 2 +- source/dnode/mnode/impl/src/mnode.c | 2 +- source/dnode/mnode/impl/src/mnodeAcct.c | 20 +++++++-------- source/dnode/mnode/impl/src/mnodeUser.c | 18 ++++++------- source/dnode/mnode/sdb/inc/sdbInt.h | 11 ++++---- source/dnode/mnode/sdb/src/sdb.c | 25 ++++++++++--------- .../mnode/transaction/src/{trnInt.c => trn.c} | 4 +-- .../transaction/src/{trnMain.c => trnExec.c} | 0 9 files changed, 43 insertions(+), 43 deletions(-) rename source/dnode/mnode/transaction/src/{trnInt.c => trn.c} (99%) rename source/dnode/mnode/transaction/src/{trnMain.c => trnExec.c} (100%) diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h index eef79fbc09..f58cbdfc56 100644 --- a/include/dnode/mnode/sdb/sdb.h +++ b/include/dnode/mnode/sdb/sdb.h @@ -121,11 +121,11 @@ typedef struct { SdbInsertFp insertFp; SdbUpdateFp updateFp; SdbDeleteFp deleteFp; -} SSdbDesc; +} SSdbHandle; int32_t sdbInit(); void sdbCleanup(); -void sdbSetHandler(SSdbDesc desc); +void sdbSetHandle(SSdbHandle handle); int32_t sdbRead(); int32_t sdbWrite(SSdbRaw *pRaw); diff --git a/source/dnode/mnode/impl/inc/mnodeInt.h b/source/dnode/mnode/impl/inc/mnodeInt.h index 95f0acf52f..96803ba4a5 100644 --- a/source/dnode/mnode/impl/inc/mnodeInt.h +++ b/source/dnode/mnode/impl/inc/mnodeInt.h @@ -24,7 +24,7 @@ extern "C" { #endif -typedef void (*MnodeRpcFp[TSDB_MSG_TYPE_MAX])(SMnodeMsg *pMsg); +typedef void (*MnodeRpcFp)(SMnodeMsg *pMsg); tmr_h mnodeGetTimer(); int32_t mnodeGetDnodeId(); diff --git a/source/dnode/mnode/impl/src/mnode.c b/source/dnode/mnode/impl/src/mnode.c index 93cd85a888..21db85fcfa 100644 --- a/source/dnode/mnode/impl/src/mnode.c +++ b/source/dnode/mnode/impl/src/mnode.c @@ -42,7 +42,7 @@ static struct { SSteps *pInitSteps; SSteps *pStartSteps; SMnodePara para; - MnodeRpcFp msgFp; + MnodeRpcFp msgFp[TSDB_MSG_TYPE_MAX]; } tsMint; int32_t mnodeGetDnodeId() { return tsMint.para.dnodeId; } diff --git a/source/dnode/mnode/impl/src/mnodeAcct.c b/source/dnode/mnode/impl/src/mnodeAcct.c index 251fcf3edc..8979ca2689 100644 --- a/source/dnode/mnode/impl/src/mnodeAcct.c +++ b/source/dnode/mnode/impl/src/mnodeAcct.c @@ -106,7 +106,7 @@ static int32_t mnodeCreateDefaultAcct() { .accessState = TSDB_VN_ALL_ACCCESS}; SSdbRaw *pRaw = mnodeAcctActionEncode(&acctObj); - if (pRaw != NULL) { + if (pRaw == NULL) { return -1; } @@ -114,15 +114,15 @@ static int32_t mnodeCreateDefaultAcct() { } int32_t mnodeInitAcct() { - SSdbDesc desc = {.sdbType = SDB_ACCT, - .keyType = SDB_KEY_BINARY, - .deployFp = (SdbDeployFp)mnodeCreateDefaultAcct, - .encodeFp = (SdbEncodeFp)mnodeAcctActionEncode, - .decodeFp = (SdbDecodeFp)mnodeAcctActionDecode, - .insertFp = (SdbInsertFp)mnodeAcctActionInsert, - .updateFp = (SdbUpdateFp)mnodeAcctActionUpdate, - .deleteFp = (SdbDeleteFp)mnodeAcctActionDelete}; - sdbSetHandler(desc); + SSdbHandle handle = {.sdbType = SDB_ACCT, + .keyType = SDB_KEY_BINARY, + .deployFp = (SdbDeployFp)mnodeCreateDefaultAcct, + .encodeFp = (SdbEncodeFp)mnodeAcctActionEncode, + .decodeFp = (SdbDecodeFp)mnodeAcctActionDecode, + .insertFp = (SdbInsertFp)mnodeAcctActionInsert, + .updateFp = (SdbUpdateFp)mnodeAcctActionUpdate, + .deleteFp = (SdbDeleteFp)mnodeAcctActionDelete}; + sdbSetHandle(handle); return 0; } diff --git a/source/dnode/mnode/impl/src/mnodeUser.c b/source/dnode/mnode/impl/src/mnodeUser.c index e7b03b8372..41ea0acc31 100644 --- a/source/dnode/mnode/impl/src/mnodeUser.c +++ b/source/dnode/mnode/impl/src/mnodeUser.c @@ -234,15 +234,15 @@ static int32_t mnodeProcessCreateUserMsg(SMnodeMsg *pMsg) { } int32_t mnodeInitUser() { - SSdbDesc desc = {.sdbType = SDB_USER, - .keyType = SDB_KEY_BINARY, - .deployFp = (SdbDeployFp)mnodeCreateDefaultUsers, - .encodeFp = (SdbEncodeFp)mnodeUserActionEncode, - .decodeFp = (SdbDecodeFp)mnodeUserActionDecode, - .insertFp = (SdbInsertFp)mnodeUserActionInsert, - .updateFp = (SdbUpdateFp)mnodeUserActionUpdate, - .deleteFp = (SdbDeleteFp)mnodeUserActionDelete}; - sdbSetHandler(desc); + SSdbHandle handle = {.sdbType = SDB_USER, + .keyType = SDB_KEY_BINARY, + .deployFp = (SdbDeployFp)mnodeCreateDefaultUsers, + .encodeFp = (SdbEncodeFp)mnodeUserActionEncode, + .decodeFp = (SdbDecodeFp)mnodeUserActionDecode, + .insertFp = (SdbInsertFp)mnodeUserActionInsert, + .updateFp = (SdbUpdateFp)mnodeUserActionUpdate, + .deleteFp = (SdbDeleteFp)mnodeUserActionDelete}; + sdbSetHandle(handle); return 0; } diff --git a/source/dnode/mnode/sdb/inc/sdbInt.h b/source/dnode/mnode/sdb/inc/sdbInt.h index 3493e97ef2..222e16abe9 100644 --- a/source/dnode/mnode/sdb/inc/sdbInt.h +++ b/source/dnode/mnode/sdb/inc/sdbInt.h @@ -19,7 +19,6 @@ #include "os.h" #include "sdb.h" #include "taosmsg.h" -#include "tglobal.h" #include "thash.h" #include "tlockfree.h" #include "tlog.h" @@ -35,7 +34,7 @@ extern "C" { #define mDebug(...) { if (mDebugFlag & DEBUG_DEBUG) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }} #define mTrace(...) { if (mDebugFlag & DEBUG_TRACE) { taosPrintLog("MND ", mDebugFlag, __VA_ARGS__); }} -#define SDB_MAX_SIZE (32*1024) +#define SDB_MAX_SIZE (32 * 1024) typedef struct { char *currDir; @@ -52,13 +51,13 @@ typedef struct { SdbDeployFp deployFps[SDB_MAX]; SdbEncodeFp encodeFps[SDB_MAX]; SdbDecodeFp decodeFps[SDB_MAX]; -} SSdbObj; +} SSdbMgr; typedef struct { ESdbStatus status; - int32_t refCount; - int32_t dataLen; - char *data[]; + int32_t refCount; + int32_t dataLen; + char pData[]; } SSdbRow; #ifdef __cplusplus diff --git a/source/dnode/mnode/sdb/src/sdb.c b/source/dnode/mnode/sdb/src/sdb.c index 119f614db5..b8e0e04c02 100644 --- a/source/dnode/mnode/sdb/src/sdb.c +++ b/source/dnode/mnode/sdb/src/sdb.c @@ -15,8 +15,9 @@ #define _DEFAULT_SOURCE #include "sdbInt.h" +#include "tglobal.h" -static SSdbObj tsSdb = {0}; +static SSdbMgr tsSdb = {0}; static int32_t sdbCreateDir() { if (!taosMkDir(tsSdb.currDir)) { @@ -153,7 +154,7 @@ static int32_t sdbWriteDataFile() { SSdbRow *pRow = taosHashIterate(hash, NULL); while (pRow != NULL) { if (pRow->status == SDB_STATUS_READY) continue; - SSdbRaw *pRaw = (*encodeFp)(pRow->data); + SSdbRaw *pRaw = (*encodeFp)(pRow->pData); if (pRaw != NULL) { taosWriteFile(fd, pRaw, sizeof(SSdbRaw) + pRaw->dataLen); } else { @@ -292,15 +293,15 @@ void sdbCleanup() { } } -void sdbSetHandler(SSdbDesc desc) { - ESdbType sdb = desc.sdbType; - tsSdb.keyTypes[sdb] = desc.keyType; - tsSdb.insertFps[sdb] = desc.insertFp; - tsSdb.updateFps[sdb] = desc.updateFp; - tsSdb.deleteFps[sdb] = desc.deleteFp; - tsSdb.deployFps[sdb] = desc.deployFp; - tsSdb.encodeFps[sdb] = desc.encodeFp; - tsSdb.decodeFps[sdb] = desc.decodeFp; +void sdbSetHandle(SSdbHandle handle) { + ESdbType sdb = handle.sdbType; + tsSdb.keyTypes[sdb] = handle.keyType; + tsSdb.insertFps[sdb] = handle.insertFp; + tsSdb.updateFps[sdb] = handle.updateFp; + tsSdb.deleteFps[sdb] = handle.deleteFp; + tsSdb.deployFps[sdb] = handle.deployFp; + tsSdb.encodeFps[sdb] = handle.encodeFp; + tsSdb.decodeFps[sdb] = handle.decodeFp; } #if 0 @@ -370,7 +371,7 @@ void *sdbAcquire(ESdbType sdb, void *pKey) { if (pRow->status == SDB_STATUS_READY) { atomic_add_fetch_32(&pRow->refCount, 1); - return pRow->data; + return pRow->pData; } else { terrno = -1; // todo return NULL; diff --git a/source/dnode/mnode/transaction/src/trnInt.c b/source/dnode/mnode/transaction/src/trn.c similarity index 99% rename from source/dnode/mnode/transaction/src/trnInt.c rename to source/dnode/mnode/transaction/src/trn.c index 48f6175c62..c4db404e7b 100644 --- a/source/dnode/mnode/transaction/src/trnInt.c +++ b/source/dnode/mnode/transaction/src/trn.c @@ -223,14 +223,14 @@ int32_t trnAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) { } int32_t trnInit() { - SSdbDesc desc = {.sdbType = SDB_TRANS, + SSdbHandle handle = {.sdbType = SDB_TRANS, .keyType = SDB_KEY_INT32, .encodeFp = (SdbEncodeFp)trnActionEncode, .decodeFp = (SdbDecodeFp)trnActionDecode, .insertFp = (SdbInsertFp)trnActionInsert, .updateFp = (SdbUpdateFp)trnActionUpdate, .deleteFp = (SdbDeleteFp)trnActionDelete}; - sdbSetHandler(desc); + sdbSetHandle(handle); return 0; } diff --git a/source/dnode/mnode/transaction/src/trnMain.c b/source/dnode/mnode/transaction/src/trnExec.c similarity index 100% rename from source/dnode/mnode/transaction/src/trnMain.c rename to source/dnode/mnode/transaction/src/trnExec.c From 4910ea2971039df75139d18fcdaad639782cf504 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 11 Nov 2021 19:02:38 +0800 Subject: [PATCH 17/17] rename some variables --- include/dnode/mnode/sdb/sdb.h | 10 +++--- include/util/taoserror.h | 19 ++++++----- source/dnode/mnode/impl/src/mnodeAcct.c | 20 +++++------ source/dnode/mnode/impl/src/mnodeUser.c | 20 +++++------ source/dnode/mnode/sdb/src/sdb.c | 43 ++++++++++++++---------- source/dnode/mnode/transaction/src/trn.c | 18 +++++----- source/util/src/terror.c | 13 +++---- 7 files changed, 76 insertions(+), 67 deletions(-) diff --git a/include/dnode/mnode/sdb/sdb.h b/include/dnode/mnode/sdb/sdb.h index f58cbdfc56..f4a8ccd06e 100644 --- a/include/dnode/mnode/sdb/sdb.h +++ b/include/dnode/mnode/sdb/sdb.h @@ -27,7 +27,7 @@ extern "C" { (dataLen) -= (valLen); \ (pData) = (char *)(pData) + (valLen); \ } else { \ - code = TSDB_CODE_SDB_INVAID_RAW_DATA_LEN; \ + code = TSDB_CODE_SDB_INVALID_DATA_LEN; \ } \ } @@ -38,7 +38,7 @@ extern "C" { (dataLen) -= sizeof(int32_t); \ (pData) = (char *)(pData) + sizeof(int32_t); \ } else { \ - code = TSDB_CODE_SDB_INVAID_RAW_DATA_LEN; \ + code = TSDB_CODE_SDB_INVALID_DATA_LEN; \ } \ } @@ -49,7 +49,7 @@ extern "C" { (dataLen) -= sizeof(int64_t); \ (pData) = (char *)(pData) + sizeof(int64_t); \ } else { \ - code = TSDB_CODE_SDB_INVAID_RAW_DATA_LEN; \ + code = TSDB_CODE_SDB_INVALID_DATA_LEN; \ } \ } @@ -121,11 +121,11 @@ typedef struct { SdbInsertFp insertFp; SdbUpdateFp updateFp; SdbDeleteFp deleteFp; -} SSdbHandle; +} SSdbTable; int32_t sdbInit(); void sdbCleanup(); -void sdbSetHandle(SSdbHandle handle); +void sdbSetTable(SSdbTable table); int32_t sdbRead(); int32_t sdbWrite(SSdbRaw *pRaw); diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 9669ccb0bf..b644f4c9a5 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -134,15 +134,16 @@ int32_t* taosGetErrno(); #define TSDB_CODE_MND_FAILED_TO_INIT_STEP TAOS_DEF_ERROR_CODE(0, 0x0314) //"failed to init components") -#define TSDB_CODE_SDB_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x0320) -#define TSDB_CODE_SDB_OUT_OF_MEMORY TAOS_DEF_ERROR_CODE(0, 0x0321) -#define TSDB_CODE_SDB_OBJ_ALREADY_THERE TAOS_DEF_ERROR_CODE(0, 0x0322) -#define TSDB_CODE_SDB_OBJ_NOT_THERE TAOS_DEF_ERROR_CODE(0, 0x0323) -#define TSDB_CODE_SDB_INVAID_RAW_DATA_VER TAOS_DEF_ERROR_CODE(0, 0x0324) -#define TSDB_CODE_SDB_INVAID_RAW_DATA_LEN TAOS_DEF_ERROR_CODE(0, 0x0325) -#define TSDB_CODE_SDB_INVALID_TABLE_TYPE TAOS_DEF_ERROR_CODE(0, 0x0326) -#define TSDB_CODE_SDB_INVAID_META_ROW TAOS_DEF_ERROR_CODE(0, 0x0327) -#define TSDB_CODE_SDB_INVAID_KEY_TYPE TAOS_DEF_ERROR_CODE(0, 0x0328) +#define TSDB_CODE_SDB_APP_ERROR TAOS_DEF_ERROR_CODE(0, 0x0320) +#define TSDB_CODE_SDB_OBJ_ALREADY_THERE TAOS_DEF_ERROR_CODE(0, 0x0321) +#define TSDB_CODE_SDB_OBJ_NOT_THERE TAOS_DEF_ERROR_CODE(0, 0x0322) +#define TSDB_CODE_SDB_INVALID_TABLE_TYPE TAOS_DEF_ERROR_CODE(0, 0x0323) +#define TSDB_CODE_SDB_INVALID_KEY_TYPE TAOS_DEF_ERROR_CODE(0, 0x0324) +#define TSDB_CODE_SDB_INVALID_ACTION_TYPE TAOS_DEF_ERROR_CODE(0, 0x0325) +#define TSDB_CODE_SDB_INVALID_STATUS_TYPE TAOS_DEF_ERROR_CODE(0, 0x0326) +#define TSDB_CODE_SDB_INVALID_DATA_VER TAOS_DEF_ERROR_CODE(0, 0x0327) +#define TSDB_CODE_SDB_INVALID_DATA_LEN TAOS_DEF_ERROR_CODE(0, 0x0328) +#define TSDB_CODE_SDB_INVALID_META_ROW TAOS_DEF_ERROR_CODE(0, 0x0329) #define TSDB_CODE_MND_DNODE_ALREADY_EXIST TAOS_DEF_ERROR_CODE(0, 0x0330) //"DNode already exists") #define TSDB_CODE_MND_DNODE_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x0331) //"DNode does not exist") diff --git a/source/dnode/mnode/impl/src/mnodeAcct.c b/source/dnode/mnode/impl/src/mnodeAcct.c index 8979ca2689..381d6a184e 100644 --- a/source/dnode/mnode/impl/src/mnodeAcct.c +++ b/source/dnode/mnode/impl/src/mnodeAcct.c @@ -47,7 +47,7 @@ static SSdbRaw *mnodeAcctActionEncode(SAcctObj *pAcct) { static SAcctObj *mnodeAcctActionDecode(SSdbRaw *pRaw) { if (pRaw->sver != ACCT_VER) { - terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER; + terrno = TSDB_CODE_SDB_INVALID_DATA_VER; return NULL; } @@ -114,15 +114,15 @@ static int32_t mnodeCreateDefaultAcct() { } int32_t mnodeInitAcct() { - SSdbHandle handle = {.sdbType = SDB_ACCT, - .keyType = SDB_KEY_BINARY, - .deployFp = (SdbDeployFp)mnodeCreateDefaultAcct, - .encodeFp = (SdbEncodeFp)mnodeAcctActionEncode, - .decodeFp = (SdbDecodeFp)mnodeAcctActionDecode, - .insertFp = (SdbInsertFp)mnodeAcctActionInsert, - .updateFp = (SdbUpdateFp)mnodeAcctActionUpdate, - .deleteFp = (SdbDeleteFp)mnodeAcctActionDelete}; - sdbSetHandle(handle); + SSdbTable table = {.sdbType = SDB_ACCT, + .keyType = SDB_KEY_BINARY, + .deployFp = (SdbDeployFp)mnodeCreateDefaultAcct, + .encodeFp = (SdbEncodeFp)mnodeAcctActionEncode, + .decodeFp = (SdbDecodeFp)mnodeAcctActionDecode, + .insertFp = (SdbInsertFp)mnodeAcctActionInsert, + .updateFp = (SdbUpdateFp)mnodeAcctActionUpdate, + .deleteFp = (SdbDeleteFp)mnodeAcctActionDelete}; + sdbSetTable(table); return 0; } diff --git a/source/dnode/mnode/impl/src/mnodeUser.c b/source/dnode/mnode/impl/src/mnodeUser.c index 41ea0acc31..b15dc78ea9 100644 --- a/source/dnode/mnode/impl/src/mnodeUser.c +++ b/source/dnode/mnode/impl/src/mnodeUser.c @@ -45,7 +45,7 @@ static SSdbRaw *mnodeUserActionEncode(SUserObj *pUser) { static SUserObj *mnodeUserActionDecode(SSdbRaw *pRaw) { if (pRaw->sver != USER_VER) { - terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER; + terrno = TSDB_CODE_SDB_INVALID_DATA_VER; return NULL; } @@ -234,15 +234,15 @@ static int32_t mnodeProcessCreateUserMsg(SMnodeMsg *pMsg) { } int32_t mnodeInitUser() { - SSdbHandle handle = {.sdbType = SDB_USER, - .keyType = SDB_KEY_BINARY, - .deployFp = (SdbDeployFp)mnodeCreateDefaultUsers, - .encodeFp = (SdbEncodeFp)mnodeUserActionEncode, - .decodeFp = (SdbDecodeFp)mnodeUserActionDecode, - .insertFp = (SdbInsertFp)mnodeUserActionInsert, - .updateFp = (SdbUpdateFp)mnodeUserActionUpdate, - .deleteFp = (SdbDeleteFp)mnodeUserActionDelete}; - sdbSetHandle(handle); + SSdbTable table = {.sdbType = SDB_USER, + .keyType = SDB_KEY_BINARY, + .deployFp = (SdbDeployFp)mnodeCreateDefaultUsers, + .encodeFp = (SdbEncodeFp)mnodeUserActionEncode, + .decodeFp = (SdbDecodeFp)mnodeUserActionDecode, + .insertFp = (SdbInsertFp)mnodeUserActionInsert, + .updateFp = (SdbUpdateFp)mnodeUserActionUpdate, + .deleteFp = (SdbDeleteFp)mnodeUserActionDelete}; + sdbSetTable(table); return 0; } diff --git a/source/dnode/mnode/sdb/src/sdb.c b/source/dnode/mnode/sdb/src/sdb.c index b8e0e04c02..7489a46552 100644 --- a/source/dnode/mnode/sdb/src/sdb.c +++ b/source/dnode/mnode/sdb/src/sdb.c @@ -21,17 +21,20 @@ static SSdbMgr tsSdb = {0}; static int32_t sdbCreateDir() { if (!taosMkDir(tsSdb.currDir)) { - mError("failed to create dir:%s", tsSdb.currDir); - return TAOS_SYSTEM_ERROR(errno); + terrno = TAOS_SYSTEM_ERROR(errno); + mError("failed to create dir:%s since %s", tsSdb.currDir, terrstr()); + return -1; } if (!taosMkDir(tsSdb.syncDir)) { - mError("failed to create dir:%s", tsSdb.syncDir); + terrno = TAOS_SYSTEM_ERROR(errno); + mError("failed to create dir:%s since %s", tsSdb.syncDir, terrstr()); return -1; } if (!taosMkDir(tsSdb.tmpDir)) { - mError("failed to create dir:%s", tsSdb.tmpDir); + terrno = TAOS_SYSTEM_ERROR(errno); + mError("failed to create dir:%s since %s", tsSdb.tmpDir, terrstr()); return -1; } @@ -41,8 +44,10 @@ static int32_t sdbCreateDir() { static int32_t sdbRunDeployFp() { for (int32_t i = SDB_START; i < SDB_MAX; ++i) { SdbDeployFp fp = tsSdb.deployFps[i]; - if (fp) { - (*fp)(); + if (fp == NULL) continue; + if ((*fp)() != 0) { + mError("failed to deploy sdb:%d since %s", i, terrstr()); + return -1; } } @@ -51,11 +56,13 @@ static int32_t sdbRunDeployFp() { static SHashObj *sdbGetHash(int32_t sdb) { if (sdb >= SDB_MAX || sdb <= SDB_START) { + terrno = TSDB_CODE_SDB_INVALID_TABLE_TYPE; return NULL; } SHashObj *hash = tsSdb.hashObjs[sdb]; if (hash == NULL) { + terrno = TSDB_CODE_SDB_APP_ERROR; return NULL; } @@ -112,7 +119,7 @@ static int32_t sdbReadDataFile() { } if (ret < sizeof(SSdbRaw)) { - code = TSDB_CODE_SDB_INTERNAL_ERROR; + code = TSDB_CODE_SDB_APP_ERROR; mError("failed to read file:%s since %s", file, tstrerror(code)); break; } @@ -159,7 +166,7 @@ static int32_t sdbWriteDataFile() { taosWriteFile(fd, pRaw, sizeof(SSdbRaw) + pRaw->dataLen); } else { taosHashCancelIterate(hash, pRow); - code = TSDB_CODE_SDB_INTERNAL_ERROR; + code = TSDB_CODE_SDB_APP_ERROR; break; } @@ -232,7 +239,7 @@ void sdbUnDeploy() {} int32_t sdbInit() { char path[PATH_MAX + 100]; - snprintf(path, PATH_MAX + 100, "%s%scurrent%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP); + snprintf(path, PATH_MAX + 100, "%s%scur%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP); tsSdb.currDir = strdup(path); snprintf(path, PATH_MAX + 100, "%s%ssync%s", tsMnodeDir, TD_DIRSEP, TD_DIRSEP); @@ -293,15 +300,15 @@ void sdbCleanup() { } } -void sdbSetHandle(SSdbHandle handle) { - ESdbType sdb = handle.sdbType; - tsSdb.keyTypes[sdb] = handle.keyType; - tsSdb.insertFps[sdb] = handle.insertFp; - tsSdb.updateFps[sdb] = handle.updateFp; - tsSdb.deleteFps[sdb] = handle.deleteFp; - tsSdb.deployFps[sdb] = handle.deployFp; - tsSdb.encodeFps[sdb] = handle.encodeFp; - tsSdb.decodeFps[sdb] = handle.decodeFp; +void sdbSetTable(SSdbTable table) { + ESdbType sdb = table.sdbType; + tsSdb.keyTypes[sdb] = table.keyType; + tsSdb.insertFps[sdb] = table.insertFp; + tsSdb.updateFps[sdb] = table.updateFp; + tsSdb.deleteFps[sdb] = table.deleteFp; + tsSdb.deployFps[sdb] = table.deployFp; + tsSdb.encodeFps[sdb] = table.encodeFp; + tsSdb.decodeFps[sdb] = table.decodeFp; } #if 0 diff --git a/source/dnode/mnode/transaction/src/trn.c b/source/dnode/mnode/transaction/src/trn.c index c4db404e7b..1f65eb72ad 100644 --- a/source/dnode/mnode/transaction/src/trn.c +++ b/source/dnode/mnode/transaction/src/trn.c @@ -61,7 +61,7 @@ SSdbRaw *trnActionEncode(STrans *pTrans) { STrans *trnActionDecode(SSdbRaw *pRaw) { if (pRaw->sver != TRN_VER) { - terrno = TSDB_CODE_SDB_INVAID_RAW_DATA_VER; + terrno = TSDB_CODE_SDB_INVALID_DATA_VER; return NULL; } @@ -223,14 +223,14 @@ int32_t trnAppendUndoAction(STrans *pTrans, SEpSet *pEpSet, void *pMsg) { } int32_t trnInit() { - SSdbHandle handle = {.sdbType = SDB_TRANS, - .keyType = SDB_KEY_INT32, - .encodeFp = (SdbEncodeFp)trnActionEncode, - .decodeFp = (SdbDecodeFp)trnActionDecode, - .insertFp = (SdbInsertFp)trnActionInsert, - .updateFp = (SdbUpdateFp)trnActionUpdate, - .deleteFp = (SdbDeleteFp)trnActionDelete}; - sdbSetHandle(handle); + SSdbTable table = {.sdbType = SDB_TRANS, + .keyType = SDB_KEY_INT32, + .encodeFp = (SdbEncodeFp)trnActionEncode, + .decodeFp = (SdbDecodeFp)trnActionDecode, + .insertFp = (SdbInsertFp)trnActionInsert, + .updateFp = (SdbUpdateFp)trnActionUpdate, + .deleteFp = (SdbDeleteFp)trnActionDelete}; + sdbSetTable(table); return 0; } diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 7f396f4a53..2053554666 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -144,15 +144,16 @@ TAOS_DEFINE_ERROR(TSDB_CODE_MND_FAILED_TO_START_SYNC, "failed to start sync" TAOS_DEFINE_ERROR(TSDB_CODE_MND_FAILED_TO_CREATE_DIR, "failed to create mnode dir") TAOS_DEFINE_ERROR(TSDB_CODE_MND_FAILED_TO_INIT_STEP, "failed to init components") -TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INTERNAL_ERROR, "Unexpected generic error in sdb") -TAOS_DEFINE_ERROR(TSDB_CODE_SDB_OUT_OF_MEMORY, "Out of memory in sdb") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_APP_ERROR, "Unexpected generic error in sdb") TAOS_DEFINE_ERROR(TSDB_CODE_SDB_OBJ_ALREADY_THERE, "Object already there") TAOS_DEFINE_ERROR(TSDB_CODE_SDB_OBJ_NOT_THERE, "Object not there") -TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVAID_RAW_DATA_VER, "Invalid raw data version") -TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVAID_RAW_DATA_LEN, "Invalid raw data len") TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVALID_TABLE_TYPE, "Invalid table type") -TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVAID_META_ROW, "Invalid meta row") -TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVAID_KEY_TYPE, "Invalid key type") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVALID_KEY_TYPE, "Invalid key type") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVALID_ACTION_TYPE, "Invalid action type") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVALID_STATUS_TYPE, "Invalid status type") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVALID_DATA_VER, "Invalid raw data version") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVALID_DATA_LEN, "Invalid raw data len") +TAOS_DEFINE_ERROR(TSDB_CODE_SDB_INVALID_META_ROW, "Invalid meta row") TAOS_DEFINE_ERROR(TSDB_CODE_MND_DNODE_ALREADY_EXIST, "DNode already exists") TAOS_DEFINE_ERROR(TSDB_CODE_MND_DNODE_NOT_EXIST, "DNode does not exist")