diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index 4718d0e4b3..cf5a3c8e05 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/server/mnode/mnode.h b/include/dnode/mnode/mnode.h similarity index 95% rename from include/server/mnode/mnode.h rename to include/dnode/mnode/mnode.h index 09dd4a3f2d..20de27e59d 100644 --- a/include/server/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 new file mode 100644 index 0000000000..f4a8ccd06e --- /dev/null +++ b/include/dnode/mnode/sdb/sdb.h @@ -0,0 +1,147 @@ +/* + * 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_H_ +#define _TD_SDB_H_ + +#ifdef __cplusplus +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); \ + (pData) = (char *)(pData) + (valLen); \ + } else { \ + code = TSDB_CODE_SDB_INVALID_DATA_LEN; \ + } \ + } + +#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); \ + (pData) = (char *)(pData) + sizeof(int32_t); \ + } else { \ + code = TSDB_CODE_SDB_INVALID_DATA_LEN; \ + } \ + } + +#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); \ + (pData) = (char *)(pData) + sizeof(int64_t); \ + } else { \ + code = TSDB_CODE_SDB_INVALID_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 { + SDB_START = 0, + SDB_TRANS = 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_MAX = 12 +} ESdbType; + +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 = 2, SDB_STATUS_DROPPING = 3 } ESdbStatus; + +typedef struct { + int8_t type; + int8_t sver; + int8_t status; + int8_t action; + int8_t reserved[4]; + int32_t cksum; + int32_t dataLen; + char data[]; +} 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)(SSdbRaw *pRaw); +typedef SSdbRaw *(*SdbEncodeFp)(void *pObj); + +typedef struct { + ESdbType sdbType; + EKeyType keyType; + SdbDeployFp deployFp; + SdbEncodeFp encodeFp; + SdbDecodeFp decodeFp; + SdbInsertFp insertFp; + SdbUpdateFp updateFp; + SdbDeleteFp deleteFp; +} SSdbTable; + +int32_t sdbInit(); +void sdbCleanup(); +void sdbSetTable(SSdbTable table); + +int32_t sdbRead(); +int32_t sdbWrite(SSdbRaw *pRaw); +int32_t sdbCommit(); + +int32_t sdbDeploy(); +void sdbUnDeploy(); + +void *sdbAcquire(ESdbType sdb, void *pKey); +void sdbRelease(void *pObj); +void *sdbFetch(ESdbType sdb, void *pIter); +void sdbCancelFetch(ESdbType sdb, void *pIter); +int32_t sdbGetSize(ESdbType sdb); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_SDB_H_*/ diff --git a/include/dnode/mnode/transaction/trn.h b/include/dnode/mnode/transaction/trn.h new file mode 100644 index 0000000000..8ba043de12 --- /dev/null +++ b/include/dnode/mnode/transaction/trn.h @@ -0,0 +1,49 @@ +/* + * 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 STrans; +typedef enum { TRN_POLICY_ROLLBACK = 1, TRN_POLICY_RETRY = 2 } ETrnPolicy; + +int32_t trnInit(); +void trnCleanup(); + +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 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 +} +#endif + +#endif /*_TD_TRANSACTION_H_*/ 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/impl/metaImpl.h b/include/dnode/vnode/meta/impl/metaImpl.h similarity index 100% rename from include/server/vnode/meta/impl/metaImpl.h rename to include/dnode/vnode/meta/impl/metaImpl.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/source/dnode/mnode/inc/mnodeWorker.h b/include/dnode/vnode/tsdb/impl/tsdbImpl.h similarity index 72% rename from source/dnode/mnode/inc/mnodeWorker.h rename to include/dnode/vnode/tsdb/impl/tsdbImpl.h index 8477af6b72..15d868de9f 100644 --- a/source/dnode/mnode/inc/mnodeWorker.h +++ b/include/dnode/vnode/tsdb/impl/tsdbImpl.h @@ -13,22 +13,17 @@ * along with this program. If not, see . */ -#ifndef _TD_MNODE_WORKER_H_ -#define _TD_MNODE_WORKER_H_ - -#include "mnodeInt.h" +#ifndef _TD_TSDB_MEM_TABLE_H_ +#define _TD_TSDB_MEM_TABLE_H_ #ifdef __cplusplus extern "C" { #endif -int32_t mnodeInitWorker(); -void mnodeCleanupWorker(); -void mnodeSendRsp(SMnMsg *pMsg, int32_t code); -void mnodeReDispatchToWriteQueue(SMnMsg *pMsg); +typedef struct SMemTable SMemTable; #ifdef __cplusplus } #endif -#endif /*_TD_MNODE_WORKER_H_*/ +#endif /*_TD_TSDB_MEM_TABLE_H_*/ \ No newline at end of file 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/include/util/taoserror.h b/include/util/taoserror.h index 6c46a4c89f..b644f4c9a5 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()) @@ -59,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") @@ -120,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") @@ -131,12 +133,17 @@ 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_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") @@ -155,12 +162,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/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/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/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/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..4c9d44b39e --- /dev/null +++ b/source/dnode/mnode/impl/CMakeLists.txt @@ -0,0 +1,14 @@ +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 + PRIVATE sdb + PRIVATE transaction + 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 87% rename from source/dnode/mnode/inc/mnodeDef.h rename to source/dnode/mnode/impl/inc/mnodeDef.h index 0825815bc7..b6449ecfe7 100644 --- a/source/dnode/mnode/inc/mnodeDef.h +++ b/source/dnode/mnode/impl/inc/mnodeDef.h @@ -57,27 +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_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, @@ -97,16 +76,9 @@ 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; int64_t id; char uid[TSDB_CLUSTER_ID_LEN]; int64_t createdTime; @@ -114,7 +86,6 @@ typedef struct SClusterObj { } SClusterObj; typedef struct SDnodeObj { - SdbHead head; int32_t id; int32_t vnodes; int64_t createdTime; @@ -131,7 +102,6 @@ typedef struct SDnodeObj { } SDnodeObj; typedef struct SMnodeObj { - SdbHead head; int32_t id; int8_t status; int8_t role; @@ -147,8 +117,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 { @@ -161,18 +131,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]; @@ -207,7 +175,6 @@ typedef struct { } SDbCfg; typedef struct SDbObj { - SdbHead head; char name[TSDB_FULL_DB_NAME_LEN]; char acct[TSDB_USER_LEN]; int64_t createdTime; @@ -251,7 +218,6 @@ typedef struct SVgObj { } SVgObj; typedef struct SSTableObj { - SdbHead head; char tableId[TSDB_TABLE_NAME_LEN]; uint64_t uid; int64_t createdTime; @@ -262,7 +228,6 @@ typedef struct SSTableObj { } SSTableObj; typedef struct SFuncObj { - SdbHead head; char name[TSDB_FUNC_NAME_LEN]; char path[128]; int32_t contLen; @@ -299,8 +264,9 @@ typedef struct { void *rsp; } SMnRsp; -typedef struct SMnMsg { - void (*fp)(SMnMsg *pMsg, int32_t code); +typedef struct SMnodeMsg { + void (*fp)(SMnodeMsg *pMsg, int32_t code); + SRpcConnInfo conn; SUserObj *pUser; int16_t received; int16_t successed; @@ -311,7 +277,7 @@ typedef struct SMnMsg { SMnRsp rpcRsp; SRpcMsg rpcMsg; char pCont[]; -} SMnReq; +} SMnodeMsg; #ifdef __cplusplus } 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 81% rename from source/dnode/mnode/inc/mnodeInt.h rename to source/dnode/mnode/impl/inc/mnodeInt.h index 77ed79f82c..96803ba4a5 100644 --- a/source/dnode/mnode/inc/mnodeInt.h +++ b/source/dnode/mnode/impl/inc/mnodeInt.h @@ -17,22 +17,25 @@ #define _TD_MNODE_INT_H_ #include "mnodeDef.h" +#include "sdb.h" +#include "trn.h" #ifdef __cplusplus 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)(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/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/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 94% rename from source/dnode/mnode/inc/mnodeSync.h rename to source/dnode/mnode/impl/inc/mnodeSync.h index 714d6ed1a8..380ad36e23 100644 --- a/source/dnode/mnode/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/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/impl/src/mnode.c b/source/dnode/mnode/impl/src/mnode.c new file mode 100644 index 0000000000..21db85fcfa --- /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[TSDB_MSG_TYPE_MAX]; +} 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 new file mode 100644 index 0000000000..381d6a184e --- /dev/null +++ b/source/dnode/mnode/impl/src/mnodeAcct.c @@ -0,0 +1,130 @@ +/* + * 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 "mnodeInt.h" + +#define ACCT_VER 1 + +static SSdbRaw *mnodeAcctActionEncode(SAcctObj *pAcct) { + SSdbRaw *pRaw = calloc(1, sizeof(SAcctObj) + sizeof(SSdbRaw)); + if (pRaw == NULL) { + terrno = TSDB_CODE_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(SSdbRaw *pRaw) { + if (pRaw->sver != ACCT_VER) { + terrno = TSDB_CODE_SDB_INVALID_DATA_VER; + return NULL; + } + + SAcctObj *pAcct = calloc(1, sizeof(SAcctObj)); + if (pAcct == NULL) { + terrno = TSDB_CODE_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 = acctObj.createdTime; + acctObj.acctId = 1; + acctObj.cfg = (SAcctCfg){.maxUsers = 1024, + .maxDbs = 1024, + .maxTimeSeries = INT32_MAX, + .maxStreams = 8092, + .maxStorage = INT64_MAX, + .accessState = TSDB_VN_ALL_ACCCESS}; + + SSdbRaw *pRaw = mnodeAcctActionEncode(&acctObj); + if (pRaw == NULL) { + return -1; + } + + return sdbWrite(pRaw); +} + +int32_t mnodeInitAcct() { + 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; +} + +void mnodeCleanupAcct() {} 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/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 87% rename from source/dnode/mnode/src/mnodeSync.c rename to source/dnode/mnode/impl/src/mnodeSync.c index c161bb971a..7541ab6b59 100644 --- a/source/dnode/mnode/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/src/mnodeTelem.c b/source/dnode/mnode/impl/src/mnodeTelem.c similarity index 99% rename from source/dnode/mnode/src/mnodeTelem.c rename to source/dnode/mnode/impl/src/mnodeTelem.c index a3977f5b17..ef1ac10eb6 100644 --- a/source/dnode/mnode/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 new file mode 100644 index 0000000000..b15dc78ea9 --- /dev/null +++ b/source/dnode/mnode/impl/src/mnodeUser.c @@ -0,0 +1,250 @@ +/* + * 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 "mnodeSync.h" +#include "os.h" +#include "tglobal.h" +#include "tkey.h" + +#define USER_VER 1 + +static SSdbRaw *mnodeUserActionEncode(SUserObj *pUser) { + SSdbRaw *pRaw = calloc(1, sizeof(SUserObj) + sizeof(SSdbRaw)); + if (pRaw == NULL) { + terrno = TSDB_CODE_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(SSdbRaw *pRaw) { + if (pRaw->sver != USER_VER) { + terrno = TSDB_CODE_SDB_INVALID_DATA_VER; + return NULL; + } + + SUserObj *pUser = calloc(1, sizeof(SUserObj)); + if (pUser == NULL) { + terrno = TSDB_CODE_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) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + pUser->pAcct = sdbAcquire(SDB_ACCT, pUser->acct); + if (pUser->pAcct == NULL) { + terrno = TSDB_CODE_MND_ACCT_NOT_EXIST; + return -1; + } + + return 0; +} + +static int32_t mnodeUserActionDelete(SUserObj *pUser) { + if (pUser->prohibitDbHash) { + taosHashCleanup(pUser->prohibitDbHash); + pUser->prohibitDbHash = NULL; + } + + if (pUser->acct != NULL) { + sdbRelease(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) { + 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; + + if (strcmp(user, TSDB_DEFAULT_USER) == 0) { + userObj.rootAuth = 1; + } + + SSdbRaw *pRaw = mnodeUserActionEncode(&userObj); + if (pRaw == NULL) { + return -1; + } + + return sdbWrite(pRaw); +} + +static int32_t mnodeCreateDefaultUsers() { + if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, TSDB_DEFAULT_USER, TSDB_DEFAULT_PASS) != 0) { + return -1; + } + + if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "monitor", tsInternalPass) != 0) { + return -1; + } + + if (mnodeCreateDefaultUser(TSDB_DEFAULT_USER, "_" TSDB_DEFAULT_USER, tsInternalPass) != 0) { + return -1; + } + + return 0; +} + +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); + taosEncryptPass((uint8_t *)pass, strlen(pass), userObj.pass); + userObj.createdTime = taosGetTimestampMs(); + userObj.updateTime = userObj.createdTime; + userObj.rootAuth = 0; + + 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_CREATING; + pRedoRaw->action = SDB_ACTION_INSERT; + + 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; + + SSdbRaw *pCommitRaw = mnodeUserActionEncode(&userObj); + if (pCommitRaw == NULL || trnAppendCommitLog(pTrans, pCommitRaw) != 0) { + trnDrop(pTrans); + 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; + } + + trnDrop(pTrans); + return 0; +} + +static int32_t mnodeProcessCreateUserMsg(SMnodeMsg *pMsg) { + SCreateUserMsg *pCreate = pMsg->rpcMsg.pCont; + + if (pCreate->user[0] == 0) { + 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) { + 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); + 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->conn.user); + if (pOperUser == NULL) { + terrno = TSDB_CODE_MND_NO_USER_FROM_CONN; + mError("user:%s, failed to create since %s", pCreate->user, terrstr()); + return -1; + } + + 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, terrstr()); + return -1; + } + + return TSDB_CODE_MND_ACTION_IN_PROGRESS; +} + +int32_t mnodeInitUser() { + 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; +} + +void mnodeCleanupUser() {} \ No newline at end of file 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/test/mnodeTests.cpp b/source/dnode/mnode/impl/test/mnodeTests.cpp similarity index 100% rename from source/dnode/mnode/test/mnodeTests.cpp rename to source/dnode/mnode/impl/test/mnodeTests.cpp diff --git a/source/dnode/mnode/inc/mnodeSdb.h b/source/dnode/mnode/inc/mnodeSdb.h deleted file mode 100644 index cba9538ac2..0000000000 --- a/source/dnode/mnode/inc/mnodeSdb.h +++ /dev/null @@ -1,52 +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_SDB_H_ -#define _TD_MNODE_SDB_H_ - -#include "mnodeInt.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef void (*SdbDeployFp)(); -typedef void *(*SdbDecodeFp)(cJSON *root); -typedef int32_t (*SdbEncodeFp)(void *pHead, char *buf, int32_t maxLen); - -int32_t sdbInit(); -void sdbCleanup(); - -int32_t sdbRead(); -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); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_MNODE_INT_H_*/ diff --git a/source/dnode/mnode/sdb/CMakeLists.txt b/source/dnode/mnode/sdb/CMakeLists.txt new file mode 100644 index 0000000000..c5a0b7299a --- /dev/null +++ b/source/dnode/mnode/sdb/CMakeLists.txt @@ -0,0 +1,13 @@ +aux_source_directory(src MNODE_SRC) +add_library(sdb ${MNODE_SRC}) +target_include_directories( + sdb + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/mnode/sdb" + private "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + sdb + PRIVATE os + PRIVATE common + PRIVATE util +) \ No newline at end of file diff --git a/source/dnode/mnode/sdb/inc/sdbInt.h b/source/dnode/mnode/sdb/inc/sdbInt.h new file mode 100644 index 0000000000..222e16abe9 --- /dev/null +++ b/source/dnode/mnode/sdb/inc/sdbInt.h @@ -0,0 +1,67 @@ +/* + * 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 "sdb.h" +#include "taosmsg.h" +#include "thash.h" +#include "tlockfree.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__); }} + +#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]; +} SSdbMgr; + +typedef struct { + ESdbStatus status; + int32_t refCount; + int32_t dataLen; + char pData[]; +} SSdbRow; + +#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 new file mode 100644 index 0000000000..7489a46552 --- /dev/null +++ b/source/dnode/mnode/sdb/src/sdb.c @@ -0,0 +1,416 @@ +/* + * 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 "sdbInt.h" +#include "tglobal.h" + +static SSdbMgr tsSdb = {0}; + +static int32_t sdbCreateDir() { + if (!taosMkDir(tsSdb.currDir)) { + terrno = TAOS_SYSTEM_ERROR(errno); + mError("failed to create dir:%s since %s", tsSdb.currDir, terrstr()); + return -1; + } + + if (!taosMkDir(tsSdb.syncDir)) { + terrno = TAOS_SYSTEM_ERROR(errno); + mError("failed to create dir:%s since %s", tsSdb.syncDir, terrstr()); + return -1; + } + + if (!taosMkDir(tsSdb.tmpDir)) { + terrno = TAOS_SYSTEM_ERROR(errno); + mError("failed to create dir:%s since %s", tsSdb.tmpDir, terrstr()); + return -1; + } + + return 0; +} + +static int32_t sdbRunDeployFp() { + for (int32_t i = SDB_START; i < SDB_MAX; ++i) { + SdbDeployFp fp = tsSdb.deployFps[i]; + if (fp == NULL) continue; + if ((*fp)() != 0) { + mError("failed to deploy sdb:%d since %s", i, terrstr()); + return -1; + } + } + + return 0; +} + +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; + } + + return hash; +} + +int32_t sdbWrite(SSdbRaw *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 int32_t sdbWriteVersion(FileFd fd) { return 0; } + +static int32_t sdbReadVersion(FileFd fd) { return 0; } + +static int32_t sdbReadDataFile() { + int32_t code = 0; + + SSdbRaw *pRaw = malloc(SDB_MAX_SIZE); + if (pRaw == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + 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(SSdbRaw)); + if (ret == 0) break; + + if (ret < 0) { + code = TAOS_SYSTEM_ERROR(errno); + mError("failed to read file:%s since %s", file, tstrerror(code)); + break; + } + + if (ret < sizeof(SSdbRaw)) { + code = TSDB_CODE_SDB_APP_ERROR; + mError("failed to read file:%s since %s", file, tstrerror(code)); + break; + } + + code = sdbWrite(pRaw); + if (code != 0) { + mError("failed to read file:%s since %s", file, tstrerror(code)); + goto PARSE_SDB_DATA_ERROR; + } + } + + code = 0; + +PARSE_SDB_DATA_ERROR: + taosCloseFile(fd); + return code; +} + +static int32_t sdbWriteDataFile() { + int32_t code = 0; + + char tmpfile[PATH_MAX] = {0}; + snprintf(tmpfile, sizeof(tmpfile), "%ssdb.data", tsSdb.tmpDir); + + FileFd fd = taosOpenFileCreateWrite(tmpfile); + if (fd <= 0) { + code = TAOS_SYSTEM_ERROR(errno); + mError("failed to open file:%s for write since %s", tmpfile, tstrerror(code)); + return code; + } + + for (int32_t i = SDB_MAX - 1; i > SDB_START; --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; + SSdbRaw *pRaw = (*encodeFp)(pRow->pData); + if (pRaw != NULL) { + taosWriteFile(fd, pRaw, sizeof(SSdbRaw) + pRaw->dataLen); + } else { + taosHashCancelIterate(hash, pRow); + code = TSDB_CODE_SDB_APP_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() { + int32_t code = sdbReadDataFile(); + if (code != 0) { + return code; + } + + mInfo("read sdb file successfully"); + return -1; +} + +int32_t sdbCommit() { + int32_t code = sdbWriteDataFile(); + if (code != 0) { + return code; + } + + return 0; +} + +int32_t sdbDeploy() { + if (sdbCreateDir() != 0) { + return -1; + } + + if (sdbRunDeployFp() != 0) { + return -1; + } + + if (sdbCommit() != 0) { + return -1; + } + + return 0; +} + +void sdbUnDeploy() {} + +int32_t sdbInit() { + char path[PATH_MAX + 100]; + + 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); + 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_OUT_OF_MEMORY; + } + + for (int32_t i = 0; i < SDB_MAX; ++i) { + int32_t type; + if (tsSdb.keyTypes[i] == SDB_KEY_INT32) { + type = TSDB_DATA_TYPE_INT; + } else if (tsSdb.keyTypes[i] == SDB_KEY_INT64) { + type = TSDB_DATA_TYPE_BIGINT; + } else { + type = TSDB_DATA_TYPE_BINARY; + } + + SHashObj *hash = taosHashInit(128, taosGetDefaultHashFunction(type), true, HASH_NO_LOCK); + if (hash == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + tsSdb.hashObjs[i] = hash; + taosInitRWLatch(&tsSdb.locks[i]); + } + + return 0; +} + +void sdbCleanup() { + 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.hashObjs[i] = NULL; + } +} + +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 +void *sdbInsertRow(ESdbType sdb, void *p) { + SdbHead *pHead = p; + pHead->type = sdb; + pHead->status = SDB_AVAIL; + + 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 == SDBINT32) { + keySize = sizeof(int32_t); + } else if (keyType == SDB_KEY_BINARY) { + keySize = strlen(pKey) + 1; + } else { + keySize = sizeof(int64_t); + } + + taosHashPut(hash, pKey, keySize, pHead, dataSize); + return taosHashGet(hash, pKey, keySize); +} + +void sdbDeleteRow(ESdbType sdb, void *p) { + SdbHead *pHead = p; + pHead->status = SDB_STATUS_DROPPED; +} + +void *sdbUpdateRow(ESdbType sdb, void *pHead) { return sdbInsertRow(sdb, pHead); } + +#endif + +void *sdbAcquire(ESdbType sdb, void *pKey) { + terrno = 0; + + SHashObj *hash = sdbGetHash(sdb); + if (hash == NULL) { + return NULL; + } + + int32_t keySize; + EKeyType keyType = tsSdb.keyTypes[sdb]; + + 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); + } + + 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->pData; + } else { + terrno = -1; // todo + return NULL; + } +} + +void sdbRelease(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; + } + + return taosHashIterate(hash, pIter); +} + +void sdbCancelFetch(ESdbType sdb, void *pIter) { + SHashObj *hash = sdbGetHash(sdb); + if (hash == NULL) { + return; + } + taosHashCancelIterate(hash, pIter); +} + +int32_t sdbGetSize(ESdbType sdb) { + SHashObj *hash = sdbGetHash(sdb); + if (hash == NULL) { + return 0; + } + return taosHashGetSize(hash); +} \ No newline at end of file diff --git a/source/dnode/mnode/src/mnodeAcct.c b/source/dnode/mnode/src/mnodeAcct.c deleted file mode 100644 index 39db95b6d1..0000000000 --- a/source/dnode/mnode/src/mnodeAcct.c +++ /dev/null @@ -1,148 +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 "mnodeSdb.h" - -static void mnodeCreateDefaultAcct() { - int32_t code = TSDB_CODE_SUCCESS; - - SAcctObj acctObj = {0}; - tstrncpy(acctObj.acct, TSDB_DEFAULT_USER, TSDB_USER_LEN); - 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; - } - 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; -} - -int32_t mnodeInitAcct() { - sdbSetFp(MN_SDB_ACCT, MN_KEY_BINARY, mnodeCreateDefaultAcct, (SdbEncodeFp)mnodeEncodeAcct, - (SdbDecodeFp)(mnodeDecodeAcct), sizeof(SAcctObj)); - - return 0; -} - -void mnodeCleanupAcct() {} diff --git a/source/dnode/mnode/src/mnodeSdb.c b/source/dnode/mnode/src/mnodeSdb.c deleted file mode 100644 index f676729469..0000000000 --- a/source/dnode/mnode/src/mnodeSdb.c +++ /dev/null @@ -1,395 +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 "thash.h" -#include "tglobal.h" -#include "cJSON.h" -#include "mnodeSdb.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 int32_t sdbCreateDir() { - if (!taosMkDir(tsSdb.currDir)) { - mError("failed to create dir:%s", tsSdb.currDir); - return TAOS_SYSTEM_ERROR(errno); - } - - if (!taosMkDir(tsSdb.backDir)) { - mError("failed to create dir:%s", tsSdb.backDir); - return -1; - } - - if (!taosMkDir(tsSdb.tmpDir)) { - mError("failed to create dir:%s", tsSdb.tmpDir); - return -1; - } - - return 0; -} - -static int32_t sdbRunDeployFp() { - for (int32_t i = MN_SDB_START; i < MN_SDB_MAX; ++i) { - SdbDeployFp fp = tsSdb.deployFp[i]; - if (fp) { - (*fp)(); - } - } - - 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; - } - - tsSdb.version = (int64_t)atoll(ver->valuestring); - mTrace("parse version success, version:%" PRIu64, tsSdb.version); - - 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 sdbReadDataFile() { - ssize_t _bytes = 0; - size_t len = 4096; - char *line = calloc(1, len); - int32_t code = -1; - FILE *fp = NULL; - cJSON *root = NULL; - - 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; - } - - while (!feof(fp)) { - memset(line, 0, len); - _bytes = tgetline(&line, &len, fp); - if (_bytes < 0) { - break; - } - - line[len - 1] = 0; - if (len <= 10) continue; - - root = cJSON_Parse(line); - if (root == NULL) { - mError("failed to parse since invalid json format, %s", line); - 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); - - return code; -} - -static int32_t sdbWriteDataFile() { - char file[PATH_MAX + 20] = {0}; - snprintf(file, sizeof(file), "%ssdb.data", tsSdb.currDir); - FileFd fd = taosOpenFileCreateWrite(file); - 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) { - return code; - } - - return 0; -} - -int32_t sdbRead() { - int32_t code = sdbReadDataFile(); - if (code != 0) { - return code; - } - - mInfo("read sdb file successfully"); - return -1; -} - -int32_t sdbDeploy() { - if (sdbCreateDir() != 0) { - return -1; - } - - if (sdbRunDeployFp() != 0) { - return -1; - } - - if (sdbCommit() != 0) { - 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); - - for (int32_t i = 0; i < MN_SDB_MAX; ++i) { - int32_t type; - if (tsSdb.hashKey[i] == MN_KEY_INT32) { - type = TSDB_DATA_TYPE_INT; - } else if (tsSdb.hashKey[i] == MN_KEY_INT64) { - type = TSDB_DATA_TYPE_BIGINT; - } else { - type = TSDB_DATA_TYPE_BINARY; - } - - SHashObj *hash = taosHashInit(128, taosGetDefaultHashFunction(type), true, HASH_NO_LOCK); - if (hash == NULL) { - return -1; - } - - tsSdb.hashObj[i] = hash; - } - - return 0; -} - -void sdbCleanup() { - for (int32_t i = 0; i < MN_SDB_MAX; ++i) { - SHashObj *hash = tsSdb.hashObj[i]; - if (hash != NULL) { - taosHashCleanup(hash); - } - tsSdb.hashObj[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; -} - -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) { - SdbHead *pHead = p; - pHead->type = sdb; - pHead->status = MN_SDB_STAT_AVAIL; - - char *pKey = (char *)pHead + sizeof(pHead); - int32_t keySize; - EMnKey keyType = tsSdb.hashKey[pHead->type]; - int32_t dataSize = tsSdb.dataSize[pHead->type]; - - SHashObj *hash = sdbGetHash(pHead->type); - if (hash == NULL) { - return NULL; - } - - if (keyType == MN_KEY_INT32) { - keySize = sizeof(int32_t); - } else if (keyType == MN_KEY_BINARY) { - keySize = strlen(pKey) + 1; - } else { - keySize = sizeof(int64_t); - } - - taosHashPut(hash, pKey, keySize, pHead, dataSize); - return taosHashGet(hash, pKey, keySize); -} - -void sdbDeleteRow(EMnSdb sdb, void *p) { - SdbHead *pHead = p; - pHead->status = MN_SDB_STAT_DROPPED; -} - -void *sdbUpdateRow(EMnSdb sdb, void *pHead) { return sdbInsertRow(sdb, pHead); } - -void *sdbGetRow(EMnSdb sdb, void *pKey) { - SHashObj *hash = sdbGetHash(sdb); - if (hash == NULL) { - return NULL; - } - - int32_t keySize; - EMnKey keyType = tsSdb.hashKey[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); - } - - return taosHashGet(hash, pKey, keySize); -} - -void *sdbFetchRow(EMnSdb sdb, void *pIter) { - SHashObj *hash = sdbGetHash(sdb); - if (hash == NULL) { - return NULL; - } - - return taosHashIterate(hash, pIter); -} - -void sdbCancelFetch(EMnSdb sdb, void *pIter) { - SHashObj *hash = sdbGetHash(sdb); - if (hash == NULL) { - return; - } - - taosHashCancelIterate(hash, pIter); -} - -int32_t sdbGetCount(EMnSdb sdb) { - SHashObj *hash = sdbGetHash(sdb); - if (hash == NULL) { - return 0; - } - return taosHashGetSize(hash); -} \ No newline at end of file diff --git a/source/dnode/mnode/src/mnodeUser.c b/source/dnode/mnode/src/mnodeUser.c deleted file mode 100644 index 381a16ae26..0000000000 --- a/source/dnode/mnode/src/mnodeUser.c +++ /dev/null @@ -1,130 +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 "tkey.h" -#include "tglobal.h" -#include "mnodeSdb.h" - -static int32_t mnodeCreateDefaultUser(char *acct, char *user, char *pass) { - int32_t code = TSDB_CODE_SUCCESS; - - 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 = taosGetTimestampMs(); - - if (strcmp(user, TSDB_DEFAULT_USER) == 0) { - 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; - } else { - memcpy(pUser->pass, base64, outlen); - free(base64); - } - - 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); - - 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); - - 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 = 0; - mTrace("user:%s, parse success", pUser->user); - -DECODE_USER_OVER: - if (code != 0) { - free(pUser); - pUser = NULL; - } - return pUser; -} - -int32_t mnodeInitUser() { - sdbSetFp(MN_SDB_USER, MN_KEY_BINARY, mnodeCreateDefaultUsers, (SdbEncodeFp)mnodeEncodeUser, - (SdbDecodeFp)(mnodeDecodeUser), sizeof(SUserObj)); - return 0; -} - -void mnodeCleanupUser() {} \ No newline at end of file diff --git a/source/dnode/mnode/src/mnodeWorker.c b/source/dnode/mnode/src/mnodeWorker.c deleted file mode 100644 index bdf0e869fc..0000000000 --- a/source/dnode/mnode/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 "mnodeSdb.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 = sdbGetRow(MN_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_INVALID_USER}; - 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_INVALID_USER}; - 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_INVALID_USER}; - 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_INVALID_USER}; - 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/src/mondeInt.c b/source/dnode/mnode/src/mondeInt.c deleted file mode 100644 index eed710a1bd..0000000000 --- a/source/dnode/mnode/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 "mnodeSdb.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_SDB_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_SDB_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/transaction/CMakeLists.txt b/source/dnode/mnode/transaction/CMakeLists.txt new file mode 100644 index 0000000000..d35a8c9b3f --- /dev/null +++ b/source/dnode/mnode/transaction/CMakeLists.txt @@ -0,0 +1,15 @@ +aux_source_directory(src MNODE_SRC) +add_library(transaction ${MNODE_SRC}) +target_include_directories( + transaction + PUBLIC "${CMAKE_SOURCE_DIR}/include/dnode/mnode/transaction" + private "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + transaction + PRIVATE os + 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 new file mode 100644 index 0000000000..03860734ee --- /dev/null +++ b/source/dnode/mnode/transaction/inc/trnInt.h @@ -0,0 +1,70 @@ +/* + * 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 "tarray.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__); }} + +#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 { + 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 +} +#endif + +#endif /*_TD_TRANSACTION_INT_H_*/ diff --git a/source/dnode/mnode/transaction/src/trn.c b/source/dnode/mnode/transaction/src/trn.c new file mode 100644 index 0000000000..1f65eb72ad --- /dev/null +++ b/source/dnode/mnode/transaction/src/trn.c @@ -0,0 +1,238 @@ +/* + * 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_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_INVALID_DATA_VER; + return NULL; + } + + STrans *pTrans = NULL; + if (pTrans == NULL) { + terrno = TSDB_CODE_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_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_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; } + +STrans *trnCreate(ETrnPolicy policy) { + STrans *pTrans = calloc(1, sizeof(STrans)); + if (pTrans == NULL) { + terrno = TSDB_CODE_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_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) { pTrans->rpcHandle = rpcHandle; } + +static int32_t trnAppendArray(SArray *pArray, SSdbRaw *pRaw) { + if (pArray == NULL || pRaw == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + void *ptr = taosArrayPush(pArray, &pRaw); + if (ptr == NULL) { + terrno = TSDB_CODE_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() { + 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; +} + +void trnCleanup() {} diff --git a/source/dnode/mnode/transaction/src/trnExec.c b/source/dnode/mnode/transaction/src/trnExec.c new file mode 100644 index 0000000000..6bb46f1283 --- /dev/null +++ b/source/dnode/mnode/transaction/src/trnExec.c @@ -0,0 +1,189 @@ +/* + * 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 "trpc.h" + +int32_t trnPrepare(STrans *pTrans, int32_t (*syncfp)(SSdbRaw *pRaw, void *pData)) { + if (syncfp == NULL) return -1; + + SSdbRaw *pRaw = trnActionEncode(pTrans); + if (pRaw == NULL) { + mError("tranId:%d, failed to decode trans since %s", pTrans->id, terrstr()); + return -1; + } + + if (sdbWrite(pRaw) != 0) { + mError("tranId:%d, failed to write trans since %s", pTrans->id, terrstr()); + return -1; + } + + if ((*syncfp)(pRaw, pTrans->rpcHandle) != 0) { + mError("tranId:%d, failed to sync trans since %s", pTrans->id, terrstr()); + return -1; + } + + return 0; +} + +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) { + trnSendRpcRsp(pData, terrno); + return 0; + } + + if (sdbWrite(pRaw) != 0) { + code = terrno; + trnSendRpcRsp(pData, code); + terrno = code; + return -1; + } + + 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) { + return -1; + } + + if (pTrans->stage == TRN_STAGE_PREPARE) { + if (trnPerformPrepareStage(pTrans) != 0) { + sdbRelease(pTrans); + return -1; + } + } + + if (pTrans->stage == TRN_STAGE_EXECUTE) { + if (trnPerformExecuteStage(pTrans) != 0) { + sdbRelease(pTrans); + return -1; + } + } + + if (pTrans->stage == TRN_STAGE_COMMIT) { + 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 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 fa26b18d0b..040b02d2b6 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 e4ea7839c8..625c07ad56 100644 --- a/source/dnode/vnode/meta/test/CMakeLists.txt +++ b/source/dnode/vnode/meta/test/CMakeLists.txt @@ -21,4 +21,4 @@ # add_test( # NAME meta_test # COMMAND metaTest -# ) \ No newline at end of file +# ) diff --git a/source/dnode/vnode/tq/CMakeLists.txt b/source/dnode/vnode/tq/CMakeLists.txt index f9e86adc73..536e97d5f7 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( 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..2053554666 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") @@ -142,12 +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_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_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_INVALID_TABLE_TYPE, "Invalid table 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") @@ -166,12 +172,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") @@ -521,3 +527,5 @@ const char* tstrerror(int32_t err) { return ""; } + +const char* terrstr() { return tstrerror(terrno); } \ No newline at end of file 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; }