From 055b9b1b69e2706989be832716c28cc820868a28 Mon Sep 17 00:00:00 2001 From: slguan Date: Mon, 9 Mar 2020 21:17:46 +0800 Subject: [PATCH 1/5] add some code in dnode module --- src/dnode/inc/dnodeRead.h | 11 +++-- src/dnode/inc/dnodeWrite.h | 11 +++-- src/dnode/src/dnodeMgmt.c | 83 ++++++++++++++++++++---------------- src/dnode/src/dnodeRead.c | 73 +++++++++++++++---------------- src/dnode/src/dnodeWrite.c | 63 +++++++++++++-------------- src/inc/taosmsg.h | 4 +- src/mnode/src/mgmtDnodeInt.c | 4 +- 7 files changed, 127 insertions(+), 122 deletions(-) diff --git a/src/dnode/inc/dnodeRead.h b/src/dnode/inc/dnodeRead.h index a5c7be74b5..e4f32b0f8c 100644 --- a/src/dnode/inc/dnodeRead.h +++ b/src/dnode/inc/dnodeRead.h @@ -20,12 +20,11 @@ extern "C" { #endif -int dnodeInitRead(); -void dnodeCleanupRead(); -void dnodeRead(SRpcMsg *); -void *dnodeAllocateReadWorker(); -void dnodeFreeReadWorker(void *rqueue); - +int32_t dnodeInitRead(); +void dnodeCleanupRead(); +void dnodeRead(void *pMsg); +void * dnodeAllocateReadWorker(); +void dnodeFreeReadWorker(void *rqueue); #ifdef __cplusplus } diff --git a/src/dnode/inc/dnodeWrite.h b/src/dnode/inc/dnodeWrite.h index 51340fe1c2..2b1edf9e40 100644 --- a/src/dnode/inc/dnodeWrite.h +++ b/src/dnode/inc/dnodeWrite.h @@ -20,12 +20,11 @@ extern "C" { #endif -int dnodeInitWrite(); -void dnodeCleanupWrite(); -void dnodeWrite(SRpcMsg *pMsg); -void *dnodeAllocateWriteWorker(); -void dnodeFreeWriteWorker(void *worker); - +int32_t dnodeInitWrite(); +void dnodeCleanupWrite(); +void dnodeWrite(void *pMsg); +void * dnodeAllocateWriteWorker(); +void dnodeFreeWriteWorker(void *worker); #ifdef __cplusplus } diff --git a/src/dnode/src/dnodeMgmt.c b/src/dnode/src/dnodeMgmt.c index 25055643df..df65ddeac1 100644 --- a/src/dnode/src/dnodeMgmt.c +++ b/src/dnode/src/dnodeMgmt.c @@ -56,7 +56,7 @@ static void * tsDnodeVnodesHash = NULL; int32_t dnodeInitMgmt() { dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_CREATE_VNODE] = dnodeProcessCreateVnodeMsg; dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_DROP_VNODE] = dnodeProcessDropVnodeMsg; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_ALTER_VNODE] = dnodeProcessDropVnodeMsg; + dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_ALTER_VNODE] = dnodeProcessAlterVnodeMsg; tsDnodeVnodesHash = taosInitIntHash(TSDB_MAX_VNODES, sizeof(SVnodeObj), taosHashInt); if (tsDnodeVnodesHash == NULL) { @@ -176,26 +176,24 @@ static void dnodeCleanupVnode(SVnodeObj *pVnode) { // remove read queue dnodeFreeReadWorker(pVnode->rworker); + pVnode->rworker = NULL; // remove write queue dnodeFreeWriteWorker(pVnode->wworker); + pVnode->wworker = NULL; // remove wal // remove tsdb if (pVnode->tsdb) { tsdbCloseRepo(pVnode->tsdb); + pVnode->tsdb = NULL; } taosDeleteIntHash(tsDnodeVnodesHash, pVnode->vgId); } static int32_t dnodeCreateVnode(SCreateVnodeMsg *pVnodeCfg) { - pVnodeCfg->vnode = htonl(pVnodeCfg->vnode); - pVnodeCfg->cfg.vgId = htonl(pVnodeCfg->cfg.vgId); - pVnodeCfg->cfg.maxSessions = htonl(pVnodeCfg->cfg.maxSessions); - pVnodeCfg->cfg.daysPerFile = htonl(pVnodeCfg->cfg.daysPerFile); - STsdbCfg tsdbCfg; tsdbCfg.precision = pVnodeCfg->cfg.precision; tsdbCfg.tsdbId = pVnodeCfg->vnode; @@ -248,47 +246,60 @@ static void dnodeDropVnode(SVnodeObj *pVnode) { dnodeCleanupVnode(pVnode); } -static void dnodeProcessCreateVnodeMsg(SRpcMsg *pMsg) { +static void dnodeProcessCreateVnodeMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// SVnodeObj *pVnode; -// int32_t vgId; -// SVPeersMsg *pCfg; - - // check everything, if not ok, set terrno; + SCreateVnodeMsg *pCreate = (SCreateVnodeMsg *) rpcMsg->pCont; + pCreate->vnode = htonl(pCreate->vnode); + pCreate->cfg.vgId = htonl(pCreate->cfg.vgId); + pCreate->cfg.maxSessions = htonl(pCreate->cfg.maxSessions); + pCreate->cfg.daysPerFile = htonl(pCreate->cfg.daysPerFile); + SVnodeObj *pVnodeObj = taosGetIntHashData(tsDnodeVnodesHash, pCreate->cfg.vgId); + if (pVnodeObj != NULL) { + rpcRsp.code = TSDB_CODE_SUCCESS; + } else { + rpcRsp.code = dnodeCreateVnode(pCreate); + } - // everything is ok - -// dnodeCreateVnode(vgId, pCfg); - - //if (pVnode == NULL) terrno = TSDB_CODE + rpcSendResponse(&rpcRsp); + rpcFreeCont(rpcMsg->pCont); } -static void dnodeProcessDropVnodeMsg(SRpcMsg *pMsg) { +static void dnodeProcessDropVnodeMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SVnodeObj *pVnode; - int32_t vgId; - - // check everything, if not ok, set terrno; + SDropVnodeMsg *pDrop = (SCreateVnodeMsg *) rpcMsg->pCont; + pDrop->vgId = htonl(pDrop->vgId); + SVnodeObj *pVnodeObj = taosGetIntHashData(tsDnodeVnodesHash, pDrop->vgId); + if (pVnodeObj != NULL) { + dnodeDropVnode(pVnodeObj); + rpcRsp.code = TSDB_CODE_SUCCESS; + } else { + rpcRsp.code = TSDB_CODE_INVALID_VGROUP_ID; + } - // everything is ok - dnodeDropVnode(pVnode); - - //if (pVnode == NULL) terrno = TSDB_CODE + rpcSendResponse(&rpcRsp); + rpcFreeCont(rpcMsg->pCont); } -static void dnodeProcessAlterVnodeMsg(SRpcMsg *pMsg) { +static void dnodeProcessAlterVnodeMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SVnodeObj *pVnode; - int32_t vgId; - - // check everything, if not ok, set terrno; + SCreateVnodeMsg *pCreate = (SCreateVnodeMsg *) rpcMsg->pCont; + pCreate->vnode = htonl(pCreate->vnode); + pCreate->cfg.vgId = htonl(pCreate->cfg.vgId); + pCreate->cfg.maxSessions = htonl(pCreate->cfg.maxSessions); + pCreate->cfg.daysPerFile = htonl(pCreate->cfg.daysPerFile); + SVnodeObj *pVnodeObj = taosGetIntHashData(tsDnodeVnodesHash, pCreate->cfg.vgId); + if (pVnodeObj != NULL) { + rpcRsp.code = TSDB_CODE_SUCCESS; + } else { + rpcRsp.code = dnodeCreateVnode(pCreate);; + } - // everything is ok -// dnodeAlterVnode(pVnode); - - //if (pVnode == NULL) terrno = TSDB_CODE + rpcSendResponse(&rpcRsp); + rpcFreeCont(rpcMsg->pCont); } - diff --git a/src/dnode/src/dnodeRead.c b/src/dnode/src/dnodeRead.c index 8175bea691..dacc93ffc2 100644 --- a/src/dnode/src/dnodeRead.c +++ b/src/dnode/src/dnodeRead.c @@ -16,10 +16,10 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taoserror.h" -#include "tlog.h" -#include "trpc.h" #include "taosmsg.h" +#include "tlog.h" #include "tqueue.h" +#include "trpc.h" #include "dnodeRead.h" #include "dnodeMgmt.h" @@ -31,7 +31,7 @@ typedef struct { typedef struct { void *pCont; - int contLen; + int32_t contLen; SRpcMsg rpcMsg; void *pVnode; SRpcContext *pRpcContext; // RPC message context @@ -42,16 +42,16 @@ static void dnodeProcessReadResult(SReadMsg *pRead); static void dnodeHandleIdleReadWorker(); static void dnodeProcessQueryMsg(SReadMsg *pMsg); static void dnodeProcessRetrieveMsg(SReadMsg *pMsg); -static void (*dnodeProcessReadMsgFp[TSDB_MSG_TYPE_MAX])(SReadMsg *pNode); +static void(*dnodeProcessReadMsgFp[TSDB_MSG_TYPE_MAX])(SReadMsg *pNode); // module global variable static taos_qset readQset; -static int threads; // number of query threads -static int maxThreads; -static int minThreads; +static int32_t threads; // number of query threads +static int32_t maxThreads; +static int32_t minThreads; -int dnodeInitRead() { - dnodeProcessReadMsgFp[TSDB_MSG_TYPE_QUERY] = dnodeProcessQueryMsg; +int32_t dnodeInitRead() { + dnodeProcessReadMsgFp[TSDB_MSG_TYPE_QUERY] = dnodeProcessQueryMsg; dnodeProcessReadMsgFp[TSDB_MSG_TYPE_RETRIEVE] = dnodeProcessRetrieveMsg; readQset = taosOpenQset(); @@ -67,12 +67,14 @@ void dnodeCleanupRead() { taosCloseQset(readQset); } -void dnodeRead(SRpcMsg *pMsg) { - int leftLen = pMsg->contLen; - char *pCont = (char *)pMsg->pCont; - int contLen = 0; - int numOfVnodes = 0; - int32_t vgId = 0; +void dnodeRead(void *rpcMsg) { + SRpcMsg *pMsg = rpcMsg; + + int32_t leftLen = pMsg->contLen; + char *pCont = (char *) pMsg->pCont; + int32_t contLen = 0; + int32_t numOfVnodes = 0; + int32_t vgId = 0; SRpcContext *pRpcContext = NULL; // parse head, get number of vnodes; @@ -87,31 +89,31 @@ void dnodeRead(SRpcMsg *pMsg) { // get pVnode from vgId void *pVnode = dnodeGetVnode(vgId); if (pVnode == NULL) { - continue; } - + // put message into queue SReadMsg readMsg; - readMsg.rpcMsg = *pMsg; - readMsg.pCont = pCont; - readMsg.contLen = contLen; - readMsg.pRpcContext = pRpcContext; - readMsg.pVnode = pVnode; + readMsg.rpcMsg = *pMsg; + readMsg.pCont = pCont; + readMsg.contLen = contLen; + readMsg.pRpcContext = pRpcContext; + readMsg.pVnode = pVnode; taos_queue queue = dnodeGetVnodeRworker(pVnode); taosWriteQitem(queue, &readMsg); - - // next vnode + + // next vnode leftLen -= contLen; - pCont -= contLen; + pCont -= contLen; + + dnodeReleaseVnode(pVnode); } } void *dnodeAllocateReadWorker() { - taos_queue *queue = taosOpenQueue(sizeof(SReadMsg)); - if ( queue == NULL ) return NULL; + if (queue == NULL) return NULL; taosAddIntoQset(readQset, queue); @@ -131,7 +133,6 @@ void *dnodeAllocateReadWorker() { } void dnodeFreeReadWorker(void *rqueue) { - taosCloseQueue(rqueue); // dynamically adjust the number of threads @@ -144,16 +145,16 @@ static void *dnodeProcessReadQueue(void *param) { while (1) { if (taosReadQitemFromQset(qset, &readMsg) <= 0) { dnodeHandleIdleReadWorker(); - continue; + continue; } terrno = 0; if (dnodeProcessReadMsgFp[readMsg.rpcMsg.msgType]) { (*dnodeProcessReadMsgFp[readMsg.rpcMsg.msgType]) (&readMsg); } else { - terrno = TSDB_CODE_MSG_NOT_PROCESSED; + terrno = TSDB_CODE_MSG_NOT_PROCESSED; } - + dnodeProcessReadResult(&readMsg); } @@ -161,7 +162,7 @@ static void *dnodeProcessReadQueue(void *param) { } static void dnodeHandleIdleReadWorker() { - int num = taosGetQueueNumber(readQset); + int32_t num = taosGetQueueNumber(readQset); if (num == 0 || (num <= minThreads && threads > minThreads)) { threads--; @@ -180,10 +181,10 @@ static void dnodeProcessReadResult(SReadMsg *pRead) { if (pRpcContext) { if (terrno) { - if (pRpcContext->code == 0) pRpcContext->code = terrno; + if (pRpcContext->code == 0) pRpcContext->code = terrno; } - int count = atomic_add_fetch_32(&pRpcContext->count, 1); + int32_t count = atomic_add_fetch_32(&pRpcContext->count, 1); if (count < pRpcContext->numOfVnodes) { // not over yet, multiple vnodes return; @@ -197,8 +198,8 @@ static void dnodeProcessReadResult(SReadMsg *pRead) { SRpcMsg rsp; rsp.handle = pRead->rpcMsg.handle; - rsp.code = code; - rsp.pCont = NULL; + rsp.code = code; + rsp.pCont = NULL; rpcSendResponse(&rsp); rpcFreeCont(pRead->rpcMsg.pCont); // free the received message } diff --git a/src/dnode/src/dnodeWrite.c b/src/dnode/src/dnodeWrite.c index b453fdff17..f86691d3d9 100644 --- a/src/dnode/src/dnodeWrite.c +++ b/src/dnode/src/dnodeWrite.c @@ -15,11 +15,11 @@ #define _DEFAULT_SOURCE #include "os.h" +#include "taosmsg.h" #include "taoserror.h" #include "tlog.h" -#include "trpc.h" #include "tqueue.h" -#include "taosmsg.h" +#include "trpc.h" #include "dnodeWrite.h" #include "dnodeMgmt.h" @@ -31,7 +31,7 @@ typedef struct { typedef struct _write { void *pCont; - int contLen; + int32_t contLen; SRpcMsg rpcMsg; void *pVnode; // pointer to vnode SRpcContext *pRpcContext; // RPC message context @@ -40,12 +40,12 @@ typedef struct _write { typedef struct { taos_qset qset; // queue set pthread_t thread; // thread - int workerId; // worker ID + int32_t workerId; // worker ID } SWriteWorker; typedef struct _thread_obj { - int max; // max number of workers - int nextId; // from 0 to max-1, cyclic + int32_t max; // max number of workers + int32_t nextId; // from 0 to max-1, cyclic SWriteWorker *writeWorker; } SWriteWorkerPool; @@ -59,8 +59,8 @@ static void dnodeProcessDropTableMsg(SWriteMsg *pMsg); SWriteWorkerPool wWorkerPool; -int dnodeInitWrite() { - dnodeProcessWriteMsgFp[TSDB_MSG_TYPE_SUBMIT] = dnodeProcessSubmitMsg; +int32_t dnodeInitWrite() { + dnodeProcessWriteMsgFp[TSDB_MSG_TYPE_SUBMIT] = dnodeProcessSubmitMsg; dnodeProcessWriteMsgFp[TSDB_MSG_TYPE_DNODE_CREATE_TABLE] = dnodeProcessCreateTableMsg; dnodeProcessWriteMsgFp[TSDB_MSG_TYPE_DNODE_REMOVE_TABLE] = dnodeProcessDropTableMsg; @@ -68,7 +68,7 @@ int dnodeInitWrite() { wWorkerPool.writeWorker = (SWriteWorker *)calloc(sizeof(SWriteWorker), wWorkerPool.max); if (wWorkerPool.writeWorker == NULL) return -1; - for (int i=0; icontLen; - char *pCont = (char *)pMsg->pCont; - int contLen = 0; - int numOfVnodes = 0; - int32_t vgId = 0; +void dnodeWrite(void *rpcMsg) { + SRpcMsg *pMsg = rpcMsg; + + int32_t leftLen = pMsg->contLen; + char *pCont = (char *) pMsg->pCont; + int32_t contLen = 0; + int32_t numOfVnodes = 0; + int32_t vgId = 0; SRpcContext *pRpcContext = NULL; // parse head, get number of vnodes; @@ -108,11 +108,11 @@ void dnodeWrite(SRpcMsg *pMsg) { // put message into queue SWriteMsg writeMsg; - writeMsg.rpcMsg = *pMsg; - writeMsg.pCont = pCont; - writeMsg.contLen = contLen; - writeMsg.pRpcContext = pRpcContext; - writeMsg.pVnode = pVnode; // pVnode shall be saved for usage later + writeMsg.rpcMsg = *pMsg; + writeMsg.pCont = pCont; + writeMsg.contLen = contLen; + writeMsg.pRpcContext = pRpcContext; + writeMsg.pVnode = pVnode; // pVnode shall be saved for usage later taos_queue queue = dnodeGetVnodeWworker(pVnode); taosWriteQitem(queue, &writeMsg); @@ -150,7 +150,6 @@ void *dnodeAllocateWriteWorker() { } void dnodeFreeWriteWorker(void *wqueue) { - taosCloseQueue(wqueue); // dynamically adjust the number of threads @@ -160,7 +159,7 @@ static void *dnodeProcessWriteQueue(void *param) { SWriteWorker *pWorker = (SWriteWorker *)param; taos_qall qall; SWriteMsg writeMsg; - int numOfMsgs; + int32_t numOfMsgs; while (1) { numOfMsgs = taosReadAllQitemsFromQset(pWorker->qset, &qall); @@ -169,7 +168,7 @@ static void *dnodeProcessWriteQueue(void *param) { continue; } - for (int i=0; icode == 0) pRpcContext->code = terrno; } - int count = atomic_add_fetch_32(&pRpcContext->count, 1); + int32_t count = atomic_add_fetch_32(&pRpcContext->count, 1); if (count < pRpcContext->numOfVnodes) { // not over yet, multiple vnodes return; @@ -226,15 +225,14 @@ static void dnodeProcessWriteResult(SWriteMsg *pWrite) { SRpcMsg rsp; rsp.handle = pWrite->rpcMsg.handle; - rsp.code = code; - rsp.pCont = NULL; + rsp.code = code; + rsp.pCont = NULL; rpcSendResponse(&rsp); rpcFreeCont(pWrite->rpcMsg.pCont); // free the received message } static void dnodeHandleIdleWorker(SWriteWorker *pWorker) { - - int num = taosGetQueueNumber(pWorker->qset); + int32_t num = taosGetQueueNumber(pWorker->qset); if (num > 0) { usleep(100); @@ -248,15 +246,12 @@ static void dnodeHandleIdleWorker(SWriteWorker *pWorker) { static void dnodeProcessSubmitMsg(SWriteMsg *pMsg) { - } static void dnodeProcessCreateTableMsg(SWriteMsg *pMsg) { - } static void dnodeProcessDropTableMsg(SWriteMsg *pMsg) { - } diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index b9de3ec0b9..2c7db77ddf 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -349,8 +349,8 @@ typedef struct { } SDRemoveSuperTableMsg; typedef struct { - int32_t vnode; -} SFreeVnodeMsg; + int32_t vgId; +} SDropVnodeMsg; typedef struct SColIndexEx { int16_t colId; diff --git a/src/mnode/src/mgmtDnodeInt.c b/src/mnode/src/mgmtDnodeInt.c index 238eca25f8..96fb226e54 100644 --- a/src/mnode/src/mgmtDnodeInt.c +++ b/src/mnode/src/mgmtDnodeInt.c @@ -291,10 +291,10 @@ void mgmtSendAlterStreamMsg(STableInfo *pTable, SRpcIpSet *ipSet, void *ahandle) void mgmtSendOneFreeVnodeMsg(int32_t vnode, SRpcIpSet *ipSet, void *ahandle) { mTrace("vnode:%d send free vnode msg, ahandle:%p", vnode, ahandle); - SFreeVnodeMsg *pFreeVnode = rpcMallocCont(sizeof(SFreeVnodeMsg)); + SDropVnodeMsg *pFreeVnode = rpcMallocCont(sizeof(SDropVnodeMsg)); if (pFreeVnode != NULL) { pFreeVnode->vnode = htonl(vnode); - mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_DROP_VNODE, pFreeVnode, sizeof(SFreeVnodeMsg), ahandle); + mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_DROP_VNODE, pFreeVnode, sizeof(SDropVnodeMsg), ahandle); } } From fcde34020025c4d69334a0615df4668e7f154735 Mon Sep 17 00:00:00 2001 From: slguan Date: Tue, 10 Mar 2020 13:20:18 +0800 Subject: [PATCH 2/5] refactor mgmtShell and mgmt --- src/client/src/tscServer.c | 2 +- src/dnode/CMakeLists.txt | 2 +- src/dnode/src/dnodeMClient.c | 1 + src/dnode/src/dnodeMgmt.c | 208 +-- src/dnode/src/dnodeMnode.c | 6 +- src/dnode/src/dnodeWrite.c | 4 +- src/inc/mnode.h | 2 - src/inc/taosmsg.h | 54 +- src/mnode/inc/mgmtChildTable.h | 2 +- src/mnode/inc/mgmtDClient.h | 31 + src/mnode/inc/mgmtDServer.h | 49 + src/mnode/inc/mgmtDnodeInt.h | 48 - src/mnode/inc/mgmtMnode.h | 3 + src/mnode/inc/mgmtNormalTable.h | 2 +- src/mnode/inc/mgmtShell.h | 41 +- src/mnode/inc/mgmtTable.h | 2 +- src/mnode/inc/mgmtUser.h | 11 +- src/mnode/inc/mgmtVgroup.h | 2 +- src/mnode/src/mgmtAcct.c | 14 +- src/mnode/src/mgmtChildTable.c | 13 +- src/mnode/src/mgmtDClient.c | 313 ++++ src/mnode/src/mgmtDServer.c | 453 ++++++ src/mnode/src/mgmtDb.c | 5 +- src/mnode/src/mgmtDnodeInt.c | 455 ------ src/mnode/src/mgmtMnode.c | 6 + src/mnode/src/mgmtNormalTable.c | 13 +- src/mnode/src/mgmtShell.c | 2454 ++++++++++++++----------------- src/mnode/src/mgmtSuperTable.c | 1 - src/mnode/src/mgmtSystem.c | 1 - src/mnode/src/mgmtTable.c | 5 +- src/mnode/src/mgmtUser.c | 293 +++- src/mnode/src/mgmtVgroup.c | 7 +- 32 files changed, 2443 insertions(+), 2060 deletions(-) create mode 100644 src/mnode/inc/mgmtDClient.h create mode 100644 src/mnode/inc/mgmtDServer.h delete mode 100644 src/mnode/inc/mgmtDnodeInt.h create mode 100644 src/mnode/src/mgmtDClient.c create mode 100644 src/mnode/src/mgmtDServer.c delete mode 100644 src/mnode/src/mgmtDnodeInt.c diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 397ad3aa11..31e16792b6 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1802,7 +1802,7 @@ int32_t tscBuildCfgDnodeMsg(SSqlObj *pSql, SSqlInfo *pInfo) { return TSDB_CODE_CLI_OUT_OF_MEMORY; } - pCmd->msgType = TSDB_MSG_TYPE_DNODE_CFG; + pCmd->msgType = TSDB_MSG_TYPE_MD_CONFIG_DNODE; return TSDB_CODE_SUCCESS; } diff --git a/src/dnode/CMakeLists.txt b/src/dnode/CMakeLists.txt index c27771a858..712f81656a 100644 --- a/src/dnode/CMakeLists.txt +++ b/src/dnode/CMakeLists.txt @@ -26,7 +26,7 @@ IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) COMMAND echo "make test directory" DEPENDS taosd COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/cfg/ - COMMAND ${CMAKE_COMMAND} -E make_directoryF ${TD_TESTS_OUTPUT_DIR}/log/ + COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/log/ COMMAND ${CMAKE_COMMAND} -E make_directory ${TD_TESTS_OUTPUT_DIR}/data/ COMMAND ${CMAKE_COMMAND} -E echo dataDir ${TD_TESTS_OUTPUT_DIR}/data > ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg COMMAND ${CMAKE_COMMAND} -E echo logDir ${TD_TESTS_OUTPUT_DIR}/log >> ${TD_TESTS_OUTPUT_DIR}/cfg/taos.cfg diff --git a/src/dnode/src/dnodeMClient.c b/src/dnode/src/dnodeMClient.c index 99aaa0bd37..3f70caf18e 100644 --- a/src/dnode/src/dnodeMClient.c +++ b/src/dnode/src/dnodeMClient.c @@ -52,6 +52,7 @@ int32_t dnodeInitMClient() { void dnodeCleanupMClient() { if (tsDnodeMClientRpc) { rpcClose(tsDnodeMClientRpc); + tsDnodeMClientRpc = NULL; } } diff --git a/src/dnode/src/dnodeMgmt.c b/src/dnode/src/dnodeMgmt.c index df65ddeac1..7a5bc66a13 100644 --- a/src/dnode/src/dnodeMgmt.c +++ b/src/dnode/src/dnodeMgmt.c @@ -21,7 +21,7 @@ #include "tlog.h" #include "trpc.h" #include "tstatus.h" -#include "tsdb.h" +//#include "tsdb.h" #include "dnodeMgmt.h" #include "dnodeRead.h" #include "dnodeWrite.h" @@ -44,19 +44,19 @@ static int32_t dnodeOpenVnodes(); static void dnodeCleanupVnodes(); static int32_t dnodeOpenVnode(int32_t vgId); static void dnodeCleanupVnode(SVnodeObj *pVnode); -static int32_t dnodeCreateVnode(SCreateVnodeMsg *cfg); +static int32_t dnodeCreateVnode(SMDCreateVnodeMsg *cfg); static void dnodeDropVnode(SVnodeObj *pVnode); -static void dnodeProcessCreateVnodeMsg(SRpcMsg *pMsg); -static void dnodeProcessDropVnodeMsg(SRpcMsg *pMsg); +static void dnodeProcesSMDCreateVnodeMsg(SRpcMsg *pMsg); +static void dnodeProcesSMDDropVnodeMsg(SRpcMsg *pMsg); static void dnodeProcessAlterVnodeMsg(SRpcMsg *pMsg); static void (*dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *pMsg); static void * tsDnodeVnodesHash = NULL; int32_t dnodeInitMgmt() { - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_CREATE_VNODE] = dnodeProcessCreateVnodeMsg; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_DROP_VNODE] = dnodeProcessDropVnodeMsg; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_ALTER_VNODE] = dnodeProcessAlterVnodeMsg; + dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = dnodeProcesSMDCreateVnodeMsg; + dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = dnodeProcesSMDDropVnodeMsg; + dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = dnodeProcessAlterVnodeMsg; tsDnodeVnodesHash = taosInitIntHash(TSDB_MAX_VNODES, sizeof(SVnodeObj), taosHashInt); if (tsDnodeVnodesHash == NULL) { @@ -139,117 +139,117 @@ static void dnodeCleanupVnodes() { } static int32_t dnodeOpenVnode(int32_t vgId) { - char rootDir[TSDB_FILENAME_LEN] = {0}; - sprintf(rootDir, "%s/vnode%d", tsDirectory, vgId); - - void *pTsdb = tsdbOpenRepo(rootDir); - if (pTsdb != NULL) { - return terrno; - } - - SVnodeObj vnodeObj; - vnodeObj.vgId = vgId; - vnodeObj.status = TSDB_VN_STATUS_NOT_READY; - vnodeObj.refCount = 1; - vnodeObj.version = 0; - vnodeObj.wworker = dnodeAllocateWriteWorker(); - vnodeObj.rworker = dnodeAllocateReadWorker(); - vnodeObj.wal = NULL; - vnodeObj.tsdb = pTsdb; - vnodeObj.replica = NULL; - vnodeObj.events = NULL; - vnodeObj.cq = NULL; - - taosAddIntHash(tsDnodeVnodesHash, vnodeObj.vgId, &vnodeObj); +// char rootDir[TSDB_FILENAME_LEN] = {0}; +// sprintf(rootDir, "%s/vnode%d", tsDirectory, vgId); +// +// void *pTsdb = tsdbOpenRepo(rootDir); +// if (pTsdb != NULL) { +// return terrno; +// } +// +// SVnodeObj vnodeObj; +// vnodeObj.vgId = vgId; +// vnodeObj.status = TSDB_VN_STATUS_NOT_READY; +// vnodeObj.refCount = 1; +// vnodeObj.version = 0; +// vnodeObj.wworker = dnodeAllocateWriteWorker(); +// vnodeObj.rworker = dnodeAllocateReadWorker(); +// vnodeObj.wal = NULL; +// vnodeObj.tsdb = pTsdb; +// vnodeObj.replica = NULL; +// vnodeObj.events = NULL; +// vnodeObj.cq = NULL; +// +// taosAddIntHash(tsDnodeVnodesHash, vnodeObj.vgId, &vnodeObj); return TSDB_CODE_SUCCESS; } static void dnodeCleanupVnode(SVnodeObj *pVnode) { - pVnode->status = TSDB_VN_STATUS_NOT_READY; - int32_t count = atomic_sub_fetch_32(&pVnode->refCount, 1); - if (count > 0) { - // wait refcount - } - - // remove replica - - // remove read queue - dnodeFreeReadWorker(pVnode->rworker); - pVnode->rworker = NULL; - - // remove write queue - dnodeFreeWriteWorker(pVnode->wworker); - pVnode->wworker = NULL; - - // remove wal - - // remove tsdb - if (pVnode->tsdb) { - tsdbCloseRepo(pVnode->tsdb); - pVnode->tsdb = NULL; - } - - taosDeleteIntHash(tsDnodeVnodesHash, pVnode->vgId); +// pVnode->status = TSDB_VN_STATUS_NOT_READY; +// int32_t count = atomic_sub_fetch_32(&pVnode->refCount, 1); +// if (count > 0) { +// // wait refcount +// } +// +// // remove replica +// +// // remove read queue +// dnodeFreeReadWorker(pVnode->rworker); +// pVnode->rworker = NULL; +// +// // remove write queue +// dnodeFreeWriteWorker(pVnode->wworker); +// pVnode->wworker = NULL; +// +// // remove wal +// +// // remove tsdb +// if (pVnode->tsdb) { +// tsdbCloseRepo(pVnode->tsdb); +// pVnode->tsdb = NULL; +// } +// +// taosDeleteIntHash(tsDnodeVnodesHash, pVnode->vgId); } -static int32_t dnodeCreateVnode(SCreateVnodeMsg *pVnodeCfg) { - STsdbCfg tsdbCfg; - tsdbCfg.precision = pVnodeCfg->cfg.precision; - tsdbCfg.tsdbId = pVnodeCfg->vnode; - tsdbCfg.maxTables = pVnodeCfg->cfg.maxSessions; - tsdbCfg.daysPerFile = pVnodeCfg->cfg.daysPerFile; - tsdbCfg.minRowsPerFileBlock = -1; - tsdbCfg.maxRowsPerFileBlock = -1; - tsdbCfg.keep = -1; - tsdbCfg.maxCacheSize = -1; +static int32_t dnodeCreateVnode(SMDCreateVnodeMsg *pVnodeCfg) { +// STsdbCfg tsdbCfg; +// tsdbCfg.precision = pVnodeCfg->cfg.precision; +// tsdbCfg.tsdbId = pVnodeCfg->vnode; +// tsdbCfg.maxTables = pVnodeCfg->cfg.maxSessions; +// tsdbCfg.daysPerFile = pVnodeCfg->cfg.daysPerFile; +// tsdbCfg.minRowsPerFileBlock = -1; +// tsdbCfg.maxRowsPerFileBlock = -1; +// tsdbCfg.keep = -1; +// tsdbCfg.maxCacheSize = -1; - char rootDir[TSDB_FILENAME_LEN] = {0}; - sprintf(rootDir, "%s/vnode%d", tsDirectory, pVnodeCfg->cfg.vgId); - - void *pTsdb = tsdbCreateRepo(rootDir, &tsdbCfg, NULL); - if (pTsdb != NULL) { - return terrno; - } - - SVnodeObj vnodeObj; - vnodeObj.vgId = pVnodeCfg->cfg.vgId; - vnodeObj.status = TSDB_VN_STATUS_NOT_READY; - vnodeObj.refCount = 1; - vnodeObj.version = 0; - vnodeObj.wworker = dnodeAllocateWriteWorker(); - vnodeObj.rworker = dnodeAllocateReadWorker(); - vnodeObj.wal = NULL; - vnodeObj.tsdb = pTsdb; - vnodeObj.replica = NULL; - vnodeObj.events = NULL; - vnodeObj.cq = NULL; - - taosAddIntHash(tsDnodeVnodesHash, vnodeObj.vgId, &vnodeObj); +// char rootDir[TSDB_FILENAME_LEN] = {0}; +// sprintf(rootDir, "%s/vnode%d", tsDirectory, pVnodeCfg->cfg.vgId); +// +// void *pTsdb = tsdbCreateRepo(rootDir, &tsdbCfg, NULL); +// if (pTsdb != NULL) { +// return terrno; +// } +// +// SVnodeObj vnodeObj; +// vnodeObj.vgId = pVnodeCfg->cfg.vgId; +// vnodeObj.status = TSDB_VN_STATUS_NOT_READY; +// vnodeObj.refCount = 1; +// vnodeObj.version = 0; +// vnodeObj.wworker = dnodeAllocateWriteWorker(); +// vnodeObj.rworker = dnodeAllocateReadWorker(); +// vnodeObj.wal = NULL; +// vnodeObj.tsdb = pTsdb; +// vnodeObj.replica = NULL; +// vnodeObj.events = NULL; +// vnodeObj.cq = NULL; +// +// taosAddIntHash(tsDnodeVnodesHash, vnodeObj.vgId, &vnodeObj); return TSDB_CODE_SUCCESS; } static void dnodeDropVnode(SVnodeObj *pVnode) { - pVnode->status = TSDB_VN_STATUS_NOT_READY; - - int32_t count = atomic_sub_fetch_32(&pVnode->refCount, 1); - if (count > 0) { - // wait refcount - } - - if (pVnode->tsdb) { - tsdbDropRepo(pVnode->tsdb); - pVnode->tsdb = NULL; - } - - dnodeCleanupVnode(pVnode); +// pVnode->status = TSDB_VN_STATUS_NOT_READY; +// +// int32_t count = atomic_sub_fetch_32(&pVnode->refCount, 1); +// if (count > 0) { +// // wait refcount +// } +// +// if (pVnode->tsdb) { +// tsdbDropRepo(pVnode->tsdb); +// pVnode->tsdb = NULL; +// } +// +// dnodeCleanupVnode(pVnode); } -static void dnodeProcessCreateVnodeMsg(SRpcMsg *rpcMsg) { +static void dnodeProcesSMDCreateVnodeMsg(SRpcMsg *rpcMsg) { SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SCreateVnodeMsg *pCreate = (SCreateVnodeMsg *) rpcMsg->pCont; + SMDCreateVnodeMsg *pCreate = (SMDCreateVnodeMsg *) rpcMsg->pCont; pCreate->vnode = htonl(pCreate->vnode); pCreate->cfg.vgId = htonl(pCreate->cfg.vgId); pCreate->cfg.maxSessions = htonl(pCreate->cfg.maxSessions); @@ -266,10 +266,10 @@ static void dnodeProcessCreateVnodeMsg(SRpcMsg *rpcMsg) { rpcFreeCont(rpcMsg->pCont); } -static void dnodeProcessDropVnodeMsg(SRpcMsg *rpcMsg) { +static void dnodeProcesSMDDropVnodeMsg(SRpcMsg *rpcMsg) { SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SDropVnodeMsg *pDrop = (SCreateVnodeMsg *) rpcMsg->pCont; + SMDDropVnodeMsg *pDrop = (SMDCreateVnodeMsg *) rpcMsg->pCont; pDrop->vgId = htonl(pDrop->vgId); SVnodeObj *pVnodeObj = taosGetIntHashData(tsDnodeVnodesHash, pDrop->vgId); @@ -287,7 +287,7 @@ static void dnodeProcessDropVnodeMsg(SRpcMsg *rpcMsg) { static void dnodeProcessAlterVnodeMsg(SRpcMsg *rpcMsg) { SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SCreateVnodeMsg *pCreate = (SCreateVnodeMsg *) rpcMsg->pCont; + SMDCreateVnodeMsg *pCreate = (SMDCreateVnodeMsg *) rpcMsg->pCont; pCreate->vnode = htonl(pCreate->vnode); pCreate->cfg.vgId = htonl(pCreate->cfg.vgId); pCreate->cfg.maxSessions = htonl(pCreate->cfg.maxSessions); diff --git a/src/dnode/src/dnodeMnode.c b/src/dnode/src/dnodeMnode.c index 62814fac17..a374dbe293 100644 --- a/src/dnode/src/dnodeMnode.c +++ b/src/dnode/src/dnodeMnode.c @@ -26,9 +26,9 @@ static void dnodeProcessMsgFromMnode(SRpcMsg *pMsg); static void *tsDnodeMnodeRpc = NULL; int32_t dnodeInitMnode() { - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_DNODE_CREATE_TABLE] = dnodeWrite; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_DNODE_REMOVE_TABLE] = dnodeWrite; - dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_DROP_VNODE] = dnodeMgmt; + dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE] = dnodeWrite; + dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_DROP_TABLE] = dnodeWrite; + dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = dnodeMgmt; SRpcInit rpcInit; memset(&rpcInit, 0, sizeof(rpcInit)); diff --git a/src/dnode/src/dnodeWrite.c b/src/dnode/src/dnodeWrite.c index f86691d3d9..9d38a5c207 100644 --- a/src/dnode/src/dnodeWrite.c +++ b/src/dnode/src/dnodeWrite.c @@ -61,8 +61,8 @@ SWriteWorkerPool wWorkerPool; int32_t dnodeInitWrite() { dnodeProcessWriteMsgFp[TSDB_MSG_TYPE_SUBMIT] = dnodeProcessSubmitMsg; - dnodeProcessWriteMsgFp[TSDB_MSG_TYPE_DNODE_CREATE_TABLE] = dnodeProcessCreateTableMsg; - dnodeProcessWriteMsgFp[TSDB_MSG_TYPE_DNODE_REMOVE_TABLE] = dnodeProcessDropTableMsg; + dnodeProcessWriteMsgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE] = dnodeProcessCreateTableMsg; + dnodeProcessWriteMsgFp[TSDB_MSG_TYPE_MD_DROP_TABLE] = dnodeProcessDropTableMsg; wWorkerPool.max = tsNumOfCores; wWorkerPool.writeWorker = (SWriteWorker *)calloc(sizeof(SWriteWorker), wWorkerPool.max); diff --git a/src/inc/mnode.h b/src/inc/mnode.h index 12a3fefe66..04ff2ff505 100644 --- a/src/inc/mnode.h +++ b/src/inc/mnode.h @@ -263,8 +263,6 @@ int32_t mgmtStartSystem(); void mgmtCleanUpSystem(); void mgmtStopSystem(); -void mgmtProcessMsgFromDnode(char msgType, void *pCont, int32_t contLen, void *pConn, int32_t code); -void dnodeProcessMsgFromMgmt(char msgType, void *pCont, int32_t contLen, void *pConn, int32_t code); #ifdef __cplusplus } diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index 2c7db77ddf..6b7b6a672c 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -37,31 +37,36 @@ extern "C" { #define TSDB_MSG_TYPE_QUERY_RSP 6 #define TSDB_MSG_TYPE_RETRIEVE 7 #define TSDB_MSG_TYPE_RETRIEVE_RSP 8 -#define TSDB_MSG_TYPE_DNODE_CREATE_TABLE 9 -#define TSDB_MSG_TYPE_DNODE_CREATE_TABLE_RSP 10 -#define TSDB_MSG_TYPE_DNODE_REMOVE_TABLE 11 -#define TSDB_MSG_TYPE_DNODE_REMOVE_TABLE_RSP 12 -// dnodeMgmt -#define TSDB_MSG_TYPE_CREATE_VNODE 13 -#define TSDB_MSG_TYPE_CREATE_VNODE_RSP 14 -#define TSDB_MSG_TYPE_DROP_VNODE 15 -#define TSDB_MSG_TYPE_DROP_VNODE_RSP 16 -#define TSDB_MSG_TYPE_ALTER_VNODE 17 -#define TSDB_MSG_TYPE_ALTER_VNODE_RSP 18 -#define TSDB_MSG_TYPE_CONFIG_VNODE 19 -#define TSDB_MSG_TYPE_CONFIG_VNODE_RSP 20 +// message from mgmt to dnode +#define TSDB_MSG_TYPE_MD_CREATE_TABLE 9 +#define TSDB_MSG_TYPE_MD_CREATE_TABLE_RSP 10 +#define TSDB_MSG_TYPE_MD_DROP_TABLE 11 +#define TSDB_MSG_TYPE_MD_DROP_TABLE_RSP 12 +#define TSDB_MSG_TYPE_MD_ALTER_TABLE 13 +#define TSDB_MSG_TYPE_MD_ALTER_TABLE_RSP 14 +#define TSDB_MSG_TYPE_MD_CREATE_VNODE 15 +#define TSDB_MSG_TYPE_MD_CREATE_VNODE_RSP 16 +#define TSDB_MSG_TYPE_MD_DROP_VNODE 17 +#define TSDB_MSG_TYPE_MD_DROP_VNODE_RSP 18 +#define TSDB_MSG_TYPE_MD_ALTER_VNODE 19 +#define TSDB_MSG_TYPE_MD_ALTER_VNODE_RSP 20 +#define TSDB_MSG_TYPE_MD_DROP_STABLE 21 +#define TSDB_MSG_TYPE_MD_DROP_STABLE_RSP 22 +#define TSDB_MSG_TYPE_MD_ALTER_STREAM 23 +#define TSDB_MSG_TYPE_MD_ALTER_STREAM_RSP 24 +#define TSDB_MSG_TYPE_MD_CONFIG_DNODE 25 +#define TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP 26 + + +#define TSDB_MSG_TYPE_DM_CONFIG_VNODE 19 +#define TSDB_MSG_TYPE_DM_CONFIG_VNODE_RSP 20 + -#define TSDB_MSG_TYPE_DNODE_CFG 17 -#define TSDB_MSG_TYPE_DNODE_CFG_RSP 18 -#define TSDB_MSG_TYPE_DNODE_ALTER_STREAM 19 -#define TSDB_MSG_TYPE_DNODE_ALTER_STREAM_RSP 20 #define TSDB_MSG_TYPE_SDB_SYNC 21 #define TSDB_MSG_TYPE_SDB_SYNC_RSP 22 #define TSDB_MSG_TYPE_SDB_FORWARD 23 #define TSDB_MSG_TYPE_SDB_FORWARD_RSP 24 -#define TSDB_MSG_TYPE_DROP_STABLE 25 -#define TSDB_MSG_TYPE_DROP_STABLE_RSP 26 #define TSDB_MSG_TYPE_CONNECT 31 #define TSDB_MSG_TYPE_CONNECT_RSP 32 #define TSDB_MSG_TYPE_CREATE_ACCT 33 @@ -259,7 +264,7 @@ typedef struct { char tableId[TSDB_TABLE_ID_LEN + 1]; char superTableId[TSDB_TABLE_ID_LEN + 1]; char data[]; -} SDCreateTableMsg; +} SDMCreateTableMsg; typedef struct { char tableId[TSDB_TABLE_ID_LEN + 1]; @@ -341,7 +346,7 @@ typedef struct { uint64_t uid; SVPeerDesc vpeerDesc[TSDB_MAX_MPEERS]; char tableId[TSDB_TABLE_ID_LEN + 1]; -} SDRemoveTableMsg; +} SMDDropTableMsg; typedef struct { char tableId[TSDB_TABLE_ID_LEN + 1]; @@ -350,7 +355,8 @@ typedef struct { typedef struct { int32_t vgId; -} SDropVnodeMsg; + int32_t vnode; +} SMDDropVnodeMsg; typedef struct SColIndexEx { int16_t colId; @@ -622,7 +628,7 @@ typedef struct { int32_t vnode; SVnodeCfg cfg; SVPeerDesc vpeerDesc[TSDB_MAX_MPEERS]; -} SCreateVnodeMsg; +} SMDCreateVnodeMsg; typedef struct { char tableId[TSDB_TABLE_ID_LEN + 1]; @@ -751,7 +757,7 @@ typedef struct { typedef struct { char ip[32]; char config[64]; -} SCfgDnodeMsg; +} SMDCfgDnodeMsg, SCMCfgDnodeMsg; typedef struct { char sql[TSDB_SHOW_SQL_LEN + 1]; diff --git a/src/mnode/inc/mgmtChildTable.h b/src/mnode/inc/mgmtChildTable.h index 83c18ca46c..988e54936e 100644 --- a/src/mnode/inc/mgmtChildTable.h +++ b/src/mnode/inc/mgmtChildTable.h @@ -31,7 +31,7 @@ void mgmtCleanUpChildTables(); void * mgmtGetChildTable(char *tableId); int32_t mgmtCreateChildTable(SCreateTableMsg *pCreate, int32_t contLen, SVgObj *pVgroup, int32_t sid, - SDCreateTableMsg **pDCreateOut, STableInfo **pTableOut); + SDMCreateTableMsg **pDCreateOut, STableInfo **pTableOut); int32_t mgmtDropChildTable(SDbObj *pDb, SChildTableObj *pTable); int32_t mgmtModifyChildTableTagValueByName(SChildTableObj *pTable, char *tagName, char *nContent); diff --git a/src/mnode/inc/mgmtDClient.h b/src/mnode/inc/mgmtDClient.h new file mode 100644 index 0000000000..ad86aa91e4 --- /dev/null +++ b/src/mnode/inc/mgmtDClient.h @@ -0,0 +1,31 @@ +/* + * 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 TDENGINE_MGMT_DCLIENT_H +#define TDENGINE_MGMT_DCLIENT_H + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t mgmtInitDClient(); +void mgmtCleanupDClient(); +void mgmtAddDClientRspHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/mnode/inc/mgmtDServer.h b/src/mnode/inc/mgmtDServer.h new file mode 100644 index 0000000000..b4cea21b68 --- /dev/null +++ b/src/mnode/inc/mgmtDServer.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 TDENGINE_MGMT_DSERVER_H +#define TDENGINE_MGMT_DSERVER_H + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t mgmtInitDServer(); +void mgmtCleanupDServer(); +void mgmtAddDServerMsgHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)); + + +//extern void *mgmtStatusTimer; +// +//void mgmtSendCreateTableMsg(SDMCreateTableMsg *pCreate, SRpcIpSet *ipSet, void *ahandle); +//void mgmtSendDropTableMsg(SMDDropTableMsg *pRemove, SRpcIpSet *ipSet, void *ahandle); +//void mgmtSendAlterStreamMsg(STableInfo *pTable, SRpcIpSet *ipSet, void *ahandle); +//void mgmtSendCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode, SRpcIpSet *ipSet, void *ahandle); +//void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle); +//void mgmtSendDropVnodeMsg(int32_t vnode, SRpcIpSet *ipSet, void *ahandle); +//void mgmtSendDropVgroupMsg(SVgObj *pVgroup, void *ahandle); +// +//int32_t mgmtInitDnodeInt(); +//void mgmtCleanUpDnodeInt(); +// +//void mgmtSendMsgToDnode(SRpcIpSet *ipSet, int8_t msgType, void *pCont, int32_t contLen, void *ahandle); +//void mgmtSendRspToDnode(void *pConn, int8_t msgType, int32_t code, void *pCont, int32_t contLen); +//void mgmtProcessMsgFromDnode(char msgType, void *pCont, int32_t contLen, void *pConn, int32_t code); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/mnode/inc/mgmtDnodeInt.h b/src/mnode/inc/mgmtDnodeInt.h deleted file mode 100644 index d3f5cffac3..0000000000 --- a/src/mnode/inc/mgmtDnodeInt.h +++ /dev/null @@ -1,48 +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 TDENGINE_MGMT_DNODE_INT_H -#define TDENGINE_MGMT_DNODE_INT_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include "mnode.h" - -extern void *mgmtStatusTimer; - -void mgmtSendCreateTableMsg(SDCreateTableMsg *pCreate, SRpcIpSet *ipSet, void *ahandle); -void mgmtSendRemoveTableMsg(SDRemoveTableMsg *pRemove, SRpcIpSet *ipSet, void *ahandle); -void mgmtSendAlterStreamMsg(STableInfo *pTable, SRpcIpSet *ipSet, void *ahandle); -void mgmtSendCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode, SRpcIpSet *ipSet, void *ahandle); -void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle); -void mgmtSendOneFreeVnodeMsg(int32_t vnode, SRpcIpSet *ipSet, void *ahandle); -void mgmtSendRemoveVgroupMsg(SVgObj *pVgroup, void *ahandle); - -int32_t mgmtInitDnodeInt(); -void mgmtCleanUpDnodeInt(); - -void mgmtSendMsgToDnode(SRpcIpSet *ipSet, int8_t msgType, void *pCont, int32_t contLen, void *ahandle); -void mgmtSendRspToDnode(void *pConn, int8_t msgType, int32_t code, void *pCont, int32_t contLen); -void mgmtProcessMsgFromDnode(char msgType, void *pCont, int32_t contLen, void *pConn, int32_t code); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/mnode/inc/mgmtMnode.h b/src/mnode/inc/mgmtMnode.h index 6e8e91ebc8..703951f44b 100644 --- a/src/mnode/inc/mgmtMnode.h +++ b/src/mnode/inc/mgmtMnode.h @@ -24,6 +24,9 @@ extern "C" { #include #include "mnode.h" + +bool mgmtCheckRedirect(void *handle); + int32_t mgmtAddMnode(uint32_t privateIp, uint32_t publicIp); int32_t mgmtRemoveMnode(uint32_t privateIp); diff --git a/src/mnode/inc/mgmtNormalTable.h b/src/mnode/inc/mgmtNormalTable.h index e632b8b987..efe83d77ed 100644 --- a/src/mnode/inc/mgmtNormalTable.h +++ b/src/mnode/inc/mgmtNormalTable.h @@ -29,7 +29,7 @@ void mgmtCleanUpNormalTables(); void * mgmtGetNormalTable(char *tableId); int32_t mgmtCreateNormalTable(SCreateTableMsg *pCreate, int32_t contLen, SVgObj *pVgroup, int32_t sid, - SDCreateTableMsg **pDCreateOut, STableInfo **pTableOut); + SDMCreateTableMsg **pDCreateOut, STableInfo **pTableOut); int32_t mgmtDropNormalTable(SDbObj *pDb, SNormalTableObj *pTable); int32_t mgmtAddNormalTableColumn(SNormalTableObj *pTable, SSchema schema[], int32_t ncols); int32_t mgmtDropNormalTableColumnByName(SNormalTableObj *pTable, char *colName); diff --git a/src/mnode/inc/mgmtShell.h b/src/mnode/inc/mgmtShell.h index d1d4433683..5a924ca067 100644 --- a/src/mnode/inc/mgmtShell.h +++ b/src/mnode/inc/mgmtShell.h @@ -19,30 +19,33 @@ #ifdef __cplusplus extern "C" { #endif - -#include -#include #include "mnode.h" -int32_t mgmtInitShell(); +int32_t mgmtInitShell(); void mgmtCleanUpShell(); +void mgmtAddShellMsgHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)); -extern int32_t (*mgmtCheckRedirectMsg)(void *pConn); +typedef int32_t (*SShowMetaFp)(STableMeta *pMeta, SShowObj *pShow, void *pConn); +typedef int32_t (*SShowRetrieveFp)(SShowObj *pShow, char *data, int32_t rows, void *pConn); +void mgmtAddShellShowMetaHandle(uint8_t showType, SShowMetaFp fp); +void mgmtAddShellShowRetrieveHandle(uint8_t showType, SShowRetrieveFp fp); -/* - * If table not exist, will create it - */ -void mgmtProcessGetTableMeta(STableInfo *pTable, void *thandle); - -/* - * If vgroup not exist, will create vgroup - */ -void mgmtProcessCreateTable(SVgObj *pVgroup, SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta); - -/* - * If vgroup create returned, will then create table - */ -void mgmtProcessCreateVgroup(SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta); +//extern int32_t (*mgmtCheckRedirect)(void *pConn); +// +///* +// * If table not exist, will create it +// */ +//void mgmtProcessGetTableMeta(STableInfo *pTable, void *thandle); +// +///* +// * If vgroup not exist, will create vgroup +// */ +//void mgmtProcessCreateTable(SVgObj *pVgroup, SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta); +// +///* +// * If vgroup create returned, will then create table +// */ +//void mgmtProcessCreateVgroup(SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtTable.h b/src/mnode/inc/mgmtTable.h index a6cc811c35..dbd8971e2c 100644 --- a/src/mnode/inc/mgmtTable.h +++ b/src/mnode/inc/mgmtTable.h @@ -42,7 +42,7 @@ void mgmtAddTableIntoSuperTable(SSuperTableObj *pStable); void mgmtRemoveTableFromSuperTable(SSuperTableObj *pStable); void mgmtSetTableDirty(STableInfo *pTable, bool isDirty); -SDRemoveTableMsg *mgmtBuildRemoveTableMsg(STableInfo *pTable); +SMDDropTableMsg *mgmtBuildRemoveTableMsg(STableInfo *pTable); SDRemoveSuperTableMsg *mgmtBuildRemoveSuperTableMsg(STableInfo *pTable); diff --git a/src/mnode/inc/mgmtUser.h b/src/mnode/inc/mgmtUser.h index f6be37f452..a1b1d4f568 100644 --- a/src/mnode/inc/mgmtUser.h +++ b/src/mnode/inc/mgmtUser.h @@ -19,20 +19,11 @@ #ifdef __cplusplus extern "C" { #endif - -#include -#include #include "mnode.h" int32_t mgmtInitUsers(); -SUserObj *mgmtGetUser(char *name); -int32_t mgmtCreateUser(SAcctObj *pAcct, char *name, char *pass); -int32_t mgmtDropUser(SAcctObj *pAcct, char *name); -int32_t mgmtUpdateUser(SUserObj *pUser); -int32_t mgmtGetUserMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveUsers(SShowObj *pShow, char *data, int32_t rows, void *pConn); void mgmtCleanUpUsers(); -SUserObj *mgmtGetUserFromConn(void *pConn); +SUserObj *mgmtGetUser(char *name); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtVgroup.h b/src/mnode/inc/mgmtVgroup.h index 8b3bcff738..33a1fcad9f 100644 --- a/src/mnode/inc/mgmtVgroup.h +++ b/src/mnode/inc/mgmtVgroup.h @@ -42,7 +42,7 @@ SVgObj *mgmtGetAvailableVgroup(SDbObj *pDb); void mgmtAddTableIntoVgroup(SVgObj *pVgroup, STableInfo *pTable); void mgmtRemoveTableFromVgroup(SVgObj *pVgroup, STableInfo *pTable); -SCreateVnodeMsg *mgmtBuildVpeersMsg(SVgObj *pVgroup, int32_t vnode); +SMDCreateVnodeMsg *mgmtBuildCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode); SRpcIpSet mgmtGetIpSetFromVgroup(SVgObj *pVgroup); SRpcIpSet mgmtGetIpSetFromIp(uint32_t ip); diff --git a/src/mnode/src/mgmtAcct.c b/src/mnode/src/mgmtAcct.c index 53db1390e7..2b3262292e 100644 --- a/src/mnode/src/mgmtAcct.c +++ b/src/mnode/src/mgmtAcct.c @@ -187,13 +187,13 @@ int32_t mgmtRetrieveAccts(SShowObj *pShow, char *data, int32_t rows, void *pConn } SAcctObj *mgmtGetAcctFromConn(void *pConn) { - SRpcConnInfo connInfo; - rpcGetConnInfo(pConn, &connInfo); - - SUserObj *pUser = mgmtGetUser(connInfo.user); - if (pUser != NULL) { - return pUser->pAcct; - } +// SRpcConnInfo connInfo; +// rpcGetConnInfo(pConn, &connInfo); +// +// SUserObj *pUser = mgmtGetUser(connInfo.user); +// if (pUser != NULL) { +// return pUser->pAcct; +// } return NULL; } diff --git a/src/mnode/src/mgmtChildTable.c b/src/mnode/src/mgmtChildTable.c index 1bc4305438..a214a44573 100644 --- a/src/mnode/src/mgmtChildTable.c +++ b/src/mnode/src/mgmtChildTable.c @@ -26,7 +26,6 @@ #include "mgmtAcct.h" #include "mgmtChildTable.h" #include "mgmtDb.h" -#include "mgmtDnodeInt.h" #include "mgmtGrant.h" #include "mgmtProfile.h" #include "mgmtSuperTable.h" @@ -273,9 +272,9 @@ void mgmtCleanUpChildTables() { static void *mgmtBuildCreateChildTableMsg(SChildTableObj *pTable, SVgObj *pVgroup, void *pTagData, int32_t tagDataLen) { int32_t totalCols = pTable->superTable->numOfColumns + pTable->superTable->numOfTags; - int32_t contLen = sizeof(SDCreateTableMsg) + totalCols * sizeof(SSchema) + tagDataLen; + int32_t contLen = sizeof(SDMCreateTableMsg) + totalCols * sizeof(SSchema) + tagDataLen; - SDCreateTableMsg *pCreateTable = rpcMallocCont(contLen); + SDMCreateTableMsg *pCreateTable = rpcMallocCont(contLen); if (pCreateTable == NULL) { return NULL; } @@ -308,13 +307,13 @@ static void *mgmtBuildCreateChildTableMsg(SChildTableObj *pTable, SVgObj *pVgrou pSchema++; } - memcpy(pCreateTable + sizeof(SDCreateTableMsg) + totalCols * sizeof(SSchema), pTagData, tagDataLen); + memcpy(pCreateTable + sizeof(SDMCreateTableMsg) + totalCols * sizeof(SSchema), pTagData, tagDataLen); return pCreateTable; } int32_t mgmtCreateChildTable(SCreateTableMsg *pCreate, int32_t contLen, SVgObj *pVgroup, int32_t sid, - SDCreateTableMsg **pDCreateOut, STableInfo **pTableOut) { + SDMCreateTableMsg **pDCreateOut, STableInfo **pTableOut) { int32_t numOfTables = sdbGetNumOfRows(tsChildTableSdb); if (numOfTables >= tsMaxTables) { mError("table:%s, numOfTables:%d exceed maxTables:%d", pCreate->tableId, numOfTables, tsMaxTables); @@ -371,7 +370,7 @@ int32_t mgmtDropChildTable(SDbObj *pDb, SChildTableObj *pTable) { return TSDB_CODE_OTHERS; } - SDRemoveTableMsg *pRemove = rpcMallocCont(sizeof(SDRemoveTableMsg)); + SMDDropTableMsg *pRemove = rpcMallocCont(sizeof(SMDDropTableMsg)); if (pRemove == NULL) { mError("table:%s, failed to drop child table, no enough memory", pTable->tableId); return TSDB_CODE_SERV_OUT_OF_MEMORY; @@ -388,7 +387,7 @@ int32_t mgmtDropChildTable(SDbObj *pDb, SChildTableObj *pTable) { } SRpcIpSet ipSet = mgmtGetIpSetFromVgroup(pVgroup); - mgmtSendRemoveTableMsg(pRemove, &ipSet, NULL); + mgmtSendDropTableMsg(pRemove, &ipSet, NULL); if (sdbDeleteRow(tsChildTableSdb, pTable) < 0) { mError("table:%s, update ctables sdb error", pTable->tableId); diff --git a/src/mnode/src/mgmtDClient.c b/src/mnode/src/mgmtDClient.c new file mode 100644 index 0000000000..0f7cce405d --- /dev/null +++ b/src/mnode/src/mgmtDClient.c @@ -0,0 +1,313 @@ +/* + * 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 "taoserror.h" +#include "tsched.h" +#include "tstatus.h" +#include "tsystem.h" +#include "tutil.h" +#include "dnode.h" +#include "mnode.h" +#include "mgmtBalance.h" +#include "mgmtDb.h" +#include "mgmtDnode.h" +#include "mgmtGrant.h" +#include "mgmtProfile.h" +#include "mgmtShell.h" +#include "mgmtTable.h" +#include "mgmtVgroup.h" + +static void mgmtProcessRspFromDnode(SRpcMsg *rpcMsg); +static void (*mgmtProcessDnodeRspFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *rpcMsg); +static void *tsMgmtDClientRpc = NULL; + +int32_t mgmtInitDClient() { + SRpcInit rpcInit = {0}; + rpcInit.localIp = tsAnyIp ? "0.0.0.0" : tsPrivateIp; + rpcInit.localPort = 0; + rpcInit.label = "MND-DC"; + rpcInit.numOfThreads = 1; + rpcInit.cfp = mgmtProcessRspFromDnode; + rpcInit.sessions = tsMaxDnodes * 5; + rpcInit.connType = TAOS_CONN_CLIENT; + rpcInit.idleTime = tsShellActivityTimer * 1000; + rpcInit.user = "mgmtDClient"; + rpcInit.ckey = "key"; + rpcInit.secret = "secret"; + + tsMgmtDClientRpc = rpcOpen(&rpcInit); + if (tsMgmtDClientRpc == NULL) { + mError("failed to init client connection to dnode"); + return -1; + } + + mPrint("client connection to dnode is opened"); + return 0; +} + +void mgmtCleanupDClient() { + if (tsMgmtDClientRpc) { + rpcClose(tsMgmtDClientRpc); + tsMgmtDClientRpc = NULL; + } +} + +void mgmtAddDClientRspHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)) { + mgmtProcessDnodeRspFp[msgType] = fp; +} + +static void mgmtProcessRspFromDnode(SRpcMsg *rpcMsg) { + if (mgmtProcessDnodeRspFp[rpcMsg->msgType]) { + (*mgmtProcessDnodeRspFp[rpcMsg->msgType])(rpcMsg); + } else { + dError("%s is not processed", taosMsg[rpcMsg->msgType]); + } + + rpcFreeCont(rpcMsg->pCont); +} + +//static void mgmtProcessCreateTableRsp(SRpcMsg *rpcMsg); +//static void mgmtProcessDropTableRsp(SRpcMsg *rpcMsg); +//static void mgmtProcessAlterTableRsp(SRpcMsg *rpcMsg); +//static void mgmtProcessCreateVnodeRsp(SRpcMsg *rpcMsg); +//static void mgmtProcessDropVnodeRsp(SRpcMsg *rpcMsg); +//static void mgmtProcessAlterVnodeRsp(SRpcMsg *rpcMsg); +//static void mgmtProcessDropStableRsp(SRpcMsg *rpcMsg); +//static void mgmtProcessAlterStreamRsp(SRpcMsg *rpcMsg); +//static void mgmtProcessConfigDnodeRsp(SRpcMsg *rpcMsg); + +// +//static void mgmtProcessCreateTableRsp(SRpcMsg *rpcMsg) { +// mTrace("create table rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); +// if (rpcMsg->handle == NULL) return; +// +// SProcessInfo *info = rpcMsg->handle; +// assert(info->type == TSDB_PROCESS_CREATE_TABLE || info->type == TSDB_PROCESS_CREATE_TABLE_GET_META); +// +// STableInfo *pTable = info->ahandle; +// if (rpcMsg->code != TSDB_CODE_SUCCESS) { +// mError("table:%s, failed to create in dnode, code:%d, set it dirty", pTable->tableId, rpcMsg->code); +// mgmtSetTableDirty(pTable, true); +// } else { +// mTrace("table:%s, created in dnode", pTable->tableId); +// mgmtSetTableDirty(pTable, false); +// } +// +// if (rpcMsg->code != TSDB_CODE_SUCCESS) { +// SRpcMsg rpcRsp = {.handle = info->thandle, .pCont = NULL, .contLen = 0, .code = rpcMsg->code, .msgType = 0}; +// rpcSendResponse(&rpcMsg); +// } else { +// if (info->type == TSDB_PROCESS_CREATE_TABLE_GET_META) { +// mTrace("table:%s, start to process get meta", pTable->tableId); +// mgmtProcessGetTableMeta(pTable, rpcMsg->handle); +// } else { +// SRpcMsg rpcRsp = {.handle = info->thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// rpcSendResponse(&rpcMsg); +// } +// } +// +// free(info); +//} +// +//static void mgmtProcessDropTableRsp(SRpcMsg *rpcMsg) { +// mTrace("drop table rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); +//} +// +//static void mgmtProcessAlterTableRsp(SRpcMsg *rpcMsg) { +// mTrace("alter table rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); +//} +// +//static void mgmtProcessCreateVnodeRsp(SRpcMsg *rpcMsg) { +// mTrace("create vnode rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); +// if (rpcMsg->handle == NULL) return; +// +// SProcessInfo *info = rpcMsg->handle; +// assert(info->type == TSDB_PROCESS_CREATE_VGROUP || info->type == TSDB_PROCESS_CREATE_VGROUP_GET_META); +// +// info->received++; +// SVgObj *pVgroup = info->ahandle; +// +// bool isGetMeta = false; +// if (info->type == TSDB_PROCESS_CREATE_VGROUP_GET_META) { +// isGetMeta = true; +// } +// +// mTrace("vgroup:%d, received:%d numOfVnodes:%d", pVgroup->vgId, info->received, pVgroup->numOfVnodes); +// if (info->received == pVgroup->numOfVnodes) { +// mgmtProcessCreateTable(pVgroup, info->cont, info->contLen, info->thandle, isGetMeta); +// free(info); +// } +//} +// +//static void mgmtProcessDropVnodeRsp(SRpcMsg *rpcMsg) { +// mTrace("drop vnode rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); +//} +// +//static void mgmtProcessAlterVnodeRsp(SRpcMsg *rpcMsg) { +// mTrace("alter vnode rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); +//} +// +//static void mgmtProcessDropStableRsp(SRpcMsg *rpcMsg) { +// mTrace("drop stable rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); +//} +// +//static void mgmtProcessAlterStreamRsp(SRpcMsg *rpcMsg) { +// mTrace("alter stream rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); +//} +// +//static void mgmtProcessConfigDnodeRsp(SRpcMsg *rpcMsg) { +// mTrace("config dnode rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); +//} +// +//void mgmtSendCreateTableMsg(SDMCreateTableMsg *pCreate, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("table:%s, send create table msg, ahandle:%p", pCreate->tableId, ahandle); +// SRpcMsg rpcMsg = { +// .handle = ahandle, +// .pCont = pCreate, +// .contLen = htonl(pCreate->contLen), +// .code = 0, +// .msgType = TSDB_MSG_TYPE_MD_CREATE_TABLE +// }; +// rpcSendRequest(tsMgmtDClientRpc, ipSet, &rpcMsg); +//} +// +//void mgmtSendDropTableMsg(SMDDropTableMsg *pDrop, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("table:%s, send drop table msg, ahandle:%p", pDrop->tableId, ahandle); +// SRpcMsg rpcMsg = { +// .handle = ahandle, +// .pCont = pDrop, +// .contLen = sizeof(SMDDropTableMsg), +// .code = 0, +// .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE +// }; +// rpcSendRequest(tsMgmtDClientRpc, ipSet, &rpcMsg); +//} +// +//void mgmtSendCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("vgroup:%d, send create vnode:%d msg, ahandle:%p", pVgroup->vgId, vnode, ahandle); +// SMDCreateVnodeMsg *pCreate = mgmtBuildCreateVnodeMsg(pVgroup, vnode); +// SRpcMsg rpcMsg = { +// .handle = ahandle, +// .pCont = pCreate, +// .contLen = pCreate ? sizeof(SMDCreateVnodeMsg) : 0, +// .code = 0, +// .msgType = TSDB_MSG_TYPE_MD_CREATE_VNODE +// }; +// rpcSendRequest(tsMgmtDClientRpc, ipSet, &rpcMsg); +//} +// +//void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle) { +// mTrace("vgroup:%d, send create all vnodes msg, handle:%p", pVgroup->vgId, ahandle); +// for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { +// SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); +// mgmtSendCreateVnodeMsg(pVgroup, pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); +// } +//} +// +//void mgmtSendAlterStreamMsg(STableInfo *pTable, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("table:%s, send alter stream msg, ahandle:%p", pTable->tableId, pTable->sid, ahandle); +//} +// +//void mgmtSendDropVnodeMsg(int32_t vgId, int32_t vnode, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("vnode:%d send free vnode msg, ahandle:%p", vnode, ahandle); +// SMDDropVnodeMsg *pDrop = rpcMallocCont(sizeof(SMDDropVnodeMsg)); +// SRpcMsg rpcMsg = { +// .handle = ahandle, +// .pCont = pDrop, +// .contLen = pDrop ? sizeof(SMDDropVnodeMsg) : 0, +// .code = 0, +// .msgType = TSDB_MSG_TYPE_MD_DROP_VNODE +// }; +// rpcSendRequest(tsMgmtDClientRpc, ipSet, &rpcMsg); +//} +// +//void mgmtSendDropVgroupMsg(SVgObj *pVgroup, void *ahandle) { +// mTrace("vgroup:%d send free vgroup msg, ahandle:%p", pVgroup->vgId, ahandle); +// for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { +// SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); +// mgmtSendDropVnodeMsg(pVgroup->vgId, pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); +// } +//} +//// +////int32_t mgmtCfgDynamicOptions(SDnodeObj *pDnode, char *msg) { +//// char *option, *value; +//// int32_t olen, valen; +//// +//// paGetToken(msg, &option, &olen); +//// if (strncasecmp(option, "unremove", 8) == 0) { +//// mgmtSetDnodeUnRemove(pDnode); +//// return TSDB_CODE_SUCCESS; +//// } else if (strncasecmp(option, "score", 5) == 0) { +//// paGetToken(option + olen + 1, &value, &valen); +//// if (valen > 0) { +//// int32_t score = atoi(value); +//// mTrace("dnode:%s, custom score set from:%d to:%d", taosIpStr(pDnode->privateIp), pDnode->customScore, score); +//// pDnode->customScore = score; +//// mgmtUpdateDnode(pDnode); +//// //mgmtStartBalanceTimer(15); +//// } +//// return TSDB_CODE_INVALID_SQL; +//// } else if (strncasecmp(option, "bandwidth", 9) == 0) { +//// paGetToken(msg, &value, &valen); +//// if (valen > 0) { +//// int32_t bandwidthMb = atoi(value); +//// if (bandwidthMb >= 0 && bandwidthMb < 10000000) { +//// mTrace("dnode:%s, bandwidth(Mb) set from:%d to:%d", taosIpStr(pDnode->privateIp), pDnode->bandwidthMb, bandwidthMb); +//// pDnode->bandwidthMb = bandwidthMb; +//// mgmtUpdateDnode(pDnode); +//// return TSDB_CODE_SUCCESS; +//// } +//// } +//// return TSDB_CODE_INVALID_SQL; +//// } +//// +//// return -1; +////} +//// +////int32_t mgmtSendCfgDnodeMsg(char *cont) { +//// SDnodeObj *pDnode; +//// SCfgDnodeMsg * pCfg = (SCfgDnodeMsg *)cont; +//// uint32_t ip; +//// +//// ip = inet_addr(pCfg->ip); +//// pDnode = mgmtGetDnode(ip); +//// if (pDnode == NULL) { +//// mError("dnode ip:%s not configured", pCfg->ip); +//// return TSDB_CODE_NOT_CONFIGURED; +//// } +//// +//// mTrace("dnode:%s, dynamic option received, content:%s", taosIpStr(pDnode->privateIp), pCfg->config); +//// int32_t code = mgmtCfgDynamicOptions(pDnode, pCfg->config); +//// if (code != -1) { +//// return code; +//// } +//// +////#ifdef CLUSTER +//// pStart = taosBuildReqMsg(pDnode->thandle, TSDB_MSG_TYPE_MD_CONFIG_DNODE); +//// if (pStart == NULL) return TSDB_CODE_NODE_OFFLINE; +//// pMsg = pStart; +//// +//// memcpy(pMsg, cont, sizeof(SCfgDnodeMsg)); +//// pMsg += sizeof(SCfgDnodeMsg); +//// +//// msgLen = pMsg - pStart; +//// mgmtSendMsgToDnode(pDnode, pStart, msgLen); +////#else +//// (void)tsCfgDynamicOptions(pCfg->config); +////#endif +//// return 0; +////} diff --git a/src/mnode/src/mgmtDServer.c b/src/mnode/src/mgmtDServer.c new file mode 100644 index 0000000000..c55731e52a --- /dev/null +++ b/src/mnode/src/mgmtDServer.c @@ -0,0 +1,453 @@ +/* + * 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 "taoserror.h" +#include "trpc.h" +#include "tsched.h" +#include "tstatus.h" +#include "tsystem.h" +#include "tutil.h" +#include "dnode.h" +#include "mnode.h" +#include "mgmtBalance.h" +#include "mgmtDb.h" +#include "mgmtDServer.h" +#include "mgmtGrant.h" +#include "mgmtProfile.h" +#include "mgmtShell.h" +#include "mgmtTable.h" +#include "mgmtVgroup.h" + + +static void mgmtProcessMsgFromDnode(SRpcMsg *rpcMsg); +static int mgmtDServerRetrieveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey); +static void (*mgmtProcessDnodeMsgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *rpcMsg); +static void *tsMgmtDServerRpc; + +int32_t mgmtInitDServer() { + SRpcInit rpcInit = {0}; + rpcInit.localIp = tsAnyIp ? "0.0.0.0" : tsPrivateIp;; + rpcInit.localPort = tsMgmtDnodePort; + rpcInit.label = "MND-DS"; + rpcInit.numOfThreads = 1; + rpcInit.cfp = mgmtProcessMsgFromDnode; + rpcInit.sessions = tsMaxDnodes * 5; + rpcInit.connType = TAOS_CONN_SERVER; + rpcInit.idleTime = tsShellActivityTimer * 1000; + rpcInit.afp = mgmtDServerRetrieveAuth; + + tsMgmtDServerRpc = rpcOpen(&rpcInit); + if (tsMgmtDServerRpc == NULL) { + mError("failed to init server connection to dnode"); + return -1; + } + + mPrint("server connection to dnode is opened"); + return 0; +} + +void mgmtCleanupDServer() { + if (tsMgmtDServerRpc) { + rpcClose(tsMgmtDServerRpc); + tsMgmtDServerRpc = NULL; + mPrint("server connection to dnode is closed"); + } +} + +void mgmtAddDServerMsgHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)) { + mgmtProcessDnodeMsgFp[msgType] = fp; +} + +static void mgmtProcessMsgFromDnode(SRpcMsg *rpcMsg) { + if (mgmtProcessDnodeMsgFp[rpcMsg->msgType]) { + (*mgmtProcessDnodeMsgFp[rpcMsg->msgType])(rpcMsg); + } else { + mError("%s is not processed", taosMsg[rpcMsg->msgType]); + } + + rpcFreeCont(rpcMsg->pCont); +} + +static int mgmtDServerRetrieveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey) { + return TSDB_CODE_SUCCESS; +} + +// +// +//static void mgmtProcessTableCfgMsg(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle) { +// STableCfgMsg *pCfg = (STableCfgMsg *) pCont; +// pCfg->dnode = htonl(pCfg->dnode); +// pCfg->vnode = htonl(pCfg->vnode); +// pCfg->sid = htonl(pCfg->sid); +// mTrace("dnode:%s, vnode:%d, sid:%d, receive table config msg", taosIpStr(pCfg->dnode), pCfg->vnode, pCfg->sid); +// +// if (!sdbMaster) { +// mError("dnode:%s, vnode:%d, sid:%d, not master, redirect it", taosIpStr(pCfg->dnode), pCfg->vnode, pCfg->sid); +// mgmtSendRspToDnode(thandle, msgType + 1, TSDB_CODE_REDIRECT, NULL, 0); +// return; +// } +// +// STableInfo *pTable = mgmtGetTableByPos(pCfg->dnode, pCfg->vnode, pCfg->sid); +// if (pTable == NULL) { +// mError("dnode:%s, vnode:%d, sid:%d, table not found", taosIpStr(pCfg->dnode), pCfg->vnode, pCfg->sid); +// mgmtSendRspToDnode(thandle, msgType + 1, TSDB_CODE_INVALID_TABLE, NULL, 0); +// return; +// } +// +// mgmtSendRspToDnode(thandle, msgType + 1, TSDB_CODE_SUCCESS, NULL, 0); +// +// //TODO +// SRpcIpSet ipSet = mgmtGetIpSetFromIp(pCfg->dnode); +// mgmtSendCreateTableMsg(NULL, &ipSet, NULL); +//} +// +//static void mgmtProcessVnodeCfgMsg(int8_t msgType, int8_t *pCont, int32_t contLen, void *pConn) { +// if (!sdbMaster) { +// mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_REDIRECT, NULL, 0); +// return; +// } +// +// SVpeerCfgMsg *pCfg = (SVpeerCfgMsg *) pCont; +// pCfg->dnode = htonl(pCfg->dnode); +// pCfg->vnode = htonl(pCfg->vnode); +// +// SVgObj *pVgroup = mgmtGetVgroupByVnode(pCfg->dnode, pCfg->vnode); +// if (pVgroup == NULL) { +// mTrace("dnode:%s, vnode:%d, no vgroup info", taosIpStr(pCfg->dnode), pCfg->vnode); +// mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_NOT_ACTIVE_VNODE, NULL, 0); +// return; +// } +// +// mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_SUCCESS, NULL, 0); +// +// SRpcIpSet ipSet = mgmtGetIpSetFromIp(pCfg->dnode); +// mgmtSendCreateVnodeMsg(pVgroup, pCfg->vnode, &ipSet, NULL); +//} +// +//static void mgmtProcessCreateTableRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { +// mTrace("create table rsp received, thandle:%p code:%d", thandle, code); +// if (thandle == NULL) return; +// +// SProcessInfo *info = thandle; +// assert(info->type == TSDB_PROCESS_CREATE_TABLE || info->type == TSDB_PROCESS_CREATE_TABLE_GET_META); +// STableInfo *pTable = info->ahandle; +// +// if (code != TSDB_CODE_SUCCESS) { +// mError("table:%s, failed to create in dnode, code:%d, set it dirty", pTable->tableId); +// mgmtSetTableDirty(pTable, true); +// } else { +// mTrace("table:%s, created in dnode", pTable->tableId); +// mgmtSetTableDirty(pTable, false); +// } +// +// if (code != TSDB_CODE_SUCCESS) { +// SRpcMsg rpcMsg = {0}; +// rpcMsg.code = code; +// rpcMsg.handle = info->thandle; +// rpcSendResponse(&rpcMsg); +// } else { +// if (info->type == TSDB_PROCESS_CREATE_TABLE_GET_META) { +// mTrace("table:%s, start to process get meta", pTable->tableId); +// mgmtProcessGetTableMeta(pTable, thandle); +// } else { +// SRpcMsg rpcMsg = {0}; +// rpcMsg.code = code; +// rpcMsg.handle = info->thandle; +// rpcSendResponse(&rpcMsg); +// } +// } +// +// free(info); +//} +// +//void mgmtSendCreateTableMsg(SDMCreateTableMsg *pCreate, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("table:%s, send create table msg, ahandle:%p", pCreate->tableId, ahandle); +// mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_MD_CREATE_TABLE, pCreate, htonl(pCreate->contLen), ahandle); +//} +// +//static void mgmtProcessRemoveTableRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { +// mTrace("remove table rsp received, thandle:%p code:%d", thandle, code); +//} +// +//void mgmtSendDropTableMsg(SMDDropTableMsg *pRemove, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("table:%s, sid:%d send remove table msg, ahandle:%p", pRemove->tableId, htonl(pRemove->sid), ahandle); +// if (pRemove != NULL) { +// mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_MD_DROP_TABLE, pRemove, sizeof(SMDDropTableMsg), ahandle); +// } +//} +// +//static void mgmtProcessDropVnodeRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { +// mTrace("free vnode rsp received, thandle:%p code:%d", thandle, code); +//} +// +//static void mgmtProcessDropStableRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { +// mTrace("drop stable rsp received, thandle:%p code:%d", thandle, code); +//} +// +//static void mgmtProcessCreateVnodeRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { +// mTrace("create vnode rsp received, thandle:%p code:%d", thandle, code); +// if (thandle == NULL) return; +// +// SProcessInfo *info = thandle; +// assert(info->type == TSDB_PROCESS_CREATE_VGROUP || info->type == TSDB_PROCESS_CREATE_VGROUP_GET_META); +// info->received++; +// SVgObj *pVgroup = info->ahandle; +// +// bool isGetMeta = false; +// if (info->type == TSDB_PROCESS_CREATE_VGROUP_GET_META) { +// isGetMeta = true; +// } +// +// mTrace("vgroup:%d, received:%d numOfVnodes:%d", pVgroup->vgId, info->received, pVgroup->numOfVnodes); +// if (info->received == pVgroup->numOfVnodes) { +// mgmtProcessCreateTable(pVgroup, info->cont, info->contLen, info->thandle, isGetMeta); +// free(info); +// } +//} +// +//void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle) { +// mTrace("vgroup:%d, send create all vnodes msg, ahandle:%p", pVgroup->vgId, ahandle); +// for (int i = 0; i < pVgroup->numOfVnodes; ++i) { +// SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); +// mgmtSendCreateVnodeMsg(pVgroup, pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); +// } +//} +// +//void mgmtSendCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("vgroup:%d, send create vnode:%d msg, ahandle:%p", pVgroup->vgId, vnode, ahandle); +// SMDCreateVnodeMsg *pVpeer = mgmtBuildCreateVnodeMsg(pVgroup, vnode); +// if (pVpeer != NULL) { +// mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_MD_CREATE_VNODE, pVpeer, sizeof(SMDCreateVnodeMsg), ahandle); +// } +//} +// +//static void mgmtProcessDnodeGrantMsg(void *pCont, void *thandle) { +// if (mgmtUpdateGrantInfoFp) { +// mgmtUpdateGrantInfoFp(pCont); +// mTrace("grant info is updated"); +// } +// +// SRpcMsg rpcMsg = {0}; +// rpcMsg.code = TSDB_CODE_SUCCESS; +// rpcMsg.handle = thandle; +// rpcSendResponse(&rpcMsg); +//} +// +//void mgmtProcessMsgFromDnode(char msgType, void *pCont, int32_t contLen, void *pConn, int32_t code) { +// if (msgType < 0 || msgType >= TSDB_MSG_TYPE_MAX) { +// mError("invalid msg type:%d", msgType); +// return; +// } +// +// mTrace("msg:%d:%s is received from dnode, pConn:%p", msgType, taosMsg[(int8_t)msgType], pConn); +// +// if (msgType == TSDB_MSG_TYPE_TABLE_CFG) { +// mgmtProcessTableCfgMsg(msgType, pCont, contLen, pConn); +// } else if (msgType == TSDB_MSG_TYPE_DM_CONFIG_VNODE) { +// mgmtProcessVnodeCfgMsg(msgType, pCont, contLen, pConn); +// } else if (msgType == TSDB_MSG_TYPE_MD_CREATE_TABLE_RSP) { +// mgmtProcessCreateTableRsp(msgType, pCont, contLen, pConn, code); +// } else if (msgType == TSDB_MSG_TYPE_MD_DROP_TABLE_RSP) { +// mgmtProcessRemoveTableRsp(msgType, pCont, contLen, pConn, code); +// } else if (msgType == TSDB_MSG_TYPE_MD_CREATE_VNODE_RSP) { +// mgmtProcessCreateVnodeRsp(msgType, pCont, contLen, pConn, code); +// } else if (msgType == TSDB_MSG_TYPE_MD_DROP_VNODE_RSP) { +// mgmtProcessDropVnodeRsp(msgType, pCont, contLen, pConn, code); +// } else if (msgType == TSDB_MSG_TYPE_MD_DROP_STABLE) { +// mgmtProcessDropStableRsp(msgType, pCont, contLen, pConn, code); +// } else if (msgType == TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP) { +// } else if (msgType == TSDB_MSG_TYPE_ALTER_STREAM_RSP) { +// } else if (msgType == TSDB_MSG_TYPE_STATUS) { +// mgmtProcessDnodeStatus(msgType, pCont, contLen, pConn, code); +// } else if (msgType == TSDB_MSG_TYPE_GRANT) { +// mgmtProcessDnodeGrantMsg(pCont, pConn); +// } else { +// mError("%s from dnode is not processed", taosMsg[(int8_t)msgType]); +// } +// +// //rpcFreeCont(pCont); +//} +// +//void mgmtSendAlterStreamMsg(STableInfo *pTable, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("table:%s, sid:%d send alter stream msg, ahandle:%p", pTable->tableId, pTable->sid, ahandle); +//} +// +//void mgmtSendDropVnodeMsg(int32_t vnode, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("vnode:%d send free vnode msg, ahandle:%p", vnode, ahandle); +// +// SMDDropVnodeMsg *pFreeVnode = rpcMallocCont(sizeof(SMDDropVnodeMsg)); +// if (pFreeVnode != NULL) { +// pFreeVnode->vnode = htonl(vnode); +// mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_MD_DROP_VNODE, pFreeVnode, sizeof(SMDDropVnodeMsg), ahandle); +// } +//} +// +//void mgmtSendDropVgroupMsg(SVgObj *pVgroup, void *ahandle) { +// mTrace("vgroup:%d send free vgroup msg, ahandle:%p", pVgroup->vgId, ahandle); +// +// for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { +// SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); +// mgmtSendDropVnodeMsg(pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); +// } +//} +// +//int32_t mgmtCfgDynamicOptions(SDnodeObj *pDnode, char *msg) { +// char *option, *value; +// int32_t olen, valen; +// +// paGetToken(msg, &option, &olen); +// if (strncasecmp(option, "unremove", 8) == 0) { +// mgmtSetDnodeUnRemove(pDnode); +// return TSDB_CODE_SUCCESS; +// } else if (strncasecmp(option, "score", 5) == 0) { +// paGetToken(option + olen + 1, &value, &valen); +// if (valen > 0) { +// int32_t score = atoi(value); +// mTrace("dnode:%s, custom score set from:%d to:%d", taosIpStr(pDnode->privateIp), pDnode->customScore, score); +// pDnode->customScore = score; +// mgmtUpdateDnode(pDnode); +// //mgmtStartBalanceTimer(15); +// } +// return TSDB_CODE_INVALID_SQL; +// } else if (strncasecmp(option, "bandwidth", 9) == 0) { +// paGetToken(msg, &value, &valen); +// if (valen > 0) { +// int32_t bandwidthMb = atoi(value); +// if (bandwidthMb >= 0 && bandwidthMb < 10000000) { +// mTrace("dnode:%s, bandwidth(Mb) set from:%d to:%d", taosIpStr(pDnode->privateIp), pDnode->bandwidthMb, bandwidthMb); +// pDnode->bandwidthMb = bandwidthMb; +// mgmtUpdateDnode(pDnode); +// return TSDB_CODE_SUCCESS; +// } +// } +// return TSDB_CODE_INVALID_SQL; +// } +// +// return -1; +//} +// +//int32_t mgmtSendCfgDnodeMsg(char *cont) { +////#ifdef CLUSTER +//// char * pMsg, *pStart; +//// int32_t msgLen = 0; +////#endif +//// +//// SDnodeObj *pDnode; +//// SCfgDnodeMsg * pCfg = (SCfgDnodeMsg *)cont; +//// uint32_t ip; +//// +//// ip = inet_addr(pCfg->ip); +//// pDnode = mgmtGetDnode(ip); +//// if (pDnode == NULL) { +//// mError("dnode ip:%s not configured", pCfg->ip); +//// return TSDB_CODE_NOT_CONFIGURED; +//// } +//// +//// mTrace("dnode:%s, dynamic option received, content:%s", taosIpStr(pDnode->privateIp), pCfg->config); +//// int32_t code = mgmtCfgDynamicOptions(pDnode, pCfg->config); +//// if (code != -1) { +//// return code; +//// } +//// +////#ifdef CLUSTER +//// pStart = taosBuildReqMsg(pDnode->thandle, TSDB_MSG_TYPE_MD_CONFIG_DNODE); +//// if (pStart == NULL) return TSDB_CODE_NODE_OFFLINE; +//// pMsg = pStart; +//// +//// memcpy(pMsg, cont, sizeof(SCfgDnodeMsg)); +//// pMsg += sizeof(SCfgDnodeMsg); +//// +//// msgLen = pMsg - pStart; +//// mgmtSendMsgToDnode(pDnode, pStart, msgLen); +////#else +//// (void)tsCfgDynamicOptions(pCfg->config); +////#endif +// return 0; +//} +// +//int32_t mgmtInitDnodeInt() { +// if (mgmtInitDnodeIntFp) { +// return mgmtInitDnodeIntFp(); +// } else { +// return 0; +// } +//} +// +//void mgmtCleanUpDnodeInt() { +// if (mgmtCleanUpDnodeIntFp) { +// mgmtCleanUpDnodeIntFp(); +// } +//} +// +//void mgmtProcessDnodeStatus(int8_t msgType, void *pCont, int32_t contLen, void *pConn, int32_t code) { +// SStatusMsg *pStatus = (SStatusMsg *)pCont; +// +// SDnodeObj *pObj = mgmtGetDnode(htonl(pStatus->privateIp)); +// if (pObj == NULL) { +// mError("dnode:%s not exist", taosIpStr(pObj->privateIp)); +// mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_DNODE_NOT_EXIST, NULL, 0); +// return; +// } +// +// pObj->lastReboot = htonl(pStatus->lastReboot); +// pObj->numOfTotalVnodes = htons(pStatus->numOfTotalVnodes); +// pObj->openVnodes = htons(pStatus->openVnodes); +// pObj->numOfCores = htons(pStatus->numOfCores); +// pObj->diskAvailable = pStatus->diskAvailable; +// pObj->alternativeRole = pStatus->alternativeRole; +//// +//// if (mgmtProcessDnodeStatusFp) { +//// mgmtProcessDnodeStatusFp(pStatus, pObj, pConn); +//// return; +//// } +// +// pObj->status = TSDB_DN_STATUS_READY; +// +//// // wait vnode dropped +//// for (int32_t vnode = 0; vnode < pObj->numOfVnodes; ++vnode) { +//// SVnodeLoad *pVload = &(pObj->vload[vnode]); +//// if (pVload->dropStatus == TSDB_VN_DROP_STATUS_DROPPING) { +//// bool existInDnode = false; +//// for (int32_t j = 0; j < pObj->openVnodes; ++j) { +//// if (htonl(pStatus->load[j].vnode) == vnode) { +//// existInDnode = true; +//// break; +//// } +//// } +//// +//// if (!existInDnode) { +//// pVload->dropStatus = TSDB_VN_DROP_STATUS_READY; +//// pVload->status = TSDB_VN_STATUS_OFFLINE; +//// mgmtUpdateDnode(pObj); +//// mPrint("dnode:%s, vid:%d, drop finished", taosIpStr(pObj->privateIp), vnode); +//// taosTmrStart(mgmtMonitorDbDrop, 10000, NULL, tsMgmtTmr); +//// } +//// } else if (pVload->vgId == 0) { +//// /* +//// * In some cases, vnode information may be reported abnormally, recover it +//// */ +//// if (pVload->dropStatus != TSDB_VN_DROP_STATUS_READY || pVload->status != TSDB_VN_STATUS_OFFLINE) { +//// mPrint("dnode:%s, vid:%d, vgroup:%d status:%s dropStatus:%s, set it to avail status", +//// taosIpStr(pObj->privateIp), vnode, pVload->vgId, taosGetVnodeStatusStr(pVload->status), +//// taosGetVnodeDropStatusStr(pVload->dropStatus)); +//// pVload->dropStatus = TSDB_VN_DROP_STATUS_READY; +//// pVload->status = TSDB_VN_STATUS_OFFLINE; +//// mgmtUpdateDnode(pObj); +//// } +//// } +//// } +//} diff --git a/src/mnode/src/mgmtDb.c b/src/mnode/src/mgmtDb.c index 0186f51e16..efb306d8cc 100644 --- a/src/mnode/src/mgmtDb.c +++ b/src/mnode/src/mgmtDb.c @@ -23,7 +23,6 @@ #include "mgmtBalance.h" #include "mgmtDb.h" #include "mgmtDnode.h" -#include "mgmtDnodeInt.h" #include "mgmtGrant.h" #include "mgmtTable.h" #include "mgmtUser.h" @@ -295,7 +294,7 @@ int32_t mgmtSetDbDropping(SDbObj *pDb) { } } } -// mgmtSendRemoveVgroupMsg(pVgroup); +// mgmtSendDropVgroupMsg(pVgroup); pVgroup = pVgroup->next; } @@ -355,7 +354,7 @@ int32_t mgmtDropDb(SDbObj *pDb) { if (!finished) { SVgObj *pVgroup = pDb->pHead; while (pVgroup != NULL) { - mgmtSendRemoveVgroupMsg(pVgroup, NULL); + mgmtSendDropVgroupMsg(pVgroup, NULL); pVgroup = pVgroup->next; } return TSDB_CODE_ACTION_IN_PROGRESS; diff --git a/src/mnode/src/mgmtDnodeInt.c b/src/mnode/src/mgmtDnodeInt.c deleted file mode 100644 index 96fb226e54..0000000000 --- a/src/mnode/src/mgmtDnodeInt.c +++ /dev/null @@ -1,455 +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 "taoserror.h" -#include "tsched.h" -#include "tstatus.h" -#include "tsystem.h" -#include "tutil.h" -#include "dnode.h" -#include "mnode.h" -#include "mgmtBalance.h" -#include "mgmtDb.h" -#include "mgmtDnode.h" -#include "mgmtDnodeInt.h" -#include "mgmtGrant.h" -#include "mgmtProfile.h" -#include "mgmtShell.h" -#include "mgmtTable.h" -#include "mgmtVgroup.h" - -int32_t (*mgmtInitDnodeIntFp)() = NULL; -void (*mgmtCleanUpDnodeIntFp)() = NULL; - -void (*mgmtSendMsgToDnodeFp)(SRpcIpSet *ipSet, int8_t msgType, void *pCont, int32_t contLen, void *ahandle) = NULL; -void (*mgmtSendRspToDnodeFp)(void *handle, int32_t code, void *pCont, int32_t contLen) = NULL; -void *mgmtStatusTimer = NULL; - -static void mgmtProcessDnodeStatus(int8_t msgType, void *pCont, int32_t contLen, void *pConn, int32_t code); - -static void mgmtSendMsgToDnodeQueueFp(SSchedMsg *sched) { - int32_t contLen = *(int32_t *) (sched->msg - 4); - int32_t code = *(int32_t *) (sched->msg - 8); - int8_t msgType = *(int8_t *) (sched->msg - 9); - void *ahandle = sched->ahandle; - int8_t *pCont = sched->msg; - -// dnodeProcessMsgFromMgmt(msgType, pCont, contLen, ahandle, code); -} - -void mgmtSendMsgToDnode(SRpcIpSet *ipSet, int8_t msgType, void *pCont, int32_t contLen, void *ahandle) { - mTrace("msg:%d:%s is sent to dnode, ahandle:%p", msgType, taosMsg[msgType], ahandle); - if (mgmtSendMsgToDnodeFp) { - mgmtSendMsgToDnodeFp(ipSet, msgType, pCont, contLen, ahandle); - } else { - if (pCont == NULL) { - pCont = rpcMallocCont(1); - contLen = 0; - } - SSchedMsg schedMsg = {0}; - schedMsg.fp = mgmtSendMsgToDnodeQueueFp; - schedMsg.msg = pCont; - schedMsg.ahandle = ahandle; - *(int32_t *) (pCont - 4) = contLen; - *(int32_t *) (pCont - 8) = TSDB_CODE_SUCCESS; - *(int8_t *) (pCont - 9) = msgType; - taosScheduleTask(tsDnodeMgmtQhandle, &schedMsg); - } -} - -void mgmtSendRspToDnode(void *pConn, int8_t msgType, int32_t code, void *pCont, int32_t contLen) { - mTrace("rsp:%d:%s is sent to dnode", msgType, taosMsg[msgType]); - if (mgmtSendRspToDnodeFp) { - mgmtSendRspToDnodeFp(pConn, code, pCont, contLen); - } else { - if (pCont == NULL) { - pCont = rpcMallocCont(1); - contLen = 0; - } - SSchedMsg schedMsg = {0}; - schedMsg.fp = mgmtSendMsgToDnodeQueueFp; - schedMsg.msg = pCont; - *(int32_t *) (pCont - 4) = contLen; - *(int32_t *) (pCont - 8) = code; - *(int8_t *) (pCont - 9) = msgType; - taosScheduleTask(tsDnodeMgmtQhandle, &schedMsg); - } -} - -static void mgmtProcessTableCfgMsg(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle) { - STableCfgMsg *pCfg = (STableCfgMsg *) pCont; - pCfg->dnode = htonl(pCfg->dnode); - pCfg->vnode = htonl(pCfg->vnode); - pCfg->sid = htonl(pCfg->sid); - mTrace("dnode:%s, vnode:%d, sid:%d, receive table config msg", taosIpStr(pCfg->dnode), pCfg->vnode, pCfg->sid); - - if (!sdbMaster) { - mError("dnode:%s, vnode:%d, sid:%d, not master, redirect it", taosIpStr(pCfg->dnode), pCfg->vnode, pCfg->sid); - mgmtSendRspToDnode(thandle, msgType + 1, TSDB_CODE_REDIRECT, NULL, 0); - return; - } - - STableInfo *pTable = mgmtGetTableByPos(pCfg->dnode, pCfg->vnode, pCfg->sid); - if (pTable == NULL) { - mError("dnode:%s, vnode:%d, sid:%d, table not found", taosIpStr(pCfg->dnode), pCfg->vnode, pCfg->sid); - mgmtSendRspToDnode(thandle, msgType + 1, TSDB_CODE_INVALID_TABLE, NULL, 0); - return; - } - - mgmtSendRspToDnode(thandle, msgType + 1, TSDB_CODE_SUCCESS, NULL, 0); - - //TODO - SRpcIpSet ipSet = mgmtGetIpSetFromIp(pCfg->dnode); - mgmtSendCreateTableMsg(NULL, &ipSet, NULL); -} - -static void mgmtProcessVnodeCfgMsg(int8_t msgType, int8_t *pCont, int32_t contLen, void *pConn) { - if (!sdbMaster) { - mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_REDIRECT, NULL, 0); - return; - } - - SVpeerCfgMsg *pCfg = (SVpeerCfgMsg *) pCont; - pCfg->dnode = htonl(pCfg->dnode); - pCfg->vnode = htonl(pCfg->vnode); - - SVgObj *pVgroup = mgmtGetVgroupByVnode(pCfg->dnode, pCfg->vnode); - if (pVgroup == NULL) { - mTrace("dnode:%s, vnode:%d, no vgroup info", taosIpStr(pCfg->dnode), pCfg->vnode); - mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_NOT_ACTIVE_VNODE, NULL, 0); - return; - } - - mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_SUCCESS, NULL, 0); - - SRpcIpSet ipSet = mgmtGetIpSetFromIp(pCfg->dnode); - mgmtSendCreateVnodeMsg(pVgroup, pCfg->vnode, &ipSet, NULL); -} - -static void mgmtProcessCreateTableRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { - mTrace("create table rsp received, thandle:%p code:%d", thandle, code); - if (thandle == NULL) return; - - SProcessInfo *info = thandle; - assert(info->type == TSDB_PROCESS_CREATE_TABLE || info->type == TSDB_PROCESS_CREATE_TABLE_GET_META); - STableInfo *pTable = info->ahandle; - - if (code != TSDB_CODE_SUCCESS) { - mError("table:%s, failed to create in dnode, code:%d, set it dirty", pTable->tableId); - mgmtSetTableDirty(pTable, true); - } else { - mTrace("table:%s, created in dnode", pTable->tableId); - mgmtSetTableDirty(pTable, false); - } - - if (code != TSDB_CODE_SUCCESS) { - SRpcMsg rpcMsg = {0}; - rpcMsg.code = code; - rpcMsg.handle = info->thandle; - rpcSendResponse(&rpcMsg); - } else { - if (info->type == TSDB_PROCESS_CREATE_TABLE_GET_META) { - mTrace("table:%s, start to process get meta", pTable->tableId); - mgmtProcessGetTableMeta(pTable, thandle); - } else { - SRpcMsg rpcMsg = {0}; - rpcMsg.code = code; - rpcMsg.handle = info->thandle; - rpcSendResponse(&rpcMsg); - } - } - - free(info); -} - -void mgmtSendCreateTableMsg(SDCreateTableMsg *pCreate, SRpcIpSet *ipSet, void *ahandle) { - mTrace("table:%s, send create table msg, ahandle:%p", pCreate->tableId, ahandle); - mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_DNODE_CREATE_TABLE, pCreate, htonl(pCreate->contLen), ahandle); -} - -static void mgmtProcessRemoveTableRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { - mTrace("remove table rsp received, thandle:%p code:%d", thandle, code); -} - -void mgmtSendRemoveTableMsg(SDRemoveTableMsg *pRemove, SRpcIpSet *ipSet, void *ahandle) { - mTrace("table:%s, sid:%d send remove table msg, ahandle:%p", pRemove->tableId, htonl(pRemove->sid), ahandle); - if (pRemove != NULL) { - mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_DNODE_REMOVE_TABLE, pRemove, sizeof(SDRemoveTableMsg), ahandle); - } -} - -static void mgmtProcessFreeVnodeRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { - mTrace("free vnode rsp received, thandle:%p code:%d", thandle, code); -} - -static void mgmtProcessDropStableRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { - mTrace("drop stable rsp received, thandle:%p code:%d", thandle, code); -} - -static void mgmtProcessCreateVnodeRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { - mTrace("create vnode rsp received, thandle:%p code:%d", thandle, code); - if (thandle == NULL) return; - - SProcessInfo *info = thandle; - assert(info->type == TSDB_PROCESS_CREATE_VGROUP || info->type == TSDB_PROCESS_CREATE_VGROUP_GET_META); - info->received++; - SVgObj *pVgroup = info->ahandle; - - bool isGetMeta = false; - if (info->type == TSDB_PROCESS_CREATE_VGROUP_GET_META) { - isGetMeta = true; - } - - mTrace("vgroup:%d, received:%d numOfVnodes:%d", pVgroup->vgId, info->received, pVgroup->numOfVnodes); - if (info->received == pVgroup->numOfVnodes) { - mgmtProcessCreateTable(pVgroup, info->cont, info->contLen, info->thandle, isGetMeta); - free(info); - } -} - -void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle) { - mTrace("vgroup:%d, send create all vnodes msg, ahandle:%p", pVgroup->vgId, ahandle); - for (int i = 0; i < pVgroup->numOfVnodes; ++i) { - SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); - mgmtSendCreateVnodeMsg(pVgroup, pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); - } -} - -void mgmtSendCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode, SRpcIpSet *ipSet, void *ahandle) { - mTrace("vgroup:%d, send create vnode:%d msg, ahandle:%p", pVgroup->vgId, vnode, ahandle); - SCreateVnodeMsg *pVpeer = mgmtBuildVpeersMsg(pVgroup, vnode); - if (pVpeer != NULL) { - mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_CREATE_VNODE, pVpeer, sizeof(SCreateVnodeMsg), ahandle); - } -} - -static void mgmtProcessDnodeGrantMsg(void *pCont, void *thandle) { - if (mgmtUpdateGrantInfoFp) { - mgmtUpdateGrantInfoFp(pCont); - mTrace("grant info is updated"); - } - - SRpcMsg rpcMsg = {0}; - rpcMsg.code = TSDB_CODE_SUCCESS; - rpcMsg.handle = thandle; - rpcSendResponse(&rpcMsg); -} - -void mgmtProcessMsgFromDnode(char msgType, void *pCont, int32_t contLen, void *pConn, int32_t code) { - if (msgType < 0 || msgType >= TSDB_MSG_TYPE_MAX) { - mError("invalid msg type:%d", msgType); - return; - } - - mTrace("msg:%d:%s is received from dnode, pConn:%p", msgType, taosMsg[(int8_t)msgType], pConn); - - if (msgType == TSDB_MSG_TYPE_TABLE_CFG) { - mgmtProcessTableCfgMsg(msgType, pCont, contLen, pConn); - } else if (msgType == TSDB_MSG_TYPE_CONFIG_VNODE) { - mgmtProcessVnodeCfgMsg(msgType, pCont, contLen, pConn); - } else if (msgType == TSDB_MSG_TYPE_DNODE_CREATE_TABLE_RSP) { - mgmtProcessCreateTableRsp(msgType, pCont, contLen, pConn, code); - } else if (msgType == TSDB_MSG_TYPE_DNODE_REMOVE_TABLE_RSP) { - mgmtProcessRemoveTableRsp(msgType, pCont, contLen, pConn, code); - } else if (msgType == TSDB_MSG_TYPE_CREATE_VNODE_RSP) { - mgmtProcessCreateVnodeRsp(msgType, pCont, contLen, pConn, code); - } else if (msgType == TSDB_MSG_TYPE_DROP_VNODE_RSP) { - mgmtProcessFreeVnodeRsp(msgType, pCont, contLen, pConn, code); - } else if (msgType == TSDB_MSG_TYPE_DROP_STABLE) { - mgmtProcessDropStableRsp(msgType, pCont, contLen, pConn, code); - } else if (msgType == TSDB_MSG_TYPE_DNODE_CFG_RSP) { - } else if (msgType == TSDB_MSG_TYPE_ALTER_STREAM_RSP) { - } else if (msgType == TSDB_MSG_TYPE_STATUS) { - mgmtProcessDnodeStatus(msgType, pCont, contLen, pConn, code); - } else if (msgType == TSDB_MSG_TYPE_GRANT) { - mgmtProcessDnodeGrantMsg(pCont, pConn); - } else { - mError("%s from dnode is not processed", taosMsg[(int8_t)msgType]); - } - - //rpcFreeCont(pCont); -} - -void mgmtSendAlterStreamMsg(STableInfo *pTable, SRpcIpSet *ipSet, void *ahandle) { - mTrace("table:%s, sid:%d send alter stream msg, ahandle:%p", pTable->tableId, pTable->sid, ahandle); -} - -void mgmtSendOneFreeVnodeMsg(int32_t vnode, SRpcIpSet *ipSet, void *ahandle) { - mTrace("vnode:%d send free vnode msg, ahandle:%p", vnode, ahandle); - - SDropVnodeMsg *pFreeVnode = rpcMallocCont(sizeof(SDropVnodeMsg)); - if (pFreeVnode != NULL) { - pFreeVnode->vnode = htonl(vnode); - mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_DROP_VNODE, pFreeVnode, sizeof(SDropVnodeMsg), ahandle); - } -} - -void mgmtSendRemoveVgroupMsg(SVgObj *pVgroup, void *ahandle) { - mTrace("vgroup:%d send free vgroup msg, ahandle:%p", pVgroup->vgId, ahandle); - - for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { - SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); - mgmtSendOneFreeVnodeMsg(pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); - } -} - -int32_t mgmtCfgDynamicOptions(SDnodeObj *pDnode, char *msg) { - char *option, *value; - int32_t olen, valen; - - paGetToken(msg, &option, &olen); - if (strncasecmp(option, "unremove", 8) == 0) { - mgmtSetDnodeUnRemove(pDnode); - return TSDB_CODE_SUCCESS; - } else if (strncasecmp(option, "score", 5) == 0) { - paGetToken(option + olen + 1, &value, &valen); - if (valen > 0) { - int32_t score = atoi(value); - mTrace("dnode:%s, custom score set from:%d to:%d", taosIpStr(pDnode->privateIp), pDnode->customScore, score); - pDnode->customScore = score; - mgmtUpdateDnode(pDnode); - //mgmtStartBalanceTimer(15); - } - return TSDB_CODE_INVALID_SQL; - } else if (strncasecmp(option, "bandwidth", 9) == 0) { - paGetToken(msg, &value, &valen); - if (valen > 0) { - int32_t bandwidthMb = atoi(value); - if (bandwidthMb >= 0 && bandwidthMb < 10000000) { - mTrace("dnode:%s, bandwidth(Mb) set from:%d to:%d", taosIpStr(pDnode->privateIp), pDnode->bandwidthMb, bandwidthMb); - pDnode->bandwidthMb = bandwidthMb; - mgmtUpdateDnode(pDnode); - return TSDB_CODE_SUCCESS; - } - } - return TSDB_CODE_INVALID_SQL; - } - - return -1; -} - -int32_t mgmtSendCfgDnodeMsg(char *cont) { -//#ifdef CLUSTER -// char * pMsg, *pStart; -// int32_t msgLen = 0; -//#endif -// -// SDnodeObj *pDnode; -// SCfgDnodeMsg * pCfg = (SCfgDnodeMsg *)cont; -// uint32_t ip; -// -// ip = inet_addr(pCfg->ip); -// pDnode = mgmtGetDnode(ip); -// if (pDnode == NULL) { -// mError("dnode ip:%s not configured", pCfg->ip); -// return TSDB_CODE_NOT_CONFIGURED; -// } -// -// mTrace("dnode:%s, dynamic option received, content:%s", taosIpStr(pDnode->privateIp), pCfg->config); -// int32_t code = mgmtCfgDynamicOptions(pDnode, pCfg->config); -// if (code != -1) { -// return code; -// } -// -//#ifdef CLUSTER -// pStart = taosBuildReqMsg(pDnode->thandle, TSDB_MSG_TYPE_DNODE_CFG); -// if (pStart == NULL) return TSDB_CODE_NODE_OFFLINE; -// pMsg = pStart; -// -// memcpy(pMsg, cont, sizeof(SCfgDnodeMsg)); -// pMsg += sizeof(SCfgDnodeMsg); -// -// msgLen = pMsg - pStart; -// mgmtSendMsgToDnode(pDnode, pStart, msgLen); -//#else -// (void)tsCfgDynamicOptions(pCfg->config); -//#endif - return 0; -} - -int32_t mgmtInitDnodeInt() { - if (mgmtInitDnodeIntFp) { - return mgmtInitDnodeIntFp(); - } else { - return 0; - } -} - -void mgmtCleanUpDnodeInt() { - if (mgmtCleanUpDnodeIntFp) { - mgmtCleanUpDnodeIntFp(); - } -} - -void mgmtProcessDnodeStatus(int8_t msgType, void *pCont, int32_t contLen, void *pConn, int32_t code) { - SStatusMsg *pStatus = (SStatusMsg *)pCont; - - SDnodeObj *pObj = mgmtGetDnode(htonl(pStatus->privateIp)); - if (pObj == NULL) { - mError("dnode:%s not exist", taosIpStr(pObj->privateIp)); - mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_DNODE_NOT_EXIST, NULL, 0); - return; - } - - pObj->lastReboot = htonl(pStatus->lastReboot); - pObj->numOfTotalVnodes = htons(pStatus->numOfTotalVnodes); - pObj->openVnodes = htons(pStatus->openVnodes); - pObj->numOfCores = htons(pStatus->numOfCores); - pObj->diskAvailable = pStatus->diskAvailable; - pObj->alternativeRole = pStatus->alternativeRole; -// -// if (mgmtProcessDnodeStatusFp) { -// mgmtProcessDnodeStatusFp(pStatus, pObj, pConn); -// return; -// } - - pObj->status = TSDB_DN_STATUS_READY; - -// // wait vnode dropped -// for (int32_t vnode = 0; vnode < pObj->numOfVnodes; ++vnode) { -// SVnodeLoad *pVload = &(pObj->vload[vnode]); -// if (pVload->dropStatus == TSDB_VN_DROP_STATUS_DROPPING) { -// bool existInDnode = false; -// for (int32_t j = 0; j < pObj->openVnodes; ++j) { -// if (htonl(pStatus->load[j].vnode) == vnode) { -// existInDnode = true; -// break; -// } -// } -// -// if (!existInDnode) { -// pVload->dropStatus = TSDB_VN_DROP_STATUS_READY; -// pVload->status = TSDB_VN_STATUS_OFFLINE; -// mgmtUpdateDnode(pObj); -// mPrint("dnode:%s, vid:%d, drop finished", taosIpStr(pObj->privateIp), vnode); -// taosTmrStart(mgmtMonitorDbDrop, 10000, NULL, tsMgmtTmr); -// } -// } else if (pVload->vgId == 0) { -// /* -// * In some cases, vnode information may be reported abnormally, recover it -// */ -// if (pVload->dropStatus != TSDB_VN_DROP_STATUS_READY || pVload->status != TSDB_VN_STATUS_OFFLINE) { -// mPrint("dnode:%s, vid:%d, vgroup:%d status:%s dropStatus:%s, set it to avail status", -// taosIpStr(pObj->privateIp), vnode, pVload->vgId, taosGetVnodeStatusStr(pVload->status), -// taosGetVnodeDropStatusStr(pVload->dropStatus)); -// pVload->dropStatus = TSDB_VN_DROP_STATUS_READY; -// pVload->status = TSDB_VN_STATUS_OFFLINE; -// mgmtUpdateDnode(pObj); -// } -// } -// } -} diff --git a/src/mnode/src/mgmtMnode.c b/src/mnode/src/mgmtMnode.c index b3b70d1d2e..5ed09b810f 100644 --- a/src/mnode/src/mgmtMnode.c +++ b/src/mnode/src/mgmtMnode.c @@ -14,6 +14,8 @@ */ #define _DEFAULT_SOURCE +#include "os.h" +#include "trpc.h" #include "tschemautil.h" #include "mgmtMnode.h" #include "mgmtUser.h" @@ -23,6 +25,10 @@ int32_t (*mgmtRemoveMnodeFp)(uint32_t privateIp) = NULL; int32_t (*mgmtGetMnodesNumFp)() = NULL; void * (*mgmtGetNextMnodeFp)(SShowObj *pShow, SSdbPeer **pMnode) = NULL; +bool mgmtCheckRedirect(void *handle) { + return false; +} + int32_t mgmtAddMnode(uint32_t privateIp, uint32_t publicIp) { if (mgmtAddMnodeFp) { return (*mgmtAddMnodeFp)(privateIp, publicIp); diff --git a/src/mnode/src/mgmtNormalTable.c b/src/mnode/src/mgmtNormalTable.c index fddb47cb7e..e4cc6c9b4c 100644 --- a/src/mnode/src/mgmtNormalTable.c +++ b/src/mnode/src/mgmtNormalTable.c @@ -24,7 +24,6 @@ #include "mnode.h" #include "mgmtAcct.h" #include "mgmtDb.h" -#include "mgmtDnodeInt.h" #include "mgmtGrant.h" #include "mgmtNormalTable.h" #include "mgmtSuperTable.h" @@ -289,9 +288,9 @@ void mgmtCleanUpNormalTables() { static void *mgmtBuildCreateNormalTableMsg(SNormalTableObj *pTable, SVgObj *pVgroup) { int32_t totalCols = pTable->numOfColumns; - int32_t contLen = sizeof(SDCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen; + int32_t contLen = sizeof(SDMCreateTableMsg) + totalCols * sizeof(SSchema) + pTable->sqlLen; - SDCreateTableMsg *pCreateTable = rpcMallocCont(contLen); + SDMCreateTableMsg *pCreateTable = rpcMallocCont(contLen); if (pCreateTable == NULL) { return NULL; } @@ -323,13 +322,13 @@ static void *mgmtBuildCreateNormalTableMsg(SNormalTableObj *pTable, SVgObj *pVgr pSchema++; } - memcpy(pCreateTable + sizeof(SDCreateTableMsg) + totalCols * sizeof(SSchema), pTable->sql, pTable->sqlLen); + memcpy(pCreateTable + sizeof(SDMCreateTableMsg) + totalCols * sizeof(SSchema), pTable->sql, pTable->sqlLen); return pCreateTable; } int32_t mgmtCreateNormalTable(SCreateTableMsg *pCreate, int32_t contLen, SVgObj *pVgroup, int32_t sid, - SDCreateTableMsg **pDCreateOut, STableInfo **pTableOut) { + SDMCreateTableMsg **pDCreateOut, STableInfo **pTableOut) { int32_t numOfTables = sdbGetNumOfRows(tsNormalTableSdb); if (numOfTables >= TSDB_MAX_NORMAL_TABLES) { mError("table:%s, numOfTables:%d exceed maxTables:%d", pCreate->tableId, numOfTables, TSDB_MAX_NORMAL_TABLES); @@ -406,7 +405,7 @@ int32_t mgmtDropNormalTable(SDbObj *pDb, SNormalTableObj *pTable) { return TSDB_CODE_OTHERS; } - SDRemoveTableMsg *pRemove = rpcMallocCont(sizeof(SDRemoveTableMsg)); + SMDDropTableMsg *pRemove = rpcMallocCont(sizeof(SMDDropTableMsg)); if (pRemove == NULL) { mError("table:%s, failed to drop normal table, no enough memory", pTable->tableId); return TSDB_CODE_SERV_OUT_OF_MEMORY; @@ -423,7 +422,7 @@ int32_t mgmtDropNormalTable(SDbObj *pDb, SNormalTableObj *pTable) { } SRpcIpSet ipSet = mgmtGetIpSetFromVgroup(pVgroup); - mgmtSendRemoveTableMsg(pRemove, &ipSet, NULL); + mgmtSendDropTableMsg(pRemove, &ipSet, NULL); if (sdbDeleteRow(tsNormalTableSdb, pTable) < 0) { mError("table:%s, update ntables sdb error", pTable->tableId); diff --git a/src/mnode/src/mgmtShell.c b/src/mnode/src/mgmtShell.c index 6567872861..28706b99c0 100644 --- a/src/mnode/src/mgmtShell.c +++ b/src/mnode/src/mgmtShell.c @@ -28,7 +28,6 @@ #include "mgmtChildTable.h" #include "mgmtDb.h" #include "mgmtDnode.h" -#include "mgmtDnodeInt.h" #include "mgmtGrant.h" #include "mgmtMnode.h" #include "mgmtNormalTable.h" @@ -39,58 +38,29 @@ #include "mgmtUser.h" #include "mgmtVgroup.h" -typedef int32_t (*GetMateFp)(STableMeta *pMeta, SShowObj *pShow, void *pConn); -typedef int32_t (*RetrieveMetaFp)(SShowObj *pShow, char *data, int32_t rows, void *pConn); -static GetMateFp mgmtGetMetaFp[TSDB_MGMT_TABLE_MAX] = {0}; -static RetrieveMetaFp mgmtRetrieveFp[TSDB_MGMT_TABLE_MAX] = {0}; +typedef int32_t (*SShowMetaFp)(STableMeta *pMeta, SShowObj *pShow, void *pConn); +typedef int32_t (*SShowRetrieveFp)(SShowObj *pShow, char *data, int32_t rows, void *pConn); -static void mgmtInitShowMsgFp(); -static void mgmtInitProcessShellMsg(); -static void mgmtProcessMsgFromShell(SRpcMsg *msg); -static void (*mgmtProcessShellMsg[TSDB_MSG_TYPE_MAX])(SRpcMsg *msg); -static void mgmtProcessUnSupportMsg(SRpcMsg *msg); -static int mgmtRetriveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey); +static void mgmtProcessMsgFromShell(SRpcMsg *pMsg); +static void mgmtProcessShowMsg(SRpcMsg *rpcMsg); +static void mgmtProcessRetrieveMsg(SRpcMsg *rpcMsg); +static int mgmtShellRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey); -void *tsShellConnServer = NULL; - -void mgmtProcessTranRequest(SSchedMsg *sched) { - SRpcMsg rpcMsg; - rpcMsg.msgType = *(int8_t *) (sched->msg); - rpcMsg.contLen = *(int32_t *) (sched->msg + sizeof(int8_t)); - rpcMsg.pCont = sched->msg + sizeof(int32_t) + sizeof(int8_t); - rpcMsg.handle = sched->thandle; - rpcMsg.code = TSDB_CODE_SUCCESS; - - (*mgmtProcessShellMsg[rpcMsg.msgType])(&rpcMsg); - if (sched->msg) { - free(sched->msg); - } -} - -void mgmtAddToTranRequest(SRpcMsg *rpcMsg) { - SSchedMsg schedMsg; - schedMsg.msg = malloc(rpcMsg->contLen + sizeof(int32_t) + sizeof(int8_t)); - schedMsg.fp = mgmtProcessTranRequest; - schedMsg.tfp = NULL; - schedMsg.thandle = rpcMsg->handle; - *(int8_t *) (schedMsg.msg) = rpcMsg->msgType; - *(int32_t *) (schedMsg.msg + sizeof(int8_t)) = rpcMsg->contLen; - memcpy(schedMsg.msg + sizeof(int32_t) + sizeof(int8_t), rpcMsg->pCont, rpcMsg->contLen); - - taosScheduleTask(tsMgmtTranQhandle, &schedMsg); -} +static void *tsMgmtShellRpc = NULL; +static void (*tsMgmtProcessShellMsgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *) = {0}; +static SShowMetaFp tsMgmtShowMetaFp[TSDB_MGMT_TABLE_MAX] = {0}; +static SShowRetrieveFp tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_MAX] = {0}; int32_t mgmtInitShell() { - SRpcInit rpcInit; - mgmtInitProcessShellMsg(); - mgmtInitShowMsgFp(); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_SHOW, mgmtProcessShowMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_RETRIEVE, mgmtProcessRetrieveMsg); int32_t numOfThreads = tsNumOfCores * tsNumOfThreadsPerCore / 4.0; if (numOfThreads < 1) { numOfThreads = 1; } - memset(&rpcInit, 0, sizeof(rpcInit)); + SRpcInit rpcInit = {0}; rpcInit.localIp = tsAnyIp ? "0.0.0.0" : tsPrivateIp; rpcInit.localPort = tsMgmtShellPort; rpcInit.label = "MND-shell"; @@ -98,587 +68,450 @@ int32_t mgmtInitShell() { rpcInit.cfp = mgmtProcessMsgFromShell; rpcInit.sessions = tsMaxShellConns; rpcInit.connType = TAOS_CONN_SERVER; - rpcInit.idleTime = tsShellActivityTimer * 2000; - rpcInit.afp = mgmtRetriveUserAuthInfo; + rpcInit.idleTime = tsShellActivityTimer * 1000; + rpcInit.afp = mgmtShellRetriveAuth; - tsShellConnServer = rpcOpen(&rpcInit); - if (tsShellConnServer == NULL) { - mError("failed to init tcp connection to shell"); + tsMgmtShellRpc = rpcOpen(&rpcInit); + if (tsMgmtShellRpc == NULL) { + mError("failed to init server connection to shell"); return -1; } + mPrint("server connection to shell is opened"); return 0; } void mgmtCleanUpShell() { - if (tsShellConnServer) { - rpcClose(tsShellConnServer); - tsShellConnServer = NULL; + if (tsMgmtShellRpc) { + rpcClose(tsMgmtShellRpc); + tsMgmtShellRpc = NULL; + mPrint("server connection to shell is closed"); } } -void mgmtProcessTableMetaMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp; - rpcRsp.handle = rpcMsg->handle; - rpcRsp.pCont = NULL; - rpcRsp.contLen = 0; +void mgmtAddShellHandle(uint8_t showType, void (*fp)(SRpcMsg *rpcMsg)) { + tsMgmtProcessShellMsgFp[showType] = fp; +} - STableInfoMsg *pInfo = rpcMsg->pCont; - pInfo->createFlag = htons(pInfo->createFlag); +void mgmtAddShellShowMetaHandle(uint8_t showType, SShowMetaFp fp) { + tsMgmtShowMetaFp[showType] = fp; +} - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - mError("table:%s, failed to get table meta, invalid user", pInfo->tableId); - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } +void mgmtAddShellShowRetrieveHandle(uint8_t msgType, SShowRetrieveFp fp) { + tsMgmtShowRetrieveFp[msgType] = fp; +} - STableInfo *pTable = mgmtGetTable(pInfo->tableId); - if (pTable == NULL) { - if (pInfo->createFlag != 1) { - mError("table:%s, failed to get table meta, table not exist", pInfo->tableId); - rpcRsp.code = TSDB_CODE_INVALID_TABLE; - rpcSendResponse(&rpcRsp); - return; - } else { - // on demand create table from super table if table does not exists - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - mError("table:%s, failed to create table while get meta info, need redirect message", pInfo->tableId); - return; - } - - int32_t contLen = sizeof(SCreateTableMsg) + sizeof(STagData); - SCreateTableMsg *pCreateMsg = rpcMallocCont(contLen); - if (pCreateMsg == NULL) { - mError("table:%s, failed to create table while get meta info, no enough memory", pInfo->tableId); - rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; - rpcSendResponse(&rpcRsp); - return; - } - - memcpy(pCreateMsg->schema, pInfo->tags, sizeof(STagData)); - strcpy(pCreateMsg->tableId, pInfo->tableId); - - mError("table:%s, start to create table while get meta info", pInfo->tableId); - mgmtCreateTable(pCreateMsg, contLen, rpcMsg->handle, true); - } +static void mgmtProcessMsgFromShell(SRpcMsg *rpcMsg) { + if (tsMgmtProcessShellMsgFp[rpcMsg->msgType]) { + (*tsMgmtProcessShellMsgFp[rpcMsg->msgType])(rpcMsg); } else { - mgmtProcessGetTableMeta(pTable, rpcMsg->handle); + mError("%s is not processed", taosMsg[rpcMsg->msgType]); } + + rpcFreeCont(rpcMsg->pCont); } -void mgmtProcessMultiTableMetaMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp; - rpcRsp.handle = rpcMsg->handle; - rpcRsp.pCont = NULL; - rpcRsp.contLen = 0; - SRpcConnInfo connInfo; - rpcGetConnInfo(rpcMsg->handle, &connInfo); - - bool usePublicIp = (connInfo.serverIp == tsPublicIpInt); - SUserObj *pUser = mgmtGetUser(connInfo.user); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - SMultiTableInfoMsg *pInfo = rpcMsg->pCont; - pInfo->numOfTables = htonl(pInfo->numOfTables); - - int32_t totalMallocLen = 4*1024*1024; // first malloc 4 MB, subsequent reallocation as twice - SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen); - if (pMultiMeta == NULL) { - rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; - rpcSendResponse(&rpcRsp); - return; - } - - pMultiMeta->contLen = sizeof(SMultiTableMeta); - pMultiMeta->numOfTables = 0; - - for (int t = 0; t < pInfo->numOfTables; ++t) { - char *tableId = (char*)(pInfo->tableIds + t * TSDB_TABLE_ID_LEN); - STableInfo *pTable = mgmtGetTable(tableId); - if (pTable == NULL) continue; - - SDbObj *pDb = mgmtGetDbByTableId(tableId); - if (pDb == NULL) continue; - - int availLen = totalMallocLen - pMultiMeta->contLen; - if (availLen <= sizeof(STableMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS) { - //TODO realloc - //totalMallocLen *= 2; - //pMultiMeta = rpcReMalloc(pMultiMeta, totalMallocLen); - //if (pMultiMeta == NULL) { - /// rpcSendResponse(ahandle, TSDB_CODE_SERV_OUT_OF_MEMORY, NULL, 0); - // return TSDB_CODE_SERV_OUT_OF_MEMORY; - //} else { - // t--; - // continue; - //} - } - - STableMeta *pMeta = (STableMeta *)(pMultiMeta->metas + pMultiMeta->contLen); - int32_t code = mgmtGetTableMeta(pDb, pTable, pMeta, usePublicIp); - if (code == TSDB_CODE_SUCCESS) { - pMultiMeta->numOfTables ++; - pMultiMeta->contLen += pMeta->contLen; - } - } - - rpcRsp.pCont = pMultiMeta; - rpcRsp.contLen = pMultiMeta->contLen; - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessSuperTableMetaMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SSuperTableInfoMsg *pInfo = rpcMsg->pCont; - STableInfo *pTable = mgmtGetSuperTable(pInfo->tableId); - if (pTable == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_TABLE; - rpcSendResponse(&rpcRsp); - return; - } - - SSuperTableInfoRsp *pRsp = mgmtGetSuperTableVgroup((SSuperTableObj *) pTable); - if (pRsp != NULL) { - int32_t msgLen = sizeof(SSuperTableObj) + htonl(pRsp->numOfDnodes) * sizeof(int32_t); - rpcRsp.pCont = pRsp; - rpcRsp.contLen = msgLen; - rpcSendResponse(&rpcRsp); - } else { - rpcRsp.code = TSDB_CODE_INVALID_TABLE; - rpcSendResponse(&rpcRsp); - } -} - -void mgmtProcessCreateDbMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - SCreateDbMsg *pCreate = (SCreateDbMsg *) rpcMsg->pCont; - - pCreate->maxSessions = htonl(pCreate->maxSessions); - pCreate->cacheBlockSize = htonl(pCreate->cacheBlockSize); - pCreate->daysPerFile = htonl(pCreate->daysPerFile); - pCreate->daysToKeep = htonl(pCreate->daysToKeep); - pCreate->daysToKeep1 = htonl(pCreate->daysToKeep1); - pCreate->daysToKeep2 = htonl(pCreate->daysToKeep2); - pCreate->commitTime = htonl(pCreate->commitTime); - pCreate->blocksPerTable = htons(pCreate->blocksPerTable); - pCreate->rowsInFileBlock = htonl(pCreate->rowsInFileBlock); - // pCreate->cacheNumOfBlocks = htonl(pCreate->cacheNumOfBlocks); - - int32_t code; - if (mgmtCheckExpired()) { - code = TSDB_CODE_GRANT_EXPIRED; - } else if (!pUser->writeAuth) { - code = TSDB_CODE_NO_RIGHTS; - } else { - code = mgmtCreateDb(pUser->pAcct, pCreate); - if (code == TSDB_CODE_SUCCESS) { - mLPrint("DB:%s is created by %s", pCreate->db, pUser->user); - } - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessAlterDbMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - SAlterDbMsg *pAlter = (SAlterDbMsg *) rpcMsg->pCont; - pAlter->daysPerFile = htonl(pAlter->daysPerFile); - pAlter->daysToKeep = htonl(pAlter->daysToKeep); - pAlter->maxSessions = htonl(pAlter->maxSessions) + 1; - - int32_t code; - if (!pUser->writeAuth) { - code = TSDB_CODE_NO_RIGHTS; - } else { - code = mgmtAlterDb(pUser->pAcct, pAlter); - if (code == TSDB_CODE_SUCCESS) { - mLPrint("DB:%s is altered by %s", pAlter->db, pUser->user); - } - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessKillQueryMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - SKillQueryMsg *pKill = (SKillQueryMsg *) rpcMsg->pCont; - int32_t code; - - if (!pUser->writeAuth) { - code = TSDB_CODE_NO_RIGHTS; - } else { - code = mgmtKillQuery(pKill->queryId, rpcMsg->handle); - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessKillStreamMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - SKillStreamMsg *pKill = (SKillStreamMsg *) rpcMsg->pCont; - int32_t code; - - if (!pUser->writeAuth) { - code = TSDB_CODE_NO_RIGHTS; - } else { - code = mgmtKillStream(pKill->queryId, rpcMsg->handle); - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessKillConnectionMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - SKillConnectionMsg *pKill = (SKillConnectionMsg *) rpcMsg->pCont; - int32_t code; - - if (!pUser->writeAuth) { - code = TSDB_CODE_NO_RIGHTS; - } else { - code = mgmtKillConnection(pKill->queryId, rpcMsg->handle); - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessCreateUserMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - int32_t code; - if (pUser->superAuth) { - SCreateUserMsg *pCreate = rpcMsg->pCont; - code = mgmtCreateUser(pUser->pAcct, pCreate->user, pCreate->pass); - if (code == TSDB_CODE_SUCCESS) { - mLPrint("user:%s is created by %s", pCreate->user, pUser->user); - } - } else { - code = TSDB_CODE_NO_RIGHTS; - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessAlterUserMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return; - } - - SUserObj *pOperUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pOperUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - SAlterUserMsg *pAlter = rpcMsg->pCont; - SUserObj *pUser = mgmtGetUser(pAlter->user); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - if (strcmp(pUser->user, "monitor") == 0 || (strcmp(pUser->user + 1, pUser->acct) == 0 && pUser->user[0] == '_')) { - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - rpcSendResponse(&rpcRsp); - return; - } - - int code; - if ((pAlter->flag & TSDB_ALTER_USER_PASSWD) != 0) { - bool hasRight = false; - if (strcmp(pOperUser->user, "root") == 0) { - hasRight = true; - } else if (strcmp(pUser->user, pOperUser->user) == 0) { - hasRight = true; - } else if (pOperUser->superAuth) { - if (strcmp(pUser->user, "root") == 0) { - hasRight = false; - } else if (strcmp(pOperUser->acct, pUser->acct) != 0) { - hasRight = false; - } else { - hasRight = true; - } - } - - if (hasRight) { - memset(pUser->pass, 0, sizeof(pUser->pass)); - taosEncryptPass((uint8_t*)pAlter->pass, strlen(pAlter->pass), pUser->pass); - code = mgmtUpdateUser(pUser); - mLPrint("user:%s password is altered by %s, code:%d", pAlter->user, pUser->user, code); - } else { - code = TSDB_CODE_NO_RIGHTS; - } - - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); - return; - } - - if ((pAlter->flag & TSDB_ALTER_USER_PRIVILEGES) != 0) { - bool hasRight = false; - - if (strcmp(pUser->user, "root") == 0) { - hasRight = false; - } else if (strcmp(pUser->user, pUser->acct) == 0) { - hasRight = false; - } else if (strcmp(pOperUser->user, "root") == 0) { - hasRight = true; - } else if (strcmp(pUser->user, pOperUser->user) == 0) { - hasRight = false; - } else if (pOperUser->superAuth) { - if (strcmp(pUser->user, "root") == 0) { - hasRight = false; - } else if (strcmp(pOperUser->acct, pUser->acct) != 0) { - hasRight = false; - } else { - hasRight = true; - } - } - - if (pAlter->privilege == 1) { // super - hasRight = false; - } - - if (hasRight) { - //if (pAlter->privilege == 1) { // super - // pUser->superAuth = 1; - // pUser->writeAuth = 1; - //} - if (pAlter->privilege == 2) { // read - pUser->superAuth = 0; - pUser->writeAuth = 0; - } - if (pAlter->privilege == 3) { // write - pUser->superAuth = 0; - pUser->writeAuth = 1; - } - - code = mgmtUpdateUser(pUser); - mLPrint("user:%s privilege is altered by %s, code:%d", pAlter->user, pUser->user, code); - } else { - code = TSDB_CODE_NO_RIGHTS; - } - - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); - return; - } - - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessDropUserMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return ; - } - - SUserObj *pOperUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pOperUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return ; - } - - SDropUserMsg *pDrop = rpcMsg->pCont; - SUserObj *pUser = mgmtGetUser(pDrop->user); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return ; - } - - if (strcmp(pUser->user, "monitor") == 0 || (strcmp(pUser->user + 1, pUser->acct) == 0 && pUser->user[0] == '_')) { - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - rpcSendResponse(&rpcRsp); - return ; - } - - bool hasRight = false; - if (strcmp(pUser->user, "root") == 0) { - hasRight = false; - } else if (strcmp(pOperUser->user, "root") == 0) { - hasRight = true; - } else if (strcmp(pUser->user, pOperUser->user) == 0) { - hasRight = false; - } else if (pOperUser->superAuth) { - if (strcmp(pUser->user, "root") == 0) { - hasRight = false; - } else if (strcmp(pOperUser->acct, pUser->acct) != 0) { - hasRight = false; - } else { - hasRight = true; - } - } - - int32_t code; - if (hasRight) { - code = mgmtDropUser(pUser->pAcct, pDrop->user); - if (code == TSDB_CODE_SUCCESS) { - mLPrint("user:%s is dropped by %s", pDrop->user, pUser->user); - } - } else { - code = TSDB_CODE_NO_RIGHTS; - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessDropDbMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return ; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return ; - } - - int32_t code; - if (pUser->superAuth) { - SDropDbMsg *pDrop = rpcMsg->pCont; - code = mgmtDropDbByName(pUser->pAcct, pDrop->db, pDrop->ignoreNotExists); - if (code == TSDB_CODE_SUCCESS) { - mLPrint("DB:%s is dropped by %s", pDrop->db, pUser->user); - } - } else { - code = TSDB_CODE_NO_RIGHTS; - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -static void mgmtInitShowMsgFp() { - mgmtGetMetaFp[TSDB_MGMT_TABLE_ACCT] = mgmtGetAcctMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_USER] = mgmtGetUserMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_DB] = mgmtGetDbMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_TABLE] = mgmtGetShowTableMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_DNODE] = mgmtGetDnodeMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_MNODE] = mgmtGetMnodeMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_VGROUP] = mgmtGetVgroupMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_METRIC] = mgmtGetShowSuperTableMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_MODULE] = mgmtGetModuleMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_QUERIES] = mgmtGetQueryMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_STREAMS] = mgmtGetStreamMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_CONFIGS] = mgmtGetConfigMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_CONNS] = mgmtGetConnsMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_SCORES] = mgmtGetScoresMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_GRANTS] = mgmtGetGrantsMeta; - mgmtGetMetaFp[TSDB_MGMT_TABLE_VNODES] = mgmtGetVnodeMeta; - - mgmtRetrieveFp[TSDB_MGMT_TABLE_ACCT] = mgmtRetrieveAccts; - mgmtRetrieveFp[TSDB_MGMT_TABLE_USER] = mgmtRetrieveUsers; - mgmtRetrieveFp[TSDB_MGMT_TABLE_DB] = mgmtRetrieveDbs; - mgmtRetrieveFp[TSDB_MGMT_TABLE_TABLE] = mgmtRetrieveShowTables; - mgmtRetrieveFp[TSDB_MGMT_TABLE_DNODE] = mgmtRetrieveDnodes; - mgmtRetrieveFp[TSDB_MGMT_TABLE_MNODE] = mgmtRetrieveMnodes; - mgmtRetrieveFp[TSDB_MGMT_TABLE_VGROUP] = mgmtRetrieveVgroups; - mgmtRetrieveFp[TSDB_MGMT_TABLE_METRIC] = mgmtRetrieveShowSuperTables; - mgmtRetrieveFp[TSDB_MGMT_TABLE_MODULE] = mgmtRetrieveModules; - mgmtRetrieveFp[TSDB_MGMT_TABLE_QUERIES] = mgmtRetrieveQueries; - mgmtRetrieveFp[TSDB_MGMT_TABLE_STREAMS] = mgmtRetrieveStreams; - mgmtRetrieveFp[TSDB_MGMT_TABLE_CONFIGS] = mgmtRetrieveConfigs; - mgmtRetrieveFp[TSDB_MGMT_TABLE_CONNS] = mgmtRetrieveConns; - mgmtRetrieveFp[TSDB_MGMT_TABLE_SCORES] = mgmtRetrieveScores; - mgmtRetrieveFp[TSDB_MGMT_TABLE_GRANTS] = mgmtRetrieveGrants; - mgmtRetrieveFp[TSDB_MGMT_TABLE_VNODES] = mgmtRetrieveVnodes; -} - -void mgmtProcessShowMsg(SRpcMsg *rpcMsg) { +//static void mgmtInitShowMsgFp(); +//static void mgmtInitProcessShellMsg(); +//static void mgmtProcessMsgFromShell(SRpcMsg *msg); +//static void (*mgmtProcessShellMsg[TSDB_MSG_TYPE_MAX])(SRpcMsg *msg); +//static void mgmtProcessUnSupportMsg(SRpcMsg *msg); +// +//void *tsMgmtShellRpc = NULL; +// +//void mgmtProcessTranRequest(SSchedMsg *sched) { +// SRpcMsg rpcMsg; +// rpcMsg.msgType = *(int8_t *) (sched->msg); +// rpcMsg.contLen = *(int32_t *) (sched->msg + sizeof(int8_t)); +// rpcMsg.pCont = sched->msg + sizeof(int32_t) + sizeof(int8_t); +// rpcMsg.handle = sched->thandle; +// rpcMsg.code = TSDB_CODE_SUCCESS; +// +// (*mgmtProcessShellMsg[rpcMsg.msgType])(&rpcMsg); +// if (sched->msg) { +// free(sched->msg); +// } +//} +// +//void mgmtAddToTranRequest(SRpcMsg *rpcMsg) { +// SSchedMsg schedMsg; +// schedMsg.msg = malloc(rpcMsg->contLen + sizeof(int32_t) + sizeof(int8_t)); +// schedMsg.fp = mgmtProcessTranRequest; +// schedMsg.tfp = NULL; +// schedMsg.thandle = rpcMsg->handle; +// *(int8_t *) (schedMsg.msg) = rpcMsg->msgType; +// *(int32_t *) (schedMsg.msg + sizeof(int8_t)) = rpcMsg->contLen; +// memcpy(schedMsg.msg + sizeof(int32_t) + sizeof(int8_t), rpcMsg->pCont, rpcMsg->contLen); +// +// taosScheduleTask(tsMgmtTranQhandle, &schedMsg); +//} +// + +// +//void mgmtProcessTableMetaMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp; +// rpcRsp.handle = rpcMsg->handle; +// rpcRsp.pCont = NULL; +// rpcRsp.contLen = 0; +// +// STableInfoMsg *pInfo = rpcMsg->pCont; +// pInfo->createFlag = htons(pInfo->createFlag); +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// mError("table:%s, failed to get table meta, invalid user", pInfo->tableId); +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// STableInfo *pTable = mgmtGetTable(pInfo->tableId); +// if (pTable == NULL) { +// if (pInfo->createFlag != 1) { +// mError("table:%s, failed to get table meta, table not exist", pInfo->tableId); +// rpcRsp.code = TSDB_CODE_INVALID_TABLE; +// rpcSendResponse(&rpcRsp); +// return; +// } else { +// // on demand create table from super table if table does not exists +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// mError("table:%s, failed to create table while get meta info, need redirect message", pInfo->tableId); +// return; +// } +// +// int32_t contLen = sizeof(SCreateTableMsg) + sizeof(STagData); +// SCreateTableMsg *pCreateMsg = rpcMallocCont(contLen); +// if (pCreateMsg == NULL) { +// mError("table:%s, failed to create table while get meta info, no enough memory", pInfo->tableId); +// rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// memcpy(pCreateMsg->schema, pInfo->tags, sizeof(STagData)); +// strcpy(pCreateMsg->tableId, pInfo->tableId); +// +// mError("table:%s, start to create table while get meta info", pInfo->tableId); +// mgmtCreateTable(pCreateMsg, contLen, rpcMsg->handle, true); +// } +// } else { +// mgmtProcessGetTableMeta(pTable, rpcMsg->handle); +// } +//} +// +//void mgmtProcessMultiTableMetaMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp; +// rpcRsp.handle = rpcMsg->handle; +// rpcRsp.pCont = NULL; +// rpcRsp.contLen = 0; +// +// SRpcConnInfo connInfo; +// rpcGetConnInfo(rpcMsg->handle, &connInfo); +// +// bool usePublicIp = (connInfo.serverIp == tsPublicIpInt); +// SUserObj *pUser = mgmtGetUser(connInfo.user); +// if (pUser == NULL) { +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SMultiTableInfoMsg *pInfo = rpcMsg->pCont; +// pInfo->numOfTables = htonl(pInfo->numOfTables); +// +// int32_t totalMallocLen = 4*1024*1024; // first malloc 4 MB, subsequent reallocation as twice +// SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen); +// if (pMultiMeta == NULL) { +// rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// pMultiMeta->contLen = sizeof(SMultiTableMeta); +// pMultiMeta->numOfTables = 0; +// +// for (int t = 0; t < pInfo->numOfTables; ++t) { +// char *tableId = (char*)(pInfo->tableIds + t * TSDB_TABLE_ID_LEN); +// STableInfo *pTable = mgmtGetTable(tableId); +// if (pTable == NULL) continue; +// +// SDbObj *pDb = mgmtGetDbByTableId(tableId); +// if (pDb == NULL) continue; +// +// int availLen = totalMallocLen - pMultiMeta->contLen; +// if (availLen <= sizeof(STableMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS) { +// //TODO realloc +// //totalMallocLen *= 2; +// //pMultiMeta = rpcReMalloc(pMultiMeta, totalMallocLen); +// //if (pMultiMeta == NULL) { +// /// rpcSendResponse(ahandle, TSDB_CODE_SERV_OUT_OF_MEMORY, NULL, 0); +// // return TSDB_CODE_SERV_OUT_OF_MEMORY; +// //} else { +// // t--; +// // continue; +// //} +// } +// +// STableMeta *pMeta = (STableMeta *)(pMultiMeta->metas + pMultiMeta->contLen); +// int32_t code = mgmtGetTableMeta(pDb, pTable, pMeta, usePublicIp); +// if (code == TSDB_CODE_SUCCESS) { +// pMultiMeta->numOfTables ++; +// pMultiMeta->contLen += pMeta->contLen; +// } +// } +// +// rpcRsp.pCont = pMultiMeta; +// rpcRsp.contLen = pMultiMeta->contLen; +// rpcSendResponse(&rpcRsp); +//} +// +//void mgmtProcessSuperTableMetaMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// SSuperTableInfoMsg *pInfo = rpcMsg->pCont; +// STableInfo *pTable = mgmtGetSuperTable(pInfo->tableId); +// if (pTable == NULL) { +// rpcRsp.code = TSDB_CODE_INVALID_TABLE; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SSuperTableInfoRsp *pRsp = mgmtGetSuperTableVgroup((SSuperTableObj *) pTable); +// if (pRsp != NULL) { +// int32_t msgLen = sizeof(SSuperTableObj) + htonl(pRsp->numOfDnodes) * sizeof(int32_t); +// rpcRsp.pCont = pRsp; +// rpcRsp.contLen = msgLen; +// rpcSendResponse(&rpcRsp); +// } else { +// rpcRsp.code = TSDB_CODE_INVALID_TABLE; +// rpcSendResponse(&rpcRsp); +// } +//} +// +//void mgmtProcessCreateDbMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SCreateDbMsg *pCreate = (SCreateDbMsg *) rpcMsg->pCont; +// +// pCreate->maxSessions = htonl(pCreate->maxSessions); +// pCreate->cacheBlockSize = htonl(pCreate->cacheBlockSize); +// pCreate->daysPerFile = htonl(pCreate->daysPerFile); +// pCreate->daysToKeep = htonl(pCreate->daysToKeep); +// pCreate->daysToKeep1 = htonl(pCreate->daysToKeep1); +// pCreate->daysToKeep2 = htonl(pCreate->daysToKeep2); +// pCreate->commitTime = htonl(pCreate->commitTime); +// pCreate->blocksPerTable = htons(pCreate->blocksPerTable); +// pCreate->rowsInFileBlock = htonl(pCreate->rowsInFileBlock); +// // pCreate->cacheNumOfBlocks = htonl(pCreate->cacheNumOfBlocks); +// +// int32_t code; +// if (mgmtCheckExpired()) { +// code = TSDB_CODE_GRANT_EXPIRED; +// } else if (!pUser->writeAuth) { +// code = TSDB_CODE_NO_RIGHTS; +// } else { +// code = mgmtCreateDb(pUser->pAcct, pCreate); +// if (code == TSDB_CODE_SUCCESS) { +// mLPrint("DB:%s is created by %s", pCreate->db, pUser->user); +// } +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// +//void mgmtProcessAlterDbMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SAlterDbMsg *pAlter = (SAlterDbMsg *) rpcMsg->pCont; +// pAlter->daysPerFile = htonl(pAlter->daysPerFile); +// pAlter->daysToKeep = htonl(pAlter->daysToKeep); +// pAlter->maxSessions = htonl(pAlter->maxSessions) + 1; +// +// int32_t code; +// if (!pUser->writeAuth) { +// code = TSDB_CODE_NO_RIGHTS; +// } else { +// code = mgmtAlterDb(pUser->pAcct, pAlter); +// if (code == TSDB_CODE_SUCCESS) { +// mLPrint("DB:%s is altered by %s", pAlter->db, pUser->user); +// } +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// +//void mgmtProcessKillQueryMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SKillQueryMsg *pKill = (SKillQueryMsg *) rpcMsg->pCont; +// int32_t code; +// +// if (!pUser->writeAuth) { +// code = TSDB_CODE_NO_RIGHTS; +// } else { +// code = mgmtKillQuery(pKill->queryId, rpcMsg->handle); +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// +//void mgmtProcessKillStreamMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SKillStreamMsg *pKill = (SKillStreamMsg *) rpcMsg->pCont; +// int32_t code; +// +// if (!pUser->writeAuth) { +// code = TSDB_CODE_NO_RIGHTS; +// } else { +// code = mgmtKillStream(pKill->queryId, rpcMsg->handle); +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// +//void mgmtProcessKillConnectionMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SKillConnectionMsg *pKill = (SKillConnectionMsg *) rpcMsg->pCont; +// int32_t code; +// +// if (!pUser->writeAuth) { +// code = TSDB_CODE_NO_RIGHTS; +// } else { +// code = mgmtKillConnection(pKill->queryId, rpcMsg->handle); +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// + +//void mgmtProcessDropDbMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// return ; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return ; +// } +// +// int32_t code; +// if (pUser->superAuth) { +// SDropDbMsg *pDrop = rpcMsg->pCont; +// code = mgmtDropDbByName(pUser->pAcct, pDrop->db, pDrop->ignoreNotExists); +// if (code == TSDB_CODE_SUCCESS) { +// mLPrint("DB:%s is dropped by %s", pDrop->db, pUser->user); +// } +// } else { +// code = TSDB_CODE_NO_RIGHTS; +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// +//static void mgmtInitShowMsgFp() { +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_ACCT] = mgmtGetAcctMeta; +// tsMgmtShowMetaFp[] = ; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_DB] = mgmtGetDbMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_TABLE] = mgmtGetShowTableMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_DNODE] = mgmtGetDnodeMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_MNODE] = mgmtGetMnodeMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_VGROUP] = mgmtGetVgroupMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_METRIC] = mgmtGetShowSuperTableMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_MODULE] = mgmtGetModuleMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_QUERIES] = mgmtGetQueryMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_STREAMS] = mgmtGetStreamMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_CONFIGS] = mgmtGetConfigMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_CONNS] = mgmtGetConnsMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_SCORES] = mgmtGetScoresMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_GRANTS] = mgmtGetGrantsMeta; +// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_VNODES] = mgmtGetVnodeMeta; +// +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_ACCT] = mgmtRetrieveAccts; +// tsMgmtShowRetrieveFp[] = ; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_DB] = mgmtRetrieveDbs; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_TABLE] = mgmtRetrieveShowTables; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_DNODE] = mgmtRetrieveDnodes; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_MNODE] = mgmtRetrieveMnodes; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_VGROUP] = mgmtRetrieveVgroups; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_METRIC] = mgmtRetrieveShowSuperTables; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_MODULE] = mgmtRetrieveModules; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_QUERIES] = mgmtRetrieveQueries; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_STREAMS] = mgmtRetrieveStreams; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_CONFIGS] = mgmtRetrieveConfigs; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_CONNS] = mgmtRetrieveConns; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_SCORES] = mgmtRetrieveScores; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_GRANTS] = mgmtRetrieveGrants; +// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_VNODES] = mgmtRetrieveVnodes; +//} + +static void mgmtProcessShowMsg(SRpcMsg *rpcMsg) { SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; SShowMsg *pShowMsg = rpcMsg->pCont; if (pShowMsg->type == TSDB_MGMT_TABLE_DNODE || TSDB_MGMT_TABLE_GRANTS || TSDB_MGMT_TABLE_SCORES) { - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { + if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { return; } } @@ -707,7 +540,7 @@ void mgmtProcessShowMsg(SRpcMsg *rpcMsg) { mgmtSaveQhandle(pShow); pShowRsp->qhandle = htobe64((uint64_t) pShow); - code = (*mgmtGetMetaFp[(uint8_t) pShowMsg->type])(&pShowRsp->tableMeta, pShow, rpcMsg->handle); + code = (*tsMgmtShowMetaFp[(uint8_t) pShowMsg->type])(&pShowRsp->tableMeta, pShow, rpcMsg->handle); if (code == 0) { size = sizeof(SShowRsp) + sizeof(SSchema) * pShow->numOfColumns; } else { @@ -722,7 +555,7 @@ void mgmtProcessShowMsg(SRpcMsg *rpcMsg) { rpcSendResponse(&rpcRsp); } -void mgmtProcessRetrieveMsg(SRpcMsg *rpcMsg) { +static void mgmtProcessRetrieveMsg(SRpcMsg *rpcMsg) { SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; int32_t rowsToRead = 0; @@ -769,7 +602,7 @@ void mgmtProcessRetrieveMsg(SRpcMsg *rpcMsg) { // if free flag is set, client wants to clean the resources if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) - rowsRead = (*mgmtRetrieveFp[(uint8_t) pShow->type])(pShow, pRsp->data, rowsToRead, rpcMsg->handle); + rowsRead = (*tsMgmtShowRetrieveFp[(uint8_t) pShow->type])(pShow, pRsp->data, rowsToRead, rpcMsg->handle); if (rowsRead < 0) { rowsRead = 0; // TSDB_CODE_ACTION_IN_PROGRESS; @@ -788,205 +621,205 @@ void mgmtProcessRetrieveMsg(SRpcMsg *rpcMsg) { mgmtFreeQhandle(pShow); } } +// +//void mgmtProcessCreateTableMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// +// SCreateTableMsg *pCreate = (SCreateTableMsg *) rpcMsg->pCont; +// pCreate->numOfColumns = htons(pCreate->numOfColumns); +// pCreate->numOfTags = htons(pCreate->numOfTags); +// pCreate->sqlLen = htons(pCreate->sqlLen); +// +// SSchema *pSchema = (SSchema*) pCreate->schema; +// for (int32_t i = 0; i < pCreate->numOfColumns + pCreate->numOfTags; ++i) { +// pSchema->bytes = htons(pSchema->bytes); +// pSchema->colId = i; +// pSchema++; +// } +// +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// mError("table:%s, failed to create table, need redirect message", pCreate->tableId); +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// mError("table:%s, failed to create table, invalid user", pCreate->tableId); +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// if (!pUser->writeAuth) { +// mError("table:%s, failed to create table, no rights", pCreate->tableId); +// rpcRsp.code = TSDB_CODE_NO_RIGHTS; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// int32_t code = mgmtCreateTable(pCreate, rpcMsg->contLen, rpcMsg->handle, false); +// if (code != TSDB_CODE_ACTION_IN_PROGRESS) { +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +// } +//} +// +//void mgmtProcessDropTableMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// SDropTableMsg *pDrop = (SDropTableMsg *) rpcMsg->pCont; +// +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// mError("table:%s, failed to drop table, need redirect message", pDrop->tableId); +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// mError("table:%s, failed to drop table, invalid user", pDrop->tableId); +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// if (!pUser->writeAuth) { +// mError("table:%s, failed to drop table, no rights", pDrop->tableId); +// rpcRsp.code = TSDB_CODE_NO_RIGHTS; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SDbObj *pDb = mgmtGetDbByTableId(pDrop->tableId); +// if (pDb == NULL) { +// mError("table:%s, failed to drop table, db not selected", pDrop->tableId); +// rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// int32_t code = mgmtDropTable(pDb, pDrop->tableId, pDrop->igNotExists); +// if (code != TSDB_CODE_ACTION_IN_PROGRESS) { +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +// } +//} +// +//void mgmtProcessAlterTableMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SAlterTableMsg *pAlter = (SAlterTableMsg *) rpcMsg->pCont; +// +// if (!pUser->writeAuth) { +// rpcRsp.code = TSDB_CODE_NO_RIGHTS; +// } else { +// pAlter->type = htons(pAlter->type); +// pAlter->numOfCols = htons(pAlter->numOfCols); +// +// if (pAlter->numOfCols > 2) { +// mError("table:%s error numOfCols:%d in alter table", pAlter->tableId, pAlter->numOfCols); +// rpcRsp.code = TSDB_CODE_APP_ERROR; +// } else { +// SDbObj *pDb = mgmtGetDb(pAlter->db); +// if (pDb) { +// for (int32_t i = 0; i < pAlter->numOfCols; ++i) { +// pAlter->schema[i].bytes = htons(pAlter->schema[i].bytes); +// } +// +// rpcRsp.code = mgmtAlterTable(pDb, pAlter); +// if (rpcRsp.code == 0) { +// mLPrint("table:%s is altered by %s", pAlter->tableId, pUser->user); +// } +// } else { +// rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; +// } +// } +// } +// +// rpcSendResponse(&rpcRsp); +//} +// +//void mgmtProcessCfgDnodeMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SCfgDnodeMsg *pCfg = (SCfgDnodeMsg *) rpcMsg->pCont; +// +// if (strcmp(pUser->pAcct->user, "root") != 0) { +// rpcRsp.code = TSDB_CODE_NO_RIGHTS; +// } else { +// rpcRsp.code = mgmtSendCfgDnodeMsg(rpcMsg->pCont); +// } +// +// if (rpcRsp.code == TSDB_CODE_SUCCESS) { +// mTrace("dnode:%s is configured by %s", pCfg->ip, pUser->user); +// } +// +// rpcSendResponse(&rpcRsp); +//} +// +//void mgmtProcessHeartBeatMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// SHeartBeatMsg *pHBMsg = (SHeartBeatMsg *) rpcMsg->pCont; +// mgmtSaveQueryStreamList(pHBMsg); +// +// SHeartBeatRsp *pHBRsp = (SHeartBeatRsp *) rpcMallocCont(sizeof(SHeartBeatRsp)); +// if (pHBRsp == NULL) { +// rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SRpcConnInfo connInfo; +// rpcGetConnInfo(rpcMsg->handle, &connInfo); +// +// pHBRsp->ipList.inUse = 0; +// pHBRsp->ipList.port = htons(tsMgmtShellPort); +// pHBRsp->ipList.numOfIps = 0; +// if (pSdbPublicIpList != NULL && pSdbIpList != NULL) { +// pHBRsp->ipList.numOfIps = htons(pSdbPublicIpList->numOfIps); +// if (connInfo.serverIp == tsPublicIpInt) { +// for (int i = 0; i < pSdbPublicIpList->numOfIps; ++i) { +// pHBRsp->ipList.ip[i] = htonl(pSdbPublicIpList->ip[i]); +// } +// } else { +// for (int i = 0; i < pSdbIpList->numOfIps; ++i) { +// pHBRsp->ipList.ip[i] = htonl(pSdbIpList->ip[i]); +// } +// } +// } +// +// /* +// * TODO +// * Dispose kill stream or kill query message +// */ +// pHBRsp->queryId = 0; +// pHBRsp->streamId = 0; +// pHBRsp->killConnection = 0; +// +// rpcRsp.pCont = pHBRsp; +// rpcRsp.contLen = sizeof(SHeartBeatRsp); +// rpcSendResponse(&rpcRsp); +//} -void mgmtProcessCreateTableMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - - SCreateTableMsg *pCreate = (SCreateTableMsg *) rpcMsg->pCont; - pCreate->numOfColumns = htons(pCreate->numOfColumns); - pCreate->numOfTags = htons(pCreate->numOfTags); - pCreate->sqlLen = htons(pCreate->sqlLen); - - SSchema *pSchema = (SSchema*) pCreate->schema; - for (int32_t i = 0; i < pCreate->numOfColumns + pCreate->numOfTags; ++i) { - pSchema->bytes = htons(pSchema->bytes); - pSchema->colId = i; - pSchema++; - } - - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - mError("table:%s, failed to create table, need redirect message", pCreate->tableId); - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - mError("table:%s, failed to create table, invalid user", pCreate->tableId); - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - if (!pUser->writeAuth) { - mError("table:%s, failed to create table, no rights", pCreate->tableId); - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - rpcSendResponse(&rpcRsp); - return; - } - - int32_t code = mgmtCreateTable(pCreate, rpcMsg->contLen, rpcMsg->handle, false); - if (code != TSDB_CODE_ACTION_IN_PROGRESS) { - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); - } -} - -void mgmtProcessDropTableMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SDropTableMsg *pDrop = (SDropTableMsg *) rpcMsg->pCont; - - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - mError("table:%s, failed to drop table, need redirect message", pDrop->tableId); - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - mError("table:%s, failed to drop table, invalid user", pDrop->tableId); - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - if (!pUser->writeAuth) { - mError("table:%s, failed to drop table, no rights", pDrop->tableId); - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - rpcSendResponse(&rpcRsp); - return; - } - - SDbObj *pDb = mgmtGetDbByTableId(pDrop->tableId); - if (pDb == NULL) { - mError("table:%s, failed to drop table, db not selected", pDrop->tableId); - rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; - rpcSendResponse(&rpcRsp); - return; - } - - int32_t code = mgmtDropTable(pDb, pDrop->tableId, pDrop->igNotExists); - if (code != TSDB_CODE_ACTION_IN_PROGRESS) { - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); - } -} - -void mgmtProcessAlterTableMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - SAlterTableMsg *pAlter = (SAlterTableMsg *) rpcMsg->pCont; - - if (!pUser->writeAuth) { - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - } else { - pAlter->type = htons(pAlter->type); - pAlter->numOfCols = htons(pAlter->numOfCols); - - if (pAlter->numOfCols > 2) { - mError("table:%s error numOfCols:%d in alter table", pAlter->tableId, pAlter->numOfCols); - rpcRsp.code = TSDB_CODE_APP_ERROR; - } else { - SDbObj *pDb = mgmtGetDb(pAlter->db); - if (pDb) { - for (int32_t i = 0; i < pAlter->numOfCols; ++i) { - pAlter->schema[i].bytes = htons(pAlter->schema[i].bytes); - } - - rpcRsp.code = mgmtAlterTable(pDb, pAlter); - if (rpcRsp.code == 0) { - mLPrint("table:%s is altered by %s", pAlter->tableId, pUser->user); - } - } else { - rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; - } - } - } - - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessCfgDnodeMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - SCfgDnodeMsg *pCfg = (SCfgDnodeMsg *) rpcMsg->pCont; - - if (strcmp(pUser->pAcct->user, "root") != 0) { - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - } else { - rpcRsp.code = mgmtSendCfgDnodeMsg(rpcMsg->pCont); - } - - if (rpcRsp.code == TSDB_CODE_SUCCESS) { - mTrace("dnode:%s is configured by %s", pCfg->ip, pUser->user); - } - - rpcSendResponse(&rpcRsp); -} - -void mgmtProcessHeartBeatMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SHeartBeatMsg *pHBMsg = (SHeartBeatMsg *) rpcMsg->pCont; - mgmtSaveQueryStreamList(pHBMsg); - - SHeartBeatRsp *pHBRsp = (SHeartBeatRsp *) rpcMallocCont(sizeof(SHeartBeatRsp)); - if (pHBRsp == NULL) { - rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; - rpcSendResponse(&rpcRsp); - return; - } - - SRpcConnInfo connInfo; - rpcGetConnInfo(rpcMsg->handle, &connInfo); - - pHBRsp->ipList.inUse = 0; - pHBRsp->ipList.port = htons(tsMgmtShellPort); - pHBRsp->ipList.numOfIps = 0; - if (pSdbPublicIpList != NULL && pSdbIpList != NULL) { - pHBRsp->ipList.numOfIps = htons(pSdbPublicIpList->numOfIps); - if (connInfo.serverIp == tsPublicIpInt) { - for (int i = 0; i < pSdbPublicIpList->numOfIps; ++i) { - pHBRsp->ipList.ip[i] = htonl(pSdbPublicIpList->ip[i]); - } - } else { - for (int i = 0; i < pSdbIpList->numOfIps; ++i) { - pHBRsp->ipList.ip[i] = htonl(pSdbIpList->ip[i]); - } - } - } - - /* - * TODO - * Dispose kill stream or kill query message - */ - pHBRsp->queryId = 0; - pHBRsp->streamId = 0; - pHBRsp->killConnection = 0; - - rpcRsp.pCont = pHBRsp; - rpcRsp.contLen = sizeof(SHeartBeatRsp); - rpcSendResponse(&rpcRsp); -} - -int mgmtRetriveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey) { +static int mgmtShellRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey) { *spi = 0; *encrypt = 0; *ckey = 0; @@ -1001,514 +834,509 @@ int mgmtRetriveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, } } -static void mgmtProcessConnectMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SConnectMsg *pConnectMsg = (SConnectMsg *) rpcMsg->pCont; - - SRpcConnInfo connInfo; - rpcGetConnInfo(rpcMsg->handle, &connInfo); - int32_t code; - - SUserObj *pUser = mgmtGetUser(connInfo.user); - if (pUser == NULL) { - code = TSDB_CODE_INVALID_USER; - goto connect_over; - } - - if (mgmtCheckExpired()) { - code = TSDB_CODE_GRANT_EXPIRED; - goto connect_over; - } - - SAcctObj *pAcct = mgmtGetAcct(pUser->acct); - if (pAcct == NULL) { - code = TSDB_CODE_INVALID_ACCT; - goto connect_over; - } - - code = taosCheckVersion(pConnectMsg->clientVersion, version, 3); - if (code != TSDB_CODE_SUCCESS) { - goto connect_over; - } - - if (pConnectMsg->db[0]) { - char dbName[TSDB_TABLE_ID_LEN] = {0}; - sprintf(dbName, "%x%s%s", pAcct->acctId, TS_PATH_DELIMITER, pConnectMsg->db); - SDbObj *pDb = mgmtGetDb(dbName); - if (pDb == NULL) { - code = TSDB_CODE_INVALID_DB; - goto connect_over; - } - } - - SConnectRsp *pConnectRsp = rpcMallocCont(sizeof(SConnectRsp)); - if (pConnectRsp == NULL) { - code = TSDB_CODE_SERV_OUT_OF_MEMORY; - goto connect_over; - } - - sprintf(pConnectRsp->acctId, "%x", pAcct->acctId); - strcpy(pConnectRsp->serverVersion, version); - pConnectRsp->writeAuth = pUser->writeAuth; - pConnectRsp->superAuth = pUser->superAuth; - pConnectRsp->ipList.inUse = 0; - pConnectRsp->ipList.port = htons(tsMgmtShellPort); - pConnectRsp->ipList.numOfIps = 0; - if (pSdbPublicIpList != NULL && pSdbIpList != NULL) { - pConnectRsp->ipList.numOfIps = htons(pSdbPublicIpList->numOfIps); - if (connInfo.serverIp == tsPublicIpInt) { - for (int i = 0; i < pSdbPublicIpList->numOfIps; ++i) { - pConnectRsp->ipList.ip[i] = htonl(pSdbPublicIpList->ip[i]); - } - } else { - for (int i = 0; i < pSdbIpList->numOfIps; ++i) { - pConnectRsp->ipList.ip[i] = htonl(pSdbIpList->ip[i]); - } - } - } - -connect_over: - rpcRsp.code = code; - if (code != TSDB_CODE_SUCCESS) { - mLError("user:%s login from %s, code:%d", connInfo.user, taosIpStr(connInfo.clientIp), code); - } else { - mLPrint("user:%s login from %s, code:%d", connInfo.user, taosIpStr(connInfo.clientIp), code); - rpcRsp.pCont = pConnectRsp; - rpcRsp.contLen = sizeof(SConnectRsp); - } - rpcSendResponse(&rpcRsp); -} - -/** - * check if we need to add mgmtProcessTableMetaMsg into tranQueue, which will be executed one-by-one. - */ -static bool mgmtCheckMeterMetaMsgType(void *pMsg) { - STableInfoMsg *pInfo = (STableInfoMsg *) pMsg; - int16_t autoCreate = htons(pInfo->createFlag); - STableInfo *pTable = mgmtGetTable(pInfo->tableId); - - // If table does not exists and autoCreate flag is set, we add the handler into task queue - bool addIntoTranQueue = (pTable == NULL && autoCreate == 1); - if (addIntoTranQueue) { - mTrace("table:%s auto created task added", pInfo->tableId); - } - - return addIntoTranQueue; -} - -static bool mgmtCheckMsgReadOnly(int8_t type, void *pCont) { - if ((type == TSDB_MSG_TYPE_TABLE_META && (!mgmtCheckMeterMetaMsgType(pCont))) || - type == TSDB_MSG_TYPE_STABLE_META || type == TSDB_MSG_TYPE_RETRIEVE || - type == TSDB_MSG_TYPE_SHOW || type == TSDB_MSG_TYPE_MULTI_TABLE_META || - type == TSDB_MSG_TYPE_CONNECT) { - return true; - } - - return false; -} - -static void mgmtProcessMsgFromShell(SRpcMsg *rpcMsg) { - if (sdbGetRunStatus() != SDB_STATUS_SERVING) { - mTrace("shell msg is ignored since SDB is not ready"); - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = TSDB_CODE_NOT_READY, .msgType = 0}; - rpcSendResponse(&rpcRsp); - rpcFreeCont(rpcMsg->pCont); - return; - } - - if (mgmtCheckMsgReadOnly(rpcMsg->msgType, rpcMsg->pCont)) { - (*mgmtProcessShellMsg[rpcMsg->msgType])(rpcMsg); - } else { - if (mgmtProcessShellMsg[rpcMsg->msgType]) { - mgmtAddToTranRequest(rpcMsg); - } else { - mError("%s from shell is not processed", taosMsg[rpcMsg->msgType]); - } - } - - rpcFreeCont(rpcMsg->pCont); -} - -void mgmtProcessCreateVgroup(SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta) { - SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SDbObj *pDb = mgmtGetDb(pCreate->db); - if (pDb == NULL) { - mError("table:%s, failed to create vgroup, db not found", pCreate->tableId); - rpcRsp.code = TSDB_CODE_INVALID_DB; - rpcSendResponse(&rpcRsp); - return; - } - - SVgObj *pVgroup = mgmtCreateVgroup(pDb); - if (pVgroup == NULL) { - mError("table:%s, failed to alloc vnode to vgroup", pCreate->tableId); - rpcRsp.code = TSDB_CODE_NO_ENOUGH_DNODES; - rpcSendResponse(&rpcRsp); - return; - } - - void *cont = rpcMallocCont(contLen); - if (cont == NULL) { - mError("table:%s, failed to create table, can not alloc memory", pCreate->tableId); - rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; - rpcSendResponse(&rpcRsp); - return; - } - - memcpy(cont, pCreate, contLen); - - SProcessInfo *info = calloc(1, sizeof(SProcessInfo)); - info->type = TSDB_PROCESS_CREATE_VGROUP; - info->thandle = thandle; - info->ahandle = pVgroup; - info->cont = cont; - info->contLen = contLen; - - if (isGetMeta) { - info->type = TSDB_PROCESS_CREATE_VGROUP_GET_META; - } - - mgmtSendCreateVgroupMsg(pVgroup, info); -} - -void mgmtProcessCreateTable(SVgObj *pVgroup, SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta) { - assert(pVgroup != NULL); - SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - - int32_t sid = taosAllocateId(pVgroup->idPool); - if (sid < 0) { - mTrace("table:%s, no enough sid in vgroup:%d, start to create a new vgroup", pCreate->tableId, pVgroup->vgId); - mgmtProcessCreateVgroup(pCreate, contLen, thandle, isGetMeta); - return; - } - - STableInfo *pTable; - SDCreateTableMsg *pDCreate = NULL; - - if (pCreate->numOfColumns == 0) { - mTrace("table:%s, start to create child table, vgroup:%d sid:%d", pCreate->tableId, pVgroup->vgId, sid); - rpcRsp.code = mgmtCreateChildTable(pCreate, contLen, pVgroup, sid, &pDCreate, &pTable); - } else { - mTrace("table:%s, start to create normal table, vgroup:%d sid:%d", pCreate->tableId, pVgroup->vgId, sid); - rpcRsp.code = mgmtCreateNormalTable(pCreate, contLen, pVgroup, sid, &pDCreate, &pTable); - } - - if (rpcRsp.code != TSDB_CODE_SUCCESS) { - mTrace("table:%s, failed to create table in vgroup:%d sid:%d ", pCreate->tableId, pVgroup->vgId, sid); - rpcSendResponse(&rpcRsp); - return; - } - - assert(pDCreate != NULL); - assert(pTable != NULL); - - SProcessInfo *info = calloc(1, sizeof(SProcessInfo)); - info->type = TSDB_PROCESS_CREATE_TABLE; - info->thandle = thandle; - info->ahandle = pTable; - SRpcIpSet ipSet = mgmtGetIpSetFromVgroup(pVgroup); - if (isGetMeta) { - info->type = TSDB_PROCESS_CREATE_TABLE_GET_META; - } - - mgmtSendCreateTableMsg(pDCreate, &ipSet, info); -} - -void mgmtProcessGetTableMeta(STableInfo *pTable, void *thandle) { - SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - SDbObj* pDb = mgmtGetDbByTableId(pTable->tableId); - if (pDb == NULL || pDb->dropStatus != TSDB_DB_STATUS_READY) { - mError("table:%s, failed to get table meta, db not selected", pTable->tableId); - rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; - rpcSendResponse(&rpcRsp); - return; - } - - SRpcConnInfo connInfo; - rpcGetConnInfo(thandle, &connInfo); - bool usePublicIp = (connInfo.serverIp == tsPublicIpInt); - - STableMeta *pMeta = rpcMallocCont(sizeof(STableMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS); - rpcRsp.code = mgmtGetTableMeta(pDb, pTable, pMeta, usePublicIp); - - if (rpcRsp.code != TSDB_CODE_SUCCESS) { - rpcFreeCont(pMeta); - } else { - pMeta->contLen = htons(pMeta->contLen); - rpcRsp.pCont = pMeta; - rpcRsp.contLen = pMeta->contLen; - } - - rpcSendResponse(&rpcRsp); -} - -static int32_t mgmtCheckRedirectMsgImp(void *pConn) { - return 0; -} - -int32_t (*mgmtCheckRedirectMsg)(void *pConn) = mgmtCheckRedirectMsgImp; - -static void mgmtProcessUnSupportMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = { - .msgType = 0, - .pCont = 0, - .contLen = 0, - .code = TSDB_CODE_OPS_NOT_SUPPORT, - .handle = rpcMsg->handle - }; - rpcSendResponse(&rpcRsp); -} - -static void mgmtProcessAlterAcctMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (!mgmtAlterAcctFp) { - rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; - rpcSendResponse(&rpcRsp); - return; - } - - SAlterAcctMsg *pAlter = rpcMsg->pCont; - pAlter->cfg.maxUsers = htonl(pAlter->cfg.maxUsers); - pAlter->cfg.maxDbs = htonl(pAlter->cfg.maxDbs); - pAlter->cfg.maxTimeSeries = htonl(pAlter->cfg.maxTimeSeries); - pAlter->cfg.maxConnections = htonl(pAlter->cfg.maxConnections); - pAlter->cfg.maxStreams = htonl(pAlter->cfg.maxStreams); - pAlter->cfg.maxPointsPerSecond = htonl(pAlter->cfg.maxPointsPerSecond); - pAlter->cfg.maxStorage = htobe64(pAlter->cfg.maxStorage); - pAlter->cfg.maxQueryTime = htobe64(pAlter->cfg.maxQueryTime); - pAlter->cfg.maxInbound = htobe64(pAlter->cfg.maxInbound); - pAlter->cfg.maxOutbound = htobe64(pAlter->cfg.maxOutbound); - - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - mError("account:%s, failed to alter account, need redirect message", pAlter->user); - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - mError("account:%s, failed to alter account, invalid user", pAlter->user); - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - if (strcmp(pUser->user, "root") != 0) { - mError("account:%s, failed to alter account, no rights", pAlter->user); - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - rpcSendResponse(&rpcRsp); - return; - } - - int32_t code = mgmtAlterAcctFp(pAlter->user, pAlter->pass, &(pAlter->cfg));; - if (code == TSDB_CODE_SUCCESS) { - mLPrint("account:%s is altered by %s", pAlter->user, pUser->user); - } else { - mError("account:%s, failed to alter account, reason:%s", pAlter->user, tstrerror(code)); - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -static void mgmtProcessDropAcctMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (!mgmtDropAcctFp) { - rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; - rpcSendResponse(&rpcRsp); - return; - } - - SDropAcctMsg *pDrop = (SDropAcctMsg *) rpcMsg->pCont; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - mError("account:%s, failed to drop account, need redirect message", pDrop->user); - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - mError("account:%s, failed to drop account, invalid user", pDrop->user); - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - if (strcmp(pUser->user, "root") != 0) { - mError("account:%s, failed to drop account, no rights", pDrop->user); - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - rpcSendResponse(&rpcRsp); - return; - } - - int32_t code = mgmtDropAcctFp(pDrop->user); - if (code == TSDB_CODE_SUCCESS) { - mLPrint("account:%s is dropped by %s", pDrop->user, pUser->user); - } else { - mError("account:%s, failed to drop account, reason:%s", pDrop->user, tstrerror(code)); - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -static void mgmtProcessCreateAcctMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (!mgmtCreateAcctFp) { - rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; - rpcSendResponse(&rpcRsp); - return; - } - - SCreateAcctMsg *pCreate = (SCreateAcctMsg *) rpcMsg->pCont; - pCreate->cfg.maxUsers = htonl(pCreate->cfg.maxUsers); - pCreate->cfg.maxDbs = htonl(pCreate->cfg.maxDbs); - pCreate->cfg.maxTimeSeries = htonl(pCreate->cfg.maxTimeSeries); - pCreate->cfg.maxConnections = htonl(pCreate->cfg.maxConnections); - pCreate->cfg.maxStreams = htonl(pCreate->cfg.maxStreams); - pCreate->cfg.maxPointsPerSecond = htonl(pCreate->cfg.maxPointsPerSecond); - pCreate->cfg.maxStorage = htobe64(pCreate->cfg.maxStorage); - pCreate->cfg.maxQueryTime = htobe64(pCreate->cfg.maxQueryTime); - pCreate->cfg.maxInbound = htobe64(pCreate->cfg.maxInbound); - pCreate->cfg.maxOutbound = htobe64(pCreate->cfg.maxOutbound); - - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - mError("account:%s, failed to create account, need redirect message", pCreate->user); - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - mError("account:%s, failed to create account, invalid user", pCreate->user); - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - if (strcmp(pUser->user, "root") != 0) { - mError("account:%s, failed to create account, no rights", pCreate->user); - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - rpcSendResponse(&rpcRsp); - return; - } - - int32_t code = mgmtCreateAcctFp(pCreate->user, pCreate->pass, &(pCreate->cfg)); - if (code == TSDB_CODE_SUCCESS) { - mLPrint("account:%s is created by %s", pCreate->user, pUser->user); - } else { - mError("account:%s, failed to create account, reason:%s", pCreate->user, tstrerror(code)); - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -static void mgmtProcessCreateDnodeMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (!mgmtCreateDnodeFp) { - rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; - rpcSendResponse(&rpcRsp); - return; - } - - SCreateDnodeMsg *pCreate = (SCreateDnodeMsg *) rpcMsg->pCont; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - mError("failed to create dnode:%s, redirect this message", pCreate->ip); - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - mError("failed to create dnode:%s, reason:%s", pCreate->ip, tstrerror(TSDB_CODE_INVALID_USER)); - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - if (strcmp(pUser->user, "root") != 0) { - mError("failed to create dnode:%s, reason:%s", pCreate->ip, tstrerror(TSDB_CODE_NO_RIGHTS)); - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - rpcSendResponse(&rpcRsp); - return; - } - - int32_t code = (*mgmtCreateDnodeFp)(inet_addr(pCreate->ip)); - if (code == TSDB_CODE_SUCCESS) { - mLPrint("dnode:%s is created by %s", pCreate->ip, pUser->user); - } else { - mError("failed to create dnode:%s, reason:%s", pCreate->ip, tstrerror(code)); - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -static void mgmtProcessDropDnodeMsg(SRpcMsg *rpcMsg) { - SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; - if (!mgmtDropDnodeByIpFp) { - rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; - rpcSendResponse(&rpcRsp); - return; - } - - SDropDnodeMsg *pDrop = (SDropDnodeMsg *) rpcMsg->pCont; - if (mgmtCheckRedirectMsg(rpcMsg->handle) != TSDB_CODE_SUCCESS) { - mError("failed to drop dnode:%s, redirect this message", pDrop->ip); - return; - } - - SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); - if (pUser == NULL) { - mError("failed to drop dnode:%s, reason:%s", pDrop->ip, tstrerror(TSDB_CODE_INVALID_USER)); - rpcRsp.code = TSDB_CODE_INVALID_USER; - rpcSendResponse(&rpcRsp); - return; - } - - if (strcmp(pUser->user, "root") != 0) { - mError("failed to drop dnode:%s, reason:%s", pDrop->ip, tstrerror(TSDB_CODE_NO_RIGHTS)); - rpcRsp.code = TSDB_CODE_NO_RIGHTS; - rpcSendResponse(&rpcRsp); - return; - } - - int32_t code = (*mgmtDropDnodeByIpFp)(inet_addr(pDrop->ip)); - if (code == TSDB_CODE_SUCCESS) { - mLPrint("dnode:%s set to removing state by %s", pDrop->ip, pUser->user); - } else { - mError("failed to drop dnode:%s, reason:%s", pDrop->ip, tstrerror(code)); - } - - rpcRsp.code = code; - rpcSendResponse(&rpcRsp); -} - -void mgmtInitProcessShellMsg() { - mgmtProcessShellMsg[TSDB_MSG_TYPE_CONNECT] = mgmtProcessConnectMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_HEARTBEAT] = mgmtProcessHeartBeatMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_DB] = mgmtProcessCreateDbMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_DB] = mgmtProcessAlterDbMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_DB] = mgmtProcessDropDbMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_USE_DB] = mgmtProcessUnSupportMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_USER] = mgmtProcessCreateUserMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_USER] = mgmtProcessAlterUserMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_USER] = mgmtProcessDropUserMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_ACCT] = mgmtProcessCreateAcctMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_ACCT] = mgmtProcessDropAcctMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_ACCT] = mgmtProcessAlterAcctMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_TABLE] = mgmtProcessCreateTableMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_TABLE] = mgmtProcessDropTableMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_TABLE] = mgmtProcessAlterTableMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_DNODE] = mgmtProcessCreateDnodeMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_DNODE] = mgmtProcessDropDnodeMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_DNODE_CFG] = mgmtProcessCfgDnodeMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_MNODE] = mgmtProcessUnSupportMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_MNODE] = mgmtProcessUnSupportMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_CFG_MNODE] = mgmtProcessUnSupportMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_QUERY] = mgmtProcessKillQueryMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_STREAM] = mgmtProcessKillStreamMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_CONNECTION] = mgmtProcessKillConnectionMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_SHOW] = mgmtProcessShowMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_RETRIEVE] = mgmtProcessRetrieveMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_TABLE_META] = mgmtProcessTableMetaMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_MULTI_TABLE_META] = mgmtProcessMultiTableMetaMsg; - mgmtProcessShellMsg[TSDB_MSG_TYPE_STABLE_META] = mgmtProcessSuperTableMetaMsg; -} +//static void mgmtProcessConnectMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// SConnectMsg *pConnectMsg = (SConnectMsg *) rpcMsg->pCont; +// +// SRpcConnInfo connInfo; +// rpcGetConnInfo(rpcMsg->handle, &connInfo); +// int32_t code; +// +// SUserObj *pUser = mgmtGetUser(connInfo.user); +// if (pUser == NULL) { +// code = TSDB_CODE_INVALID_USER; +// goto connect_over; +// } +// +// if (mgmtCheckExpired()) { +// code = TSDB_CODE_GRANT_EXPIRED; +// goto connect_over; +// } +// +// SAcctObj *pAcct = mgmtGetAcct(pUser->acct); +// if (pAcct == NULL) { +// code = TSDB_CODE_INVALID_ACCT; +// goto connect_over; +// } +// +// code = taosCheckVersion(pConnectMsg->clientVersion, version, 3); +// if (code != TSDB_CODE_SUCCESS) { +// goto connect_over; +// } +// +// if (pConnectMsg->db[0]) { +// char dbName[TSDB_TABLE_ID_LEN] = {0}; +// sprintf(dbName, "%x%s%s", pAcct->acctId, TS_PATH_DELIMITER, pConnectMsg->db); +// SDbObj *pDb = mgmtGetDb(dbName); +// if (pDb == NULL) { +// code = TSDB_CODE_INVALID_DB; +// goto connect_over; +// } +// } +// +// SConnectRsp *pConnectRsp = rpcMallocCont(sizeof(SConnectRsp)); +// if (pConnectRsp == NULL) { +// code = TSDB_CODE_SERV_OUT_OF_MEMORY; +// goto connect_over; +// } +// +// sprintf(pConnectRsp->acctId, "%x", pAcct->acctId); +// strcpy(pConnectRsp->serverVersion, version); +// pConnectRsp->writeAuth = pUser->writeAuth; +// pConnectRsp->superAuth = pUser->superAuth; +// pConnectRsp->ipList.inUse = 0; +// pConnectRsp->ipList.port = htons(tsMgmtShellPort); +// pConnectRsp->ipList.numOfIps = 0; +// if (pSdbPublicIpList != NULL && pSdbIpList != NULL) { +// pConnectRsp->ipList.numOfIps = htons(pSdbPublicIpList->numOfIps); +// if (connInfo.serverIp == tsPublicIpInt) { +// for (int i = 0; i < pSdbPublicIpList->numOfIps; ++i) { +// pConnectRsp->ipList.ip[i] = htonl(pSdbPublicIpList->ip[i]); +// } +// } else { +// for (int i = 0; i < pSdbIpList->numOfIps; ++i) { +// pConnectRsp->ipList.ip[i] = htonl(pSdbIpList->ip[i]); +// } +// } +// } +// +//connect_over: +// rpcRsp.code = code; +// if (code != TSDB_CODE_SUCCESS) { +// mLError("user:%s login from %s, code:%d", connInfo.user, taosIpStr(connInfo.clientIp), code); +// } else { +// mLPrint("user:%s login from %s, code:%d", connInfo.user, taosIpStr(connInfo.clientIp), code); +// rpcRsp.pCont = pConnectRsp; +// rpcRsp.contLen = sizeof(SConnectRsp); +// } +// rpcSendResponse(&rpcRsp); +//} +// +///** +// * check if we need to add mgmtProcessTableMetaMsg into tranQueue, which will be executed one-by-one. +// */ +//static bool mgmtCheckMeterMetaMsgType(void *pMsg) { +// STableInfoMsg *pInfo = (STableInfoMsg *) pMsg; +// int16_t autoCreate = htons(pInfo->createFlag); +// STableInfo *pTable = mgmtGetTable(pInfo->tableId); +// +// // If table does not exists and autoCreate flag is set, we add the handler into task queue +// bool addIntoTranQueue = (pTable == NULL && autoCreate == 1); +// if (addIntoTranQueue) { +// mTrace("table:%s auto created task added", pInfo->tableId); +// } +// +// return addIntoTranQueue; +//} +// +//static bool mgmtCheckMsgReadOnly(int8_t type, void *pCont) { +// if ((type == TSDB_MSG_TYPE_TABLE_META && (!mgmtCheckMeterMetaMsgType(pCont))) || +// type == TSDB_MSG_TYPE_STABLE_META || type == TSDB_MSG_TYPE_RETRIEVE || +// type == TSDB_MSG_TYPE_SHOW || type == TSDB_MSG_TYPE_MULTI_TABLE_META || +// type == TSDB_MSG_TYPE_CONNECT) { +// return true; +// } +// +// return false; +//} +// +//static void mgmtProcessMsgFromShell(SRpcMsg *rpcMsg) { +// if (sdbGetRunStatus() != SDB_STATUS_SERVING) { +// mTrace("shell msg is ignored since SDB is not ready"); +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = TSDB_CODE_NOT_READY, .msgType = 0}; +// rpcSendResponse(&rpcRsp); +// rpcFreeCont(rpcMsg->pCont); +// return; +// } +// +// if (mgmtCheckMsgReadOnly(rpcMsg->msgType, rpcMsg->pCont)) { +// (*mgmtProcessShellMsg[rpcMsg->msgType])(rpcMsg); +// } else { +// if (mgmtProcessShellMsg[rpcMsg->msgType]) { +// mgmtAddToTranRequest(rpcMsg); +// } else { +// mError("%s from shell is not processed", taosMsg[rpcMsg->msgType]); +// } +// } +// +// rpcFreeCont(rpcMsg->pCont); +//} +// +//void mgmtProcessCreateVgroup(SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta) { +// SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// SDbObj *pDb = mgmtGetDb(pCreate->db); +// if (pDb == NULL) { +// mError("table:%s, failed to create vgroup, db not found", pCreate->tableId); +// rpcRsp.code = TSDB_CODE_INVALID_DB; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SVgObj *pVgroup = mgmtCreateVgroup(pDb); +// if (pVgroup == NULL) { +// mError("table:%s, failed to alloc vnode to vgroup", pCreate->tableId); +// rpcRsp.code = TSDB_CODE_NO_ENOUGH_DNODES; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// void *cont = rpcMallocCont(contLen); +// if (cont == NULL) { +// mError("table:%s, failed to create table, can not alloc memory", pCreate->tableId); +// rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// memcpy(cont, pCreate, contLen); +// +// SProcessInfo *info = calloc(1, sizeof(SProcessInfo)); +// info->type = TSDB_PROCESS_CREATE_VGROUP; +// info->thandle = thandle; +// info->ahandle = pVgroup; +// info->cont = cont; +// info->contLen = contLen; +// +// if (isGetMeta) { +// info->type = TSDB_PROCESS_CREATE_VGROUP_GET_META; +// } +// +// mgmtSendCreateVgroupMsg(pVgroup, info); +//} +// +//void mgmtProcessCreateTable(SVgObj *pVgroup, SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta) { +// assert(pVgroup != NULL); +// SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// +// int32_t sid = taosAllocateId(pVgroup->idPool); +// if (sid < 0) { +// mTrace("table:%s, no enough sid in vgroup:%d, start to create a new vgroup", pCreate->tableId, pVgroup->vgId); +// mgmtProcessCreateVgroup(pCreate, contLen, thandle, isGetMeta); +// return; +// } +// +// STableInfo *pTable; +// SDMCreateTableMsg *pDCreate = NULL; +// +// if (pCreate->numOfColumns == 0) { +// mTrace("table:%s, start to create child table, vgroup:%d sid:%d", pCreate->tableId, pVgroup->vgId, sid); +// rpcRsp.code = mgmtCreateChildTable(pCreate, contLen, pVgroup, sid, &pDCreate, &pTable); +// } else { +// mTrace("table:%s, start to create normal table, vgroup:%d sid:%d", pCreate->tableId, pVgroup->vgId, sid); +// rpcRsp.code = mgmtCreateNormalTable(pCreate, contLen, pVgroup, sid, &pDCreate, &pTable); +// } +// +// if (rpcRsp.code != TSDB_CODE_SUCCESS) { +// mTrace("table:%s, failed to create table in vgroup:%d sid:%d ", pCreate->tableId, pVgroup->vgId, sid); +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// assert(pDCreate != NULL); +// assert(pTable != NULL); +// +// SProcessInfo *info = calloc(1, sizeof(SProcessInfo)); +// info->type = TSDB_PROCESS_CREATE_TABLE; +// info->thandle = thandle; +// info->ahandle = pTable; +// SRpcIpSet ipSet = mgmtGetIpSetFromVgroup(pVgroup); +// if (isGetMeta) { +// info->type = TSDB_PROCESS_CREATE_TABLE_GET_META; +// } +// +// mgmtSendCreateTableMsg(pDCreate, &ipSet, info); +//} +// +//void mgmtProcessGetTableMeta(STableInfo *pTable, void *thandle) { +// SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// SDbObj* pDb = mgmtGetDbByTableId(pTable->tableId); +// if (pDb == NULL || pDb->dropStatus != TSDB_DB_STATUS_READY) { +// mError("table:%s, failed to get table meta, db not selected", pTable->tableId); +// rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SRpcConnInfo connInfo; +// rpcGetConnInfo(thandle, &connInfo); +// bool usePublicIp = (connInfo.serverIp == tsPublicIpInt); +// +// STableMeta *pMeta = rpcMallocCont(sizeof(STableMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS); +// rpcRsp.code = mgmtGetTableMeta(pDb, pTable, pMeta, usePublicIp); +// +// if (rpcRsp.code != TSDB_CODE_SUCCESS) { +// rpcFreeCont(pMeta); +// } else { +// pMeta->contLen = htons(pMeta->contLen); +// rpcRsp.pCont = pMeta; +// rpcRsp.contLen = pMeta->contLen; +// } +// +// rpcSendResponse(&rpcRsp); +//} +// +//static int32_t mgmtCheckRedirectMsgImp(void *pConn) { +// return 0; +//} +// +//int32_t (*mgmtCheckRedirect)(void *pConn) = mgmtCheckRedirectMsgImp; +// +//static void mgmtProcessUnSupportMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = { +// .msgType = 0, +// .pCont = 0, +// .contLen = 0, +// .code = TSDB_CODE_OPS_NOT_SUPPORT, +// .handle = rpcMsg->handle +// }; +// rpcSendResponse(&rpcRsp); +//} +// +//static void mgmtProcessAlterAcctMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (!mgmtAlterAcctFp) { +// rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SAlterAcctMsg *pAlter = rpcMsg->pCont; +// pAlter->cfg.maxUsers = htonl(pAlter->cfg.maxUsers); +// pAlter->cfg.maxDbs = htonl(pAlter->cfg.maxDbs); +// pAlter->cfg.maxTimeSeries = htonl(pAlter->cfg.maxTimeSeries); +// pAlter->cfg.maxConnections = htonl(pAlter->cfg.maxConnections); +// pAlter->cfg.maxStreams = htonl(pAlter->cfg.maxStreams); +// pAlter->cfg.maxPointsPerSecond = htonl(pAlter->cfg.maxPointsPerSecond); +// pAlter->cfg.maxStorage = htobe64(pAlter->cfg.maxStorage); +// pAlter->cfg.maxQueryTime = htobe64(pAlter->cfg.maxQueryTime); +// pAlter->cfg.maxInbound = htobe64(pAlter->cfg.maxInbound); +// pAlter->cfg.maxOutbound = htobe64(pAlter->cfg.maxOutbound); +// +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// mError("account:%s, failed to alter account, need redirect message", pAlter->user); +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// mError("account:%s, failed to alter account, invalid user", pAlter->user); +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// if (strcmp(pUser->user, "root") != 0) { +// mError("account:%s, failed to alter account, no rights", pAlter->user); +// rpcRsp.code = TSDB_CODE_NO_RIGHTS; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// int32_t code = mgmtAlterAcctFp(pAlter->user, pAlter->pass, &(pAlter->cfg));; +// if (code == TSDB_CODE_SUCCESS) { +// mLPrint("account:%s is altered by %s", pAlter->user, pUser->user); +// } else { +// mError("account:%s, failed to alter account, reason:%s", pAlter->user, tstrerror(code)); +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// +//static void mgmtProcessDropAcctMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (!mgmtDropAcctFp) { +// rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SDropAcctMsg *pDrop = (SDropAcctMsg *) rpcMsg->pCont; +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// mError("account:%s, failed to drop account, need redirect message", pDrop->user); +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// mError("account:%s, failed to drop account, invalid user", pDrop->user); +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// if (strcmp(pUser->user, "root") != 0) { +// mError("account:%s, failed to drop account, no rights", pDrop->user); +// rpcRsp.code = TSDB_CODE_NO_RIGHTS; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// int32_t code = mgmtDropAcctFp(pDrop->user); +// if (code == TSDB_CODE_SUCCESS) { +// mLPrint("account:%s is dropped by %s", pDrop->user, pUser->user); +// } else { +// mError("account:%s, failed to drop account, reason:%s", pDrop->user, tstrerror(code)); +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// +//static void mgmtProcessCreateAcctMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (!mgmtCreateAcctFp) { +// rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SCreateAcctMsg *pCreate = (SCreateAcctMsg *) rpcMsg->pCont; +// pCreate->cfg.maxUsers = htonl(pCreate->cfg.maxUsers); +// pCreate->cfg.maxDbs = htonl(pCreate->cfg.maxDbs); +// pCreate->cfg.maxTimeSeries = htonl(pCreate->cfg.maxTimeSeries); +// pCreate->cfg.maxConnections = htonl(pCreate->cfg.maxConnections); +// pCreate->cfg.maxStreams = htonl(pCreate->cfg.maxStreams); +// pCreate->cfg.maxPointsPerSecond = htonl(pCreate->cfg.maxPointsPerSecond); +// pCreate->cfg.maxStorage = htobe64(pCreate->cfg.maxStorage); +// pCreate->cfg.maxQueryTime = htobe64(pCreate->cfg.maxQueryTime); +// pCreate->cfg.maxInbound = htobe64(pCreate->cfg.maxInbound); +// pCreate->cfg.maxOutbound = htobe64(pCreate->cfg.maxOutbound); +// +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// mError("account:%s, failed to create account, need redirect message", pCreate->user); +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// mError("account:%s, failed to create account, invalid user", pCreate->user); +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// if (strcmp(pUser->user, "root") != 0) { +// mError("account:%s, failed to create account, no rights", pCreate->user); +// rpcRsp.code = TSDB_CODE_NO_RIGHTS; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// int32_t code = mgmtCreateAcctFp(pCreate->user, pCreate->pass, &(pCreate->cfg)); +// if (code == TSDB_CODE_SUCCESS) { +// mLPrint("account:%s is created by %s", pCreate->user, pUser->user); +// } else { +// mError("account:%s, failed to create account, reason:%s", pCreate->user, tstrerror(code)); +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// +//static void mgmtProcessCreateDnodeMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (!mgmtCreateDnodeFp) { +// rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SCreateDnodeMsg *pCreate = (SCreateDnodeMsg *) rpcMsg->pCont; +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// mError("failed to create dnode:%s, redirect this message", pCreate->ip); +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// mError("failed to create dnode:%s, reason:%s", pCreate->ip, tstrerror(TSDB_CODE_INVALID_USER)); +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// if (strcmp(pUser->user, "root") != 0) { +// mError("failed to create dnode:%s, reason:%s", pCreate->ip, tstrerror(TSDB_CODE_NO_RIGHTS)); +// rpcRsp.code = TSDB_CODE_NO_RIGHTS; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// int32_t code = (*mgmtCreateDnodeFp)(inet_addr(pCreate->ip)); +// if (code == TSDB_CODE_SUCCESS) { +// mLPrint("dnode:%s is created by %s", pCreate->ip, pUser->user); +// } else { +// mError("failed to create dnode:%s, reason:%s", pCreate->ip, tstrerror(code)); +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// +//static void mgmtProcessDropDnodeMsg(SRpcMsg *rpcMsg) { +// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; +// if (!mgmtDropDnodeByIpFp) { +// rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// SDropDnodeMsg *pDrop = (SDropDnodeMsg *) rpcMsg->pCont; +// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { +// mError("failed to drop dnode:%s, redirect this message", pDrop->ip); +// return; +// } +// +// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); +// if (pUser == NULL) { +// mError("failed to drop dnode:%s, reason:%s", pDrop->ip, tstrerror(TSDB_CODE_INVALID_USER)); +// rpcRsp.code = TSDB_CODE_INVALID_USER; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// if (strcmp(pUser->user, "root") != 0) { +// mError("failed to drop dnode:%s, reason:%s", pDrop->ip, tstrerror(TSDB_CODE_NO_RIGHTS)); +// rpcRsp.code = TSDB_CODE_NO_RIGHTS; +// rpcSendResponse(&rpcRsp); +// return; +// } +// +// int32_t code = (*mgmtDropDnodeByIpFp)(inet_addr(pDrop->ip)); +// if (code == TSDB_CODE_SUCCESS) { +// mLPrint("dnode:%s set to removing state by %s", pDrop->ip, pUser->user); +// } else { +// mError("failed to drop dnode:%s, reason:%s", pDrop->ip, tstrerror(code)); +// } +// +// rpcRsp.code = code; +// rpcSendResponse(&rpcRsp); +//} +// +//void mgmtInitProcessShellMsg() { +// mgmtProcessShellMsg[TSDB_MSG_TYPE_CONNECT] = mgmtProcessConnectMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_HEARTBEAT] = mgmtProcessHeartBeatMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_DB] = mgmtProcessCreateDbMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_DB] = mgmtProcessAlterDbMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_DB] = mgmtProcessDropDbMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_USE_DB] = mgmtProcessUnSupportMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_ACCT] = mgmtProcessCreateAcctMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_ACCT] = mgmtProcessDropAcctMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_ACCT] = mgmtProcessAlterAcctMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_TABLE] = mgmtProcessCreateTableMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_TABLE] = mgmtProcessDropTableMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_TABLE] = mgmtProcessAlterTableMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_DNODE] = mgmtProcessCreateDnodeMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_DNODE] = mgmtProcessDropDnodeMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_MD_CONFIG_DNODE] = mgmtProcessCfgDnodeMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_MNODE] = mgmtProcessUnSupportMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_MNODE] = mgmtProcessUnSupportMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_CFG_MNODE] = mgmtProcessUnSupportMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_QUERY] = mgmtProcessKillQueryMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_STREAM] = mgmtProcessKillStreamMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_CONNECTION] = mgmtProcessKillConnectionMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_TABLE_META] = mgmtProcessTableMetaMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_MULTI_TABLE_META] = mgmtProcessMultiTableMetaMsg; +// mgmtProcessShellMsg[TSDB_MSG_TYPE_STABLE_META] = mgmtProcessSuperTableMetaMsg; +//} diff --git a/src/mnode/src/mgmtSuperTable.c b/src/mnode/src/mgmtSuperTable.c index 2fb4129e75..ddef464088 100644 --- a/src/mnode/src/mgmtSuperTable.c +++ b/src/mnode/src/mgmtSuperTable.c @@ -28,7 +28,6 @@ #include "mgmtChildTable.h" #include "mgmtDb.h" #include "mgmtDnode.h" -#include "mgmtDnodeInt.h" #include "mgmtGrant.h" #include "mgmtSuperTable.h" #include "mgmtTable.h" diff --git a/src/mnode/src/mgmtSystem.c b/src/mnode/src/mgmtSystem.c index bcb5f64a7b..5265c3bcda 100644 --- a/src/mnode/src/mgmtSystem.c +++ b/src/mnode/src/mgmtSystem.c @@ -23,7 +23,6 @@ #include "mgmtBalance.h" #include "mgmtDb.h" #include "mgmtDnode.h" -#include "mgmtDnodeInt.h" #include "mgmtVgroup.h" #include "mgmtUser.h" #include "mgmtSystem.h" diff --git a/src/mnode/src/mgmtTable.c b/src/mnode/src/mgmtTable.c index ca35de3757..59f313bf54 100644 --- a/src/mnode/src/mgmtTable.c +++ b/src/mnode/src/mgmtTable.c @@ -30,7 +30,6 @@ #include "mgmtChildTable.h" #include "mgmtDb.h" #include "mgmtDnode.h" -#include "mgmtDnodeInt.h" #include "mgmtGrant.h" #include "mgmtNormalTable.h" #include "mgmtProfile.h" @@ -412,8 +411,8 @@ int32_t mgmtRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void * return numOfRows; } -SDRemoveTableMsg *mgmtBuildRemoveTableMsg(STableInfo *pTable) { - SDRemoveTableMsg *pRemove = NULL; +SMDDropTableMsg *mgmtBuildRemoveTableMsg(STableInfo *pTable) { + SMDDropTableMsg *pRemove = NULL; return pRemove; diff --git a/src/mnode/src/mgmtUser.c b/src/mnode/src/mgmtUser.c index 33b6c06c51..1747f0bafd 100644 --- a/src/mnode/src/mgmtUser.c +++ b/src/mnode/src/mgmtUser.c @@ -15,42 +15,45 @@ #define _DEFAULT_SOURCE #include "os.h" +#include "trpc.h" #include "tschemautil.h" #include "ttime.h" #include "mnode.h" -#include "mgmtAcct.h" + + +#include "mgmtMnode.h" +#include "mgmtShell.h" #include "mgmtUser.h" + +#include "mgmtAcct.h" #include "mgmtGrant.h" #include "mgmtTable.h" + void *tsUserSdb = NULL; static int32_t tsUserUpdateSize = 0; -void *(*mgmtUserActionFp[SDB_MAX_ACTION_TYPES])(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtUserActionInsert(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtUserActionDelete(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtUserActionUpdate(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtUserActionEncode(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtUserActionDecode(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtUserActionReset(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtUserActionDestroy(void *row, char *str, int32_t size, int32_t *ssize); +static int32_t mgmtCreateUser(SAcctObj *pAcct, char *name, char *pass); +static int32_t mgmtDropUser(SAcctObj *pAcct, char *name); +static int32_t mgmtUpdateUser(SUserObj *pUser); +static int32_t mgmtGetUserMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveUsers(SShowObj *pShow, char *data, int32_t rows, void *pConn); +static SUserObj *mgmtGetUserFromConn(void *pConn); -void mgmtUserActionInit() { - mgmtUserActionFp[SDB_TYPE_INSERT] = mgmtUserActionInsert; - mgmtUserActionFp[SDB_TYPE_DELETE] = mgmtUserActionDelete; - mgmtUserActionFp[SDB_TYPE_UPDATE] = mgmtUserActionUpdate; - mgmtUserActionFp[SDB_TYPE_ENCODE] = mgmtUserActionEncode; - mgmtUserActionFp[SDB_TYPE_DECODE] = mgmtUserActionDecode; - mgmtUserActionFp[SDB_TYPE_RESET] = mgmtUserActionReset; - mgmtUserActionFp[SDB_TYPE_DESTROY] = mgmtUserActionDestroy; -} +static void mgmtProcessCreateUserMsg(SRpcMsg *rpcMsg); +static void mgmtProcessAlterUserMsg(SRpcMsg *rpcMsg); +static void mgmtProcessDropUserMsg(SRpcMsg *rpcMsg); -void *mgmtUserAction(char action, void *row, char *str, int32_t size, int32_t *ssize) { - if (mgmtUserActionFp[(uint8_t) action] != NULL) { - return (*(mgmtUserActionFp[(uint8_t) action]))(row, str, size, ssize); - } - return NULL; -} +static void *(*mgmtUserActionFp[SDB_MAX_ACTION_TYPES])(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtUserActionInsert(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtUserActionDelete(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtUserActionUpdate(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtUserActionEncode(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtUserActionDecode(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtUserActionReset(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtUserActionDestroy(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtUserAction(char action, void *row, char *str, int32_t size, int32_t *ssize); +static void mgmtUserActionInit(); int32_t mgmtInitUsers() { void *pNode = NULL; @@ -87,19 +90,29 @@ int32_t mgmtInitUsers() { mgmtCreateUser(pAcct, "monitor", tsInternalPass); mgmtCreateUser(pAcct, "_root", tsInternalPass); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_CREATE_USER, mgmtProcessCreateUserMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_ALTER_USER, mgmtProcessAlterUserMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_DROP_USER, mgmtProcessDropUserMsg); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_USER, mgmtGetUserMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_USER, mgmtRetrieveUsers); + mTrace("user data is initialized"); return 0; } +void mgmtCleanUpUsers() { + sdbCloseTable(tsUserSdb); +} + SUserObj *mgmtGetUser(char *name) { return (SUserObj *)sdbGetRow(tsUserSdb, name); } -int32_t mgmtUpdateUser(SUserObj *pUser) { +static int32_t mgmtUpdateUser(SUserObj *pUser) { return sdbUpdateRow(tsUserSdb, pUser, 0, 1); } -int32_t mgmtCreateUser(SAcctObj *pAcct, char *name, char *pass) { +static int32_t mgmtCreateUser(SAcctObj *pAcct, char *name, char *pass) { int32_t code = mgmtCheckUserLimit(pAcct); if (code != 0) { return code; @@ -141,7 +154,7 @@ int32_t mgmtCreateUser(SAcctObj *pAcct, char *name, char *pass) { return code; } -int32_t mgmtDropUser(SAcctObj *pAcct, char *name) { +static int32_t mgmtDropUser(SAcctObj *pAcct, char *name) { SUserObj *pUser; pUser = (SUserObj *)sdbGetRow(tsUserSdb, name); @@ -159,11 +172,7 @@ int32_t mgmtDropUser(SAcctObj *pAcct, char *name) { return 0; } -void mgmtCleanUpUsers() { - sdbCloseTable(tsUserSdb); -} - -int32_t mgmtGetUserMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { +static int32_t mgmtGetUserMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { SUserObj *pUser = mgmtGetUserFromConn(pConn); if (pUser == NULL) { return TSDB_CODE_INVALID_USER; @@ -206,7 +215,7 @@ int32_t mgmtGetUserMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { return 0; } -int32_t mgmtRetrieveUsers(SShowObj *pShow, char *data, int32_t rows, void *pConn) { +static int32_t mgmtRetrieveUsers(SShowObj *pShow, char *data, int32_t rows, void *pConn) { int32_t numOfRows = 0; SUserObj *pUser = NULL; int32_t cols = 0; @@ -243,7 +252,24 @@ int32_t mgmtRetrieveUsers(SShowObj *pShow, char *data, int32_t rows, void *pConn return numOfRows; } -void *mgmtUserActionInsert(void *row, char *str, int32_t size, int32_t *ssize) { +static void mgmtUserActionInit() { + mgmtUserActionFp[SDB_TYPE_INSERT] = mgmtUserActionInsert; + mgmtUserActionFp[SDB_TYPE_DELETE] = mgmtUserActionDelete; + mgmtUserActionFp[SDB_TYPE_UPDATE] = mgmtUserActionUpdate; + mgmtUserActionFp[SDB_TYPE_ENCODE] = mgmtUserActionEncode; + mgmtUserActionFp[SDB_TYPE_DECODE] = mgmtUserActionDecode; + mgmtUserActionFp[SDB_TYPE_RESET] = mgmtUserActionReset; + mgmtUserActionFp[SDB_TYPE_DESTROY] = mgmtUserActionDestroy; +} + +static void *mgmtUserAction(char action, void *row, char *str, int32_t size, int32_t *ssize) { + if (mgmtUserActionFp[(uint8_t) action] != NULL) { + return (*(mgmtUserActionFp[(uint8_t) action]))(row, str, size, ssize); + } + return NULL; +} + +static void *mgmtUserActionInsert(void *row, char *str, int32_t size, int32_t *ssize) { SUserObj *pUser = (SUserObj *) row; SAcctObj *pAcct = mgmtGetAcct(pUser->acct); @@ -253,7 +279,7 @@ void *mgmtUserActionInsert(void *row, char *str, int32_t size, int32_t *ssize) { return NULL; } -void *mgmtUserActionDelete(void *row, char *str, int32_t size, int32_t *ssize) { +static void *mgmtUserActionDelete(void *row, char *str, int32_t size, int32_t *ssize) { SUserObj *pUser = (SUserObj *) row; SAcctObj *pAcct = mgmtGetAcct(pUser->acct); @@ -262,11 +288,11 @@ void *mgmtUserActionDelete(void *row, char *str, int32_t size, int32_t *ssize) { return NULL; } -void *mgmtUserActionUpdate(void *row, char *str, int32_t size, int32_t *ssize) { +static void *mgmtUserActionUpdate(void *row, char *str, int32_t size, int32_t *ssize) { return mgmtUserActionReset(row, str, size, ssize); } -void *mgmtUserActionEncode(void *row, char *str, int32_t size, int32_t *ssize) { +static void *mgmtUserActionEncode(void *row, char *str, int32_t size, int32_t *ssize) { SUserObj *pUser = (SUserObj *) row; if (size < tsUserUpdateSize) { @@ -279,7 +305,7 @@ void *mgmtUserActionEncode(void *row, char *str, int32_t size, int32_t *ssize) { return NULL; } -void *mgmtUserActionDecode(void *row, char *str, int32_t size, int32_t *ssize) { +static void *mgmtUserActionDecode(void *row, char *str, int32_t size, int32_t *ssize) { SUserObj *pUser = (SUserObj *) malloc(sizeof(SUserObj)); if (pUser == NULL) return NULL; memset(pUser, 0, sizeof(SUserObj)); @@ -289,7 +315,7 @@ void *mgmtUserActionDecode(void *row, char *str, int32_t size, int32_t *ssize) { return (void *)pUser; } -void *mgmtUserActionReset(void *row, char *str, int32_t size, int32_t *ssize) { +static void *mgmtUserActionReset(void *row, char *str, int32_t size, int32_t *ssize) { SUserObj *pUser = (SUserObj *)row; memcpy(pUser, str, tsUserUpdateSize); @@ -297,15 +323,200 @@ void *mgmtUserActionReset(void *row, char *str, int32_t size, int32_t *ssize) { return NULL; } -void *mgmtUserActionDestroy(void *row, char *str, int32_t size, int32_t *ssize) { +static void *mgmtUserActionDestroy(void *row, char *str, int32_t size, int32_t *ssize) { tfree(row); return NULL; } -SUserObj *mgmtGetUserFromConn(void *pConn) { +static SUserObj *mgmtGetUserFromConn(void *pConn) { SRpcConnInfo connInfo; rpcGetConnInfo(pConn, &connInfo); return mgmtGetUser(connInfo.user); } + +static void mgmtProcessCreateUserMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle)) return; + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + if (pUser->superAuth) { + SCreateUserMsg *pCreate = rpcMsg->pCont; + rpcRsp.code = mgmtCreateUser(pUser->pAcct, pCreate->user, pCreate->pass); + if (rpcRsp.code == TSDB_CODE_SUCCESS) { + mLPrint("user:%s is created by %s", pCreate->user, pUser->user); + } + } else { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } + + rpcSendResponse(&rpcRsp); +} + +static void mgmtProcessAlterUserMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle)) return; + + SUserObj *pOperUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pOperUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + SAlterUserMsg *pAlter = rpcMsg->pCont; + SUserObj *pUser = mgmtGetUser(pAlter->user); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + if (strcmp(pUser->user, "monitor") == 0 || (strcmp(pUser->user + 1, pUser->acct) == 0 && pUser->user[0] == '_')) { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + rpcSendResponse(&rpcRsp); + return; + } + + if ((pAlter->flag & TSDB_ALTER_USER_PASSWD) != 0) { + bool hasRight = false; + if (strcmp(pOperUser->user, "root") == 0) { + hasRight = true; + } else if (strcmp(pUser->user, pOperUser->user) == 0) { + hasRight = true; + } else if (pOperUser->superAuth) { + if (strcmp(pUser->user, "root") == 0) { + hasRight = false; + } else if (strcmp(pOperUser->acct, pUser->acct) != 0) { + hasRight = false; + } else { + hasRight = true; + } + } + + if (hasRight) { + memset(pUser->pass, 0, sizeof(pUser->pass)); + taosEncryptPass((uint8_t*)pAlter->pass, strlen(pAlter->pass), pUser->pass); + rpcRsp.code = mgmtUpdateUser(pUser); + mLPrint("user:%s password is altered by %s, code:%d", pAlter->user, pUser->user, rpcRsp.code); + } else { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } + + rpcSendResponse(&rpcRsp); + return; + } + + if ((pAlter->flag & TSDB_ALTER_USER_PRIVILEGES) != 0) { + bool hasRight = false; + + if (strcmp(pUser->user, "root") == 0) { + hasRight = false; + } else if (strcmp(pUser->user, pUser->acct) == 0) { + hasRight = false; + } else if (strcmp(pOperUser->user, "root") == 0) { + hasRight = true; + } else if (strcmp(pUser->user, pOperUser->user) == 0) { + hasRight = false; + } else if (pOperUser->superAuth) { + if (strcmp(pUser->user, "root") == 0) { + hasRight = false; + } else if (strcmp(pOperUser->acct, pUser->acct) != 0) { + hasRight = false; + } else { + hasRight = true; + } + } + + if (pAlter->privilege == 1) { // super + hasRight = false; + } + + if (hasRight) { + //if (pAlter->privilege == 1) { // super + // pUser->superAuth = 1; + // pUser->writeAuth = 1; + //} + if (pAlter->privilege == 2) { // read + pUser->superAuth = 0; + pUser->writeAuth = 0; + } + if (pAlter->privilege == 3) { // write + pUser->superAuth = 0; + pUser->writeAuth = 1; + } + + rpcRsp.code = mgmtUpdateUser(pUser); + mLPrint("user:%s privilege is altered by %s, code:%d", pAlter->user, pUser->user, rpcRsp.code); + } else { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } + + rpcSendResponse(&rpcRsp); + return; + } + + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + rpcSendResponse(&rpcRsp); +} + +static void mgmtProcessDropUserMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle)) return; + + SUserObj *pOperUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pOperUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return ; + } + + SDropUserMsg *pDrop = rpcMsg->pCont; + SUserObj *pUser = mgmtGetUser(pDrop->user); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return ; + } + + if (strcmp(pUser->user, "monitor") == 0 || (strcmp(pUser->user + 1, pUser->acct) == 0 && pUser->user[0] == '_')) { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + rpcSendResponse(&rpcRsp); + return ; + } + + bool hasRight = false; + if (strcmp(pUser->user, "root") == 0) { + hasRight = false; + } else if (strcmp(pOperUser->user, "root") == 0) { + hasRight = true; + } else if (strcmp(pUser->user, pOperUser->user) == 0) { + hasRight = false; + } else if (pOperUser->superAuth) { + if (strcmp(pUser->user, "root") == 0) { + hasRight = false; + } else if (strcmp(pOperUser->acct, pUser->acct) != 0) { + hasRight = false; + } else { + hasRight = true; + } + } + + if (hasRight) { + rpcRsp.code = mgmtDropUser(pUser->pAcct, pDrop->user); + if (rpcRsp.code == TSDB_CODE_SUCCESS) { + mLPrint("user:%s is dropped by %s", pDrop->user, pUser->user); + } + } else { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } + + rpcSendResponse(&rpcRsp); +} diff --git a/src/mnode/src/mgmtVgroup.c b/src/mnode/src/mgmtVgroup.c index b0ff80a819..84ed20c002 100644 --- a/src/mnode/src/mgmtVgroup.c +++ b/src/mnode/src/mgmtVgroup.c @@ -23,7 +23,6 @@ #include "mgmtBalance.h" #include "mgmtDb.h" #include "mgmtDnode.h" -#include "mgmtDnodeInt.h" #include "mgmtTable.h" #include "mgmtVgroup.h" @@ -193,7 +192,7 @@ int32_t mgmtDropVgroup(SDbObj *pDb, SVgObj *pVgroup) { mTrace("vgroup:%d, db:%s replica:%d is deleted", pVgroup->vgId, pDb->name, pVgroup->numOfVnodes); - mgmtSendRemoveVgroupMsg(pVgroup, NULL); + mgmtSendDropVgroupMsg(pVgroup, NULL); sdbDeleteRow(tsVgroupSdb, pVgroup); @@ -503,11 +502,11 @@ void mgmtRemoveTableFromVgroup(SVgObj *pVgroup, STableInfo *pTable) { taosFreeId(pVgroup->idPool, pTable->sid); } -SCreateVnodeMsg *mgmtBuildVpeersMsg(SVgObj *pVgroup, int32_t vnode) { +SMDCreateVnodeMsg *mgmtBuildCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode) { SDbObj *pDb = mgmtGetDb(pVgroup->dbName); if (pDb == NULL) return NULL; - SCreateVnodeMsg *pVPeers = rpcMallocCont(sizeof(SCreateVnodeMsg)); + SMDCreateVnodeMsg *pVPeers = rpcMallocCont(sizeof(SMDCreateVnodeMsg)); if (pVPeers == NULL) return NULL; pVPeers->vnode = htonl(vnode); From 0a912796674fb3569daa2a6d972283f56a3cb45f Mon Sep 17 00:00:00 2001 From: slguan Date: Tue, 10 Mar 2020 14:04:41 +0800 Subject: [PATCH 3/5] refactor mgmtUser and mgmtAcct --- src/mnode/inc/mgmtAcct.h | 11 +---- src/mnode/src/mgmtAcct.c | 95 +++++++++------------------------------- src/mnode/src/mgmtUser.c | 16 +++---- 3 files changed, 30 insertions(+), 92 deletions(-) diff --git a/src/mnode/inc/mgmtAcct.h b/src/mnode/inc/mgmtAcct.h index 1aaa35e685..a39641a9b6 100644 --- a/src/mnode/inc/mgmtAcct.h +++ b/src/mnode/inc/mgmtAcct.h @@ -19,28 +19,21 @@ #ifdef __cplusplus extern "C" { #endif - #include "mnode.h" -int32_t mgmtInitAccts(); -void mgmtCleanUpAccts(); +int32_t mgmtInitAccts(); +void mgmtCleanUpAccts(); SAcctObj *mgmtGetAcct(char *acctName); int32_t mgmtCheckUserLimit(SAcctObj *pAcct); int32_t mgmtCheckDbLimit(SAcctObj *pAcct); int32_t mgmtCheckTableLimit(SAcctObj *pAcct, int32_t numOfTimeSeries); -int32_t mgmtGetAcctMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveAccts(SShowObj *pShow, char *data, int32_t rows, void *pConn); int32_t mgmtAddDbIntoAcct(SAcctObj *pAcct, SDbObj *pDb); int32_t mgmtRemoveDbFromAcct(SAcctObj *pAcct, SDbObj *pDb); int32_t mgmtAddUserIntoAcct(SAcctObj *pAcct, SUserObj *pUser); int32_t mgmtRemoveUserFromAcct(SAcctObj *pAcct, SUserObj *pUser); -extern int32_t (*mgmtCreateAcctFp)(char *name, char *pass, SAcctCfg *pCfg); -extern int32_t (*mgmtDropAcctFp)(char *name); -extern int32_t (*mgmtAlterAcctFp)(char *name, char *pass, SAcctCfg *pCfg); - #ifdef __cplusplus } #endif diff --git a/src/mnode/src/mgmtAcct.c b/src/mnode/src/mgmtAcct.c index 2b3262292e..4c25449c22 100644 --- a/src/mnode/src/mgmtAcct.c +++ b/src/mnode/src/mgmtAcct.c @@ -15,30 +15,16 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "tschemautil.h" -#include "taoserror.h" -#include "mnode.h" #include "mgmtAcct.h" -#include "mgmtTable.h" -#include "mgmtUser.h" -extern void *tsUserSdb; -extern void *tsDbSdb; static SAcctObj tsAcctObj; -int32_t (*mgmtInitAcctsFp)() = NULL; -void (*mgmtCleanUpAcctsFp)() = NULL; - -int32_t (*mgmtCreateAcctFp)(char *name, char *pass, SAcctCfg *pCfg) = NULL; -int32_t (*mgmtDropAcctFp)(char *name) = NULL; -int32_t (*mgmtAlterAcctFp)(char *name, char *pass, SAcctCfg *pCfg) = NULL; -int32_t (*mgmtGetAcctMetaFp)(STableMeta *pMeta, SShowObj *pShow, void *pConn) = NULL; -int32_t (*mgmtRetrieveAcctsFp)(SShowObj *pShow, char *data, int32_t rows, void *pConn) = NULL; +int32_t (*mgmtInitAcctsFp)() = NULL; +void (*mgmtCleanUpAcctsFp)() = NULL; SAcctObj *(*mgmtGetAcctFp)(char *acctName) = NULL; - -int32_t (*mgmtCheckUserLimitFp)(SAcctObj *pAcct) = NULL; -int32_t (*mgmtCheckDbLimitFp)(SAcctObj *pAcct) = NULL; -int32_t (*mgmtCheckTimeSeriesLimitFp)(SAcctObj *pAcct, int32_t numOfTimeSeries) = NULL; +int32_t (*mgmtCheckUserLimitFp)(SAcctObj *pAcct) = NULL; +int32_t (*mgmtCheckDbLimitFp)(SAcctObj *pAcct) = NULL; +int32_t (*mgmtCheckTimeSeriesLimitFp)(SAcctObj *pAcct, int32_t numOfTimeSeries) = NULL; int32_t mgmtAddDbIntoAcct(SAcctObj *pAcct, SDbObj *pDb) { pthread_mutex_lock(&pAcct->mutex); @@ -113,88 +99,47 @@ int32_t mgmtRemoveUserFromAcct(SAcctObj *pAcct, SUserObj *pUser) { int32_t mgmtInitAccts() { if (mgmtInitAcctsFp) { - return mgmtInitAcctsFp(); + return (*mgmtInitAcctsFp)(); } else { - SAcctObj *pAcct = &tsAcctObj; - pAcct->acctId = 0; - strcpy(pAcct->user, "root"); + tsAcctObj.acctId = 0; + strcpy(tsAcctObj.user, "root"); return 0; } } SAcctObj *mgmtGetAcct(char *acctName) { if (mgmtGetAcctFp) { - return mgmtGetAcctFp(acctName); + return (*mgmtGetAcctFp)(acctName); } else { return &tsAcctObj; } } +void mgmtCleanUpAccts() { + if (mgmtCleanUpAcctsFp) { + (*mgmtCleanUpAcctsFp)(); + } +} + int32_t mgmtCheckUserLimit(SAcctObj *pAcct) { if (mgmtCheckUserLimitFp) { - return mgmtCheckUserLimitFp(pAcct); - } else { - int32_t numOfUsers = sdbGetNumOfRows(tsUserSdb); - if (numOfUsers >= tsMaxUsers) { - mWarn("numOfUsers:%d, exceed tsMaxUsers:%d", numOfUsers, tsMaxUsers); - return TSDB_CODE_TOO_MANY_USERS; - } - return 0; + return (*mgmtCheckUserLimitFp)(pAcct); } + return 0; } int32_t mgmtCheckDbLimit(SAcctObj *pAcct) { if (mgmtCheckDbLimitFp) { - return mgmtCheckDbLimitFp(pAcct); + return (*mgmtCheckDbLimitFp)(pAcct); } else { - int32_t numOfDbs = sdbGetNumOfRows(tsDbSdb); - if (numOfDbs >= tsMaxDbs) { - mWarn("numOfDbs:%d, exceed tsMaxDbs:%d", numOfDbs, tsMaxDbs); - return TSDB_CODE_TOO_MANY_DATABASES; - } return 0; } } int32_t mgmtCheckTableLimit(SAcctObj *pAcct, int32_t numOfTimeSeries) { if (mgmtCheckTimeSeriesLimitFp) { - return mgmtCheckTimeSeriesLimitFp(pAcct, numOfTimeSeries); + return (*mgmtCheckTimeSeriesLimitFp)(pAcct, numOfTimeSeries); } else { return 0; } -} - -void mgmtCleanUpAccts() { - if (mgmtCleanUpAcctsFp) { - mgmtCleanUpAcctsFp(); - } -} - -int32_t mgmtGetAcctMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { - if (mgmtGetAcctMetaFp) { - return mgmtGetAcctMetaFp(pMeta, pShow, pConn); - } else { - return TSDB_CODE_OPS_NOT_SUPPORT; - } -} - -int32_t mgmtRetrieveAccts(SShowObj *pShow, char *data, int32_t rows, void *pConn) { - if (mgmtRetrieveAcctsFp) { - return mgmtRetrieveAcctsFp(pShow, data, rows, pConn); - } else { - return 0; - } -} - -SAcctObj *mgmtGetAcctFromConn(void *pConn) { -// SRpcConnInfo connInfo; -// rpcGetConnInfo(pConn, &connInfo); -// -// SUserObj *pUser = mgmtGetUser(connInfo.user); -// if (pUser != NULL) { -// return pUser->pAcct; -// } - - return NULL; -} - +} \ No newline at end of file diff --git a/src/mnode/src/mgmtUser.c b/src/mnode/src/mgmtUser.c index 1747f0bafd..1503b721ab 100644 --- a/src/mnode/src/mgmtUser.c +++ b/src/mnode/src/mgmtUser.c @@ -18,18 +18,12 @@ #include "trpc.h" #include "tschemautil.h" #include "ttime.h" -#include "mnode.h" - - +#include "mgmtAcct.h" +#include "mgmtGrant.h" #include "mgmtMnode.h" #include "mgmtShell.h" #include "mgmtUser.h" -#include "mgmtAcct.h" -#include "mgmtGrant.h" -#include "mgmtTable.h" - - void *tsUserSdb = NULL; static int32_t tsUserUpdateSize = 0; @@ -113,6 +107,12 @@ static int32_t mgmtUpdateUser(SUserObj *pUser) { } static int32_t mgmtCreateUser(SAcctObj *pAcct, char *name, char *pass) { + int32_t numOfUsers = sdbGetNumOfRows(tsUserSdb); + if (numOfUsers >= tsMaxUsers) { + mWarn("numOfUsers:%d, exceed tsMaxUsers:%d", numOfUsers, tsMaxUsers); + return TSDB_CODE_TOO_MANY_USERS; + } + int32_t code = mgmtCheckUserLimit(pAcct); if (code != 0) { return code; From 31265ce2f85704e63cf2d72f852d2031f664658a Mon Sep 17 00:00:00 2001 From: slguan Date: Tue, 10 Mar 2020 16:55:56 +0800 Subject: [PATCH 4/5] refact mnode --- src/CMakeLists.txt | 2 +- src/common/CMakeLists.txt | 18 +- src/inc/mnode.h | 2 - src/inc/taosmsg.h | 3 + src/mnode/inc/mgmtBalance.h | 2 - src/mnode/inc/mgmtDClient.h | 1 + src/mnode/inc/mgmtDServer.h | 3 - src/mnode/inc/mgmtDb.h | 10 +- src/mnode/inc/mgmtDnode.h | 38 +- src/mnode/inc/mgmtGrant.h | 8 - src/mnode/inc/mgmtMnode.h | 11 - src/mnode/inc/mgmtProfile.h | 18 +- src/mnode/inc/mgmtShell.h | 17 - src/mnode/inc/mgmtSuperTable.h | 2 - src/mnode/inc/mgmtTable.h | 8 +- src/mnode/inc/mgmtUser.h | 1 + src/mnode/inc/mgmtVgroup.h | 5 +- src/mnode/src/mgmtBalance.c | 19 +- src/mnode/src/mgmtChildTable.c | 13 +- src/mnode/src/mgmtDClient.c | 101 +-- src/mnode/src/mgmtDServer.c | 85 +- src/mnode/src/mgmtDb.c | 180 ++++- src/mnode/src/mgmtDnode.c | 216 ++--- src/mnode/src/mgmtGrant.c | 34 - src/mnode/src/mgmtMnode.c | 147 +--- src/mnode/src/mgmtNormalTable.c | 11 +- src/mnode/src/mgmtProfile.c | 98 +++ src/mnode/src/mgmtShell.c | 1316 +++++-------------------------- src/mnode/src/mgmtSuperTable.c | 8 +- src/mnode/src/mgmtSystem.c | 22 +- src/mnode/src/mgmtTable.c | 422 +++++++++- src/mnode/src/mgmtUser.c | 3 +- src/mnode/src/mgmtVgroup.c | 31 +- 33 files changed, 1069 insertions(+), 1786 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7eba0cd736..729a47b306 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,5 +15,5 @@ ADD_SUBDIRECTORY(plugins) ADD_SUBDIRECTORY(sdb) ADD_SUBDIRECTORY(mnode) # ADD_SUBDIRECTORY(vnode) -# ADD_SUBDIRECTORY(dnode) +ADD_SUBDIRECTORY(dnode) #ADD_SUBDIRECTORY(connector/jdbc) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 5c5366c53f..6a5415a066 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -1,9 +1,11 @@ -aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SOURCE_LIST) +CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +PROJECT(TDengine) -add_library(common ${SOURCE_LIST}) -target_include_directories( - common - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc - PUBLIC ${CMAKE_SOURCE_DIR}/src/inc - PUBLIC ${CMAKE_SOURCE_DIR}/src/os/linux/inc - ) \ No newline at end of file +IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) + INCLUDE_DIRECTORIES(${TD_OS_DIR}/inc) + INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/inc) + INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/util/inc) + INCLUDE_DIRECTORIES(inc) + AUX_SOURCE_DIRECTORY(src SRC) + ADD_LIBRARY(common ${SRC}) +ENDIF () diff --git a/src/inc/mnode.h b/src/inc/mnode.h index 04ff2ff505..15f1e653c5 100644 --- a/src/inc/mnode.h +++ b/src/inc/mnode.h @@ -47,8 +47,6 @@ extern void *tsMgmtTmr; extern void *tsMgmtTranQhandle; extern char tsMgmtDirectory[]; -extern int tsDbUpdateSize; - typedef struct { uint32_t privateIp; int32_t sid; diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index 6b7b6a672c..a2efd917df 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -59,6 +59,9 @@ extern "C" { #define TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP 26 +#define TSDB_MSG_TYPE_CM_CONFIG_DNODE TSDB_MSG_TYPE_MD_CONFIG_DNODE +#define TSDB_MSG_TYPE_CM_CONFIG_DNODE_RSP TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP + #define TSDB_MSG_TYPE_DM_CONFIG_VNODE 19 #define TSDB_MSG_TYPE_DM_CONFIG_VNODE_RSP 20 diff --git a/src/mnode/inc/mgmtBalance.h b/src/mnode/inc/mgmtBalance.h index 7a6bb3a9aa..ad55e0645c 100644 --- a/src/mnode/inc/mgmtBalance.h +++ b/src/mnode/inc/mgmtBalance.h @@ -19,13 +19,11 @@ #ifdef __cplusplus extern "C" { #endif - #include "mnode.h" int32_t mgmtInitBalance(); void mgmtCleanupBalance(); int32_t mgmtAllocVnodes(SVgObj *pVgroup); -char* mgmtGetVnodeStatus(SVgObj *pVgroup, SVnodeGid *pVnode); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtDClient.h b/src/mnode/inc/mgmtDClient.h index ad86aa91e4..1bd222f4bc 100644 --- a/src/mnode/inc/mgmtDClient.h +++ b/src/mnode/inc/mgmtDClient.h @@ -23,6 +23,7 @@ extern "C" { int32_t mgmtInitDClient(); void mgmtCleanupDClient(); void mgmtAddDClientRspHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)); +void mgmtSendMsgToDnode(SRpcIpSet *ipSet, SRpcMsg *rpcMsg); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtDServer.h b/src/mnode/inc/mgmtDServer.h index b4cea21b68..5f2679f266 100644 --- a/src/mnode/inc/mgmtDServer.h +++ b/src/mnode/inc/mgmtDServer.h @@ -30,10 +30,7 @@ void mgmtAddDServerMsgHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)); //void mgmtSendCreateTableMsg(SDMCreateTableMsg *pCreate, SRpcIpSet *ipSet, void *ahandle); //void mgmtSendDropTableMsg(SMDDropTableMsg *pRemove, SRpcIpSet *ipSet, void *ahandle); //void mgmtSendAlterStreamMsg(STableInfo *pTable, SRpcIpSet *ipSet, void *ahandle); -//void mgmtSendCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode, SRpcIpSet *ipSet, void *ahandle); -//void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle); //void mgmtSendDropVnodeMsg(int32_t vnode, SRpcIpSet *ipSet, void *ahandle); -//void mgmtSendDropVgroupMsg(SVgObj *pVgroup, void *ahandle); // //int32_t mgmtInitDnodeInt(); //void mgmtCleanUpDnodeInt(); diff --git a/src/mnode/inc/mgmtDb.h b/src/mnode/inc/mgmtDb.h index b59cd6582a..32bb9d9ec6 100644 --- a/src/mnode/inc/mgmtDb.h +++ b/src/mnode/inc/mgmtDb.h @@ -22,24 +22,16 @@ extern "C" { #include "mnode.h" -void mgmtMonitorDbDrop(void *unused, void *unusedt); -int32_t mgmtAlterDb(SAcctObj *pAcct, SAlterDbMsg *pAlter); int32_t mgmtAddVgroupIntoDb(SDbObj *pDb, SVgObj *pVgroup); int32_t mgmtAddVgroupIntoDbTail(SDbObj *pDb, SVgObj *pVgroup); int32_t mgmtRemoveVgroupFromDb(SDbObj *pDb, SVgObj *pVgroup); int32_t mgmtMoveVgroupToTail(SDbObj *pDb, SVgObj *pVgroup); int32_t mgmtMoveVgroupToHead(SDbObj *pDb, SVgObj *pVgroup); -int32_t mgmtGetDbMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveDbs(SShowObj *pShow, char *data, int32_t rows, void *pConn); -void mgmtCleanUpDbs(); int32_t mgmtInitDbs(); -int32_t mgmtUpdateDb(SDbObj *pDb); +void mgmtCleanUpDbs(); SDbObj *mgmtGetDb(char *db); SDbObj *mgmtGetDbByTableId(char *db); -int32_t mgmtCreateDb(SAcctObj *pAcct, SCreateDbMsg *pCreate); -int32_t mgmtDropDbByName(SAcctObj *pAcct, char *name, short ignoreNotExists); -int32_t mgmtDropDb(SDbObj *pDb); bool mgmtCheckIsMonitorDB(char *db, char *monitordb); void mgmtAddSuperTableIntoDb(SDbObj *pDb); diff --git a/src/mnode/inc/mgmtDnode.h b/src/mnode/inc/mgmtDnode.h index 6532c98612..2fd6cb3d8d 100644 --- a/src/mnode/inc/mgmtDnode.h +++ b/src/mnode/inc/mgmtDnode.h @@ -19,46 +19,22 @@ #ifdef __cplusplus extern "C" { #endif - -#include -#include #include "mnode.h" -void mgmtSetDnodeVgid(SVnodeGid vnodeGid[], int32_t numOfVnodes, int32_t vgId); -void mgmtUnSetDnodeVgid(SVnodeGid vnodeGid[], int32_t numOfVnodes); -int32_t mgmtGetDnodeMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn); -int32_t mgmtSendCfgDnodeMsg(char *cont); -void mgmtSetDnodeMaxVnodes(SDnodeObj *pDnode); - -int32_t mgmtGetConfigMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn); - -bool mgmtCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType); -int32_t mgmtGetModuleMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn); - -int32_t mgmtGetVnodeMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn); - -int32_t mgmtGetScoresMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveScores(SShowObj *pShow, char *data, int32_t rows, void *pConn); - int32_t mgmtInitDnodes(); void mgmtCleanUpDnodes(); int32_t mgmtGetDnodesNum(); int32_t mgmtUpdateDnode(SDnodeObj *pDnode); -void* mgmtGetNextDnode(SShowObj *pShow, SDnodeObj **pDnode); -bool mgmtCheckConfigShow(SGlobalConfig *cfg); -bool mgmtCheckDnodeInRemoveState(SDnodeObj *pDnode); -bool mgmtCheckDnodeInOfflineState(SDnodeObj *pDnode); -void mgmtSetDnodeUnRemove(SDnodeObj *pDnode); SDnodeObj* mgmtGetDnode(uint32_t ip); -extern int32_t (*mgmtCreateDnodeFp)(uint32_t ip); -extern int32_t (*mgmtDropDnodeByIpFp)(uint32_t ip); - +bool mgmtCheckDnodeInRemoveState(SDnodeObj *pDnode); +bool mgmtCheckDnodeInOfflineState(SDnodeObj *pDnode); +bool mgmtCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType); +void mgmtSetDnodeUnRemove(SDnodeObj *pDnode); +void mgmtSetDnodeMaxVnodes(SDnodeObj *pDnode); void mgmtCalcNumOfFreeVnodes(SDnodeObj *pDnode); +void mgmtSetDnodeVgid(SVnodeGid vnodeGid[], int32_t numOfVnodes, int32_t vgId); +void mgmtUnSetDnodeVgid(SVnodeGid vnodeGid[], int32_t numOfVnodes); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtGrant.h b/src/mnode/inc/mgmtGrant.h index 01a5068bad..ef404f1705 100644 --- a/src/mnode/inc/mgmtGrant.h +++ b/src/mnode/inc/mgmtGrant.h @@ -19,9 +19,6 @@ #ifdef __cplusplus "C" { #endif - -#include -#include #include "mnode.h" bool mgmtCheckExpired(); @@ -30,11 +27,6 @@ void mgmtRestoreTimeSeries(SAcctObj *pAcct, uint32_t timeseries); int32_t mgmtCheckTimeSeries(uint32_t timeseries); int32_t mgmtCheckUserGrant(); int32_t mgmtCheckDbGrant(); -int32_t mgmtCheckDnodeGrant(); -int32_t mgmtGetGrantsMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveGrants(SShowObj *pShow, char *data, int32_t rows, void *pConn); - -extern void (*mgmtUpdateGrantInfoFp)(void *pCont); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtMnode.h b/src/mnode/inc/mgmtMnode.h index 703951f44b..27256d805a 100644 --- a/src/mnode/inc/mgmtMnode.h +++ b/src/mnode/inc/mgmtMnode.h @@ -20,19 +20,8 @@ extern "C" { #endif -#include -#include -#include "mnode.h" - - bool mgmtCheckRedirect(void *handle); -int32_t mgmtAddMnode(uint32_t privateIp, uint32_t publicIp); -int32_t mgmtRemoveMnode(uint32_t privateIp); - -int32_t mgmtGetMnodeMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveMnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn); - #ifdef __cplusplus } #endif diff --git a/src/mnode/inc/mgmtProfile.h b/src/mnode/inc/mgmtProfile.h index 5af38a73b8..3190466658 100644 --- a/src/mnode/inc/mgmtProfile.h +++ b/src/mnode/inc/mgmtProfile.h @@ -19,27 +19,15 @@ #ifdef __cplusplus extern "C" { #endif - #include "mnode.h" +int32_t mgmtInitProfile(); +void mgmtCleanUpProfile(); + bool mgmtCheckQhandle(uint64_t qhandle); void mgmtSaveQhandle(void *qhandle); void mgmtFreeQhandle(void *qhandle); -int32_t mgmtSaveQueryStreamList(SHeartBeatMsg *pHBMsg); -int32_t mgmtGetQueryMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveQueries(SShowObj *pShow, char *data, int32_t rows, void *pConn); - -int32_t mgmtGetStreamMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveStreams(SShowObj *pShow, char *data, int32_t rows, void *pConn); - -int32_t mgmtGetConnsMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveConns(SShowObj *pShow, char *data, int32_t rows, void *pConn); - -int32_t mgmtKillQuery(char *qidstr, void *pConn); -int32_t mgmtKillStream(char *qidstr, void *pConn); -int32_t mgmtKillConnection(char *qidstr, void *pConn); - enum { TSDB_PROCESS_CREATE_VGROUP, TSDB_PROCESS_CREATE_VGROUP_GET_META, diff --git a/src/mnode/inc/mgmtShell.h b/src/mnode/inc/mgmtShell.h index 5a924ca067..ff089dad7e 100644 --- a/src/mnode/inc/mgmtShell.h +++ b/src/mnode/inc/mgmtShell.h @@ -30,23 +30,6 @@ typedef int32_t (*SShowRetrieveFp)(SShowObj *pShow, char *data, int32_t rows, vo void mgmtAddShellShowMetaHandle(uint8_t showType, SShowMetaFp fp); void mgmtAddShellShowRetrieveHandle(uint8_t showType, SShowRetrieveFp fp); -//extern int32_t (*mgmtCheckRedirect)(void *pConn); -// -///* -// * If table not exist, will create it -// */ -//void mgmtProcessGetTableMeta(STableInfo *pTable, void *thandle); -// -///* -// * If vgroup not exist, will create vgroup -// */ -//void mgmtProcessCreateTable(SVgObj *pVgroup, SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta); -// -///* -// * If vgroup create returned, will then create table -// */ -//void mgmtProcessCreateVgroup(SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta); - #ifdef __cplusplus } #endif diff --git a/src/mnode/inc/mgmtSuperTable.h b/src/mnode/inc/mgmtSuperTable.h index 4709772e84..609e8d079f 100644 --- a/src/mnode/inc/mgmtSuperTable.h +++ b/src/mnode/inc/mgmtSuperTable.h @@ -30,8 +30,6 @@ int32_t mgmtInitSuperTables(); void mgmtCleanUpSuperTables(); void * mgmtGetSuperTable(char *tableId); -int32_t mgmtGetShowSuperTableMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn); int32_t mgmtCreateSuperTable(SDbObj *pDb, SCreateTableMsg *pCreate); int32_t mgmtDropSuperTable(SDbObj *pDb, SSuperTableObj *pTable); diff --git a/src/mnode/inc/mgmtTable.h b/src/mnode/inc/mgmtTable.h index dbd8971e2c..76b956f538 100644 --- a/src/mnode/inc/mgmtTable.h +++ b/src/mnode/inc/mgmtTable.h @@ -26,7 +26,9 @@ extern "C" { #include "mnode.h" int32_t mgmtInitTables(); +void mgmtCleanUpTables(); STableInfo* mgmtGetTable(char *tableId); + STableInfo* mgmtGetTableByPos(uint32_t dnodeIp, int32_t vnode, int32_t sid); int32_t mgmtGetTableMeta(SDbObj *pDb, STableInfo *pTable, STableMeta *pMeta, bool usePublicIp); @@ -34,9 +36,6 @@ int32_t mgmtRetrieveMetricMeta(void *pConn, char **pStart, SSuperTableMetaMsg * int32_t mgmtCreateTable(SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta); int32_t mgmtDropTable(SDbObj *pDb, char *tableId, int32_t ignore); int32_t mgmtAlterTable(SDbObj *pDb, SAlterTableMsg *pAlter); -int32_t mgmtGetShowTableMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn); -void mgmtCleanUpMeters(); void mgmtAddTableIntoSuperTable(SSuperTableObj *pStable); void mgmtRemoveTableFromSuperTable(SSuperTableObj *pStable); @@ -45,6 +44,9 @@ void mgmtSetTableDirty(STableInfo *pTable, bool isDirty); SMDDropTableMsg *mgmtBuildRemoveTableMsg(STableInfo *pTable); SDRemoveSuperTableMsg *mgmtBuildRemoveSuperTableMsg(STableInfo *pTable); +void mgmtProcessGetTableMeta(STableInfo *pTable, void *thandle); +void mgmtProcessCreateTable(SVgObj *pVgroup, SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta); +void mgmtProcessCreateVgroup(SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtUser.h b/src/mnode/inc/mgmtUser.h index a1b1d4f568..d1f927f6de 100644 --- a/src/mnode/inc/mgmtUser.h +++ b/src/mnode/inc/mgmtUser.h @@ -24,6 +24,7 @@ extern "C" { int32_t mgmtInitUsers(); void mgmtCleanUpUsers(); SUserObj *mgmtGetUser(char *name); +SUserObj *mgmtGetUserFromConn(void *pConn); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtVgroup.h b/src/mnode/inc/mgmtVgroup.h index 33a1fcad9f..975a10dc47 100644 --- a/src/mnode/inc/mgmtVgroup.h +++ b/src/mnode/inc/mgmtVgroup.h @@ -33,9 +33,6 @@ SVgObj *mgmtCreateVgroup(SDbObj *pDb); int32_t mgmtDropVgroup(SDbObj *pDb, SVgObj *pVgroup); void mgmtUpdateVgroup(SVgObj *pVgroup); -int32_t mgmtGetVgroupMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); -int32_t mgmtRetrieveVgroups(SShowObj *pShow, char *data, int32_t rows, void *pConn); - void mgmtSetVgroupIdPool(); SVgObj *mgmtGetAvailableVgroup(SDbObj *pDb); @@ -43,6 +40,8 @@ void mgmtAddTableIntoVgroup(SVgObj *pVgroup, STableInfo *pTable); void mgmtRemoveTableFromVgroup(SVgObj *pVgroup, STableInfo *pTable); SMDCreateVnodeMsg *mgmtBuildCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode); +void mgmtSendCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode, SRpcIpSet *ipSet, void *ahandle); +void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle); SRpcIpSet mgmtGetIpSetFromVgroup(SVgObj *pVgroup); SRpcIpSet mgmtGetIpSetFromIp(uint32_t ip); diff --git a/src/mnode/src/mgmtBalance.c b/src/mnode/src/mgmtBalance.c index 8bd9cf8933..81f410548e 100644 --- a/src/mnode/src/mgmtBalance.c +++ b/src/mnode/src/mgmtBalance.c @@ -14,24 +14,13 @@ */ #define _DEFAULT_SOURCE -#include "tglobalcfg.h" -#include "tmodule.h" #include "tstatus.h" -#include "ttime.h" #include "mgmtBalance.h" #include "mgmtDnode.h" -void (*mgmtStartBalanceTimerFp)(int64_t mseconds) = NULL; int32_t (*mgmtInitBalanceFp)() = NULL; void (*mgmtCleanupBalanceFp)() = NULL; int32_t (*mgmtAllocVnodesFp)(SVgObj *pVgroup) = NULL; -char * (*mgmtGetVnodeStatusFp)(SVgObj *pVgroup, SVnodeGid *pVnode) = NULL; - -void mgmtStartBalanceTimer(int64_t mseconds) { - if (mgmtStartBalanceTimerFp) { - (*mgmtStartBalanceTimerFp)(mseconds); - } -} int32_t mgmtInitBalance() { if (mgmtInitBalanceFp) { @@ -55,11 +44,11 @@ int32_t mgmtAllocVnodes(SVgObj *pVgroup) { SDnodeObj *pDnode = mgmtGetDnode(0); if (pDnode == NULL) return TSDB_CODE_OTHERS; - int selectedVnode = -1; - int lastAllocVode = pDnode->lastAllocVnode; + int32_t selectedVnode = -1; + int32_t lastAllocVode = pDnode->lastAllocVnode; - for (int i = 0; i < pDnode->numOfVnodes; i++) { - int vnode = (i + lastAllocVode) % pDnode->numOfVnodes; + for (int32_t i = 0; i < pDnode->numOfVnodes; i++) { + int32_t vnode = (i + lastAllocVode) % pDnode->numOfVnodes; if (pDnode->vload[vnode].vgId == 0 && pDnode->vload[vnode].status == TSDB_VN_STATUS_OFFLINE) { selectedVnode = vnode; break; diff --git a/src/mnode/src/mgmtChildTable.c b/src/mnode/src/mgmtChildTable.c index a214a44573..d27fa80102 100644 --- a/src/mnode/src/mgmtChildTable.c +++ b/src/mnode/src/mgmtChildTable.c @@ -28,6 +28,8 @@ #include "mgmtDb.h" #include "mgmtGrant.h" #include "mgmtProfile.h" +#include "mgmtShell.h" +#include "mgmtDClient.h" #include "mgmtSuperTable.h" #include "mgmtTable.h" #include "mgmtVgroup.h" @@ -387,7 +389,16 @@ int32_t mgmtDropChildTable(SDbObj *pDb, SChildTableObj *pTable) { } SRpcIpSet ipSet = mgmtGetIpSetFromVgroup(pVgroup); - mgmtSendDropTableMsg(pRemove, &ipSet, NULL); + + mTrace("table:%s, send drop table msg", pRemove->tableId); + SRpcMsg rpcMsg = { + .handle = 0, + .pCont = pRemove, + .contLen = sizeof(SMDDropTableMsg), + .code = 0, + .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE + }; + mgmtSendMsgToDnode(&ipSet, &rpcMsg); if (sdbDeleteRow(tsChildTableSdb, pTable) < 0) { mError("table:%s, update ctables sdb error", pTable->tableId); diff --git a/src/mnode/src/mgmtDClient.c b/src/mnode/src/mgmtDClient.c index 0f7cce405d..4670663222 100644 --- a/src/mnode/src/mgmtDClient.c +++ b/src/mnode/src/mgmtDClient.c @@ -70,6 +70,10 @@ void mgmtAddDClientRspHandle(uint8_t msgType, void (*fp)(SRpcMsg *rpcMsg)) { mgmtProcessDnodeRspFp[msgType] = fp; } +void mgmtSendMsgToDnode(SRpcIpSet *ipSet, SRpcMsg *rpcMsg) { + rpcSendRequest(tsMgmtDClientRpc, ipSet, rpcMsg); +} + static void mgmtProcessRspFromDnode(SRpcMsg *rpcMsg) { if (mgmtProcessDnodeRspFp[rpcMsg->msgType]) { (*mgmtProcessDnodeRspFp[rpcMsg->msgType])(rpcMsg); @@ -80,17 +84,7 @@ static void mgmtProcessRspFromDnode(SRpcMsg *rpcMsg) { rpcFreeCont(rpcMsg->pCont); } -//static void mgmtProcessCreateTableRsp(SRpcMsg *rpcMsg); -//static void mgmtProcessDropTableRsp(SRpcMsg *rpcMsg); -//static void mgmtProcessAlterTableRsp(SRpcMsg *rpcMsg); -//static void mgmtProcessCreateVnodeRsp(SRpcMsg *rpcMsg); -//static void mgmtProcessDropVnodeRsp(SRpcMsg *rpcMsg); -//static void mgmtProcessAlterVnodeRsp(SRpcMsg *rpcMsg); -//static void mgmtProcessDropStableRsp(SRpcMsg *rpcMsg); -//static void mgmtProcessAlterStreamRsp(SRpcMsg *rpcMsg); -//static void mgmtProcessConfigDnodeRsp(SRpcMsg *rpcMsg); -// //static void mgmtProcessCreateTableRsp(SRpcMsg *rpcMsg) { // mTrace("create table rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); // if (rpcMsg->handle == NULL) return; @@ -173,50 +167,8 @@ static void mgmtProcessRspFromDnode(SRpcMsg *rpcMsg) { // mTrace("config dnode rsp received, handle:%p code:%d", rpcMsg->handle, rpcMsg->code); //} // -//void mgmtSendCreateTableMsg(SDMCreateTableMsg *pCreate, SRpcIpSet *ipSet, void *ahandle) { -// mTrace("table:%s, send create table msg, ahandle:%p", pCreate->tableId, ahandle); -// SRpcMsg rpcMsg = { -// .handle = ahandle, -// .pCont = pCreate, -// .contLen = htonl(pCreate->contLen), -// .code = 0, -// .msgType = TSDB_MSG_TYPE_MD_CREATE_TABLE -// }; -// rpcSendRequest(tsMgmtDClientRpc, ipSet, &rpcMsg); -//} -// -//void mgmtSendDropTableMsg(SMDDropTableMsg *pDrop, SRpcIpSet *ipSet, void *ahandle) { -// mTrace("table:%s, send drop table msg, ahandle:%p", pDrop->tableId, ahandle); -// SRpcMsg rpcMsg = { -// .handle = ahandle, -// .pCont = pDrop, -// .contLen = sizeof(SMDDropTableMsg), -// .code = 0, -// .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE -// }; -// rpcSendRequest(tsMgmtDClientRpc, ipSet, &rpcMsg); -//} -// -//void mgmtSendCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode, SRpcIpSet *ipSet, void *ahandle) { -// mTrace("vgroup:%d, send create vnode:%d msg, ahandle:%p", pVgroup->vgId, vnode, ahandle); -// SMDCreateVnodeMsg *pCreate = mgmtBuildCreateVnodeMsg(pVgroup, vnode); -// SRpcMsg rpcMsg = { -// .handle = ahandle, -// .pCont = pCreate, -// .contLen = pCreate ? sizeof(SMDCreateVnodeMsg) : 0, -// .code = 0, -// .msgType = TSDB_MSG_TYPE_MD_CREATE_VNODE -// }; -// rpcSendRequest(tsMgmtDClientRpc, ipSet, &rpcMsg); -//} -// -//void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle) { -// mTrace("vgroup:%d, send create all vnodes msg, handle:%p", pVgroup->vgId, ahandle); -// for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { -// SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); -// mgmtSendCreateVnodeMsg(pVgroup, pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); -// } -//} + + // //void mgmtSendAlterStreamMsg(STableInfo *pTable, SRpcIpSet *ipSet, void *ahandle) { // mTrace("table:%s, send alter stream msg, ahandle:%p", pTable->tableId, pTable->sid, ahandle); @@ -235,13 +187,7 @@ static void mgmtProcessRspFromDnode(SRpcMsg *rpcMsg) { // rpcSendRequest(tsMgmtDClientRpc, ipSet, &rpcMsg); //} // -//void mgmtSendDropVgroupMsg(SVgObj *pVgroup, void *ahandle) { -// mTrace("vgroup:%d send free vgroup msg, ahandle:%p", pVgroup->vgId, ahandle); -// for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { -// SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); -// mgmtSendDropVnodeMsg(pVgroup->vgId, pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); -// } -//} + //// ////int32_t mgmtCfgDynamicOptions(SDnodeObj *pDnode, char *msg) { //// char *option, *value; @@ -278,36 +224,3 @@ static void mgmtProcessRspFromDnode(SRpcMsg *rpcMsg) { //// return -1; ////} //// -////int32_t mgmtSendCfgDnodeMsg(char *cont) { -//// SDnodeObj *pDnode; -//// SCfgDnodeMsg * pCfg = (SCfgDnodeMsg *)cont; -//// uint32_t ip; -//// -//// ip = inet_addr(pCfg->ip); -//// pDnode = mgmtGetDnode(ip); -//// if (pDnode == NULL) { -//// mError("dnode ip:%s not configured", pCfg->ip); -//// return TSDB_CODE_NOT_CONFIGURED; -//// } -//// -//// mTrace("dnode:%s, dynamic option received, content:%s", taosIpStr(pDnode->privateIp), pCfg->config); -//// int32_t code = mgmtCfgDynamicOptions(pDnode, pCfg->config); -//// if (code != -1) { -//// return code; -//// } -//// -////#ifdef CLUSTER -//// pStart = taosBuildReqMsg(pDnode->thandle, TSDB_MSG_TYPE_MD_CONFIG_DNODE); -//// if (pStart == NULL) return TSDB_CODE_NODE_OFFLINE; -//// pMsg = pStart; -//// -//// memcpy(pMsg, cont, sizeof(SCfgDnodeMsg)); -//// pMsg += sizeof(SCfgDnodeMsg); -//// -//// msgLen = pMsg - pStart; -//// mgmtSendMsgToDnode(pDnode, pStart, msgLen); -////#else -//// (void)tsCfgDynamicOptions(pCfg->config); -////#endif -//// return 0; -////} diff --git a/src/mnode/src/mgmtDServer.c b/src/mnode/src/mgmtDServer.c index c55731e52a..27cbf230d5 100644 --- a/src/mnode/src/mgmtDServer.c +++ b/src/mnode/src/mgmtDServer.c @@ -174,21 +174,12 @@ static int mgmtDServerRetrieveAuth(char *user, char *spi, char *encrypt, char *s // free(info); //} // -//void mgmtSendCreateTableMsg(SDMCreateTableMsg *pCreate, SRpcIpSet *ipSet, void *ahandle) { -// mTrace("table:%s, send create table msg, ahandle:%p", pCreate->tableId, ahandle); -// mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_MD_CREATE_TABLE, pCreate, htonl(pCreate->contLen), ahandle); -//} -// + //static void mgmtProcessRemoveTableRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { // mTrace("remove table rsp received, thandle:%p code:%d", thandle, code); //} // -//void mgmtSendDropTableMsg(SMDDropTableMsg *pRemove, SRpcIpSet *ipSet, void *ahandle) { -// mTrace("table:%s, sid:%d send remove table msg, ahandle:%p", pRemove->tableId, htonl(pRemove->sid), ahandle); -// if (pRemove != NULL) { -// mgmtSendMsgToDnode(ipSet, TSDB_MSG_TYPE_MD_DROP_TABLE, pRemove, sizeof(SMDDropTableMsg), ahandle); -// } -//} + // //static void mgmtProcessDropVnodeRsp(int8_t msgType, int8_t *pCont, int32_t contLen, void *thandle, int32_t code) { // mTrace("free vnode rsp received, thandle:%p code:%d", thandle, code); @@ -235,18 +226,6 @@ static int mgmtDServerRetrieveAuth(char *user, char *spi, char *encrypt, char *s // } //} // -//static void mgmtProcessDnodeGrantMsg(void *pCont, void *thandle) { -// if (mgmtUpdateGrantInfoFp) { -// mgmtUpdateGrantInfoFp(pCont); -// mTrace("grant info is updated"); -// } -// -// SRpcMsg rpcMsg = {0}; -// rpcMsg.code = TSDB_CODE_SUCCESS; -// rpcMsg.handle = thandle; -// rpcSendResponse(&rpcMsg); -//} -// //void mgmtProcessMsgFromDnode(char msgType, void *pCont, int32_t contLen, void *pConn, int32_t code) { // if (msgType < 0 || msgType >= TSDB_MSG_TYPE_MAX) { // mError("invalid msg type:%d", msgType); @@ -273,9 +252,7 @@ static int mgmtDServerRetrieveAuth(char *user, char *spi, char *encrypt, char *s // } else if (msgType == TSDB_MSG_TYPE_ALTER_STREAM_RSP) { // } else if (msgType == TSDB_MSG_TYPE_STATUS) { // mgmtProcessDnodeStatus(msgType, pCont, contLen, pConn, code); -// } else if (msgType == TSDB_MSG_TYPE_GRANT) { -// mgmtProcessDnodeGrantMsg(pCont, pConn); -// } else { +// } else { // mError("%s from dnode is not processed", taosMsg[(int8_t)msgType]); // } // @@ -296,15 +273,7 @@ static int mgmtDServerRetrieveAuth(char *user, char *spi, char *encrypt, char *s // } //} // -//void mgmtSendDropVgroupMsg(SVgObj *pVgroup, void *ahandle) { -// mTrace("vgroup:%d send free vgroup msg, ahandle:%p", pVgroup->vgId, ahandle); -// -// for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { -// SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); -// mgmtSendDropVnodeMsg(pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); -// } -//} -// + //int32_t mgmtCfgDynamicOptions(SDnodeObj *pDnode, char *msg) { // char *option, *value; // int32_t olen, valen; @@ -340,52 +309,6 @@ static int mgmtDServerRetrieveAuth(char *user, char *spi, char *encrypt, char *s // return -1; //} // -//int32_t mgmtSendCfgDnodeMsg(char *cont) { -////#ifdef CLUSTER -//// char * pMsg, *pStart; -//// int32_t msgLen = 0; -////#endif -//// -//// SDnodeObj *pDnode; -//// SCfgDnodeMsg * pCfg = (SCfgDnodeMsg *)cont; -//// uint32_t ip; -//// -//// ip = inet_addr(pCfg->ip); -//// pDnode = mgmtGetDnode(ip); -//// if (pDnode == NULL) { -//// mError("dnode ip:%s not configured", pCfg->ip); -//// return TSDB_CODE_NOT_CONFIGURED; -//// } -//// -//// mTrace("dnode:%s, dynamic option received, content:%s", taosIpStr(pDnode->privateIp), pCfg->config); -//// int32_t code = mgmtCfgDynamicOptions(pDnode, pCfg->config); -//// if (code != -1) { -//// return code; -//// } -//// -////#ifdef CLUSTER -//// pStart = taosBuildReqMsg(pDnode->thandle, TSDB_MSG_TYPE_MD_CONFIG_DNODE); -//// if (pStart == NULL) return TSDB_CODE_NODE_OFFLINE; -//// pMsg = pStart; -//// -//// memcpy(pMsg, cont, sizeof(SCfgDnodeMsg)); -//// pMsg += sizeof(SCfgDnodeMsg); -//// -//// msgLen = pMsg - pStart; -//// mgmtSendMsgToDnode(pDnode, pStart, msgLen); -////#else -//// (void)tsCfgDynamicOptions(pCfg->config); -////#endif -// return 0; -//} -// -//int32_t mgmtInitDnodeInt() { -// if (mgmtInitDnodeIntFp) { -// return mgmtInitDnodeIntFp(); -// } else { -// return 0; -// } -//} // //void mgmtCleanUpDnodeInt() { // if (mgmtCleanUpDnodeIntFp) { diff --git a/src/mnode/src/mgmtDb.c b/src/mnode/src/mgmtDb.c index efb306d8cc..750aa9a1c1 100644 --- a/src/mnode/src/mgmtDb.c +++ b/src/mnode/src/mgmtDb.c @@ -18,31 +18,43 @@ #include "taoserror.h" #include "tschemautil.h" #include "tstatus.h" +#include "tutil.h" #include "mnode.h" #include "mgmtAcct.h" #include "mgmtBalance.h" #include "mgmtDb.h" #include "mgmtDnode.h" +#include "mgmtMnode.h" #include "mgmtGrant.h" +#include "mgmtShell.h" #include "mgmtTable.h" #include "mgmtUser.h" #include "mgmtVgroup.h" -extern void *tsVgroupSdb; +static void *tsDbSdb = NULL; +static int32_t tsDbUpdateSize; -void *tsDbSdb = NULL; -int32_t tsDbUpdateSize; +static int32_t mgmtUpdateDb(SDbObj *pDb); +static int32_t mgmtCreateDb(SAcctObj *pAcct, SCreateDbMsg *pCreate); +static int32_t mgmtDropDbByName(SAcctObj *pAcct, char *name, short ignoreNotExists); +static int32_t mgmtDropDb(SDbObj *pDb); -void *(*mgmtDbActionFp[SDB_MAX_ACTION_TYPES])(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtDbActionInsert(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtDbActionDelete(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtDbActionUpdate(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtDbActionEncode(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtDbActionDecode(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtDbActionReset(void *row, char *str, int32_t size, int32_t *ssize); -void *mgmtDbActionDestroy(void *row, char *str, int32_t size, int32_t *ssize); +static int32_t mgmtGetDbMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveDbs(SShowObj *pShow, char *data, int32_t rows, void *pConn); +static void mgmtProcessCreateDbMsg(SRpcMsg *rpcMsg); +static void mgmtProcessAlterDbMsg(SRpcMsg *rpcMsg); +static void mgmtProcessDropDbMsg(SRpcMsg *rpcMsg); -void mgmtDbActionInit() { +static void *(*mgmtDbActionFp[SDB_MAX_ACTION_TYPES])(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtDbActionInsert(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtDbActionDelete(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtDbActionUpdate(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtDbActionEncode(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtDbActionDecode(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtDbActionReset(void *row, char *str, int32_t size, int32_t *ssize); +static void *mgmtDbActionDestroy(void *row, char *str, int32_t size, int32_t *ssize); + +static void mgmtDbActionInit() { mgmtDbActionFp[SDB_TYPE_INSERT] = mgmtDbActionInsert; mgmtDbActionFp[SDB_TYPE_DELETE] = mgmtDbActionDelete; mgmtDbActionFp[SDB_TYPE_UPDATE] = mgmtDbActionUpdate; @@ -52,7 +64,7 @@ void mgmtDbActionInit() { mgmtDbActionFp[SDB_TYPE_DESTROY] = mgmtDbActionDestroy; } -void *mgmtDbAction(char action, void *row, char *str, int32_t size, int32_t *ssize) { +static void *mgmtDbAction(char action, void *row, char *str, int32_t size, int32_t *ssize) { if (mgmtDbActionFp[(uint8_t)action] != NULL) { return (*(mgmtDbActionFp[(uint8_t)action]))(row, str, size, ssize); } @@ -96,6 +108,12 @@ int32_t mgmtInitDbs() { } } + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_CREATE_DB, mgmtProcessCreateDbMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_ALTER_DB, mgmtProcessAlterDbMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_DROP_DB, mgmtProcessDropDbMsg); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_DB, mgmtGetDbMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_DB, mgmtRetrieveDbs); + mTrace("db data is initialized"); return 0; } @@ -115,7 +133,7 @@ SDbObj *mgmtGetDbByTableId(char *tableId) { return (SDbObj *)sdbGetRow(tsDbSdb, db); } -int32_t mgmtCheckDBParams(SCreateDbMsg *pCreate) { +static int32_t mgmtCheckDBParams(SCreateDbMsg *pCreate) { if (pCreate->commitLog < 0 || pCreate->commitLog > 1) { mError("invalid db option commitLog: %d, only 0 or 1 allowed", pCreate->commitLog); return TSDB_CODE_INVALID_OPTION; @@ -188,7 +206,7 @@ int32_t mgmtCheckDBParams(SCreateDbMsg *pCreate) { return TSDB_CODE_SUCCESS; } -int32_t mgmtCheckDbParams(SCreateDbMsg *pCreate) { +static int32_t mgmtCheckDbParams(SCreateDbMsg *pCreate) { // assign default parameters if (pCreate->maxSessions < 0) pCreate->maxSessions = tsSessionsPerVnode; // if (pCreate->cacheBlockSize < 0) pCreate->cacheBlockSize = tsCacheBlockSize; // @@ -233,7 +251,13 @@ int32_t mgmtCheckDbParams(SCreateDbMsg *pCreate) { return TSDB_CODE_SUCCESS; } -int32_t mgmtCreateDb(SAcctObj *pAcct, SCreateDbMsg *pCreate) { +static int32_t mgmtCreateDb(SAcctObj *pAcct, SCreateDbMsg *pCreate) { + int32_t numOfDbs = sdbGetNumOfRows(tsDbSdb); + if (numOfDbs >= tsMaxDbs) { + mWarn("numOfDbs:%d, exceed tsMaxDbs:%d", numOfDbs, tsMaxDbs); + return TSDB_CODE_TOO_MANY_DATABASES; + } + int32_t code = mgmtCheckDbLimit(pAcct); if (code != 0) { return code; @@ -269,11 +293,11 @@ int32_t mgmtCreateDb(SAcctObj *pAcct, SCreateDbMsg *pCreate) { return code; } -int32_t mgmtUpdateDb(SDbObj *pDb) { +static int32_t mgmtUpdateDb(SDbObj *pDb) { return sdbUpdateRow(tsDbSdb, pDb, tsDbUpdateSize, 1); } -int32_t mgmtSetDbDropping(SDbObj *pDb) { +static int32_t mgmtSetDbDropping(SDbObj *pDb) { if (pDb->dropStatus == TSDB_DB_STATUS_DROP_FROM_SDB) return 0; SVgObj *pVgroup = pDb->pHead; @@ -294,6 +318,16 @@ int32_t mgmtSetDbDropping(SDbObj *pDb) { } } } + + //void mgmtSendDropVgroupMsg(SVgObj *pVgroup, void *ahandle) { + // mTrace("vgroup:%d send free vgroup msg, ahandle:%p", pVgroup->vgId, ahandle); + // + // for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { + // SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); + // mgmtSendDropVnodeMsg(pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); + // } + //} + // // mgmtSendDropVgroupMsg(pVgroup); pVgroup = pVgroup->next; } @@ -310,7 +344,7 @@ int32_t mgmtSetDbDropping(SDbObj *pDb) { return 0; } -bool mgmtCheckDropDbFinished(SDbObj *pDb) { +static bool mgmtCheckDropDbFinished(SDbObj *pDb) { SVgObj *pVgroup = pDb->pHead; while (pVgroup) { for (int32_t i = 0; i < pVgroup->numOfVnodes; i++) { @@ -333,7 +367,7 @@ bool mgmtCheckDropDbFinished(SDbObj *pDb) { return true; } -void mgmtDropDbFromSdb(SDbObj *pDb) { +static void mgmtDropDbFromSdb(SDbObj *pDb) { while (pDb->pHead) mgmtDropVgroup(pDb, pDb->pHead); // SSuperTableObj *pMetric = pDb->pSTable; @@ -348,13 +382,13 @@ void mgmtDropDbFromSdb(SDbObj *pDb) { mPrint("db:%s database drop finished", pDb->name); } -int32_t mgmtDropDb(SDbObj *pDb) { +static int32_t mgmtDropDb(SDbObj *pDb) { if (pDb->dropStatus == TSDB_DB_STATUS_DROPPING) { bool finished = mgmtCheckDropDbFinished(pDb); if (!finished) { SVgObj *pVgroup = pDb->pHead; while (pVgroup != NULL) { - mgmtSendDropVgroupMsg(pVgroup, NULL); + //mgmtSendDropVgroupMsg(pVgroup, NULL); pVgroup = pVgroup->next; } return TSDB_CODE_ACTION_IN_PROGRESS; @@ -371,7 +405,7 @@ int32_t mgmtDropDb(SDbObj *pDb) { } } -int32_t mgmtDropDbByName(SAcctObj *pAcct, char *name, short ignoreNotExists) { +static int32_t mgmtDropDbByName(SAcctObj *pAcct, char *name, short ignoreNotExists) { SDbObj *pDb = (SDbObj *)sdbGetRow(tsDbSdb, name); if (pDb == NULL) { if (ignoreNotExists) return TSDB_CODE_SUCCESS; @@ -394,7 +428,8 @@ bool mgmtCheckIsMonitorDB(char *db, char *monitordb) { return (strncasecmp(dbName, monitordb, len) == 0 && len == strlen(monitordb)); } -void mgmtMonitorDbDrop(void *unused, void *unusedt) { +UNUSED_FUNC +static void mgmtMonitorDbDrop(void *unused, void *unusedt) { void * pNode = NULL; SDbObj *pDb = NULL; @@ -407,7 +442,7 @@ void mgmtMonitorDbDrop(void *unused, void *unusedt) { } } -int32_t mgmtAlterDb(SAcctObj *pAcct, SAlterDbMsg *pAlter) { +static int32_t mgmtAlterDb(SAcctObj *pAcct, SAlterDbMsg *pAlter) { return 0; // int32_t code = TSDB_CODE_SUCCESS; // @@ -527,7 +562,7 @@ void mgmtCleanUpDbs() { sdbCloseTable(tsDbSdb); } -int32_t mgmtGetDbMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { +static int32_t mgmtGetDbMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { int32_t cols = 0; SSchema *pSchema = tsGetSchema(pMeta); @@ -676,7 +711,7 @@ char *mgmtGetDbStr(char *src) { return ++pos; } -int32_t mgmtRetrieveDbs(SShowObj *pShow, char *data, int32_t rows, void *pConn) { +static int32_t mgmtRetrieveDbs(SShowObj *pShow, char *data, int32_t rows, void *pConn) { int32_t numOfRows = 0; SDbObj *pDb = NULL; char * pWrite; @@ -868,3 +903,94 @@ void mgmtAddTableIntoDb(SDbObj *pDb) { void mgmtRemoveTableFromDb(SDbObj *pDb) { atomic_add_fetch_32(&pDb->numOfTables, -1); } + +static void mgmtProcessCreateDbMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle)) return; + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + SCreateDbMsg *pCreate = (SCreateDbMsg *) rpcMsg->pCont; + + pCreate->maxSessions = htonl(pCreate->maxSessions); + pCreate->cacheBlockSize = htonl(pCreate->cacheBlockSize); + pCreate->daysPerFile = htonl(pCreate->daysPerFile); + pCreate->daysToKeep = htonl(pCreate->daysToKeep); + pCreate->daysToKeep1 = htonl(pCreate->daysToKeep1); + pCreate->daysToKeep2 = htonl(pCreate->daysToKeep2); + pCreate->commitTime = htonl(pCreate->commitTime); + pCreate->blocksPerTable = htons(pCreate->blocksPerTable); + pCreate->rowsInFileBlock = htonl(pCreate->rowsInFileBlock); + // pCreate->cacheNumOfBlocks = htonl(pCreate->cacheNumOfBlocks); + + if (mgmtCheckExpired()) { + rpcRsp.code = TSDB_CODE_GRANT_EXPIRED; + } else if (!pUser->writeAuth) { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } else { + rpcRsp.code = mgmtCreateDb(pUser->pAcct, pCreate); + if (rpcRsp.code == TSDB_CODE_SUCCESS) { + mLPrint("DB:%s is created by %s", pCreate->db, pUser->user); + } + } + + rpcSendResponse(&rpcRsp); +} + +static void mgmtProcessAlterDbMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle)) return; + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + SAlterDbMsg *pAlter = (SAlterDbMsg *) rpcMsg->pCont; + pAlter->daysPerFile = htonl(pAlter->daysPerFile); + pAlter->daysToKeep = htonl(pAlter->daysToKeep); + pAlter->maxSessions = htonl(pAlter->maxSessions) + 1; + + if (!pUser->writeAuth) { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } else { + rpcRsp.code = mgmtAlterDb(pUser->pAcct, pAlter); + if (rpcRsp.code == TSDB_CODE_SUCCESS) { + mLPrint("DB:%s is altered by %s", pAlter->db, pUser->user); + } + } + + rpcSendResponse(&rpcRsp); +} + + +static void mgmtProcessDropDbMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle)) return; + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return ; + } + + if (pUser->superAuth) { + SDropDbMsg *pDrop = rpcMsg->pCont; + rpcRsp.code = mgmtDropDbByName(pUser->pAcct, pDrop->db, pDrop->ignoreNotExists); + if (rpcRsp.code == TSDB_CODE_SUCCESS) { + mLPrint("DB:%s is dropped by %s", pDrop->db, pUser->user); + } + } else { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } + + rpcSendResponse(&rpcRsp); +} diff --git a/src/mnode/src/mgmtDnode.c b/src/mnode/src/mgmtDnode.c index a9c784eca9..576b7ebf6e 100644 --- a/src/mnode/src/mgmtDnode.c +++ b/src/mnode/src/mgmtDnode.c @@ -18,9 +18,11 @@ #include "tmodule.h" #include "tschemautil.h" #include "tstatus.h" -#include "mnode.h" -#include "mgmtDnode.h" #include "mgmtBalance.h" +#include "mgmtDnode.h" +#include "mgmtDClient.h" +#include "mgmtMnode.h" +#include "mgmtShell.h" #include "mgmtUser.h" #include "mgmtVgroup.h" @@ -30,14 +32,18 @@ SDnodeObj *(*mgmtGetDnodeFp)(uint32_t ip) = NULL; int32_t (*mgmtGetDnodesNumFp)() = NULL; int32_t (*mgmtUpdateDnodeFp)(SDnodeObj *pDnode) = NULL; void * (*mgmtGetNextDnodeFp)(SShowObj *pShow, SDnodeObj **pDnode) = NULL; -int32_t (*mgmtGetScoresMetaFp)(STableMeta *pMeta, SShowObj *pShow, void *pConn) = NULL; -int32_t (*mgmtRetrieveScoresFp)(SShowObj *pShow, char *data, int32_t rows, void *pConn) = NULL; void (*mgmtSetDnodeUnRemoveFp)(SDnodeObj *pDnode) = NULL; -int32_t (*mgmtCreateDnodeFp)(uint32_t ip) = NULL; -int32_t (*mgmtDropDnodeByIpFp)(uint32_t ip) = NULL; - static SDnodeObj tsDnodeObj = {0}; +static void * mgmtGetNextDnode(SShowObj *pShow, SDnodeObj **pDnode); +static bool mgmtCheckConfigShow(SGlobalConfig *cfg); +static int32_t mgmtGetModuleMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn); +static int32_t mgmtGetConfigMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn); +static int32_t mgmtGetVnodeMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn); +static void mgmtProcessCfgDnodeMsg(SRpcMsg *rpcMsg); void mgmtSetDnodeMaxVnodes(SDnodeObj *pDnode) { int32_t maxVnodes = pDnode->numOfCores * tsNumOfVnodesPerCore; @@ -111,122 +117,6 @@ void mgmtUnSetDnodeVgid(SVnodeGid vnodeGid[], int32_t numOfVnodes) { } } -int32_t mgmtGetDnodeMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { - int32_t cols = 0; - - SUserObj *pUser = mgmtGetUserFromConn(pConn); - if (pUser == NULL) return 0; - - if (strcmp(pUser->user, "root") != 0) return TSDB_CODE_NO_RIGHTS; - - SSchema *pSchema = tsGetSchema(pMeta); - - pShow->bytes[cols] = 16; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "IP"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pShow->bytes[cols] = 8; - pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP; - strcpy(pSchema[cols].name, "created time"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pShow->bytes[cols] = 2; - pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT; - strcpy(pSchema[cols].name, "open vnodes"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pShow->bytes[cols] = 2; - pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT; - strcpy(pSchema[cols].name, "free vnodes"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pShow->bytes[cols] = 10; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "status"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pShow->bytes[cols] = 18; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "balance state"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pShow->bytes[cols] = 16; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "public ip"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pMeta->numOfColumns = htons(cols); - pShow->numOfColumns = cols; - - pShow->offset[0] = 0; - for (int32_t i = 1; i < cols; ++i) { - pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; - } - - pShow->numOfRows = mgmtGetDnodesNum(); - pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; - pShow->pNode = NULL; - - return 0; -} - -int32_t mgmtRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) { - int32_t numOfRows = 0; - int32_t cols = 0; - SDnodeObj *pDnode = NULL; - char *pWrite; - char ipstr[20]; - - while (numOfRows < rows) { - pShow->pNode = mgmtGetNextDnode(pShow, (SDnodeObj **)&pDnode); - if (pDnode == NULL) break; - - cols = 0; - - tinet_ntoa(ipstr, pDnode->privateIp); - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, ipstr); - cols++; - - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - *(int64_t *)pWrite = pDnode->createdTime; - cols++; - - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - *(int16_t *)pWrite = pDnode->openVnodes; - cols++; - - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - *(int16_t *)pWrite = pDnode->numOfFreeVnodes; - cols++; - - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, taosGetDnodeStatusStr(pDnode->status) ); - cols++; - - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, taosGetDnodeLbStatusStr(pDnode->lbStatus)); - cols++; - - tinet_ntoa(ipstr, pDnode->publicIp); - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, ipstr); - cols++; - - numOfRows++; - } - - pShow->numOfReads += numOfRows; - return numOfRows; -} bool mgmtCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { uint32_t status = pDnode->moduleStatus & (1 << moduleType); @@ -326,7 +216,7 @@ int32_t mgmtRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pCo return numOfRows; } -int32_t mgmtGetConfigMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { +static int32_t mgmtGetConfigMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { int32_t cols = 0; SUserObj *pUser = mgmtGetUserFromConn(pConn); @@ -367,7 +257,7 @@ int32_t mgmtGetConfigMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { return 0; } -int32_t mgmtRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn) { +static int32_t mgmtRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn) { int32_t numOfRows = 0; for (int32_t i = tsGlobalConfigNum - 1; i >= 0 && numOfRows < rows; --i) { @@ -414,7 +304,7 @@ int32_t mgmtRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pCo return numOfRows; } -int32_t mgmtGetVnodeMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { +static int32_t mgmtGetVnodeMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { int32_t cols = 0; SUserObj *pUser = mgmtGetUserFromConn(pConn); if (pUser == NULL) return 0; @@ -488,7 +378,7 @@ int32_t mgmtGetVnodeMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { return 0; } -int32_t mgmtRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) { +static int32_t mgmtRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) { int32_t numOfRows = 0; SDnodeObj *pDnode = NULL; char * pWrite; @@ -538,6 +428,14 @@ int32_t mgmtRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pCon } int32_t mgmtInitDnodes() { + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_MODULE, mgmtGetModuleMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_MODULE, mgmtRetrieveModules); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_CONFIGS, mgmtGetConfigMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_CONFIGS, mgmtRetrieveConfigs); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_VNODES, mgmtGetVnodeMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_VNODES, mgmtRetrieveVnodes); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_CM_CONFIG_DNODE, mgmtProcessCfgDnodeMsg); + if (mgmtInitDnodesFp) { return mgmtInitDnodesFp(); } else { @@ -607,25 +505,6 @@ void *mgmtGetNextDnode(SShowObj *pShow, SDnodeObj **pDnode) { return *pDnode; } -int32_t mgmtGetScoresMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { - if (mgmtGetScoresMetaFp) { - SUserObj *pUser = mgmtGetUserFromConn(pConn); - if (pUser == NULL) return 0; - if (strcmp(pUser->user, "root") != 0) return TSDB_CODE_NO_RIGHTS; - return mgmtGetScoresMetaFp(pMeta, pShow, pConn); - } else { - return TSDB_CODE_OPS_NOT_SUPPORT; - } -} - -int32_t mgmtRetrieveScores(SShowObj *pShow, char *data, int32_t rows, void *pConn) { - if (mgmtRetrieveScoresFp) { - return mgmtRetrieveScoresFp(pShow, data, rows, pConn); - } else { - return 0; - } -} - void mgmtSetDnodeUnRemove(SDnodeObj *pDnode) { if (mgmtSetDnodeUnRemoveFp) { mgmtSetDnodeUnRemoveFp(pDnode); @@ -640,16 +519,49 @@ bool mgmtCheckConfigShow(SGlobalConfig *cfg) { return true; } -/** - * check if a dnode in remove state - **/ bool mgmtCheckDnodeInRemoveState(SDnodeObj *pDnode) { return pDnode->lbStatus == TSDB_DN_LB_STATUS_OFFLINE_REMOVING || pDnode->lbStatus == TSDB_DN_LB_STATE_SHELL_REMOVING; } -/** - * check if a dnode in offline state - **/ bool mgmtCheckDnodeInOfflineState(SDnodeObj *pDnode) { return pDnode->status == TSDB_DN_STATUS_OFFLINE; } + +void mgmtProcessCfgDnodeMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle)) return; + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + SCMCfgDnodeMsg *pCmCfgDnode = (SCMCfgDnodeMsg *) rpcMsg->pCont; + uint32_t dnodeIp = inet_addr(pCmCfgDnode->ip); + + if (strcmp(pUser->pAcct->user, "root") != 0) { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } else { + SRpcIpSet ipSet = mgmtGetIpSetFromIp(dnodeIp); + SMDCfgDnodeMsg *pMdCfgDnode = rpcMallocCont(sizeof(SMDCfgDnodeMsg)); + strcpy(pMdCfgDnode->ip, pCmCfgDnode->ip); + strcpy(pMdCfgDnode->config, pCmCfgDnode->config); + SRpcMsg rpcMdCfgDnodeMsg = { + .handle = 0, + .code = 0, + .msgType = TSDB_MSG_TYPE_MD_CONFIG_DNODE, + .pCont = pMdCfgDnode, + .contLen = sizeof(SMDCfgDnodeMsg) + }; + mgmtSendMsgToDnode(&ipSet, &rpcMdCfgDnodeMsg); + rpcRsp.code = TSDB_CODE_SUCCESS; + } + + if (rpcRsp.code == TSDB_CODE_SUCCESS) { + mTrace("dnode:%s is configured by %s", pCmCfgDnode->ip, pUser->user); + } + + rpcSendResponse(&rpcRsp); +} diff --git a/src/mnode/src/mgmtGrant.c b/src/mnode/src/mgmtGrant.c index 0ea212b86f..be1861e45b 100644 --- a/src/mnode/src/mgmtGrant.c +++ b/src/mnode/src/mgmtGrant.c @@ -15,21 +15,14 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "mnode.h" #include "mgmtAcct.h" -#include "mgmtGrant.h" -#include "mgmtUser.h" int32_t (*mgmtCheckUserGrantFp)() = NULL; int32_t (*mgmtCheckDbGrantFp)() = NULL; -int32_t (*mgmtCheckDnodeGrantFp)() = NULL; void (*mgmtAddTimeSeriesFp)(uint32_t timeSeriesNum) = NULL; void (*mgmtRestoreTimeSeriesFp)(uint32_t timeSeriesNum) = NULL; int32_t (*mgmtCheckTimeSeriesFp)(uint32_t timeseries) = NULL; bool (*mgmtCheckExpiredFp)() = NULL; -int32_t (*mgmtGetGrantsMetaFp)(STableMeta *pMeta, SShowObj *pShow, void *pConn) = NULL; -int32_t (*mgmtRetrieveGrantsFp)(SShowObj *pShow, char *data, int rows, void *pConn) = NULL; -void (*mgmtUpdateGrantInfoFp)(void *pCont) = NULL; int32_t mgmtCheckUserGrant() { if (mgmtCheckUserGrantFp) { @@ -47,14 +40,6 @@ int32_t mgmtCheckDbGrant() { } } -int32_t mgmtCheckDnodeGrant() { - if (mgmtCheckDnodeGrantFp) { - return (*mgmtCheckDnodeGrantFp)(); - } else { - return 0; - } -} - void mgmtAddTimeSeries(SAcctObj *pAcct, uint32_t timeSeriesNum) { pAcct->acctInfo.numOfTimeSeries += timeSeriesNum; if (mgmtAddTimeSeriesFp) { @@ -84,22 +69,3 @@ bool mgmtCheckExpired() { return false; } } - -int32_t mgmtGetGrantsMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { - if (mgmtGetGrantsMetaFp) { - SUserObj *pUser = mgmtGetUserFromConn(pConn); - if (pUser == NULL) return 0; - if (strcmp(pUser->user, "root") != 0) return TSDB_CODE_NO_RIGHTS; - return mgmtGetGrantsMetaFp(pMeta, pShow, pConn); - } else { - return TSDB_CODE_OPS_NOT_SUPPORT; - } -} - -int32_t mgmtRetrieveGrants(SShowObj *pShow, char *data, int32_t rows, void *pConn) { - if (mgmtRetrieveGrantsFp) { - return mgmtRetrieveGrantsFp(pShow, data, rows, pConn); - } else { - return 0; - } -} diff --git a/src/mnode/src/mgmtMnode.c b/src/mnode/src/mgmtMnode.c index 5ed09b810f..0b8c7797d9 100644 --- a/src/mnode/src/mgmtMnode.c +++ b/src/mnode/src/mgmtMnode.c @@ -14,151 +14,8 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "trpc.h" -#include "tschemautil.h" #include "mgmtMnode.h" -#include "mgmtUser.h" - -int32_t (*mgmtAddMnodeFp)(uint32_t privateIp, uint32_t publicIp) = NULL; -int32_t (*mgmtRemoveMnodeFp)(uint32_t privateIp) = NULL; -int32_t (*mgmtGetMnodesNumFp)() = NULL; -void * (*mgmtGetNextMnodeFp)(SShowObj *pShow, SSdbPeer **pMnode) = NULL; bool mgmtCheckRedirect(void *handle) { - return false; -} - -int32_t mgmtAddMnode(uint32_t privateIp, uint32_t publicIp) { - if (mgmtAddMnodeFp) { - return (*mgmtAddMnodeFp)(privateIp, publicIp); - } else { - return 0; - } -} - -int32_t mgmtRemoveMnode(uint32_t privateIp) { - if (mgmtRemoveMnodeFp) { - return (*mgmtRemoveMnodeFp)(privateIp); - } else { - return 0; - } -} - -static int32_t mgmtGetMnodesNum() { - if (mgmtGetMnodesNumFp) { - return (*mgmtGetMnodesNumFp)(); - } else { - return 1; - } -} - -static void *mgmtGetNextMnode(SShowObj *pShow, SSdbPeer **pMnode) { - if (mgmtGetNextMnodeFp) { - return (*mgmtGetNextMnodeFp)(pShow, pMnode); - } else { - if (*pMnode == NULL) { - *pMnode = NULL; - } else { - *pMnode = NULL; - } - } - - return *pMnode; -} - -int32_t mgmtGetMnodeMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { - int32_t cols = 0; - - SUserObj *pUser = mgmtGetUserFromConn(pConn); - if (pUser == NULL) return 0; - - if (strcmp(pUser->user, "root") != 0) return TSDB_CODE_NO_RIGHTS; - - SSchema *pSchema = tsGetSchema(pMeta); - - pShow->bytes[cols] = 16; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "IP"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pShow->bytes[cols] = 8; - pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP; - strcpy(pSchema[cols].name, "created time"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pShow->bytes[cols] = 10; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "status"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pShow->bytes[cols] = 10; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "role"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pShow->bytes[cols] = 16; - pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "public ip"); - pSchema[cols].bytes = htons(pShow->bytes[cols]); - cols++; - - pMeta->numOfColumns = htons(cols); - pShow->numOfColumns = cols; - - pShow->offset[0] = 0; - for (int32_t i = 1; i < cols; ++i) { - pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; - } - - pShow->numOfRows = mgmtGetMnodesNum(); - pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; - pShow->pNode = NULL; - - return 0; -} - -int32_t mgmtRetrieveMnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) { - int32_t numOfRows = 0; - int32_t cols = 0; - SSdbPeer *pMnode = NULL; - char *pWrite; - char ipstr[20]; - - while (numOfRows < rows) { - pShow->pNode = mgmtGetNextMnode(pShow, (SSdbPeer **)&pMnode); - if (pMnode == NULL) break; - - cols = 0; - - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, pMnode->ipstr); - cols++; - - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - *(int64_t *)pWrite = pMnode->createdTime; - cols++; - - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, sdbStatusStr[(uint8_t)pMnode->status]); - cols++; - - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, sdbRoleStr[(uint8_t)pMnode->role]); - cols++; - - tinet_ntoa(ipstr, pMnode->publicIp); - pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, ipstr); - cols++; - - numOfRows++; - } - - pShow->numOfReads += numOfRows; - return numOfRows; -} + return true; +} \ No newline at end of file diff --git a/src/mnode/src/mgmtNormalTable.c b/src/mnode/src/mgmtNormalTable.c index e4cc6c9b4c..3e215888df 100644 --- a/src/mnode/src/mgmtNormalTable.c +++ b/src/mnode/src/mgmtNormalTable.c @@ -24,6 +24,7 @@ #include "mnode.h" #include "mgmtAcct.h" #include "mgmtDb.h" +#include "mgmtDClient.h" #include "mgmtGrant.h" #include "mgmtNormalTable.h" #include "mgmtSuperTable.h" @@ -422,7 +423,15 @@ int32_t mgmtDropNormalTable(SDbObj *pDb, SNormalTableObj *pTable) { } SRpcIpSet ipSet = mgmtGetIpSetFromVgroup(pVgroup); - mgmtSendDropTableMsg(pRemove, &ipSet, NULL); + mTrace("table:%s, send drop table msg", pRemove->tableId); + SRpcMsg rpcMsg = { + .handle = 0, + .pCont = pRemove, + .contLen = sizeof(SMDDropTableMsg), + .code = 0, + .msgType = TSDB_MSG_TYPE_MD_DROP_TABLE + }; + mgmtSendMsgToDnode(&ipSet, &rpcMsg); if (sdbDeleteRow(tsNormalTableSdb, pTable) < 0) { mError("table:%s, update ntables sdb error", pTable->tableId); diff --git a/src/mnode/src/mgmtProfile.c b/src/mnode/src/mgmtProfile.c index d8cc5af06b..eafa860596 100644 --- a/src/mnode/src/mgmtProfile.c +++ b/src/mnode/src/mgmtProfile.c @@ -17,7 +17,16 @@ #include "os.h" #include "taosmsg.h" #include "tschemautil.h" +#include "mgmtMnode.h" #include "mgmtProfile.h" +#include "mgmtShell.h" +#include "mgmtUser.h" + +int32_t mgmtSaveQueryStreamList(SHeartBeatMsg *pHBMsg); + +int32_t mgmtKillQuery(char *qidstr, void *pConn); +int32_t mgmtKillStream(char *qidstr, void *pConn); +int32_t mgmtKillConnection(char *qidstr, void *pConn); typedef struct { char user[TSDB_TABLE_ID_LEN + 1]; @@ -663,3 +672,92 @@ int32_t mgmtRetrieveConns(SShowObj *pShow, char *data, int32_t rows, void *pConn pShow->numOfReads += numOfRows; return numOfRows; } + +void mgmtProcessKillQueryMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle)) return; + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + SKillQueryMsg *pKill = (SKillQueryMsg *) rpcMsg->pCont; + int32_t code; + + if (!pUser->writeAuth) { + code = TSDB_CODE_NO_RIGHTS; + } else { + code = mgmtKillQuery(pKill->queryId, rpcMsg->handle); + } + + rpcRsp.code = code; + rpcSendResponse(&rpcRsp); +} + +void mgmtProcessKillStreamMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle)) return; + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + SKillStreamMsg *pKill = (SKillStreamMsg *) rpcMsg->pCont; + int32_t code; + + if (!pUser->writeAuth) { + code = TSDB_CODE_NO_RIGHTS; + } else { + code = mgmtKillStream(pKill->queryId, rpcMsg->handle); + } + + rpcRsp.code = code; + rpcSendResponse(&rpcRsp); +} + +void mgmtProcessKillConnectionMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle)) return; + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + SKillConnectionMsg *pKill = (SKillConnectionMsg *) rpcMsg->pCont; + int32_t code; + + if (!pUser->writeAuth) { + code = TSDB_CODE_NO_RIGHTS; + } else { + code = mgmtKillConnection(pKill->queryId, rpcMsg->handle); + } + + rpcRsp.code = code; + rpcSendResponse(&rpcRsp); +} + +int32_t mgmtInitProfile() { + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_QUERIES, mgmtGetQueryMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_QUERIES, mgmtRetrieveQueries); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_CONNS, mgmtGetConnsMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_CONNS, mgmtRetrieveConns); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_STREAMS, mgmtGetStreamMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_STREAMS, mgmtRetrieveStreams); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_KILL_QUERY, mgmtProcessKillQueryMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_KILL_STREAM, mgmtProcessKillStreamMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_KILL_CONNECTION, mgmtProcessKillConnectionMsg); + + return 0; +} + +void mgmtCleanUpProfile() { +} diff --git a/src/mnode/src/mgmtShell.c b/src/mnode/src/mgmtShell.c index 28706b99c0..6d02bbf18a 100644 --- a/src/mnode/src/mgmtShell.c +++ b/src/mnode/src/mgmtShell.c @@ -44,7 +44,11 @@ typedef int32_t (*SShowRetrieveFp)(SShowObj *pShow, char *data, int32_t rows, vo static void mgmtProcessMsgFromShell(SRpcMsg *pMsg); static void mgmtProcessShowMsg(SRpcMsg *rpcMsg); static void mgmtProcessRetrieveMsg(SRpcMsg *rpcMsg); +static void mgmtProcessUnSupportMsg(SRpcMsg *rpcMsg); static int mgmtShellRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey); +static bool mgmtCheckMsgReadOnly(int8_t type, void *pCont); +static void mgmtProcessHeartBeatMsg(SRpcMsg *rpcMsg); +static void mgmtProcessConnectMsg(SRpcMsg *rpcMsg); static void *tsMgmtShellRpc = NULL; static void (*tsMgmtProcessShellMsgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *) = {0}; @@ -77,6 +81,9 @@ int32_t mgmtInitShell() { return -1; } + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_HEARTBEAT, mgmtProcessHeartBeatMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_CONNECT, mgmtProcessConnectMsg); + mPrint("server connection to shell is opened"); return 0; } @@ -101,410 +108,41 @@ void mgmtAddShellShowRetrieveHandle(uint8_t msgType, SShowRetrieveFp fp) { tsMgmtShowRetrieveFp[msgType] = fp; } -static void mgmtProcessMsgFromShell(SRpcMsg *rpcMsg) { - if (tsMgmtProcessShellMsgFp[rpcMsg->msgType]) { - (*tsMgmtProcessShellMsgFp[rpcMsg->msgType])(rpcMsg); - } else { - mError("%s is not processed", taosMsg[rpcMsg->msgType]); - } - +void mgmtProcessTranRequest(SSchedMsg *sched) { + SRpcMsg *rpcMsg = sched->msg; + (*tsMgmtProcessShellMsgFp[rpcMsg->msgType])(rpcMsg); rpcFreeCont(rpcMsg->pCont); } +void mgmtAddToTranRequest(SRpcMsg *rpcMsg) { + SSchedMsg schedMsg; + schedMsg.msg = rpcMsg; + schedMsg.fp = mgmtProcessTranRequest; + taosScheduleTask(tsMgmtTranQhandle, &schedMsg); +} -//static void mgmtInitShowMsgFp(); -//static void mgmtInitProcessShellMsg(); -//static void mgmtProcessMsgFromShell(SRpcMsg *msg); -//static void (*mgmtProcessShellMsg[TSDB_MSG_TYPE_MAX])(SRpcMsg *msg); -//static void mgmtProcessUnSupportMsg(SRpcMsg *msg); -// -//void *tsMgmtShellRpc = NULL; -// -//void mgmtProcessTranRequest(SSchedMsg *sched) { -// SRpcMsg rpcMsg; -// rpcMsg.msgType = *(int8_t *) (sched->msg); -// rpcMsg.contLen = *(int32_t *) (sched->msg + sizeof(int8_t)); -// rpcMsg.pCont = sched->msg + sizeof(int32_t) + sizeof(int8_t); -// rpcMsg.handle = sched->thandle; -// rpcMsg.code = TSDB_CODE_SUCCESS; -// -// (*mgmtProcessShellMsg[rpcMsg.msgType])(&rpcMsg); -// if (sched->msg) { -// free(sched->msg); -// } -//} -// -//void mgmtAddToTranRequest(SRpcMsg *rpcMsg) { -// SSchedMsg schedMsg; -// schedMsg.msg = malloc(rpcMsg->contLen + sizeof(int32_t) + sizeof(int8_t)); -// schedMsg.fp = mgmtProcessTranRequest; -// schedMsg.tfp = NULL; -// schedMsg.thandle = rpcMsg->handle; -// *(int8_t *) (schedMsg.msg) = rpcMsg->msgType; -// *(int32_t *) (schedMsg.msg + sizeof(int8_t)) = rpcMsg->contLen; -// memcpy(schedMsg.msg + sizeof(int32_t) + sizeof(int8_t), rpcMsg->pCont, rpcMsg->contLen); -// -// taosScheduleTask(tsMgmtTranQhandle, &schedMsg); -//} -// +static void mgmtProcessMsgFromShell(SRpcMsg *rpcMsg) { + if (sdbGetRunStatus() != SDB_STATUS_SERVING) { + mTrace("shell msg is ignored since SDB is not ready"); + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = TSDB_CODE_NOT_READY, .msgType = 0}; + rpcSendResponse(&rpcRsp); + rpcFreeCont(rpcMsg->pCont); + return; + } -// -//void mgmtProcessTableMetaMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp; -// rpcRsp.handle = rpcMsg->handle; -// rpcRsp.pCont = NULL; -// rpcRsp.contLen = 0; -// -// STableInfoMsg *pInfo = rpcMsg->pCont; -// pInfo->createFlag = htons(pInfo->createFlag); -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// mError("table:%s, failed to get table meta, invalid user", pInfo->tableId); -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// STableInfo *pTable = mgmtGetTable(pInfo->tableId); -// if (pTable == NULL) { -// if (pInfo->createFlag != 1) { -// mError("table:%s, failed to get table meta, table not exist", pInfo->tableId); -// rpcRsp.code = TSDB_CODE_INVALID_TABLE; -// rpcSendResponse(&rpcRsp); -// return; -// } else { -// // on demand create table from super table if table does not exists -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// mError("table:%s, failed to create table while get meta info, need redirect message", pInfo->tableId); -// return; -// } -// -// int32_t contLen = sizeof(SCreateTableMsg) + sizeof(STagData); -// SCreateTableMsg *pCreateMsg = rpcMallocCont(contLen); -// if (pCreateMsg == NULL) { -// mError("table:%s, failed to create table while get meta info, no enough memory", pInfo->tableId); -// rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// memcpy(pCreateMsg->schema, pInfo->tags, sizeof(STagData)); -// strcpy(pCreateMsg->tableId, pInfo->tableId); -// -// mError("table:%s, start to create table while get meta info", pInfo->tableId); -// mgmtCreateTable(pCreateMsg, contLen, rpcMsg->handle, true); -// } -// } else { -// mgmtProcessGetTableMeta(pTable, rpcMsg->handle); -// } -//} -// -//void mgmtProcessMultiTableMetaMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp; -// rpcRsp.handle = rpcMsg->handle; -// rpcRsp.pCont = NULL; -// rpcRsp.contLen = 0; -// -// SRpcConnInfo connInfo; -// rpcGetConnInfo(rpcMsg->handle, &connInfo); -// -// bool usePublicIp = (connInfo.serverIp == tsPublicIpInt); -// SUserObj *pUser = mgmtGetUser(connInfo.user); -// if (pUser == NULL) { -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SMultiTableInfoMsg *pInfo = rpcMsg->pCont; -// pInfo->numOfTables = htonl(pInfo->numOfTables); -// -// int32_t totalMallocLen = 4*1024*1024; // first malloc 4 MB, subsequent reallocation as twice -// SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen); -// if (pMultiMeta == NULL) { -// rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// pMultiMeta->contLen = sizeof(SMultiTableMeta); -// pMultiMeta->numOfTables = 0; -// -// for (int t = 0; t < pInfo->numOfTables; ++t) { -// char *tableId = (char*)(pInfo->tableIds + t * TSDB_TABLE_ID_LEN); -// STableInfo *pTable = mgmtGetTable(tableId); -// if (pTable == NULL) continue; -// -// SDbObj *pDb = mgmtGetDbByTableId(tableId); -// if (pDb == NULL) continue; -// -// int availLen = totalMallocLen - pMultiMeta->contLen; -// if (availLen <= sizeof(STableMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS) { -// //TODO realloc -// //totalMallocLen *= 2; -// //pMultiMeta = rpcReMalloc(pMultiMeta, totalMallocLen); -// //if (pMultiMeta == NULL) { -// /// rpcSendResponse(ahandle, TSDB_CODE_SERV_OUT_OF_MEMORY, NULL, 0); -// // return TSDB_CODE_SERV_OUT_OF_MEMORY; -// //} else { -// // t--; -// // continue; -// //} -// } -// -// STableMeta *pMeta = (STableMeta *)(pMultiMeta->metas + pMultiMeta->contLen); -// int32_t code = mgmtGetTableMeta(pDb, pTable, pMeta, usePublicIp); -// if (code == TSDB_CODE_SUCCESS) { -// pMultiMeta->numOfTables ++; -// pMultiMeta->contLen += pMeta->contLen; -// } -// } -// -// rpcRsp.pCont = pMultiMeta; -// rpcRsp.contLen = pMultiMeta->contLen; -// rpcSendResponse(&rpcRsp); -//} -// -//void mgmtProcessSuperTableMetaMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// SSuperTableInfoMsg *pInfo = rpcMsg->pCont; -// STableInfo *pTable = mgmtGetSuperTable(pInfo->tableId); -// if (pTable == NULL) { -// rpcRsp.code = TSDB_CODE_INVALID_TABLE; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SSuperTableInfoRsp *pRsp = mgmtGetSuperTableVgroup((SSuperTableObj *) pTable); -// if (pRsp != NULL) { -// int32_t msgLen = sizeof(SSuperTableObj) + htonl(pRsp->numOfDnodes) * sizeof(int32_t); -// rpcRsp.pCont = pRsp; -// rpcRsp.contLen = msgLen; -// rpcSendResponse(&rpcRsp); -// } else { -// rpcRsp.code = TSDB_CODE_INVALID_TABLE; -// rpcSendResponse(&rpcRsp); -// } -//} -// -//void mgmtProcessCreateDbMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SCreateDbMsg *pCreate = (SCreateDbMsg *) rpcMsg->pCont; -// -// pCreate->maxSessions = htonl(pCreate->maxSessions); -// pCreate->cacheBlockSize = htonl(pCreate->cacheBlockSize); -// pCreate->daysPerFile = htonl(pCreate->daysPerFile); -// pCreate->daysToKeep = htonl(pCreate->daysToKeep); -// pCreate->daysToKeep1 = htonl(pCreate->daysToKeep1); -// pCreate->daysToKeep2 = htonl(pCreate->daysToKeep2); -// pCreate->commitTime = htonl(pCreate->commitTime); -// pCreate->blocksPerTable = htons(pCreate->blocksPerTable); -// pCreate->rowsInFileBlock = htonl(pCreate->rowsInFileBlock); -// // pCreate->cacheNumOfBlocks = htonl(pCreate->cacheNumOfBlocks); -// -// int32_t code; -// if (mgmtCheckExpired()) { -// code = TSDB_CODE_GRANT_EXPIRED; -// } else if (!pUser->writeAuth) { -// code = TSDB_CODE_NO_RIGHTS; -// } else { -// code = mgmtCreateDb(pUser->pAcct, pCreate); -// if (code == TSDB_CODE_SUCCESS) { -// mLPrint("DB:%s is created by %s", pCreate->db, pUser->user); -// } -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// -//void mgmtProcessAlterDbMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SAlterDbMsg *pAlter = (SAlterDbMsg *) rpcMsg->pCont; -// pAlter->daysPerFile = htonl(pAlter->daysPerFile); -// pAlter->daysToKeep = htonl(pAlter->daysToKeep); -// pAlter->maxSessions = htonl(pAlter->maxSessions) + 1; -// -// int32_t code; -// if (!pUser->writeAuth) { -// code = TSDB_CODE_NO_RIGHTS; -// } else { -// code = mgmtAlterDb(pUser->pAcct, pAlter); -// if (code == TSDB_CODE_SUCCESS) { -// mLPrint("DB:%s is altered by %s", pAlter->db, pUser->user); -// } -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// -//void mgmtProcessKillQueryMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SKillQueryMsg *pKill = (SKillQueryMsg *) rpcMsg->pCont; -// int32_t code; -// -// if (!pUser->writeAuth) { -// code = TSDB_CODE_NO_RIGHTS; -// } else { -// code = mgmtKillQuery(pKill->queryId, rpcMsg->handle); -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// -//void mgmtProcessKillStreamMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SKillStreamMsg *pKill = (SKillStreamMsg *) rpcMsg->pCont; -// int32_t code; -// -// if (!pUser->writeAuth) { -// code = TSDB_CODE_NO_RIGHTS; -// } else { -// code = mgmtKillStream(pKill->queryId, rpcMsg->handle); -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// -//void mgmtProcessKillConnectionMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SKillConnectionMsg *pKill = (SKillConnectionMsg *) rpcMsg->pCont; -// int32_t code; -// -// if (!pUser->writeAuth) { -// code = TSDB_CODE_NO_RIGHTS; -// } else { -// code = mgmtKillConnection(pKill->queryId, rpcMsg->handle); -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// - -//void mgmtProcessDropDbMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// return ; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return ; -// } -// -// int32_t code; -// if (pUser->superAuth) { -// SDropDbMsg *pDrop = rpcMsg->pCont; -// code = mgmtDropDbByName(pUser->pAcct, pDrop->db, pDrop->ignoreNotExists); -// if (code == TSDB_CODE_SUCCESS) { -// mLPrint("DB:%s is dropped by %s", pDrop->db, pUser->user); -// } -// } else { -// code = TSDB_CODE_NO_RIGHTS; -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// -//static void mgmtInitShowMsgFp() { -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_ACCT] = mgmtGetAcctMeta; -// tsMgmtShowMetaFp[] = ; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_DB] = mgmtGetDbMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_TABLE] = mgmtGetShowTableMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_DNODE] = mgmtGetDnodeMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_MNODE] = mgmtGetMnodeMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_VGROUP] = mgmtGetVgroupMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_METRIC] = mgmtGetShowSuperTableMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_MODULE] = mgmtGetModuleMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_QUERIES] = mgmtGetQueryMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_STREAMS] = mgmtGetStreamMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_CONFIGS] = mgmtGetConfigMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_CONNS] = mgmtGetConnsMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_SCORES] = mgmtGetScoresMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_GRANTS] = mgmtGetGrantsMeta; -// tsMgmtShowMetaFp[TSDB_MGMT_TABLE_VNODES] = mgmtGetVnodeMeta; -// -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_ACCT] = mgmtRetrieveAccts; -// tsMgmtShowRetrieveFp[] = ; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_DB] = mgmtRetrieveDbs; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_TABLE] = mgmtRetrieveShowTables; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_DNODE] = mgmtRetrieveDnodes; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_MNODE] = mgmtRetrieveMnodes; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_VGROUP] = mgmtRetrieveVgroups; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_METRIC] = mgmtRetrieveShowSuperTables; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_MODULE] = mgmtRetrieveModules; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_QUERIES] = mgmtRetrieveQueries; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_STREAMS] = mgmtRetrieveStreams; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_CONFIGS] = mgmtRetrieveConfigs; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_CONNS] = mgmtRetrieveConns; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_SCORES] = mgmtRetrieveScores; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_GRANTS] = mgmtRetrieveGrants; -// tsMgmtShowRetrieveFp[TSDB_MGMT_TABLE_VNODES] = mgmtRetrieveVnodes; -//} + if (tsMgmtProcessShellMsgFp[rpcMsg->msgType]) { + if (mgmtCheckMsgReadOnly(rpcMsg->msgType, rpcMsg->pCont)) { + (*tsMgmtProcessShellMsgFp[rpcMsg->msgType])(rpcMsg); + rpcFreeCont(rpcMsg->pCont); + } else { + mgmtAddToTranRequest(rpcMsg); + } + } else { + mError("%s is not processed", taosMsg[rpcMsg->msgType]); + mgmtProcessUnSupportMsg(rpcMsg); + rpcFreeCont(rpcMsg->pCont); + } +} static void mgmtProcessShowMsg(SRpcMsg *rpcMsg) { SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; @@ -540,16 +178,21 @@ static void mgmtProcessShowMsg(SRpcMsg *rpcMsg) { mgmtSaveQhandle(pShow); pShowRsp->qhandle = htobe64((uint64_t) pShow); - code = (*tsMgmtShowMetaFp[(uint8_t) pShowMsg->type])(&pShowRsp->tableMeta, pShow, rpcMsg->handle); - if (code == 0) { - size = sizeof(SShowRsp) + sizeof(SSchema) * pShow->numOfColumns; + if (tsMgmtShowMetaFp[pShowMsg->type]) { + code = (*tsMgmtShowMetaFp[(uint8_t) pShowMsg->type])(&pShowRsp->tableMeta, pShow, rpcMsg->handle); + if (code == 0) { + size = sizeof(SShowRsp) + sizeof(SSchema) * pShow->numOfColumns; + } else { + mError("pShow:%p, type:%d %s, failed to get Meta, code:%d", pShow, pShowMsg->type, + taosMsg[(uint8_t) pShowMsg->type], code); + free(pShow); + } } else { - mError("pShow:%p, type:%d %s, failed to get Meta, code:%d", pShow, pShowMsg->type, - taosMsg[(uint8_t) pShowMsg->type], code); - free(pShow); + code = TSDB_CODE_OPS_NOT_SUPPORT; } } + rpcRsp.code = code; rpcRsp.pCont = pShowRsp; rpcRsp.contLen = size; rpcSendResponse(&rpcRsp); @@ -621,203 +264,50 @@ static void mgmtProcessRetrieveMsg(SRpcMsg *rpcMsg) { mgmtFreeQhandle(pShow); } } -// -//void mgmtProcessCreateTableMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// -// SCreateTableMsg *pCreate = (SCreateTableMsg *) rpcMsg->pCont; -// pCreate->numOfColumns = htons(pCreate->numOfColumns); -// pCreate->numOfTags = htons(pCreate->numOfTags); -// pCreate->sqlLen = htons(pCreate->sqlLen); -// -// SSchema *pSchema = (SSchema*) pCreate->schema; -// for (int32_t i = 0; i < pCreate->numOfColumns + pCreate->numOfTags; ++i) { -// pSchema->bytes = htons(pSchema->bytes); -// pSchema->colId = i; -// pSchema++; -// } -// -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// mError("table:%s, failed to create table, need redirect message", pCreate->tableId); -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// mError("table:%s, failed to create table, invalid user", pCreate->tableId); -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// if (!pUser->writeAuth) { -// mError("table:%s, failed to create table, no rights", pCreate->tableId); -// rpcRsp.code = TSDB_CODE_NO_RIGHTS; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// int32_t code = mgmtCreateTable(pCreate, rpcMsg->contLen, rpcMsg->handle, false); -// if (code != TSDB_CODE_ACTION_IN_PROGRESS) { -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -// } -//} -// -//void mgmtProcessDropTableMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// SDropTableMsg *pDrop = (SDropTableMsg *) rpcMsg->pCont; -// -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// mError("table:%s, failed to drop table, need redirect message", pDrop->tableId); -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// mError("table:%s, failed to drop table, invalid user", pDrop->tableId); -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// if (!pUser->writeAuth) { -// mError("table:%s, failed to drop table, no rights", pDrop->tableId); -// rpcRsp.code = TSDB_CODE_NO_RIGHTS; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SDbObj *pDb = mgmtGetDbByTableId(pDrop->tableId); -// if (pDb == NULL) { -// mError("table:%s, failed to drop table, db not selected", pDrop->tableId); -// rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// int32_t code = mgmtDropTable(pDb, pDrop->tableId, pDrop->igNotExists); -// if (code != TSDB_CODE_ACTION_IN_PROGRESS) { -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -// } -//} -// -//void mgmtProcessAlterTableMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SAlterTableMsg *pAlter = (SAlterTableMsg *) rpcMsg->pCont; -// -// if (!pUser->writeAuth) { -// rpcRsp.code = TSDB_CODE_NO_RIGHTS; -// } else { -// pAlter->type = htons(pAlter->type); -// pAlter->numOfCols = htons(pAlter->numOfCols); -// -// if (pAlter->numOfCols > 2) { -// mError("table:%s error numOfCols:%d in alter table", pAlter->tableId, pAlter->numOfCols); -// rpcRsp.code = TSDB_CODE_APP_ERROR; -// } else { -// SDbObj *pDb = mgmtGetDb(pAlter->db); -// if (pDb) { -// for (int32_t i = 0; i < pAlter->numOfCols; ++i) { -// pAlter->schema[i].bytes = htons(pAlter->schema[i].bytes); -// } -// -// rpcRsp.code = mgmtAlterTable(pDb, pAlter); -// if (rpcRsp.code == 0) { -// mLPrint("table:%s is altered by %s", pAlter->tableId, pUser->user); -// } -// } else { -// rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; -// } -// } -// } -// -// rpcSendResponse(&rpcRsp); -//} -// -//void mgmtProcessCfgDnodeMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SCfgDnodeMsg *pCfg = (SCfgDnodeMsg *) rpcMsg->pCont; -// -// if (strcmp(pUser->pAcct->user, "root") != 0) { -// rpcRsp.code = TSDB_CODE_NO_RIGHTS; -// } else { -// rpcRsp.code = mgmtSendCfgDnodeMsg(rpcMsg->pCont); -// } -// -// if (rpcRsp.code == TSDB_CODE_SUCCESS) { -// mTrace("dnode:%s is configured by %s", pCfg->ip, pUser->user); -// } -// -// rpcSendResponse(&rpcRsp); -//} -// -//void mgmtProcessHeartBeatMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// SHeartBeatMsg *pHBMsg = (SHeartBeatMsg *) rpcMsg->pCont; -// mgmtSaveQueryStreamList(pHBMsg); -// -// SHeartBeatRsp *pHBRsp = (SHeartBeatRsp *) rpcMallocCont(sizeof(SHeartBeatRsp)); -// if (pHBRsp == NULL) { -// rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SRpcConnInfo connInfo; -// rpcGetConnInfo(rpcMsg->handle, &connInfo); -// -// pHBRsp->ipList.inUse = 0; -// pHBRsp->ipList.port = htons(tsMgmtShellPort); -// pHBRsp->ipList.numOfIps = 0; -// if (pSdbPublicIpList != NULL && pSdbIpList != NULL) { -// pHBRsp->ipList.numOfIps = htons(pSdbPublicIpList->numOfIps); -// if (connInfo.serverIp == tsPublicIpInt) { -// for (int i = 0; i < pSdbPublicIpList->numOfIps; ++i) { -// pHBRsp->ipList.ip[i] = htonl(pSdbPublicIpList->ip[i]); -// } -// } else { -// for (int i = 0; i < pSdbIpList->numOfIps; ++i) { -// pHBRsp->ipList.ip[i] = htonl(pSdbIpList->ip[i]); -// } -// } -// } -// -// /* -// * TODO -// * Dispose kill stream or kill query message -// */ -// pHBRsp->queryId = 0; -// pHBRsp->streamId = 0; -// pHBRsp->killConnection = 0; -// -// rpcRsp.pCont = pHBRsp; -// rpcRsp.contLen = sizeof(SHeartBeatRsp); -// rpcSendResponse(&rpcRsp); -//} + +static void mgmtProcessHeartBeatMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + //SHeartBeatMsg *pHBMsg = (SHeartBeatMsg *) rpcMsg->pCont; + //mgmtSaveQueryStreamList(pHBMsg); + + SHeartBeatRsp *pHBRsp = (SHeartBeatRsp *) rpcMallocCont(sizeof(SHeartBeatRsp)); + if (pHBRsp == NULL) { + rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; + rpcSendResponse(&rpcRsp); + return; + } + + SRpcConnInfo connInfo; + rpcGetConnInfo(rpcMsg->handle, &connInfo); + + pHBRsp->ipList.inUse = 0; + pHBRsp->ipList.port = htons(tsMgmtShellPort); + pHBRsp->ipList.numOfIps = 0; + if (pSdbPublicIpList != NULL && pSdbIpList != NULL) { + pHBRsp->ipList.numOfIps = htons(pSdbPublicIpList->numOfIps); + if (connInfo.serverIp == tsPublicIpInt) { + for (int i = 0; i < pSdbPublicIpList->numOfIps; ++i) { + pHBRsp->ipList.ip[i] = htonl(pSdbPublicIpList->ip[i]); + } + } else { + for (int i = 0; i < pSdbIpList->numOfIps; ++i) { + pHBRsp->ipList.ip[i] = htonl(pSdbIpList->ip[i]); + } + } + } + + /* + * TODO + * Dispose kill stream or kill query message + */ + pHBRsp->queryId = 0; + pHBRsp->streamId = 0; + pHBRsp->killConnection = 0; + + rpcRsp.pCont = pHBRsp; + rpcRsp.contLen = sizeof(SHeartBeatRsp); + rpcSendResponse(&rpcRsp); +} static int mgmtShellRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey) { *spi = 0; @@ -834,509 +324,119 @@ static int mgmtShellRetriveAuth(char *user, char *spi, char *encrypt, char *secr } } -//static void mgmtProcessConnectMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// SConnectMsg *pConnectMsg = (SConnectMsg *) rpcMsg->pCont; -// -// SRpcConnInfo connInfo; -// rpcGetConnInfo(rpcMsg->handle, &connInfo); -// int32_t code; -// -// SUserObj *pUser = mgmtGetUser(connInfo.user); -// if (pUser == NULL) { -// code = TSDB_CODE_INVALID_USER; -// goto connect_over; -// } -// -// if (mgmtCheckExpired()) { -// code = TSDB_CODE_GRANT_EXPIRED; -// goto connect_over; -// } -// -// SAcctObj *pAcct = mgmtGetAcct(pUser->acct); -// if (pAcct == NULL) { -// code = TSDB_CODE_INVALID_ACCT; -// goto connect_over; -// } -// -// code = taosCheckVersion(pConnectMsg->clientVersion, version, 3); -// if (code != TSDB_CODE_SUCCESS) { -// goto connect_over; -// } -// -// if (pConnectMsg->db[0]) { -// char dbName[TSDB_TABLE_ID_LEN] = {0}; -// sprintf(dbName, "%x%s%s", pAcct->acctId, TS_PATH_DELIMITER, pConnectMsg->db); -// SDbObj *pDb = mgmtGetDb(dbName); -// if (pDb == NULL) { -// code = TSDB_CODE_INVALID_DB; -// goto connect_over; -// } -// } -// -// SConnectRsp *pConnectRsp = rpcMallocCont(sizeof(SConnectRsp)); -// if (pConnectRsp == NULL) { -// code = TSDB_CODE_SERV_OUT_OF_MEMORY; -// goto connect_over; -// } -// -// sprintf(pConnectRsp->acctId, "%x", pAcct->acctId); -// strcpy(pConnectRsp->serverVersion, version); -// pConnectRsp->writeAuth = pUser->writeAuth; -// pConnectRsp->superAuth = pUser->superAuth; -// pConnectRsp->ipList.inUse = 0; -// pConnectRsp->ipList.port = htons(tsMgmtShellPort); -// pConnectRsp->ipList.numOfIps = 0; -// if (pSdbPublicIpList != NULL && pSdbIpList != NULL) { -// pConnectRsp->ipList.numOfIps = htons(pSdbPublicIpList->numOfIps); -// if (connInfo.serverIp == tsPublicIpInt) { -// for (int i = 0; i < pSdbPublicIpList->numOfIps; ++i) { -// pConnectRsp->ipList.ip[i] = htonl(pSdbPublicIpList->ip[i]); -// } -// } else { -// for (int i = 0; i < pSdbIpList->numOfIps; ++i) { -// pConnectRsp->ipList.ip[i] = htonl(pSdbIpList->ip[i]); -// } -// } -// } -// -//connect_over: -// rpcRsp.code = code; -// if (code != TSDB_CODE_SUCCESS) { -// mLError("user:%s login from %s, code:%d", connInfo.user, taosIpStr(connInfo.clientIp), code); -// } else { -// mLPrint("user:%s login from %s, code:%d", connInfo.user, taosIpStr(connInfo.clientIp), code); -// rpcRsp.pCont = pConnectRsp; -// rpcRsp.contLen = sizeof(SConnectRsp); -// } -// rpcSendResponse(&rpcRsp); -//} -// -///** -// * check if we need to add mgmtProcessTableMetaMsg into tranQueue, which will be executed one-by-one. -// */ -//static bool mgmtCheckMeterMetaMsgType(void *pMsg) { -// STableInfoMsg *pInfo = (STableInfoMsg *) pMsg; -// int16_t autoCreate = htons(pInfo->createFlag); -// STableInfo *pTable = mgmtGetTable(pInfo->tableId); -// -// // If table does not exists and autoCreate flag is set, we add the handler into task queue -// bool addIntoTranQueue = (pTable == NULL && autoCreate == 1); -// if (addIntoTranQueue) { -// mTrace("table:%s auto created task added", pInfo->tableId); -// } -// -// return addIntoTranQueue; -//} -// -//static bool mgmtCheckMsgReadOnly(int8_t type, void *pCont) { -// if ((type == TSDB_MSG_TYPE_TABLE_META && (!mgmtCheckMeterMetaMsgType(pCont))) || -// type == TSDB_MSG_TYPE_STABLE_META || type == TSDB_MSG_TYPE_RETRIEVE || -// type == TSDB_MSG_TYPE_SHOW || type == TSDB_MSG_TYPE_MULTI_TABLE_META || -// type == TSDB_MSG_TYPE_CONNECT) { -// return true; -// } -// -// return false; -//} -// -//static void mgmtProcessMsgFromShell(SRpcMsg *rpcMsg) { -// if (sdbGetRunStatus() != SDB_STATUS_SERVING) { -// mTrace("shell msg is ignored since SDB is not ready"); -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = TSDB_CODE_NOT_READY, .msgType = 0}; -// rpcSendResponse(&rpcRsp); -// rpcFreeCont(rpcMsg->pCont); -// return; -// } -// -// if (mgmtCheckMsgReadOnly(rpcMsg->msgType, rpcMsg->pCont)) { -// (*mgmtProcessShellMsg[rpcMsg->msgType])(rpcMsg); -// } else { -// if (mgmtProcessShellMsg[rpcMsg->msgType]) { -// mgmtAddToTranRequest(rpcMsg); -// } else { -// mError("%s from shell is not processed", taosMsg[rpcMsg->msgType]); -// } -// } -// -// rpcFreeCont(rpcMsg->pCont); -//} -// -//void mgmtProcessCreateVgroup(SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta) { -// SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// SDbObj *pDb = mgmtGetDb(pCreate->db); -// if (pDb == NULL) { -// mError("table:%s, failed to create vgroup, db not found", pCreate->tableId); -// rpcRsp.code = TSDB_CODE_INVALID_DB; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SVgObj *pVgroup = mgmtCreateVgroup(pDb); -// if (pVgroup == NULL) { -// mError("table:%s, failed to alloc vnode to vgroup", pCreate->tableId); -// rpcRsp.code = TSDB_CODE_NO_ENOUGH_DNODES; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// void *cont = rpcMallocCont(contLen); -// if (cont == NULL) { -// mError("table:%s, failed to create table, can not alloc memory", pCreate->tableId); -// rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// memcpy(cont, pCreate, contLen); -// -// SProcessInfo *info = calloc(1, sizeof(SProcessInfo)); -// info->type = TSDB_PROCESS_CREATE_VGROUP; -// info->thandle = thandle; -// info->ahandle = pVgroup; -// info->cont = cont; -// info->contLen = contLen; -// -// if (isGetMeta) { -// info->type = TSDB_PROCESS_CREATE_VGROUP_GET_META; -// } -// -// mgmtSendCreateVgroupMsg(pVgroup, info); -//} -// -//void mgmtProcessCreateTable(SVgObj *pVgroup, SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta) { -// assert(pVgroup != NULL); -// SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// -// int32_t sid = taosAllocateId(pVgroup->idPool); -// if (sid < 0) { -// mTrace("table:%s, no enough sid in vgroup:%d, start to create a new vgroup", pCreate->tableId, pVgroup->vgId); -// mgmtProcessCreateVgroup(pCreate, contLen, thandle, isGetMeta); -// return; -// } -// -// STableInfo *pTable; -// SDMCreateTableMsg *pDCreate = NULL; -// -// if (pCreate->numOfColumns == 0) { -// mTrace("table:%s, start to create child table, vgroup:%d sid:%d", pCreate->tableId, pVgroup->vgId, sid); -// rpcRsp.code = mgmtCreateChildTable(pCreate, contLen, pVgroup, sid, &pDCreate, &pTable); -// } else { -// mTrace("table:%s, start to create normal table, vgroup:%d sid:%d", pCreate->tableId, pVgroup->vgId, sid); -// rpcRsp.code = mgmtCreateNormalTable(pCreate, contLen, pVgroup, sid, &pDCreate, &pTable); -// } -// -// if (rpcRsp.code != TSDB_CODE_SUCCESS) { -// mTrace("table:%s, failed to create table in vgroup:%d sid:%d ", pCreate->tableId, pVgroup->vgId, sid); -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// assert(pDCreate != NULL); -// assert(pTable != NULL); -// -// SProcessInfo *info = calloc(1, sizeof(SProcessInfo)); -// info->type = TSDB_PROCESS_CREATE_TABLE; -// info->thandle = thandle; -// info->ahandle = pTable; -// SRpcIpSet ipSet = mgmtGetIpSetFromVgroup(pVgroup); -// if (isGetMeta) { -// info->type = TSDB_PROCESS_CREATE_TABLE_GET_META; -// } -// -// mgmtSendCreateTableMsg(pDCreate, &ipSet, info); -//} -// -//void mgmtProcessGetTableMeta(STableInfo *pTable, void *thandle) { -// SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// SDbObj* pDb = mgmtGetDbByTableId(pTable->tableId); -// if (pDb == NULL || pDb->dropStatus != TSDB_DB_STATUS_READY) { -// mError("table:%s, failed to get table meta, db not selected", pTable->tableId); -// rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SRpcConnInfo connInfo; -// rpcGetConnInfo(thandle, &connInfo); -// bool usePublicIp = (connInfo.serverIp == tsPublicIpInt); -// -// STableMeta *pMeta = rpcMallocCont(sizeof(STableMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS); -// rpcRsp.code = mgmtGetTableMeta(pDb, pTable, pMeta, usePublicIp); -// -// if (rpcRsp.code != TSDB_CODE_SUCCESS) { -// rpcFreeCont(pMeta); -// } else { -// pMeta->contLen = htons(pMeta->contLen); -// rpcRsp.pCont = pMeta; -// rpcRsp.contLen = pMeta->contLen; -// } -// -// rpcSendResponse(&rpcRsp); -//} -// -//static int32_t mgmtCheckRedirectMsgImp(void *pConn) { -// return 0; -//} -// -//int32_t (*mgmtCheckRedirect)(void *pConn) = mgmtCheckRedirectMsgImp; -// -//static void mgmtProcessUnSupportMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = { -// .msgType = 0, -// .pCont = 0, -// .contLen = 0, -// .code = TSDB_CODE_OPS_NOT_SUPPORT, -// .handle = rpcMsg->handle -// }; -// rpcSendResponse(&rpcRsp); -//} -// -//static void mgmtProcessAlterAcctMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (!mgmtAlterAcctFp) { -// rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SAlterAcctMsg *pAlter = rpcMsg->pCont; -// pAlter->cfg.maxUsers = htonl(pAlter->cfg.maxUsers); -// pAlter->cfg.maxDbs = htonl(pAlter->cfg.maxDbs); -// pAlter->cfg.maxTimeSeries = htonl(pAlter->cfg.maxTimeSeries); -// pAlter->cfg.maxConnections = htonl(pAlter->cfg.maxConnections); -// pAlter->cfg.maxStreams = htonl(pAlter->cfg.maxStreams); -// pAlter->cfg.maxPointsPerSecond = htonl(pAlter->cfg.maxPointsPerSecond); -// pAlter->cfg.maxStorage = htobe64(pAlter->cfg.maxStorage); -// pAlter->cfg.maxQueryTime = htobe64(pAlter->cfg.maxQueryTime); -// pAlter->cfg.maxInbound = htobe64(pAlter->cfg.maxInbound); -// pAlter->cfg.maxOutbound = htobe64(pAlter->cfg.maxOutbound); -// -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// mError("account:%s, failed to alter account, need redirect message", pAlter->user); -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// mError("account:%s, failed to alter account, invalid user", pAlter->user); -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// if (strcmp(pUser->user, "root") != 0) { -// mError("account:%s, failed to alter account, no rights", pAlter->user); -// rpcRsp.code = TSDB_CODE_NO_RIGHTS; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// int32_t code = mgmtAlterAcctFp(pAlter->user, pAlter->pass, &(pAlter->cfg));; -// if (code == TSDB_CODE_SUCCESS) { -// mLPrint("account:%s is altered by %s", pAlter->user, pUser->user); -// } else { -// mError("account:%s, failed to alter account, reason:%s", pAlter->user, tstrerror(code)); -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// -//static void mgmtProcessDropAcctMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (!mgmtDropAcctFp) { -// rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SDropAcctMsg *pDrop = (SDropAcctMsg *) rpcMsg->pCont; -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// mError("account:%s, failed to drop account, need redirect message", pDrop->user); -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// mError("account:%s, failed to drop account, invalid user", pDrop->user); -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// if (strcmp(pUser->user, "root") != 0) { -// mError("account:%s, failed to drop account, no rights", pDrop->user); -// rpcRsp.code = TSDB_CODE_NO_RIGHTS; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// int32_t code = mgmtDropAcctFp(pDrop->user); -// if (code == TSDB_CODE_SUCCESS) { -// mLPrint("account:%s is dropped by %s", pDrop->user, pUser->user); -// } else { -// mError("account:%s, failed to drop account, reason:%s", pDrop->user, tstrerror(code)); -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// -//static void mgmtProcessCreateAcctMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (!mgmtCreateAcctFp) { -// rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SCreateAcctMsg *pCreate = (SCreateAcctMsg *) rpcMsg->pCont; -// pCreate->cfg.maxUsers = htonl(pCreate->cfg.maxUsers); -// pCreate->cfg.maxDbs = htonl(pCreate->cfg.maxDbs); -// pCreate->cfg.maxTimeSeries = htonl(pCreate->cfg.maxTimeSeries); -// pCreate->cfg.maxConnections = htonl(pCreate->cfg.maxConnections); -// pCreate->cfg.maxStreams = htonl(pCreate->cfg.maxStreams); -// pCreate->cfg.maxPointsPerSecond = htonl(pCreate->cfg.maxPointsPerSecond); -// pCreate->cfg.maxStorage = htobe64(pCreate->cfg.maxStorage); -// pCreate->cfg.maxQueryTime = htobe64(pCreate->cfg.maxQueryTime); -// pCreate->cfg.maxInbound = htobe64(pCreate->cfg.maxInbound); -// pCreate->cfg.maxOutbound = htobe64(pCreate->cfg.maxOutbound); -// -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// mError("account:%s, failed to create account, need redirect message", pCreate->user); -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// mError("account:%s, failed to create account, invalid user", pCreate->user); -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// if (strcmp(pUser->user, "root") != 0) { -// mError("account:%s, failed to create account, no rights", pCreate->user); -// rpcRsp.code = TSDB_CODE_NO_RIGHTS; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// int32_t code = mgmtCreateAcctFp(pCreate->user, pCreate->pass, &(pCreate->cfg)); -// if (code == TSDB_CODE_SUCCESS) { -// mLPrint("account:%s is created by %s", pCreate->user, pUser->user); -// } else { -// mError("account:%s, failed to create account, reason:%s", pCreate->user, tstrerror(code)); -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// -//static void mgmtProcessCreateDnodeMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (!mgmtCreateDnodeFp) { -// rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SCreateDnodeMsg *pCreate = (SCreateDnodeMsg *) rpcMsg->pCont; -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// mError("failed to create dnode:%s, redirect this message", pCreate->ip); -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// mError("failed to create dnode:%s, reason:%s", pCreate->ip, tstrerror(TSDB_CODE_INVALID_USER)); -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// if (strcmp(pUser->user, "root") != 0) { -// mError("failed to create dnode:%s, reason:%s", pCreate->ip, tstrerror(TSDB_CODE_NO_RIGHTS)); -// rpcRsp.code = TSDB_CODE_NO_RIGHTS; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// int32_t code = (*mgmtCreateDnodeFp)(inet_addr(pCreate->ip)); -// if (code == TSDB_CODE_SUCCESS) { -// mLPrint("dnode:%s is created by %s", pCreate->ip, pUser->user); -// } else { -// mError("failed to create dnode:%s, reason:%s", pCreate->ip, tstrerror(code)); -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// -//static void mgmtProcessDropDnodeMsg(SRpcMsg *rpcMsg) { -// SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; -// if (!mgmtDropDnodeByIpFp) { -// rpcRsp.code = TSDB_CODE_OPS_NOT_SUPPORT; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// SDropDnodeMsg *pDrop = (SDropDnodeMsg *) rpcMsg->pCont; -// if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { -// mError("failed to drop dnode:%s, redirect this message", pDrop->ip); -// return; -// } -// -// SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); -// if (pUser == NULL) { -// mError("failed to drop dnode:%s, reason:%s", pDrop->ip, tstrerror(TSDB_CODE_INVALID_USER)); -// rpcRsp.code = TSDB_CODE_INVALID_USER; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// if (strcmp(pUser->user, "root") != 0) { -// mError("failed to drop dnode:%s, reason:%s", pDrop->ip, tstrerror(TSDB_CODE_NO_RIGHTS)); -// rpcRsp.code = TSDB_CODE_NO_RIGHTS; -// rpcSendResponse(&rpcRsp); -// return; -// } -// -// int32_t code = (*mgmtDropDnodeByIpFp)(inet_addr(pDrop->ip)); -// if (code == TSDB_CODE_SUCCESS) { -// mLPrint("dnode:%s set to removing state by %s", pDrop->ip, pUser->user); -// } else { -// mError("failed to drop dnode:%s, reason:%s", pDrop->ip, tstrerror(code)); -// } -// -// rpcRsp.code = code; -// rpcSendResponse(&rpcRsp); -//} -// -//void mgmtInitProcessShellMsg() { -// mgmtProcessShellMsg[TSDB_MSG_TYPE_CONNECT] = mgmtProcessConnectMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_HEARTBEAT] = mgmtProcessHeartBeatMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_DB] = mgmtProcessCreateDbMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_DB] = mgmtProcessAlterDbMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_DB] = mgmtProcessDropDbMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_USE_DB] = mgmtProcessUnSupportMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_ACCT] = mgmtProcessCreateAcctMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_ACCT] = mgmtProcessDropAcctMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_ACCT] = mgmtProcessAlterAcctMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_TABLE] = mgmtProcessCreateTableMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_TABLE] = mgmtProcessDropTableMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_ALTER_TABLE] = mgmtProcessAlterTableMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_DNODE] = mgmtProcessCreateDnodeMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_DNODE] = mgmtProcessDropDnodeMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_MD_CONFIG_DNODE] = mgmtProcessCfgDnodeMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_CREATE_MNODE] = mgmtProcessUnSupportMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_DROP_MNODE] = mgmtProcessUnSupportMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_CFG_MNODE] = mgmtProcessUnSupportMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_QUERY] = mgmtProcessKillQueryMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_STREAM] = mgmtProcessKillStreamMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_KILL_CONNECTION] = mgmtProcessKillConnectionMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_TABLE_META] = mgmtProcessTableMetaMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_MULTI_TABLE_META] = mgmtProcessMultiTableMetaMsg; -// mgmtProcessShellMsg[TSDB_MSG_TYPE_STABLE_META] = mgmtProcessSuperTableMetaMsg; -//} +static void mgmtProcessConnectMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + SConnectMsg *pConnectMsg = (SConnectMsg *) rpcMsg->pCont; + + SRpcConnInfo connInfo; + rpcGetConnInfo(rpcMsg->handle, &connInfo); + int32_t code; + + SUserObj *pUser = mgmtGetUser(connInfo.user); + if (pUser == NULL) { + code = TSDB_CODE_INVALID_USER; + goto connect_over; + } + + if (mgmtCheckExpired()) { + code = TSDB_CODE_GRANT_EXPIRED; + goto connect_over; + } + + SAcctObj *pAcct = mgmtGetAcct(pUser->acct); + if (pAcct == NULL) { + code = TSDB_CODE_INVALID_ACCT; + goto connect_over; + } + + code = taosCheckVersion(pConnectMsg->clientVersion, version, 3); + if (code != TSDB_CODE_SUCCESS) { + goto connect_over; + } + + if (pConnectMsg->db[0]) { + char dbName[TSDB_TABLE_ID_LEN] = {0}; + sprintf(dbName, "%x%s%s", pAcct->acctId, TS_PATH_DELIMITER, pConnectMsg->db); + SDbObj *pDb = mgmtGetDb(dbName); + if (pDb == NULL) { + code = TSDB_CODE_INVALID_DB; + goto connect_over; + } + } + + SConnectRsp *pConnectRsp = rpcMallocCont(sizeof(SConnectRsp)); + if (pConnectRsp == NULL) { + code = TSDB_CODE_SERV_OUT_OF_MEMORY; + goto connect_over; + } + + sprintf(pConnectRsp->acctId, "%x", pAcct->acctId); + strcpy(pConnectRsp->serverVersion, version); + pConnectRsp->writeAuth = pUser->writeAuth; + pConnectRsp->superAuth = pUser->superAuth; + pConnectRsp->ipList.inUse = 0; + pConnectRsp->ipList.port = htons(tsMgmtShellPort); + pConnectRsp->ipList.numOfIps = 0; + if (pSdbPublicIpList != NULL && pSdbIpList != NULL) { + pConnectRsp->ipList.numOfIps = htons(pSdbPublicIpList->numOfIps); + if (connInfo.serverIp == tsPublicIpInt) { + for (int i = 0; i < pSdbPublicIpList->numOfIps; ++i) { + pConnectRsp->ipList.ip[i] = htonl(pSdbPublicIpList->ip[i]); + } + } else { + for (int i = 0; i < pSdbIpList->numOfIps; ++i) { + pConnectRsp->ipList.ip[i] = htonl(pSdbIpList->ip[i]); + } + } + } + +connect_over: + rpcRsp.code = code; + if (code != TSDB_CODE_SUCCESS) { + mLError("user:%s login from %s, code:%d", connInfo.user, taosIpStr(connInfo.clientIp), code); + } else { + mLPrint("user:%s login from %s, code:%d", connInfo.user, taosIpStr(connInfo.clientIp), code); + rpcRsp.pCont = pConnectRsp; + rpcRsp.contLen = sizeof(SConnectRsp); + } + rpcSendResponse(&rpcRsp); +} + +/** + * check if we need to add mgmtProcessTableMetaMsg into tranQueue, which will be executed one-by-one. + */ +static bool mgmtCheckMeterMetaMsgType(void *pMsg) { + STableInfoMsg *pInfo = (STableInfoMsg *) pMsg; + int16_t autoCreate = htons(pInfo->createFlag); + STableInfo *pTable = mgmtGetTable(pInfo->tableId); + + // If table does not exists and autoCreate flag is set, we add the handler into task queue + bool addIntoTranQueue = (pTable == NULL && autoCreate == 1); + if (addIntoTranQueue) { + mTrace("table:%s auto created task added", pInfo->tableId); + } + + return addIntoTranQueue; +} + +static bool mgmtCheckMsgReadOnly(int8_t type, void *pCont) { + if ((type == TSDB_MSG_TYPE_TABLE_META && (!mgmtCheckMeterMetaMsgType(pCont))) || + type == TSDB_MSG_TYPE_STABLE_META || type == TSDB_MSG_TYPE_RETRIEVE || + type == TSDB_MSG_TYPE_SHOW || type == TSDB_MSG_TYPE_MULTI_TABLE_META || + type == TSDB_MSG_TYPE_CONNECT) { + return true; + } + + return false; +} + +static void mgmtProcessUnSupportMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = { + .msgType = 0, + .pCont = 0, + .contLen = 0, + .code = TSDB_CODE_OPS_NOT_SUPPORT, + .handle = rpcMsg->handle + }; + rpcSendResponse(&rpcRsp); +} diff --git a/src/mnode/src/mgmtSuperTable.c b/src/mnode/src/mgmtSuperTable.c index ddef464088..1d7db53a5e 100644 --- a/src/mnode/src/mgmtSuperTable.c +++ b/src/mnode/src/mgmtSuperTable.c @@ -29,6 +29,7 @@ #include "mgmtDb.h" #include "mgmtDnode.h" #include "mgmtGrant.h" +#include "mgmtShell.h" #include "mgmtSuperTable.h" #include "mgmtTable.h" #include "mgmtUser.h" @@ -45,6 +46,8 @@ static void *mgmtSuperTableActionEncode(void *row, char *str, int32_t size, int3 static void *mgmtSuperTableActionDecode(void *row, char *str, int32_t size, int32_t *ssize); static void *mgmtSuperTableActionReset(void *row, char *str, int32_t size, int32_t *ssize); static void *mgmtSuperTableActionDestroy(void *row, char *str, int32_t size, int32_t *ssize); +static int32_t mgmtRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, void *pConn); +static int32_t mgmtGetShowSuperTableMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); static void mgmtDestroySuperTable(SSuperTableObj *pTable) { free(pTable->schema); @@ -186,6 +189,9 @@ int32_t mgmtInitSuperTables() { mgmtAddSuperTableIntoDb(pDb); } + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_METRIC, mgmtGetShowSuperTableMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_METRIC, mgmtRetrieveShowSuperTables); + mTrace("stables is initialized"); return 0; } @@ -477,7 +483,7 @@ int32_t mgmtDropSuperTableColumnByName(SSuperTableObj *pStable, char *colName) { return TSDB_CODE_SUCCESS; } -int32_t mgmtGetShowSuperTableMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { +static int32_t mgmtGetShowSuperTableMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn) { SDbObj *pDb = mgmtGetDb(pShow->db); if (pDb == NULL) { return TSDB_CODE_DB_NOT_SELECTED; diff --git a/src/mnode/src/mgmtSystem.c b/src/mnode/src/mgmtSystem.c index 5265c3bcda..96e0abcb70 100644 --- a/src/mnode/src/mgmtSystem.c +++ b/src/mnode/src/mgmtSystem.c @@ -22,7 +22,9 @@ #include "mgmtAcct.h" #include "mgmtBalance.h" #include "mgmtDb.h" +#include "mgmtDClient.h" #include "mgmtDnode.h" +#include "mgmtDServer.h" #include "mgmtVgroup.h" #include "mgmtUser.h" #include "mgmtSystem.h" @@ -39,9 +41,10 @@ void mgmtCleanUpSystem() { sdbCleanUpPeers(); mgmtCleanupBalance(); - mgmtCleanUpDnodeInt(); + mgmtCleanupDClient(); + mgmtCleanupDServer(); mgmtCleanUpShell(); - mgmtCleanUpMeters(); + mgmtCleanUpTables(); mgmtCleanUpVgroups(); mgmtCleanUpDbs(); mgmtCleanUpDnodes(); @@ -114,15 +117,18 @@ int32_t mgmtStartSystem() { return -1; } - if (mgmtInitDnodeInt() < 0) { - mError("failed to init inter-mgmt communication"); + if (mgmtInitDClient() < 0) { return -1; } -// if (mgmtInitShell() < 0) { -// mError("failed to init shell"); -// return -1; -// } + if (mgmtInitDServer() < 0) { + return -1; + } + + if (mgmtInitShell() < 0) { + mError("failed to init shell"); + return -1; + } if (sdbInitPeers(tsMgmtDirectory) < 0) { mError("failed to init peers"); diff --git a/src/mnode/src/mgmtTable.c b/src/mnode/src/mgmtTable.c index 59f313bf54..c4bbd9b3f2 100644 --- a/src/mnode/src/mgmtTable.c +++ b/src/mnode/src/mgmtTable.c @@ -29,8 +29,10 @@ #include "mgmtAcct.h" #include "mgmtChildTable.h" #include "mgmtDb.h" +#include "mgmtDClient.h" #include "mgmtDnode.h" #include "mgmtGrant.h" +#include "mgmtMnode.h" #include "mgmtNormalTable.h" #include "mgmtProfile.h" #include "mgmtShell.h" @@ -42,6 +44,15 @@ extern void *tsNormalTableSdb; extern void *tsChildTableSdb; +static void mgmtProcessCreateTableMsg(SRpcMsg *rpcMsg); +static void mgmtProcessDropTableMsg(SRpcMsg *rpcMsg); +static void mgmtProcessAlterTableMsg(SRpcMsg *rpcMsg); +static void mgmtProcessTableMetaMsg(SRpcMsg *rpcMsg); +static void mgmtProcessMultiTableMetaMsg(SRpcMsg *rpcMsg); +static void mgmtProcessSuperTableMetaMsg(SRpcMsg *rpcMsg); +static int32_t mgmtGetShowTableMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, void *pConn); + int32_t mgmtInitTables() { int32_t code = mgmtInitSuperTables(); if (code != TSDB_CODE_SUCCESS) { @@ -60,6 +71,15 @@ int32_t mgmtInitTables() { mgmtSetVgroupIdPool(); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_CREATE_TABLE, mgmtProcessCreateTableMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_DROP_TABLE, mgmtProcessDropTableMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_ALTER_TABLE, mgmtProcessAlterTableMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_TABLE_META, mgmtProcessTableMetaMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_MULTI_TABLE_META, mgmtProcessMultiTableMetaMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_STABLE_META, mgmtProcessSuperTableMetaMsg); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_TABLE, mgmtGetShowTableMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_TABLE, mgmtRetrieveShowTables); + return TSDB_CODE_SUCCESS; } @@ -111,6 +131,114 @@ int32_t mgmtGetTableMeta(SDbObj *pDb, STableInfo *pTable, STableMeta *pMeta, boo return TSDB_CODE_SUCCESS; } + + +void mgmtProcessCreateVgroup(SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta) { + SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + SDbObj *pDb = mgmtGetDb(pCreate->db); + if (pDb == NULL) { + mError("table:%s, failed to create vgroup, db not found", pCreate->tableId); + rpcRsp.code = TSDB_CODE_INVALID_DB; + rpcSendResponse(&rpcRsp); + return; + } + + SVgObj *pVgroup = mgmtCreateVgroup(pDb); + if (pVgroup == NULL) { + mError("table:%s, failed to alloc vnode to vgroup", pCreate->tableId); + rpcRsp.code = TSDB_CODE_NO_ENOUGH_DNODES; + rpcSendResponse(&rpcRsp); + return; + } + + void *cont = rpcMallocCont(contLen); + if (cont == NULL) { + mError("table:%s, failed to create table, can not alloc memory", pCreate->tableId); + rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; + rpcSendResponse(&rpcRsp); + return; + } + + memcpy(cont, pCreate, contLen); + + SProcessInfo *info = calloc(1, sizeof(SProcessInfo)); + info->type = TSDB_PROCESS_CREATE_VGROUP; + info->thandle = thandle; + info->ahandle = pVgroup; + info->cont = cont; + info->contLen = contLen; + + if (isGetMeta) { + info->type = TSDB_PROCESS_CREATE_VGROUP_GET_META; + } + + mgmtSendCreateVgroupMsg(pVgroup, info); +} + +//void mgmtSendCreateTableMsg(SDMCreateTableMsg *pCreate, SRpcIpSet *ipSet, void *ahandle) { +// mTrace("table:%s, send create table msg, ahandle:%p", pCreate->tableId, ahandle); +// SRpcMsg rpcMsg = { +// .handle = ahandle, +// .pCont = pCreate, +// .contLen = htonl(pCreate->contLen), +// .code = 0, +// .msgType = TSDB_MSG_TYPE_MD_CREATE_TABLE +// }; +// rpcSendRequest(tsMgmtDClientRpc, ipSet, &rpcMsg); +//} +// + + +void mgmtProcessCreateTable(SVgObj *pVgroup, SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta) { + assert(pVgroup != NULL); + SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + + int32_t sid = taosAllocateId(pVgroup->idPool); + if (sid < 0) { + mTrace("table:%s, no enough sid in vgroup:%d, start to create a new vgroup", pCreate->tableId, pVgroup->vgId); + mgmtProcessCreateVgroup(pCreate, contLen, thandle, isGetMeta); + return; + } + + STableInfo *pTable; + SDMCreateTableMsg *pDCreate = NULL; + + if (pCreate->numOfColumns == 0) { + mTrace("table:%s, start to create child table, vgroup:%d sid:%d", pCreate->tableId, pVgroup->vgId, sid); + rpcRsp.code = mgmtCreateChildTable(pCreate, contLen, pVgroup, sid, &pDCreate, &pTable); + } else { + mTrace("table:%s, start to create normal table, vgroup:%d sid:%d", pCreate->tableId, pVgroup->vgId, sid); + rpcRsp.code = mgmtCreateNormalTable(pCreate, contLen, pVgroup, sid, &pDCreate, &pTable); + } + + if (rpcRsp.code != TSDB_CODE_SUCCESS) { + mTrace("table:%s, failed to create table in vgroup:%d sid:%d ", pCreate->tableId, pVgroup->vgId, sid); + rpcSendResponse(&rpcRsp); + return; + } + + assert(pDCreate != NULL); + assert(pTable != NULL); + + SProcessInfo *info = calloc(1, sizeof(SProcessInfo)); + info->type = TSDB_PROCESS_CREATE_TABLE; + info->thandle = thandle; + info->ahandle = pTable; + SRpcIpSet ipSet = mgmtGetIpSetFromVgroup(pVgroup); + if (isGetMeta) { + info->type = TSDB_PROCESS_CREATE_TABLE_GET_META; + } + + SRpcMsg rpcMsg = { + .handle = info, + .pCont = pCreate, + .contLen = htonl(pDCreate->contLen), + .code = 0, + .msgType = TSDB_MSG_TYPE_MD_CREATE_TABLE + }; + mgmtSendMsgToDnode(&ipSet, &rpcMsg); +} + int32_t mgmtCreateTable(SCreateTableMsg *pCreate, int32_t contLen, void *thandle, bool isGetMeta) { SDbObj *pDb = mgmtGetDb(pCreate->db); if (pDb == NULL) { @@ -246,7 +374,7 @@ int32_t mgmtAlterTable(SDbObj *pDb, SAlterTableMsg *pAlter) { return TSDB_CODE_OPS_NOT_SUPPORT; } -void mgmtCleanUpMeters() { +void mgmtCleanUpTables() { mgmtCleanUpNormalTables(); mgmtCleanUpChildTables(); mgmtCleanUpSuperTables(); @@ -422,3 +550,295 @@ void mgmtSetTableDirty(STableInfo *pTable, bool isDirty) { pTable->dirty = isDirty; } +void mgmtProcessCreateTableMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + + SCreateTableMsg *pCreate = (SCreateTableMsg *) rpcMsg->pCont; + pCreate->numOfColumns = htons(pCreate->numOfColumns); + pCreate->numOfTags = htons(pCreate->numOfTags); + pCreate->sqlLen = htons(pCreate->sqlLen); + + SSchema *pSchema = (SSchema*) pCreate->schema; + for (int32_t i = 0; i < pCreate->numOfColumns + pCreate->numOfTags; ++i) { + pSchema->bytes = htons(pSchema->bytes); + pSchema->colId = i; + pSchema++; + } + + if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { + mError("table:%s, failed to create table, need redirect message", pCreate->tableId); + return; + } + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + mError("table:%s, failed to create table, invalid user", pCreate->tableId); + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + if (!pUser->writeAuth) { + mError("table:%s, failed to create table, no rights", pCreate->tableId); + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + rpcSendResponse(&rpcRsp); + return; + } + + int32_t code = mgmtCreateTable(pCreate, rpcMsg->contLen, rpcMsg->handle, false); + if (code != TSDB_CODE_ACTION_IN_PROGRESS) { + rpcRsp.code = code; + rpcSendResponse(&rpcRsp); + } +} + +void mgmtProcessDropTableMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + SDropTableMsg *pDrop = (SDropTableMsg *) rpcMsg->pCont; + + if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { + mError("table:%s, failed to drop table, need redirect message", pDrop->tableId); + return; + } + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + mError("table:%s, failed to drop table, invalid user", pDrop->tableId); + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + if (!pUser->writeAuth) { + mError("table:%s, failed to drop table, no rights", pDrop->tableId); + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + rpcSendResponse(&rpcRsp); + return; + } + + SDbObj *pDb = mgmtGetDbByTableId(pDrop->tableId); + if (pDb == NULL) { + mError("table:%s, failed to drop table, db not selected", pDrop->tableId); + rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; + rpcSendResponse(&rpcRsp); + return; + } + + int32_t code = mgmtDropTable(pDb, pDrop->tableId, pDrop->igNotExists); + if (code != TSDB_CODE_ACTION_IN_PROGRESS) { + rpcRsp.code = code; + rpcSendResponse(&rpcRsp); + } +} + +void mgmtProcessAlterTableMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { + return; + } + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + SAlterTableMsg *pAlter = (SAlterTableMsg *) rpcMsg->pCont; + + if (!pUser->writeAuth) { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } else { + pAlter->type = htons(pAlter->type); + pAlter->numOfCols = htons(pAlter->numOfCols); + + if (pAlter->numOfCols > 2) { + mError("table:%s error numOfCols:%d in alter table", pAlter->tableId, pAlter->numOfCols); + rpcRsp.code = TSDB_CODE_APP_ERROR; + } else { + SDbObj *pDb = mgmtGetDb(pAlter->db); + if (pDb) { + for (int32_t i = 0; i < pAlter->numOfCols; ++i) { + pAlter->schema[i].bytes = htons(pAlter->schema[i].bytes); + } + + rpcRsp.code = mgmtAlterTable(pDb, pAlter); + if (rpcRsp.code == 0) { + mLPrint("table:%s is altered by %s", pAlter->tableId, pUser->user); + } + } else { + rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; + } + } + } + + rpcSendResponse(&rpcRsp); +} + +void mgmtProcessGetTableMeta(STableInfo *pTable, void *thandle) { + SRpcMsg rpcRsp = {.handle = thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + SDbObj* pDb = mgmtGetDbByTableId(pTable->tableId); + if (pDb == NULL || pDb->dropStatus != TSDB_DB_STATUS_READY) { + mError("table:%s, failed to get table meta, db not selected", pTable->tableId); + rpcRsp.code = TSDB_CODE_DB_NOT_SELECTED; + rpcSendResponse(&rpcRsp); + return; + } + + SRpcConnInfo connInfo; + rpcGetConnInfo(thandle, &connInfo); + bool usePublicIp = (connInfo.serverIp == tsPublicIpInt); + + STableMeta *pMeta = rpcMallocCont(sizeof(STableMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS); + rpcRsp.code = mgmtGetTableMeta(pDb, pTable, pMeta, usePublicIp); + + if (rpcRsp.code != TSDB_CODE_SUCCESS) { + rpcFreeCont(pMeta); + } else { + pMeta->contLen = htons(pMeta->contLen); + rpcRsp.pCont = pMeta; + rpcRsp.contLen = pMeta->contLen; + } + + rpcSendResponse(&rpcRsp); +} + + +void mgmtProcessTableMetaMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp; + rpcRsp.handle = rpcMsg->handle; + rpcRsp.pCont = NULL; + rpcRsp.contLen = 0; + + STableInfoMsg *pInfo = rpcMsg->pCont; + pInfo->createFlag = htons(pInfo->createFlag); + + SUserObj *pUser = mgmtGetUserFromConn(rpcMsg->handle); + if (pUser == NULL) { + mError("table:%s, failed to get table meta, invalid user", pInfo->tableId); + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + STableInfo *pTable = mgmtGetTable(pInfo->tableId); + if (pTable == NULL) { + if (pInfo->createFlag != 1) { + mError("table:%s, failed to get table meta, table not exist", pInfo->tableId); + rpcRsp.code = TSDB_CODE_INVALID_TABLE; + rpcSendResponse(&rpcRsp); + return; + } else { + // on demand create table from super table if table does not exists + if (mgmtCheckRedirect(rpcMsg->handle) != TSDB_CODE_SUCCESS) { + mError("table:%s, failed to create table while get meta info, need redirect message", pInfo->tableId); + return; + } + + int32_t contLen = sizeof(SCreateTableMsg) + sizeof(STagData); + SCreateTableMsg *pCreateMsg = rpcMallocCont(contLen); + if (pCreateMsg == NULL) { + mError("table:%s, failed to create table while get meta info, no enough memory", pInfo->tableId); + rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; + rpcSendResponse(&rpcRsp); + return; + } + + memcpy(pCreateMsg->schema, pInfo->tags, sizeof(STagData)); + strcpy(pCreateMsg->tableId, pInfo->tableId); + + mError("table:%s, start to create table while get meta info", pInfo->tableId); + mgmtCreateTable(pCreateMsg, contLen, rpcMsg->handle, true); + } + } else { + mgmtProcessGetTableMeta(pTable, rpcMsg->handle); + } +} + +void mgmtProcessMultiTableMetaMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp; + rpcRsp.handle = rpcMsg->handle; + rpcRsp.pCont = NULL; + rpcRsp.contLen = 0; + + SRpcConnInfo connInfo; + rpcGetConnInfo(rpcMsg->handle, &connInfo); + + bool usePublicIp = (connInfo.serverIp == tsPublicIpInt); + SUserObj *pUser = mgmtGetUser(connInfo.user); + if (pUser == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_USER; + rpcSendResponse(&rpcRsp); + return; + } + + SMultiTableInfoMsg *pInfo = rpcMsg->pCont; + pInfo->numOfTables = htonl(pInfo->numOfTables); + + int32_t totalMallocLen = 4*1024*1024; // first malloc 4 MB, subsequent reallocation as twice + SMultiTableMeta *pMultiMeta = rpcMallocCont(totalMallocLen); + if (pMultiMeta == NULL) { + rpcRsp.code = TSDB_CODE_SERV_OUT_OF_MEMORY; + rpcSendResponse(&rpcRsp); + return; + } + + pMultiMeta->contLen = sizeof(SMultiTableMeta); + pMultiMeta->numOfTables = 0; + + for (int t = 0; t < pInfo->numOfTables; ++t) { + char *tableId = (char*)(pInfo->tableIds + t * TSDB_TABLE_ID_LEN); + STableInfo *pTable = mgmtGetTable(tableId); + if (pTable == NULL) continue; + + SDbObj *pDb = mgmtGetDbByTableId(tableId); + if (pDb == NULL) continue; + + int availLen = totalMallocLen - pMultiMeta->contLen; + if (availLen <= sizeof(STableMeta) + sizeof(SSchema) * TSDB_MAX_COLUMNS) { + //TODO realloc + //totalMallocLen *= 2; + //pMultiMeta = rpcReMalloc(pMultiMeta, totalMallocLen); + //if (pMultiMeta == NULL) { + /// rpcSendResponse(ahandle, TSDB_CODE_SERV_OUT_OF_MEMORY, NULL, 0); + // return TSDB_CODE_SERV_OUT_OF_MEMORY; + //} else { + // t--; + // continue; + //} + } + + STableMeta *pMeta = (STableMeta *)(pMultiMeta->metas + pMultiMeta->contLen); + int32_t code = mgmtGetTableMeta(pDb, pTable, pMeta, usePublicIp); + if (code == TSDB_CODE_SUCCESS) { + pMultiMeta->numOfTables ++; + pMultiMeta->contLen += pMeta->contLen; + } + } + + rpcRsp.pCont = pMultiMeta; + rpcRsp.contLen = pMultiMeta->contLen; + rpcSendResponse(&rpcRsp); +} + +void mgmtProcessSuperTableMetaMsg(SRpcMsg *rpcMsg) { + SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + SSuperTableInfoMsg *pInfo = rpcMsg->pCont; + STableInfo *pTable = mgmtGetSuperTable(pInfo->tableId); + if (pTable == NULL) { + rpcRsp.code = TSDB_CODE_INVALID_TABLE; + rpcSendResponse(&rpcRsp); + return; + } + + SSuperTableInfoRsp *pRsp = mgmtGetSuperTableVgroup((SSuperTableObj *) pTable); + if (pRsp != NULL) { + int32_t msgLen = sizeof(SSuperTableObj) + htonl(pRsp->numOfDnodes) * sizeof(int32_t); + rpcRsp.pCont = pRsp; + rpcRsp.contLen = msgLen; + rpcSendResponse(&rpcRsp); + } else { + rpcRsp.code = TSDB_CODE_INVALID_TABLE; + rpcSendResponse(&rpcRsp); + } +} \ No newline at end of file diff --git a/src/mnode/src/mgmtUser.c b/src/mnode/src/mgmtUser.c index 1503b721ab..e3ea2292d8 100644 --- a/src/mnode/src/mgmtUser.c +++ b/src/mnode/src/mgmtUser.c @@ -32,7 +32,6 @@ static int32_t mgmtDropUser(SAcctObj *pAcct, char *name); static int32_t mgmtUpdateUser(SUserObj *pUser); static int32_t mgmtGetUserMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); static int32_t mgmtRetrieveUsers(SShowObj *pShow, char *data, int32_t rows, void *pConn); -static SUserObj *mgmtGetUserFromConn(void *pConn); static void mgmtProcessCreateUserMsg(SRpcMsg *rpcMsg); static void mgmtProcessAlterUserMsg(SRpcMsg *rpcMsg); @@ -329,7 +328,7 @@ static void *mgmtUserActionDestroy(void *row, char *str, int32_t size, int32_t * return NULL; } -static SUserObj *mgmtGetUserFromConn(void *pConn) { +SUserObj *mgmtGetUserFromConn(void *pConn) { SRpcConnInfo connInfo; rpcGetConnInfo(pConn, &connInfo); diff --git a/src/mnode/src/mgmtVgroup.c b/src/mnode/src/mgmtVgroup.c index 84ed20c002..f8331ec968 100644 --- a/src/mnode/src/mgmtVgroup.c +++ b/src/mnode/src/mgmtVgroup.c @@ -22,7 +22,9 @@ #include "mnode.h" #include "mgmtBalance.h" #include "mgmtDb.h" +#include "mgmtDClient.h" #include "mgmtDnode.h" +#include "mgmtShell.h" #include "mgmtTable.h" #include "mgmtVgroup.h" @@ -38,6 +40,9 @@ static void *mgmtVgroupActionDecode(void *row, char *str, int32_t size, int32_t static void *mgmtVgroupActionReset(void *row, char *str, int32_t size, int32_t *ssize); static void *mgmtVgroupActionDestroy(void *row, char *str, int32_t size, int32_t *ssize); +static int32_t mgmtGetVgroupMeta(STableMeta *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveVgroups(SShowObj *pShow, char *data, int32_t rows, void *pConn); + static void mgmtVgroupActionInit() { SVgObj tObj; tsVgUpdateSize = tObj.updateEnd - (int8_t *)&tObj; @@ -107,6 +112,9 @@ int32_t mgmtInitVgroups() { mgmtSetDnodeVgid(pVgroup->vnodeGid, pVgroup->numOfVnodes, pVgroup->vgId); } + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_VGROUP, mgmtGetVgroupMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_VGROUP, mgmtRetrieveVgroups); + mTrace("vgroup is initialized"); return 0; } @@ -192,7 +200,7 @@ int32_t mgmtDropVgroup(SDbObj *pDb, SVgObj *pVgroup) { mTrace("vgroup:%d, db:%s replica:%d is deleted", pVgroup->vgId, pDb->name, pVgroup->numOfVnodes); - mgmtSendDropVgroupMsg(pVgroup, NULL); + //mgmtSendDropVgroupMsg(pVgroup, NULL); sdbDeleteRow(tsVgroupSdb, pVgroup); @@ -560,4 +568,25 @@ SRpcIpSet mgmtGetIpSetFromVgroup(SVgObj *pVgroup) { SRpcIpSet mgmtGetIpSetFromIp(uint32_t ip) { SRpcIpSet ipSet = {.ip[0] = ip, .numOfIps = 1, .inUse = 0, .port = tsMgmtDnodePort + 1}; return ipSet; +} + +void mgmtSendCreateVnodeMsg(SVgObj *pVgroup, int32_t vnode, SRpcIpSet *ipSet, void *ahandle) { + mTrace("vgroup:%d, send create vnode:%d msg, ahandle:%p", pVgroup->vgId, vnode, ahandle); + SMDCreateVnodeMsg *pCreate = mgmtBuildCreateVnodeMsg(pVgroup, vnode); + SRpcMsg rpcMsg = { + .handle = ahandle, + .pCont = pCreate, + .contLen = pCreate ? sizeof(SMDCreateVnodeMsg) : 0, + .code = 0, + .msgType = TSDB_MSG_TYPE_MD_CREATE_VNODE + }; + mgmtSendMsgToDnode(ipSet, &rpcMsg); +} + +void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle) { + mTrace("vgroup:%d, send create all vnodes msg, handle:%p", pVgroup->vgId, ahandle); + for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { + SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].ip); + mgmtSendCreateVnodeMsg(pVgroup, pVgroup->vnodeGid[i].vnode, &ipSet, ahandle); + } } \ No newline at end of file From 82a72bf2be537c1f36a46fda9936bd18d50216a2 Mon Sep 17 00:00:00 2001 From: slguan Date: Tue, 10 Mar 2020 17:41:25 +0800 Subject: [PATCH 5/5] fix compile errors --- src/client/src/tscSQLParser.c | 2 +- src/client/src/tscServer.c | 2 +- src/dnode/CMakeLists.txt | 2 +- src/dnode/src/dnodeMgmt.c | 184 +++++++++++++++++----------------- src/mnode/src/mgmtMnode.c | 1 + src/mnode/src/mgmtShell.c | 2 +- 6 files changed, 97 insertions(+), 96 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 92323dcdfe..25df797c04 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -395,7 +395,7 @@ int32_t tscToSQLCmd(SSqlObj* pSql, struct SSqlInfo* pInfo) { char* pMsg = pCmd->payload + tsRpcHeadSize; pMsg += sizeof(SMgmtHead); - SCfgDnodeMsg* pCfg = (SCfgDnodeMsg*)pMsg; + SCMCfgDnodeMsg* pCfg = (SCMCfgDnodeMsg*)pMsg; strncpy(pCfg->ip, pDCL->a[0].z, pDCL->a[0].n); strncpy(pCfg->config, pDCL->a[1].z, pDCL->a[1].n); diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 31e16792b6..68faf08dd6 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1795,7 +1795,7 @@ int32_t tscBuildUserMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int32_t tscBuildCfgDnodeMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd *pCmd = &pSql->cmd; - pCmd->payloadLen = sizeof(SCfgDnodeMsg); + pCmd->payloadLen = sizeof(SCMCfgDnodeMsg); if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, pCmd->payloadLen)) { tscError("%p failed to malloc for query msg", pSql); diff --git a/src/dnode/CMakeLists.txt b/src/dnode/CMakeLists.txt index 712f81656a..d4901845fa 100644 --- a/src/dnode/CMakeLists.txt +++ b/src/dnode/CMakeLists.txt @@ -7,7 +7,7 @@ IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/util/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/query/inc) INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/vnode/tsdb/inc) - INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/vnode/common/inc) + INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/common/inc) INCLUDE_DIRECTORIES(${TD_ENTERPRISE_DIR}/src/inc) INCLUDE_DIRECTORIES(inc) AUX_SOURCE_DIRECTORY(src SRC) diff --git a/src/dnode/src/dnodeMgmt.c b/src/dnode/src/dnodeMgmt.c index 7a5bc66a13..e8fe94f358 100644 --- a/src/dnode/src/dnodeMgmt.c +++ b/src/dnode/src/dnodeMgmt.c @@ -21,7 +21,7 @@ #include "tlog.h" #include "trpc.h" #include "tstatus.h" -//#include "tsdb.h" +#include "tsdb.h" #include "dnodeMgmt.h" #include "dnodeRead.h" #include "dnodeWrite.h" @@ -139,111 +139,111 @@ static void dnodeCleanupVnodes() { } static int32_t dnodeOpenVnode(int32_t vgId) { -// char rootDir[TSDB_FILENAME_LEN] = {0}; -// sprintf(rootDir, "%s/vnode%d", tsDirectory, vgId); -// -// void *pTsdb = tsdbOpenRepo(rootDir); -// if (pTsdb != NULL) { -// return terrno; -// } -// -// SVnodeObj vnodeObj; -// vnodeObj.vgId = vgId; -// vnodeObj.status = TSDB_VN_STATUS_NOT_READY; -// vnodeObj.refCount = 1; -// vnodeObj.version = 0; -// vnodeObj.wworker = dnodeAllocateWriteWorker(); -// vnodeObj.rworker = dnodeAllocateReadWorker(); -// vnodeObj.wal = NULL; -// vnodeObj.tsdb = pTsdb; -// vnodeObj.replica = NULL; -// vnodeObj.events = NULL; -// vnodeObj.cq = NULL; -// -// taosAddIntHash(tsDnodeVnodesHash, vnodeObj.vgId, &vnodeObj); + char rootDir[TSDB_FILENAME_LEN] = {0}; + sprintf(rootDir, "%s/vnode%d", tsDirectory, vgId); + + void *pTsdb = tsdbOpenRepo(rootDir); + if (pTsdb != NULL) { + return terrno; + } + + SVnodeObj vnodeObj; + vnodeObj.vgId = vgId; + vnodeObj.status = TSDB_VN_STATUS_NOT_READY; + vnodeObj.refCount = 1; + vnodeObj.version = 0; + vnodeObj.wworker = dnodeAllocateWriteWorker(); + vnodeObj.rworker = dnodeAllocateReadWorker(); + vnodeObj.wal = NULL; + vnodeObj.tsdb = pTsdb; + vnodeObj.replica = NULL; + vnodeObj.events = NULL; + vnodeObj.cq = NULL; + + taosAddIntHash(tsDnodeVnodesHash, vnodeObj.vgId, &vnodeObj); return TSDB_CODE_SUCCESS; } static void dnodeCleanupVnode(SVnodeObj *pVnode) { -// pVnode->status = TSDB_VN_STATUS_NOT_READY; -// int32_t count = atomic_sub_fetch_32(&pVnode->refCount, 1); -// if (count > 0) { -// // wait refcount -// } -// -// // remove replica -// -// // remove read queue -// dnodeFreeReadWorker(pVnode->rworker); -// pVnode->rworker = NULL; -// -// // remove write queue -// dnodeFreeWriteWorker(pVnode->wworker); -// pVnode->wworker = NULL; -// -// // remove wal -// -// // remove tsdb -// if (pVnode->tsdb) { -// tsdbCloseRepo(pVnode->tsdb); -// pVnode->tsdb = NULL; -// } -// -// taosDeleteIntHash(tsDnodeVnodesHash, pVnode->vgId); + pVnode->status = TSDB_VN_STATUS_NOT_READY; + int32_t count = atomic_sub_fetch_32(&pVnode->refCount, 1); + if (count > 0) { + // wait refcount + } + + // remove replica + + // remove read queue + dnodeFreeReadWorker(pVnode->rworker); + pVnode->rworker = NULL; + + // remove write queue + dnodeFreeWriteWorker(pVnode->wworker); + pVnode->wworker = NULL; + + // remove wal + + // remove tsdb + if (pVnode->tsdb) { + tsdbCloseRepo(pVnode->tsdb); + pVnode->tsdb = NULL; + } + + taosDeleteIntHash(tsDnodeVnodesHash, pVnode->vgId); } static int32_t dnodeCreateVnode(SMDCreateVnodeMsg *pVnodeCfg) { -// STsdbCfg tsdbCfg; -// tsdbCfg.precision = pVnodeCfg->cfg.precision; -// tsdbCfg.tsdbId = pVnodeCfg->vnode; -// tsdbCfg.maxTables = pVnodeCfg->cfg.maxSessions; -// tsdbCfg.daysPerFile = pVnodeCfg->cfg.daysPerFile; -// tsdbCfg.minRowsPerFileBlock = -1; -// tsdbCfg.maxRowsPerFileBlock = -1; -// tsdbCfg.keep = -1; -// tsdbCfg.maxCacheSize = -1; + STsdbCfg tsdbCfg; + tsdbCfg.precision = pVnodeCfg->cfg.precision; + tsdbCfg.tsdbId = pVnodeCfg->vnode; + tsdbCfg.maxTables = pVnodeCfg->cfg.maxSessions; + tsdbCfg.daysPerFile = pVnodeCfg->cfg.daysPerFile; + tsdbCfg.minRowsPerFileBlock = -1; + tsdbCfg.maxRowsPerFileBlock = -1; + tsdbCfg.keep = -1; + tsdbCfg.maxCacheSize = -1; -// char rootDir[TSDB_FILENAME_LEN] = {0}; -// sprintf(rootDir, "%s/vnode%d", tsDirectory, pVnodeCfg->cfg.vgId); -// -// void *pTsdb = tsdbCreateRepo(rootDir, &tsdbCfg, NULL); -// if (pTsdb != NULL) { -// return terrno; -// } -// -// SVnodeObj vnodeObj; -// vnodeObj.vgId = pVnodeCfg->cfg.vgId; -// vnodeObj.status = TSDB_VN_STATUS_NOT_READY; -// vnodeObj.refCount = 1; -// vnodeObj.version = 0; -// vnodeObj.wworker = dnodeAllocateWriteWorker(); -// vnodeObj.rworker = dnodeAllocateReadWorker(); -// vnodeObj.wal = NULL; -// vnodeObj.tsdb = pTsdb; -// vnodeObj.replica = NULL; -// vnodeObj.events = NULL; -// vnodeObj.cq = NULL; -// -// taosAddIntHash(tsDnodeVnodesHash, vnodeObj.vgId, &vnodeObj); + char rootDir[TSDB_FILENAME_LEN] = {0}; + sprintf(rootDir, "%s/vnode%d", tsDirectory, pVnodeCfg->cfg.vgId); + + void *pTsdb = tsdbCreateRepo(rootDir, &tsdbCfg, NULL); + if (pTsdb != NULL) { + return terrno; + } + + SVnodeObj vnodeObj; + vnodeObj.vgId = pVnodeCfg->cfg.vgId; + vnodeObj.status = TSDB_VN_STATUS_NOT_READY; + vnodeObj.refCount = 1; + vnodeObj.version = 0; + vnodeObj.wworker = dnodeAllocateWriteWorker(); + vnodeObj.rworker = dnodeAllocateReadWorker(); + vnodeObj.wal = NULL; + vnodeObj.tsdb = pTsdb; + vnodeObj.replica = NULL; + vnodeObj.events = NULL; + vnodeObj.cq = NULL; + + taosAddIntHash(tsDnodeVnodesHash, vnodeObj.vgId, &vnodeObj); return TSDB_CODE_SUCCESS; } static void dnodeDropVnode(SVnodeObj *pVnode) { -// pVnode->status = TSDB_VN_STATUS_NOT_READY; -// -// int32_t count = atomic_sub_fetch_32(&pVnode->refCount, 1); -// if (count > 0) { -// // wait refcount -// } -// -// if (pVnode->tsdb) { -// tsdbDropRepo(pVnode->tsdb); -// pVnode->tsdb = NULL; -// } -// -// dnodeCleanupVnode(pVnode); + pVnode->status = TSDB_VN_STATUS_NOT_READY; + + int32_t count = atomic_sub_fetch_32(&pVnode->refCount, 1); + if (count > 0) { + // wait refcount + } + + if (pVnode->tsdb) { + tsdbDropRepo(pVnode->tsdb); + pVnode->tsdb = NULL; + } + + dnodeCleanupVnode(pVnode); } static void dnodeProcesSMDCreateVnodeMsg(SRpcMsg *rpcMsg) { diff --git a/src/mnode/src/mgmtMnode.c b/src/mnode/src/mgmtMnode.c index 0b8c7797d9..9e1b3f8ceb 100644 --- a/src/mnode/src/mgmtMnode.c +++ b/src/mnode/src/mgmtMnode.c @@ -14,6 +14,7 @@ */ #define _DEFAULT_SOURCE +#include "os.h" #include "mgmtMnode.h" bool mgmtCheckRedirect(void *handle) { diff --git a/src/mnode/src/mgmtShell.c b/src/mnode/src/mgmtShell.c index 6d02bbf18a..be8de5b331 100644 --- a/src/mnode/src/mgmtShell.c +++ b/src/mnode/src/mgmtShell.c @@ -96,7 +96,7 @@ void mgmtCleanUpShell() { } } -void mgmtAddShellHandle(uint8_t showType, void (*fp)(SRpcMsg *rpcMsg)) { +void mgmtAddShellMsgHandle(uint8_t showType, void (*fp)(SRpcMsg *rpcMsg)) { tsMgmtProcessShellMsgFp[showType] = fp; }