From 139a4c7fb1e8f403f6e40df0f4e2bdab757c9346 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Fri, 29 Oct 2021 17:11:15 +0800 Subject: [PATCH 01/33] adjust tqueue codes --- include/util/tqueue.h | 21 +-- include/util/tworker.h | 54 +++---- source/server/mnode/src/mnodeWorker.c | 116 +++++++++------- source/server/vnode/inc/vnodeWrite.h | 7 +- source/server/vnode/src/vnodeInt.c | 20 +-- source/server/vnode/src/vnodeMgmt.c | 24 ++-- source/server/vnode/src/vnodeRead.c | 42 +++--- source/server/vnode/src/vnodeWrite.c | 193 ++++++++++++-------------- source/util/src/tqueue.c | 168 +++++++++++----------- source/util/src/tworker.c | 64 ++++----- 10 files changed, 339 insertions(+), 370 deletions(-) diff --git a/include/util/tqueue.h b/include/util/tqueue.h index cd897435fa..dc16222c2b 100644 --- a/include/util/tqueue.h +++ b/include/util/tqueue.h @@ -37,21 +37,24 @@ shall be used to set up the protection. */ -typedef void* taos_queue; -typedef void* taos_qset; -typedef void* taos_qall; +typedef void *taos_queue; +typedef void *taos_qset; +typedef void *taos_qall; +typedef void *(*FProcessOneItem)(void *pItem, void *ahandle); +typedef void *(*FProcessAllItem)(taos_qall qall, int numOfItems, void *ahandle); taos_queue taosOpenQueue(); void taosCloseQueue(taos_queue); +void taosSetQueueFp(taos_queue, FProcessOneItem, FProcessAllItem fp); void *taosAllocateQitem(int size); -void taosFreeQitem(void *item); -int taosWriteQitem(taos_queue, int type, void *item); -int taosReadQitem(taos_queue, int *type, void **pitem); +void taosFreeQitem(void *pItem); +int taosWriteQitem(taos_queue, void *pItem); +int taosReadQitem(taos_queue, void **pItem); taos_qall taosAllocateQall(); void taosFreeQall(taos_qall); int taosReadAllQitems(taos_queue, taos_qall); -int taosGetQitem(taos_qall, int *type, void **pitem); +int taosGetQitem(taos_qall, void **pItem); void taosResetQitems(taos_qall); taos_qset taosOpenQset(); @@ -61,8 +64,8 @@ int taosAddIntoQset(taos_qset, taos_queue, void *ahandle); void taosRemoveFromQset(taos_qset, taos_queue); int taosGetQueueNumber(taos_qset); -int taosReadQitemFromQset(taos_qset, int *type, void **pitem, void **handle); -int taosReadAllQitemsFromQset(taos_qset, taos_qall, void **handle); +int taosReadQitemFromQset(taos_qset, void **pItem, void **ahandle, FProcessOneItem *); +int taosReadAllQitemsFromQset(taos_qset, taos_qall, void **ahandle, FProcessAllItem *); int taosGetQueueItemsNumber(taos_queue param); int taosGetQsetItemsNumber(taos_qset param); diff --git a/include/util/tworker.h b/include/util/tworker.h index 47ad0bd9e7..591ebf8967 100644 --- a/include/util/tworker.h +++ b/include/util/tworker.h @@ -22,13 +22,6 @@ extern "C" { #endif -typedef int32_t (*ProcessStartFp)(void *ahandle, void *pMsg, int32_t qtype); -typedef void (*ProcessEndFp)(void *ahandle, void *pMsg, int32_t qtype, int32_t code); - -typedef bool (*ProcessWriteStartFp)(void *ahandle, void *pMsg, int32_t qtype); -typedef void (*ProcessWriteSyncFp)(void *ahandle, int32_t code); -typedef void (*ProcessWriteEndFp)(void *ahandle, void *pMsg, int32_t qtype); - typedef struct SWorker { int32_t id; // worker ID pthread_t thread; // thread @@ -40,41 +33,36 @@ typedef struct SWorkerPool { int32_t min; // min number of workers int32_t num; // current number of workers taos_qset qset; - const char * name; - ProcessStartFp startFp; - ProcessEndFp endFp; - SWorker * workers; + const char *name; + SWorker *workers; pthread_mutex_t mutex; } SWorkerPool; -typedef struct SWriteWorker { - int32_t id; // worker id - pthread_t thread; // thread - taos_qall qall; - taos_qset qset; // queue set - struct SWriteWorkerPool *pool; -} SWriteWorker; +typedef struct SMWorker { + int32_t id; // worker id + pthread_t thread; // thread + taos_qall qall; + taos_qset qset; // queue set + struct SMWorkerPool *pool; +} SMWorker; -typedef struct SWriteWorkerPool { - int32_t max; // max number of workers - int32_t nextId; // from 0 to max-1, cyclic - const char * name; - ProcessWriteStartFp startFp; - ProcessWriteSyncFp syncFp; - ProcessWriteEndFp endFp; - SWriteWorker * workers; - pthread_mutex_t mutex; -} SWriteWorkerPool; +typedef struct SMWorkerPool { + int32_t max; // max number of workers + int32_t nextId; // from 0 to max-1, cyclic + const char *name; + SMWorker *workers; + pthread_mutex_t mutex; +} SMWorkerPool; int32_t tWorkerInit(SWorkerPool *pool); void tWorkerCleanup(SWorkerPool *pool); -taos_queue tWorkerAllocQueue(SWorkerPool *pool, void *ahandle); +taos_queue tWorkerAllocQueue(SWorkerPool *pool, void *ahandle, FProcessOneItem fp); void tWorkerFreeQueue(SWorkerPool *pool, taos_queue queue); -int32_t tWriteWorkerInit(SWriteWorkerPool *pool); -void tWriteWorkerCleanup(SWriteWorkerPool *pool); -taos_queue tWriteWorkerAllocQueue(SWriteWorkerPool *pool, void *ahandle); -void tWriteWorkerFreeQueue(SWriteWorkerPool *pool, taos_queue queue); +int32_t tMWorkerInit(SMWorkerPool *pool); +void tMWorkerCleanup(SMWorkerPool *pool); +taos_queue tMWorkerAllocQueue(SMWorkerPool *pool, void *ahandle, FProcessAllItem fp); +void tMWorkerFreeQueue(SMWorkerPool *pool, taos_queue queue); #ifdef __cplusplus } diff --git a/source/server/mnode/src/mnodeWorker.c b/source/server/mnode/src/mnodeWorker.c index 84beddb2da..fc370538aa 100644 --- a/source/server/mnode/src/mnodeWorker.c +++ b/source/server/mnode/src/mnodeWorker.c @@ -28,10 +28,10 @@ static struct { SWorkerPool write; SWorkerPool peerReq; SWorkerPool peerRsp; - taos_queue readQ; - taos_queue writeQ; - taos_queue peerReqQ; - taos_queue peerRspQ; + taos_queue readQ; + taos_queue writeQ; + taos_queue peerReqQ; + taos_queue peerRspQ; int32_t (*writeMsgFp[TSDB_MSG_TYPE_MAX])(SMnMsg *); int32_t (*readMsgFp[TSDB_MSG_TYPE_MAX])(SMnMsg *); int32_t (*peerReqFp[TSDB_MSG_TYPE_MAX])(SMnMsg *); @@ -81,7 +81,7 @@ static void mnodeDispatchToWriteQueue(SRpcMsg *pRpcMsg) { rpcSendResponse(&rpcRsp); } else { mTrace("msg:%p, app:%p type:%s is put into wqueue", pMsg, pMsg->rpcMsg.ahandle, taosMsg[pMsg->rpcMsg.msgType]); - taosWriteQitem(tsMworker.writeQ, TAOS_QTYPE_RPC, pMsg); + taosWriteQitem(tsMworker.writeQ, pMsg); } } @@ -93,7 +93,7 @@ void mnodeReDispatchToWriteQueue(SMnMsg *pMsg) { mnodeSendRedirectMsg(&pMsg->rpcMsg, true); mnodeCleanupMsg(pMsg); } else { - taosWriteQitem(tsMworker.writeQ, TAOS_QTYPE_RPC, pMsg); + taosWriteQitem(tsMworker.writeQ, pMsg); } } @@ -107,7 +107,7 @@ static void mnodeDispatchToReadQueue(SRpcMsg *pRpcMsg) { rpcSendResponse(&rpcRsp); } else { mTrace("msg:%p, app:%p type:%s is put into rqueue", pMsg, pMsg->rpcMsg.ahandle, taosMsg[pMsg->rpcMsg.msgType]); - taosWriteQitem(tsMworker.readQ, TAOS_QTYPE_RPC, pMsg); + taosWriteQitem(tsMworker.readQ, pMsg); } } @@ -125,7 +125,7 @@ static void mnodeDispatchToPeerQueue(SRpcMsg *pRpcMsg) { } else { mTrace("msg:%p, app:%p type:%s is put into peer req queue", pMsg, pMsg->rpcMsg.ahandle, taosMsg[pMsg->rpcMsg.msgType]); - taosWriteQitem(tsMworker.peerReqQ, TAOS_QTYPE_RPC, pMsg); + taosWriteQitem(tsMworker.peerReqQ, pMsg); } } @@ -140,13 +140,13 @@ void mnodeDispatchToPeerRspQueue(SRpcMsg *pRpcMsg) { } else { mTrace("msg:%p, app:%p type:%s is put into peer rsp queue", pMsg, pMsg->rpcMsg.ahandle, taosMsg[pMsg->rpcMsg.msgType]); - taosWriteQitem(tsMworker.peerRspQ, TAOS_QTYPE_RPC, pMsg); + taosWriteQitem(tsMworker.peerRspQ, pMsg); } // rpcFreeCont(pRpcMsg->pCont); } -static void mnodeSendRpcRsp(void *ahandle, SMnMsg *pMsg, int32_t qtype, int32_t code) { +void mnodeSendRsp(SMnMsg *pMsg, int32_t code) { if (pMsg == NULL) return; if (code == TSDB_CODE_MND_ACTION_IN_PROGRESS) return; if (code == TSDB_CODE_MND_ACTION_NEED_REPROCESSED) { @@ -155,22 +155,16 @@ static void mnodeSendRpcRsp(void *ahandle, SMnMsg *pMsg, int32_t qtype, int32_t } SRpcMsg rpcRsp = { - .handle = pMsg->rpcMsg.handle, - .pCont = pMsg->rpcRsp.rsp, - .contLen = pMsg->rpcRsp.len, - .code = code, + .handle = pMsg->rpcMsg.handle, + .pCont = pMsg->rpcRsp.rsp, + .contLen = pMsg->rpcRsp.len, + .code = code, }; rpcSendResponse(&rpcRsp); mnodeCleanupMsg(pMsg); } -void mnodeSendRsp(SMnMsg *pMsg, int32_t code) { mnodeSendRpcRsp(NULL, pMsg, 0, code); } - -static void mnodeProcessPeerRspEnd(void *ahandle, SMnMsg *pMsg, int32_t qtype, int32_t code) { - mnodeCleanupMsg(pMsg); -} - static void mnodeInitMsgFp() { // // peer req // tsMworker.msgFp[TSDB_MSG_TYPE_DM_CONFIG_TABLE] = mnodeDispatchToPeerQueue; @@ -290,13 +284,15 @@ static void mnodeInitMsgFp() { // tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_KILL_CONN] = mnodeProcessKillConnectionMsg; } -static int32_t mnodeProcessWriteReq(void *unused, SMnMsg *pMsg, int32_t qtype) { +static void mnodeProcessWriteReq(SMnMsg *pMsg, void *unused) { int32_t msgType = pMsg->rpcMsg.msgType; - void *ahandle = pMsg->rpcMsg.ahandle; + void *ahandle = pMsg->rpcMsg.ahandle; + int32_t code = 0; if (pMsg->rpcMsg.pCont == NULL) { mError("msg:%p, app:%p type:%s content is null", pMsg, ahandle, taosMsg[msgType]); - return TSDB_CODE_MND_INVALID_MSG_LEN; + code = TSDB_CODE_MND_INVALID_MSG_LEN; + goto PROCESS_WRITE_REQ_END; } if (!mnodeIsMaster()) { @@ -309,31 +305,39 @@ static int32_t mnodeProcessWriteReq(void *unused, SMnMsg *pMsg, int32_t qtype) { mDebug("msg:%p, app:%p type:%s in write queue, is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, taosMsg[msgType], epSet->numOfEps, epSet->inUse); - return TSDB_CODE_RPC_REDIRECT; + code = TSDB_CODE_RPC_REDIRECT; + goto PROCESS_WRITE_REQ_END; } if (tsMworker.writeMsgFp[msgType] == NULL) { mError("msg:%p, app:%p type:%s not processed", pMsg, ahandle, taosMsg[msgType]); - return TSDB_CODE_MND_MSG_NOT_PROCESSED; + code = TSDB_CODE_MND_MSG_NOT_PROCESSED; + goto PROCESS_WRITE_REQ_END; } - return (*tsMworker.writeMsgFp[msgType])(pMsg); + code = (*tsMworker.writeMsgFp[msgType])(pMsg); + +PROCESS_WRITE_REQ_END: + mnodeSendRsp(pMsg, code); } -static int32_t mnodeProcessReadReq(void* unused, SMnMsg *pMsg, int32_t qtype) { +static void mnodeProcessReadReq(SMnMsg *pMsg, void *unused) { int32_t msgType = pMsg->rpcMsg.msgType; - void *ahandle = pMsg->rpcMsg.ahandle; + void *ahandle = pMsg->rpcMsg.ahandle; + int32_t code = 0; if (pMsg->rpcMsg.pCont == NULL) { mError("msg:%p, app:%p type:%s in mread queue, content is null", pMsg, ahandle, taosMsg[msgType]); - return TSDB_CODE_MND_INVALID_MSG_LEN; + code = TSDB_CODE_MND_INVALID_MSG_LEN; + goto PROCESS_READ_REQ_END; } if (!mnodeIsMaster()) { SMnRsp *rpcRsp = &pMsg->rpcRsp; SRpcEpSet *epSet = rpcMallocCont(sizeof(SRpcEpSet)); if (!epSet) { - return TSDB_CODE_MND_OUT_OF_MEMORY; + code = TSDB_CODE_MND_OUT_OF_MEMORY; + goto PROCESS_READ_REQ_END; } mnodeGetMnodeEpSetForShell(epSet, true); rpcRsp->rsp = epSet; @@ -341,25 +345,32 @@ static int32_t mnodeProcessReadReq(void* unused, SMnMsg *pMsg, int32_t qtype) { mDebug("msg:%p, app:%p type:%s in mread queue is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, taosMsg[msgType], epSet->numOfEps, epSet->inUse); - return TSDB_CODE_RPC_REDIRECT; + code = TSDB_CODE_RPC_REDIRECT; + goto PROCESS_READ_REQ_END; } if (tsMworker.readMsgFp[msgType] == NULL) { mError("msg:%p, app:%p type:%s in mread queue, not processed", pMsg, ahandle, taosMsg[msgType]); - return TSDB_CODE_MND_MSG_NOT_PROCESSED; + code = TSDB_CODE_MND_MSG_NOT_PROCESSED; + goto PROCESS_READ_REQ_END; } mTrace("msg:%p, app:%p type:%s will be processed in mread queue", pMsg, ahandle, taosMsg[msgType]); - return (*tsMworker.readMsgFp[msgType])(pMsg); + code = (*tsMworker.readMsgFp[msgType])(pMsg); + +PROCESS_READ_REQ_END: + mnodeSendRsp(pMsg, code); } -static int32_t mnodeProcessPeerReq(void *unused, SMnMsg *pMsg, int32_t qtype) { +static void mnodeProcessPeerReq(SMnMsg *pMsg, void *unused) { int32_t msgType = pMsg->rpcMsg.msgType; - void * ahandle = pMsg->rpcMsg.ahandle; + void *ahandle = pMsg->rpcMsg.ahandle; + int32_t code = 0; if (pMsg->rpcMsg.pCont == NULL) { mError("msg:%p, ahandle:%p type:%s in mpeer queue, content is null", pMsg, ahandle, taosMsg[msgType]); - return TSDB_CODE_MND_INVALID_MSG_LEN; + code = TSDB_CODE_MND_INVALID_MSG_LEN; + goto PROCESS_PEER_REQ_END; } if (!mnodeIsMaster()) { @@ -372,24 +383,29 @@ static int32_t mnodeProcessPeerReq(void *unused, SMnMsg *pMsg, int32_t qtype) { mDebug("msg:%p, ahandle:%p type:%s in mpeer queue is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, taosMsg[msgType], epSet->numOfEps, epSet->inUse); - return TSDB_CODE_RPC_REDIRECT; + code = TSDB_CODE_RPC_REDIRECT; + goto PROCESS_PEER_REQ_END; } if (tsMworker.peerReqFp[msgType] == NULL) { mError("msg:%p, ahandle:%p type:%s in mpeer queue, not processed", pMsg, ahandle, taosMsg[msgType]); - return TSDB_CODE_MND_MSG_NOT_PROCESSED; + code = TSDB_CODE_MND_MSG_NOT_PROCESSED; + goto PROCESS_PEER_REQ_END; } - return (*tsMworker.peerReqFp[msgType])(pMsg); + code = (*tsMworker.peerReqFp[msgType])(pMsg); + +PROCESS_PEER_REQ_END: + mnodeSendRsp(pMsg, code); } -static int32_t mnodeProcessPeerRsp(void *ahandle, SMnMsg *pMsg, int32_t qtype) { +static void mnodeProcessPeerRsp(SMnMsg *pMsg, void *unused) { int32_t msgType = pMsg->rpcMsg.msgType; SRpcMsg *pRpcMsg = &pMsg->rpcMsg; if (!mnodeIsMaster()) { mError("msg:%p, ahandle:%p type:%s not processed for not master", pRpcMsg, pRpcMsg->ahandle, taosMsg[msgType]); - return 0; + mnodeCleanupMsg(pMsg); } if (tsMworker.peerRspFp[msgType]) { @@ -398,7 +414,7 @@ static int32_t mnodeProcessPeerRsp(void *ahandle, SMnMsg *pMsg, int32_t qtype) { mError("msg:%p, ahandle:%p type:%s is not processed", pRpcMsg, pRpcMsg->ahandle, taosMsg[msgType]); } - return 0; + mnodeCleanupMsg(pMsg); } int32_t mnodeInitWorker() { @@ -406,20 +422,16 @@ int32_t mnodeInitWorker() { SWorkerPool *pPool = &tsMworker.write; pPool->name = "mnode-write"; - pPool->startFp = (ProcessStartFp)mnodeProcessWriteReq; - pPool->endFp = (ProcessEndFp)mnodeSendRpcRsp; pPool->min = 1; pPool->max = 1; if (tWorkerInit(pPool) != 0) { return TSDB_CODE_MND_OUT_OF_MEMORY; } else { - tsMworker.writeQ = tWorkerAllocQueue(pPool, NULL); + tsMworker.writeQ = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)mnodeProcessWriteReq); } pPool = &tsMworker.read; pPool->name = "mnode-read"; - pPool->startFp = (ProcessStartFp)mnodeProcessReadReq; - pPool->endFp = (ProcessEndFp)mnodeSendRpcRsp; pPool->min = 2; pPool->max = (int32_t)(tsNumOfCores * tsNumOfThreadsPerCore / 2); pPool->max = MAX(2, pPool->max); @@ -427,31 +439,27 @@ int32_t mnodeInitWorker() { if (tWorkerInit(pPool) != 0) { return TSDB_CODE_MND_OUT_OF_MEMORY; } else { - tsMworker.readQ = tWorkerAllocQueue(pPool, NULL); + tsMworker.readQ = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)mnodeProcessReadReq); } pPool = &tsMworker.peerReq; pPool->name = "mnode-peer-req"; - pPool->startFp = (ProcessStartFp)mnodeProcessPeerReq; - pPool->endFp = (ProcessEndFp)mnodeSendRpcRsp; pPool->min = 1; pPool->max = 1; if (tWorkerInit(pPool) != 0) { return TSDB_CODE_MND_OUT_OF_MEMORY; } else { - tsMworker.peerReqQ = tWorkerAllocQueue(pPool, NULL); + tsMworker.peerReqQ = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)mnodeProcessPeerReq); } pPool = &tsMworker.peerRsp; pPool->name = "mnode-peer-rsp"; - pPool->startFp = (ProcessStartFp)mnodeProcessPeerRsp; - pPool->endFp = (ProcessEndFp)mnodeProcessPeerRspEnd; pPool->min = 1; pPool->max = 1; if (tWorkerInit(pPool) != 0) { return TSDB_CODE_MND_OUT_OF_MEMORY; } else { - tsMworker.peerRspQ = tWorkerAllocQueue(pPool, NULL); + tsMworker.peerRspQ = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)mnodeProcessPeerRsp); } mInfo("mnode worker is initialized"); diff --git a/source/server/vnode/inc/vnodeWrite.h b/source/server/vnode/inc/vnodeWrite.h index 0bb670de5b..8500607694 100644 --- a/source/server/vnode/inc/vnodeWrite.h +++ b/source/server/vnode/inc/vnodeWrite.h @@ -23,11 +23,12 @@ extern "C" { int32_t vnodeInitWrite(); void vnodeCleanupWrite(); + taos_queue vnodeAllocWriteQueue(SVnode *pVnode); void vnodeFreeWriteQueue(taos_queue pQueue); - -void vnodeProcessWriteMsg(SRpcMsg *pRpcMsg); -int32_t vnodeProcessWalMsg(SVnode *pVnode, SWalHead *pHead); +taos_queue vnodeAllocApplyQueue(SVnode *pVnode); +void vnodeFreeApplyQueue(taos_queue pQueue); +void vnodeProcessWriteReq(SRpcMsg *pRpcMsg); void vnodeStartWrite(SVnode *pVnode); void vnodeStopWrite(SVnode *pVnode); diff --git a/source/server/vnode/src/vnodeInt.c b/source/server/vnode/src/vnodeInt.c index 9e1739a68e..4061c04d94 100644 --- a/source/server/vnode/src/vnodeInt.c +++ b/source/server/vnode/src/vnodeInt.c @@ -54,17 +54,17 @@ static void vnodeInitMsgFp() { tsVint.msgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessMgmtMsg; tsVint.msgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessMgmtMsg; tsVint.msgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessMgmtMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE] = vnodeProcessWriteMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MD_DROP_TABLE] = vnodeProcessWriteMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MD_ALTER_TABLE] = vnodeProcessWriteMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MD_DROP_STABLE] = vnodeProcessWriteMsg; - tsVint.msgFp[TSDB_MSG_TYPE_SUBMIT] = vnodeProcessWriteMsg; - tsVint.msgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = vnodeProcessWriteMsg; + tsVint.msgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE] = vnodeProcessWriteReq; + tsVint.msgFp[TSDB_MSG_TYPE_MD_DROP_TABLE] = vnodeProcessWriteReq; + tsVint.msgFp[TSDB_MSG_TYPE_MD_ALTER_TABLE] = vnodeProcessWriteReq; + tsVint.msgFp[TSDB_MSG_TYPE_MD_DROP_STABLE] = vnodeProcessWriteReq; + tsVint.msgFp[TSDB_MSG_TYPE_SUBMIT] = vnodeProcessWriteReq; + tsVint.msgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = vnodeProcessWriteReq; // mq related - tsVint.msgFp[TSDB_MSG_TYPE_MQ_CONNECT] = vnodeProcessWriteMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = vnodeProcessWriteMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MQ_ACK] = vnodeProcessWriteMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MQ_RESET] = vnodeProcessWriteMsg; + tsVint.msgFp[TSDB_MSG_TYPE_MQ_CONNECT] = vnodeProcessWriteReq; + tsVint.msgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = vnodeProcessWriteReq; + tsVint.msgFp[TSDB_MSG_TYPE_MQ_ACK] = vnodeProcessWriteReq; + tsVint.msgFp[TSDB_MSG_TYPE_MQ_RESET] = vnodeProcessWriteReq; tsVint.msgFp[TSDB_MSG_TYPE_MQ_QUERY] = vnodeProcessReadMsg; tsVint.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = vnodeProcessReadMsg; // mq related end diff --git a/source/server/vnode/src/vnodeMgmt.c b/source/server/vnode/src/vnodeMgmt.c index e0e76d5b56..dfb3c95b8d 100644 --- a/source/server/vnode/src/vnodeMgmt.c +++ b/source/server/vnode/src/vnodeMgmt.c @@ -194,22 +194,18 @@ static int32_t vnodeProcessAlterStreamReq(SRpcMsg *pMsg) { return TSDB_CODE_VND_MSG_NOT_PROCESSED; } -static int32_t vnodeProcessMgmtStart(void *unused, SVnMgmtMsg *pMgmt, int32_t qtype) { +static void vnodeProcessMgmtReq(SVnMgmtMsg *pMgmt, void *unused) { SRpcMsg *pMsg = &pMgmt->rpcMsg; int32_t msgType = pMsg->msgType; + int32_t code = 0; if (tsVmgmt.msgFp[msgType]) { vTrace("msg:%p, ahandle:%p type:%s will be processed", pMgmt, pMsg->ahandle, taosMsg[msgType]); - return (*tsVmgmt.msgFp[msgType])(pMsg); + code = (*tsVmgmt.msgFp[msgType])(pMsg); } else { vError("msg:%p, ahandle:%p type:%s not processed since no handle", pMgmt, pMsg->ahandle, taosMsg[msgType]); - return TSDB_CODE_DND_MSG_NOT_PROCESSED; + code = TSDB_CODE_DND_MSG_NOT_PROCESSED; } -} - -static void vnodeProcessMgmtEnd(void *unused, SVnMgmtMsg *pMgmt, int32_t qtype, int32_t code) { - SRpcMsg *pMsg = &pMgmt->rpcMsg; - vTrace("msg:%p, is processed, result:%s", pMgmt, tstrerror(code)); SRpcMsg rsp = {.code = code, .handle = pMsg->handle}; rpcSendResponse(&rsp); @@ -235,9 +231,9 @@ static int32_t vnodeWriteToMgmtQueue(SRpcMsg *pMsg) { memcpy(pMgmt->pCont, pMsg->pCont, pMsg->contLen); if (pMsg->msgType == TSDB_MSG_TYPE_MD_CREATE_VNODE) { - return taosWriteQitem(tsVmgmt.createQueue, TAOS_QTYPE_RPC, pMgmt); + return taosWriteQitem(tsVmgmt.createQueue, pMgmt); } else { - return taosWriteQitem(tsVmgmt.workerQueue, TAOS_QTYPE_RPC, pMgmt); + return taosWriteQitem(tsVmgmt.workerQueue, pMgmt); } } @@ -257,27 +253,23 @@ int32_t vnodeInitMgmt() { SWorkerPool *pPool = &tsVmgmt.createPool; pPool->name = "vnode-mgmt-create"; - pPool->startFp = (ProcessStartFp)vnodeProcessMgmtStart; - pPool->endFp = (ProcessEndFp)vnodeProcessMgmtEnd; pPool->min = 1; pPool->max = 1; if (tWorkerInit(pPool) != 0) { return TSDB_CODE_VND_OUT_OF_MEMORY; } - tsVmgmt.createQueue = tWorkerAllocQueue(pPool, NULL); + tsVmgmt.createQueue = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)vnodeProcessMgmtReq); pPool = &tsVmgmt.workerPool; pPool->name = "vnode-mgmt-worker"; - pPool->startFp = (ProcessStartFp)vnodeProcessMgmtStart; - pPool->endFp = (ProcessEndFp)vnodeProcessMgmtEnd; pPool->min = 1; pPool->max = 1; if (tWorkerInit(pPool) != 0) { return TSDB_CODE_VND_OUT_OF_MEMORY; } - tsVmgmt.workerQueue = tWorkerAllocQueue(pPool, NULL); + tsVmgmt.workerQueue = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)vnodeProcessMgmtReq); vInfo("vmgmt is initialized"); return TSDB_CODE_SUCCESS; diff --git a/source/server/vnode/src/vnodeRead.c b/source/server/vnode/src/vnodeRead.c index 2ca2a81739..ce6348c992 100644 --- a/source/server/vnode/src/vnodeRead.c +++ b/source/server/vnode/src/vnodeRead.c @@ -71,9 +71,9 @@ static int32_t vnodeWriteToRQueue(SVnode *pVnode, void *pCont, int32_t contLen, atomic_add_fetch_32(&pVnode->queuedRMsg, 1); if (pRead->code == TSDB_CODE_RPC_NETWORK_UNAVAIL || pRead->msgType == TSDB_MSG_TYPE_FETCH) { - return taosWriteQitem(pVnode->pFetchQ, qtype, pRead); + return taosWriteQitem(pVnode->pFetchQ, pRead); } else { - return taosWriteQitem(pVnode->pQueryQ, qtype, pRead); + return taosWriteQitem(pVnode->pQueryQ, pRead); } } @@ -136,18 +136,6 @@ static void vnodeInitReadMsgFp() { tsVread.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = vnodeProcessConsumeMsg; } -static int32_t vnodeProcessReadStart(SVnode *pVnode, SReadMsg *pRead, int32_t qtype) { - int32_t msgType = pRead->msgType; - if (tsVread.msgFp[msgType] == NULL) { - vDebug("vgId:%d, msgType:%s not processed, no handle", pVnode->vgId, taosMsg[msgType]); - return TSDB_CODE_VND_MSG_NOT_PROCESSED; - } else { - vTrace("msg:%p, app:%p type:%s will be processed", pRead, pRead->rpcAhandle, taosMsg[msgType]); - } - - return (*tsVread.msgFp[msgType])(pVnode, pRead); -} - static void vnodeSendReadRsp(SReadMsg *pRead, int32_t code) { SRpcMsg rpcRsp = { .handle = pRead->rpcHandle, @@ -159,8 +147,18 @@ static void vnodeSendReadRsp(SReadMsg *pRead, int32_t code) { rpcSendResponse(&rpcRsp); } -static void vnodeProcessReadEnd(SVnode *pVnode, SReadMsg *pRead, int32_t qtype, int32_t code) { - if (qtype == TAOS_QTYPE_RPC && code != TSDB_CODE_QRY_NOT_READY) { +static void vnodeProcessReadReq(SReadMsg *pRead, SVnode *pVnode) { + int32_t msgType = pRead->msgType; + int32_t code = 0; + if (tsVread.msgFp[msgType] == NULL) { + vDebug("vgId:%d, msgType:%s not processed, no handle", pVnode->vgId, taosMsg[msgType]); + code = TSDB_CODE_VND_MSG_NOT_PROCESSED; + } else { + vTrace("msg:%p, app:%p type:%s will be processed", pRead, pRead->rpcAhandle, taosMsg[msgType]); + code = (*tsVread.msgFp[msgType])(pVnode, pRead); + } + + if (/*qtype == TAOS_QTYPE_RPC && */ code != TSDB_CODE_QRY_NOT_READY) { vnodeSendReadRsp(pRead, code); } else { if (code == TSDB_CODE_QRY_HAS_RSP) { @@ -181,16 +179,12 @@ int32_t vnodeInitRead() { SWorkerPool *pPool = &tsVread.query; pPool->name = "vquery"; - pPool->startFp = (ProcessStartFp)vnodeProcessReadStart; - pPool->endFp = (ProcessEndFp)vnodeProcessReadEnd; pPool->min = (int32_t)threadsForQuery; pPool->max = pPool->min; if (tWorkerInit(pPool) != 0) return -1; pPool = &tsVread.fetch; pPool->name = "vfetch"; - pPool->startFp = (ProcessStartFp)vnodeProcessReadStart; - pPool->endFp = (ProcessEndFp)vnodeProcessReadEnd; pPool->min = MIN(maxFetchThreads, tsNumOfCores); pPool->max = pPool->min; if (tWorkerInit(pPool) != 0) return -1; @@ -205,9 +199,13 @@ void vnodeCleanupRead() { vInfo("vread is closed"); } -taos_queue vnodeAllocQueryQueue(SVnode *pVnode) { return tWorkerAllocQueue(&tsVread.query, pVnode); } +taos_queue vnodeAllocQueryQueue(SVnode *pVnode) { + return tWorkerAllocQueue(&tsVread.query, pVnode, (FProcessOneItem)vnodeProcessReadReq); +} -taos_queue vnodeAllocFetchQueue(SVnode *pVnode) { return tWorkerAllocQueue(&tsVread.fetch, pVnode); } +taos_queue vnodeAllocFetchQueue(SVnode *pVnode) { + return tWorkerAllocQueue(&tsVread.fetch, pVnode, (FProcessOneItem)vnodeProcessReadReq); +} void vnodeFreeQueryQueue(taos_queue pQueue) { tWorkerFreeQueue(&tsVread.query, pQueue); } diff --git a/source/server/vnode/src/vnodeWrite.c b/source/server/vnode/src/vnodeWrite.c index f3258af0bf..119cd29537 100644 --- a/source/server/vnode/src/vnodeWrite.c +++ b/source/server/vnode/src/vnodeWrite.c @@ -32,15 +32,13 @@ typedef struct { } SVnWriteMsg; static struct { - SWriteWorkerPool pool; - int64_t queuedBytes; - int32_t queuedMsgs; + SMWorkerPool pool; + int64_t queuedBytes; + int32_t queuedMsgs; } tsVwrite = {0}; void vnodeStartWrite(SVnode *pVnode) {} -void vnodeStoprite(SVnode *pVnode) {} - -void vnodeWaitWriteCompleted(SVnode *pVnode) { +void vnodeStopWrite(SVnode *pVnode) { while (pVnode->queuedWMsg > 0) { vTrace("vgId:%d, queued wmsg num:%d", pVnode->vgId, pVnode->queuedWMsg); taosMsleep(10); @@ -86,7 +84,7 @@ static int32_t vnodeWriteToWQueue(SVnode *pVnode, SWalHead *pHead, int32_t qtype atomic_add_fetch_32(&tsVwrite.queuedMsgs, 1); atomic_add_fetch_32(&pVnode->refCount, 1); atomic_add_fetch_32(&pVnode->queuedWMsg, 1); - taosWriteQitem(pVnode->pWriteQ, pWrite->qtype, pWrite); + taosWriteQitem(pVnode->pWriteQ, pWrite); return TSDB_CODE_SUCCESS; } @@ -101,11 +99,7 @@ static void vnodeFreeFromWQueue(SVnode *pVnode, SVnWriteMsg *pWrite) { vnodeRelease(pVnode); } -int32_t vnodeProcessWalMsg(SVnode *pVnode, SWalHead *pHead) { - return vnodeWriteToWQueue(pVnode, pHead, TAOS_QTYPE_WAL, NULL); -} - -void vnodeProcessWriteMsg(SRpcMsg *pRpcMsg) { +void vnodeProcessWriteReq(SRpcMsg *pRpcMsg) { int32_t code; SMsgHead *pMsg = pRpcMsg->pCont; @@ -132,109 +126,104 @@ void vnodeProcessWriteMsg(SRpcMsg *pRpcMsg) { rpcFreeCont(pRpcMsg->pCont); } -static bool vnodeProcessWriteStart(SVnode *pVnode, SVnWriteMsg *pWrite, int32_t qtype) { - SWalHead *pHead = &pWrite->walHead; - SVnRsp * pRet = &pWrite->rspRet; - int32_t msgType = pHead->msgType; - - vTrace("vgId:%d, msg:%s will be processed, hver:%" PRIu64, pVnode->vgId, taosMsg[pHead->msgType], pHead->version); - - // write into WAL -#if 0 - pWrite->code = walWrite(pVnode->wal, pHead); - if (pWrite->code < 0) return false; - - - pVnode->version = pHead->version; -#endif - // write data locally - switch (msgType) { - case TSDB_MSG_TYPE_SUBMIT: - pRet->len = sizeof(SSubmitRsp); - pRet->rsp = rpcMallocCont(pRet->len); - pWrite->code = vnodeProcessSubmitReq(pVnode, (void*)pHead->cont, pRet->rsp); - break; - case TSDB_MSG_TYPE_MD_CREATE_TABLE: - pWrite->code = vnodeProcessCreateTableReq(pVnode, (void*)pHead->cont, NULL); - break; - case TSDB_MSG_TYPE_MD_DROP_TABLE: - pWrite->code = vnodeProcessDropTableReq(pVnode, (void*)pHead->cont, NULL); - break; - case TSDB_MSG_TYPE_MD_ALTER_TABLE: - pWrite->code = vnodeProcessAlterTableReq(pVnode, (void*)pHead->cont, NULL); - break; - case TSDB_MSG_TYPE_MD_DROP_STABLE: - pWrite->code = vnodeProcessDropStableReq(pVnode, (void*)pHead->cont, NULL); - break; - case TSDB_MSG_TYPE_UPDATE_TAG_VAL: - pWrite->code = vnodeProcessUpdateTagValReq(pVnode, (void*)pHead->cont, NULL); - break; - //mq related - case TSDB_MSG_TYPE_MQ_CONNECT: - pWrite->code = vnodeProcessMqConnectReq(pVnode, (void*)pHead->cont, NULL); - break; - case TSDB_MSG_TYPE_MQ_DISCONNECT: - pWrite->code = vnodeProcessMqDisconnectReq(pVnode, (void*)pHead->cont, NULL); - break; - case TSDB_MSG_TYPE_MQ_ACK: - pWrite->code = vnodeProcessMqAckReq(pVnode, (void*)pHead->cont, NULL); - break; - case TSDB_MSG_TYPE_MQ_RESET: - pWrite->code = vnodeProcessMqResetReq(pVnode, (void*)pHead->cont, NULL); - break; - //mq related end - default: - pWrite->code = TSDB_CODE_VND_MSG_NOT_PROCESSED; - break; - } - - if (pWrite->code < 0) return false; - - // update fsync - return (pWrite->code == 0 && msgType != TSDB_MSG_TYPE_SUBMIT); +static void vnodeProcessWrite(SVnWriteMsg *pWrite, SVnode *pVnode) { } + // SWalHead *pHead = &pWrite->walHead; + // SVnRsp * pRet = &pWrite->rspRet; + // int32_t msgType = pHead->msgType; -static void vnodeFsync(SVnode *pVnode, bool fsync) { -#if 0 - walFsync(pVnode->wal, fsync); -#endif -} + // vTrace("vgId:%d, msg:%s will be processed, hver:%" PRIu64, pVnode->vgId, taosMsg[pHead->msgType], pHead->version); -static void vnodeProcessWriteEnd(SVnode *pVnode, SVnWriteMsg *pWrite, int32_t qtype, int32_t code) { - if (qtype == TAOS_QTYPE_RPC) { - SRpcMsg rpcRsp = { - .handle = pWrite->rpcMsg.handle, - .pCont = pWrite->rspRet.rsp, - .contLen = pWrite->rspRet.len, - .code = pWrite->code, - }; - rpcSendResponse(&rpcRsp); - } else { - if (pWrite->rspRet.rsp) { - rpcFreeCont(pWrite->rspRet.rsp); - } - } - vnodeFreeFromWQueue(pVnode, pWrite); -} + // // write data locally + // switch (msgType) { + // case TSDB_MSG_TYPE_SUBMIT: + // pRet->len = sizeof(SSubmitRsp); + // pRet->rsp = rpcMallocCont(pRet->len); + // pWrite->code = vnodeProcessSubmitReq(pVnode, (void*)pHead->cont, pRet->rsp); + // break; + // case TSDB_MSG_TYPE_MD_CREATE_TABLE: + // pWrite->code = vnodeProcessCreateTableReq(pVnode, (void*)pHead->cont, NULL); + // break; + // case TSDB_MSG_TYPE_MD_DROP_TABLE: + // pWrite->code = vnodeProcessDropTableReq(pVnode, (void*)pHead->cont, NULL); + // break; + // case TSDB_MSG_TYPE_MD_ALTER_TABLE: + // pWrite->code = vnodeProcessAlterTableReq(pVnode, (void*)pHead->cont, NULL); + // break; + // case TSDB_MSG_TYPE_MD_DROP_STABLE: + // pWrite->code = vnodeProcessDropStableReq(pVnode, (void*)pHead->cont, NULL); + // break; + // case TSDB_MSG_TYPE_UPDATE_TAG_VAL: + // pWrite->code = vnodeProcessUpdateTagValReq(pVnode, (void*)pHead->cont, NULL); + // break; + // //mq related + // case TSDB_MSG_TYPE_MQ_CONNECT: + // pWrite->code = vnodeProcessMqConnectReq(pVnode, (void*)pHead->cont, NULL); + // break; + // case TSDB_MSG_TYPE_MQ_DISCONNECT: + // pWrite->code = vnodeProcessMqDisconnectReq(pVnode, (void*)pHead->cont, NULL); + // break; + // case TSDB_MSG_TYPE_MQ_ACK: + // pWrite->code = vnodeProcessMqAckReq(pVnode, (void*)pHead->cont, NULL); + // break; + // case TSDB_MSG_TYPE_MQ_RESET: + // pWrite->code = vnodeProcessMqResetReq(pVnode, (void*)pHead->cont, NULL); + // break; + // //mq related end + // default: + // pWrite->code = TSDB_CODE_VND_MSG_NOT_PROCESSED; + // break; + // } + + // if (pWrite->code < 0) return false; + + // // update fsync + // return (pWrite->code == 0 && msgType != TSDB_MSG_TYPE_SUBMIT); + + + // // walFsync(pVnode->wal, fsync); + + +// static void vnodeProcessWriteEnd(SVnWriteMsg *pWrite, SVnode *pVnode) { +// if (qtype == TAOS_QTYPE_RPC) { +// SRpcMsg rpcRsp = { +// .handle = pWrite->rpcMsg.handle, +// .pCont = pWrite->rspRet.rsp, +// .contLen = pWrite->rspRet.len, +// .code = pWrite->code, +// }; +// rpcSendResponse(&rpcRsp); +// } else { +// if (pWrite->rspRet.rsp) { +// rpcFreeCont(pWrite->rspRet.rsp); +// } +// } +// vnodeFreeFromWQueue(pVnode, pWrite); +// } int32_t vnodeInitWrite() { - SWriteWorkerPool *pPool = &tsVwrite.pool; - pPool->name = "vwrite"; + SMWorkerPool *pPool = &tsVwrite.pool; + pPool->name = "vnode-write"; pPool->max = tsNumOfCores; - pPool->startFp = (ProcessWriteStartFp)vnodeProcessWriteStart; - pPool->syncFp = (ProcessWriteSyncFp)vnodeFsync; - pPool->endFp = (ProcessWriteEndFp)vnodeProcessWriteEnd; - if (tWriteWorkerInit(pPool) != 0) return -1; + if (tMWorkerInit(pPool) != 0) return -1; vInfo("vwrite is initialized, max worker %d", pPool->max); return TSDB_CODE_SUCCESS; } void vnodeCleanupWrite() { - tWriteWorkerCleanup(&tsVwrite.pool); + tMWorkerCleanup(&tsVwrite.pool); vInfo("vwrite is closed"); } -taos_queue vnodeAllocWriteQueue(SVnode *pVnode) { return tWriteWorkerAllocQueue(&tsVwrite.pool, pVnode); } +taos_queue vnodeAllocWriteQueue(SVnode *pVnode) { + return tMWorkerAllocQueue(&tsVwrite.pool, pVnode, NULL); +} -void vnodeFreeWriteQueue(taos_queue pQueue) { tWriteWorkerFreeQueue(&tsVwrite.pool, pQueue); } +void vnodeFreeWriteQueue(taos_queue pQueue) { tMWorkerFreeQueue(&tsVwrite.pool, pQueue); } + +taos_queue vnodeAllocApplyQueue(SVnode *pVnode) { + return tMWorkerAllocQueue(&tsVwrite.pool, pVnode, NULL); +} + +void vnodeFreeApplyQueue(taos_queue pQueue) { tMWorkerFreeQueue(&tsVwrite.pool, pQueue); } diff --git a/source/util/src/tqueue.c b/source/util/src/tqueue.c index da1fb1837f..bb0303c04a 100644 --- a/source/util/src/tqueue.c +++ b/source/util/src/tqueue.c @@ -19,41 +19,41 @@ #include "tqueue.h" typedef struct STaosQnode { - int type; - struct STaosQnode *next; - char item[]; + struct STaosQnode *next; + char item[]; } STaosQnode; typedef struct STaosQueue { - int32_t itemSize; - int32_t numOfItems; - struct STaosQnode *head; - struct STaosQnode *tail; - struct STaosQueue *next; // for queue set - struct STaosQset *qset; // for queue set - void *ahandle; // for queue set - pthread_mutex_t mutex; + int32_t itemSize; + int32_t numOfItems; + struct STaosQnode *head; + struct STaosQnode *tail; + struct STaosQueue *next; // for queue set + struct STaosQset *qset; // for queue set + void *ahandle; // for queue set + FProcessOneItem fpOneItem; + FProcessAllItem fpAllItem; + pthread_mutex_t mutex; } STaosQueue; typedef struct STaosQset { - STaosQueue *head; - STaosQueue *current; - pthread_mutex_t mutex; - int32_t numOfQueues; - int32_t numOfItems; - tsem_t sem; + STaosQueue *head; + STaosQueue *current; + pthread_mutex_t mutex; + int32_t numOfQueues; + int32_t numOfItems; + tsem_t sem; } STaosQset; typedef struct STaosQall { - STaosQnode *current; - STaosQnode *start; - int32_t itemSize; - int32_t numOfItems; -} STaosQall; - + STaosQnode *current; + STaosQnode *start; + int32_t itemSize; + int32_t numOfItems; +} STaosQall; + taos_queue taosOpenQueue() { - - STaosQueue *queue = (STaosQueue *) calloc(sizeof(STaosQueue), 1); + STaosQueue *queue = (STaosQueue *)calloc(sizeof(STaosQueue), 1); if (queue == NULL) { terrno = TSDB_CODE_COM_OUT_OF_MEMORY; return NULL; @@ -65,6 +65,13 @@ taos_queue taosOpenQueue() { return queue; } +void taosSetQueueFp(taos_queue param, FProcessOneItem fpOneItem, FProcessAllItem fpAllItem) { + if (param == NULL) return; + STaosQueue *queue = (STaosQueue *)param; + queue->fpOneItem = fpOneItem; + queue->fpAllItem = fpAllItem; +} + void taosCloseQueue(taos_queue param) { if (param == NULL) return; STaosQueue *queue = (STaosQueue *)param; @@ -72,17 +79,17 @@ void taosCloseQueue(taos_queue param) { STaosQset *qset; pthread_mutex_lock(&queue->mutex); - STaosQnode *pNode = queue->head; + STaosQnode *pNode = queue->head; queue->head = NULL; qset = queue->qset; pthread_mutex_unlock(&queue->mutex); - if (queue->qset) taosRemoveFromQset(qset, queue); + if (queue->qset) taosRemoveFromQset(qset, queue); while (pNode) { pTemp = pNode; pNode = pNode->next; - free (pTemp); + free(pTemp); } pthread_mutex_destroy(&queue->mutex); @@ -93,7 +100,7 @@ void taosCloseQueue(taos_queue param) { void *taosAllocateQitem(int size) { STaosQnode *pNode = (STaosQnode *)calloc(sizeof(STaosQnode) + size, 1); - + if (pNode == NULL) return NULL; uTrace("item:%p, node:%p is allocated", pNode->item, pNode); return (void *)pNode->item; @@ -108,10 +115,9 @@ void taosFreeQitem(void *param) { free(temp); } -int taosWriteQitem(taos_queue param, int type, void *item) { +int taosWriteQitem(taos_queue param, void *item) { STaosQueue *queue = (STaosQueue *)param; STaosQnode *pNode = (STaosQnode *)(((char *)item) - sizeof(STaosQnode)); - pNode->type = type; pNode->next = NULL; pthread_mutex_lock(&queue->mutex); @@ -121,12 +127,12 @@ int taosWriteQitem(taos_queue param, int type, void *item) { queue->tail = pNode; } else { queue->head = pNode; - queue->tail = pNode; + queue->tail = pNode; } queue->numOfItems++; if (queue->qset) atomic_add_fetch_32(&queue->qset->numOfItems, 1); - uTrace("item:%p is put into queue:%p, type:%d items:%d", item, queue, type, queue->numOfItems); + uTrace("item:%p is put into queue:%p, items:%d", item, queue, queue->numOfItems); pthread_mutex_unlock(&queue->mutex); @@ -135,7 +141,7 @@ int taosWriteQitem(taos_queue param, int type, void *item) { return 0; } -int taosReadQitem(taos_queue param, int *type, void **pitem) { +int taosReadQitem(taos_queue param, void **pitem) { STaosQueue *queue = (STaosQueue *)param; STaosQnode *pNode = NULL; int code = 0; @@ -143,17 +149,15 @@ int taosReadQitem(taos_queue param, int *type, void **pitem) { pthread_mutex_lock(&queue->mutex); if (queue->head) { - pNode = queue->head; - *pitem = pNode->item; - *type = pNode->type; - queue->head = pNode->next; - if (queue->head == NULL) - queue->tail = NULL; - queue->numOfItems--; - if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, 1); - code = 1; - uDebug("item:%p is read out from queue:%p, type:%d items:%d", *pitem, queue, *type, queue->numOfItems); - } + pNode = queue->head; + *pitem = pNode->item; + queue->head = pNode->next; + if (queue->head == NULL) queue->tail = NULL; + queue->numOfItems--; + if (queue->qset) atomic_sub_fetch_32(&queue->qset->numOfItems, 1); + code = 1; + uDebug("item:%p is read out from queue:%p, items:%d", *pitem, queue, queue->numOfItems); + } pthread_mutex_unlock(&queue->mutex); @@ -165,9 +169,7 @@ void *taosAllocateQall() { return p; } -void taosFreeQall(void *param) { - free(param); -} +void taosFreeQall(void *param) { free(param); } int taosReadAllQitems(taos_queue param, taos_qall p2) { STaosQueue *queue = (STaosQueue *)param; @@ -203,33 +205,30 @@ int taosReadAllQitems(taos_queue param, taos_qall p2) { return code; } -int taosGetQitem(taos_qall param, int *type, void **pitem) { +int taosGetQitem(taos_qall param, void **pitem) { STaosQall *qall = (STaosQall *)param; STaosQnode *pNode; int num = 0; pNode = qall->current; - if (pNode) - qall->current = pNode->next; - + if (pNode) qall->current = pNode->next; + if (pNode) { *pitem = pNode->item; - *type = pNode->type; num = 1; - uTrace("item:%p is fetched, type:%d", *pitem, *type); + uTrace("item:%p is fetched", *pitem); } return num; } void taosResetQitems(taos_qall param) { - STaosQall *qall = (STaosQall *)param; + STaosQall *qall = (STaosQall *)param; qall->current = qall->start; } taos_qset taosOpenQset() { - - STaosQset *qset = (STaosQset *) calloc(sizeof(STaosQset), 1); + STaosQset *qset = (STaosQset *)calloc(sizeof(STaosQset), 1); if (qset == NULL) { terrno = TSDB_CODE_COM_OUT_OF_MEMORY; return NULL; @@ -276,7 +275,7 @@ int taosAddIntoQset(taos_qset p1, taos_queue p2, void *ahandle) { STaosQueue *queue = (STaosQueue *)p2; STaosQset *qset = (STaosQset *)p1; - if (queue->qset) return -1; + if (queue->qset) return -1; pthread_mutex_lock(&qset->mutex); @@ -299,7 +298,7 @@ int taosAddIntoQset(taos_qset p1, taos_queue p2, void *ahandle) { void taosRemoveFromQset(taos_qset p1, taos_queue p2) { STaosQueue *queue = (STaosQueue *)p2; STaosQset *qset = (STaosQset *)p1; - + STaosQueue *tqueue = NULL; pthread_mutex_lock(&qset->mutex); @@ -313,7 +312,7 @@ void taosRemoveFromQset(taos_qset p1, taos_queue p2) { tqueue = qset->head->next; while (tqueue) { assert(tqueue->qset); - if (tqueue== queue) { + if (tqueue == queue) { prev->next = tqueue->next; break; } else { @@ -333,29 +332,26 @@ void taosRemoveFromQset(taos_qset p1, taos_queue p2) { queue->next = NULL; pthread_mutex_unlock(&queue->mutex); } - } - + } + pthread_mutex_unlock(&qset->mutex); uTrace("queue:%p is removed from qset:%p", queue, qset); } -int taosGetQueueNumber(taos_qset param) { - return ((STaosQset *)param)->numOfQueues; -} +int taosGetQueueNumber(taos_qset param) { return ((STaosQset *)param)->numOfQueues; } -int taosReadQitemFromQset(taos_qset param, int *type, void **pitem, void **phandle) { +int taosReadQitemFromQset(taos_qset param, void **pitem, void **ahandle, FProcessOneItem *fpOneItem) { STaosQset *qset = (STaosQset *)param; STaosQnode *pNode = NULL; int code = 0; - + tsem_wait(&qset->sem); pthread_mutex_lock(&qset->mutex); - for(int i=0; inumOfQueues; ++i) { - if (qset->current == NULL) - qset->current = qset->head; + for (int i = 0; i < qset->numOfQueues; ++i) { + if (qset->current == NULL) qset->current = qset->head; STaosQueue *queue = qset->current; if (queue) qset->current = queue->next; if (queue == NULL) break; @@ -364,18 +360,17 @@ int taosReadQitemFromQset(taos_qset param, int *type, void **pitem, void **phand pthread_mutex_lock(&queue->mutex); if (queue->head) { - pNode = queue->head; - *pitem = pNode->item; - if (type) *type = pNode->type; - if (phandle) *phandle = queue->ahandle; - queue->head = pNode->next; - if (queue->head == NULL) - queue->tail = NULL; - queue->numOfItems--; - atomic_sub_fetch_32(&qset->numOfItems, 1); - code = 1; - uTrace("item:%p is read out from queue:%p, type:%d items:%d", *pitem, queue, pNode->type, queue->numOfItems); - } + pNode = queue->head; + *pitem = pNode->item; + if (ahandle) *ahandle = queue->ahandle; + if (fpOneItem) *fpOneItem = queue->fpOneItem; + queue->head = pNode->next; + if (queue->head == NULL) queue->tail = NULL; + queue->numOfItems--; + atomic_sub_fetch_32(&qset->numOfItems, 1); + code = 1; + uTrace("item:%p is read out from queue:%p, items:%d", *pitem, queue, queue->numOfItems); + } pthread_mutex_unlock(&queue->mutex); if (pNode) break; @@ -383,10 +378,10 @@ int taosReadQitemFromQset(taos_qset param, int *type, void **pitem, void **phand pthread_mutex_unlock(&qset->mutex); - return code; + return code; } -int taosReadAllQitemsFromQset(taos_qset param, taos_qall p2, void **phandle) { +int taosReadAllQitemsFromQset(taos_qset param, taos_qall p2, void **ahandle, FProcessAllItem *fpAllItem) { STaosQset *qset = (STaosQset *)param; STaosQueue *queue; STaosQall *qall = (STaosQall *)p2; @@ -411,8 +406,9 @@ int taosReadAllQitemsFromQset(taos_qset param, taos_qall p2, void **phandle) { qall->numOfItems = queue->numOfItems; qall->itemSize = queue->itemSize; code = qall->numOfItems; - *phandle = queue->ahandle; - + if (ahandle) *ahandle = queue->ahandle; + if (fpAllItem) *fpAllItem = queue->fpAllItem; + queue->head = NULL; queue->tail = NULL; queue->numOfItems = 0; diff --git a/source/util/src/tworker.c b/source/util/src/tworker.c index abed265e0b..12f1aac9f9 100644 --- a/source/util/src/tworker.c +++ b/source/util/src/tworker.c @@ -58,11 +58,11 @@ void tWorkerCleanup(SWorkerPool *pool) { } static void *tWorkerThreadFp(SWorker *worker) { - SWorkerPool *pool = worker->pool; + SWorkerPool *pool = worker->pool; + FProcessOneItem fp = NULL; - void * msg = NULL; - void * ahandle = NULL; - int32_t qtype = 0; + void *msg = NULL; + void *ahandle = NULL; int32_t code = 0; taosBlockSIGPIPE(); @@ -70,19 +70,20 @@ static void *tWorkerThreadFp(SWorker *worker) { uDebug("worker:%s:%d is running", pool->name, worker->id); while (1) { - if (taosReadQitemFromQset(pool->qset, &qtype, (void **)&msg, &ahandle) == 0) { + if (taosReadQitemFromQset(pool->qset, (void **)&msg, &ahandle, &fp) == 0) { uDebug("worker:%s:%d qset:%p, got no message and exiting", pool->name, worker->id, pool->qset); break; } - code = (*pool->startFp)(ahandle, msg, qtype); - if (pool->endFp) (*pool->endFp)(ahandle, msg, qtype, code); + if (fp) { + (*fp)(msg, ahandle); + } } return NULL; } -taos_queue tWorkerAllocQueue(SWorkerPool *pool, void *ahandle) { +taos_queue tWorkerAllocQueue(SWorkerPool *pool, void *ahandle, FProcessOneItem fp) { pthread_mutex_lock(&pool->mutex); taos_queue queue = taosOpenQueue(); if (queue == NULL) { @@ -90,6 +91,7 @@ taos_queue tWorkerAllocQueue(SWorkerPool *pool, void *ahandle) { return NULL; } + taosSetQueueFp(queue, fp, NULL); taosAddIntoQset(pool->qset, queue, ahandle); // spawn a thread to process queue @@ -122,14 +124,14 @@ void tWorkerFreeQueue(SWorkerPool *pool, void *queue) { uDebug("worker:%s, queue:%p is freed", pool->name, queue); } -int32_t tWriteWorkerInit(SWriteWorkerPool *pool) { +int32_t tMWorkerInit(SMWorkerPool *pool) { pool->nextId = 0; - pool->workers = calloc(sizeof(SWriteWorker), pool->max); + pool->workers = calloc(sizeof(SMWorker), pool->max); if (pool->workers == NULL) return -1; pthread_mutex_init(&pool->mutex, NULL); for (int32_t i = 0; i < pool->max; ++i) { - SWriteWorker *worker = pool->workers + i; + SMWorker *worker = pool->workers + i; worker->id = i; worker->qall = NULL; worker->qset = NULL; @@ -140,16 +142,16 @@ int32_t tWriteWorkerInit(SWriteWorkerPool *pool) { return 0; } -void tWriteWorkerCleanup(SWriteWorkerPool *pool) { +void tMWorkerCleanup(SMWorkerPool *pool) { for (int32_t i = 0; i < pool->max; ++i) { - SWriteWorker *worker = pool->workers + i; + SMWorker *worker = pool->workers + i; if (taosCheckPthreadValid(worker->thread)) { if (worker->qset) taosQsetThreadResume(worker->qset); } } for (int32_t i = 0; i < pool->max; ++i) { - SWriteWorker *worker = pool->workers + i; + SMWorker *worker = pool->workers + i; if (taosCheckPthreadValid(worker->thread)) { pthread_join(worker->thread, NULL); taosFreeQall(worker->qall); @@ -163,11 +165,12 @@ void tWriteWorkerCleanup(SWriteWorkerPool *pool) { uInfo("worker:%s is closed", pool->name); } -static void *tWriteWorkerThreadFp(SWriteWorker *worker) { - SWriteWorkerPool *pool = worker->pool; +static void *tWriteWorkerThreadFp(SMWorker *worker) { + SMWorkerPool *pool = worker->pool; + FProcessAllItem fp = NULL; - void * msg = NULL; - void * ahandle = NULL; + void *msg = NULL; + void *ahandle = NULL; int32_t numOfMsgs = 0; int32_t qtype = 0; @@ -176,34 +179,23 @@ static void *tWriteWorkerThreadFp(SWriteWorker *worker) { uDebug("worker:%s:%d is running", pool->name, worker->id); while (1) { - numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &ahandle); + numOfMsgs = taosReadAllQitemsFromQset(worker->qset, worker->qall, &ahandle, &fp); if (numOfMsgs == 0) { uDebug("worker:%s:%d qset:%p, got no message and exiting", pool->name, worker->id, worker->qset); break; } - bool fsync = false; - for (int32_t i = 0; i < numOfMsgs; ++i) { - taosGetQitem(worker->qall, &qtype, (void **)&msg); - fsync = fsync | (*pool->startFp)(ahandle, msg, qtype); - } - - (*pool->syncFp)(ahandle, fsync); - - // browse all items, and process them one by one - taosResetQitems(worker->qall); - for (int32_t i = 0; i < numOfMsgs; ++i) { - taosGetQitem(worker->qall, &qtype, (void **)&msg); - (*pool->endFp)(ahandle, msg, qtype); + if (fp) { + (*fp)(worker->qall, numOfMsgs, ahandle); } } return NULL; } -taos_queue tWriteWorkerAllocQueue(SWriteWorkerPool *pool, void *ahandle) { +taos_queue tMWorkerAllocQueue(SMWorkerPool *pool, void *ahandle, FProcessAllItem fp) { pthread_mutex_lock(&pool->mutex); - SWriteWorker *worker = pool->workers + pool->nextId; + SMWorker *worker = pool->workers + pool->nextId; taos_queue *queue = taosOpenQueue(); if (queue == NULL) { @@ -211,6 +203,8 @@ taos_queue tWriteWorkerAllocQueue(SWriteWorkerPool *pool, void *ahandle) { return NULL; } + taosSetQueueFp(queue, NULL, fp); + if (worker->qset == NULL) { worker->qset = taosOpenQset(); if (worker->qset == NULL) { @@ -254,7 +248,7 @@ taos_queue tWriteWorkerAllocQueue(SWriteWorkerPool *pool, void *ahandle) { return queue; } -void tWriteWorkerFreeQueue(SWriteWorkerPool *pool, taos_queue queue) { +void tMWorkerFreeQueue(SMWorkerPool *pool, taos_queue queue) { taosCloseQueue(queue); uDebug("worker:%s, queue:%p is freed", pool->name, queue); } From 3521ec34d2a390af1f02f0a5f88b36fcd4d87961 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 31 Oct 2021 13:17:19 +0800 Subject: [PATCH 02/33] add user manual of SMA --- .../Small_Materialized_Aggrates.md | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 docs/user_manual/Small_Materialized_Aggrates.md diff --git a/docs/user_manual/Small_Materialized_Aggrates.md b/docs/user_manual/Small_Materialized_Aggrates.md new file mode 100644 index 0000000000..59a5ed7b45 --- /dev/null +++ b/docs/user_manual/Small_Materialized_Aggrates.md @@ -0,0 +1,43 @@ +# Small Materialized Aggragates + +**SMA** (**S**mall **M**aterialized **A**ggrates) is used to speed up the query process on materialized data cube in TDengine. TDengine 3.0 gives more flexibility on the SMA configurations. + +There are two kinds of SMA in TDengine: +1. Block-wise SMA +2. Time-range-wise SMA + + +![SMA in TDengine 3.0](http://www.plantuml.com/plantuml/png/Km02X-AInAAItCoybDp40WKG7Gzan9Ua5fTmWUIr589z7I4iBGMddFpaR6I1aCpSLDsWnBpqLBYeGsfU2jGu0000) + +## Block-wise SMA +Block-wise SMA is created by default when the data are committed. Since time-series data are saved as block data in files, a corresponding SMA is create when the data block is written. The default block-wise SMA includes: +1. sum(*) +2. max(*) +3. min(*) + +By default, the system will create SMA for each column except those columns with type *binary* and *nchar*. However, users can change the behavior by the keyword **NOSMA** to disable the SMA for a certain column like below: +```SQL +# create a super table with the SMA on column b disabled +create table st (ts timestamp, a int, b int NOSMA, c double) tags (tg1 binary(10), tg2 int); +``` + +## Time-range-wise SMA +In addition to the default block-wise SMA, users can create their own SMAs ondemand. Below is an example to create a SMA. +```SQL +# create a SMA every 10 minutes with SMA of sum, max and min +create sma_indx sma_5min on st (sum(*), max(*), min(*), twa(*)) interval(10m); +``` +Users can also drop a time-range-wise SMA like below: +```SQL +# drop the sma index +drop sma_index sma_5min on st; +``` +**NOTE: Creating a SMA index is a heavy operation which may take a long time and block the write operation. So create the time-range-wise SMA when creating the table or when there are not too much data.** \ No newline at end of file From 78628286831a51e34a04b914aad10c193b2fad4a Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 31 Oct 2021 13:25:09 +0800 Subject: [PATCH 03/33] change more --- docs/user_manual/Small_Materialized_Aggrates.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/user_manual/Small_Materialized_Aggrates.md b/docs/user_manual/Small_Materialized_Aggrates.md index 59a5ed7b45..81a067139d 100644 --- a/docs/user_manual/Small_Materialized_Aggrates.md +++ b/docs/user_manual/Small_Materialized_Aggrates.md @@ -10,13 +10,12 @@ There are two kinds of SMA in TDengine: ```plantuml @startmindmap mind_map_test * SMA - ** Block SMA - ** Time-Range SMA + ** Block-wise SMA + ** Time-range-wise SMA @endmindmap -``` ---> -![SMA in TDengine 3.0](http://www.plantuml.com/plantuml/png/Km02X-AInAAItCoybDp40WKG7Gzan9Ua5fTmWUIr589z7I4iBGMddFpaR6I1aCpSLDsWnBpqLBYeGsfU2jGu0000) +``` --> +![SMA in TDengine 3.0](http://www.plantuml.com/plantuml/png/Kr1GK70eBaaiAidDp4l9JInG0D7nG4PyIMfn2HTGMa5B8TZN4SBIKd3AoK_ErYtFB4v55Wt9p4tLBKhCIqz5bN981HeACHW0) ## Block-wise SMA Block-wise SMA is created by default when the data are committed. Since time-series data are saved as block data in files, a corresponding SMA is create when the data block is written. The default block-wise SMA includes: 1. sum(*) From 96bb823464769f13cd3f2c638edcc8e21b55bfcf Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 31 Oct 2021 13:26:15 +0800 Subject: [PATCH 04/33] change --- docs/user_manual/Small_Materialized_Aggrates.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user_manual/Small_Materialized_Aggrates.md b/docs/user_manual/Small_Materialized_Aggrates.md index 81a067139d..dc2d9e41ed 100644 --- a/docs/user_manual/Small_Materialized_Aggrates.md +++ b/docs/user_manual/Small_Materialized_Aggrates.md @@ -32,7 +32,7 @@ create table st (ts timestamp, a int, b int NOSMA, c double) tags (tg1 binary(10 In addition to the default block-wise SMA, users can create their own SMAs ondemand. Below is an example to create a SMA. ```SQL # create a SMA every 10 minutes with SMA of sum, max and min -create sma_indx sma_5min on st (sum(*), max(*), min(*), twa(*)) interval(10m); +create sma_indx sma_10min on st (sum(*), max(*), min(*), twa(*)) interval(10m); ``` Users can also drop a time-range-wise SMA like below: ```SQL From f84b1eb2dfff0642ff1cdd8203d693259b50da58 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 31 Oct 2021 13:29:09 +0800 Subject: [PATCH 05/33] change --- docs/user_manual/Small_Materialized_Aggrates.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user_manual/Small_Materialized_Aggrates.md b/docs/user_manual/Small_Materialized_Aggrates.md index dc2d9e41ed..e13a623afb 100644 --- a/docs/user_manual/Small_Materialized_Aggrates.md +++ b/docs/user_manual/Small_Materialized_Aggrates.md @@ -39,4 +39,4 @@ Users can also drop a time-range-wise SMA like below: # drop the sma index drop sma_index sma_5min on st; ``` -**NOTE: Creating a SMA index is a heavy operation which may take a long time and block the write operation. So create the time-range-wise SMA when creating the table or when there are not too much data.** \ No newline at end of file +**NOTE: Creating an SMA index is a heavy operation which may take a long time and block the write operation. So create the time-range-wise SMA when creating the table or when there are not too much data.** \ No newline at end of file From bc6bee69b1f86ccac7931012bb00de3e2edf6d23 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 1 Nov 2021 16:04:58 +0800 Subject: [PATCH 06/33] refact --- source/server/vnode/CMakeLists.txt | 24 +------------------ source/server/vnode/impl/CMakeLists.txt | 22 +++++++++++++++++ .../server/vnode/{ => impl}/inc/vnodeCommit.h | 0 .../server/vnode/{ => impl}/inc/vnodeFile.h | 0 source/server/vnode/{ => impl}/inc/vnodeInt.h | 0 .../server/vnode/{ => impl}/inc/vnodeMain.h | 0 .../vnode/{ => impl}/inc/vnodeMemAllocator.h | 0 .../server/vnode/{ => impl}/inc/vnodeMgmt.h | 0 .../server/vnode/{ => impl}/inc/vnodeRead.h | 0 .../vnode/{ => impl}/inc/vnodeReadMsg.h | 0 .../server/vnode/{ => impl}/inc/vnodeWrite.h | 0 .../vnode/{ => impl}/inc/vnodeWriteMsg.h | 0 .../server/vnode/{ => impl}/src/vnodeCommit.c | 0 .../server/vnode/{ => impl}/src/vnodeFile.c | 0 source/server/vnode/{ => impl}/src/vnodeInt.c | 0 .../server/vnode/{ => impl}/src/vnodeMain.c | 0 .../vnode/{ => impl}/src/vnodeMemAllocator.c | 0 .../server/vnode/{ => impl}/src/vnodeMgmt.c | 0 .../server/vnode/{ => impl}/src/vnodeRead.c | 0 .../vnode/{ => impl}/src/vnodeReadMsg.c | 0 .../server/vnode/{ => impl}/src/vnodeWrite.c | 0 .../vnode/{ => impl}/src/vnodeWriteMsg.c | 0 .../vnode/{ => impl}/test/CMakeLists.txt | 0 .../{ => impl}/test/vnodeMemAllocatorTest.cpp | 0 .../vnode/{ => impl}/test/vnodeTests.cpp | 0 25 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 source/server/vnode/impl/CMakeLists.txt rename source/server/vnode/{ => impl}/inc/vnodeCommit.h (100%) rename source/server/vnode/{ => impl}/inc/vnodeFile.h (100%) rename source/server/vnode/{ => impl}/inc/vnodeInt.h (100%) rename source/server/vnode/{ => impl}/inc/vnodeMain.h (100%) rename source/server/vnode/{ => impl}/inc/vnodeMemAllocator.h (100%) rename source/server/vnode/{ => impl}/inc/vnodeMgmt.h (100%) rename source/server/vnode/{ => impl}/inc/vnodeRead.h (100%) rename source/server/vnode/{ => impl}/inc/vnodeReadMsg.h (100%) rename source/server/vnode/{ => impl}/inc/vnodeWrite.h (100%) rename source/server/vnode/{ => impl}/inc/vnodeWriteMsg.h (100%) rename source/server/vnode/{ => impl}/src/vnodeCommit.c (100%) rename source/server/vnode/{ => impl}/src/vnodeFile.c (100%) rename source/server/vnode/{ => impl}/src/vnodeInt.c (100%) rename source/server/vnode/{ => impl}/src/vnodeMain.c (100%) rename source/server/vnode/{ => impl}/src/vnodeMemAllocator.c (100%) rename source/server/vnode/{ => impl}/src/vnodeMgmt.c (100%) rename source/server/vnode/{ => impl}/src/vnodeRead.c (100%) rename source/server/vnode/{ => impl}/src/vnodeReadMsg.c (100%) rename source/server/vnode/{ => impl}/src/vnodeWrite.c (100%) rename source/server/vnode/{ => impl}/src/vnodeWriteMsg.c (100%) rename source/server/vnode/{ => impl}/test/CMakeLists.txt (100%) rename source/server/vnode/{ => impl}/test/vnodeMemAllocatorTest.cpp (100%) rename source/server/vnode/{ => impl}/test/vnodeTests.cpp (100%) diff --git a/source/server/vnode/CMakeLists.txt b/source/server/vnode/CMakeLists.txt index 573cea79b5..a4a9cff002 100644 --- a/source/server/vnode/CMakeLists.txt +++ b/source/server/vnode/CMakeLists.txt @@ -1,26 +1,4 @@ add_subdirectory(meta) add_subdirectory(tq) add_subdirectory(tsdb) - -aux_source_directory(src VNODE_SRC) -add_library(vnode STATIC ${VNODE_SRC}) -target_include_directories( - vnode - PUBLIC "${CMAKE_SOURCE_DIR}/include/server/vnode" - private "${CMAKE_CURRENT_SOURCE_DIR}/inc" -) -target_link_libraries( - vnode - PUBLIC transport - PUBLIC meta - PUBLIC tq - PUBLIC tsdb - PUBLIC wal - PUBLIC sync - PUBLIC cjson -) - -# test -if(${BUILD_TEST}) - add_subdirectory(test) -endif(${BUILD_TEST}) \ No newline at end of file +add_subdirectory(impl) \ No newline at end of file diff --git a/source/server/vnode/impl/CMakeLists.txt b/source/server/vnode/impl/CMakeLists.txt new file mode 100644 index 0000000000..81744df79c --- /dev/null +++ b/source/server/vnode/impl/CMakeLists.txt @@ -0,0 +1,22 @@ +aux_source_directory(src VNODE_SRC) +add_library(vnode STATIC ${VNODE_SRC}) +target_include_directories( + vnode + PUBLIC "${CMAKE_SOURCE_DIR}/include/server/vnode" + private "${CMAKE_CURRENT_SOURCE_DIR}/inc" +) +target_link_libraries( + vnode + PUBLIC transport + PUBLIC meta + PUBLIC tq + PUBLIC tsdb + PUBLIC wal + PUBLIC sync + PUBLIC cjson +) + +# test +if(${BUILD_TEST}) + add_subdirectory(test) +endif(${BUILD_TEST}) \ No newline at end of file diff --git a/source/server/vnode/inc/vnodeCommit.h b/source/server/vnode/impl/inc/vnodeCommit.h similarity index 100% rename from source/server/vnode/inc/vnodeCommit.h rename to source/server/vnode/impl/inc/vnodeCommit.h diff --git a/source/server/vnode/inc/vnodeFile.h b/source/server/vnode/impl/inc/vnodeFile.h similarity index 100% rename from source/server/vnode/inc/vnodeFile.h rename to source/server/vnode/impl/inc/vnodeFile.h diff --git a/source/server/vnode/inc/vnodeInt.h b/source/server/vnode/impl/inc/vnodeInt.h similarity index 100% rename from source/server/vnode/inc/vnodeInt.h rename to source/server/vnode/impl/inc/vnodeInt.h diff --git a/source/server/vnode/inc/vnodeMain.h b/source/server/vnode/impl/inc/vnodeMain.h similarity index 100% rename from source/server/vnode/inc/vnodeMain.h rename to source/server/vnode/impl/inc/vnodeMain.h diff --git a/source/server/vnode/inc/vnodeMemAllocator.h b/source/server/vnode/impl/inc/vnodeMemAllocator.h similarity index 100% rename from source/server/vnode/inc/vnodeMemAllocator.h rename to source/server/vnode/impl/inc/vnodeMemAllocator.h diff --git a/source/server/vnode/inc/vnodeMgmt.h b/source/server/vnode/impl/inc/vnodeMgmt.h similarity index 100% rename from source/server/vnode/inc/vnodeMgmt.h rename to source/server/vnode/impl/inc/vnodeMgmt.h diff --git a/source/server/vnode/inc/vnodeRead.h b/source/server/vnode/impl/inc/vnodeRead.h similarity index 100% rename from source/server/vnode/inc/vnodeRead.h rename to source/server/vnode/impl/inc/vnodeRead.h diff --git a/source/server/vnode/inc/vnodeReadMsg.h b/source/server/vnode/impl/inc/vnodeReadMsg.h similarity index 100% rename from source/server/vnode/inc/vnodeReadMsg.h rename to source/server/vnode/impl/inc/vnodeReadMsg.h diff --git a/source/server/vnode/inc/vnodeWrite.h b/source/server/vnode/impl/inc/vnodeWrite.h similarity index 100% rename from source/server/vnode/inc/vnodeWrite.h rename to source/server/vnode/impl/inc/vnodeWrite.h diff --git a/source/server/vnode/inc/vnodeWriteMsg.h b/source/server/vnode/impl/inc/vnodeWriteMsg.h similarity index 100% rename from source/server/vnode/inc/vnodeWriteMsg.h rename to source/server/vnode/impl/inc/vnodeWriteMsg.h diff --git a/source/server/vnode/src/vnodeCommit.c b/source/server/vnode/impl/src/vnodeCommit.c similarity index 100% rename from source/server/vnode/src/vnodeCommit.c rename to source/server/vnode/impl/src/vnodeCommit.c diff --git a/source/server/vnode/src/vnodeFile.c b/source/server/vnode/impl/src/vnodeFile.c similarity index 100% rename from source/server/vnode/src/vnodeFile.c rename to source/server/vnode/impl/src/vnodeFile.c diff --git a/source/server/vnode/src/vnodeInt.c b/source/server/vnode/impl/src/vnodeInt.c similarity index 100% rename from source/server/vnode/src/vnodeInt.c rename to source/server/vnode/impl/src/vnodeInt.c diff --git a/source/server/vnode/src/vnodeMain.c b/source/server/vnode/impl/src/vnodeMain.c similarity index 100% rename from source/server/vnode/src/vnodeMain.c rename to source/server/vnode/impl/src/vnodeMain.c diff --git a/source/server/vnode/src/vnodeMemAllocator.c b/source/server/vnode/impl/src/vnodeMemAllocator.c similarity index 100% rename from source/server/vnode/src/vnodeMemAllocator.c rename to source/server/vnode/impl/src/vnodeMemAllocator.c diff --git a/source/server/vnode/src/vnodeMgmt.c b/source/server/vnode/impl/src/vnodeMgmt.c similarity index 100% rename from source/server/vnode/src/vnodeMgmt.c rename to source/server/vnode/impl/src/vnodeMgmt.c diff --git a/source/server/vnode/src/vnodeRead.c b/source/server/vnode/impl/src/vnodeRead.c similarity index 100% rename from source/server/vnode/src/vnodeRead.c rename to source/server/vnode/impl/src/vnodeRead.c diff --git a/source/server/vnode/src/vnodeReadMsg.c b/source/server/vnode/impl/src/vnodeReadMsg.c similarity index 100% rename from source/server/vnode/src/vnodeReadMsg.c rename to source/server/vnode/impl/src/vnodeReadMsg.c diff --git a/source/server/vnode/src/vnodeWrite.c b/source/server/vnode/impl/src/vnodeWrite.c similarity index 100% rename from source/server/vnode/src/vnodeWrite.c rename to source/server/vnode/impl/src/vnodeWrite.c diff --git a/source/server/vnode/src/vnodeWriteMsg.c b/source/server/vnode/impl/src/vnodeWriteMsg.c similarity index 100% rename from source/server/vnode/src/vnodeWriteMsg.c rename to source/server/vnode/impl/src/vnodeWriteMsg.c diff --git a/source/server/vnode/test/CMakeLists.txt b/source/server/vnode/impl/test/CMakeLists.txt similarity index 100% rename from source/server/vnode/test/CMakeLists.txt rename to source/server/vnode/impl/test/CMakeLists.txt diff --git a/source/server/vnode/test/vnodeMemAllocatorTest.cpp b/source/server/vnode/impl/test/vnodeMemAllocatorTest.cpp similarity index 100% rename from source/server/vnode/test/vnodeMemAllocatorTest.cpp rename to source/server/vnode/impl/test/vnodeMemAllocatorTest.cpp diff --git a/source/server/vnode/test/vnodeTests.cpp b/source/server/vnode/impl/test/vnodeTests.cpp similarity index 100% rename from source/server/vnode/test/vnodeTests.cpp rename to source/server/vnode/impl/test/vnodeTests.cpp From 6d55ee00efc95d4c69d99467b3ecc390507c6e85 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Mon, 1 Nov 2021 16:14:55 +0800 Subject: [PATCH 07/33] implement tq meta --- source/server/vnode/tq/inc/tqMetaStore.h | 33 ++-- source/server/vnode/tq/src/tq.c | 26 +-- source/server/vnode/tq/src/tqMetaStore.c | 226 +++++++++++++++++++++-- 3 files changed, 241 insertions(+), 44 deletions(-) diff --git a/source/server/vnode/tq/inc/tqMetaStore.h b/source/server/vnode/tq/inc/tqMetaStore.h index 066e76028d..a82bf2a4a9 100644 --- a/source/server/vnode/tq/inc/tqMetaStore.h +++ b/source/server/vnode/tq/inc/tqMetaStore.h @@ -17,9 +17,9 @@ #define _TQ_META_STORE_H_ #include "os.h" +#include "tq.h" -#define TQ_INUSE_SIZE 0xFF -#define TQ_PAGE_SIZE 4096 +#define TQ_BUCKET_SIZE 0xFF #ifdef __cplusplus extern "C" { @@ -35,34 +35,39 @@ typedef struct TqMetaHandle { typedef struct TqMetaList { TqMetaHandle handle; struct TqMetaList* next; - struct TqMetaList* inTxnPrev; - struct TqMetaList* inTxnNext; + //struct TqMetaList* inTxnPrev; + //struct TqMetaList* inTxnNext; struct TqMetaList* unpersistPrev; struct TqMetaList* unpersistNext; } TqMetaList; typedef struct TqMetaStore { - TqMetaList* inUse[TQ_INUSE_SIZE]; + TqMetaList* bucket[TQ_BUCKET_SIZE]; //a table head, key is empty TqMetaList* unpersistHead; - int fileFd; //TODO:temporaral use - int idxFd; //TODO:temporaral use - void* (*serializer)(void*); - void* (*deserializer)(void*); + int fileFd; //TODO:temporaral use, to be replaced by unified tfile + int idxFd; //TODO:temporaral use, to be replaced by unified tfile + int (*serializer)(TqGroupHandle*, void**); + const void* (*deserializer)(const void*, TqGroupHandle*); void (*deleter)(void*); } TqMetaStore; -TqMetaStore* tqStoreOpen(const char* path, void* serializer(void* ), void* deserializer(void*), void deleter(void*)); +TqMetaStore* tqStoreOpen(const char* path, + int serializer(TqGroupHandle*, void**), + const void* deserializer(const void*, TqGroupHandle*), + void deleter(void*)); int32_t tqStoreClose(TqMetaStore*); -int32_t tqStoreDelete(TqMetaStore*); +//int32_t tqStoreDelete(TqMetaStore*); //int32_t TqStoreCommitAll(TqMetaStore*); int32_t tqStorePersist(TqMetaStore*); TqMetaHandle* tqHandleGetInUse(TqMetaStore*, int64_t key); -int32_t tqHandlePutInUse(TqMetaStore*, TqMetaHandle* handle); +int32_t tqHandlePutInUse(TqMetaStore*, int64_t key, void* value); TqMetaHandle* tqHandleGetInTxn(TqMetaStore*, int64_t key); -int32_t tqHandlePutInTxn(TqMetaStore*, TqMetaHandle* handle); -//delete in-use-handle, make in-txn-handle in use +int32_t tqHandlePutInTxn(TqMetaStore*, int64_t key, void* value); +//will replace old handle +//int32_t tqHandlePut(TqMetaStore*, TqMetaHandle* handle); +//delete in-use-handle, and put it in use int32_t tqHandleCommit(TqMetaStore*, int64_t key); //delete in-txn-handle int32_t tqHandleAbort(TqMetaStore*, int64_t key); diff --git a/source/server/vnode/tq/src/tq.c b/source/server/vnode/tq/src/tq.c index 7ecdfe7f19..e8076515a7 100644 --- a/source/server/vnode/tq/src/tq.c +++ b/source/server/vnode/tq/src/tq.c @@ -49,12 +49,12 @@ static int tqAckOneTopic(TqBufferHandle *bhandle, TmqOneAck *pAck, TqQueryMsg** return 0; } -static int tqAck(TqGroupHandle* ghandle, TmqAcks* pAcks) { +static int tqAck(TqGroupHandle* gHandle, TmqAcks* pAcks) { int32_t ackNum = pAcks->ackNum; TmqOneAck *acks = pAcks->acks; //double ptr for acks and list int i = 0; - TqListHandle* node = ghandle->head; + TqListHandle* node = gHandle->head; int ackCnt = 0; TqQueryMsg *pQuery = NULL; while(i < ackNum && node->next) { @@ -99,8 +99,8 @@ int tqDropTCGroup(STQ* pTq, int64_t topicId, int64_t cgId, int64_t cId) { return 0; } -static int tqFetch(TqGroupHandle* ghandle, void** msg) { - TqListHandle* head = ghandle->head; +static int tqFetch(TqGroupHandle* gHandle, void** msg) { + TqListHandle* head = gHandle->head; TqListHandle* node = head; int totSize = 0; //TODO: make it a macro @@ -148,7 +148,7 @@ TqGroupHandle* tqGetGroupHandle(STQ* pTq, int64_t cId) { return NULL; } -int tqLaunchQuery(TqGroupHandle* ghandle) { +int tqLaunchQuery(TqGroupHandle* gHandle) { return 0; } @@ -156,7 +156,7 @@ int tqSendLaunchQuery(TqGroupHandle* gHandle) { return 0; } -/*int tqMoveOffsetToNext(TqGroupHandle* ghandle) {*/ +/*int tqMoveOffsetToNext(TqGroupHandle* gHandle) {*/ /*return 0;*/ /*}*/ @@ -177,13 +177,13 @@ int tqConsume(STQ* pTq, TmqConsumeReq* pMsg) { return -1; } int64_t clientId = pMsg->head.clientId; - TqGroupHandle *ghandle = tqGetGroupHandle(pTq, clientId); - if(ghandle == NULL) { + TqGroupHandle *gHandle = tqGetGroupHandle(pTq, clientId); + if(gHandle == NULL) { //client not connect return -1; } if(pMsg->acks.ackNum != 0) { - if(tqAck(ghandle, &pMsg->acks) != 0) { + if(tqAck(gHandle, &pMsg->acks) != 0) { //ack not success return -1; } @@ -191,13 +191,13 @@ int tqConsume(STQ* pTq, TmqConsumeReq* pMsg) { TmqConsumeRsp *pRsp = (TmqConsumeRsp*) pMsg; - if(tqFetch(ghandle, (void**)&pRsp->msgs) <= 0) { + if(tqFetch(gHandle, (void**)&pRsp->msgs) <= 0) { //fetch error return -1; } //judge and launch new query - if(tqLaunchQuery(ghandle)) { + if(tqLaunchQuery(gHandle)) { //launch query error return -1; } @@ -206,7 +206,7 @@ int tqConsume(STQ* pTq, TmqConsumeReq* pMsg) { int tqSerializeGroupHandle(TqGroupHandle *gHandle, void** ppBytes) { //calculate size - int sz = tqGetGHandleSSize(gHandle); + int sz = tqGetgHandleSSize(gHandle); void* ptr = realloc(*ppBytes, sz); if(ptr == NULL) { free(ppBytes); @@ -313,7 +313,7 @@ const void* tqDeserializeBufItem(const void* pBytes, TqBufferItem *bufItem) { } //TODO: make this a macro -int tqGetGHandleSSize(const TqGroupHandle *gHandle) { +int tqGetgHandleSSize(const TqGroupHandle *gHandle) { return sizeof(int64_t) * 2 + sizeof(int32_t) + gHandle->topicNum * tqBufHandleSSize(); diff --git a/source/server/vnode/tq/src/tqMetaStore.c b/source/server/vnode/tq/src/tqMetaStore.c index d652058c74..a93db7851b 100644 --- a/source/server/vnode/tq/src/tqMetaStore.c +++ b/source/server/vnode/tq/src/tqMetaStore.c @@ -15,17 +15,28 @@ #include "tqMetaStore.h" //TODO:replace by a abstract file layer #include +#include #include +#define TQ_PAGE_SIZE 4096 +#define TQ_META_NAME "tq.meta" +#define TQ_IDX_NAME "tq.idx" + typedef struct TqMetaPageBuf { int16_t offset; char buffer[TQ_PAGE_SIZE]; } TqMetaPageBuf; -TqMetaStore* tqStoreOpen(const char* path, void* serializer(void*), - void* deserializer(void*), void deleter(void*)) { +TqMetaStore* tqStoreOpen(const char* path, + int serializer(TqGroupHandle*, void**), + const void* deserializer(const void*, TqGroupHandle*), + void deleter(void*)) { //concat data file name and index file name - int fileFd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0755); + size_t pathLen = strlen(path); + char name[pathLen+10]; + strcpy(name, path); + strcat(name, "/" TQ_META_NAME); + int fileFd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0755); if(fileFd < 0) return NULL; TqMetaStore* pMeta = malloc(sizeof(TqMetaStore)); if(pMeta == NULL) { @@ -35,7 +46,9 @@ TqMetaStore* tqStoreOpen(const char* path, void* serializer(void*), memset(pMeta, 0, sizeof(TqMetaStore)); pMeta->fileFd = fileFd; - int idxFd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0755); + strcpy(name, path); + strcat(name, "/" TQ_IDX_NAME); + int idxFd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0755); if(idxFd < 0) { //close file //free memory @@ -56,8 +69,29 @@ TqMetaStore* tqStoreOpen(const char* path, void* serializer(void*), int32_t tqStoreClose(TqMetaStore* pMeta) { //commit data and idx - //close file + tqStorePersist(pMeta); + ASSERT(pMeta->unpersistHead && pMeta->unpersistHead->next==NULL); + close(pMeta->fileFd); + close(pMeta->idxFd); //free memory + for(int i = 0; i < TQ_BUCKET_SIZE; i++) { + TqMetaList* node = pMeta->bucket[i]; + pMeta->bucket[i] = NULL; + while(node) { + ASSERT(node->unpersistNext == NULL); + ASSERT(node->unpersistPrev == NULL); + if(node->handle.valueInTxn) { + pMeta->deleter(node->handle.valueInTxn); + } + if(node->handle.valueInUse) { + pMeta->deleter(node->handle.valueInUse); + } + TqMetaList* next = node->next; + free(node); + node = next; + } + } + free(pMeta); return 0; } @@ -69,44 +103,202 @@ int32_t tqStoreDelete(TqMetaStore* pMeta) { } int32_t tqStorePersist(TqMetaStore* pMeta) { - TqMetaList *node = pMeta->unpersistHead; - while(node->unpersistNext) { + int64_t idxBuf[3]; + TqMetaList *pHead = pMeta->unpersistHead; + TqMetaList *pNode = pHead->unpersistNext; + while(pHead != pNode) { + ASSERT(pNode->handle.valueInUse != NULL); //serialize + void* pBytes = NULL; + int sz = pMeta->serializer(pNode->handle.valueInUse, &pBytes); + ASSERT(pBytes != NULL); + //get current offset //append data - //write offset and idx + int nBytes = write(pMeta->fileFd, pBytes, sz); + //TODO: handle error in tfile + ASSERT(nBytes == sz); + + //write idx + //TODO: endian check and convert + idxBuf[0] = pNode->handle.key; + idxBuf[1] = pNode->handle.offset; + idxBuf[2] = (int64_t)sz; + nBytes = write(pMeta->idxFd, idxBuf, sizeof(idxBuf)); + //TODO: handle error in tfile + ASSERT(nBytes == sizeof(idxBuf)); + //remove from unpersist list + pHead->unpersistNext = pNode->unpersistNext; + pHead->unpersistNext->unpersistPrev = pHead; + + pNode->unpersistPrev = pNode->unpersistNext = NULL; + pNode = pHead->unpersistNext; + } + //TODO:fsync and return upper layer + return 0; +} + +int32_t tqHandlePutInUse(TqMetaStore* pMeta, int64_t key, void* value) { + int64_t bucketKey = key & TQ_BUCKET_SIZE; + TqMetaList* pNode = pMeta->bucket[bucketKey]; + while(pNode) { + if(pNode->handle.key == key) { + //TODO: think about thread safety + pMeta->deleter(pNode->handle.valueInUse); + //change pointer ownership + pNode->handle.valueInUse = value; + } else { + pNode = pNode->next; + } } return 0; } -int32_t tqHandlePutInUse(TqMetaStore* pMeta, TqMetaHandle* handle) { - return 0; -} - TqMetaHandle* tqHandleGetInUse(TqMetaStore* pMeta, int64_t key) { + int64_t bucketKey = key & TQ_BUCKET_SIZE; + TqMetaList* pNode = pMeta->bucket[bucketKey]; + while(pNode) { + if(pNode->handle.key == key) { + if(pNode->handle.valueInUse != NULL) { + return &pNode->handle; + } else { + return NULL; + } + } else { + pNode = pNode->next; + } + } return NULL; } -int32_t tqHandlePutInTxn(TqMetaStore* pMeta, TqMetaHandle* handle) { +int32_t tqHandlePutInTxn(TqMetaStore* pMeta, int64_t key, void* value) { + int64_t bucketKey = key & TQ_BUCKET_SIZE; + TqMetaList* pNode = pMeta->bucket[bucketKey]; + while(pNode) { + if(pNode->handle.key == key) { + //TODO: think about thread safety + pMeta->deleter(pNode->handle.valueInTxn); + //change pointer ownership + pNode->handle.valueInTxn = value; + } else { + pNode = pNode->next; + } + } return 0; } TqMetaHandle* tqHandleGetInTxn(TqMetaStore* pMeta, int64_t key) { + int64_t bucketKey = key & TQ_BUCKET_SIZE; + TqMetaList* pNode = pMeta->bucket[bucketKey]; + while(pNode) { + if(pNode->handle.key == key) { + if(pNode->handle.valueInTxn != NULL) { + return &pNode->handle; + } else { + return NULL; + } + } else { + pNode = pNode->next; + } + } return NULL; } int32_t tqHandleCommit(TqMetaStore* pMeta, int64_t key) { - return 0; + int64_t bucketKey = key & TQ_BUCKET_SIZE; + TqMetaList* pNode = pMeta->bucket[bucketKey]; + while(pNode) { + if(pNode->handle.key == key) { + if(pNode->handle.valueInUse != NULL) { + pMeta->deleter(pNode->handle.valueInUse); + } + pNode->handle.valueInUse = pNode->handle.valueInTxn; + if(pNode->unpersistNext == NULL) { + pNode->unpersistNext = pMeta->unpersistHead->unpersistNext; + pNode->unpersistPrev = pMeta->unpersistHead; + pMeta->unpersistHead->unpersistNext->unpersistPrev = pNode; + pMeta->unpersistHead->unpersistNext = pNode; + } + return 0; + } else { + pNode = pNode->next; + } + } + return -1; } int32_t tqHandleAbort(TqMetaStore* pMeta, int64_t key) { - return 0; + int64_t bucketKey = key & TQ_BUCKET_SIZE; + TqMetaList* pNode = pMeta->bucket[bucketKey]; + while(pNode) { + if(pNode->handle.key == key) { + if(pNode->handle.valueInTxn != NULL) { + pMeta->deleter(pNode->handle.valueInTxn); + pNode->handle.valueInTxn = NULL; + return 0; + } + return -1; + } else { + pNode = pNode->next; + } + } + return -2; } int32_t tqHandleDel(TqMetaStore* pMeta, int64_t key) { - return 0; + int64_t bucketKey = key & TQ_BUCKET_SIZE; + TqMetaList* pNode = pMeta->bucket[bucketKey]; + while(pNode) { + if(pNode->handle.key == key) { + if(pNode->handle.valueInUse != NULL) { + pMeta->deleter(pNode->handle.valueInUse); + pNode->handle.valueInUse = NULL; + //if not in unpersist, put into unpersist + if(pNode->unpersistNext == NULL) { + pNode->unpersistNext = pMeta->unpersistHead->unpersistNext; + pNode->unpersistPrev = pMeta->unpersistHead; + pMeta->unpersistHead->unpersistNext->unpersistPrev = pNode; + pMeta->unpersistHead->unpersistNext = pNode; + } + return 0; + } + return -1; + } else { + pNode = pNode->next; + } + } + return -2; } int32_t tqHandleClear(TqMetaStore* pMeta, int64_t key) { - return 0; + int64_t bucketKey = key & TQ_BUCKET_SIZE; + TqMetaList* pNode = pMeta->bucket[bucketKey]; + bool exist = false; + while(pNode) { + if(pNode->handle.key == key) { + if(pNode->handle.valueInUse != NULL) { + exist = true; + pMeta->deleter(pNode->handle.valueInUse); + pNode->handle.valueInUse = NULL; + } + if(pNode->handle.valueInTxn != NULL) { + exist = true; + pMeta->deleter(pNode->handle.valueInTxn); + pNode->handle.valueInTxn = NULL; + } + if(exist) { + if(pNode->unpersistNext == NULL) { + pNode->unpersistNext = pMeta->unpersistHead->unpersistNext; + pNode->unpersistPrev = pMeta->unpersistHead; + pMeta->unpersistHead->unpersistNext->unpersistPrev = pNode; + pMeta->unpersistHead->unpersistNext = pNode; + } + return 0; + } + return -1; + } else { + pNode = pNode->next; + } + } + return -2; } From e42f02803462d050a4216b2c56cce3491bcbed4a Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Mon, 1 Nov 2021 16:45:48 +0800 Subject: [PATCH 08/33] refine tq meta interface --- source/server/vnode/tq/inc/tqMetaStore.h | 16 ++++++---------- source/server/vnode/tq/src/tqMetaStore.c | 12 ++++++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/source/server/vnode/tq/inc/tqMetaStore.h b/source/server/vnode/tq/inc/tqMetaStore.h index a82bf2a4a9..977980b2ae 100644 --- a/source/server/vnode/tq/inc/tqMetaStore.h +++ b/source/server/vnode/tq/inc/tqMetaStore.h @@ -61,19 +61,15 @@ int32_t tqStoreClose(TqMetaStore*); //int32_t TqStoreCommitAll(TqMetaStore*); int32_t tqStorePersist(TqMetaStore*); -TqMetaHandle* tqHandleGetInUse(TqMetaStore*, int64_t key); -int32_t tqHandlePutInUse(TqMetaStore*, int64_t key, void* value); -TqMetaHandle* tqHandleGetInTxn(TqMetaStore*, int64_t key); -int32_t tqHandlePutInTxn(TqMetaStore*, int64_t key, void* value); -//will replace old handle -//int32_t tqHandlePut(TqMetaStore*, TqMetaHandle* handle); -//delete in-use-handle, and put it in use +TqMetaHandle* tqHandleGet(TqMetaStore*, int64_t key); +int32_t tqHandlePut(TqMetaStore*, int64_t key, void* value); +//do commit int32_t tqHandleCommit(TqMetaStore*, int64_t key); -//delete in-txn-handle +//delete uncommitted int32_t tqHandleAbort(TqMetaStore*, int64_t key); -//delete in-use-handle +//delete committed int32_t tqHandleDel(TqMetaStore*, int64_t key); -//delete in-use-handle and in-txn-handle +//delete both committed and uncommitted int32_t tqHandleClear(TqMetaStore*, int64_t key); #ifdef __cplusplus diff --git a/source/server/vnode/tq/src/tqMetaStore.c b/source/server/vnode/tq/src/tqMetaStore.c index a93db7851b..90c2ad824b 100644 --- a/source/server/vnode/tq/src/tqMetaStore.c +++ b/source/server/vnode/tq/src/tqMetaStore.c @@ -22,6 +22,10 @@ #define TQ_META_NAME "tq.meta" #define TQ_IDX_NAME "tq.idx" + +static int32_t tqHandlePutCommitted(TqMetaStore*, int64_t key, void* value); +static TqMetaHandle* tqHandleGetUncommitted(TqMetaStore*, int64_t key); + typedef struct TqMetaPageBuf { int16_t offset; char buffer[TQ_PAGE_SIZE]; @@ -138,7 +142,7 @@ int32_t tqStorePersist(TqMetaStore* pMeta) { return 0; } -int32_t tqHandlePutInUse(TqMetaStore* pMeta, int64_t key, void* value) { +static int32_t tqHandlePutCommitted(TqMetaStore* pMeta, int64_t key, void* value) { int64_t bucketKey = key & TQ_BUCKET_SIZE; TqMetaList* pNode = pMeta->bucket[bucketKey]; while(pNode) { @@ -154,7 +158,7 @@ int32_t tqHandlePutInUse(TqMetaStore* pMeta, int64_t key, void* value) { return 0; } -TqMetaHandle* tqHandleGetInUse(TqMetaStore* pMeta, int64_t key) { +TqMetaHandle* tqHandleGet(TqMetaStore* pMeta, int64_t key) { int64_t bucketKey = key & TQ_BUCKET_SIZE; TqMetaList* pNode = pMeta->bucket[bucketKey]; while(pNode) { @@ -171,7 +175,7 @@ TqMetaHandle* tqHandleGetInUse(TqMetaStore* pMeta, int64_t key) { return NULL; } -int32_t tqHandlePutInTxn(TqMetaStore* pMeta, int64_t key, void* value) { +int32_t tqHandlePut(TqMetaStore* pMeta, int64_t key, void* value) { int64_t bucketKey = key & TQ_BUCKET_SIZE; TqMetaList* pNode = pMeta->bucket[bucketKey]; while(pNode) { @@ -187,7 +191,7 @@ int32_t tqHandlePutInTxn(TqMetaStore* pMeta, int64_t key, void* value) { return 0; } -TqMetaHandle* tqHandleGetInTxn(TqMetaStore* pMeta, int64_t key) { +static TqMetaHandle* tqHandleGetUncommitted(TqMetaStore* pMeta, int64_t key) { int64_t bucketKey = key & TQ_BUCKET_SIZE; TqMetaList* pNode = pMeta->bucket[bucketKey]; while(pNode) { From 2f36662750f163e7f9514d5c9e67c62007e3b11b Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 1 Nov 2021 19:49:44 +0800 Subject: [PATCH 09/33] remove mgmt from vnode --- include/common/taosmsg.h | 153 ++-- include/server/vnode/vnode.h | 118 +-- include/util/tqueue.h | 10 +- include/util/tworker.h | 4 +- source/libs/parser/src/parserUtil.c | 2 +- source/libs/transport/src/rpcMain.c | 8 +- source/server/dnode/src/dnodeInt.c | 16 +- source/server/dnode/src/dnodeMsg.c | 8 +- source/server/dnode/src/dnodeTrans.c | 211 +++--- source/server/mnode/src/mnodeWorker.c | 148 ++-- source/server/vnode/impl/inc/vnodeCommit.h | 2 + source/server/vnode/impl/inc/vnodeFile.h | 33 - source/server/vnode/impl/inc/vnodeInt.h | 80 +- source/server/vnode/impl/inc/vnodeMain.h | 42 -- .../server/vnode/impl/inc/vnodeMemAllocator.h | 2 +- source/server/vnode/impl/inc/vnodeMgmt.h | 40 - source/server/vnode/impl/inc/vnodeRead.h | 14 +- source/server/vnode/impl/inc/vnodeReadMsg.h | 46 -- source/server/vnode/impl/inc/vnodeWrite.h | 13 +- source/server/vnode/impl/inc/vnodeWriteMsg.h | 41 - source/server/vnode/impl/src/vnodeFile.c | 372 --------- source/server/vnode/impl/src/vnodeInt.c | 84 +-- source/server/vnode/impl/src/vnodeMain.c | 712 ------------------ source/server/vnode/impl/src/vnodeMgmt.c | 287 ------- source/server/vnode/impl/src/vnodeRead.c | 195 ----- source/server/vnode/impl/src/vnodeReadMsg.c | 342 --------- source/server/vnode/impl/src/vnodeWrite.c | 212 ------ source/server/vnode/impl/src/vnodeWriteMsg.c | 94 --- source/server/vnode/impl/test/CMakeLists.txt | 2 +- source/util/src/tqueue.c | 18 +- source/util/src/tworker.c | 12 +- src/client/src/tscServer.c | 60 +- src/client/src/tscUtil.c | 2 +- 33 files changed, 425 insertions(+), 2958 deletions(-) delete mode 100644 source/server/vnode/impl/inc/vnodeFile.h delete mode 100644 source/server/vnode/impl/inc/vnodeMain.h delete mode 100644 source/server/vnode/impl/inc/vnodeMgmt.h delete mode 100644 source/server/vnode/impl/inc/vnodeReadMsg.h delete mode 100644 source/server/vnode/impl/inc/vnodeWriteMsg.h delete mode 100644 source/server/vnode/impl/src/vnodeFile.c delete mode 100644 source/server/vnode/impl/src/vnodeMain.c delete mode 100644 source/server/vnode/impl/src/vnodeMgmt.c delete mode 100644 source/server/vnode/impl/src/vnodeReadMsg.c delete mode 100644 source/server/vnode/impl/src/vnodeWriteMsg.c diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index d571153c1a..dde737162b 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -40,87 +40,112 @@ enum { TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SUBMIT, "submit" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_QUERY, "query" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_FETCH, "fetch" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_TABLE, "create-table" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_TABLE, "drop-table" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_TABLE, "alter-table" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_UPDATE_TAG_VAL, "update-tag-val" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_CONNECT, "mq-connect" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_DISCONNECT, "mq-disconnect" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_TABLE_META, "table-meta" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_TABLES_META, "tables-meta" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_STABLE_VGROUP, "stable-vgroup" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_CONSUME, "mq-consume" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_QUERY, "mq-query" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_CONNECT, "mq-connect" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_DISCONNECT, "mq-disconnect" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_ACK, "mq-ack" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_RESET, "mq-reset" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_NETWORK_TEST, "nettest" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY0, "dummy0" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY1, "dummy1" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY2, "dummy2" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY3, "dummy3" ) - -// message from mnode to dnode -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_CREATE_TABLE, "create-table" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_DROP_TABLE, "drop-table" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_ALTER_TABLE, "alter-table" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_CREATE_VNODE, "create-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_DROP_VNODE, "drop-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_DROP_STABLE, "drop-stable" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_ALTER_STREAM, "alter-stream" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_CONFIG_DNODE, "config-dnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_ALTER_VNODE, "alter-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_SYNC_VNODE, "sync-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_CREATE_MNODE, "create-mnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MD_COMPACT_VNODE, "compact-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY4, "dummy4" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY5, "dummy5" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY6, "dummy6" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY7, "dummy7" ) - - -// message from client to mnode -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_CONNECT, "connect" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_CREATE_ACCT, "create-acct" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_ALTER_ACCT, "alter-acct" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_DROP_ACCT, "drop-acct" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_CREATE_USER, "create-user" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_ALTER_USER, "alter-user" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_DROP_USER, "drop-user" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_CREATE_DNODE, "create-dnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_DROP_DNODE, "drop-dnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_CREATE_DB, "create-db" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_CREATE_FUNCTION, "create-function" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_DROP_DB, "drop-db" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_DROP_FUNCTION, "drop-function" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_USE_DB, "use-db" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_ALTER_DB, "alter-db" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_SYNC_DB, "sync-db-replica" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_CREATE_TABLE, "create-table" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_DROP_TABLE, "drop-table" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_ALTER_TABLE, "alter-table" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_TABLE_META, "table-meta" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_STABLE_VGROUP, "stable-vgroup" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_COMPACT_VNODE, "compact-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_TABLES_META, "multiTable-meta" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_ALTER_STREAM, "alter-stream" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_SHOW, "show" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_RETRIEVE, "retrieve" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_KILL_QUERY, "kill-query" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_KILL_STREAM, "kill-stream" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_KILL_CONN, "kill-conn" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_CONFIG_DNODE, "cm-config-dnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_HEARTBEAT, "heartbeat" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_RETRIEVE_FUNC, "retrieve-func" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY8, "dummy8" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY9, "dummy9" ) + +// message from mnode to dnode +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_STABLE_IN, "create-stable" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_STABLE_IN, "alter-stable" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_STABLE_IN, "drop-stable" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_VNODE_IN, "create-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_VNODE_IN, "alter-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_VNODE_IN, "drop-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SYNC_VNODE_IN, "sync-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_COMPACT_VNODE_IN, "compact-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_MNODE_IN, "create-mnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_MNODE_IN, "drop-mnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CONFIG_DNODE_IN, "config-dnode" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY10, "dummy10" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY11, "dummy11" ) - -// message from dnode to mnode -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_CONFIG_TABLE, "config-table" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_CONFIG_VNODE, "config-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_STATUS, "status" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_GRANT, "grant" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_AUTH, "auth" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY12, "dummy12" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY13, "dummy13" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY14, "dummy14" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_NETWORK_TEST, "nettest" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY15, "dummy15" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY16, "dummy16" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY17, "dummy17" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY18, "dummy18" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY19, "dummy19" ) -// message for topic -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_CREATE_TP, "create-tp" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_DROP_TP, "drop-tp" ) -//TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_USE_TP, "use-tp" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CM_ALTER_TP, "alter-tp" ) + +// message from client to mnode +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CONNECT, "connect" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_ACCT, "create-acct" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_ACCT, "alter-acct" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_ACCT, "drop-acct" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_USER, "create-user" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_USER, "alter-user" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_USER, "drop-user" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_DNODE, "create-dnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CONFIG_DNODE, "config-dnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_DNODE, "drop-dnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_DB, "create-db" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_DB, "drop-db" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_USE_DB, "use-db" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_DB, "alter-db" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SYNC_DB, "sync-db" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_TOPIC, "create-topic" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_TOPIC, "drop-topic" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_TOPIC, "alter-topic" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_FUNCTION, "create-function" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_FUNCTION, "alter-function" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_FUNCTION, "drop-function" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_STABLE, "create-stable" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_STABLE, "drop-stable" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_STABLE, "alter-stable" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_KILL_QUERY, "kill-query" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_KILL_CONN, "kill-conn" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_HEARTBEAT, "heartbeat" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SHOW, "show" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SHOW_RETRIEVE, "retrieve" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SHOW_RETRIEVE_FUNC, "retrieve-func" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_COMPACT_VNODE, "compact-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY20, "dummy20" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY21, "dummy21" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY22, "dummy22" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY23, "dummy23" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY24, "dummy24" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY25, "dummy25" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY26, "dummy26" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY27, "dummy27" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY28, "dummy28" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY29, "dummy29" ) + +// message from dnode to mnode +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_STATUS, "status" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_GRANT, "grant" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_AUTH, "auth" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY30, "dummy30" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY31, "dummy31" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY32, "dummy32" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY33, "dummy33" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY34, "dummy34" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY35, "dummy35" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY36, "dummy36" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY37, "dummy37" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY38, "dummy38" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY39, "dummy39" ) #ifndef TAOS_MESSAGE_C TSDB_MSG_TYPE_MAX // 147 diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index ecb1412b06..36112a2cf8 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -16,93 +16,111 @@ #ifndef _TD_VNODE_H_ #define _TD_VNODE_H_ +#include "os.h" +#include "taosmsg.h" +#include "trpc.h" + #ifdef __cplusplus extern "C" { #endif -typedef struct { - /** - * Send messages to other dnodes, such as create vnode message. - * - * @param epSet, the endpoint list of dnodes. - * @param rpcMsg, message to be sent. - */ - void (*SendMsgToDnode)(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg); - - /** - * Send messages to mnode, such as config message. - * - * @param rpcMsg, message to be sent. - */ - void (*SendMsgToMnode)(struct SRpcMsg *rpcMsg); - - /** - * Get the corresponding endpoint information from dnodeId. - * - * @param dnodeId, the id ot dnode. - * @param ep, the endpoint of dnode. - * @param fqdn, the fqdn of dnode. - * @param port, the port of dnode. - */ - void (*GetDnodeEp)(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); - - /** - * Report the startup progress. - */ - void (*ReportStartup)(char *name, char *desc); - -} SVnodeFp; +typedef struct SVnode SVnode; typedef struct { - SVnodeFp fp; -} SVnodePara; + char dbName[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + int32_t cacheBlockSize; // MB + int32_t totalBlocks; + int32_t daysPerFile; + int32_t daysToKeep0; + int32_t daysToKeep1; + int32_t daysToKeep2; + int32_t minRowsPerFileBlock; + int32_t maxRowsPerFileBlock; + int8_t precision; // time resolution + int8_t compression; + int8_t cacheLastRow; + int8_t update; + int8_t quorum; + int8_t replica; + int8_t walLevel; + int32_t fsyncPeriod; // millisecond + SVnodeDesc replicas[TSDB_MAX_REPLICA]; +} SVnodeCfg; + +typedef struct { + int64_t totalStorage; + int64_t compStorage; + int64_t pointsWritten; + int64_t tablesNum; +} SVnodeStatisic; + +typedef struct { + int8_t syncRole; +} SVnodeStatus; + +typedef struct { + int32_t accessState; +} SVnodeAccess; + +typedef struct SVnodeMsg { + int32_t msgType; + int32_t code; + SRpcMsg rpcMsg; // original message from rpc + int32_t contLen; + char pCont[]; +} SVnodeMsg; /** * Start initialize vnode module. * - * @param para, initialization parameters. * @return Error code. */ -int32_t vnodeInit(SVnodePara para); +int32_t vnodeInit(); /** * Cleanup vnode module. */ void vnodeCleanup(); -typedef struct { - int32_t unused; -} SVnodeStat; - /** * Get the statistical information of vnode. * - * @param stat, statistical information. + * @param pVnode, + * @param pStat, statistical information. * @return Error Code. */ -int32_t vnodeGetStatistics(SVnodeStat *stat); +int32_t vnodeGetStatistics(SVnode *pVnode, SVnodeStatisic *pStat); /** * Get the status of all vnodes. * - * @param status, status msg. + * @param pVnode, + * @param status, status information. + * @return Error Code. */ -void vnodeGetStatus(struct SStatusMsg *status); +int32_t vnodeGetStatus(SVnode *pVnode, SVnodeStatus *pStatus); /** - * Set access permissions for all vnodes. + * Operation functions of vnode * - * @param access, access permissions of vnodes. - * @param numOfVnodes, the size of vnodes. + * @return Error Code. */ -void vnodeSetAccess(struct SVgroupAccess *access, int32_t numOfVnodes); +SVnode *vnodeOpen(int32_t vgId, const char *path); +void vnodeClose(SVnode *pVnode); +int32_t vnodeAlter(SVnode *pVnode, const SVnodeCfg *pCfg); +SVnode *vnodeCreate(int32_t vgId, const char *path, const SVnodeCfg *pCfg); +int32_t vnodeDrop(SVnode *pVnode); +int32_t vnodeCompact(SVnode *pVnode); +int32_t vnodeSync(SVnode *pVnode); /** * Interface for processing messages. * - * @param msg, message to be processed. + * @param pVnode, + * @param pMsg, message to be processed. + * */ -void vnodeProcessMsg(SRpcMsg *msg); +int32_t vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg); #ifdef __cplusplus } diff --git a/include/util/tqueue.h b/include/util/tqueue.h index dc16222c2b..faac1afe70 100644 --- a/include/util/tqueue.h +++ b/include/util/tqueue.h @@ -40,12 +40,12 @@ shall be used to set up the protection. typedef void *taos_queue; typedef void *taos_qset; typedef void *taos_qall; -typedef void *(*FProcessOneItem)(void *pItem, void *ahandle); -typedef void *(*FProcessAllItem)(taos_qall qall, int numOfItems, void *ahandle); +typedef void *(*FProcessItem)(void *pItem, void *ahandle); +typedef void *(*FProcessItems)(taos_qall qall, int numOfItems, void *ahandle); taos_queue taosOpenQueue(); void taosCloseQueue(taos_queue); -void taosSetQueueFp(taos_queue, FProcessOneItem, FProcessAllItem fp); +void taosSetQueueFp(taos_queue, FProcessItem, FProcessItems); void *taosAllocateQitem(int size); void taosFreeQitem(void *pItem); int taosWriteQitem(taos_queue, void *pItem); @@ -64,8 +64,8 @@ int taosAddIntoQset(taos_qset, taos_queue, void *ahandle); void taosRemoveFromQset(taos_qset, taos_queue); int taosGetQueueNumber(taos_qset); -int taosReadQitemFromQset(taos_qset, void **pItem, void **ahandle, FProcessOneItem *); -int taosReadAllQitemsFromQset(taos_qset, taos_qall, void **ahandle, FProcessAllItem *); +int taosReadQitemFromQset(taos_qset, void **pItem, void **ahandle, FProcessItem *); +int taosReadAllQitemsFromQset(taos_qset, taos_qall, void **ahandle, FProcessItems *); int taosGetQueueItemsNumber(taos_queue param); int taosGetQsetItemsNumber(taos_qset param); diff --git a/include/util/tworker.h b/include/util/tworker.h index 591ebf8967..9b0fe4f3a5 100644 --- a/include/util/tworker.h +++ b/include/util/tworker.h @@ -56,12 +56,12 @@ typedef struct SMWorkerPool { int32_t tWorkerInit(SWorkerPool *pool); void tWorkerCleanup(SWorkerPool *pool); -taos_queue tWorkerAllocQueue(SWorkerPool *pool, void *ahandle, FProcessOneItem fp); +taos_queue tWorkerAllocQueue(SWorkerPool *pool, void *ahandle, FProcessItem fp); void tWorkerFreeQueue(SWorkerPool *pool, taos_queue queue); int32_t tMWorkerInit(SMWorkerPool *pool); void tMWorkerCleanup(SMWorkerPool *pool); -taos_queue tMWorkerAllocQueue(SMWorkerPool *pool, void *ahandle, FProcessAllItem fp); +taos_queue tMWorkerAllocQueue(SMWorkerPool *pool, void *ahandle, FProcessItems fp); void tMWorkerFreeQueue(SMWorkerPool *pool, taos_queue queue); #ifdef __cplusplus diff --git a/source/libs/parser/src/parserUtil.c b/source/libs/parser/src/parserUtil.c index c970283ca7..0bef796026 100644 --- a/source/libs/parser/src/parserUtil.c +++ b/source/libs/parser/src/parserUtil.c @@ -1841,7 +1841,7 @@ int tscTransferTableNameList(SSqlObj *pSql, const char *pNameList, int32_t lengt SSqlCmd *pCmd = &pSql->cmd; pCmd->command = TSDB_SQL_MULTI_META; - pCmd->msgType = TSDB_MSG_TYPE_CM_TABLES_META; + pCmd->msgType = TSDB_MSG_TYPE_TABLES_META; int code = TSDB_CODE_TSC_INVALID_TABLE_ID_LENGTH; char *str = (char *)pNameList; diff --git a/source/libs/transport/src/rpcMain.c b/source/libs/transport/src/rpcMain.c index fb555ea33f..eecde288b1 100644 --- a/source/libs/transport/src/rpcMain.c +++ b/source/libs/transport/src/rpcMain.c @@ -403,10 +403,10 @@ void rpcSendRequest(void *shandle, const SRpcEpSet *pEpSet, SRpcMsg *pMsg, int64 // connection type is application specific. // for TDengine, all the query, show commands shall have TCP connection char type = pMsg->msgType; - if (type == TSDB_MSG_TYPE_QUERY || type == TSDB_MSG_TYPE_CM_RETRIEVE - || type == TSDB_MSG_TYPE_FETCH || type == TSDB_MSG_TYPE_CM_STABLE_VGROUP - || type == TSDB_MSG_TYPE_CM_TABLES_META || type == TSDB_MSG_TYPE_CM_TABLE_META - || type == TSDB_MSG_TYPE_CM_SHOW || type == TSDB_MSG_TYPE_DM_STATUS || type == TSDB_MSG_TYPE_CM_ALTER_TABLE) + if (type == TSDB_MSG_TYPE_QUERY || type == TSDB_MSG_TYPE_SHOW_RETRIEVE + || type == TSDB_MSG_TYPE_FETCH || type == TSDB_MSG_TYPE_STABLE_VGROUP + || type == TSDB_MSG_TYPE_TABLES_META || type == TSDB_MSG_TYPE_TABLE_META + || type == TSDB_MSG_TYPE_SHOW || type == TSDB_MSG_TYPE_DM_STATUS || type == TSDB_MSG_TYPE_ALTER_TABLE) pContext->connType = RPC_CONN_TCPC; pContext->rid = taosAddRef(tsRpcRefId, pContext); diff --git a/source/server/dnode/src/dnodeInt.c b/source/server/dnode/src/dnodeInt.c index 7b0b87368e..1166a06d38 100644 --- a/source/server/dnode/src/dnodeInt.c +++ b/source/server/dnode/src/dnodeInt.c @@ -54,13 +54,7 @@ static void dnodeReportStartupFinished(char *name, char *desc) { void dnodeGetStartup(SStartupStep *pStep) { memcpy(pStep, &tsDnode.startup, sizeof(SStartupStep)); } static int32_t dnodeInitVnode() { - SVnodePara para; - para.fp.GetDnodeEp = dnodeGetEp; - para.fp.SendMsgToDnode = dnodeSendMsgToDnode; - para.fp.SendMsgToMnode = dnodeSendMsgToMnode; - para.fp.ReportStartup = dnodeReportStartup; - - return vnodeInit(para); + return vnodeInit(); } static int32_t dnodeInitMnode() { @@ -200,3 +194,11 @@ void dnodeCleanup() { tsDnode.steps = NULL; } } + +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = vnodeProcessMgmtMsg; +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = vnodeProcessMgmtMsg; +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = vnodeProcessMgmtMsg; +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessMgmtMsg; +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessMgmtMsg; +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessMgmtMsg; + \ No newline at end of file diff --git a/source/server/dnode/src/dnodeMsg.c b/source/server/dnode/src/dnodeMsg.c index 6df01a5b7f..a5c3db5e14 100644 --- a/source/server/dnode/src/dnodeMsg.c +++ b/source/server/dnode/src/dnodeMsg.c @@ -52,9 +52,9 @@ static void dnodeSendStatusMsg() { tstrncpy(pStatus->clusterCfg.locale, tsLocale, TSDB_LOCALE_LEN); tstrncpy(pStatus->clusterCfg.charset, tsCharset, TSDB_LOCALE_LEN); - vnodeGetStatus(pStatus); - contLen = sizeof(SStatusMsg) + pStatus->openVnodes * sizeof(SVnodeLoad); - pStatus->openVnodes = htons(pStatus->openVnodes); + // vnodeGetStatus(NULL, pStatus); + // contLen = sizeof(SStatusMsg) + pStatus->openVnodes * sizeof(SVnodeLoad); + // pStatus->openVnodes = htons(pStatus->openVnodes); SRpcMsg rpcMsg = {.ahandle = NULL, .pCont = pStatus, .contLen = contLen, .msgType = TSDB_MSG_TYPE_DM_STATUS}; @@ -79,7 +79,7 @@ void dnodeProcessStatusRsp(SRpcMsg *pMsg) { return; } - vnodeSetAccess(pStatusRsp->vgAccess, pCfg->numOfVnodes); + // vnodeSetAccess(pStatusRsp->vgAccess, pCfg->numOfVnodes); SDnodeEps *eps = (SDnodeEps *)((char *)pStatusRsp->vgAccess + pCfg->numOfVnodes * sizeof(SVgroupAccess)); eps->dnodeNum = htonl(eps->dnodeNum); diff --git a/source/server/dnode/src/dnodeTrans.c b/source/server/dnode/src/dnodeTrans.c index 4eba31ab68..3e32510df9 100644 --- a/source/server/dnode/src/dnodeTrans.c +++ b/source/server/dnode/src/dnodeTrans.c @@ -26,18 +26,101 @@ #include "mnode.h" #include "vnode.h" -typedef void (*RpcMsgFp)(SRpcMsg *pMsg); +typedef void (*MsgFp)(SRpcMsg *pMsg); static struct { - void *serverRpc; - void *clientRpc; - void *shellRpc; - int32_t queryReqNum; - int32_t submitReqNum; - RpcMsgFp peerMsgFp[TSDB_MSG_TYPE_MAX]; - RpcMsgFp shellMsgFp[TSDB_MSG_TYPE_MAX]; + void *serverRpc; + void *clientRpc; + void *shellRpc; + MsgFp msgFp[TSDB_MSG_TYPE_MAX]; } tsTrans; +static void dnodeInitMsgFp() { + // msg from client to dnode + tsTrans.msgFp[TSDB_MSG_TYPE_SUBMIT] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_QUERY] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TABLE] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TABLE] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TABLE] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_TABLE_META] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_TABLES_META] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_QUERY] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONNECT] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_ACK] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_RESET] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_NETWORK_TEST] = dnodeProcessStartupReq; + + // msg from client to mnode + tsTrans.msgFp[TSDB_MSG_TYPE_CONNECT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_ACCT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_ACCT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_ACCT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_USER] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_USER] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_USER] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_DNODE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_DNODE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_DB] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_DB] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_USE_DB] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_DB] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_DB] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TOPIC] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TOPIC] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TOPIC] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_FUNCTION] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_FUNCTION] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_FUNCTION] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_KILL_QUERY] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_KILL_CONN] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_HEARTBEAT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SHOW] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE_FUNC] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE] = mnodeProcessMsg; + + // message from mnode to dnode + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN] = dnodeProcessCreateMnodeReq; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN] = NULL; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE_IN] = NULL; + tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE_IN_RSP] = mnodeProcessMsg; + + // message from dnode to mnode + tsTrans.msgFp[TSDB_MSG_TYPE_DM_AUTH] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DM_AUTH_RSP] = NULL; + tsTrans.msgFp[TSDB_MSG_TYPE_DM_GRANT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DM_GRANT_RSP] = NULL; + tsTrans.msgFp[TSDB_MSG_TYPE_DM_STATUS] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DM_STATUS_RSP] = dnodeProcessStatusRsp; +} + static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0}; int32_t msgType = pMsg->msgType; @@ -61,7 +144,7 @@ static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { return; } - RpcMsgFp fp = tsTrans.peerMsgFp[msgType]; + MsgFp fp = tsTrans.msgFp[msgType]; if (fp != NULL) { dTrace("RPC %p, peer req:%s will be processed", pMsg->handle, taosMsg[msgType]); (*fp)(pMsg); @@ -74,35 +157,6 @@ static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { } static int32_t dnodeInitServer() { - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_DROP_TABLE] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_TABLE] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_DROP_STABLE] = vnodeProcessMsg; - - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessMsg; - - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessMsg; - - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE] = dnodeProcessConfigDnodeReq; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_MNODE] = dnodeProcessCreateMnodeReq; - - tsTrans.peerMsgFp[TSDB_MSG_TYPE_DM_CONFIG_TABLE] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_DM_CONFIG_VNODE] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_DM_AUTH] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_DM_GRANT] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_DM_STATUS] = mnodeProcessMsg; - - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MQ_CONNECT] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MQ_ACK] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MQ_RESET] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MQ_CONSUME] = vnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MQ_QUERY] = vnodeProcessMsg; - SRpcInit rpcInit; memset(&rpcInit, 0, sizeof(rpcInit)); rpcInit.localPort = tsDnodeDnodePort; @@ -145,7 +199,7 @@ static void dnodeProcessPeerRsp(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { dnodeUpdateMnodeEps(pEpSet); } - RpcMsgFp fp = tsTrans.peerMsgFp[msgType]; + MsgFp fp = tsTrans.msgFp[msgType]; if (fp != NULL) { dTrace("RPC %p, peer rsp:%s will be processed", pMsg->handle, taosMsg[msgType]); (*fp)(pMsg); @@ -157,28 +211,6 @@ static void dnodeProcessPeerRsp(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { } static int32_t dnodeInitClient() { - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_DROP_TABLE_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_TABLE_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_DROP_STABLE_RSP] = mnodeProcessMsg; - - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_DROP_VNODE_RSP] = mnodeProcessMsg; - - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM_RSP] = mnodeProcessMsg; - - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_MD_CREATE_MNODE_RSP] = mnodeProcessMsg; - - tsTrans.peerMsgFp[TSDB_MSG_TYPE_DM_CONFIG_TABLE_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_DM_CONFIG_VNODE_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_DM_AUTH_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_DM_GRANT_RSP] = mnodeProcessMsg; - tsTrans.peerMsgFp[TSDB_MSG_TYPE_DM_STATUS_RSP] = dnodeProcessStatusRsp; - char secret[TSDB_KEY_LEN] = "secret"; SRpcInit rpcInit; memset(&rpcInit, 0, sizeof(rpcInit)); @@ -234,14 +266,7 @@ static void dnodeProcessShellReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { return; } - if (msgType == TSDB_MSG_TYPE_QUERY) { - atomic_fetch_add_32(&tsTrans.queryReqNum, 1); - } else if (msgType == TSDB_MSG_TYPE_SUBMIT) { - atomic_fetch_add_32(&tsTrans.submitReqNum, 1); - } else { - } - - RpcMsgFp fp = tsTrans.shellMsgFp[msgType]; + MsgFp fp = tsTrans.msgFp[msgType]; if (fp != NULL) { dTrace("RPC %p, shell req:%s will be processed", pMsg->handle, taosMsg[msgType]); (*fp)(pMsg); @@ -299,54 +324,6 @@ static int32_t dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, c } static int32_t dnodeInitShell() { - tsTrans.shellMsgFp[TSDB_MSG_TYPE_SUBMIT] = vnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_QUERY] = vnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = vnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_MQ_QUERY] = vnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_MQ_CONSUME] = vnodeProcessMsg; - - // the following message shall be treated as mnode write - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_ACCT] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_ACCT] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_DROP_ACCT] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_USER] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_USER] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_DROP_USER] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_DNODE] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_DROP_DNODE] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_DB] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_TP] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_FUNCTION] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_DROP_DB] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_SYNC_DB] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_DROP_TP] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_DROP_FUNCTION] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_DB] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_TP] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_CREATE_TABLE] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_DROP_TABLE] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_TABLE] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_ALTER_STREAM] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_KILL_QUERY] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_KILL_STREAM] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_KILL_CONN] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_CONFIG_DNODE] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_COMPACT_VNODE] = mnodeProcessMsg; - - // the following message shall be treated as mnode query - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_HEARTBEAT] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_CONNECT] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_USE_DB] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_TABLE_META] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_STABLE_VGROUP] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_TABLES_META] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_SHOW] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_RETRIEVE] = mnodeProcessMsg; - tsTrans.shellMsgFp[TSDB_MSG_TYPE_CM_RETRIEVE_FUNC] = mnodeProcessMsg; - - tsTrans.shellMsgFp[TSDB_MSG_TYPE_NETWORK_TEST] = dnodeProcessStartupReq; - int32_t numOfThreads = (int32_t)((tsNumOfCores * tsNumOfThreadsPerCore) / 2.0); if (numOfThreads < 1) { numOfThreads = 1; diff --git a/source/server/mnode/src/mnodeWorker.c b/source/server/mnode/src/mnodeWorker.c index fc370538aa..a477a11f7c 100644 --- a/source/server/mnode/src/mnodeWorker.c +++ b/source/server/mnode/src/mnodeWorker.c @@ -201,87 +201,87 @@ static void mnodeInitMsgFp() { // tsMworker.peerRspFp[TSDB_MSG_TYPE_MD_DROP_VNODE_RSP] = mnodeProcessDropVnodeRsp; // // read msg -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_HEARTBEAT] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_HEARTBEAT] = mnodeProcessHeartBeatMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_CONNECT] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_CONNECT] = mnodeProcessConnectMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_USE_DB] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_USE_DB] = mnodeProcessUseMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_HEARTBEAT] = mnodeDispatchToReadQueue; +// tsMworker.readMsgFp[TSDB_MSG_TYPE_HEARTBEAT] = mnodeProcessHeartBeatMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_CONNECT] = mnodeDispatchToReadQueue; +// tsMworker.readMsgFp[TSDB_MSG_TYPE_CONNECT] = mnodeProcessConnectMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_USE_DB] = mnodeDispatchToReadQueue; +// tsMworker.readMsgFp[TSDB_MSG_TYPE_USE_DB] = mnodeProcessUseMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_TABLE_META] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_TABLE_META] = mnodeProcessTableMetaMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_TABLES_META] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_TABLES_META] = mnodeProcessMultiTableMetaMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_STABLE_VGROUP] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_STABLE_VGROUP] = mnodeProcessSuperTableVgroupMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_TABLE_META] = mnodeDispatchToReadQueue; +// tsMworker.readMsgFp[TSDB_MSG_TYPE_TABLE_META] = mnodeProcessTableMetaMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_TABLES_META] = mnodeDispatchToReadQueue; +// tsMworker.readMsgFp[TSDB_MSG_TYPE_TABLES_META] = mnodeProcessMultiTableMetaMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = mnodeDispatchToReadQueue; +// tsMworker.readMsgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = mnodeProcessSuperTableVgroupMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_SHOW] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_SHOW] = mnodeProcessShowMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_RETRIEVE] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_RETRIEVE] = mnodeProcessRetrieveMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_RETRIEVE_FUNC] = mnodeDispatchToReadQueue; -// tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_RETRIEVE_FUNC] = mnodeProcessRetrieveFuncReq; +// tsMworker.msgFp[TSDB_MSG_TYPE_SHOW] = mnodeDispatchToReadQueue; +// tsMworker.readMsgFp[TSDB_MSG_TYPE_SHOW] = mnodeProcessShowMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE] = mnodeDispatchToReadQueue; +// tsMworker.readMsgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE] = mnodeProcessRetrieveMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_RETRIEVE_FUNC] = mnodeDispatchToReadQueue; +// tsMworker.readMsgFp[TSDB_MSG_TYPE_RETRIEVE_FUNC] = mnodeProcessRetrieveFuncReq; -// // tsMworker.msgFp[TSDB_MSG_TYPE_CM_CREATE_ACCT] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_CREATE_ACCT] = acctProcessCreateAcctMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_CM_ALTER_ACCT] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_ALTER_ACCT] = acctProcessDropAcctMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_CM_DROP_ACCT] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_DROP_ACCT] = acctProcessAlterAcctMsg; +// // tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_ACCT] = mnodeDispatchToWriteQueue; +// // tsMworker.readMsgFp[TSDB_MSG_TYPE_CREATE_ACCT] = acctProcessCreateAcctMsg; +// // tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_ACCT] = mnodeDispatchToWriteQueue; +// // tsMworker.readMsgFp[TSDB_MSG_TYPE_ALTER_ACCT] = acctProcessDropAcctMsg; +// // tsMworker.msgFp[TSDB_MSG_TYPE_DROP_ACCT] = mnodeDispatchToWriteQueue; +// // tsMworker.readMsgFp[TSDB_MSG_TYPE_DROP_ACCT] = acctProcessAlterAcctMsg; // // write msg -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_CREATE_USER] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_CREATE_USER] = mnodeProcessCreateUserMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_ALTER_USER] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_ALTER_USER] = mnodeProcessAlterUserMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_DROP_USER] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_DROP_USER] = mnodeProcessDropUserMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_USER] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CREATE_USER] = mnodeProcessCreateUserMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_USER] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_ALTER_USER] = mnodeProcessAlterUserMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_DROP_USER] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_DROP_USER] = mnodeProcessDropUserMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_CREATE_DNODE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_CREATE_DNODE] = mnodeProcessCreateDnodeMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_DROP_DNODE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_DROP_DNODE] = mnodeProcessDropDnodeMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_CONFIG_DNODE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_CONFIG_DNODE] = mnodeProcessCfgDnodeMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_DNODE] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CREATE_DNODE] = mnodeProcessCreateDnodeMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_DROP_DNODE] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_DROP_DNODE] = mnodeProcessDropDnodeMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CONFIG_DNODE] = mnodeProcessCfgDnodeMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_CREATE_DB] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_CREATE_DB] = mnodeProcessCreateDbMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_ALTER_DB] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_ALTER_DB] = mnodeProcessAlterDbMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_DROP_DB] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_DROP_DB] = mnodeProcessDropDbMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_SYNC_DB] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_SYNC_DB] = mnodeProcessSyncDbMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_COMPACT_VNODE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_COMPACT_VNODE] = mnodeProcessCompactMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_DB] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CREATE_DB] = mnodeProcessCreateDbMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_DB] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_ALTER_DB] = mnodeProcessAlterDbMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_DROP_DB] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_DROP_DB] = mnodeProcessDropDbMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_SYNC_DB] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_SYNC_DB] = mnodeProcessSyncDbMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_COMPACT_VNODE] = mnodeProcessCompactMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_CREATE_FUNCTION] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_CREATE_FUNCTION] = mnodeProcessCreateFuncMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_DROP_FUNCTION] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_DROP_FUNCTION] = mnodeProcessDropFuncMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_FUNCTION] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CREATE_FUNCTION] = mnodeProcessCreateFuncMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_DROP_FUNCTION] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_DROP_FUNCTION] = mnodeProcessDropFuncMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_CM_CREATE_TP] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_CREATE_TP] = tpProcessCreateTpMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_CM_DROP_TP] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_DROP_TP] = tpProcessAlterTpMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_CM_ALTER_TP] = mnodeDispatchToWriteQueue; -// // tsMworker.readMsgFp[TSDB_MSG_TYPE_CM_ALTER_TP] = tpProcessDropTpMsg; +// // tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_TP] = mnodeDispatchToWriteQueue; +// // tsMworker.readMsgFp[TSDB_MSG_TYPE_CREATE_TP] = tpProcessCreateTpMsg; +// // tsMworker.msgFp[TSDB_MSG_TYPE_DROP_TP] = mnodeDispatchToWriteQueue; +// // tsMworker.readMsgFp[TSDB_MSG_TYPE_DROP_TP] = tpProcessAlterTpMsg; +// // tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_TP] = mnodeDispatchToWriteQueue; +// // tsMworker.readMsgFp[TSDB_MSG_TYPE_ALTER_TP] = tpProcessDropTpMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_CREATE_TABLE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_CREATE_TABLE] = mnodeProcessCreateTableMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_DROP_TABLE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_DROP_TABLE] = mnodeProcessDropTableMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_ALTER_TABLE] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_ALTER_TABLE] = mnodeProcessAlterTableMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_CREATE_TABLE] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CREATE_TABLE] = mnodeProcessCreateTableMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_DROP_TABLE] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_DROP_TABLE] = mnodeProcessDropTableMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_TABLE] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_ALTER_TABLE] = mnodeProcessAlterTableMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_ALTER_STREAM] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_ALTER_STREAM] = NULL; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_KILL_QUERY] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_KILL_QUERY] = mnodeProcessKillQueryMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_KILL_STREAM] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_KILL_STREAM] = mnodeProcessKillStreamMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_CM_KILL_CONN] = mnodeDispatchToWriteQueue; -// tsMworker.writeMsgFp[TSDB_MSG_TYPE_CM_KILL_CONN] = mnodeProcessKillConnectionMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_ALTER_STREAM] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_ALTER_STREAM] = NULL; +// tsMworker.msgFp[TSDB_MSG_TYPE_KILL_QUERY] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_KILL_QUERY] = mnodeProcessKillQueryMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_KILL_STREAM] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_KILL_STREAM] = mnodeProcessKillStreamMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_KILL_CONN] = mnodeDispatchToWriteQueue; +// tsMworker.writeMsgFp[TSDB_MSG_TYPE_KILL_CONN] = mnodeProcessKillConnectionMsg; } static void mnodeProcessWriteReq(SMnMsg *pMsg, void *unused) { @@ -427,7 +427,7 @@ int32_t mnodeInitWorker() { if (tWorkerInit(pPool) != 0) { return TSDB_CODE_MND_OUT_OF_MEMORY; } else { - tsMworker.writeQ = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)mnodeProcessWriteReq); + tsMworker.writeQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)mnodeProcessWriteReq); } pPool = &tsMworker.read; @@ -439,7 +439,7 @@ int32_t mnodeInitWorker() { if (tWorkerInit(pPool) != 0) { return TSDB_CODE_MND_OUT_OF_MEMORY; } else { - tsMworker.readQ = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)mnodeProcessReadReq); + tsMworker.readQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)mnodeProcessReadReq); } pPool = &tsMworker.peerReq; @@ -449,7 +449,7 @@ int32_t mnodeInitWorker() { if (tWorkerInit(pPool) != 0) { return TSDB_CODE_MND_OUT_OF_MEMORY; } else { - tsMworker.peerReqQ = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)mnodeProcessPeerReq); + tsMworker.peerReqQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)mnodeProcessPeerReq); } pPool = &tsMworker.peerRsp; @@ -459,7 +459,7 @@ int32_t mnodeInitWorker() { if (tWorkerInit(pPool) != 0) { return TSDB_CODE_MND_OUT_OF_MEMORY; } else { - tsMworker.peerRspQ = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)mnodeProcessPeerRsp); + tsMworker.peerRspQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)mnodeProcessPeerRsp); } mInfo("mnode worker is initialized"); diff --git a/source/server/vnode/impl/inc/vnodeCommit.h b/source/server/vnode/impl/inc/vnodeCommit.h index d37c61008c..544a42f0e8 100644 --- a/source/server/vnode/impl/inc/vnodeCommit.h +++ b/source/server/vnode/impl/inc/vnodeCommit.h @@ -16,6 +16,8 @@ #ifndef _TD_VNODE_COMMIT_H_ #define _TD_VNODE_COMMIT_H_ +#include "vnodeInt.h" + #ifdef __cplusplus extern "C" { #endif diff --git a/source/server/vnode/impl/inc/vnodeFile.h b/source/server/vnode/impl/inc/vnodeFile.h deleted file mode 100644 index bea28324ee..0000000000 --- a/source/server/vnode/impl/inc/vnodeFile.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_VNODE_CFG_H_ -#define _TD_VNODE_CFG_H_ - -#ifdef __cplusplus -extern "C" { -#endif -#include "vnodeInt.h" - -int32_t vnodeReadCfg(int32_t vgId, SVnodeCfg *pCfg); -int32_t vnodeWriteCfg(int32_t vgId, SVnodeCfg *pCfg); -int32_t vnodeReadState(int32_t vgId, SSyncServerState *pState); -int32_t vnodeSaveState(int32_t vgid, SSyncServerState *pState); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_VNODE_CFG_H_*/ diff --git a/source/server/vnode/impl/inc/vnodeInt.h b/source/server/vnode/impl/inc/vnodeInt.h index 90d9e7105e..d7b84be83d 100644 --- a/source/server/vnode/impl/inc/vnodeInt.h +++ b/source/server/vnode/impl/inc/vnodeInt.h @@ -16,19 +16,14 @@ #ifndef _TD_VNODE_INT_H_ #define _TD_VNODE_INT_H_ -#include "os.h" +#include "vnode.h" + #include "amalloc.h" #include "meta.h" #include "sync.h" -#include "taosmsg.h" -#include "tglobal.h" #include "tlog.h" #include "tq.h" -#include "tqueue.h" -#include "trpc.h" #include "tsdb.h" -#include "tworker.h" -#include "vnode.h" #include "wal.h" #ifdef __cplusplus @@ -44,71 +39,16 @@ extern int32_t vDebugFlag; #define vDebug(...) { if (vDebugFlag & DEBUG_DEBUG) { taosPrintLog("VND ", vDebugFlag, __VA_ARGS__); }} #define vTrace(...) { if (vDebugFlag & DEBUG_TRACE) { taosPrintLog("VND ", vDebugFlag, __VA_ARGS__); }} -typedef struct STsdbCfg { - int32_t cacheBlockSize; // MB - int32_t totalBlocks; - int32_t daysPerFile; - int32_t daysToKeep0; - int32_t daysToKeep1; - int32_t daysToKeep2; - int32_t minRowsPerFileBlock; - int32_t maxRowsPerFileBlock; - uint8_t precision; // time resolution - int8_t compression; - int8_t cacheLastRow; - int8_t update; -} STsdbCfg; - -typedef struct SMetaCfg { -} SMetaCfg; - -typedef struct SVnodeCfg { - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; - int8_t dropped; - int8_t quorum; - SWalCfg wal; - STsdbCfg tsdb; - SMetaCfg meta; - SSyncCluster sync; -} SVnodeCfg; - -typedef struct { - int32_t vgId; // global vnode group ID - int32_t refCount; // reference count - SMemAllocator *allocator; - SMeta *pMeta; - STsdb *pTsdb; - STQ *pTQ; - SWal *pWal; - void *pQuery; - SSyncNode *pSync; - taos_queue pWriteQ; // write queue - taos_queue pQueryQ; // read query queue - taos_queue pFetchQ; // read fetch/cancel queue - SVnodeCfg cfg; - SSyncServerState term; - int64_t queuedWMsgSize; - int32_t queuedWMsg; - int32_t queuedRMsg; - int32_t numOfQHandle; // current initialized and existed query handle in current dnode - int8_t role; - int8_t accessState; - int8_t dropped; - int8_t status; - pthread_mutex_t statusMutex; +typedef struct SVnode { + int32_t vgId; + SVnodeCfg cfg; + SMeta *pMeta; + STsdb *pTsdb; + STQ *pTQ; + SWal *pWal; + SSyncNode *pSync; } SVnode; -typedef struct { - int32_t len; - void *rsp; - void *qhandle; // used by query and retrieve msg -} SVnRsp; - -void vnodeSendMsgToDnode(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg); -void vnodeSendMsgToMnode(struct SRpcMsg *rpcMsg); -void vnodeGetDnodeEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); -void vnodeReportStartup(char *name, char *desc); - #ifdef __cplusplus } #endif diff --git a/source/server/vnode/impl/inc/vnodeMain.h b/source/server/vnode/impl/inc/vnodeMain.h deleted file mode 100644 index d0d84792e8..0000000000 --- a/source/server/vnode/impl/inc/vnodeMain.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_VNODE_MAIN_H_ -#define _TD_VNODE_MAIN_H_ - -#include "vnodeInt.h" - -#ifdef __cplusplus -extern "C" { -#endif - -int32_t vnodeInitMain(); -void vnodeCleanupMain(); - -SVnode *vnodeAcquireInAllState(int32_t vgId); -SVnode *vnodeAcquire(int32_t vgId); -void vnodeRelease(SVnode *pVnode); - -int32_t vnodeCreateVnode(int32_t vgId, SVnodeCfg *pCfg); -int32_t vnodeAlterVnode(SVnode *pVnode, SVnodeCfg *pCfg); -int32_t vnodeDropVnode(SVnode *pVnode); -int32_t vnodeSyncVnode(SVnode *pVnode); -int32_t vnodeCompactVnode(SVnode *pVnode); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_VNODE_MAIN_H_*/ diff --git a/source/server/vnode/impl/inc/vnodeMemAllocator.h b/source/server/vnode/impl/inc/vnodeMemAllocator.h index ca00abc70a..76aa2c6714 100644 --- a/source/server/vnode/impl/inc/vnodeMemAllocator.h +++ b/source/server/vnode/impl/inc/vnodeMemAllocator.h @@ -16,7 +16,7 @@ #ifndef _TD_VNODE_MEM_ALLOCATOR_H_ #define _TD_VNODE_MEM_ALLOCATOR_H_ -#include "os.h" +#include "vnodeInt.h" #ifdef __cplusplus extern "C" { diff --git a/source/server/vnode/impl/inc/vnodeMgmt.h b/source/server/vnode/impl/inc/vnodeMgmt.h deleted file mode 100644 index ccd1b28b61..0000000000 --- a/source/server/vnode/impl/inc/vnodeMgmt.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_VNODE_MGMT_H_ -#define _TD_VNODE_MGMT_H_ - -#ifdef __cplusplus -extern "C" { -#endif -#include "vnodeInt.h" - - -typedef struct { - SVnode *pVnode; - SRpcMsg rpcMsg; - char pCont[]; -} SVnMgmtMsg; - - -int32_t vnodeInitMgmt(); -void vnodeCleanupMgmt(); -void vnodeProcessMgmtMsg(SRpcMsg *pMsg); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_VNODE_MGMT_H_*/ diff --git a/source/server/vnode/impl/inc/vnodeRead.h b/source/server/vnode/impl/inc/vnodeRead.h index e5efae3d93..5ce84b2ebf 100644 --- a/source/server/vnode/impl/inc/vnodeRead.h +++ b/source/server/vnode/impl/inc/vnodeRead.h @@ -21,19 +21,7 @@ extern "C" { #endif #include "vnodeInt.h" -int32_t vnodeInitRead(); -void vnodeCleanupRead(); -taos_queue vnodeAllocQueryQueue(SVnode *pVnode); -taos_queue vnodeAllocFetchQueue(SVnode *pVnode); -void vnodeFreeQueryQueue(taos_queue pQueue); -void vnodeFreeFetchQueue(taos_queue pQueue); - -void vnodeProcessReadMsg(SRpcMsg *pRpcMsg); -int32_t vnodeReputPutToRQueue(SVnode *pVnode, void **qhandle, void *ahandle); - -void vnodeStartRead(SVnode *pVnode); -void vnodeStopRead(SVnode *pVnode); -void vnodeWaitReadCompleted(SVnode *pVnode); +void vnodeProcessReadMsg(SVnode *pVnode, SVnodeMsg *pMsg); #ifdef __cplusplus } diff --git a/source/server/vnode/impl/inc/vnodeReadMsg.h b/source/server/vnode/impl/inc/vnodeReadMsg.h deleted file mode 100644 index 1efc74d1af..0000000000 --- a/source/server/vnode/impl/inc/vnodeReadMsg.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_VNODE_READ_MSG_H_ -#define _TD_VNODE_READ_MSG_H_ - -#ifdef __cplusplus -extern "C" { -#endif -#include "vnodeInt.h" - -typedef struct SReadMsg { - int32_t code; - int32_t contLen; - int8_t qtype; - int8_t msgType; - SVnode *pVnode; - SVnRsp rspRet; - void * rpcHandle; - void * rpcAhandle; - void * qhandle; - char pCont[]; -} SReadMsg; - -int32_t vnodeProcessQueryMsg(SVnode *pVnode, SReadMsg *pRead); -int32_t vnodeProcessFetchMsg(SVnode *pVnode, SReadMsg *pRead); -int32_t vnodeProcessConsumeMsg(SVnode *pVnode, SReadMsg *pRead); -int32_t vnodeProcessTqQueryMsg(SVnode *pVnode, SReadMsg *pRead); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_VNODE_READ_MSG_H_*/ diff --git a/source/server/vnode/impl/inc/vnodeWrite.h b/source/server/vnode/impl/inc/vnodeWrite.h index 8500607694..11fe9836f6 100644 --- a/source/server/vnode/impl/inc/vnodeWrite.h +++ b/source/server/vnode/impl/inc/vnodeWrite.h @@ -21,18 +21,7 @@ extern "C" { #endif #include "vnodeInt.h" -int32_t vnodeInitWrite(); -void vnodeCleanupWrite(); - -taos_queue vnodeAllocWriteQueue(SVnode *pVnode); -void vnodeFreeWriteQueue(taos_queue pQueue); -taos_queue vnodeAllocApplyQueue(SVnode *pVnode); -void vnodeFreeApplyQueue(taos_queue pQueue); -void vnodeProcessWriteReq(SRpcMsg *pRpcMsg); - -void vnodeStartWrite(SVnode *pVnode); -void vnodeStopWrite(SVnode *pVnode); -void vnodeWaitWriteCompleted(SVnode *pVnode); +void vnodeProcessWriteMsg(SVnode* pVnode, SVnodeMsg* pMsg); #ifdef __cplusplus } diff --git a/source/server/vnode/impl/inc/vnodeWriteMsg.h b/source/server/vnode/impl/inc/vnodeWriteMsg.h deleted file mode 100644 index 9dbc4fe490..0000000000 --- a/source/server/vnode/impl/inc/vnodeWriteMsg.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_VNODE_WRITE_MSG_H_ -#define _TD_VNODE_WRITE_MSG_H_ - -#ifdef __cplusplus -extern "C" { -#endif -#include "vnodeInt.h" - -int32_t vnodeProcessSubmitReq(SVnode *pVnode, SSubmitReq *pReq, SSubmitRsp *pRsp); -int32_t vnodeProcessCreateTableReq(SVnode *pVnode, SCreateTableReq *pReq, SCreateTableRsp *pRsp); -int32_t vnodeProcessDropTableReq(SVnode *pVnode, SDropTableReq *pReq, SDropTableRsp *pRsp); -int32_t vnodeProcessAlterTableReq(SVnode *pVnode, SAlterTableReq *pReq, SAlterTableRsp *pRsp); -int32_t vnodeProcessDropStableReq(SVnode *pVnode, SDropStableReq *pReq, SDropStableRsp *pRsp); -int32_t vnodeProcessUpdateTagValReq(SVnode *pVnode, SUpdateTagValReq *pReq, SUpdateTagValRsp *pRsp); -//mq related -int32_t vnodeProcessMqConnectReq(SVnode* pVnode, SMqConnectReq *pReq, SMqConnectRsp *pRsp); -int32_t vnodeProcessMqDisconnectReq(SVnode* pVnode, SMqConnectReq *pReq, SMqConnectRsp *pRsp); -int32_t vnodeProcessMqAckReq(SVnode* pVnode, SMqAckReq *pReq, SMqAckRsp *pRsp); -int32_t vnodeProcessMqResetReq(SVnode* pVnode, SMqResetReq *pReq, SMqResetRsp *pRsp); -//mq related end - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_VNODE_WRITE_MSG_H_*/ diff --git a/source/server/vnode/impl/src/vnodeFile.c b/source/server/vnode/impl/src/vnodeFile.c deleted file mode 100644 index ddcbd2689d..0000000000 --- a/source/server/vnode/impl/src/vnodeFile.c +++ /dev/null @@ -1,372 +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 "cJSON.h" -#include "vnodeFile.h" - -int32_t vnodeReadCfg(int32_t vgId, SVnodeCfg *pCfg) { - int32_t ret = TSDB_CODE_VND_APP_ERROR; - int32_t len = 0; - int maxLen = 1000; - char *content = calloc(1, maxLen + 1); - cJSON *root = NULL; - FILE *fp = NULL; - - char file[PATH_MAX + 30] = {0}; - sprintf(file, "%s/vnode%d/config.json", tsVnodeDir, vgId); - - fp = fopen(file, "r"); - if (!fp) { - vError("vgId:%d, failed to open vnode cfg file:%s to read since %s", vgId, file, strerror(errno)); - ret = TAOS_SYSTEM_ERROR(errno); - goto PARSE_VCFG_ERROR; - } - - len = (int32_t)fread(content, 1, maxLen, fp); - if (len <= 0) { - vError("vgId:%d, failed to read %s since content is null", vgId, file); - goto PARSE_VCFG_ERROR; - } - - content[len] = 0; - root = cJSON_Parse(content); - if (root == NULL) { - vError("vgId:%d, failed to read %s since invalid json format", vgId, file); - goto PARSE_VCFG_ERROR; - } - - cJSON *db = cJSON_GetObjectItem(root, "db"); - if (!db || db->type != cJSON_String || db->valuestring == NULL) { - vError("vgId:%d, failed to read %s since db not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - tstrncpy(pCfg->db, db->valuestring, sizeof(pCfg->db)); - - cJSON *dropped = cJSON_GetObjectItem(root, "dropped"); - if (!dropped || dropped->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since dropped not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->dropped = (int32_t)dropped->valueint; - - cJSON *quorum = cJSON_GetObjectItem(root, "quorum"); - if (!quorum || quorum->type != cJSON_Number) { - vError("vgId: %d, failed to read %s, quorum not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->quorum = (int8_t)quorum->valueint; - - cJSON *cacheBlockSize = cJSON_GetObjectItem(root, "cacheBlockSize"); - if (!cacheBlockSize || cacheBlockSize->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since cacheBlockSize not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.cacheBlockSize = (int32_t)cacheBlockSize->valueint; - - cJSON *totalBlocks = cJSON_GetObjectItem(root, "totalBlocks"); - if (!totalBlocks || totalBlocks->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since totalBlocks not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.totalBlocks = (int32_t)totalBlocks->valueint; - - cJSON *daysPerFile = cJSON_GetObjectItem(root, "daysPerFile"); - if (!daysPerFile || daysPerFile->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since daysPerFile not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.daysPerFile = (int32_t)daysPerFile->valueint; - - cJSON *daysToKeep0 = cJSON_GetObjectItem(root, "daysToKeep0"); - if (!daysToKeep0 || daysToKeep0->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since daysToKeep0 not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.daysToKeep0 = (int32_t)daysToKeep0->valueint; - - cJSON *daysToKeep1 = cJSON_GetObjectItem(root, "daysToKeep1"); - if (!daysToKeep1 || daysToKeep1->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since daysToKeep1 not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.daysToKeep1 = (int32_t)daysToKeep1->valueint; - - cJSON *daysToKeep2 = cJSON_GetObjectItem(root, "daysToKeep2"); - if (!daysToKeep2 || daysToKeep2->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since daysToKeep2 not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.daysToKeep2 = (int32_t)daysToKeep2->valueint; - - cJSON *minRowsPerFileBlock = cJSON_GetObjectItem(root, "minRowsPerFileBlock"); - if (!minRowsPerFileBlock || minRowsPerFileBlock->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since minRowsPerFileBlock not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.minRowsPerFileBlock = (int32_t)minRowsPerFileBlock->valueint; - - cJSON *maxRowsPerFileBlock = cJSON_GetObjectItem(root, "maxRowsPerFileBlock"); - if (!maxRowsPerFileBlock || maxRowsPerFileBlock->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since maxRowsPerFileBlock not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.maxRowsPerFileBlock = (int32_t)maxRowsPerFileBlock->valueint; - - cJSON *precision = cJSON_GetObjectItem(root, "precision"); - if (!precision || precision->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since precision not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.precision = (int8_t)precision->valueint; - - cJSON *compression = cJSON_GetObjectItem(root, "compression"); - if (!compression || compression->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since compression not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.compression = (int8_t)compression->valueint; - - cJSON *update = cJSON_GetObjectItem(root, "update"); - if (!update || update->type != cJSON_Number) { - vError("vgId: %d, failed to read %s since update not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.update = (int8_t)update->valueint; - - cJSON *cacheLastRow = cJSON_GetObjectItem(root, "cacheLastRow"); - if (!cacheLastRow || cacheLastRow->type != cJSON_Number) { - vError("vgId: %d, failed to read %s since cacheLastRow not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->tsdb.cacheLastRow = (int8_t)cacheLastRow->valueint; - - cJSON *walLevel = cJSON_GetObjectItem(root, "walLevel"); - if (!walLevel || walLevel->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since walLevel not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->wal.walLevel = (int8_t)walLevel->valueint; - - cJSON *fsyncPeriod = cJSON_GetObjectItem(root, "fsyncPeriod"); - if (!walLevel || walLevel->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since fsyncPeriod not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->wal.fsyncPeriod = (int32_t)fsyncPeriod->valueint; - - cJSON *selfIndex = cJSON_GetObjectItem(root, "selfIndex"); - if (!selfIndex || selfIndex->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since selfIndex not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->sync.selfIndex = selfIndex->valueint; - - cJSON *replica = cJSON_GetObjectItem(root, "replica"); - if (!replica || replica->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since replica not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - pCfg->sync.replica = replica->valueint; - - cJSON *nodes = cJSON_GetObjectItem(root, "nodes"); - if (!nodes || nodes->type != cJSON_Array) { - vError("vgId:%d, failed to read %s, nodes not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - - int size = cJSON_GetArraySize(nodes); - if (size != pCfg->sync.replica) { - vError("vgId:%d, failed to read %s since nodes size not matched", vgId, file); - goto PARSE_VCFG_ERROR; - } - - for (int i = 0; i < size; ++i) { - cJSON *nodeInfo = cJSON_GetArrayItem(nodes, i); - if (nodeInfo == NULL) continue; - SNodeInfo *node = &pCfg->sync.nodeInfo[i]; - - cJSON *nodeId = cJSON_GetObjectItem(nodeInfo, "id"); - if (!nodeId || nodeId->type != cJSON_Number) { - vError("vgId:%d, failed to read %s since nodeId not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - node->nodeId = nodeId->valueint; - - cJSON *nodePort = cJSON_GetObjectItem(nodeInfo, "port"); - if (!nodePort || nodePort->type != cJSON_Number) { - vError("vgId:%d, failed to read %s sincenodePort not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - node->nodePort = (uint16_t)nodePort->valueint; - - cJSON *nodeFqdn = cJSON_GetObjectItem(nodeInfo, "fqdn"); - if (!nodeFqdn || nodeFqdn->type != cJSON_String || nodeFqdn->valuestring == NULL) { - vError("vgId:%d, failed to read %s since nodeFqdn not found", vgId, file); - goto PARSE_VCFG_ERROR; - } - tstrncpy(node->nodeFqdn, nodeFqdn->valuestring, TSDB_FQDN_LEN); - } - - ret = TSDB_CODE_SUCCESS; - -PARSE_VCFG_ERROR: - if (content != NULL) free(content); - if (root != NULL) cJSON_Delete(root); - if (fp != NULL) fclose(fp); - - terrno = 0; - return ret; -} - -int32_t vnodeWriteCfg(int32_t vgId, SVnodeCfg *pCfg) { - int32_t code = 0; - char file[PATH_MAX + 30] = {0}; - sprintf(file, "%s/vnode%d/config.json", tsVnodeDir, vgId); - - FILE *fp = fopen(file, "w"); - if (!fp) { - vError("vgId:%d, failed to write %s error:%s", vgId, file, strerror(errno)); - terrno = TAOS_SYSTEM_ERROR(errno); - return terrno; - } - - int32_t len = 0; - int32_t maxLen = 1000; - char *content = calloc(1, maxLen + 1); - - len += snprintf(content + len, maxLen - len, "{\n"); - // vnode - len += snprintf(content + len, maxLen - len, " \"vgId\": %d,\n", vgId); - len += snprintf(content + len, maxLen - len, " \"db\": \"%s\",\n", pCfg->db); - len += snprintf(content + len, maxLen - len, " \"dropped\": %d,\n", pCfg->dropped); - len += snprintf(content + len, maxLen - len, " \"quorum\": %d,\n", pCfg->quorum); - // tsdb - len += snprintf(content + len, maxLen - len, " \"cacheBlockSize\": %d,\n", pCfg->tsdb.cacheBlockSize); - len += snprintf(content + len, maxLen - len, " \"totalBlocks\": %d,\n", pCfg->tsdb.totalBlocks); - len += snprintf(content + len, maxLen - len, " \"daysPerFile\": %d,\n", pCfg->tsdb.daysPerFile); - len += snprintf(content + len, maxLen - len, " \"daysToKeep0\": %d,\n", pCfg->tsdb.daysToKeep0); - len += snprintf(content + len, maxLen - len, " \"daysToKeep1\": %d,\n", pCfg->tsdb.daysToKeep1); - len += snprintf(content + len, maxLen - len, " \"daysToKeep2\": %d,\n", pCfg->tsdb.daysToKeep2); - len += snprintf(content + len, maxLen - len, " \"minRowsPerFileBlock\": %d,\n", pCfg->tsdb.minRowsPerFileBlock); - len += snprintf(content + len, maxLen - len, " \"maxRowsPerFileBlock\": %d,\n", pCfg->tsdb.maxRowsPerFileBlock); - len += snprintf(content + len, maxLen - len, " \"precision\": %d,\n", pCfg->tsdb.precision); - len += snprintf(content + len, maxLen - len, " \"compression\": %d,\n", pCfg->tsdb.compression); - len += snprintf(content + len, maxLen - len, " \"cacheLastRow\": %d,\n", pCfg->tsdb.cacheLastRow); - len += snprintf(content + len, maxLen - len, " \"update\": %d,\n", pCfg->tsdb.update); - // wal - len += snprintf(content + len, maxLen - len, " \"walLevel\": %d,\n", pCfg->wal.walLevel); - len += snprintf(content + len, maxLen - len, " \"fsyncPeriod\": %d,\n", pCfg->wal.fsyncPeriod); - // sync - len += snprintf(content + len, maxLen - len, " \"replica\": %d,\n", pCfg->sync.replica); - len += snprintf(content + len, maxLen - len, " \"selfIndex\": %d,\n", pCfg->sync.selfIndex); - len += snprintf(content + len, maxLen - len, " \"nodes\": [{\n"); - for (int32_t i = 0; i < pCfg->sync.replica; i++) { - SNodeInfo *node = &pCfg->sync.nodeInfo[i]; - len += snprintf(content + len, maxLen - len, " \"id\": %d,\n", node->nodeId); - len += snprintf(content + len, maxLen - len, " \"port\": %u,\n", node->nodePort); - len += snprintf(content + len, maxLen - len, " \"fqdn\": \"%s\"\n", node->nodeFqdn); - if (i < pCfg->sync.replica - 1) { - len += snprintf(content + len, maxLen - len, " },{\n"); - } else { - len += snprintf(content + len, maxLen - len, " }]\n"); - } - } - len += snprintf(content + len, maxLen - len, "}\n"); - - fwrite(content, 1, len, fp); - taosFsyncFile(fileno(fp)); - fclose(fp); - free(content); - terrno = 0; - - vInfo("vgId:%d, successed to write %s", vgId, file); - return TSDB_CODE_SUCCESS; -} - -int32_t vnodeReadState(int32_t vgId, SSyncServerState *pState) { - int32_t ret = TSDB_CODE_VND_APP_ERROR; - int32_t len = 0; - int32_t maxLen = 100; - char *content = calloc(1, maxLen + 1); - cJSON *root = NULL; - FILE *fp = NULL; - - char file[PATH_MAX + 30] = {0}; - sprintf(file, "%s/vnode%d/state.json", tsVnodeDir, vgId); - - len = (int32_t)fread(content, 1, maxLen, fp); - if (len <= 0) { - vError("vgId:%d, failed to read %s since content is null", vgId, file); - goto PARSE_TERM_ERROR; - } - - root = cJSON_Parse(content); - if (root == NULL) { - vError("vgId:%d, failed to read %s since invalid json format", vgId, file); - goto PARSE_TERM_ERROR; - } - - cJSON *term = cJSON_GetObjectItem(root, "term"); - if (!term || term->type != cJSON_String) { - vError("vgId:%d, failed to read %s since term not found", vgId, file); - goto PARSE_TERM_ERROR; - } - pState->term = atoll(term->valuestring); - - cJSON *voteFor = cJSON_GetObjectItem(root, "voteFor"); - if (!voteFor || voteFor->type != cJSON_String) { - vError("vgId:%d, failed to read %s since voteFor not found", vgId, file); - goto PARSE_TERM_ERROR; - } - pState->voteFor = atoi(voteFor->valuestring); - - vInfo("vgId:%d, read %s success, voteFor:%d, term:%" PRIu64, vgId, file, pState->voteFor, pState->term); - -PARSE_TERM_ERROR: - if (content != NULL) free(content); - if (root != NULL) cJSON_Delete(root); - if (fp != NULL) fclose(fp); - - return ret; -} - -int32_t vnodeSaveState(int32_t vgId, SSyncServerState *pState) { - char file[PATH_MAX + 30] = {0}; - sprintf(file, "%s/vnode%d/state.json", tsVnodeDir, vgId); - - FILE *fp = fopen(file, "w"); - if (!fp) { - vError("vgId:%d, failed to write %s since %s", vgId, file, strerror(errno)); - return -1; - } - - int32_t len = 0; - int32_t maxLen = 100; - char *content = calloc(1, maxLen + 1); - - len += snprintf(content + len, maxLen - len, "{\n"); - len += snprintf(content + len, maxLen - len, " \"term\": \"%" PRIu64 "\",\n", pState->term); - len += snprintf(content + len, maxLen - len, " \"voteFor\": \"%d\"\n", pState->voteFor); - len += snprintf(content + len, maxLen - len, "}\n"); - - fwrite(content, 1, len, fp); - taosFsyncFile(fileno(fp)); - fclose(fp); - free(content); - - vInfo("vgId:%d, write %s success, voteFor:%d, term:%" PRIu64, vgId, file, pState->voteFor, pState->term); - return TSDB_CODE_SUCCESS; -} \ No newline at end of file diff --git a/source/server/vnode/impl/src/vnodeInt.c b/source/server/vnode/impl/src/vnodeInt.c index 4061c04d94..7a395706e9 100644 --- a/source/server/vnode/impl/src/vnodeInt.c +++ b/source/server/vnode/impl/src/vnodeInt.c @@ -14,78 +14,20 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "tstep.h" -#include "vnodeMain.h" -#include "vnodeMgmt.h" -#include "vnodeRead.h" -#include "vnodeWrite.h" +#include "vnodeInt.h" -static struct { - SSteps *steps; - SVnodeFp fp; - void (*msgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *); -} tsVint; +int32_t vnodeInit() { return 0; } +void vnodeCleanup() {} -void vnodeGetDnodeEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port) { - return (*tsVint.fp.GetDnodeEp)(dnodeId, ep, fqdn, port); -} +int32_t vnodeGetStatistics(SVnode *pVnode, SVnodeStatisic *pStat) { return 0; } +int32_t vnodeGetStatus(SVnode *pVnode, SVnodeStatus *pStatus) { return 0; } -void vnodeSendMsgToDnode(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg) { - (*tsVint.fp.SendMsgToDnode)(epSet, rpcMsg); -} +SVnode *vnodeOpen(int32_t vgId, const char *path) { return NULL; } +void vnodeClose(SVnode *pVnode) {} +int32_t vnodeAlter(SVnode *pVnode, const SVnodeCfg *pCfg) { return 0; } +SVnode *vnodeCreate(int32_t vgId, const char *path, const SVnodeCfg *pCfg) { return NULL; } +int32_t vnodeDrop(SVnode *pVnode) { return 0; } +int32_t vnodeCompact(SVnode *pVnode) { return 0; } +int32_t vnodeSync(SVnode *pVnode) { return 0; } -void vnodeSendMsgToMnode(struct SRpcMsg *rpcMsg) { return (*tsVint.fp.SendMsgToMnode)(rpcMsg); } - -void vnodeReportStartup(char *name, char *desc) { (*tsVint.fp.ReportStartup)(name, desc); } - -void vnodeProcessMsg(SRpcMsg *pMsg) { - if (tsVint.msgFp[pMsg->msgType]) { - (*tsVint.msgFp[pMsg->msgType])(pMsg); - } else { - assert(0); - } -} - -static void vnodeInitMsgFp() { - tsVint.msgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = vnodeProcessMgmtMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = vnodeProcessMgmtMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = vnodeProcessMgmtMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessMgmtMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessMgmtMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessMgmtMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MD_CREATE_TABLE] = vnodeProcessWriteReq; - tsVint.msgFp[TSDB_MSG_TYPE_MD_DROP_TABLE] = vnodeProcessWriteReq; - tsVint.msgFp[TSDB_MSG_TYPE_MD_ALTER_TABLE] = vnodeProcessWriteReq; - tsVint.msgFp[TSDB_MSG_TYPE_MD_DROP_STABLE] = vnodeProcessWriteReq; - tsVint.msgFp[TSDB_MSG_TYPE_SUBMIT] = vnodeProcessWriteReq; - tsVint.msgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = vnodeProcessWriteReq; - // mq related - tsVint.msgFp[TSDB_MSG_TYPE_MQ_CONNECT] = vnodeProcessWriteReq; - tsVint.msgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = vnodeProcessWriteReq; - tsVint.msgFp[TSDB_MSG_TYPE_MQ_ACK] = vnodeProcessWriteReq; - tsVint.msgFp[TSDB_MSG_TYPE_MQ_RESET] = vnodeProcessWriteReq; - tsVint.msgFp[TSDB_MSG_TYPE_MQ_QUERY] = vnodeProcessReadMsg; - tsVint.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = vnodeProcessReadMsg; - // mq related end - tsVint.msgFp[TSDB_MSG_TYPE_QUERY] = vnodeProcessReadMsg; - tsVint.msgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessReadMsg; -} - -int32_t vnodeInit(SVnodePara para) { - vnodeInitMsgFp(); - tsVint.fp = para.fp; - - struct SSteps *steps = taosStepInit(8, NULL); - if (steps == NULL) return -1; - - taosStepAdd(steps, "vnode-main", vnodeInitMain, vnodeCleanupMain); - taosStepAdd(steps, "vnode-read", vnodeInitRead, vnodeCleanupRead); - taosStepAdd(steps, "vnode-mgmt", vnodeInitMgmt, vnodeCleanupMgmt); - taosStepAdd(steps, "vnode-write", vnodeInitWrite, vnodeCleanupWrite); - - tsVint.steps = steps; - return taosStepExec(tsVint.steps); -} - -void vnodeCleanup() { taosStepCleanup(tsVint.steps); } +int32_t vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg) { return 0; } diff --git a/source/server/vnode/impl/src/vnodeMain.c b/source/server/vnode/impl/src/vnodeMain.c deleted file mode 100644 index ced93ea6a7..0000000000 --- a/source/server/vnode/impl/src/vnodeMain.c +++ /dev/null @@ -1,712 +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 "thash.h" -#include "tthread.h" -#include "vnodeFile.h" -#include "vnodeMain.h" -#include "vnodeMgmt.h" -#include "vnodeRead.h" -#include "vnodeWrite.h" - -typedef enum _VN_STATUS { - TAOS_VN_STATUS_INIT = 0, - TAOS_VN_STATUS_READY = 1, - TAOS_VN_STATUS_CLOSING = 2, - TAOS_VN_STATUS_UPDATING = 3 -} EVnodeStatus; - -char *vnodeStatus[] = {"init", "ready", "closing", "updating"}; - -typedef struct { - pthread_t *threadId; - int32_t threadIndex; - int32_t failed; - int32_t opened; - int32_t vnodeNum; - int32_t *vnodeList; -} SOpenVnodeThread; - -static struct { - SHashObj *hash; - int32_t openVnodes; - int32_t totalVnodes; -} tsVnode; - -static bool vnodeSetInitStatus(SVnode *pVnode) { - pthread_mutex_lock(&pVnode->statusMutex); - pVnode->status = TAOS_VN_STATUS_INIT; - pthread_mutex_unlock(&pVnode->statusMutex); - return true; -} - -static bool vnodeSetReadyStatus(SVnode *pVnode) { - bool set = false; - pthread_mutex_lock(&pVnode->statusMutex); - - if (pVnode->status == TAOS_VN_STATUS_INIT || pVnode->status == TAOS_VN_STATUS_UPDATING) { - pVnode->status = TAOS_VN_STATUS_READY; - set = true; - } - - pthread_mutex_unlock(&pVnode->statusMutex); - return set; -} - -static bool vnodeSetUpdatingStatus(SVnode *pVnode) { - bool set = false; - pthread_mutex_lock(&pVnode->statusMutex); - - if (pVnode->status == TAOS_VN_STATUS_READY) { - pVnode->status = TAOS_VN_STATUS_UPDATING; - set = true; - } - - pthread_mutex_unlock(&pVnode->statusMutex); - return set; -} - -static bool vnodeSetClosingStatus(SVnode *pVnode) { - bool set = false; - pthread_mutex_lock(&pVnode->statusMutex); - - if (pVnode->status == TAOS_VN_STATUS_INIT || pVnode->status == TAOS_VN_STATUS_READY) { - pVnode->status = TAOS_VN_STATUS_CLOSING; - set = true; - } - - pthread_mutex_unlock(&pVnode->statusMutex); - return set; -} - -static bool vnodeInStatus(SVnode *pVnode, EVnodeStatus status) { - bool in = false; - pthread_mutex_lock(&pVnode->statusMutex); - - if (pVnode->status == status) { - in = true; - } - - pthread_mutex_unlock(&pVnode->statusMutex); - return in; -} - -static void vnodeDestroyVnode(SVnode *pVnode) { - int32_t code = 0; - int32_t vgId = pVnode->vgId; - - if (pVnode->pSync != NULL) { - syncStop(pVnode->pSync); - pVnode->pSync = NULL; - } - - if (pVnode->pQuery) { - // todo - } - - if (pVnode->pMeta) { - // todo - } - - if (pVnode->pTsdb) { - // todo - } - - if (pVnode->pTQ) { - // todo - } - - if (pVnode->pWal) { - walClose(pVnode->pWal); - pVnode->pWal = NULL; - } - - if (pVnode->allocator) { - // todo - } - - if (pVnode->pWriteQ) { - vnodeFreeWriteQueue(pVnode->pWriteQ); - pVnode->pWriteQ = NULL; - } - - if (pVnode->pQueryQ) { - vnodeFreeQueryQueue(pVnode->pQueryQ); - pVnode->pQueryQ = NULL; - } - - if (pVnode->pFetchQ) { - vnodeFreeFetchQueue(pVnode->pFetchQ); - pVnode->pFetchQ = NULL; - } - - if (pVnode->dropped) { - // todo - } - - pthread_mutex_destroy(&pVnode->statusMutex); - free(pVnode); -} - -static void vnodeCleanupVnode(SVnode *pVnode) { - vnodeSetClosingStatus(pVnode); - taosHashRemove(tsVnode.hash, &pVnode->vgId, sizeof(int32_t)); - vnodeRelease(pVnode); -} - -static inline int32_t vnodeLogWrite(struct SSyncLogStore *logStore, SyncIndex index, SSyncBuffer *pBuf) { - SVnode *pVnode = logStore->pData; // vnode status can be checked here - return walWrite(pVnode->pWal, index, pBuf->data, (int32_t)pBuf->len); -} - -static inline int32_t vnodeLogCommit(struct SSyncLogStore *logStore, SyncIndex index) { - SVnode *pVnode = logStore->pData; // vnode status can be checked here - return walCommit(pVnode->pWal, index); -} - -static inline int32_t vnodeLogPrune(struct SSyncLogStore *logStore, SyncIndex index) { - SVnode *pVnode = logStore->pData; // vnode status can be checked here - return walPrune(pVnode->pWal, index); -} - -static inline int32_t vnodeLogRollback(struct SSyncLogStore *logStore, SyncIndex index) { - SVnode *pVnode = logStore->pData; // vnode status can be checked here - return walRollback(pVnode->pWal, index); -} - -static inline int32_t vnodeSaveServerState(struct SStateManager *stateMng, SSyncServerState *pState) { - SVnode *pVnode = stateMng->pData; - return vnodeSaveState(pVnode->vgId, pState); -} - -static inline int32_t vnodeReadServerState(struct SStateManager *stateMng, SSyncServerState *pState) { - SVnode *pVnode = stateMng->pData; - return vnodeSaveState(pVnode->vgId, pState); -} - -static inline int32_t vnodeApplyLog(struct SSyncFSM *fsm, SyncIndex index, const SSyncBuffer *buf, void *pData) { - return 0; -} - -static inline int32_t vnodeOnClusterChanged(struct SSyncFSM *fsm, const SSyncCluster *cluster, void *pData) { return 0; } - -static inline int32_t vnodeGetSnapshot(struct SSyncFSM *fsm, SSyncBuffer **ppBuf, int32_t *objId, bool *isLast) { - return 0; -} - -static inline int32_t vnodeApplySnapshot(struct SSyncFSM *fsm, SSyncBuffer *pBuf, int32_t objId, bool isLast) { - return 0; -} - -static inline int32_t vnodeOnRestoreDone(struct SSyncFSM *fsm) { return 0; } - -static inline void vnodeOnRollback(struct SSyncFSM *fsm, SyncIndex index, const SSyncBuffer *buf) {} - -static inline void vnodeOnRoleChanged(struct SSyncFSM *fsm, const SNodesRole *pRole) {} - -static int32_t vnodeOpenVnode(int32_t vgId) { - int32_t code = 0; - - SVnode *pVnode = calloc(sizeof(SVnode), 1); - if (pVnode == NULL) { - vError("vgId:%d, failed to open vnode since no enough memory", vgId); - return TAOS_SYSTEM_ERROR(errno); - } - - pVnode->vgId = vgId; - pVnode->accessState = TAOS_VN_STATUS_INIT; - pVnode->status = TSDB_VN_ALL_ACCCESS; - pVnode->refCount = 1; - pVnode->role = TAOS_SYNC_ROLE_CANDIDATE; - pthread_mutex_init(&pVnode->statusMutex, NULL); - - vDebug("vgId:%d, vnode is opened", pVnode->vgId); - taosHashPut(tsVnode.hash, &pVnode->vgId, sizeof(int32_t), &pVnode, sizeof(SVnode *)); - - code = vnodeReadCfg(vgId, &pVnode->cfg); - if (code != TSDB_CODE_SUCCESS) { - vError("vgId:%d, failed to read config file, set cfgVersion to 0", pVnode->vgId); - pVnode->cfg.dropped = 1; - vnodeCleanupVnode(pVnode); - return 0; - } - - code = vnodeSaveState(vgId, &pVnode->term); - if (code != TSDB_CODE_SUCCESS) { - vError("vgId:%d, failed to read term file since %s", pVnode->vgId, tstrerror(code)); - pVnode->cfg.dropped = 1; - vnodeCleanupVnode(pVnode); - return code; - } - - pVnode->pWriteQ = vnodeAllocWriteQueue(pVnode); - pVnode->pQueryQ = vnodeAllocQueryQueue(pVnode); - pVnode->pFetchQ = vnodeAllocFetchQueue(pVnode); - if (pVnode->pWriteQ == NULL || pVnode->pQueryQ == NULL || pVnode->pFetchQ == NULL) { - vnodeCleanupVnode(pVnode); - return terrno; - } - - char path[PATH_MAX + 20]; - snprintf(path, sizeof(path), "%s/vnode%d/wal", tsVnodeDir, vgId); - pVnode->pWal = walOpen(path, &pVnode->cfg.wal); - if (pVnode->pWal == NULL) { - vnodeCleanupVnode(pVnode); - return terrno; - } - - // create sync node - SSyncInfo syncInfo = {0}; - syncInfo.vgId = vgId; - syncInfo.snapshotIndex = 0; // todo, from tsdb - memcpy(&syncInfo.syncCfg, &pVnode->cfg.sync, sizeof(SSyncCluster)); - syncInfo.fsm.pData = pVnode; - syncInfo.fsm.applyLog = vnodeApplyLog; - syncInfo.fsm.onClusterChanged = vnodeOnClusterChanged; - syncInfo.fsm.getSnapshot = vnodeGetSnapshot; - syncInfo.fsm.applySnapshot = vnodeApplySnapshot; - syncInfo.fsm.onRestoreDone = vnodeOnRestoreDone; - syncInfo.fsm.onRollback = vnodeOnRollback; - syncInfo.fsm.onRoleChanged = vnodeOnRoleChanged; - syncInfo.logStore.pData = pVnode; - syncInfo.logStore.logWrite = vnodeLogWrite; - syncInfo.logStore.logCommit = vnodeLogCommit; - syncInfo.logStore.logPrune = vnodeLogPrune; - syncInfo.logStore.logRollback = vnodeLogRollback; - syncInfo.stateManager.pData = pVnode; - syncInfo.stateManager.saveServerState = vnodeSaveServerState; - syncInfo.stateManager.readServerState = vnodeReadServerState; - - pVnode->pSync = syncStart(&syncInfo); - if (pVnode->pSync == NULL) { - vnodeCleanupVnode(pVnode); - return terrno; - } - - vnodeSetReadyStatus(pVnode); - return TSDB_CODE_SUCCESS; -} - -int32_t vnodeCreateVnode(int32_t vgId, SVnodeCfg *pCfg) { - int32_t code = 0; - char path[PATH_MAX + 20] = {0}; - - snprintf(path, sizeof(path), "%s/vnode%d", tsVnodeDir, vgId); - if (taosMkDir(path) < 0) { - code = TAOS_SYSTEM_ERROR(errno); - vError("vgId:%d, failed to create since %s", vgId, tstrerror(code)); - return code; - } - - snprintf(path, sizeof(path), "%s/vnode%d/cfg", tsVnodeDir, vgId); - if (taosMkDir(path) < 0) { - code = TAOS_SYSTEM_ERROR(errno); - vError("vgId:%d, failed to create since %s", vgId, tstrerror(code)); - return code; - } - - snprintf(path, sizeof(path), "%s/vnode%d/wal", tsVnodeDir, vgId); - if (taosMkDir(path) < 0) { - code = TAOS_SYSTEM_ERROR(errno); - vError("vgId:%d, failed to create since %s", vgId, tstrerror(code)); - return code; - } - - snprintf(path, sizeof(path), "%s/vnode%d/tq", tsVnodeDir, vgId); - if (taosMkDir(path) < 0) { - code = TAOS_SYSTEM_ERROR(errno); - vError("vgId:%d, failed to create since %s", vgId, tstrerror(code)); - return code; - } - - snprintf(path, sizeof(path), "%s/vnode%d/tsdb", tsVnodeDir, vgId); - if (taosMkDir(path) < 0) { - code = TAOS_SYSTEM_ERROR(errno); - vError("vgId:%d, failed to create since %s", vgId, tstrerror(code)); - return code; - } - - snprintf(path, sizeof(path), "%s/vnode%d/meta", tsVnodeDir, vgId); - if (taosMkDir(path) < 0) { - code = TAOS_SYSTEM_ERROR(errno); - vError("vgId:%d, failed to create since %s", vgId, tstrerror(code)); - return code; - } - - code = vnodeWriteCfg(vgId, pCfg); - if (code != 0) { - vError("vgId:%d, failed to save vnode cfg since %s", vgId, tstrerror(code)); - return code; - } - - return vnodeOpenVnode(vgId); -} - -int32_t vnodeAlterVnode(SVnode * pVnode, SVnodeCfg *pCfg) { - int32_t code = 0; - int32_t vgId = pVnode->vgId; - - bool walChanged = (memcmp(&pCfg->wal, &pVnode->cfg.wal, sizeof(SWalCfg)) != 0); - bool tsdbChanged = (memcmp(&pCfg->tsdb, &pVnode->cfg.tsdb, sizeof(STsdbCfg)) != 0); - bool metaChanged = (memcmp(&pCfg->meta, &pVnode->cfg.meta, sizeof(SMetaCfg)) != 0); - bool syncChanged = (memcmp(&pCfg->sync, &pVnode->cfg.sync, sizeof(SSyncCluster)) != 0); - - if (!walChanged && !tsdbChanged && !metaChanged && !syncChanged) { - vDebug("vgId:%d, nothing changed", vgId); - vnodeRelease(pVnode); - return code; - } - - code = vnodeWriteCfg(pVnode->vgId, pCfg); - if (code != 0) { - vError("vgId:%d, failed to write alter msg to file since %s", vgId, tstrerror(code)); - vnodeRelease(pVnode); - return code; - } - - pVnode->cfg = *pCfg; - - if (walChanged) { - code = walAlter(pVnode->pWal, &pVnode->cfg.wal); - if (code != 0) { - vDebug("vgId:%d, failed to alter wal since %s", vgId, tstrerror(code)); - vnodeRelease(pVnode); - return code; - } - } - - if (tsdbChanged) { - // todo - } - - if (metaChanged) { - // todo - } - - if (syncChanged) { - syncReconfig(pVnode->pSync, &pVnode->cfg.sync); - } - - vnodeRelease(pVnode); - return code; -} - -int32_t vnodeDropVnode(SVnode *pVnode) { - if (pVnode->cfg.dropped) { - vInfo("vgId:%d, already set drop flag, ref:%d", pVnode->vgId, pVnode->refCount); - vnodeRelease(pVnode); - return TSDB_CODE_SUCCESS; - } - - pVnode->cfg.dropped = 1; - int32_t code = vnodeWriteCfg(pVnode->vgId, &pVnode->cfg); - if (code == 0) { - vInfo("vgId:%d, set drop flag, ref:%d", pVnode->vgId, pVnode->refCount); - vnodeCleanupVnode(pVnode); - } else { - vError("vgId:%d, failed to set drop flag since %s", pVnode->vgId, tstrerror(code)); - pVnode->cfg.dropped = 0; - } - - vnodeRelease(pVnode); - return code; -} - -int32_t vnodeSyncVnode(SVnode *pVnode) { - return TSDB_CODE_SUCCESS; -} - -int32_t vnodeCompactVnode(SVnode *pVnode) { - return TSDB_CODE_SUCCESS; -} - -static void *vnodeOpenVnodeFunc(void *param) { - SOpenVnodeThread *pThread = param; - - vDebug("thread:%d, start to open %d vnodes", pThread->threadIndex, pThread->vnodeNum); - setThreadName("vnodeOpenVnode"); - - for (int32_t v = 0; v < pThread->vnodeNum; ++v) { - int32_t vgId = pThread->vnodeList[v]; - - char stepDesc[TSDB_STEP_DESC_LEN] = {0}; - snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", vgId, - tsVnode.openVnodes, tsVnode.totalVnodes); - // (*vnodeInst()->fp.ReportStartup)("open-vnodes", stepDesc); - - if (vnodeOpenVnode(vgId) < 0) { - vError("vgId:%d, failed to open vnode by thread:%d", vgId, pThread->threadIndex); - pThread->failed++; - } else { - vDebug("vgId:%d, is opened by thread:%d", vgId, pThread->threadIndex); - pThread->opened++; - } - - atomic_add_fetch_32(&tsVnode.openVnodes, 1); - } - - vDebug("thread:%d, total vnodes:%d, opened:%d failed:%d", pThread->threadIndex, pThread->vnodeNum, pThread->opened, - pThread->failed); - return NULL; -} - -static int32_t vnodeGetVnodeListFromDisk(int32_t vnodeList[], int32_t *numOfVnodes) { -#if 0 - DIR *dir = opendir(tsVnodeDir); - if (dir == NULL) return TSDB_CODE_DND_NO_WRITE_ACCESS; - - *numOfVnodes = 0; - struct dirent *de = NULL; - while ((de = readdir(dir)) != NULL) { - if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; - if (de->d_type & DT_DIR) { - if (strncmp("vnode", de->d_name, 5) != 0) continue; - int32_t vnode = atoi(de->d_name + 5); - if (vnode == 0) continue; - - (*numOfVnodes)++; - - if (*numOfVnodes >= TSDB_MAX_VNODES) { - vError("vgId:%d, too many vnode directory in disk, exist:%d max:%d", vnode, *numOfVnodes, TSDB_MAX_VNODES); - closedir(dir); - return TSDB_CODE_DND_TOO_MANY_VNODES; - } else { - vnodeList[*numOfVnodes - 1] = vnode; - } - } - } - closedir(dir); -#endif - return TSDB_CODE_SUCCESS; -} - -static int32_t vnodeOpenVnodes() { - int32_t vnodeList[TSDB_MAX_VNODES] = {0}; - int32_t numOfVnodes = 0; - int32_t status = vnodeGetVnodeListFromDisk(vnodeList, &numOfVnodes); - - if (status != TSDB_CODE_SUCCESS) { - vInfo("failed to get vnode list from disk since code:%d", status); - return status; - } - - tsVnode.totalVnodes = numOfVnodes; - - int32_t threadNum = tsNumOfCores; - int32_t vnodesPerThread = numOfVnodes / threadNum + 1; - - SOpenVnodeThread *threads = calloc(threadNum, sizeof(SOpenVnodeThread)); - for (int32_t t = 0; t < threadNum; ++t) { - threads[t].threadIndex = t; - threads[t].vnodeList = calloc(vnodesPerThread, sizeof(int32_t)); - } - - for (int32_t v = 0; v < numOfVnodes; ++v) { - int32_t t = v % threadNum; - SOpenVnodeThread *pThread = &threads[t]; - pThread->vnodeList[pThread->vnodeNum++] = vnodeList[v]; - } - - vInfo("start %d threads to open %d vnodes", threadNum, numOfVnodes); - - for (int32_t t = 0; t < threadNum; ++t) { - SOpenVnodeThread *pThread = &threads[t]; - if (pThread->vnodeNum == 0) continue; - - pThread->threadId = taosCreateThread(vnodeOpenVnodeFunc, pThread); - if (pThread->threadId == NULL) { - vError("thread:%d, failed to create thread to open vnode, reason:%s", pThread->threadIndex, strerror(errno)); - } - } - - int32_t openVnodes = 0; - int32_t failedVnodes = 0; - for (int32_t t = 0; t < threadNum; ++t) { - SOpenVnodeThread *pThread = &threads[t]; - taosDestoryThread(pThread->threadId); - pThread->threadId = NULL; - - openVnodes += pThread->opened; - failedVnodes += pThread->failed; - free(pThread->vnodeList); - } - - free(threads); - vInfo("there are total vnodes:%d, opened:%d", numOfVnodes, openVnodes); - - if (failedVnodes != 0) { - vError("there are total vnodes:%d, failed:%d", numOfVnodes, failedVnodes); - return -1; - } - - return TSDB_CODE_SUCCESS; -} - -static int32_t vnodeGetVnodeList(SVnode *vnodeList[], int32_t *numOfVnodes) { - void *pIter = taosHashIterate(tsVnode.hash, NULL); - while (pIter) { - SVnode **pVnode = pIter; - if (*pVnode) { - (*numOfVnodes)++; - if (*numOfVnodes >= TSDB_MAX_VNODES) { - vError("vgId:%d, too many open vnodes, exist:%d max:%d", (*pVnode)->vgId, *numOfVnodes, TSDB_MAX_VNODES); - continue; - } else { - vnodeList[*numOfVnodes - 1] = (*pVnode); - } - } - - pIter = taosHashIterate(tsVnode.hash, pIter); - } - - return TSDB_CODE_SUCCESS; -} - -static void vnodeCleanupVnodes() { - SVnode* vnodeList[TSDB_MAX_VNODES] = {0}; - int32_t numOfVnodes = 0; - - int32_t code = vnodeGetVnodeList(vnodeList, &numOfVnodes); - - if (code != TSDB_CODE_SUCCESS) { - vInfo("failed to get dnode list since code %d", code); - return; - } - - for (int32_t i = 0; i < numOfVnodes; ++i) { - vnodeCleanupVnode(vnodeList[i]); - } - - vInfo("total vnodes:%d are all closed", numOfVnodes); -} - -static void vnodeIncRef(void *ptNode) { - assert(ptNode != NULL); - - SVnode **ppVnode = (SVnode **)ptNode; - assert(ppVnode); - assert(*ppVnode); - - SVnode *pVnode = *ppVnode; - atomic_add_fetch_32(&pVnode->refCount, 1); - vTrace("vgId:%d, get vnode, refCount:%d pVnode:%p", pVnode->vgId, pVnode->refCount, pVnode); -} - -SVnode *vnodeAcquireInAllState(int32_t vgId) { - SVnode *pVnode = NULL; - - // taosHashGetClone(tsVnode.hash, &vgId, sizeof(int32_t), vnodeIncRef, (void*)&pVnode); - if (pVnode == NULL) { - vDebug("vgId:%d, can't accquire since not exist", vgId); - terrno = TSDB_CODE_VND_INVALID_VGROUP_ID; - return NULL; - } - - return pVnode; -} - -SVnode *vnodeAcquire(int32_t vgId) { - SVnode *pVnode = vnodeAcquireInAllState(vgId); - if (pVnode == NULL) return NULL; - - if (vnodeInStatus(pVnode, TAOS_VN_STATUS_READY)) { - return pVnode; - } else { - vDebug("vgId:%d, can't accquire since not in ready status", vgId); - vnodeRelease(pVnode); - terrno = TSDB_CODE_VND_INVALID_TSDB_STATE; - return NULL; - } -} - -void vnodeRelease(SVnode *pVnode) { - if (pVnode == NULL) return; - - int32_t refCount = atomic_sub_fetch_32(&pVnode->refCount, 1); - int32_t vgId = pVnode->vgId; - - vTrace("vgId:%d, release vnode, refCount:%d pVnode:%p", vgId, refCount, pVnode); - assert(refCount >= 0); - - if (refCount <= 0) { - vDebug("vgId:%d, vnode will be destroyed, refCount:%d pVnode:%p", vgId, refCount, pVnode); - vnodeDestroyVnode(pVnode); - int32_t count = taosHashGetSize(tsVnode.hash); - vDebug("vgId:%d, vnode is destroyed, vnodes:%d", vgId, count); - } -} - -int32_t vnodeInitMain() { - tsVnode.hash = taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); - if (tsVnode.hash == NULL) { - vError("failed to init vnode mgmt"); - return -1; - } - - vInfo("vnode main is initialized"); - return vnodeOpenVnodes(); -} - -void vnodeCleanupMain() { - vnodeCleanupVnodes(); - taosHashCleanup(tsVnode.hash); - tsVnode.hash = NULL; -} - -static void vnodeBuildVloadMsg(SVnode *pVnode, SStatusMsg *pStatus) { - int64_t totalStorage = 0; - int64_t compStorage = 0; - int64_t pointsWritten = 0; - - if (pStatus->openVnodes >= TSDB_MAX_VNODES) return; - - // if (pVnode->tsdb) { - // tsdbReportStat(pVnode->tsdb, &pointsWritten, &totalStorage, &compStorage); - // } - - SVnodeLoad *pLoad = &pStatus->load[pStatus->openVnodes++]; - pLoad->vgId = htonl(pVnode->vgId); - pLoad->totalStorage = htobe64(totalStorage); - pLoad->compStorage = htobe64(compStorage); - pLoad->pointsWritten = htobe64(pointsWritten); - pLoad->status = pVnode->status; - pLoad->role = pVnode->role; -} - -void vnodeGetStatus(SStatusMsg *pStatus) { - void *pIter = taosHashIterate(tsVnode.hash, NULL); - while (pIter) { - SVnode **pVnode = pIter; - if (*pVnode) { - vnodeBuildVloadMsg(*pVnode, pStatus); - } - pIter = taosHashIterate(tsVnode.hash, pIter); - } -} - -void vnodeSetAccess(SVgroupAccess *pAccess, int32_t numOfVnodes) { - for (int32_t i = 0; i < numOfVnodes; ++i) { - pAccess[i].vgId = htonl(pAccess[i].vgId); - SVnode *pVnode = vnodeAcquire(pAccess[i].vgId); - if (pVnode != NULL) { - pVnode->accessState = pAccess[i].accessState; - if (pVnode->accessState != TSDB_VN_ALL_ACCCESS) { - vDebug("vgId:%d, access state is set to %d", pAccess[i].vgId, pVnode->accessState); - } - vnodeRelease(pVnode); - } - } -} diff --git a/source/server/vnode/impl/src/vnodeMgmt.c b/source/server/vnode/impl/src/vnodeMgmt.c deleted file mode 100644 index dfb3c95b8d..0000000000 --- a/source/server/vnode/impl/src/vnodeMgmt.c +++ /dev/null @@ -1,287 +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 "vnodeMain.h" -#include "vnodeMgmt.h" - -static struct { - SWorkerPool createPool; - taos_queue createQueue; - SWorkerPool workerPool; - taos_queue workerQueue; - int32_t (*msgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *); -} tsVmgmt = {0}; - -static int32_t vnodeParseCreateVnodeReq(SRpcMsg *rpcMsg, int32_t *vgId, SVnodeCfg *pCfg) { - SCreateVnodeMsg *pCreate = rpcMsg->pCont; - *vgId = htonl(pCreate->vgId); - - pCfg->dropped = 0; - pCfg->quorum = pCreate->quorum; - tstrncpy(pCfg->db, pCreate->db, sizeof(pCfg->db)); - - pCfg->tsdb.cacheBlockSize = htonl(pCreate->cacheBlockSize); - pCfg->tsdb.totalBlocks = htonl(pCreate->totalBlocks); - pCfg->tsdb.daysPerFile = htonl(pCreate->daysPerFile); - pCfg->tsdb.daysToKeep1 = htonl(pCreate->daysToKeep1); - pCfg->tsdb.daysToKeep2 = htonl(pCreate->daysToKeep2); - pCfg->tsdb.daysToKeep0 = htonl(pCreate->daysToKeep0); - pCfg->tsdb.minRowsPerFileBlock = htonl(pCreate->minRowsPerFileBlock); - pCfg->tsdb.maxRowsPerFileBlock = htonl(pCreate->maxRowsPerFileBlock); - pCfg->tsdb.precision = pCreate->precision; - pCfg->tsdb.compression = pCreate->compression; - pCfg->tsdb.cacheLastRow = pCreate->cacheLastRow; - pCfg->tsdb.update = pCreate->update; - - pCfg->wal.fsyncPeriod = htonl(pCreate->fsyncPeriod); - pCfg->wal.walLevel = pCreate->walLevel; - - pCfg->sync.replica = pCreate->replica; - pCfg->sync.selfIndex = pCreate->selfIndex; - - for (int32_t j = 0; j < pCreate->replica; ++j) { - pCfg->sync.nodeInfo[j].nodePort = htons(pCreate->nodes[j].port); - tstrncpy(pCfg->sync.nodeInfo[j].nodeFqdn, pCreate->nodes[j].fqdn, TSDB_FQDN_LEN); - } - - return 0; -} - -static int32_t vnodeProcessCreateVnodeReq(SRpcMsg *rpcMsg) { - SVnodeCfg vnodeCfg = {0}; - int32_t vgId = 0; - - int32_t code = vnodeParseCreateVnodeReq(rpcMsg, &vgId, &vnodeCfg); - if (code != 0) { - vError("failed to parse create vnode msg since %s", tstrerror(code)); - } - - vDebug("vgId:%d, create vnode req is received", vgId); - - SVnode *pVnode = vnodeAcquireInAllState(vgId); - if (pVnode != NULL) { - vDebug("vgId:%d, already exist, return success", vgId); - vnodeRelease(pVnode); - return code; - } - - code = vnodeCreateVnode(vgId, &vnodeCfg); - if (code != 0) { - vError("vgId:%d, failed to create vnode since %s", vgId, tstrerror(code)); - } - - return code; -} - -static int32_t vnodeProcessAlterVnodeReq(SRpcMsg *rpcMsg) { - SVnodeCfg vnodeCfg = {0}; - int32_t vgId = 0; - - int32_t code = vnodeParseCreateVnodeReq(rpcMsg, &vgId, &vnodeCfg); - if (code != 0) { - vError("failed to parse create vnode msg since %s", tstrerror(code)); - } - - vDebug("vgId:%d, alter vnode req is received", vgId); - - SVnode *pVnode = vnodeAcquire(vgId); - if (pVnode == NULL) { - code = terrno; - vDebug("vgId:%d, failed to alter vnode since %s", vgId, tstrerror(code)); - return code; - } - - code = vnodeAlterVnode(pVnode, &vnodeCfg); - if (code != 0) { - vError("vgId:%d, failed to alter vnode since %s", vgId, tstrerror(code)); - } - - vnodeRelease(pVnode); - return code; -} - -static SDropVnodeMsg *vnodeParseDropVnodeReq(SRpcMsg *rpcMsg) { - SDropVnodeMsg *pDrop = rpcMsg->pCont; - pDrop->vgId = htonl(pDrop->vgId); - return pDrop; -} - -static int32_t vnodeProcessSyncVnodeReq(SRpcMsg *rpcMsg) { - SSyncVnodeMsg *pSync = (SSyncVnodeMsg *)vnodeParseDropVnodeReq(rpcMsg); - - int32_t code = 0; - int32_t vgId = pSync->vgId; - vDebug("vgId:%d, sync vnode req is received", vgId); - - SVnode *pVnode = vnodeAcquire(vgId); - if (pVnode == NULL) { - code = terrno; - vDebug("vgId:%d, failed to sync since %s", vgId, tstrerror(code)); - return code; - } - - code = vnodeSyncVnode(pVnode); - if (code != 0) { - vError("vgId:%d, failed to compact vnode since %s", vgId, tstrerror(code)); - } - - vnodeRelease(pVnode); - return code; -} - -static int32_t vnodeProcessCompactVnodeReq(SRpcMsg *rpcMsg) { - SCompactVnodeMsg *pCompact = (SCompactVnodeMsg *)vnodeParseDropVnodeReq(rpcMsg); - - int32_t code = 0; - int32_t vgId = pCompact->vgId; - vDebug("vgId:%d, compact vnode req is received", vgId); - - SVnode *pVnode = vnodeAcquire(vgId); - if (pVnode == NULL) { - code = terrno; - vDebug("vgId:%d, failed to compact since %s", vgId, tstrerror(code)); - return code; - } - - code = vnodeCompactVnode(pVnode); - if (code != 0) { - vError("vgId:%d, failed to compact vnode since %s", vgId, tstrerror(code)); - } - - vnodeRelease(pVnode); - return code; -} - -static int32_t vnodeProcessDropVnodeReq(SRpcMsg *rpcMsg) { - SDropVnodeMsg *pDrop = vnodeParseDropVnodeReq(rpcMsg); - - int32_t code = 0; - int32_t vgId = pDrop->vgId; - vDebug("vgId:%d, drop vnode req is received", vgId); - - SVnode *pVnode = vnodeAcquire(vgId); - if (pVnode == NULL) { - code = terrno; - vDebug("vgId:%d, failed to drop since %s", vgId, tstrerror(code)); - return code; - } - - code = vnodeDropVnode(pVnode); - if (code != 0) { - vError("vgId:%d, failed to drop vnode since %s", vgId, tstrerror(code)); - } - - vnodeRelease(pVnode); - return code; -} - -static int32_t vnodeProcessAlterStreamReq(SRpcMsg *pMsg) { - vError("alter stream msg not processed"); - return TSDB_CODE_VND_MSG_NOT_PROCESSED; -} - -static void vnodeProcessMgmtReq(SVnMgmtMsg *pMgmt, void *unused) { - SRpcMsg *pMsg = &pMgmt->rpcMsg; - int32_t msgType = pMsg->msgType; - int32_t code = 0; - - if (tsVmgmt.msgFp[msgType]) { - vTrace("msg:%p, ahandle:%p type:%s will be processed", pMgmt, pMsg->ahandle, taosMsg[msgType]); - code = (*tsVmgmt.msgFp[msgType])(pMsg); - } else { - vError("msg:%p, ahandle:%p type:%s not processed since no handle", pMgmt, pMsg->ahandle, taosMsg[msgType]); - code = TSDB_CODE_DND_MSG_NOT_PROCESSED; - } - - SRpcMsg rsp = {.code = code, .handle = pMsg->handle}; - rpcSendResponse(&rsp); - taosFreeQitem(pMgmt); -} - -static void vnodeInitMgmtReqFp() { - tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = vnodeProcessCreateVnodeReq; - tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = vnodeProcessAlterVnodeReq; - tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = vnodeProcessSyncVnodeReq; - tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessCompactVnodeReq; - tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessDropVnodeReq; - tsVmgmt.msgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessAlterStreamReq; -} - -static int32_t vnodeWriteToMgmtQueue(SRpcMsg *pMsg) { - int32_t size = sizeof(SVnMgmtMsg) + pMsg->contLen; - SVnMgmtMsg *pMgmt = taosAllocateQitem(size); - if (pMgmt == NULL) return TSDB_CODE_DND_OUT_OF_MEMORY; - - pMgmt->rpcMsg = *pMsg; - pMgmt->rpcMsg.pCont = pMgmt->pCont; - memcpy(pMgmt->pCont, pMsg->pCont, pMsg->contLen); - - if (pMsg->msgType == TSDB_MSG_TYPE_MD_CREATE_VNODE) { - return taosWriteQitem(tsVmgmt.createQueue, pMgmt); - } else { - return taosWriteQitem(tsVmgmt.workerQueue, pMgmt); - } -} - -void vnodeProcessMgmtMsg(SRpcMsg *pMsg) { - int32_t code = vnodeWriteToMgmtQueue(pMsg); - if (code != TSDB_CODE_SUCCESS) { - vError("msg, ahandle:%p type:%s not processed since %s", pMsg->ahandle, taosMsg[pMsg->msgType], tstrerror(code)); - SRpcMsg rsp = {.handle = pMsg->handle, .code = code}; - rpcSendResponse(&rsp); - } - - rpcFreeCont(pMsg->pCont); -} - -int32_t vnodeInitMgmt() { - vnodeInitMgmtReqFp(); - - SWorkerPool *pPool = &tsVmgmt.createPool; - pPool->name = "vnode-mgmt-create"; - pPool->min = 1; - pPool->max = 1; - if (tWorkerInit(pPool) != 0) { - return TSDB_CODE_VND_OUT_OF_MEMORY; - } - - tsVmgmt.createQueue = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)vnodeProcessMgmtReq); - - pPool = &tsVmgmt.workerPool; - pPool->name = "vnode-mgmt-worker"; - pPool->min = 1; - pPool->max = 1; - if (tWorkerInit(pPool) != 0) { - return TSDB_CODE_VND_OUT_OF_MEMORY; - } - - tsVmgmt.workerQueue = tWorkerAllocQueue(pPool, NULL, (FProcessOneItem)vnodeProcessMgmtReq); - - vInfo("vmgmt is initialized"); - return TSDB_CODE_SUCCESS; -} - -void vnodeCleanupMgmt() { - tWorkerFreeQueue(&tsVmgmt.createPool, tsVmgmt.createQueue); - tWorkerCleanup(&tsVmgmt.createPool); - tsVmgmt.createQueue = NULL; - - tWorkerFreeQueue(&tsVmgmt.workerPool, tsVmgmt.workerQueue); - tWorkerCleanup(&tsVmgmt.workerPool); - tsVmgmt.createQueue = NULL; - vInfo("vmgmt is closed"); -} diff --git a/source/server/vnode/impl/src/vnodeRead.c b/source/server/vnode/impl/src/vnodeRead.c index ce6348c992..8964402f6c 100644 --- a/source/server/vnode/impl/src/vnodeRead.c +++ b/source/server/vnode/impl/src/vnodeRead.c @@ -14,199 +14,4 @@ */ #define _DEFAULT_SOURCE -#include "vnodeMain.h" #include "vnodeRead.h" -#include "vnodeReadMsg.h" - -static struct { - SWorkerPool query; - SWorkerPool fetch; - int32_t (*msgFp[TSDB_MSG_TYPE_MAX])(SVnode *, struct SReadMsg *); -} tsVread = {0}; - -void vnodeStartRead(SVnode *pVnode) {} -void vnodeStopRead(SVnode *pVnode) {} - -void vnodeWaitReadCompleted(SVnode *pVnode) { - while (pVnode->queuedRMsg > 0) { - vTrace("vgId:%d, queued rmsg num:%d", pVnode->vgId, pVnode->queuedRMsg); - taosMsleep(10); - } -} - -static int32_t vnodeWriteToRQueue(SVnode *pVnode, void *pCont, int32_t contLen, int8_t qtype, SRpcMsg *pRpcMsg) { - if (pVnode->dropped) { - return TSDB_CODE_APP_NOT_READY; - } - -#if 0 - if (!((pVnode->role == TAOS_SYNC_ROLE_MASTER) || (tsEnableSlaveQuery && pVnode->role == TAOS_SYNC_ROLE_SLAVE))) { - return TSDB_CODE_APP_NOT_READY; - } -#endif - - int32_t size = sizeof(SReadMsg) + contLen; - SReadMsg *pRead = taosAllocateQitem(size); - if (pRead == NULL) { - return TSDB_CODE_VND_OUT_OF_MEMORY; - } - - if (pRpcMsg != NULL) { - pRead->rpcHandle = pRpcMsg->handle; - pRead->rpcAhandle = pRpcMsg->ahandle; - pRead->msgType = pRpcMsg->msgType; - pRead->code = pRpcMsg->code; - } - - if (contLen != 0) { - pRead->contLen = contLen; - memcpy(pRead->pCont, pCont, contLen); - } else { - pRead->qhandle = pCont; - } - - pRead->qtype = qtype; - - atomic_add_fetch_32(&pVnode->refCount, 1); - atomic_add_fetch_32(&pVnode->queuedRMsg, 1); - - if (pRead->code == TSDB_CODE_RPC_NETWORK_UNAVAIL || pRead->msgType == TSDB_MSG_TYPE_FETCH) { - return taosWriteQitem(pVnode->pFetchQ, pRead); - } else { - return taosWriteQitem(pVnode->pQueryQ, pRead); - } -} - -static void vnodeFreeFromRQueue(SVnode *pVnode, SReadMsg *pRead) { - atomic_sub_fetch_32(&pVnode->queuedRMsg, 1); - - taosFreeQitem(pRead); - vnodeRelease(pVnode); -} - -int32_t vnodeReputPutToRQueue(SVnode *pVnode, void **qhandle, void *ahandle) { - SRpcMsg rpcMsg = {0}; - rpcMsg.msgType = TSDB_MSG_TYPE_QUERY; - rpcMsg.ahandle = ahandle; - - int32_t code = vnodeWriteToRQueue(pVnode, qhandle, 0, TAOS_QTYPE_QUERY, &rpcMsg); - if (code == TSDB_CODE_SUCCESS) { - vTrace("QInfo:%p add to vread queue for exec query", *qhandle); - } - - return code; -} - -void vnodeProcessReadMsg(SRpcMsg *pMsg) { - int32_t queuedMsgNum = 0; - int32_t leftLen = pMsg->contLen; - int32_t code = TSDB_CODE_VND_INVALID_VGROUP_ID; - char * pCont = pMsg->pCont; - - while (leftLen > 0) { - SMsgHead *pHead = (SMsgHead *)pCont; - pHead->vgId = htonl(pHead->vgId); - pHead->contLen = htonl(pHead->contLen); - - assert(pHead->contLen > 0); - SVnode *pVnode = vnodeAcquire(pHead->vgId); - if (pVnode != NULL) { - code = vnodeWriteToRQueue(pVnode, pCont, pHead->contLen, TAOS_QTYPE_RPC, pMsg); - if (code == TSDB_CODE_SUCCESS) queuedMsgNum++; - vnodeRelease(pVnode); - } - - leftLen -= pHead->contLen; - pCont -= pHead->contLen; - } - - if (queuedMsgNum == 0) { - SRpcMsg rpcRsp = {.handle = pMsg->handle, .code = code}; - rpcSendResponse(&rpcRsp); - } - - rpcFreeCont(pMsg->pCont); -} - -static void vnodeInitReadMsgFp() { - tsVread.msgFp[TSDB_MSG_TYPE_QUERY] = vnodeProcessQueryMsg; - tsVread.msgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessFetchMsg; - - tsVread.msgFp[TSDB_MSG_TYPE_MQ_QUERY] = vnodeProcessTqQueryMsg; - tsVread.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = vnodeProcessConsumeMsg; -} - -static void vnodeSendReadRsp(SReadMsg *pRead, int32_t code) { - SRpcMsg rpcRsp = { - .handle = pRead->rpcHandle, - .pCont = pRead->rspRet.rsp, - .contLen = pRead->rspRet.len, - .code = code, - }; - - rpcSendResponse(&rpcRsp); -} - -static void vnodeProcessReadReq(SReadMsg *pRead, SVnode *pVnode) { - int32_t msgType = pRead->msgType; - int32_t code = 0; - if (tsVread.msgFp[msgType] == NULL) { - vDebug("vgId:%d, msgType:%s not processed, no handle", pVnode->vgId, taosMsg[msgType]); - code = TSDB_CODE_VND_MSG_NOT_PROCESSED; - } else { - vTrace("msg:%p, app:%p type:%s will be processed", pRead, pRead->rpcAhandle, taosMsg[msgType]); - code = (*tsVread.msgFp[msgType])(pVnode, pRead); - } - - if (/*qtype == TAOS_QTYPE_RPC && */ code != TSDB_CODE_QRY_NOT_READY) { - vnodeSendReadRsp(pRead, code); - } else { - if (code == TSDB_CODE_QRY_HAS_RSP) { - vnodeSendReadRsp(pRead, pRead->code); - } else { // code == TSDB_CODE_QRY_NOT_READY, do not return msg to client - assert(pRead->rpcHandle == NULL || (pRead->rpcHandle != NULL && pRead->msgType == 5)); - } - } - - vnodeFreeFromRQueue(pVnode, pRead); -} - -int32_t vnodeInitRead() { - vnodeInitReadMsgFp(); - - int32_t maxFetchThreads = 4; - float threadsForQuery = MAX(tsNumOfCores * tsRatioOfQueryCores, 1); - - SWorkerPool *pPool = &tsVread.query; - pPool->name = "vquery"; - pPool->min = (int32_t)threadsForQuery; - pPool->max = pPool->min; - if (tWorkerInit(pPool) != 0) return -1; - - pPool = &tsVread.fetch; - pPool->name = "vfetch"; - pPool->min = MIN(maxFetchThreads, tsNumOfCores); - pPool->max = pPool->min; - if (tWorkerInit(pPool) != 0) return -1; - - vInfo("vread is initialized, max worker %d", pPool->max); - return 0; -} - -void vnodeCleanupRead() { - tWorkerCleanup(&tsVread.fetch); - tWorkerCleanup(&tsVread.query); - vInfo("vread is closed"); -} - -taos_queue vnodeAllocQueryQueue(SVnode *pVnode) { - return tWorkerAllocQueue(&tsVread.query, pVnode, (FProcessOneItem)vnodeProcessReadReq); -} - -taos_queue vnodeAllocFetchQueue(SVnode *pVnode) { - return tWorkerAllocQueue(&tsVread.fetch, pVnode, (FProcessOneItem)vnodeProcessReadReq); -} - -void vnodeFreeQueryQueue(taos_queue pQueue) { tWorkerFreeQueue(&tsVread.query, pQueue); } - -void vnodeFreeFetchQueue(taos_queue pQueue) { tWorkerFreeQueue(&tsVread.fetch, pQueue); } diff --git a/source/server/vnode/impl/src/vnodeReadMsg.c b/source/server/vnode/impl/src/vnodeReadMsg.c deleted file mode 100644 index 58dfe4116a..0000000000 --- a/source/server/vnode/impl/src/vnodeReadMsg.c +++ /dev/null @@ -1,342 +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 "vnodeMain.h" -#include "vnodeRead.h" -#include "vnodeReadMsg.h" - -#if 0 -// notify connection(handle) that current qhandle is created, if current connection from -// client is broken, the query needs to be killed immediately. -static int32_t vnodeNotifyCurrentQhandle(void *handle, uint64_t qId, void *qhandle, int32_t vgId) { - SRetrieveTableMsg *pMsg = rpcMallocCont(sizeof(SRetrieveTableMsg)); - pMsg->qId = htobe64(qId); - pMsg->header.vgId = htonl(vgId); - pMsg->header.contLen = htonl(sizeof(SRetrieveTableMsg)); - - vTrace("QInfo:0x%" PRIx64 "-%p register qhandle to connect:%p", qId, qhandle, handle); - return rpcReportProgress(handle, (char *)pMsg, sizeof(SRetrieveTableMsg)); -} - -/** - * @param pRet response message object - * @param pVnode the vnode object - * @param handle qhandle for executing query - * @param freeHandle free qhandle or not - * @param ahandle sqlObj address at client side - * @return - */ -static int32_t vnodeDumpQueryResult(SVnRsp *pRet, void *pVnode, uint64_t qId, void **handle, bool *freeHandle, - void *ahandle) { - bool continueExec = false; - - int32_t code = TSDB_CODE_SUCCESS; - if ((code = qDumpRetrieveResult(*handle, (SRetrieveTableRsp **)&pRet->rsp, &pRet->len, &continueExec)) == - TSDB_CODE_SUCCESS) { - if (continueExec) { - *freeHandle = false; - code = vnodeReputPutToRQueue(pVnode, handle, ahandle); - if (code != TSDB_CODE_SUCCESS) { - *freeHandle = true; - return code; - } else { - pRet->qhandle = *handle; - } - } else { - *freeHandle = true; - vTrace("QInfo:0x%" PRIx64 "-%p exec completed, free handle:%d", qId, *handle, *freeHandle); - } - } else { - SRetrieveTableRsp *pRsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp)); - memset(pRsp, 0, sizeof(SRetrieveTableRsp)); - pRsp->completed = true; - - pRet->rsp = pRsp; - pRet->len = sizeof(SRetrieveTableRsp); - *freeHandle = true; - } - - return code; -} - -static void vnodeBuildNoResultQueryRsp(SVnRsp *pRet) { - pRet->rsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp)); - pRet->len = sizeof(SRetrieveTableRsp); - - memset(pRet->rsp, 0, sizeof(SRetrieveTableRsp)); - SRetrieveTableRsp *pRsp = pRet->rsp; - - pRsp->completed = true; -} -#endif - -int32_t vnodeProcessQueryMsg(SVnode *pVnode, SReadMsg *pRead) { -#if 0 - void * pCont = pRead->pCont; - int32_t contLen = pRead->contLen; - SVnRsp *pRet = &pRead->rspRet; - - SQueryTableMsg *pQueryTableMsg = (SQueryTableMsg *)pCont; - memset(pRet, 0, sizeof(SVnRsp)); - - // qHandle needs to be freed correctly - if (pRead->code == TSDB_CODE_RPC_NETWORK_UNAVAIL) { - vError("error rpc msg in query, %s", tstrerror(pRead->code)); - } - - int32_t code = TSDB_CODE_SUCCESS; - void ** handle = NULL; - - if (contLen != 0) { - qinfo_t pQInfo = NULL; - uint64_t qId = genQueryId(); - code = qCreateQueryInfo(pVnode->tsdb, pVnode->vgId, pQueryTableMsg, &pQInfo, qId); - - SQueryTableRsp *pRsp = (SQueryTableRsp *)rpcMallocCont(sizeof(SQueryTableRsp)); - pRsp->code = code; - pRsp->qId = 0; - - pRet->len = sizeof(SQueryTableRsp); - pRet->rsp = pRsp; - int32_t vgId = pVnode->vgId; - - // current connect is broken - if (code == TSDB_CODE_SUCCESS) { - handle = qRegisterQInfo(pVnode->qMgmt, qId, pQInfo); - if (handle == NULL) { // failed to register qhandle - pRsp->code = terrno; - terrno = 0; - - vError("vgId:%d, QInfo:0x%" PRIx64 "-%p register qhandle failed, return to app, code:%s,", pVnode->vgId, qId, - (void *)pQInfo, tstrerror(pRsp->code)); - qDestroyQueryInfo(pQInfo); // destroy it directly - return pRsp->code; - } else { - assert(*handle == pQInfo); - pRsp->qId = htobe64(qId); - } - - if (handle != NULL && - vnodeNotifyCurrentQhandle(pRead->rpcHandle, qId, *handle, pVnode->vgId) != TSDB_CODE_SUCCESS) { - vError("vgId:%d, QInfo:0x%" PRIx64 "-%p, query discarded since link is broken, %p", pVnode->vgId, qId, *handle, - pRead->rpcHandle); - - pRsp->code = TSDB_CODE_RPC_NETWORK_UNAVAIL; - qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); - return pRsp->code; - } - - } else { - assert(pQInfo == NULL); - } - - if (handle != NULL) { - vTrace("vgId:%d, QInfo:0x%" PRIx64 "-%p, query msg disposed, create qhandle and returns to app", vgId, qId, - *handle); - code = vnodeReputPutToRQueue(pVnode, handle, pRead->rpcHandle); - if (code != TSDB_CODE_SUCCESS) { - pRsp->code = code; - qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); - return pRsp->code; - } - } - - int32_t remain = atomic_add_fetch_32(&pVnode->numOfQHandle, 1); - vTrace("vgId:%d, new qhandle created, total qhandle:%d", pVnode->vgId, remain); - } else { - assert(pCont != NULL); - void ** qhandle = (void **)pRead->qhandle; - uint64_t qId = 0; - - vTrace("vgId:%d, QInfo:%p, continues to exec query", pVnode->vgId, *qhandle); - - // In the retrieve blocking model, only 50% CPU will be used in query processing - if (tsRetrieveBlockingModel) { - qTableQuery(*qhandle, &qId); // do execute query - qReleaseQInfo(pVnode->qMgmt, (void **)&qhandle, false); - } else { - bool freehandle = false; - bool buildRes = qTableQuery(*qhandle, &qId); // do execute query - - // build query rsp, the retrieve request has reached here already - if (buildRes) { - // update the connection info according to the retrieve connection - pRead->rpcHandle = qGetResultRetrieveMsg(*qhandle); - assert(pRead->rpcHandle != NULL); - - vTrace("vgId:%d, QInfo:%p, start to build retrieval rsp after query paused, %p", pVnode->vgId, *qhandle, - pRead->rpcHandle); - - // set the real rsp error code - pRead->code = vnodeDumpQueryResult(&pRead->rspRet, pVnode, qId, qhandle, &freehandle, pRead->rpcHandle); - - // NOTE: set return code to be TSDB_CODE_QRY_HAS_RSP to notify dnode to return msg to client - code = TSDB_CODE_QRY_HAS_RSP; - } else { - // void *h1 = qGetResultRetrieveMsg(*qhandle); - - /* remove this assert, one possible case that will cause h1 not NULL: query thread unlock pQInfo->lock, and then - * FETCH thread execute twice before query thread reach here */ - // assert(h1 == NULL); - - freehandle = qQueryCompleted(*qhandle); - } - - // NOTE: if the qhandle is not put into vread queue or query is completed, free the qhandle. - // If the building of result is not required, simply free it. Otherwise, mandatorily free the qhandle - if (freehandle || (!buildRes)) { - if (freehandle) { - int32_t remain = atomic_sub_fetch_32(&pVnode->numOfQHandle, 1); - vTrace("vgId:%d, QInfo:%p, start to free qhandle, remain qhandle:%d", pVnode->vgId, *qhandle, remain); - } - - qReleaseQInfo(pVnode->qMgmt, (void **)&qhandle, freehandle); - } - } - } - - return code; -#endif - return 0; -} - -//mq related -int32_t vnodeProcessConsumeMsg(SVnode *pVnode, SReadMsg *pRead) { - //parse message and optionally move offset - void* pMsg = pRead->pCont; - TmqConsumeReq *pConsumeMsg = (TmqConsumeReq*) pMsg; - TmqMsgHead msgHead = pConsumeMsg->head; - //extract head - STQ *pTq = pVnode->pTQ; - /*tqBufferHandle *pHandle = tqGetHandle(pTq, msgHead.clientId);*/ - //return msg if offset not moved - /*if(pConsumeMsg->commitOffset == pHandle->consumeOffset) {*/ - //return msg - /*return 0;*/ - /*}*/ - //or move offset - /*tqMoveOffsetToNext(pHandle);*/ - //fetch or register context - /*tqFetchMsg(pHandle, pRead);*/ - //judge mode, tail read or catch up read - /*int64_t lastVer = walLastVer(pVnode->wal);*/ - //launch new query - return 0; -} - -int32_t vnodeProcessTqQueryMsg(SVnode *pVnode, SReadMsg *pRead) { - //get operator tree from tq data structure - //execute operator tree - //put data into ringbuffer - //unref memory - return 0; -} -//mq related end - -int32_t vnodeProcessFetchMsg(SVnode *pVnode, SReadMsg *pRead) { -#if 0 - void * pCont = pRead->pCont; - SVnRsp *pRet = &pRead->rspRet; - - SRetrieveTableMsg *pRetrieve = pCont; - pRetrieve->free = htons(pRetrieve->free); - pRetrieve->qId = htobe64(pRetrieve->qId); - - vTrace("vgId:%d, qId:0x%" PRIx64 ", retrieve msg is disposed, free:%d, conn:%p", pVnode->vgId, pRetrieve->qId, - pRetrieve->free, pRead->rpcHandle); - - memset(pRet, 0, sizeof(SVnRsp)); - - terrno = TSDB_CODE_SUCCESS; - int32_t code = TSDB_CODE_SUCCESS; - void ** handle = qAcquireQInfo(pVnode->qMgmt, pRetrieve->qId); - if (handle == NULL) { - code = terrno; - terrno = TSDB_CODE_SUCCESS; - } else if (!checkQIdEqual(*handle, pRetrieve->qId)) { - code = TSDB_CODE_QRY_INVALID_QHANDLE; - } - - if (code != TSDB_CODE_SUCCESS) { - vError("vgId:%d, invalid qId in retrieving result, code:%s, QInfo:%" PRIu64, pVnode->vgId, tstrerror(code), - pRetrieve->qId); - vnodeBuildNoResultQueryRsp(pRet); - return code; - } - - // kill current query and free corresponding resources. - if (pRetrieve->free == 1) { - int32_t remain = atomic_sub_fetch_32(&pVnode->numOfQHandle, 1); - vWarn("vgId:%d, QInfo:%" PRIx64 "-%p, retrieve msg received to kill query and free qhandle, remain qhandle:%d", - pVnode->vgId, pRetrieve->qId, *handle, remain); - - qKillQuery(*handle); - qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); - - vnodeBuildNoResultQueryRsp(pRet); - code = TSDB_CODE_TSC_QUERY_CANCELLED; - return code; - } - - // register the qhandle to connect to quit query immediate if connection is broken - if (vnodeNotifyCurrentQhandle(pRead->rpcHandle, pRetrieve->qId, *handle, pVnode->vgId) != TSDB_CODE_SUCCESS) { - int32_t remain = atomic_sub_fetch_32(&pVnode->numOfQHandle, 1); - vError("vgId:%d, QInfo:%" PRIu64 "-%p, retrieve discarded since link is broken, conn:%p, remain qhandle:%d", - pVnode->vgId, pRetrieve->qhandle, *handle, pRead->rpcHandle, remain); - - code = TSDB_CODE_RPC_NETWORK_UNAVAIL; - qKillQuery(*handle); - qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); - return code; - } - - bool freeHandle = true; - bool buildRes = false; - - code = qRetrieveQueryResultInfo(*handle, &buildRes, pRead->rpcHandle); - if (code != TSDB_CODE_SUCCESS) { - // TODO handle malloc failure - pRet->rsp = (SRetrieveTableRsp *)rpcMallocCont(sizeof(SRetrieveTableRsp)); - pRet->len = sizeof(SRetrieveTableRsp); - memset(pRet->rsp, 0, sizeof(SRetrieveTableRsp)); - freeHandle = true; - } else { // result is not ready, return immediately - // Only affects the non-blocking model - if (!tsRetrieveBlockingModel) { - if (!buildRes) { - assert(pRead->rpcHandle != NULL); - qReleaseQInfo(pVnode->qMgmt, (void **)&handle, false); - return TSDB_CODE_QRY_NOT_READY; - } - } - - // ahandle is the sqlObj pointer - code = vnodeDumpQueryResult(pRet, pVnode, pRetrieve->qId, handle, &freeHandle, pRead->rpcHandle); - } - - // If qhandle is not added into vread queue, the query should be completed already or paused with error. - // Here free qhandle immediately - if (freeHandle) { - int32_t remain = atomic_sub_fetch_32(&pVnode->numOfQHandle, 1); - vTrace("vgId:%d, QInfo:%p, start to free qhandle, remain qhandle:%d", pVnode->vgId, *handle, remain); - qReleaseQInfo(pVnode->qMgmt, (void **)&handle, true); - } - - return code; -#endif - return 0; -} - diff --git a/source/server/vnode/impl/src/vnodeWrite.c b/source/server/vnode/impl/src/vnodeWrite.c index 119cd29537..dc12ffb241 100644 --- a/source/server/vnode/impl/src/vnodeWrite.c +++ b/source/server/vnode/impl/src/vnodeWrite.c @@ -14,216 +14,4 @@ */ #define _DEFAULT_SOURCE -#include "os.h" -#include "vnodeMain.h" #include "vnodeWrite.h" -#include "vnodeWriteMsg.h" - -typedef int32_t (*WriteMsgFp)(SVnode *, void *pCont, SVnRsp *); - -typedef struct { - int32_t code; - int8_t qtype; - SVnode * pVnode; - SRpcMsg rpcMsg; - SVnRsp rspRet; - char reserveForSync[24]; - SWalHead walHead; -} SVnWriteMsg; - -static struct { - SMWorkerPool pool; - int64_t queuedBytes; - int32_t queuedMsgs; -} tsVwrite = {0}; - -void vnodeStartWrite(SVnode *pVnode) {} -void vnodeStopWrite(SVnode *pVnode) { - while (pVnode->queuedWMsg > 0) { - vTrace("vgId:%d, queued wmsg num:%d", pVnode->vgId, pVnode->queuedWMsg); - taosMsleep(10); - } -} - -static int32_t vnodeWriteToWQueue(SVnode *pVnode, SWalHead *pHead, int32_t qtype, SRpcMsg *pRpcMsg) { - if (!(pVnode->accessState & TSDB_VN_WRITE_ACCCESS)) { - vWarn("vgId:%d, no write auth", pVnode->vgId); - return TSDB_CODE_VND_NO_WRITE_AUTH; - } - - if (tsAvailDataDirGB <= tsMinimalDataDirGB) { - vWarn("vgId:%d, failed to write into vwqueue since no diskspace, avail:%fGB", pVnode->vgId, tsAvailDataDirGB); - return TSDB_CODE_VND_NO_DISKSPACE; - } - - if (pHead->len > TSDB_MAX_WAL_SIZE) { - vError("vgId:%d, wal len:%d exceeds limit, hver:%" PRIu64, pVnode->vgId, pHead->len, pHead->version); - return TSDB_CODE_WAL_SIZE_LIMIT; - } - - if (tsVwrite.queuedBytes > tsMaxVnodeQueuedBytes) { - vDebug("vgId:%d, too many bytes:%" PRId64 " in vwqueue, flow control", pVnode->vgId, tsVwrite.queuedBytes); - return TSDB_CODE_VND_IS_FLOWCTRL; - } - - int32_t size = sizeof(SVnWriteMsg) + pHead->len; - SVnWriteMsg *pWrite = taosAllocateQitem(size); - if (pWrite == NULL) { - return TSDB_CODE_VND_OUT_OF_MEMORY; - } - - if (pRpcMsg != NULL) { - pWrite->rpcMsg = *pRpcMsg; - } - - memcpy(&pWrite->walHead, pHead, sizeof(SWalHead) + pHead->len); - pWrite->pVnode = pVnode; - pWrite->qtype = qtype; - - atomic_add_fetch_64(&tsVwrite.queuedBytes, size); - atomic_add_fetch_32(&tsVwrite.queuedMsgs, 1); - atomic_add_fetch_32(&pVnode->refCount, 1); - atomic_add_fetch_32(&pVnode->queuedWMsg, 1); - taosWriteQitem(pVnode->pWriteQ, pWrite); - - return TSDB_CODE_SUCCESS; -} - -static void vnodeFreeFromWQueue(SVnode *pVnode, SVnWriteMsg *pWrite) { - int64_t size = sizeof(SVnWriteMsg) + pWrite->walHead.len; - atomic_sub_fetch_64(&tsVwrite.queuedBytes, size); - atomic_sub_fetch_32(&tsVwrite.queuedMsgs, 1); - atomic_sub_fetch_32(&pVnode->queuedWMsg, 1); - - taosFreeQitem(pWrite); - vnodeRelease(pVnode); -} - -void vnodeProcessWriteReq(SRpcMsg *pRpcMsg) { - int32_t code; - - SMsgHead *pMsg = pRpcMsg->pCont; - pMsg->vgId = htonl(pMsg->vgId); - pMsg->contLen = htonl(pMsg->contLen); - - SVnode *pVnode = vnodeAcquire(pMsg->vgId); - if (pVnode == NULL) { - code = TSDB_CODE_VND_INVALID_VGROUP_ID; - } else { - SWalHead *pHead = (SWalHead *)((char *)pRpcMsg->pCont - sizeof(SWalHead)); - pHead->msgType = pRpcMsg->msgType; - pHead->version = 0; - pHead->len = pMsg->contLen; - code = vnodeWriteToWQueue(pVnode, pHead, TAOS_QTYPE_RPC, pRpcMsg); - } - - if (code != TSDB_CODE_SUCCESS) { - SRpcMsg rpcRsp = {.handle = pRpcMsg->handle, .code = code}; - rpcSendResponse(&rpcRsp); - } - - vnodeRelease(pVnode); - rpcFreeCont(pRpcMsg->pCont); -} - -static void vnodeProcessWrite(SVnWriteMsg *pWrite, SVnode *pVnode) { -} - // SWalHead *pHead = &pWrite->walHead; - // SVnRsp * pRet = &pWrite->rspRet; - // int32_t msgType = pHead->msgType; - - // vTrace("vgId:%d, msg:%s will be processed, hver:%" PRIu64, pVnode->vgId, taosMsg[pHead->msgType], pHead->version); - - // // write data locally - // switch (msgType) { - // case TSDB_MSG_TYPE_SUBMIT: - // pRet->len = sizeof(SSubmitRsp); - // pRet->rsp = rpcMallocCont(pRet->len); - // pWrite->code = vnodeProcessSubmitReq(pVnode, (void*)pHead->cont, pRet->rsp); - // break; - // case TSDB_MSG_TYPE_MD_CREATE_TABLE: - // pWrite->code = vnodeProcessCreateTableReq(pVnode, (void*)pHead->cont, NULL); - // break; - // case TSDB_MSG_TYPE_MD_DROP_TABLE: - // pWrite->code = vnodeProcessDropTableReq(pVnode, (void*)pHead->cont, NULL); - // break; - // case TSDB_MSG_TYPE_MD_ALTER_TABLE: - // pWrite->code = vnodeProcessAlterTableReq(pVnode, (void*)pHead->cont, NULL); - // break; - // case TSDB_MSG_TYPE_MD_DROP_STABLE: - // pWrite->code = vnodeProcessDropStableReq(pVnode, (void*)pHead->cont, NULL); - // break; - // case TSDB_MSG_TYPE_UPDATE_TAG_VAL: - // pWrite->code = vnodeProcessUpdateTagValReq(pVnode, (void*)pHead->cont, NULL); - // break; - // //mq related - // case TSDB_MSG_TYPE_MQ_CONNECT: - // pWrite->code = vnodeProcessMqConnectReq(pVnode, (void*)pHead->cont, NULL); - // break; - // case TSDB_MSG_TYPE_MQ_DISCONNECT: - // pWrite->code = vnodeProcessMqDisconnectReq(pVnode, (void*)pHead->cont, NULL); - // break; - // case TSDB_MSG_TYPE_MQ_ACK: - // pWrite->code = vnodeProcessMqAckReq(pVnode, (void*)pHead->cont, NULL); - // break; - // case TSDB_MSG_TYPE_MQ_RESET: - // pWrite->code = vnodeProcessMqResetReq(pVnode, (void*)pHead->cont, NULL); - // break; - // //mq related end - // default: - // pWrite->code = TSDB_CODE_VND_MSG_NOT_PROCESSED; - // break; - // } - - // if (pWrite->code < 0) return false; - - // // update fsync - // return (pWrite->code == 0 && msgType != TSDB_MSG_TYPE_SUBMIT); - - - // // walFsync(pVnode->wal, fsync); - - -// static void vnodeProcessWriteEnd(SVnWriteMsg *pWrite, SVnode *pVnode) { -// if (qtype == TAOS_QTYPE_RPC) { -// SRpcMsg rpcRsp = { -// .handle = pWrite->rpcMsg.handle, -// .pCont = pWrite->rspRet.rsp, -// .contLen = pWrite->rspRet.len, -// .code = pWrite->code, -// }; -// rpcSendResponse(&rpcRsp); -// } else { -// if (pWrite->rspRet.rsp) { -// rpcFreeCont(pWrite->rspRet.rsp); -// } -// } -// vnodeFreeFromWQueue(pVnode, pWrite); -// } - -int32_t vnodeInitWrite() { - SMWorkerPool *pPool = &tsVwrite.pool; - pPool->name = "vnode-write"; - pPool->max = tsNumOfCores; - if (tMWorkerInit(pPool) != 0) return -1; - - vInfo("vwrite is initialized, max worker %d", pPool->max); - return TSDB_CODE_SUCCESS; -} - -void vnodeCleanupWrite() { - tMWorkerCleanup(&tsVwrite.pool); - vInfo("vwrite is closed"); -} - -taos_queue vnodeAllocWriteQueue(SVnode *pVnode) { - return tMWorkerAllocQueue(&tsVwrite.pool, pVnode, NULL); -} - -void vnodeFreeWriteQueue(taos_queue pQueue) { tMWorkerFreeQueue(&tsVwrite.pool, pQueue); } - -taos_queue vnodeAllocApplyQueue(SVnode *pVnode) { - return tMWorkerAllocQueue(&tsVwrite.pool, pVnode, NULL); -} - -void vnodeFreeApplyQueue(taos_queue pQueue) { tMWorkerFreeQueue(&tsVwrite.pool, pQueue); } diff --git a/source/server/vnode/impl/src/vnodeWriteMsg.c b/source/server/vnode/impl/src/vnodeWriteMsg.c deleted file mode 100644 index 2e13d0035d..0000000000 --- a/source/server/vnode/impl/src/vnodeWriteMsg.c +++ /dev/null @@ -1,94 +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 "vnodeWriteMsg.h" - -int32_t vnodeProcessSubmitReq(SVnode *pVnode, SSubmitReq *pReq, SSubmitRsp *pRsp) { - // TODO: Check inputs - -#if 0 - void *pMem = NULL; - if ((pMem = amalloc(pVnode->allocator, REQ_SIZE(pReq))) == NULL) { - // No more memory to allocate, schedule an async commit - // and continue - vnodeAsyncCommit(pVnode); - - // Reset allocator and allocat more - vnodeResetAllocator(pVnode); - pMem = amalloc(pVnode->allocator, REQ_SIZE(pReq)); - if (pMem == NULL) { - // TODO: handle the error - } - } - - // TODO: if SSubmitReq is compressed or encoded, we need to decode the request - memcpy(pMem, pReq, REQ_SIZE(pReq)); - - if (tqPushMsg((SSubmitReq *)pReq) < 0) { - // TODO: handle error - } - - SSubmitReqReader reader; - taosInitSubmitReqReader(&reader, (SSubmitReq *)pMem); - - if (tsdbInsert(pVnode->pTsdb, (SSubmitReq *)pMem) < 0) { - // TODO: handler error - } -#endif - - return 0; -} - -int32_t vnodeProcessCreateTableReq(SVnode *pVnode, SCreateTableReq *pReq, SCreateTableRsp *pRsp) { - // TODO - return 0; -} - - int32_t vnodeProcessDropTableReq(SVnode *pVnode, SDropTableReq *pReq, SDropTableRsp *pRsp) { - // TODO - return 0; -} - -int32_t vnodeProcessAlterTableReq(SVnode *pVnode, SAlterTableReq *pReq, SAlterTableRsp *pRsp) { - // TODO - return 0; -} - -int32_t vnodeProcessDropStableReq(SVnode *pVnode, SDropStableReq *pReq, SDropStableRsp *pRsp) { - // TODO - return 0; -} - -int32_t vnodeProcessUpdateTagValReq(SVnode *pVnode, SUpdateTagValReq *pReq, SUpdateTagValRsp *pRsp) { - // TODO - return 0; -} - -//mq related -int32_t vnodeProcessMqConnectReq(SVnode* pVnode, SMqConnectReq *pReq, SMqConnectRsp *pRsp){ - return 0; -} -int32_t vnodeProcessMqDisconnectReq(SVnode* pVnode, SMqConnectReq *pReq, SMqConnectRsp *pRsp) { - return 0; -} -int32_t vnodeProcessMqAckReq(SVnode* pVnode, SMqAckReq *pReq, SMqAckRsp *pRsp) { - return 0; -} -int32_t vnodeProcessMqResetReq(SVnode* pVnode, SMqResetReq *pReq, SMqResetRsp *pRsp) { - return 0; -} -//mq related end diff --git a/source/server/vnode/impl/test/CMakeLists.txt b/source/server/vnode/impl/test/CMakeLists.txt index fad366e6ef..9c09be56fb 100644 --- a/source/server/vnode/impl/test/CMakeLists.txt +++ b/source/server/vnode/impl/test/CMakeLists.txt @@ -6,4 +6,4 @@ target_sources(VMATest "vnodeMemAllocatorTest.cpp" ) target_include_directories(VMATest PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../inc") -target_link_libraries(VMATest os gtest_main) \ No newline at end of file +target_link_libraries(VMATest os gtest_main vnode) \ No newline at end of file diff --git a/source/util/src/tqueue.c b/source/util/src/tqueue.c index bb0303c04a..2813a55fea 100644 --- a/source/util/src/tqueue.c +++ b/source/util/src/tqueue.c @@ -31,8 +31,8 @@ typedef struct STaosQueue { struct STaosQueue *next; // for queue set struct STaosQset *qset; // for queue set void *ahandle; // for queue set - FProcessOneItem fpOneItem; - FProcessAllItem fpAllItem; + FProcessItem itemFp; + FProcessItems itemsFp; pthread_mutex_t mutex; } STaosQueue; @@ -65,11 +65,11 @@ taos_queue taosOpenQueue() { return queue; } -void taosSetQueueFp(taos_queue param, FProcessOneItem fpOneItem, FProcessAllItem fpAllItem) { +void taosSetQueueFp(taos_queue param, FProcessItem itemFp, FProcessItems itemsFp) { if (param == NULL) return; STaosQueue *queue = (STaosQueue *)param; - queue->fpOneItem = fpOneItem; - queue->fpAllItem = fpAllItem; + queue->itemFp = itemFp; + queue->itemsFp = itemsFp; } void taosCloseQueue(taos_queue param) { @@ -341,7 +341,7 @@ void taosRemoveFromQset(taos_qset p1, taos_queue p2) { int taosGetQueueNumber(taos_qset param) { return ((STaosQset *)param)->numOfQueues; } -int taosReadQitemFromQset(taos_qset param, void **pitem, void **ahandle, FProcessOneItem *fpOneItem) { +int taosReadQitemFromQset(taos_qset param, void **pitem, void **ahandle, FProcessItem *itemFp) { STaosQset *qset = (STaosQset *)param; STaosQnode *pNode = NULL; int code = 0; @@ -363,7 +363,7 @@ int taosReadQitemFromQset(taos_qset param, void **pitem, void **ahandle, FProces pNode = queue->head; *pitem = pNode->item; if (ahandle) *ahandle = queue->ahandle; - if (fpOneItem) *fpOneItem = queue->fpOneItem; + if (itemFp) *itemFp = queue->itemFp; queue->head = pNode->next; if (queue->head == NULL) queue->tail = NULL; queue->numOfItems--; @@ -381,7 +381,7 @@ int taosReadQitemFromQset(taos_qset param, void **pitem, void **ahandle, FProces return code; } -int taosReadAllQitemsFromQset(taos_qset param, taos_qall p2, void **ahandle, FProcessAllItem *fpAllItem) { +int taosReadAllQitemsFromQset(taos_qset param, taos_qall p2, void **ahandle, FProcessItems *itemsFp) { STaosQset *qset = (STaosQset *)param; STaosQueue *queue; STaosQall *qall = (STaosQall *)p2; @@ -407,7 +407,7 @@ int taosReadAllQitemsFromQset(taos_qset param, taos_qall p2, void **ahandle, FPr qall->itemSize = queue->itemSize; code = qall->numOfItems; if (ahandle) *ahandle = queue->ahandle; - if (fpAllItem) *fpAllItem = queue->fpAllItem; + if (itemsFp) *itemsFp = queue->itemsFp; queue->head = NULL; queue->tail = NULL; diff --git a/source/util/src/tworker.c b/source/util/src/tworker.c index 12f1aac9f9..7df12089b7 100644 --- a/source/util/src/tworker.c +++ b/source/util/src/tworker.c @@ -58,8 +58,8 @@ void tWorkerCleanup(SWorkerPool *pool) { } static void *tWorkerThreadFp(SWorker *worker) { - SWorkerPool *pool = worker->pool; - FProcessOneItem fp = NULL; + SWorkerPool *pool = worker->pool; + FProcessItem fp = NULL; void *msg = NULL; void *ahandle = NULL; @@ -83,7 +83,7 @@ static void *tWorkerThreadFp(SWorker *worker) { return NULL; } -taos_queue tWorkerAllocQueue(SWorkerPool *pool, void *ahandle, FProcessOneItem fp) { +taos_queue tWorkerAllocQueue(SWorkerPool *pool, void *ahandle, FProcessItem fp) { pthread_mutex_lock(&pool->mutex); taos_queue queue = taosOpenQueue(); if (queue == NULL) { @@ -166,8 +166,8 @@ void tMWorkerCleanup(SMWorkerPool *pool) { } static void *tWriteWorkerThreadFp(SMWorker *worker) { - SMWorkerPool *pool = worker->pool; - FProcessAllItem fp = NULL; + SMWorkerPool *pool = worker->pool; + FProcessItems fp = NULL; void *msg = NULL; void *ahandle = NULL; @@ -193,7 +193,7 @@ static void *tWriteWorkerThreadFp(SMWorker *worker) { return NULL; } -taos_queue tMWorkerAllocQueue(SMWorkerPool *pool, void *ahandle, FProcessAllItem fp) { +taos_queue tMWorkerAllocQueue(SMWorkerPool *pool, void *ahandle, FProcessItems fp) { pthread_mutex_lock(&pool->mutex); SMWorker *worker = pool->workers + pool->nextId; diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 308898950a..caa334aaed 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1166,7 +1166,7 @@ int32_t tscBuildCreateDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd *pCmd = &pSql->cmd; pCmd->payloadLen = sizeof(SCreateDbMsg); - pCmd->msgType = (pInfo->pMiscInfo->dbOpt.dbType == TSDB_DB_TYPE_DEFAULT) ? TSDB_MSG_TYPE_CM_CREATE_DB : TSDB_MSG_TYPE_CM_CREATE_TP; + pCmd->msgType = (pInfo->pMiscInfo->dbOpt.dbType == TSDB_DB_TYPE_DEFAULT) ? TSDB_MSG_TYPE_CREATE_DB : TSDB_MSG_TYPE_CREATE_TP; SCreateDbMsg *pCreateDbMsg = (SCreateDbMsg *)pCmd->payload; @@ -1182,7 +1182,7 @@ int32_t tscBuildCreateFuncMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd *pCmd = &pSql->cmd; SCreateFuncMsg *pCreateFuncMsg = (SCreateFuncMsg *)pCmd->payload; - pCmd->msgType = TSDB_MSG_TYPE_CM_CREATE_FUNCTION; + pCmd->msgType = TSDB_MSG_TYPE_CREATE_FUNCTION; pCmd->payloadLen = sizeof(SCreateFuncMsg) + htonl(pCreateFuncMsg->codeLen); @@ -1203,7 +1203,7 @@ int32_t tscBuildCreateDnodeMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SStrToken* t0 = taosArrayGet(pInfo->pMiscInfo->a, 0); strncpy(pCreate->ep, t0->z, t0->n); - pCmd->msgType = TSDB_MSG_TYPE_CM_CREATE_DNODE; + pCmd->msgType = TSDB_MSG_TYPE_CREATE_DNODE; return TSDB_CODE_SUCCESS; } @@ -1249,7 +1249,7 @@ int32_t tscBuildAcctMsg(SSqlObj *pSql, SSqlInfo *pInfo) { } } - pCmd->msgType = TSDB_MSG_TYPE_CM_CREATE_ACCT; + pCmd->msgType = TSDB_MSG_TYPE_CREATE_ACCT; return TSDB_CODE_SUCCESS; } @@ -1277,9 +1277,9 @@ int32_t tscBuildUserMsg(SSqlObj *pSql, SSqlInfo *pInfo) { } if (pUser->type == TSDB_ALTER_USER_PASSWD || pUser->type == TSDB_ALTER_USER_PRIVILEGES) { - pCmd->msgType = TSDB_MSG_TYPE_CM_ALTER_USER; + pCmd->msgType = TSDB_MSG_TYPE_ALTER_USER; } else { - pCmd->msgType = TSDB_MSG_TYPE_CM_CREATE_USER; + pCmd->msgType = TSDB_MSG_TYPE_CREATE_USER; } return TSDB_CODE_SUCCESS; @@ -1288,7 +1288,7 @@ int32_t tscBuildUserMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int32_t tscBuildCfgDnodeMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd *pCmd = &pSql->cmd; pCmd->payloadLen = sizeof(SCfgDnodeMsg); - pCmd->msgType = TSDB_MSG_TYPE_CM_CONFIG_DNODE; + pCmd->msgType = TSDB_MSG_TYPE_CONFIG_DNODE; return TSDB_CODE_SUCCESS; } @@ -1310,14 +1310,14 @@ int32_t tscBuildDropDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pDropDbMsg->ignoreNotExists = pInfo->pMiscInfo->existsCheck ? 1 : 0; - pCmd->msgType = (pInfo->pMiscInfo->dbType == TSDB_DB_TYPE_DEFAULT) ? TSDB_MSG_TYPE_CM_DROP_DB : TSDB_MSG_TYPE_CM_DROP_TP; + pCmd->msgType = (pInfo->pMiscInfo->dbType == TSDB_DB_TYPE_DEFAULT) ? TSDB_MSG_TYPE_DROP_DB : TSDB_MSG_TYPE_DROP_TP; return TSDB_CODE_SUCCESS; } int32_t tscBuildDropFuncMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd *pCmd = &pSql->cmd; - pCmd->msgType = TSDB_MSG_TYPE_CM_DROP_FUNCTION; + pCmd->msgType = TSDB_MSG_TYPE_DROP_FUNCTION; pCmd->payloadLen = sizeof(SDropFuncMsg); @@ -1340,7 +1340,7 @@ int32_t tscBuildDropTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pDropTableMsg->supertable = (pInfo->pMiscInfo->tableType == TSDB_SUPER_TABLE)? 1:0; pDropTableMsg->igNotExists = pInfo->pMiscInfo->existsCheck ? 1 : 0; - pCmd->msgType = TSDB_MSG_TYPE_CM_DROP_TABLE; + pCmd->msgType = TSDB_MSG_TYPE_DROP_TABLE; return TSDB_CODE_SUCCESS; } @@ -1358,7 +1358,7 @@ int32_t tscBuildDropDnodeMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SDropDnodeMsg * pDrop = (SDropDnodeMsg *)pCmd->payload; tstrncpy(pDrop->ep, dnodeEp, tListLen(pDrop->ep)); - pCmd->msgType = TSDB_MSG_TYPE_CM_DROP_DNODE; + pCmd->msgType = TSDB_MSG_TYPE_DROP_DNODE; return TSDB_CODE_SUCCESS; } @@ -1370,7 +1370,7 @@ int32_t tscBuildDropUserAcctMsg(SSqlObj *pSql, SSqlInfo *pInfo) { tstrncpy(user, pCmd->payload, TSDB_USER_LEN); pCmd->payloadLen = sizeof(SDropUserMsg); - pCmd->msgType = (pInfo->type == TSDB_SQL_DROP_USER)? TSDB_MSG_TYPE_CM_DROP_USER:TSDB_MSG_TYPE_CM_DROP_ACCT; + pCmd->msgType = (pInfo->type == TSDB_SQL_DROP_USER)? TSDB_MSG_TYPE_DROP_USER:TSDB_MSG_TYPE_DROP_ACCT; if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, pCmd->payloadLen)) { tscError("0x%"PRIx64" failed to malloc for query msg", pSql->self); @@ -1395,7 +1395,7 @@ int32_t tscBuildUseDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SUseDbMsg *pUseDbMsg = (SUseDbMsg *)pCmd->payload; STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0); tNameExtractFullName(&pTableMetaInfo->name, pUseDbMsg->db); - pCmd->msgType = TSDB_MSG_TYPE_CM_USE_DB; + pCmd->msgType = TSDB_MSG_TYPE_USE_DB; return TSDB_CODE_SUCCESS; } @@ -1412,14 +1412,14 @@ int32_t tscBuildSyncDbReplicaMsg(SSqlObj* pSql, SSqlInfo *pInfo) { SSyncDbMsg *pSyncMsg = (SSyncDbMsg *)pCmd->payload; STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, 0); tNameExtractFullName(&pTableMetaInfo->name, pSyncMsg->db); - pCmd->msgType = TSDB_MSG_TYPE_CM_SYNC_DB; + pCmd->msgType = TSDB_MSG_TYPE_SYNC_DB; return TSDB_CODE_SUCCESS; } int32_t tscBuildShowMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd *pCmd = &pSql->cmd; - pCmd->msgType = TSDB_MSG_TYPE_CM_SHOW; + pCmd->msgType = TSDB_MSG_TYPE_SHOW; pCmd->payloadLen = sizeof(SShowMsg) + 100; if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, pCmd->payloadLen)) { @@ -1473,13 +1473,13 @@ int32_t tscBuildKillMsg(SSqlObj *pSql, SSqlInfo *pInfo) { switch (pCmd->command) { case TSDB_SQL_KILL_QUERY: - pCmd->msgType = TSDB_MSG_TYPE_CM_KILL_QUERY; + pCmd->msgType = TSDB_MSG_TYPE_KILL_QUERY; break; case TSDB_SQL_KILL_CONNECTION: - pCmd->msgType = TSDB_MSG_TYPE_CM_KILL_CONN; + pCmd->msgType = TSDB_MSG_TYPE_KILL_CONN; break; case TSDB_SQL_KILL_STREAM: - pCmd->msgType = TSDB_MSG_TYPE_CM_KILL_STREAM; + pCmd->msgType = TSDB_MSG_TYPE_KILL_STREAM; break; } return TSDB_CODE_SUCCESS; @@ -1592,7 +1592,7 @@ int tscBuildCreateTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { msgLen = (int32_t)(pMsg - (char*)pCreateTableMsg); pCreateTableMsg->contLen = htonl(msgLen); pCmd->payloadLen = msgLen; - pCmd->msgType = TSDB_MSG_TYPE_CM_CREATE_TABLE; + pCmd->msgType = TSDB_MSG_TYPE_CREATE_TABLE; assert(msgLen + minMsgSize() <= size); return TSDB_CODE_SUCCESS; @@ -1645,7 +1645,7 @@ int tscBuildAlterTableMsg(SSqlObj *pSql, SSqlInfo *pInfo) { msgLen = (int32_t)(pMsg - (char*)pAlterTableMsg); pCmd->payloadLen = msgLen; - pCmd->msgType = TSDB_MSG_TYPE_CM_ALTER_TABLE; + pCmd->msgType = TSDB_MSG_TYPE_ALTER_TABLE; assert(msgLen + minMsgSize() <= size); @@ -1674,7 +1674,7 @@ int tscBuildUpdateTagMsg(SSqlObj* pSql, SSqlInfo *pInfo) { int tscAlterDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd *pCmd = &pSql->cmd; pCmd->payloadLen = sizeof(SAlterDbMsg); - pCmd->msgType = (pInfo->pMiscInfo->dbOpt.dbType == TSDB_DB_TYPE_DEFAULT) ? TSDB_MSG_TYPE_CM_ALTER_DB : TSDB_MSG_TYPE_CM_ALTER_TP; + pCmd->msgType = (pInfo->pMiscInfo->dbOpt.dbType == TSDB_DB_TYPE_DEFAULT) ? TSDB_MSG_TYPE_ALTER_DB : TSDB_MSG_TYPE_ALTER_TP; SAlterDbMsg *pAlterDbMsg = (SAlterDbMsg* )pCmd->payload; pAlterDbMsg->dbType = -1; @@ -1711,7 +1711,7 @@ int tscBuildCompactMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int count = removeDupVgid(result, size); pCmd->payloadLen = sizeof(SCompactMsg) + count * sizeof(int32_t); - pCmd->msgType = TSDB_MSG_TYPE_CM_COMPACT_VNODE; + pCmd->msgType = TSDB_MSG_TYPE_COMPACT_VNODE; if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, pCmd->payloadLen)) { tscError("0x%"PRIx64" failed to malloc for query msg", pSql->self); @@ -1741,7 +1741,7 @@ int tscBuildCompactMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int tscBuildRetrieveFromMgmtMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd *pCmd = &pSql->cmd; - pCmd->msgType = TSDB_MSG_TYPE_CM_RETRIEVE; + pCmd->msgType = TSDB_MSG_TYPE_SHOW_RETRIEVE; pCmd->payloadLen = sizeof(SRetrieveTableMsg); if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, pCmd->payloadLen)) { @@ -1861,7 +1861,7 @@ int tscProcessEmptyResultRsp(SSqlObj *pSql) { return tscLocalResultCommonBuilder int tscBuildConnectMsg(SSqlObj *pSql, SSqlInfo *pInfo) { STscObj *pObj = pSql->pTscObj; SSqlCmd *pCmd = &pSql->cmd; - pCmd->msgType = TSDB_MSG_TYPE_CM_CONNECT; + pCmd->msgType = TSDB_MSG_TYPE_CONNECT; pCmd->payloadLen = sizeof(SConnectMsg); if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, pCmd->payloadLen)) { @@ -1898,7 +1898,7 @@ int tscBuildConnectMsg(SSqlObj *pSql, SSqlInfo *pInfo) { int tscBuildMultiTableMetaMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SSqlCmd *pCmd = &pSql->cmd; - pCmd->msgType = TSDB_MSG_TYPE_CM_TABLES_META; + pCmd->msgType = TSDB_MSG_TYPE_TABLES_META; assert(pCmd->payloadLen + minMsgSize() <= pCmd->allocSize); tscDebug("0x%"PRIx64" build load multi-tablemeta msg completed, numOfTables:%d, msg size:%d", pSql->self, pCmd->count, @@ -1925,7 +1925,7 @@ int tscBuildSTableVgroupMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pMsg += TSDB_TABLE_FNAME_LEN; } - pCmd->msgType = TSDB_MSG_TYPE_CM_STABLE_VGROUP; + pCmd->msgType = TSDB_MSG_TYPE_STABLE_VGROUP; pCmd->payloadLen = (int32_t)(pMsg - pCmd->payload); return TSDB_CODE_SUCCESS; @@ -1948,7 +1948,7 @@ int tscBuildRetrieveFuncMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pMsg += varDataNetTLen(pMsg); } - pCmd->msgType = TSDB_MSG_TYPE_CM_RETRIEVE_FUNC; + pCmd->msgType = TSDB_MSG_TYPE_RETRIEVE_FUNC; pCmd->payloadLen = (int32_t)(pMsg - pCmd->payload); return TSDB_CODE_SUCCESS; @@ -1996,7 +1996,7 @@ int tscBuildHeartBeatMsg(SSqlObj *pSql, SSqlInfo *pInfo) { pthread_mutex_unlock(&pObj->mutex); pCmd->payloadLen = msgLen; - pCmd->msgType = TSDB_MSG_TYPE_CM_HEARTBEAT; + pCmd->msgType = TSDB_MSG_TYPE_HEARTBEAT; return TSDB_CODE_SUCCESS; } @@ -2838,7 +2838,7 @@ static int32_t getTableMetaFromMnode(SSqlObj *pSql, STableMetaInfo *pTableMetaIn } pNew->cmd.payloadLen = (int32_t)(pMsg - (char*)pInfoMsg); - pNew->cmd.msgType = TSDB_MSG_TYPE_CM_TABLE_META; + pNew->cmd.msgType = TSDB_MSG_TYPE_TABLE_META; } int32_t code = tscBuildAndSendRequest(pNew, NULL); @@ -2913,7 +2913,7 @@ int32_t getMultiTableMetaFromMnode(SSqlObj *pSql, SArray* pNameList, SArray* pVg } pNew->cmd.payloadLen = (int32_t) ((start - pInfo->tableNames) + sizeof(SMultiTableInfoMsg)); - pNew->cmd.msgType = TSDB_MSG_TYPE_CM_TABLES_META; + pNew->cmd.msgType = TSDB_MSG_TYPE_TABLES_META; registerSqlObj(pNew); tscDebug("0x%"PRIx64" new pSqlObj:0x%"PRIx64" to get %d tableMeta, vgroupInfo:%d, udf:%d, msg size:%d", pSql->self, diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 59c20270ec..986ce41a9c 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -5011,7 +5011,7 @@ int tscTransferTableNameList(SSqlObj *pSql, const char *pNameList, int32_t lengt SSqlCmd *pCmd = &pSql->cmd; pCmd->command = TSDB_SQL_MULTI_META; - pCmd->msgType = TSDB_MSG_TYPE_CM_TABLES_META; + pCmd->msgType = TSDB_MSG_TYPE_TABLES_META; int code = TSDB_CODE_TSC_INVALID_TABLE_ID_LENGTH; char *str = (char *)pNameList; From a3eef865beae60b3a681efc546ca7bcdf092f96d Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 1 Nov 2021 19:55:39 +0800 Subject: [PATCH 10/33] remove dnode to mgmt --- source/{server => dnode}/CMakeLists.txt | 0 source/{server/dnode => dnode/mgmt}/CMakeLists.txt | 0 source/{server/dnode => dnode/mgmt}/inc/dnodeCheck.h | 0 source/{server/dnode => dnode/mgmt}/inc/dnodeEps.h | 0 source/{server/dnode => dnode/mgmt}/inc/dnodeInt.h | 0 source/{server/dnode => dnode/mgmt}/inc/dnodeMsg.h | 0 source/{server/dnode => dnode/mgmt}/inc/dnodeTrans.h | 0 source/{server/dnode => dnode/mgmt}/src/dnodeCheck.c | 0 source/{server/dnode => dnode/mgmt}/src/dnodeEps.c | 0 source/{server/dnode => dnode/mgmt}/src/dnodeInt.c | 0 source/{server/dnode => dnode/mgmt}/src/dnodeMsg.c | 0 source/{server/dnode => dnode/mgmt}/src/dnodeTrans.c | 0 source/{server => dnode}/mnode/CMakeLists.txt | 0 source/{server => dnode}/mnode/inc/mnodeAcct.h | 0 source/{server => dnode}/mnode/inc/mnodeAuth.h | 0 source/{server => dnode}/mnode/inc/mnodeBalance.h | 0 source/{server => dnode}/mnode/inc/mnodeCluster.h | 0 source/{server => dnode}/mnode/inc/mnodeDb.h | 0 source/{server => dnode}/mnode/inc/mnodeDef.h | 0 source/{server => dnode}/mnode/inc/mnodeDnode.h | 0 source/{server => dnode}/mnode/inc/mnodeFunc.h | 0 source/{server => dnode}/mnode/inc/mnodeInt.h | 0 source/{server => dnode}/mnode/inc/mnodeMnode.h | 0 source/{server => dnode}/mnode/inc/mnodeOper.h | 0 source/{server => dnode}/mnode/inc/mnodeProfile.h | 0 source/{server => dnode}/mnode/inc/mnodeSdb.h | 0 source/{server => dnode}/mnode/inc/mnodeShow.h | 0 source/{server => dnode}/mnode/inc/mnodeStable.h | 0 source/{server => dnode}/mnode/inc/mnodeSync.h | 0 source/{server => dnode}/mnode/inc/mnodeTelem.h | 0 source/{server => dnode}/mnode/inc/mnodeUser.h | 0 source/{server => dnode}/mnode/inc/mnodeVgroup.h | 0 source/{server => dnode}/mnode/inc/mnodeWorker.h | 0 source/{server => dnode}/mnode/src/mnodeAcct.c | 0 source/{server => dnode}/mnode/src/mnodeAuth.c | 0 source/{server => dnode}/mnode/src/mnodeBalance.c | 0 source/{server => dnode}/mnode/src/mnodeCluster.c | 0 source/{server => dnode}/mnode/src/mnodeDb.c | 0 source/{server => dnode}/mnode/src/mnodeDnode.c | 0 source/{server => dnode}/mnode/src/mnodeFunc.c | 0 source/{server => dnode}/mnode/src/mnodeMnode.c | 0 source/{server => dnode}/mnode/src/mnodeOper.c | 0 source/{server => dnode}/mnode/src/mnodeProfile.c | 0 source/{server => dnode}/mnode/src/mnodeSdb.c | 0 source/{server => dnode}/mnode/src/mnodeShow.c | 0 source/{server => dnode}/mnode/src/mnodeStable.c | 0 source/{server => dnode}/mnode/src/mnodeSync.c | 0 source/{server => dnode}/mnode/src/mnodeTelem.c | 0 source/{server => dnode}/mnode/src/mnodeUser.c | 0 source/{server => dnode}/mnode/src/mnodeVgroup.c | 0 source/{server => dnode}/mnode/src/mnodeWorker.c | 0 source/{server => dnode}/mnode/src/mondeInt.c | 0 source/{server => dnode}/mnode/test/mnodeTests.cpp | 0 source/{server => dnode}/qnode/CMakeLists.txt | 0 source/{server => dnode}/qnode/inc/qnodeInt.h | 0 source/{server => dnode}/qnode/src/qnode.c | 0 source/{server => dnode}/qnode/test/qnodeTests.cpp | 0 source/{server => dnode}/server.c | 0 source/{server => dnode}/vnode/CMakeLists.txt | 0 source/{server => dnode}/vnode/impl/CMakeLists.txt | 0 source/{server => dnode}/vnode/impl/inc/vnodeCommit.h | 0 source/{server => dnode}/vnode/impl/inc/vnodeInt.h | 0 source/{server => dnode}/vnode/impl/inc/vnodeMemAllocator.h | 0 source/{server => dnode}/vnode/impl/inc/vnodeRead.h | 0 source/{server => dnode}/vnode/impl/inc/vnodeWrite.h | 0 source/{server => dnode}/vnode/impl/src/vnodeCommit.c | 0 source/{server => dnode}/vnode/impl/src/vnodeInt.c | 0 source/{server => dnode}/vnode/impl/src/vnodeMemAllocator.c | 0 source/{server => dnode}/vnode/impl/src/vnodeRead.c | 0 source/{server => dnode}/vnode/impl/src/vnodeWrite.c | 0 source/{server => dnode}/vnode/impl/test/CMakeLists.txt | 0 .../{server => dnode}/vnode/impl/test/vnodeMemAllocatorTest.cpp | 0 source/{server => dnode}/vnode/impl/test/vnodeTests.cpp | 0 source/{server => dnode}/vnode/meta/CMakeLists.txt | 0 source/{server => dnode}/vnode/meta/inc/metaDef.h | 0 source/{server => dnode}/vnode/meta/inc/metaUid.h | 0 source/{server => dnode}/vnode/meta/src/metaMain.c | 0 source/{server => dnode}/vnode/meta/src/metaUid.c | 0 source/{server => dnode}/vnode/meta/test/CMakeLists.txt | 0 source/{server => dnode}/vnode/meta/test/metaTests.cpp | 0 source/{server => dnode}/vnode/tq/CMakeLists.txt | 0 source/{server => dnode}/vnode/tq/inc/tqCommit.h | 0 source/{server => dnode}/vnode/tq/inc/tqInt.h | 0 source/{server => dnode}/vnode/tq/inc/tqMetaStore.h | 0 source/{server => dnode}/vnode/tq/src/tq.c | 0 source/{server => dnode}/vnode/tq/src/tqCommit.c | 0 source/{server => dnode}/vnode/tq/src/tqMetaStore.c | 0 source/{server => dnode}/vnode/tq/test/tqTests.cpp | 0 source/{server => dnode}/vnode/tsdb/CMakeLists.txt | 0 source/{server => dnode}/vnode/tsdb/inc/tsdbInt.h | 0 source/{server => dnode}/vnode/tsdb/inc/tsdbMemTable.h | 0 source/{server => dnode}/vnode/tsdb/inc/tsdbWriteBatch.h | 0 source/{server => dnode}/vnode/tsdb/src/tsdb.c | 0 source/{server => dnode}/vnode/tsdb/src/tsdbMemTable.c | 0 source/{server => dnode}/vnode/tsdb/src/tsdbSMA.c | 0 source/{server => dnode}/vnode/tsdb/test/tsdbTests.cpp | 0 96 files changed, 0 insertions(+), 0 deletions(-) rename source/{server => dnode}/CMakeLists.txt (100%) rename source/{server/dnode => dnode/mgmt}/CMakeLists.txt (100%) rename source/{server/dnode => dnode/mgmt}/inc/dnodeCheck.h (100%) rename source/{server/dnode => dnode/mgmt}/inc/dnodeEps.h (100%) rename source/{server/dnode => dnode/mgmt}/inc/dnodeInt.h (100%) rename source/{server/dnode => dnode/mgmt}/inc/dnodeMsg.h (100%) rename source/{server/dnode => dnode/mgmt}/inc/dnodeTrans.h (100%) rename source/{server/dnode => dnode/mgmt}/src/dnodeCheck.c (100%) rename source/{server/dnode => dnode/mgmt}/src/dnodeEps.c (100%) rename source/{server/dnode => dnode/mgmt}/src/dnodeInt.c (100%) rename source/{server/dnode => dnode/mgmt}/src/dnodeMsg.c (100%) rename source/{server/dnode => dnode/mgmt}/src/dnodeTrans.c (100%) rename source/{server => dnode}/mnode/CMakeLists.txt (100%) rename source/{server => dnode}/mnode/inc/mnodeAcct.h (100%) rename source/{server => dnode}/mnode/inc/mnodeAuth.h (100%) rename source/{server => dnode}/mnode/inc/mnodeBalance.h (100%) rename source/{server => dnode}/mnode/inc/mnodeCluster.h (100%) rename source/{server => dnode}/mnode/inc/mnodeDb.h (100%) rename source/{server => dnode}/mnode/inc/mnodeDef.h (100%) rename source/{server => dnode}/mnode/inc/mnodeDnode.h (100%) rename source/{server => dnode}/mnode/inc/mnodeFunc.h (100%) rename source/{server => dnode}/mnode/inc/mnodeInt.h (100%) rename source/{server => dnode}/mnode/inc/mnodeMnode.h (100%) rename source/{server => dnode}/mnode/inc/mnodeOper.h (100%) rename source/{server => dnode}/mnode/inc/mnodeProfile.h (100%) rename source/{server => dnode}/mnode/inc/mnodeSdb.h (100%) rename source/{server => dnode}/mnode/inc/mnodeShow.h (100%) rename source/{server => dnode}/mnode/inc/mnodeStable.h (100%) rename source/{server => dnode}/mnode/inc/mnodeSync.h (100%) rename source/{server => dnode}/mnode/inc/mnodeTelem.h (100%) rename source/{server => dnode}/mnode/inc/mnodeUser.h (100%) rename source/{server => dnode}/mnode/inc/mnodeVgroup.h (100%) rename source/{server => dnode}/mnode/inc/mnodeWorker.h (100%) rename source/{server => dnode}/mnode/src/mnodeAcct.c (100%) rename source/{server => dnode}/mnode/src/mnodeAuth.c (100%) rename source/{server => dnode}/mnode/src/mnodeBalance.c (100%) rename source/{server => dnode}/mnode/src/mnodeCluster.c (100%) rename source/{server => dnode}/mnode/src/mnodeDb.c (100%) rename source/{server => dnode}/mnode/src/mnodeDnode.c (100%) rename source/{server => dnode}/mnode/src/mnodeFunc.c (100%) rename source/{server => dnode}/mnode/src/mnodeMnode.c (100%) rename source/{server => dnode}/mnode/src/mnodeOper.c (100%) rename source/{server => dnode}/mnode/src/mnodeProfile.c (100%) rename source/{server => dnode}/mnode/src/mnodeSdb.c (100%) rename source/{server => dnode}/mnode/src/mnodeShow.c (100%) rename source/{server => dnode}/mnode/src/mnodeStable.c (100%) rename source/{server => dnode}/mnode/src/mnodeSync.c (100%) rename source/{server => dnode}/mnode/src/mnodeTelem.c (100%) rename source/{server => dnode}/mnode/src/mnodeUser.c (100%) rename source/{server => dnode}/mnode/src/mnodeVgroup.c (100%) rename source/{server => dnode}/mnode/src/mnodeWorker.c (100%) rename source/{server => dnode}/mnode/src/mondeInt.c (100%) rename source/{server => dnode}/mnode/test/mnodeTests.cpp (100%) rename source/{server => dnode}/qnode/CMakeLists.txt (100%) rename source/{server => dnode}/qnode/inc/qnodeInt.h (100%) rename source/{server => dnode}/qnode/src/qnode.c (100%) rename source/{server => dnode}/qnode/test/qnodeTests.cpp (100%) rename source/{server => dnode}/server.c (100%) rename source/{server => dnode}/vnode/CMakeLists.txt (100%) rename source/{server => dnode}/vnode/impl/CMakeLists.txt (100%) rename source/{server => dnode}/vnode/impl/inc/vnodeCommit.h (100%) rename source/{server => dnode}/vnode/impl/inc/vnodeInt.h (100%) rename source/{server => dnode}/vnode/impl/inc/vnodeMemAllocator.h (100%) rename source/{server => dnode}/vnode/impl/inc/vnodeRead.h (100%) rename source/{server => dnode}/vnode/impl/inc/vnodeWrite.h (100%) rename source/{server => dnode}/vnode/impl/src/vnodeCommit.c (100%) rename source/{server => dnode}/vnode/impl/src/vnodeInt.c (100%) rename source/{server => dnode}/vnode/impl/src/vnodeMemAllocator.c (100%) rename source/{server => dnode}/vnode/impl/src/vnodeRead.c (100%) rename source/{server => dnode}/vnode/impl/src/vnodeWrite.c (100%) rename source/{server => dnode}/vnode/impl/test/CMakeLists.txt (100%) rename source/{server => dnode}/vnode/impl/test/vnodeMemAllocatorTest.cpp (100%) rename source/{server => dnode}/vnode/impl/test/vnodeTests.cpp (100%) rename source/{server => dnode}/vnode/meta/CMakeLists.txt (100%) rename source/{server => dnode}/vnode/meta/inc/metaDef.h (100%) rename source/{server => dnode}/vnode/meta/inc/metaUid.h (100%) rename source/{server => dnode}/vnode/meta/src/metaMain.c (100%) rename source/{server => dnode}/vnode/meta/src/metaUid.c (100%) rename source/{server => dnode}/vnode/meta/test/CMakeLists.txt (100%) rename source/{server => dnode}/vnode/meta/test/metaTests.cpp (100%) rename source/{server => dnode}/vnode/tq/CMakeLists.txt (100%) rename source/{server => dnode}/vnode/tq/inc/tqCommit.h (100%) rename source/{server => dnode}/vnode/tq/inc/tqInt.h (100%) rename source/{server => dnode}/vnode/tq/inc/tqMetaStore.h (100%) rename source/{server => dnode}/vnode/tq/src/tq.c (100%) rename source/{server => dnode}/vnode/tq/src/tqCommit.c (100%) rename source/{server => dnode}/vnode/tq/src/tqMetaStore.c (100%) rename source/{server => dnode}/vnode/tq/test/tqTests.cpp (100%) rename source/{server => dnode}/vnode/tsdb/CMakeLists.txt (100%) rename source/{server => dnode}/vnode/tsdb/inc/tsdbInt.h (100%) rename source/{server => dnode}/vnode/tsdb/inc/tsdbMemTable.h (100%) rename source/{server => dnode}/vnode/tsdb/inc/tsdbWriteBatch.h (100%) rename source/{server => dnode}/vnode/tsdb/src/tsdb.c (100%) rename source/{server => dnode}/vnode/tsdb/src/tsdbMemTable.c (100%) rename source/{server => dnode}/vnode/tsdb/src/tsdbSMA.c (100%) rename source/{server => dnode}/vnode/tsdb/test/tsdbTests.cpp (100%) diff --git a/source/server/CMakeLists.txt b/source/dnode/CMakeLists.txt similarity index 100% rename from source/server/CMakeLists.txt rename to source/dnode/CMakeLists.txt diff --git a/source/server/dnode/CMakeLists.txt b/source/dnode/mgmt/CMakeLists.txt similarity index 100% rename from source/server/dnode/CMakeLists.txt rename to source/dnode/mgmt/CMakeLists.txt diff --git a/source/server/dnode/inc/dnodeCheck.h b/source/dnode/mgmt/inc/dnodeCheck.h similarity index 100% rename from source/server/dnode/inc/dnodeCheck.h rename to source/dnode/mgmt/inc/dnodeCheck.h diff --git a/source/server/dnode/inc/dnodeEps.h b/source/dnode/mgmt/inc/dnodeEps.h similarity index 100% rename from source/server/dnode/inc/dnodeEps.h rename to source/dnode/mgmt/inc/dnodeEps.h diff --git a/source/server/dnode/inc/dnodeInt.h b/source/dnode/mgmt/inc/dnodeInt.h similarity index 100% rename from source/server/dnode/inc/dnodeInt.h rename to source/dnode/mgmt/inc/dnodeInt.h diff --git a/source/server/dnode/inc/dnodeMsg.h b/source/dnode/mgmt/inc/dnodeMsg.h similarity index 100% rename from source/server/dnode/inc/dnodeMsg.h rename to source/dnode/mgmt/inc/dnodeMsg.h diff --git a/source/server/dnode/inc/dnodeTrans.h b/source/dnode/mgmt/inc/dnodeTrans.h similarity index 100% rename from source/server/dnode/inc/dnodeTrans.h rename to source/dnode/mgmt/inc/dnodeTrans.h diff --git a/source/server/dnode/src/dnodeCheck.c b/source/dnode/mgmt/src/dnodeCheck.c similarity index 100% rename from source/server/dnode/src/dnodeCheck.c rename to source/dnode/mgmt/src/dnodeCheck.c diff --git a/source/server/dnode/src/dnodeEps.c b/source/dnode/mgmt/src/dnodeEps.c similarity index 100% rename from source/server/dnode/src/dnodeEps.c rename to source/dnode/mgmt/src/dnodeEps.c diff --git a/source/server/dnode/src/dnodeInt.c b/source/dnode/mgmt/src/dnodeInt.c similarity index 100% rename from source/server/dnode/src/dnodeInt.c rename to source/dnode/mgmt/src/dnodeInt.c diff --git a/source/server/dnode/src/dnodeMsg.c b/source/dnode/mgmt/src/dnodeMsg.c similarity index 100% rename from source/server/dnode/src/dnodeMsg.c rename to source/dnode/mgmt/src/dnodeMsg.c diff --git a/source/server/dnode/src/dnodeTrans.c b/source/dnode/mgmt/src/dnodeTrans.c similarity index 100% rename from source/server/dnode/src/dnodeTrans.c rename to source/dnode/mgmt/src/dnodeTrans.c diff --git a/source/server/mnode/CMakeLists.txt b/source/dnode/mnode/CMakeLists.txt similarity index 100% rename from source/server/mnode/CMakeLists.txt rename to source/dnode/mnode/CMakeLists.txt diff --git a/source/server/mnode/inc/mnodeAcct.h b/source/dnode/mnode/inc/mnodeAcct.h similarity index 100% rename from source/server/mnode/inc/mnodeAcct.h rename to source/dnode/mnode/inc/mnodeAcct.h diff --git a/source/server/mnode/inc/mnodeAuth.h b/source/dnode/mnode/inc/mnodeAuth.h similarity index 100% rename from source/server/mnode/inc/mnodeAuth.h rename to source/dnode/mnode/inc/mnodeAuth.h diff --git a/source/server/mnode/inc/mnodeBalance.h b/source/dnode/mnode/inc/mnodeBalance.h similarity index 100% rename from source/server/mnode/inc/mnodeBalance.h rename to source/dnode/mnode/inc/mnodeBalance.h diff --git a/source/server/mnode/inc/mnodeCluster.h b/source/dnode/mnode/inc/mnodeCluster.h similarity index 100% rename from source/server/mnode/inc/mnodeCluster.h rename to source/dnode/mnode/inc/mnodeCluster.h diff --git a/source/server/mnode/inc/mnodeDb.h b/source/dnode/mnode/inc/mnodeDb.h similarity index 100% rename from source/server/mnode/inc/mnodeDb.h rename to source/dnode/mnode/inc/mnodeDb.h diff --git a/source/server/mnode/inc/mnodeDef.h b/source/dnode/mnode/inc/mnodeDef.h similarity index 100% rename from source/server/mnode/inc/mnodeDef.h rename to source/dnode/mnode/inc/mnodeDef.h diff --git a/source/server/mnode/inc/mnodeDnode.h b/source/dnode/mnode/inc/mnodeDnode.h similarity index 100% rename from source/server/mnode/inc/mnodeDnode.h rename to source/dnode/mnode/inc/mnodeDnode.h diff --git a/source/server/mnode/inc/mnodeFunc.h b/source/dnode/mnode/inc/mnodeFunc.h similarity index 100% rename from source/server/mnode/inc/mnodeFunc.h rename to source/dnode/mnode/inc/mnodeFunc.h diff --git a/source/server/mnode/inc/mnodeInt.h b/source/dnode/mnode/inc/mnodeInt.h similarity index 100% rename from source/server/mnode/inc/mnodeInt.h rename to source/dnode/mnode/inc/mnodeInt.h diff --git a/source/server/mnode/inc/mnodeMnode.h b/source/dnode/mnode/inc/mnodeMnode.h similarity index 100% rename from source/server/mnode/inc/mnodeMnode.h rename to source/dnode/mnode/inc/mnodeMnode.h diff --git a/source/server/mnode/inc/mnodeOper.h b/source/dnode/mnode/inc/mnodeOper.h similarity index 100% rename from source/server/mnode/inc/mnodeOper.h rename to source/dnode/mnode/inc/mnodeOper.h diff --git a/source/server/mnode/inc/mnodeProfile.h b/source/dnode/mnode/inc/mnodeProfile.h similarity index 100% rename from source/server/mnode/inc/mnodeProfile.h rename to source/dnode/mnode/inc/mnodeProfile.h diff --git a/source/server/mnode/inc/mnodeSdb.h b/source/dnode/mnode/inc/mnodeSdb.h similarity index 100% rename from source/server/mnode/inc/mnodeSdb.h rename to source/dnode/mnode/inc/mnodeSdb.h diff --git a/source/server/mnode/inc/mnodeShow.h b/source/dnode/mnode/inc/mnodeShow.h similarity index 100% rename from source/server/mnode/inc/mnodeShow.h rename to source/dnode/mnode/inc/mnodeShow.h diff --git a/source/server/mnode/inc/mnodeStable.h b/source/dnode/mnode/inc/mnodeStable.h similarity index 100% rename from source/server/mnode/inc/mnodeStable.h rename to source/dnode/mnode/inc/mnodeStable.h diff --git a/source/server/mnode/inc/mnodeSync.h b/source/dnode/mnode/inc/mnodeSync.h similarity index 100% rename from source/server/mnode/inc/mnodeSync.h rename to source/dnode/mnode/inc/mnodeSync.h diff --git a/source/server/mnode/inc/mnodeTelem.h b/source/dnode/mnode/inc/mnodeTelem.h similarity index 100% rename from source/server/mnode/inc/mnodeTelem.h rename to source/dnode/mnode/inc/mnodeTelem.h diff --git a/source/server/mnode/inc/mnodeUser.h b/source/dnode/mnode/inc/mnodeUser.h similarity index 100% rename from source/server/mnode/inc/mnodeUser.h rename to source/dnode/mnode/inc/mnodeUser.h diff --git a/source/server/mnode/inc/mnodeVgroup.h b/source/dnode/mnode/inc/mnodeVgroup.h similarity index 100% rename from source/server/mnode/inc/mnodeVgroup.h rename to source/dnode/mnode/inc/mnodeVgroup.h diff --git a/source/server/mnode/inc/mnodeWorker.h b/source/dnode/mnode/inc/mnodeWorker.h similarity index 100% rename from source/server/mnode/inc/mnodeWorker.h rename to source/dnode/mnode/inc/mnodeWorker.h diff --git a/source/server/mnode/src/mnodeAcct.c b/source/dnode/mnode/src/mnodeAcct.c similarity index 100% rename from source/server/mnode/src/mnodeAcct.c rename to source/dnode/mnode/src/mnodeAcct.c diff --git a/source/server/mnode/src/mnodeAuth.c b/source/dnode/mnode/src/mnodeAuth.c similarity index 100% rename from source/server/mnode/src/mnodeAuth.c rename to source/dnode/mnode/src/mnodeAuth.c diff --git a/source/server/mnode/src/mnodeBalance.c b/source/dnode/mnode/src/mnodeBalance.c similarity index 100% rename from source/server/mnode/src/mnodeBalance.c rename to source/dnode/mnode/src/mnodeBalance.c diff --git a/source/server/mnode/src/mnodeCluster.c b/source/dnode/mnode/src/mnodeCluster.c similarity index 100% rename from source/server/mnode/src/mnodeCluster.c rename to source/dnode/mnode/src/mnodeCluster.c diff --git a/source/server/mnode/src/mnodeDb.c b/source/dnode/mnode/src/mnodeDb.c similarity index 100% rename from source/server/mnode/src/mnodeDb.c rename to source/dnode/mnode/src/mnodeDb.c diff --git a/source/server/mnode/src/mnodeDnode.c b/source/dnode/mnode/src/mnodeDnode.c similarity index 100% rename from source/server/mnode/src/mnodeDnode.c rename to source/dnode/mnode/src/mnodeDnode.c diff --git a/source/server/mnode/src/mnodeFunc.c b/source/dnode/mnode/src/mnodeFunc.c similarity index 100% rename from source/server/mnode/src/mnodeFunc.c rename to source/dnode/mnode/src/mnodeFunc.c diff --git a/source/server/mnode/src/mnodeMnode.c b/source/dnode/mnode/src/mnodeMnode.c similarity index 100% rename from source/server/mnode/src/mnodeMnode.c rename to source/dnode/mnode/src/mnodeMnode.c diff --git a/source/server/mnode/src/mnodeOper.c b/source/dnode/mnode/src/mnodeOper.c similarity index 100% rename from source/server/mnode/src/mnodeOper.c rename to source/dnode/mnode/src/mnodeOper.c diff --git a/source/server/mnode/src/mnodeProfile.c b/source/dnode/mnode/src/mnodeProfile.c similarity index 100% rename from source/server/mnode/src/mnodeProfile.c rename to source/dnode/mnode/src/mnodeProfile.c diff --git a/source/server/mnode/src/mnodeSdb.c b/source/dnode/mnode/src/mnodeSdb.c similarity index 100% rename from source/server/mnode/src/mnodeSdb.c rename to source/dnode/mnode/src/mnodeSdb.c diff --git a/source/server/mnode/src/mnodeShow.c b/source/dnode/mnode/src/mnodeShow.c similarity index 100% rename from source/server/mnode/src/mnodeShow.c rename to source/dnode/mnode/src/mnodeShow.c diff --git a/source/server/mnode/src/mnodeStable.c b/source/dnode/mnode/src/mnodeStable.c similarity index 100% rename from source/server/mnode/src/mnodeStable.c rename to source/dnode/mnode/src/mnodeStable.c diff --git a/source/server/mnode/src/mnodeSync.c b/source/dnode/mnode/src/mnodeSync.c similarity index 100% rename from source/server/mnode/src/mnodeSync.c rename to source/dnode/mnode/src/mnodeSync.c diff --git a/source/server/mnode/src/mnodeTelem.c b/source/dnode/mnode/src/mnodeTelem.c similarity index 100% rename from source/server/mnode/src/mnodeTelem.c rename to source/dnode/mnode/src/mnodeTelem.c diff --git a/source/server/mnode/src/mnodeUser.c b/source/dnode/mnode/src/mnodeUser.c similarity index 100% rename from source/server/mnode/src/mnodeUser.c rename to source/dnode/mnode/src/mnodeUser.c diff --git a/source/server/mnode/src/mnodeVgroup.c b/source/dnode/mnode/src/mnodeVgroup.c similarity index 100% rename from source/server/mnode/src/mnodeVgroup.c rename to source/dnode/mnode/src/mnodeVgroup.c diff --git a/source/server/mnode/src/mnodeWorker.c b/source/dnode/mnode/src/mnodeWorker.c similarity index 100% rename from source/server/mnode/src/mnodeWorker.c rename to source/dnode/mnode/src/mnodeWorker.c diff --git a/source/server/mnode/src/mondeInt.c b/source/dnode/mnode/src/mondeInt.c similarity index 100% rename from source/server/mnode/src/mondeInt.c rename to source/dnode/mnode/src/mondeInt.c diff --git a/source/server/mnode/test/mnodeTests.cpp b/source/dnode/mnode/test/mnodeTests.cpp similarity index 100% rename from source/server/mnode/test/mnodeTests.cpp rename to source/dnode/mnode/test/mnodeTests.cpp diff --git a/source/server/qnode/CMakeLists.txt b/source/dnode/qnode/CMakeLists.txt similarity index 100% rename from source/server/qnode/CMakeLists.txt rename to source/dnode/qnode/CMakeLists.txt diff --git a/source/server/qnode/inc/qnodeInt.h b/source/dnode/qnode/inc/qnodeInt.h similarity index 100% rename from source/server/qnode/inc/qnodeInt.h rename to source/dnode/qnode/inc/qnodeInt.h diff --git a/source/server/qnode/src/qnode.c b/source/dnode/qnode/src/qnode.c similarity index 100% rename from source/server/qnode/src/qnode.c rename to source/dnode/qnode/src/qnode.c diff --git a/source/server/qnode/test/qnodeTests.cpp b/source/dnode/qnode/test/qnodeTests.cpp similarity index 100% rename from source/server/qnode/test/qnodeTests.cpp rename to source/dnode/qnode/test/qnodeTests.cpp diff --git a/source/server/server.c b/source/dnode/server.c similarity index 100% rename from source/server/server.c rename to source/dnode/server.c diff --git a/source/server/vnode/CMakeLists.txt b/source/dnode/vnode/CMakeLists.txt similarity index 100% rename from source/server/vnode/CMakeLists.txt rename to source/dnode/vnode/CMakeLists.txt diff --git a/source/server/vnode/impl/CMakeLists.txt b/source/dnode/vnode/impl/CMakeLists.txt similarity index 100% rename from source/server/vnode/impl/CMakeLists.txt rename to source/dnode/vnode/impl/CMakeLists.txt diff --git a/source/server/vnode/impl/inc/vnodeCommit.h b/source/dnode/vnode/impl/inc/vnodeCommit.h similarity index 100% rename from source/server/vnode/impl/inc/vnodeCommit.h rename to source/dnode/vnode/impl/inc/vnodeCommit.h diff --git a/source/server/vnode/impl/inc/vnodeInt.h b/source/dnode/vnode/impl/inc/vnodeInt.h similarity index 100% rename from source/server/vnode/impl/inc/vnodeInt.h rename to source/dnode/vnode/impl/inc/vnodeInt.h diff --git a/source/server/vnode/impl/inc/vnodeMemAllocator.h b/source/dnode/vnode/impl/inc/vnodeMemAllocator.h similarity index 100% rename from source/server/vnode/impl/inc/vnodeMemAllocator.h rename to source/dnode/vnode/impl/inc/vnodeMemAllocator.h diff --git a/source/server/vnode/impl/inc/vnodeRead.h b/source/dnode/vnode/impl/inc/vnodeRead.h similarity index 100% rename from source/server/vnode/impl/inc/vnodeRead.h rename to source/dnode/vnode/impl/inc/vnodeRead.h diff --git a/source/server/vnode/impl/inc/vnodeWrite.h b/source/dnode/vnode/impl/inc/vnodeWrite.h similarity index 100% rename from source/server/vnode/impl/inc/vnodeWrite.h rename to source/dnode/vnode/impl/inc/vnodeWrite.h diff --git a/source/server/vnode/impl/src/vnodeCommit.c b/source/dnode/vnode/impl/src/vnodeCommit.c similarity index 100% rename from source/server/vnode/impl/src/vnodeCommit.c rename to source/dnode/vnode/impl/src/vnodeCommit.c diff --git a/source/server/vnode/impl/src/vnodeInt.c b/source/dnode/vnode/impl/src/vnodeInt.c similarity index 100% rename from source/server/vnode/impl/src/vnodeInt.c rename to source/dnode/vnode/impl/src/vnodeInt.c diff --git a/source/server/vnode/impl/src/vnodeMemAllocator.c b/source/dnode/vnode/impl/src/vnodeMemAllocator.c similarity index 100% rename from source/server/vnode/impl/src/vnodeMemAllocator.c rename to source/dnode/vnode/impl/src/vnodeMemAllocator.c diff --git a/source/server/vnode/impl/src/vnodeRead.c b/source/dnode/vnode/impl/src/vnodeRead.c similarity index 100% rename from source/server/vnode/impl/src/vnodeRead.c rename to source/dnode/vnode/impl/src/vnodeRead.c diff --git a/source/server/vnode/impl/src/vnodeWrite.c b/source/dnode/vnode/impl/src/vnodeWrite.c similarity index 100% rename from source/server/vnode/impl/src/vnodeWrite.c rename to source/dnode/vnode/impl/src/vnodeWrite.c diff --git a/source/server/vnode/impl/test/CMakeLists.txt b/source/dnode/vnode/impl/test/CMakeLists.txt similarity index 100% rename from source/server/vnode/impl/test/CMakeLists.txt rename to source/dnode/vnode/impl/test/CMakeLists.txt diff --git a/source/server/vnode/impl/test/vnodeMemAllocatorTest.cpp b/source/dnode/vnode/impl/test/vnodeMemAllocatorTest.cpp similarity index 100% rename from source/server/vnode/impl/test/vnodeMemAllocatorTest.cpp rename to source/dnode/vnode/impl/test/vnodeMemAllocatorTest.cpp diff --git a/source/server/vnode/impl/test/vnodeTests.cpp b/source/dnode/vnode/impl/test/vnodeTests.cpp similarity index 100% rename from source/server/vnode/impl/test/vnodeTests.cpp rename to source/dnode/vnode/impl/test/vnodeTests.cpp diff --git a/source/server/vnode/meta/CMakeLists.txt b/source/dnode/vnode/meta/CMakeLists.txt similarity index 100% rename from source/server/vnode/meta/CMakeLists.txt rename to source/dnode/vnode/meta/CMakeLists.txt diff --git a/source/server/vnode/meta/inc/metaDef.h b/source/dnode/vnode/meta/inc/metaDef.h similarity index 100% rename from source/server/vnode/meta/inc/metaDef.h rename to source/dnode/vnode/meta/inc/metaDef.h diff --git a/source/server/vnode/meta/inc/metaUid.h b/source/dnode/vnode/meta/inc/metaUid.h similarity index 100% rename from source/server/vnode/meta/inc/metaUid.h rename to source/dnode/vnode/meta/inc/metaUid.h diff --git a/source/server/vnode/meta/src/metaMain.c b/source/dnode/vnode/meta/src/metaMain.c similarity index 100% rename from source/server/vnode/meta/src/metaMain.c rename to source/dnode/vnode/meta/src/metaMain.c diff --git a/source/server/vnode/meta/src/metaUid.c b/source/dnode/vnode/meta/src/metaUid.c similarity index 100% rename from source/server/vnode/meta/src/metaUid.c rename to source/dnode/vnode/meta/src/metaUid.c diff --git a/source/server/vnode/meta/test/CMakeLists.txt b/source/dnode/vnode/meta/test/CMakeLists.txt similarity index 100% rename from source/server/vnode/meta/test/CMakeLists.txt rename to source/dnode/vnode/meta/test/CMakeLists.txt diff --git a/source/server/vnode/meta/test/metaTests.cpp b/source/dnode/vnode/meta/test/metaTests.cpp similarity index 100% rename from source/server/vnode/meta/test/metaTests.cpp rename to source/dnode/vnode/meta/test/metaTests.cpp diff --git a/source/server/vnode/tq/CMakeLists.txt b/source/dnode/vnode/tq/CMakeLists.txt similarity index 100% rename from source/server/vnode/tq/CMakeLists.txt rename to source/dnode/vnode/tq/CMakeLists.txt diff --git a/source/server/vnode/tq/inc/tqCommit.h b/source/dnode/vnode/tq/inc/tqCommit.h similarity index 100% rename from source/server/vnode/tq/inc/tqCommit.h rename to source/dnode/vnode/tq/inc/tqCommit.h diff --git a/source/server/vnode/tq/inc/tqInt.h b/source/dnode/vnode/tq/inc/tqInt.h similarity index 100% rename from source/server/vnode/tq/inc/tqInt.h rename to source/dnode/vnode/tq/inc/tqInt.h diff --git a/source/server/vnode/tq/inc/tqMetaStore.h b/source/dnode/vnode/tq/inc/tqMetaStore.h similarity index 100% rename from source/server/vnode/tq/inc/tqMetaStore.h rename to source/dnode/vnode/tq/inc/tqMetaStore.h diff --git a/source/server/vnode/tq/src/tq.c b/source/dnode/vnode/tq/src/tq.c similarity index 100% rename from source/server/vnode/tq/src/tq.c rename to source/dnode/vnode/tq/src/tq.c diff --git a/source/server/vnode/tq/src/tqCommit.c b/source/dnode/vnode/tq/src/tqCommit.c similarity index 100% rename from source/server/vnode/tq/src/tqCommit.c rename to source/dnode/vnode/tq/src/tqCommit.c diff --git a/source/server/vnode/tq/src/tqMetaStore.c b/source/dnode/vnode/tq/src/tqMetaStore.c similarity index 100% rename from source/server/vnode/tq/src/tqMetaStore.c rename to source/dnode/vnode/tq/src/tqMetaStore.c diff --git a/source/server/vnode/tq/test/tqTests.cpp b/source/dnode/vnode/tq/test/tqTests.cpp similarity index 100% rename from source/server/vnode/tq/test/tqTests.cpp rename to source/dnode/vnode/tq/test/tqTests.cpp diff --git a/source/server/vnode/tsdb/CMakeLists.txt b/source/dnode/vnode/tsdb/CMakeLists.txt similarity index 100% rename from source/server/vnode/tsdb/CMakeLists.txt rename to source/dnode/vnode/tsdb/CMakeLists.txt diff --git a/source/server/vnode/tsdb/inc/tsdbInt.h b/source/dnode/vnode/tsdb/inc/tsdbInt.h similarity index 100% rename from source/server/vnode/tsdb/inc/tsdbInt.h rename to source/dnode/vnode/tsdb/inc/tsdbInt.h diff --git a/source/server/vnode/tsdb/inc/tsdbMemTable.h b/source/dnode/vnode/tsdb/inc/tsdbMemTable.h similarity index 100% rename from source/server/vnode/tsdb/inc/tsdbMemTable.h rename to source/dnode/vnode/tsdb/inc/tsdbMemTable.h diff --git a/source/server/vnode/tsdb/inc/tsdbWriteBatch.h b/source/dnode/vnode/tsdb/inc/tsdbWriteBatch.h similarity index 100% rename from source/server/vnode/tsdb/inc/tsdbWriteBatch.h rename to source/dnode/vnode/tsdb/inc/tsdbWriteBatch.h diff --git a/source/server/vnode/tsdb/src/tsdb.c b/source/dnode/vnode/tsdb/src/tsdb.c similarity index 100% rename from source/server/vnode/tsdb/src/tsdb.c rename to source/dnode/vnode/tsdb/src/tsdb.c diff --git a/source/server/vnode/tsdb/src/tsdbMemTable.c b/source/dnode/vnode/tsdb/src/tsdbMemTable.c similarity index 100% rename from source/server/vnode/tsdb/src/tsdbMemTable.c rename to source/dnode/vnode/tsdb/src/tsdbMemTable.c diff --git a/source/server/vnode/tsdb/src/tsdbSMA.c b/source/dnode/vnode/tsdb/src/tsdbSMA.c similarity index 100% rename from source/server/vnode/tsdb/src/tsdbSMA.c rename to source/dnode/vnode/tsdb/src/tsdbSMA.c diff --git a/source/server/vnode/tsdb/test/tsdbTests.cpp b/source/dnode/vnode/tsdb/test/tsdbTests.cpp similarity index 100% rename from source/server/vnode/tsdb/test/tsdbTests.cpp rename to source/dnode/vnode/tsdb/test/tsdbTests.cpp From d9fc5d8c3f227dcda49ec753389785277f9d8162 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 1 Nov 2021 19:59:09 +0800 Subject: [PATCH 11/33] rename dnode to mgmt --- source/CMakeLists.txt | 2 +- source/dnode/CMakeLists.txt | 10 +--------- source/dnode/mgmt/CMakeLists.txt | 8 ++++---- source/dnode/{server.c => mgmt/src/dnodeMain.c} | 0 4 files changed, 6 insertions(+), 14 deletions(-) rename source/dnode/{server.c => mgmt/src/dnodeMain.c} (100%) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 11193d7185..2833b329a7 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -3,4 +3,4 @@ add_subdirectory(util) add_subdirectory(common) add_subdirectory(libs) add_subdirectory(client) -add_subdirectory(server) \ No newline at end of file +add_subdirectory(dnode) \ No newline at end of file diff --git a/source/dnode/CMakeLists.txt b/source/dnode/CMakeLists.txt index 8afa650d36..d719a2d106 100644 --- a/source/dnode/CMakeLists.txt +++ b/source/dnode/CMakeLists.txt @@ -1,12 +1,4 @@ add_subdirectory(mnode) add_subdirectory(vnode) add_subdirectory(qnode) -add_subdirectory(dnode) - -aux_source_directory(. TAOSD_SRC) -add_executable(taosd ${TAOSD_SRC}) -target_link_libraries( - taosd - PUBLIC dnode - PUBLIC util -) \ No newline at end of file +add_subdirectory(mgmt) diff --git a/source/dnode/mgmt/CMakeLists.txt b/source/dnode/mgmt/CMakeLists.txt index e1462ab3c8..74ee422bb0 100644 --- a/source/dnode/mgmt/CMakeLists.txt +++ b/source/dnode/mgmt/CMakeLists.txt @@ -1,7 +1,7 @@ aux_source_directory(src DNODE_SRC) -add_library(dnode ${DNODE_SRC}) +add_executable(taosd ${DNODE_SRC}) target_link_libraries( - dnode + taosd PUBLIC cjson PUBLIC mnode PUBLIC vnode @@ -10,7 +10,7 @@ target_link_libraries( PUBLIC taos ) target_include_directories( - dnode + taosd PUBLIC "${CMAKE_SOURCE_DIR}/include/server/dnode" private "${CMAKE_CURRENT_SOURCE_DIR}/inc" -) \ No newline at end of file +) diff --git a/source/dnode/server.c b/source/dnode/mgmt/src/dnodeMain.c similarity index 100% rename from source/dnode/server.c rename to source/dnode/mgmt/src/dnodeMain.c From f528d5a13d0ae4ef04fcb54c6eafb802c9544df6 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 1 Nov 2021 20:02:30 +0800 Subject: [PATCH 12/33] remove dnode.h --- include/server/dnode/dnode.h | 79 ------------------------------- source/dnode/mgmt/inc/dnodeInt.h | 4 +- source/dnode/mgmt/src/dnodeMain.c | 2 +- 3 files changed, 4 insertions(+), 81 deletions(-) delete mode 100644 include/server/dnode/dnode.h diff --git a/include/server/dnode/dnode.h b/include/server/dnode/dnode.h deleted file mode 100644 index bc0d1e89b0..0000000000 --- a/include/server/dnode/dnode.h +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_DNODE_H_ -#define _TD_DNODE_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -struct SRpcEpSet; -struct SRpcMsg; -/** - * Initialize and start the dnode module. - * - * @return Error code. - */ -int32_t dnodeInit(); - -/** - * Stop and cleanup dnode module. - */ -void dnodeCleanup(); - -/** - * Send messages to other dnodes, such as create vnode message. - * - * @param epSet, the endpoint list of the dnodes. - * @param rpcMsg, message to be sent. - */ -void dnodeSendMsgToDnode(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg); - -/** - * Send messages to mnode, such as config message. - * - * @param rpcMsg, message to be sent. - */ -void dnodeSendMsgToMnode(struct SRpcMsg *rpcMsg); - -/** - * Send redirect message to dnode or shell. - * - * @param rpcMsg, message to be sent. - * @param forShell, used to identify whether to send to shell or dnode. - */ -void dnodeSendRedirectMsg(struct SRpcMsg *rpcMsg, bool forShell); - -/** - * Get the corresponding endpoint information from dnodeId. - * - * @param dnodeId, the id ot dnode. - * @param ep, the endpoint of dnode. - * @param fqdn, the fqdn of dnode. - * @param port, the port of dnode. - */ -void dnodeGetEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); - -/** - * Report the startup progress. - */ -void dnodeReportStartup(char *name, char *desc); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_DNODE_H_*/ diff --git a/source/dnode/mgmt/inc/dnodeInt.h b/source/dnode/mgmt/inc/dnodeInt.h index 85f474a391..20257140d6 100644 --- a/source/dnode/mgmt/inc/dnodeInt.h +++ b/source/dnode/mgmt/inc/dnodeInt.h @@ -25,7 +25,6 @@ extern "C" { #include "tlog.h" #include "trpc.h" #include "ttimer.h" -#include "dnode.h" extern int32_t dDebugFlag; @@ -38,6 +37,9 @@ extern int32_t dDebugFlag; typedef enum { DN_RUN_STAT_INIT, DN_RUN_STAT_RUNNING, DN_RUN_STAT_STOPPED } EDnStat; +int32_t dnodeInit(); +void dnodeCleanup(); + EDnStat dnodeGetRunStat(); void dnodeSetRunStat(); void dnodeGetStartup(SStartupStep *); diff --git a/source/dnode/mgmt/src/dnodeMain.c b/source/dnode/mgmt/src/dnodeMain.c index 67e8e7bc58..c41acc6853 100644 --- a/source/dnode/mgmt/src/dnodeMain.c +++ b/source/dnode/mgmt/src/dnodeMain.c @@ -14,7 +14,7 @@ */ #include "os.h" #include "ulog.h" -#include "dnode.h" +#include "dnodeInt.h" static bool stop = false; static void sigintHandler(int32_t signum, void *info, void *ctx) { stop = true; } From 401cf95662b7d9c56de0f773cd50abeffae68c8f Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 1 Nov 2021 20:08:54 +0800 Subject: [PATCH 13/33] refact file directory --- .../mgmt/inc/{dnodeEps.h => dnodeConfig.h} | 6 +- source/dnode/mgmt/inc/dnodeDnode.h | 34 ++ source/dnode/mgmt/inc/dnodeMnode.h | 30 ++ .../inc/{dnodeTrans.h => dnodeTransport.h} | 6 +- .../mgmt/inc/{dnodeMsg.h => dnodeVnodes.h} | 6 +- .../mgmt/src/{dnodeEps.c => dnodeConfig.c} | 2 +- source/dnode/mgmt/src/dnodeDnode.c | 174 ++++++++ source/dnode/mgmt/src/dnodeInt.c | 6 +- source/dnode/mgmt/src/dnodeTransport.c | 382 ++++++++++++++++++ source/dnode/mgmt/src/dnodeVnodes.c | 0 source/server/dnode/src/dnodeInt.c | 204 ++++++++++ .../mgmt => server/dnode}/src/dnodeMsg.c | 4 +- .../mgmt => server/dnode}/src/dnodeTrans.c | 6 +- 13 files changed, 842 insertions(+), 18 deletions(-) rename source/dnode/mgmt/inc/{dnodeEps.h => dnodeConfig.h} (93%) create mode 100644 source/dnode/mgmt/inc/dnodeDnode.h create mode 100644 source/dnode/mgmt/inc/dnodeMnode.h rename source/dnode/mgmt/inc/{dnodeTrans.h => dnodeTransport.h} (90%) rename source/dnode/mgmt/inc/{dnodeMsg.h => dnodeVnodes.h} (91%) rename source/dnode/mgmt/src/{dnodeEps.c => dnodeConfig.c} (99%) create mode 100644 source/dnode/mgmt/src/dnodeDnode.c create mode 100644 source/dnode/mgmt/src/dnodeTransport.c create mode 100644 source/dnode/mgmt/src/dnodeVnodes.c create mode 100644 source/server/dnode/src/dnodeInt.c rename source/{dnode/mgmt => server/dnode}/src/dnodeMsg.c (99%) rename source/{dnode/mgmt => server/dnode}/src/dnodeTrans.c (99%) diff --git a/source/dnode/mgmt/inc/dnodeEps.h b/source/dnode/mgmt/inc/dnodeConfig.h similarity index 93% rename from source/dnode/mgmt/inc/dnodeEps.h rename to source/dnode/mgmt/inc/dnodeConfig.h index ac68d16374..9d446a9f1a 100644 --- a/source/dnode/mgmt/inc/dnodeEps.h +++ b/source/dnode/mgmt/inc/dnodeConfig.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_DNODE_EPS_H_ -#define _TD_DNODE_EPS_H_ +#ifndef _TD_DNODE_CONFIG_H_ +#define _TD_DNODE_CONFIG_H_ #ifdef __cplusplus extern "C" { @@ -39,4 +39,4 @@ void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell); } #endif -#endif /*_TD_DNODE_EPS_H_*/ \ No newline at end of file +#endif /*_TD_DNODE_CONFIG_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/inc/dnodeDnode.h b/source/dnode/mgmt/inc/dnodeDnode.h new file mode 100644 index 0000000000..35f68f0306 --- /dev/null +++ b/source/dnode/mgmt/inc/dnodeDnode.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef _TD_DNODE_DNODE_H_ +#define _TD_DNODE_DNODE_H_ + +#ifdef __cplusplus +extern "C" { +#endif +#include "dnodeInt.h" + +int32_t dnodeInitMsg(); +void dnodeCleanupMsg(); +void dnodeProcessStatusRsp(SRpcMsg *pMsg); +void dnodeProcessStartupReq(SRpcMsg *pMsg); +void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_DNODE_DNODE_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/inc/dnodeMnode.h b/source/dnode/mgmt/inc/dnodeMnode.h new file mode 100644 index 0000000000..0dccb8c39d --- /dev/null +++ b/source/dnode/mgmt/inc/dnodeMnode.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef _TD_DNODE_MNODE_H_ +#define _TD_DNODE_MNODE_H_ + +#ifdef __cplusplus +extern "C" { +#endif +#include "dnodeInt.h" + +void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_DNODE_MNODE_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/inc/dnodeTrans.h b/source/dnode/mgmt/inc/dnodeTransport.h similarity index 90% rename from source/dnode/mgmt/inc/dnodeTrans.h rename to source/dnode/mgmt/inc/dnodeTransport.h index f2dc647de3..e8223f4c06 100644 --- a/source/dnode/mgmt/inc/dnodeTrans.h +++ b/source/dnode/mgmt/inc/dnodeTransport.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_DNODE_TRANS_H_ -#define _TD_DNODE_TRANS_H_ +#ifndef _TD_DNODE_TRANSPORT_H_ +#define _TD_DNODE_TRANSPORT_H_ #ifdef __cplusplus extern "C" { @@ -30,4 +30,4 @@ void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg); } #endif -#endif /*_TD_DNODE_TRANS_H_*/ +#endif /*_TD_DNODE_TRANSPORT_H_*/ diff --git a/source/dnode/mgmt/inc/dnodeMsg.h b/source/dnode/mgmt/inc/dnodeVnodes.h similarity index 91% rename from source/dnode/mgmt/inc/dnodeMsg.h rename to source/dnode/mgmt/inc/dnodeVnodes.h index 0790fa7e3e..d54fda654a 100644 --- a/source/dnode/mgmt/inc/dnodeMsg.h +++ b/source/dnode/mgmt/inc/dnodeVnodes.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_DNODE_STATUS_H_ -#define _TD_DNODE_STATUS_H_ +#ifndef _TD_DNODE_VNODES_H_ +#define _TD_DNODE_VNODES_H_ #ifdef __cplusplus extern "C" { @@ -32,4 +32,4 @@ void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg); } #endif -#endif /*_TD_DNODE_STATUS_H_*/ \ No newline at end of file +#endif /*_TD_DNODE_VNODES_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/src/dnodeEps.c b/source/dnode/mgmt/src/dnodeConfig.c similarity index 99% rename from source/dnode/mgmt/src/dnodeEps.c rename to source/dnode/mgmt/src/dnodeConfig.c index 5b843df2f2..fc4c821bb6 100644 --- a/source/dnode/mgmt/src/dnodeEps.c +++ b/source/dnode/mgmt/src/dnodeConfig.c @@ -14,7 +14,7 @@ */ #define _DEFAULT_SOURCE -#include "dnodeEps.h" +#include "dnodeConfig.h" #include "cJSON.h" #include "thash.h" diff --git a/source/dnode/mgmt/src/dnodeDnode.c b/source/dnode/mgmt/src/dnodeDnode.c new file mode 100644 index 0000000000..0a598527b4 --- /dev/null +++ b/source/dnode/mgmt/src/dnodeDnode.c @@ -0,0 +1,174 @@ +/* + * 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 "dnodeDnode.h" +#include "dnodeConfig.h" +#include "mnode.h" +#include "tthread.h" +#include "ttime.h" +#include "vnode.h" + +static struct { + pthread_t *threadId; + bool stop; + uint32_t rebootTime; +} tsMsg; + +static void dnodeSendStatusMsg() { + int32_t contLen = sizeof(SStatusMsg) + TSDB_MAX_VNODES * sizeof(SVnodeLoad); + SStatusMsg *pStatus = rpcMallocCont(contLen); + if (pStatus == NULL) { + dError("failed to malloc status message"); + return; + } + + pStatus->version = htonl(tsVersion); + pStatus->dnodeId = htonl(dnodeGetDnodeId()); + tstrncpy(pStatus->dnodeEp, tsLocalEp, TSDB_EP_LEN); + pStatus->clusterId = htobe64(dnodeGetClusterId()); + pStatus->lastReboot = htonl(tsMsg.rebootTime); + pStatus->numOfCores = htonl(tsNumOfCores); + pStatus->diskAvailable = tsAvailDataDirGB; + + // fill cluster cfg parameters + pStatus->clusterCfg.statusInterval = htonl(tsStatusInterval); + pStatus->clusterCfg.checkTime = 0; + tstrncpy(pStatus->clusterCfg.timezone, tsTimezone, 64); + char timestr[32] = "1970-01-01 00:00:00.00"; + (void)taosParseTime(timestr, &pStatus->clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0); + tstrncpy(pStatus->clusterCfg.locale, tsLocale, TSDB_LOCALE_LEN); + tstrncpy(pStatus->clusterCfg.charset, tsCharset, TSDB_LOCALE_LEN); + + // vnodeGetStatus(NULL, pStatus); + // contLen = sizeof(SStatusMsg) + pStatus->openVnodes * sizeof(SVnodeLoad); + // pStatus->openVnodes = htons(pStatus->openVnodes); + + SRpcMsg rpcMsg = {.ahandle = NULL, .pCont = pStatus, .contLen = contLen, .msgType = TSDB_MSG_TYPE_DM_STATUS}; + + dnodeSendMsgToMnode(&rpcMsg); +} + +void dnodeProcessStatusRsp(SRpcMsg *pMsg) { + dTrace("status rsp is received, code:%s", tstrerror(pMsg->code)); + if (pMsg->code != TSDB_CODE_SUCCESS) return; + + SStatusRsp *pStatusRsp = pMsg->pCont; + + SDnodeCfg *pCfg = &pStatusRsp->dnodeCfg; + pCfg->dnodeId = htonl(pCfg->dnodeId); + pCfg->clusterId = htobe64(pCfg->clusterId); + pCfg->numOfVnodes = htonl(pCfg->numOfVnodes); + pCfg->numOfDnodes = htonl(pCfg->numOfDnodes); + dnodeUpdateCfg(pCfg); + + if (pCfg->dropped) { + dError("status rsp is received, and set dnode to drop status"); + return; + } + + // vnodeSetAccess(pStatusRsp->vgAccess, pCfg->numOfVnodes); + + SDnodeEps *eps = (SDnodeEps *)((char *)pStatusRsp->vgAccess + pCfg->numOfVnodes * sizeof(SVgroupAccess)); + eps->dnodeNum = htonl(eps->dnodeNum); + for (int32_t i = 0; i < eps->dnodeNum; ++i) { + eps->dnodeEps[i].dnodeId = htonl(eps->dnodeEps[i].dnodeId); + eps->dnodeEps[i].dnodePort = htons(eps->dnodeEps[i].dnodePort); + } + + dnodeUpdateDnodeEps(eps); +} + +static void *dnodeThreadRoutine(void *param) { + int32_t ms = tsStatusInterval * 1000; + while (!tsMsg.stop) { + taosMsleep(ms); + dnodeSendStatusMsg(); + } +} + +int32_t dnodeInitMsg() { + tsMsg.stop = false; + tsMsg.rebootTime = taosGetTimestampSec(); + tsMsg.threadId = taosCreateThread(dnodeThreadRoutine, NULL); + if (tsMsg.threadId == NULL) { + return -1; + } + + dInfo("dnode msg is initialized"); + return 0; +} + +void dnodeCleanupMsg() { + if (tsMsg.threadId != NULL) { + tsMsg.stop = true; + taosDestoryThread(tsMsg.threadId); + tsMsg.threadId = NULL; + } + + dInfo("dnode msg is cleanuped"); +} + +static int32_t dnodeStartMnode(SRpcMsg *pMsg) { + SCreateMnodeMsg *pCfg = pMsg->pCont; + pCfg->dnodeId = htonl(pCfg->dnodeId); + pCfg->mnodeNum = htonl(pCfg->mnodeNum); + for (int32_t i = 0; i < pCfg->mnodeNum; ++i) { + pCfg->mnodeEps[i].dnodeId = htonl(pCfg->mnodeEps[i].dnodeId); + pCfg->mnodeEps[i].dnodePort = htons(pCfg->mnodeEps[i].dnodePort); + } + + if (pCfg->dnodeId != dnodeGetDnodeId()) { + dDebug("dnode:%d, in create meps msg is not equal with saved dnodeId:%d", pCfg->dnodeId, dnodeGetDnodeId()); + return TSDB_CODE_MND_DNODE_ID_NOT_CONFIGURED; + } + + if (mnodeGetStatus() == MN_STATUS_READY) return 0; + + return mnodeDeploy(); +} + +void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg) { + int32_t code = dnodeStartMnode(pMsg); + + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; + + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); +} + +void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg) { + SCfgDnodeMsg *pCfg = pMsg->pCont; + + int32_t code = taosCfgDynamicOptions(pCfg->config); + + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; + + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); +} + +void dnodeProcessStartupReq(SRpcMsg *pMsg) { + dInfo("startup msg is received, cont:%s", (char *)pMsg->pCont); + + SStartupStep *pStep = rpcMallocCont(sizeof(SStartupStep)); + dnodeGetStartup(pStep); + + dDebug("startup msg is sent, step:%s desc:%s finished:%d", pStep->name, pStep->desc, pStep->finished); + + SRpcMsg rpcRsp = {.handle = pMsg->handle, .pCont = pStep, .contLen = sizeof(SStartupStep)}; + rpcSendResponse(&rpcRsp); + rpcFreeCont(pMsg->pCont); +} diff --git a/source/dnode/mgmt/src/dnodeInt.c b/source/dnode/mgmt/src/dnodeInt.c index 1166a06d38..aa66ab3da3 100644 --- a/source/dnode/mgmt/src/dnodeInt.c +++ b/source/dnode/mgmt/src/dnodeInt.c @@ -15,9 +15,9 @@ #define _DEFAULT_SOURCE #include "dnodeCheck.h" -#include "dnodeEps.h" -#include "dnodeMsg.h" -#include "dnodeTrans.h" +#include "dnodeConfig.h" +#include "dnodeDnode.h" +#include "dnodeTransport.h" #include "mnode.h" #include "sync.h" #include "tcache.h" diff --git a/source/dnode/mgmt/src/dnodeTransport.c b/source/dnode/mgmt/src/dnodeTransport.c new file mode 100644 index 0000000000..68ec0a44e5 --- /dev/null +++ b/source/dnode/mgmt/src/dnodeTransport.c @@ -0,0 +1,382 @@ +/* + * 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 . + */ + +/* this file is mainly responsible for the communication between DNODEs. Each + * dnode works as both server and client. Dnode may send status, grant, config + * messages to mnode, mnode may send create/alter/drop table/vnode messages + * to dnode. All theses messages are handled from here + */ + +#define _DEFAULT_SOURCE +#include "dnodeTransport.h" +#include "dnodeConfig.h" +#include "dnodeDnode.h" +#include "dnodeMnode.h" +#include "dnodeVnodes.h" +#include "mnode.h" +#include "vnode.h" + +typedef void (*MsgFp)(SRpcMsg *pMsg); + +static struct { + void *serverRpc; + void *clientRpc; + void *shellRpc; + MsgFp msgFp[TSDB_MSG_TYPE_MAX]; +} tsTrans; + +static void dnodeInitMsgFp() { + // msg from client to dnode + tsTrans.msgFp[TSDB_MSG_TYPE_SUBMIT] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_QUERY] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TABLE] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TABLE] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TABLE] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_TABLE_META] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_TABLES_META] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_QUERY] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONNECT] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_ACK] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_RESET] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_NETWORK_TEST] = dnodeProcessStartupReq; + + // msg from client to mnode + tsTrans.msgFp[TSDB_MSG_TYPE_CONNECT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_ACCT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_ACCT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_ACCT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_USER] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_USER] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_USER] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_DNODE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_DNODE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_DB] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_DB] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_USE_DB] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_DB] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_DB] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TOPIC] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TOPIC] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TOPIC] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_FUNCTION] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_FUNCTION] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_FUNCTION] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_KILL_QUERY] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_KILL_CONN] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_HEARTBEAT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SHOW] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE_FUNC] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE] = mnodeProcessMsg; + + // message from mnode to dnode + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN] = vnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN] = dnodeProcessCreateMnodeReq; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN] = NULL; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE_IN] = NULL; + tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE_IN_RSP] = mnodeProcessMsg; + + // message from dnode to mnode + tsTrans.msgFp[TSDB_MSG_TYPE_DM_AUTH] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DM_AUTH_RSP] = NULL; + tsTrans.msgFp[TSDB_MSG_TYPE_DM_GRANT] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DM_GRANT_RSP] = NULL; + tsTrans.msgFp[TSDB_MSG_TYPE_DM_STATUS] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DM_STATUS_RSP] = dnodeProcessStatusRsp; +} + +static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0}; + int32_t msgType = pMsg->msgType; + + if (msgType == TSDB_MSG_TYPE_NETWORK_TEST) { + dnodeProcessStartupReq(pMsg); + return; + } + + if (dnodeGetRunStat() != DN_RUN_STAT_RUNNING) { + rspMsg.code = TSDB_CODE_APP_NOT_READY; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); + dTrace("RPC %p, peer req:%s is ignored since dnode not running", pMsg->handle, taosMsg[msgType]); + return; + } + + if (pMsg->pCont == NULL) { + rspMsg.code = TSDB_CODE_DND_INVALID_MSG_LEN; + rpcSendResponse(&rspMsg); + return; + } + + MsgFp fp = tsTrans.msgFp[msgType]; + if (fp != NULL) { + dTrace("RPC %p, peer req:%s will be processed", pMsg->handle, taosMsg[msgType]); + (*fp)(pMsg); + } else { + dError("RPC %p, peer req:%s not processed", pMsg->handle, taosMsg[msgType]); + rspMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); + } +} + +static int32_t dnodeInitServer() { + SRpcInit rpcInit; + memset(&rpcInit, 0, sizeof(rpcInit)); + rpcInit.localPort = tsDnodeDnodePort; + rpcInit.label = "DND-S"; + rpcInit.numOfThreads = 1; + rpcInit.cfp = dnodeProcessPeerReq; + rpcInit.sessions = TSDB_MAX_VNODES << 4; + rpcInit.connType = TAOS_CONN_SERVER; + rpcInit.idleTime = tsShellActivityTimer * 1000; + + tsTrans.serverRpc = rpcOpen(&rpcInit); + if (tsTrans.serverRpc == NULL) { + dError("failed to init peer rpc server"); + return -1; + } + + dInfo("dnode peer rpc server is initialized"); + return 0; +} + +static void dnodeCleanupServer() { + if (tsTrans.serverRpc) { + rpcClose(tsTrans.serverRpc); + tsTrans.serverRpc = NULL; + dInfo("dnode peer server is closed"); + } +} + +static void dnodeProcessPeerRsp(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { + int32_t msgType = pMsg->msgType; + + if (dnodeGetRunStat() == DN_RUN_STAT_STOPPED) { + if (pMsg == NULL || pMsg->pCont == NULL) return; + dTrace("RPC %p, peer rsp:%s is ignored since dnode is stopping", pMsg->handle, taosMsg[msgType]); + rpcFreeCont(pMsg->pCont); + return; + } + + if (msgType == TSDB_MSG_TYPE_DM_STATUS_RSP && pEpSet) { + dnodeUpdateMnodeEps(pEpSet); + } + + MsgFp fp = tsTrans.msgFp[msgType]; + if (fp != NULL) { + dTrace("RPC %p, peer rsp:%s will be processed", pMsg->handle, taosMsg[msgType]); + (*fp)(pMsg); + } else { + dDebug("RPC %p, peer rsp:%s not processed", pMsg->handle, taosMsg[msgType]); + } + + rpcFreeCont(pMsg->pCont); +} + +static int32_t dnodeInitClient() { + char secret[TSDB_KEY_LEN] = "secret"; + SRpcInit rpcInit; + memset(&rpcInit, 0, sizeof(rpcInit)); + rpcInit.label = "DND-C"; + rpcInit.numOfThreads = 1; + rpcInit.cfp = dnodeProcessPeerRsp; + rpcInit.sessions = TSDB_MAX_VNODES << 4; + rpcInit.connType = TAOS_CONN_CLIENT; + rpcInit.idleTime = tsShellActivityTimer * 1000; + rpcInit.user = "t"; + rpcInit.ckey = "key"; + rpcInit.secret = secret; + + tsTrans.clientRpc = rpcOpen(&rpcInit); + if (tsTrans.clientRpc == NULL) { + dError("failed to init peer rpc client"); + return -1; + } + + dInfo("dnode peer rpc client is initialized"); + return 0; +} + +static void dnodeCleanupClient() { + if (tsTrans.clientRpc) { + rpcClose(tsTrans.clientRpc); + tsTrans.clientRpc = NULL; + dInfo("dnode peer rpc client is closed"); + } +} + +static void dnodeProcessShellReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0}; + int32_t msgType = pMsg->msgType; + + if (dnodeGetRunStat() == DN_RUN_STAT_STOPPED) { + dError("RPC %p, shell req:%s is ignored since dnode exiting", pMsg->handle, taosMsg[msgType]); + rspMsg.code = TSDB_CODE_DND_EXITING; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); + return; + } else if (dnodeGetRunStat() != DN_RUN_STAT_RUNNING) { + dError("RPC %p, shell req:%s is ignored since dnode not running", pMsg->handle, taosMsg[msgType]); + rspMsg.code = TSDB_CODE_APP_NOT_READY; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); + return; + } + + if (pMsg->pCont == NULL) { + rspMsg.code = TSDB_CODE_DND_INVALID_MSG_LEN; + rpcSendResponse(&rspMsg); + return; + } + + MsgFp fp = tsTrans.msgFp[msgType]; + if (fp != NULL) { + dTrace("RPC %p, shell req:%s will be processed", pMsg->handle, taosMsg[msgType]); + (*fp)(pMsg); + } else { + dError("RPC %p, shell req:%s is not processed", pMsg->handle, taosMsg[msgType]); + rspMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); + } +} + +void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg) { rpcSendRequest(tsTrans.clientRpc, epSet, rpcMsg, NULL); } + +void dnodeSendMsgToMnode(SRpcMsg *rpcMsg) { + SRpcEpSet epSet = {0}; + dnodeGetEpSetForPeer(&epSet); + dnodeSendMsgToDnode(&epSet, rpcMsg); +} + +static void dnodeSendMsgToMnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp) { + SRpcEpSet epSet = {0}; + dnodeGetEpSetForPeer(&epSet); + rpcSendRecv(tsTrans.clientRpc, &epSet, rpcMsg, rpcRsp); +} + +static int32_t dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey) { + int32_t code = mnodeRetriveAuth(user, spi, encrypt, secret, ckey); + if (code != TSDB_CODE_APP_NOT_READY) return code; + + SAuthMsg *pMsg = rpcMallocCont(sizeof(SAuthMsg)); + tstrncpy(pMsg->user, user, sizeof(pMsg->user)); + + SRpcMsg rpcMsg = {0}; + rpcMsg.pCont = pMsg; + rpcMsg.contLen = sizeof(SAuthMsg); + rpcMsg.msgType = TSDB_MSG_TYPE_DM_AUTH; + + dDebug("user:%s, send auth msg to mnodes", user); + SRpcMsg rpcRsp = {0}; + dnodeSendMsgToMnodeRecv(&rpcMsg, &rpcRsp); + + if (rpcRsp.code != 0) { + dError("user:%s, auth msg received from mnodes, error:%s", user, tstrerror(rpcRsp.code)); + } else { + SAuthRsp *pRsp = rpcRsp.pCont; + dDebug("user:%s, auth msg received from mnodes", user); + memcpy(secret, pRsp->secret, TSDB_KEY_LEN); + memcpy(ckey, pRsp->ckey, TSDB_KEY_LEN); + *spi = pRsp->spi; + *encrypt = pRsp->encrypt; + } + + rpcFreeCont(rpcRsp.pCont); + return rpcRsp.code; +} + +static int32_t dnodeInitShell() { + int32_t numOfThreads = (int32_t)((tsNumOfCores * tsNumOfThreadsPerCore) / 2.0); + if (numOfThreads < 1) { + numOfThreads = 1; + } + + SRpcInit rpcInit; + memset(&rpcInit, 0, sizeof(rpcInit)); + rpcInit.localPort = tsDnodeShellPort; + rpcInit.label = "SHELL"; + rpcInit.numOfThreads = numOfThreads; + rpcInit.cfp = dnodeProcessShellReq; + rpcInit.sessions = tsMaxShellConns; + rpcInit.connType = TAOS_CONN_SERVER; + rpcInit.idleTime = tsShellActivityTimer * 1000; + rpcInit.afp = dnodeRetrieveUserAuthInfo; + + tsTrans.shellRpc = rpcOpen(&rpcInit); + if (tsTrans.shellRpc == NULL) { + dError("failed to init shell rpc server"); + return -1; + } + + dInfo("dnode shell rpc server is initialized"); + return 0; +} + +static void dnodeCleanupShell() { + if (tsTrans.shellRpc) { + rpcClose(tsTrans.shellRpc); + tsTrans.shellRpc = NULL; + } +} + +int32_t dnodeInitTrans() { + if (dnodeInitClient() != 0) { + return -1; + } + + if (dnodeInitServer() != 0) { + return -1; + } + + if (dnodeInitShell() != 0) { + return -1; + } + + return 0; +} + +void dnodeCleanupTrans() { + dnodeCleanupShell(); + dnodeCleanupServer(); + dnodeCleanupClient(); +} diff --git a/source/dnode/mgmt/src/dnodeVnodes.c b/source/dnode/mgmt/src/dnodeVnodes.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/source/server/dnode/src/dnodeInt.c b/source/server/dnode/src/dnodeInt.c new file mode 100644 index 0000000000..aa66ab3da3 --- /dev/null +++ b/source/server/dnode/src/dnodeInt.c @@ -0,0 +1,204 @@ +/* + * 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 "dnodeCheck.h" +#include "dnodeConfig.h" +#include "dnodeDnode.h" +#include "dnodeTransport.h" +#include "mnode.h" +#include "sync.h" +#include "tcache.h" +#include "tconfig.h" +#include "tnote.h" +#include "tstep.h" +#include "vnode.h" +#include "wal.h" + +static struct { + EDnStat runStatus; + SStartupStep startup; + SSteps *steps; +} tsDnode; + +EDnStat dnodeGetRunStat() { return tsDnode.runStatus; } + +void dnodeSetRunStat(EDnStat stat) { tsDnode.runStatus = stat; } + +void dnodeReportStartup(char *name, char *desc) { + SStartupStep *startup = &tsDnode.startup; + tstrncpy(startup->name, name, strlen(startup->name)); + tstrncpy(startup->desc, desc, strlen(startup->desc)); + startup->finished = 0; +} + +static void dnodeReportStartupFinished(char *name, char *desc) { + SStartupStep *startup = &tsDnode.startup; + tstrncpy(startup->name, name, strlen(startup->name)); + tstrncpy(startup->desc, desc, strlen(startup->desc)); + startup->finished = 1; +} + +void dnodeGetStartup(SStartupStep *pStep) { memcpy(pStep, &tsDnode.startup, sizeof(SStartupStep)); } + +static int32_t dnodeInitVnode() { + return vnodeInit(); +} + +static int32_t dnodeInitMnode() { + SMnodePara para; + para.fp.GetDnodeEp = dnodeGetEp; + para.fp.SendMsgToDnode = dnodeSendMsgToDnode; + para.fp.SendMsgToMnode = dnodeSendMsgToMnode; + para.fp.SendRedirectMsg = dnodeSendRedirectMsg; + para.dnodeId = dnodeGetDnodeId(); + para.clusterId = dnodeGetClusterId(); + + return mnodeInit(para); +} + +static int32_t dnodeInitTfs() {} + +static int32_t dnodeInitMain() { + tsDnode.runStatus = DN_RUN_STAT_STOPPED; + tscEmbedded = 1; + taosIgnSIGPIPE(); + taosBlockSIGPIPE(); + taosResolveCRC(); + taosInitGlobalCfg(); + taosReadGlobalLogCfg(); + taosSetCoreDump(tsEnableCoreFile); + + if (!taosMkDir(tsLogDir)) { + printf("failed to create dir: %s, reason: %s\n", tsLogDir, strerror(errno)); + return -1; + } + + char temp[TSDB_FILENAME_LEN]; + sprintf(temp, "%s/taosdlog", tsLogDir); + if (taosInitLog(temp, tsNumOfLogLines, 1) < 0) { + printf("failed to init log file\n"); + } + + if (!taosReadGlobalCfg()) { + taosPrintGlobalCfg(); + dError("TDengine read global config failed"); + return -1; + } + + dInfo("start to initialize TDengine"); + + taosInitNotes(); + + return taosCheckGlobalCfg(); +} + +static void dnodeCleanupMain() { + taos_cleanup(); + taosCloseLog(); + taosStopCacheRefreshWorker(); +} + +static int32_t dnodeCheckRunning(char *dir) { + char filepath[256] = {0}; + snprintf(filepath, sizeof(filepath), "%s/.running", dir); + + FileFd fd = taosOpenFileCreateWriteTrunc(filepath); + if (fd < 0) { + dError("failed to open lock file:%s since %s, quit", filepath, strerror(errno)); + return -1; + } + + int32_t ret = taosLockFile(fd); + if (ret != 0) { + dError("failed to lock file:%s since %s, quit", filepath, strerror(errno)); + taosCloseFile(fd); + return -1; + } + + return 0; +} + +static int32_t dnodeInitDir() { + sprintf(tsMnodeDir, "%s/mnode", tsDataDir); + sprintf(tsVnodeDir, "%s/vnode", tsDataDir); + sprintf(tsDnodeDir, "%s/dnode", tsDataDir); + + if (!taosMkDir(tsDnodeDir)) { + dError("failed to create dir:%s since %s", tsDnodeDir, strerror(errno)); + return -1; + } + + if (!taosMkDir(tsMnodeDir)) { + dError("failed to create dir:%s since %s", tsMnodeDir, strerror(errno)); + return -1; + } + + if (!taosMkDir(tsVnodeDir)) { + dError("failed to create dir:%s since %s", tsVnodeDir, strerror(errno)); + return -1; + } + + if (dnodeCheckRunning(tsDnodeDir) != 0) { + return -1; + } + + return 0; +} + +static void dnodeCleanupDir() {} + +int32_t dnodeInit() { + SSteps *steps = taosStepInit(24, dnodeReportStartup); + if (steps == NULL) return -1; + + taosStepAdd(steps, "dnode-main", dnodeInitMain, dnodeCleanupMain); + taosStepAdd(steps, "dnode-dir", dnodeInitDir, dnodeCleanupDir); + taosStepAdd(steps, "dnode-check", dnodeInitCheck, dnodeCleanupCheck); + taosStepAdd(steps, "dnode-rpc", rpcInit, rpcCleanup); + taosStepAdd(steps, "dnode-tfs", dnodeInitTfs, NULL); + taosStepAdd(steps, "dnode-wal", walInit, walCleanUp); + taosStepAdd(steps, "dnode-sync", syncInit, syncCleanUp); + taosStepAdd(steps, "dnode-eps", dnodeInitEps, dnodeCleanupEps); + taosStepAdd(steps, "dnode-vnode", dnodeInitVnode, vnodeCleanup); + taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, mnodeCleanup); + taosStepAdd(steps, "dnode-trans", dnodeInitTrans, dnodeCleanupTrans); + taosStepAdd(steps, "dnode-msg", dnodeInitMsg, dnodeCleanupMsg); + + tsDnode.steps = steps; + taosStepExec(tsDnode.steps); + + dnodeSetRunStat(DN_RUN_STAT_RUNNING); + dnodeReportStartupFinished("TDengine", "initialized successfully"); + dInfo("TDengine is initialized successfully"); + + return 0; +} + +void dnodeCleanup() { + if (dnodeGetRunStat() != DN_RUN_STAT_STOPPED) { + dnodeSetRunStat(DN_RUN_STAT_STOPPED); + taosStepCleanup(tsDnode.steps); + tsDnode.steps = NULL; + } +} + +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = vnodeProcessMgmtMsg; +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = vnodeProcessMgmtMsg; +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = vnodeProcessMgmtMsg; +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessMgmtMsg; +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessMgmtMsg; +// tsVnode.msgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessMgmtMsg; + \ No newline at end of file diff --git a/source/dnode/mgmt/src/dnodeMsg.c b/source/server/dnode/src/dnodeMsg.c similarity index 99% rename from source/dnode/mgmt/src/dnodeMsg.c rename to source/server/dnode/src/dnodeMsg.c index a5c3db5e14..0a598527b4 100644 --- a/source/dnode/mgmt/src/dnodeMsg.c +++ b/source/server/dnode/src/dnodeMsg.c @@ -14,8 +14,8 @@ */ #define _DEFAULT_SOURCE -#include "dnodeMsg.h" -#include "dnodeEps.h" +#include "dnodeDnode.h" +#include "dnodeConfig.h" #include "mnode.h" #include "tthread.h" #include "ttime.h" diff --git a/source/dnode/mgmt/src/dnodeTrans.c b/source/server/dnode/src/dnodeTrans.c similarity index 99% rename from source/dnode/mgmt/src/dnodeTrans.c rename to source/server/dnode/src/dnodeTrans.c index 3e32510df9..a9e130cd58 100644 --- a/source/dnode/mgmt/src/dnodeTrans.c +++ b/source/server/dnode/src/dnodeTrans.c @@ -20,9 +20,9 @@ */ #define _DEFAULT_SOURCE -#include "dnodeTrans.h" -#include "dnodeEps.h" -#include "dnodeMsg.h" +#include "dnodeTransport.h" +#include "dnodeConfig.h" +#include "dnodeDnode.h" #include "mnode.h" #include "vnode.h" From 0dddcdfbeb3d8d3bd27fafbb0c04b5e416d2eb0e Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 1 Nov 2021 20:21:21 +0800 Subject: [PATCH 14/33] refact files --- source/dnode/mgmt/inc/dnodeConfig.h | 5 +- source/dnode/mgmt/src/dnodeConfig.c | 228 ++++++++-------- source/dnode/mgmt/src/dnodeInt.c | 2 +- source/dnode/mgmt/src/dnodeMain.c | 6 +- source/server/dnode/src/dnodeInt.c | 204 -------------- source/server/dnode/src/dnodeMsg.c | 174 ------------ source/server/dnode/src/dnodeTrans.c | 380 --------------------------- 7 files changed, 120 insertions(+), 879 deletions(-) delete mode 100644 source/server/dnode/src/dnodeInt.c delete mode 100644 source/server/dnode/src/dnodeMsg.c delete mode 100644 source/server/dnode/src/dnodeTrans.c diff --git a/source/dnode/mgmt/inc/dnodeConfig.h b/source/dnode/mgmt/inc/dnodeConfig.h index 9d446a9f1a..71f3ac3f97 100644 --- a/source/dnode/mgmt/inc/dnodeConfig.h +++ b/source/dnode/mgmt/inc/dnodeConfig.h @@ -21,8 +21,8 @@ extern "C" { #endif #include "dnodeInt.h" -int32_t dnodeInitEps(); -void dnodeCleanupEps(); +int32_t dnodeInitConfig(); +void dnodeCleanupConfig(); void dnodeUpdateCfg(SDnodeCfg *data); void dnodeUpdateDnodeEps(SDnodeEps *data); @@ -32,7 +32,6 @@ int64_t dnodeGetClusterId(); void dnodeGetEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port); void dnodeGetEpSetForPeer(SRpcEpSet *epSet); -void dnodeGetEpSetForShell(SRpcEpSet *epSet); void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell); #ifdef __cplusplus diff --git a/source/dnode/mgmt/src/dnodeConfig.c b/source/dnode/mgmt/src/dnodeConfig.c index fc4c821bb6..d6189eb632 100644 --- a/source/dnode/mgmt/src/dnodeConfig.c +++ b/source/dnode/mgmt/src/dnodeConfig.c @@ -19,27 +19,27 @@ #include "thash.h" static struct { - int32_t dnodeId; - int32_t dropped; - int64_t clusterId; - SDnodeEps *dnodeEps; - SHashObj *dnodeHash; - SRpcEpSet mnodeEpSetForShell; - SRpcEpSet mnodeEpSetForPeer; - char file[PATH_MAX + 20]; + int32_t dnodeId; + int32_t dropped; + int64_t clusterId; + SDnodeEps *dnodeEps; + SHashObj *dnodeHash; + SRpcEpSet mnodeEpSetForShell; + SRpcEpSet mnodeEpSetForPeer; + char file[PATH_MAX + 20]; pthread_mutex_t mutex; -} tsEps; +} tsConfig; -void dnodeGetEpSetForPeer(SRpcEpSet *epSet) { - pthread_mutex_lock(&tsEps.mutex); - *epSet = tsEps.mnodeEpSetForPeer; - pthread_mutex_unlock(&tsEps.mutex); +vstaticoid dnodeGetEpSetForPeer(SRpcEpSet *epSet) { + pthread_mutex_lock(&tsConfig.mutex); + *epSet = tsConfig.mnodeEpSetForPeer; + pthread_mutex_unlock(&tsConfig.mutex); } -void dnodeGetEpSetForShell(SRpcEpSet *epSet) { - pthread_mutex_lock(&tsEps.mutex); - *epSet = tsEps.mnodeEpSetForShell; - pthread_mutex_unlock(&tsEps.mutex); +static void dnodeGetEpSetForShell(SRpcEpSet *epSet) { + pthread_mutex_lock(&tsConfig.mutex); + *epSet = tsConfig.mnodeEpSetForShell; + pthread_mutex_unlock(&tsConfig.mutex); } void dnodeUpdateMnodeEps(SRpcEpSet *ep) { @@ -48,18 +48,18 @@ void dnodeUpdateMnodeEps(SRpcEpSet *ep) { return; } - pthread_mutex_lock(&tsEps.mutex); + pthread_mutex_lock(&tsConfig.mutex); dInfo("mnode is changed, num:%d use:%d", ep->numOfEps, ep->inUse); - tsEps.mnodeEpSetForPeer = *ep; + tsConfig.mnodeEpSetForPeer = *ep; for (int32_t i = 0; i < ep->numOfEps; ++i) { ep->port[i] -= TSDB_PORT_DNODEDNODE; dInfo("mnode index:%d %s:%u", i, ep->fqdn[i], ep->port[i]); } - tsEps.mnodeEpSetForShell = *ep; + tsConfig.mnodeEpSetForShell = *ep; - pthread_mutex_unlock(&tsEps.mutex); + pthread_mutex_unlock(&tsConfig.mutex); } void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell) { @@ -92,9 +92,9 @@ void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell) { } static void dnodePrintEps() { - dDebug("print dnode list, num:%d", tsEps.dnodeEps->dnodeNum); - for (int32_t i = 0; i < tsEps.dnodeEps->dnodeNum; i++) { - SDnodeEp *ep = &tsEps.dnodeEps->dnodeEps[i]; + dDebug("print dnode list, num:%d", tsConfig.dnodeEps->dnodeNum); + for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; i++) { + SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; dDebug("dnode:%d, fqdn:%s port:%u isMnode:%d", ep->dnodeId, ep->dnodeFqdn, ep->dnodePort, ep->isMnode); } } @@ -104,35 +104,35 @@ static void dnodeResetEps(SDnodeEps *data) { int32_t size = sizeof(SDnodeEps) + data->dnodeNum * sizeof(SDnodeEp); - if (data->dnodeNum > tsEps.dnodeEps->dnodeNum) { + if (data->dnodeNum > tsConfig.dnodeEps->dnodeNum) { SDnodeEps *tmp = calloc(1, size); if (tmp == NULL) return; - tfree(tsEps.dnodeEps); - tsEps.dnodeEps = tmp; + tfree(tsConfig.dnodeEps); + tsConfig.dnodeEps = tmp; } - if (tsEps.dnodeEps != data) { - memcpy(tsEps.dnodeEps, data, size); + if (tsConfig.dnodeEps != data) { + memcpy(tsConfig.dnodeEps, data, size); } - tsEps.mnodeEpSetForPeer.inUse = 0; - tsEps.mnodeEpSetForShell.inUse = 0; + tsConfig.mnodeEpSetForPeer.inUse = 0; + tsConfig.mnodeEpSetForShell.inUse = 0; int32_t index = 0; - for (int32_t i = 0; i < tsEps.dnodeEps->dnodeNum; i++) { - SDnodeEp *ep = &tsEps.dnodeEps->dnodeEps[i]; + for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; i++) { + SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; if (!ep->isMnode) continue; if (index >= TSDB_MAX_REPLICA) continue; - strcpy(tsEps.mnodeEpSetForShell.fqdn[index], ep->dnodeFqdn); - strcpy(tsEps.mnodeEpSetForPeer.fqdn[index], ep->dnodeFqdn); - tsEps.mnodeEpSetForShell.port[index] = ep->dnodePort; - tsEps.mnodeEpSetForShell.port[index] = ep->dnodePort + tsDnodeDnodePort; + strcpy(tsConfig.mnodeEpSetForShell.fqdn[index], ep->dnodeFqdn); + strcpy(tsConfig.mnodeEpSetForPeer.fqdn[index], ep->dnodeFqdn); + tsConfig.mnodeEpSetForShell.port[index] = ep->dnodePort; + tsConfig.mnodeEpSetForShell.port[index] = ep->dnodePort + tsDnodeDnodePort; index++; } - for (int32_t i = 0; i < tsEps.dnodeEps->dnodeNum; ++i) { - SDnodeEp *ep = &tsEps.dnodeEps->dnodeEps[i]; - taosHashPut(tsEps.dnodeHash, &ep->dnodeId, sizeof(int32_t), ep, sizeof(SDnodeEp)); + for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; ++i) { + SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; + taosHashPut(tsConfig.dnodeHash, &ep->dnodeId, sizeof(int32_t), ep, sizeof(SDnodeEp)); } dnodePrintEps(); @@ -141,9 +141,9 @@ static void dnodeResetEps(SDnodeEps *data) { static bool dnodeIsDnodeEpChanged(int32_t dnodeId, char *epstr) { bool changed = false; - pthread_mutex_lock(&tsEps.mutex); + pthread_mutex_lock(&tsConfig.mutex); - SDnodeEp *ep = taosHashGet(tsEps.dnodeHash, &dnodeId, sizeof(int32_t)); + SDnodeEp *ep = taosHashGet(tsConfig.dnodeHash, &dnodeId, sizeof(int32_t)); if (ep != NULL) { char epSaved[TSDB_EP_LEN + 1]; snprintf(epSaved, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); @@ -151,7 +151,7 @@ static bool dnodeIsDnodeEpChanged(int32_t dnodeId, char *epstr) { tstrncpy(epstr, epSaved, TSDB_EP_LEN); } - pthread_mutex_unlock(&tsEps.mutex); + pthread_mutex_unlock(&tsConfig.mutex); return changed; } @@ -163,101 +163,101 @@ static int32_t dnodeReadEps() { cJSON *root = NULL; FILE *fp = NULL; - fp = fopen(tsEps.file, "r"); + fp = fopen(tsConfig.file, "r"); if (!fp) { - dDebug("file %s not exist", tsEps.file); + dDebug("file %s not exist", tsConfig.file); goto PRASE_EPS_OVER; } len = (int32_t)fread(content, 1, maxLen, fp); if (len <= 0) { - dError("failed to read %s since content is null", tsEps.file); + dError("failed to read %s since content is null", tsConfig.file); goto PRASE_EPS_OVER; } content[len] = 0; root = cJSON_Parse(content); if (root == NULL) { - dError("failed to read %s since invalid json format", tsEps.file); + dError("failed to read %s since invalid json format", tsConfig.file); goto PRASE_EPS_OVER; } cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId"); if (!dnodeId || dnodeId->type != cJSON_String) { - dError("failed to read %s since dnodeId not found", tsEps.file); + dError("failed to read %s since dnodeId not found", tsConfig.file); goto PRASE_EPS_OVER; } - tsEps.dnodeId = atoi(dnodeId->valuestring); + tsConfig.dnodeId = atoi(dnodeId->valuestring); cJSON *dropped = cJSON_GetObjectItem(root, "dropped"); if (!dropped || dropped->type != cJSON_String) { - dError("failed to read %s since dropped not found", tsEps.file); + dError("failed to read %s since dropped not found", tsConfig.file); goto PRASE_EPS_OVER; } - tsEps.dropped = atoi(dropped->valuestring); + tsConfig.dropped = atoi(dropped->valuestring); cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId"); if (!clusterId || clusterId->type != cJSON_String) { - dError("failed to read %s since clusterId not found", tsEps.file); + dError("failed to read %s since clusterId not found", tsConfig.file); goto PRASE_EPS_OVER; } - tsEps.clusterId = atoll(clusterId->valuestring); + tsConfig.clusterId = atoll(clusterId->valuestring); cJSON *dnodeInfos = cJSON_GetObjectItem(root, "dnodeInfos"); if (!dnodeInfos || dnodeInfos->type != cJSON_Array) { - dError("failed to read %s since dnodeInfos not found", tsEps.file); + dError("failed to read %s since dnodeInfos not found", tsConfig.file); goto PRASE_EPS_OVER; } int32_t dnodeInfosSize = cJSON_GetArraySize(dnodeInfos); if (dnodeInfosSize <= 0) { - dError("failed to read %s since dnodeInfos size:%d invalid", tsEps.file, dnodeInfosSize); + dError("failed to read %s since dnodeInfos size:%d invalid", tsConfig.file, dnodeInfosSize); goto PRASE_EPS_OVER; } - tsEps.dnodeEps = calloc(1, dnodeInfosSize * sizeof(SDnodeEp) + sizeof(SDnodeEps)); - if (tsEps.dnodeEps == NULL) { + tsConfig.dnodeEps = calloc(1, dnodeInfosSize * sizeof(SDnodeEp) + sizeof(SDnodeEps)); + if (tsConfig.dnodeEps == NULL) { dError("failed to calloc dnodeEpList since %s", strerror(errno)); goto PRASE_EPS_OVER; } - tsEps.dnodeEps->dnodeNum = dnodeInfosSize; + tsConfig.dnodeEps->dnodeNum = dnodeInfosSize; for (int32_t i = 0; i < dnodeInfosSize; ++i) { cJSON *dnodeInfo = cJSON_GetArrayItem(dnodeInfos, i); if (dnodeInfo == NULL) break; - SDnodeEp *ep = &tsEps.dnodeEps->dnodeEps[i]; + SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; cJSON *dnodeId = cJSON_GetObjectItem(dnodeInfo, "dnodeId"); if (!dnodeId || dnodeId->type != cJSON_String) { - dError("failed to read %s, dnodeId not found", tsEps.file); + dError("failed to read %s, dnodeId not found", tsConfig.file); goto PRASE_EPS_OVER; } ep->dnodeId = atoi(dnodeId->valuestring); cJSON *isMnode = cJSON_GetObjectItem(dnodeInfo, "isMnode"); if (!isMnode || isMnode->type != cJSON_String) { - dError("failed to read %s, isMnode not found", tsEps.file); + dError("failed to read %s, isMnode not found", tsConfig.file); goto PRASE_EPS_OVER; } ep->isMnode = atoi(isMnode->valuestring); cJSON *dnodeFqdn = cJSON_GetObjectItem(dnodeInfo, "dnodeFqdn"); if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) { - dError("failed to read %s, dnodeFqdn not found", tsEps.file); + dError("failed to read %s, dnodeFqdn not found", tsConfig.file); goto PRASE_EPS_OVER; } tstrncpy(ep->dnodeFqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN); cJSON *dnodePort = cJSON_GetObjectItem(dnodeInfo, "dnodePort"); if (!dnodePort || dnodePort->type != cJSON_String) { - dError("failed to read %s, dnodePort not found", tsEps.file); + dError("failed to read %s, dnodePort not found", tsConfig.file); goto PRASE_EPS_OVER; } ep->dnodePort = atoi(dnodePort->valuestring); } - dInfo("succcessed to read file %s", tsEps.file); + dInfo("succcessed to read file %s", tsConfig.file); dnodePrintEps(); PRASE_EPS_OVER: @@ -265,21 +265,21 @@ PRASE_EPS_OVER: if (root != NULL) cJSON_Delete(root); if (fp != NULL) fclose(fp); - if (dnodeIsDnodeEpChanged(tsEps.dnodeId, tsLocalEp)) { - dError("dnode:%d, localEp %s different with dnodeEps.json and need reconfigured", tsEps.dnodeId, tsLocalEp); + if (dnodeIsDnodeEpChanged(tsConfig.dnodeId, tsLocalEp)) { + dError("dnode:%d, localEp %s different with dnodeEps.json and need reconfigured", tsConfig.dnodeId, tsLocalEp); return -1; } - dnodeResetEps(tsEps.dnodeEps); + dnodeResetEps(tsConfig.dnodeEps); terrno = 0; return 0; } static int32_t dnodeWriteEps() { - FILE *fp = fopen(tsEps.file, "w"); + FILE *fp = fopen(tsConfig.file, "w"); if (!fp) { - dError("failed to write %s since %s", tsEps.file, strerror(errno)); + dError("failed to write %s since %s", tsConfig.file, strerror(errno)); return -1; } @@ -288,17 +288,17 @@ static int32_t dnodeWriteEps() { char *content = calloc(1, maxLen + 1); len += snprintf(content + len, maxLen - len, "{\n"); - len += snprintf(content + len, maxLen - len, " \"dnodeId\": \"%d\",\n", tsEps.dnodeId); - len += snprintf(content + len, maxLen - len, " \"dropped\": \"%d\",\n", tsEps.dropped); - len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%" PRId64 "\",\n", tsEps.clusterId); + len += snprintf(content + len, maxLen - len, " \"dnodeId\": \"%d\",\n", tsConfig.dnodeId); + len += snprintf(content + len, maxLen - len, " \"dropped\": \"%d\",\n", tsConfig.dropped); + len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%" PRId64 "\",\n", tsConfig.clusterId); len += snprintf(content + len, maxLen - len, " \"dnodeInfos\": [{\n"); - for (int32_t i = 0; i < tsEps.dnodeEps->dnodeNum; ++i) { - SDnodeEp *ep = &tsEps.dnodeEps->dnodeEps[i]; + for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; ++i) { + SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; len += snprintf(content + len, maxLen - len, " \"dnodeId\": \"%d\",\n", ep->dnodeId); len += snprintf(content + len, maxLen - len, " \"isMnode\": \"%d\",\n", ep->isMnode); len += snprintf(content + len, maxLen - len, " \"dnodeFqdn\": \"%s\",\n", ep->dnodeFqdn); len += snprintf(content + len, maxLen - len, " \"dnodePort\": \"%u\"\n", ep->dnodePort); - if (i < tsEps.dnodeEps->dnodeNum - 1) { + if (i < tsConfig.dnodeEps->dnodeNum - 1) { len += snprintf(content + len, maxLen - len, " },{\n"); } else { len += snprintf(content + len, maxLen - len, " }]\n"); @@ -312,20 +312,20 @@ static int32_t dnodeWriteEps() { free(content); terrno = 0; - dInfo("successed to write %s", tsEps.file); + dInfo("successed to write %s", tsConfig.file); return 0; } -int32_t dnodeInitEps() { - tsEps.dnodeId = 0; - tsEps.dropped = 0; - tsEps.clusterId = 0; - tsEps.dnodeEps = NULL; - snprintf(tsEps.file, sizeof(tsEps.file), "%s/dnodeEps.json", tsDnodeDir); - pthread_mutex_init(&tsEps.mutex, NULL); +int32_t dnodeInitConfig() { + tsConfig.dnodeId = 0; + tsConfig.dropped = 0; + tsConfig.clusterId = 0; + tsConfig.dnodeEps = NULL; + snprintf(tsConfig.file, sizeof(tsConfig.file), "%s/dnodeEps.json", tsDnodeDir); + pthread_mutex_init(&tsConfig.mutex, NULL); - tsEps.dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); - if (tsEps.dnodeHash == NULL) return -1; + tsConfig.dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); + if (tsConfig.dnodeHash == NULL) return -1; int32_t ret = dnodeReadEps(); if (ret == 0) { @@ -335,81 +335,81 @@ int32_t dnodeInitEps() { return ret; } -void dnodeCleanupEps() { - pthread_mutex_lock(&tsEps.mutex); +void dnodeCleanupConfig() { + pthread_mutex_lock(&tsConfig.mutex); - if (tsEps.dnodeEps != NULL) { - free(tsEps.dnodeEps); - tsEps.dnodeEps = NULL; + if (tsConfig.dnodeEps != NULL) { + free(tsConfig.dnodeEps); + tsConfig.dnodeEps = NULL; } - if (tsEps.dnodeHash) { - taosHashCleanup(tsEps.dnodeHash); - tsEps.dnodeHash = NULL; + if (tsConfig.dnodeHash) { + taosHashCleanup(tsConfig.dnodeHash); + tsConfig.dnodeHash = NULL; } - pthread_mutex_unlock(&tsEps.mutex); - pthread_mutex_destroy(&tsEps.mutex); + pthread_mutex_unlock(&tsConfig.mutex); + pthread_mutex_destroy(&tsConfig.mutex); } void dnodeUpdateDnodeEps(SDnodeEps *data) { if (data == NULL || data->dnodeNum <= 0) return; - pthread_mutex_lock(&tsEps.mutex); + pthread_mutex_lock(&tsConfig.mutex); - if (data->dnodeNum != tsEps.dnodeEps->dnodeNum) { + if (data->dnodeNum != tsConfig.dnodeEps->dnodeNum) { dnodeResetEps(data); dnodeWriteEps(); } else { int32_t size = data->dnodeNum * sizeof(SDnodeEp) + sizeof(SDnodeEps); - if (memcmp(tsEps.dnodeEps, data, size) != 0) { + if (memcmp(tsConfig.dnodeEps, data, size) != 0) { dnodeResetEps(data); dnodeWriteEps(); } } - pthread_mutex_unlock(&tsEps.mutex); + pthread_mutex_unlock(&tsConfig.mutex); } void dnodeGetEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port) { - pthread_mutex_lock(&tsEps.mutex); + pthread_mutex_lock(&tsConfig.mutex); - SDnodeEp *ep = taosHashGet(tsEps.dnodeHash, &dnodeId, sizeof(int32_t)); + SDnodeEp *ep = taosHashGet(tsConfig.dnodeHash, &dnodeId, sizeof(int32_t)); if (ep != NULL) { if (port) *port = ep->dnodePort; if (fqdn) tstrncpy(fqdn, ep->dnodeFqdn, TSDB_FQDN_LEN); if (epstr) snprintf(epstr, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); } - pthread_mutex_unlock(&tsEps.mutex); + pthread_mutex_unlock(&tsConfig.mutex); } void dnodeUpdateCfg(SDnodeCfg *data) { - if (tsEps.dnodeId != 0 && !data->dropped) return; + if (tsConfig.dnodeId != 0 && !data->dropped) return; - pthread_mutex_lock(&tsEps.mutex); + pthread_mutex_lock(&tsConfig.mutex); - tsEps.dnodeId = data->dnodeId; - tsEps.clusterId = data->clusterId; - tsEps.dropped = data->dropped; + tsConfig.dnodeId = data->dnodeId; + tsConfig.clusterId = data->clusterId; + tsConfig.dropped = data->dropped; dInfo("dnodeId is set to %d, clusterId is set to %" PRId64, data->dnodeId, data->clusterId); dnodeWriteEps(); - pthread_mutex_unlock(&tsEps.mutex); + pthread_mutex_unlock(&tsConfig.mutex); } int32_t dnodeGetDnodeId() { int32_t dnodeId = 0; - pthread_mutex_lock(&tsEps.mutex); - dnodeId = tsEps.dnodeId; - pthread_mutex_unlock(&tsEps.mutex); + pthread_mutex_lock(&tsConfig.mutex); + dnodeId = tsConfig.dnodeId; + pthread_mutex_unlock(&tsConfig.mutex); return dnodeId; } int64_t dnodeGetClusterId() { int64_t clusterId = 0; - pthread_mutex_lock(&tsEps.mutex); - clusterId = tsEps.clusterId; - pthread_mutex_unlock(&tsEps.mutex); + pthread_mutex_lock(&tsConfig.mutex); + clusterId = tsConfig.clusterId; + pthread_mutex_unlock(&tsConfig.mutex); return clusterId; } diff --git a/source/dnode/mgmt/src/dnodeInt.c b/source/dnode/mgmt/src/dnodeInt.c index aa66ab3da3..de1d03ae7a 100644 --- a/source/dnode/mgmt/src/dnodeInt.c +++ b/source/dnode/mgmt/src/dnodeInt.c @@ -171,7 +171,7 @@ int32_t dnodeInit() { taosStepAdd(steps, "dnode-tfs", dnodeInitTfs, NULL); taosStepAdd(steps, "dnode-wal", walInit, walCleanUp); taosStepAdd(steps, "dnode-sync", syncInit, syncCleanUp); - taosStepAdd(steps, "dnode-eps", dnodeInitEps, dnodeCleanupEps); + taosStepAdd(steps, "dnode-eps", dnodeInitConfig, dnodeCleanupConfig); taosStepAdd(steps, "dnode-vnode", dnodeInitVnode, vnodeCleanup); taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, mnodeCleanup); taosStepAdd(steps, "dnode-trans", dnodeInitTrans, dnodeCleanupTrans); diff --git a/source/dnode/mgmt/src/dnodeMain.c b/source/dnode/mgmt/src/dnodeMain.c index c41acc6853..69960173f4 100644 --- a/source/dnode/mgmt/src/dnodeMain.c +++ b/source/dnode/mgmt/src/dnodeMain.c @@ -31,17 +31,17 @@ int main(int argc, char const *argv[]) { int32_t code = dnodeInit(); if (code != 0) { - uInfo("Failed to start TDengine, please check the log at:%s", tsLogDir); + dInfo("Failed to start TDengine, please check the log at:%s", tsLogDir); exit(EXIT_FAILURE); } - uInfo("Started TDengine service successfully."); + dInfo("Started TDengine service successfully."); while (!stop) { taosMsleep(100); } - uInfo("TDengine is shut down!"); + dInfo("TDengine is shut down!"); dnodeCleanup(); return 0; diff --git a/source/server/dnode/src/dnodeInt.c b/source/server/dnode/src/dnodeInt.c deleted file mode 100644 index aa66ab3da3..0000000000 --- a/source/server/dnode/src/dnodeInt.c +++ /dev/null @@ -1,204 +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 "dnodeCheck.h" -#include "dnodeConfig.h" -#include "dnodeDnode.h" -#include "dnodeTransport.h" -#include "mnode.h" -#include "sync.h" -#include "tcache.h" -#include "tconfig.h" -#include "tnote.h" -#include "tstep.h" -#include "vnode.h" -#include "wal.h" - -static struct { - EDnStat runStatus; - SStartupStep startup; - SSteps *steps; -} tsDnode; - -EDnStat dnodeGetRunStat() { return tsDnode.runStatus; } - -void dnodeSetRunStat(EDnStat stat) { tsDnode.runStatus = stat; } - -void dnodeReportStartup(char *name, char *desc) { - SStartupStep *startup = &tsDnode.startup; - tstrncpy(startup->name, name, strlen(startup->name)); - tstrncpy(startup->desc, desc, strlen(startup->desc)); - startup->finished = 0; -} - -static void dnodeReportStartupFinished(char *name, char *desc) { - SStartupStep *startup = &tsDnode.startup; - tstrncpy(startup->name, name, strlen(startup->name)); - tstrncpy(startup->desc, desc, strlen(startup->desc)); - startup->finished = 1; -} - -void dnodeGetStartup(SStartupStep *pStep) { memcpy(pStep, &tsDnode.startup, sizeof(SStartupStep)); } - -static int32_t dnodeInitVnode() { - return vnodeInit(); -} - -static int32_t dnodeInitMnode() { - SMnodePara para; - para.fp.GetDnodeEp = dnodeGetEp; - para.fp.SendMsgToDnode = dnodeSendMsgToDnode; - para.fp.SendMsgToMnode = dnodeSendMsgToMnode; - para.fp.SendRedirectMsg = dnodeSendRedirectMsg; - para.dnodeId = dnodeGetDnodeId(); - para.clusterId = dnodeGetClusterId(); - - return mnodeInit(para); -} - -static int32_t dnodeInitTfs() {} - -static int32_t dnodeInitMain() { - tsDnode.runStatus = DN_RUN_STAT_STOPPED; - tscEmbedded = 1; - taosIgnSIGPIPE(); - taosBlockSIGPIPE(); - taosResolveCRC(); - taosInitGlobalCfg(); - taosReadGlobalLogCfg(); - taosSetCoreDump(tsEnableCoreFile); - - if (!taosMkDir(tsLogDir)) { - printf("failed to create dir: %s, reason: %s\n", tsLogDir, strerror(errno)); - return -1; - } - - char temp[TSDB_FILENAME_LEN]; - sprintf(temp, "%s/taosdlog", tsLogDir); - if (taosInitLog(temp, tsNumOfLogLines, 1) < 0) { - printf("failed to init log file\n"); - } - - if (!taosReadGlobalCfg()) { - taosPrintGlobalCfg(); - dError("TDengine read global config failed"); - return -1; - } - - dInfo("start to initialize TDengine"); - - taosInitNotes(); - - return taosCheckGlobalCfg(); -} - -static void dnodeCleanupMain() { - taos_cleanup(); - taosCloseLog(); - taosStopCacheRefreshWorker(); -} - -static int32_t dnodeCheckRunning(char *dir) { - char filepath[256] = {0}; - snprintf(filepath, sizeof(filepath), "%s/.running", dir); - - FileFd fd = taosOpenFileCreateWriteTrunc(filepath); - if (fd < 0) { - dError("failed to open lock file:%s since %s, quit", filepath, strerror(errno)); - return -1; - } - - int32_t ret = taosLockFile(fd); - if (ret != 0) { - dError("failed to lock file:%s since %s, quit", filepath, strerror(errno)); - taosCloseFile(fd); - return -1; - } - - return 0; -} - -static int32_t dnodeInitDir() { - sprintf(tsMnodeDir, "%s/mnode", tsDataDir); - sprintf(tsVnodeDir, "%s/vnode", tsDataDir); - sprintf(tsDnodeDir, "%s/dnode", tsDataDir); - - if (!taosMkDir(tsDnodeDir)) { - dError("failed to create dir:%s since %s", tsDnodeDir, strerror(errno)); - return -1; - } - - if (!taosMkDir(tsMnodeDir)) { - dError("failed to create dir:%s since %s", tsMnodeDir, strerror(errno)); - return -1; - } - - if (!taosMkDir(tsVnodeDir)) { - dError("failed to create dir:%s since %s", tsVnodeDir, strerror(errno)); - return -1; - } - - if (dnodeCheckRunning(tsDnodeDir) != 0) { - return -1; - } - - return 0; -} - -static void dnodeCleanupDir() {} - -int32_t dnodeInit() { - SSteps *steps = taosStepInit(24, dnodeReportStartup); - if (steps == NULL) return -1; - - taosStepAdd(steps, "dnode-main", dnodeInitMain, dnodeCleanupMain); - taosStepAdd(steps, "dnode-dir", dnodeInitDir, dnodeCleanupDir); - taosStepAdd(steps, "dnode-check", dnodeInitCheck, dnodeCleanupCheck); - taosStepAdd(steps, "dnode-rpc", rpcInit, rpcCleanup); - taosStepAdd(steps, "dnode-tfs", dnodeInitTfs, NULL); - taosStepAdd(steps, "dnode-wal", walInit, walCleanUp); - taosStepAdd(steps, "dnode-sync", syncInit, syncCleanUp); - taosStepAdd(steps, "dnode-eps", dnodeInitEps, dnodeCleanupEps); - taosStepAdd(steps, "dnode-vnode", dnodeInitVnode, vnodeCleanup); - taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, mnodeCleanup); - taosStepAdd(steps, "dnode-trans", dnodeInitTrans, dnodeCleanupTrans); - taosStepAdd(steps, "dnode-msg", dnodeInitMsg, dnodeCleanupMsg); - - tsDnode.steps = steps; - taosStepExec(tsDnode.steps); - - dnodeSetRunStat(DN_RUN_STAT_RUNNING); - dnodeReportStartupFinished("TDengine", "initialized successfully"); - dInfo("TDengine is initialized successfully"); - - return 0; -} - -void dnodeCleanup() { - if (dnodeGetRunStat() != DN_RUN_STAT_STOPPED) { - dnodeSetRunStat(DN_RUN_STAT_STOPPED); - taosStepCleanup(tsDnode.steps); - tsDnode.steps = NULL; - } -} - -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = vnodeProcessMgmtMsg; -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = vnodeProcessMgmtMsg; -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = vnodeProcessMgmtMsg; -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessMgmtMsg; -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessMgmtMsg; -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessMgmtMsg; - \ No newline at end of file diff --git a/source/server/dnode/src/dnodeMsg.c b/source/server/dnode/src/dnodeMsg.c deleted file mode 100644 index 0a598527b4..0000000000 --- a/source/server/dnode/src/dnodeMsg.c +++ /dev/null @@ -1,174 +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 "dnodeDnode.h" -#include "dnodeConfig.h" -#include "mnode.h" -#include "tthread.h" -#include "ttime.h" -#include "vnode.h" - -static struct { - pthread_t *threadId; - bool stop; - uint32_t rebootTime; -} tsMsg; - -static void dnodeSendStatusMsg() { - int32_t contLen = sizeof(SStatusMsg) + TSDB_MAX_VNODES * sizeof(SVnodeLoad); - SStatusMsg *pStatus = rpcMallocCont(contLen); - if (pStatus == NULL) { - dError("failed to malloc status message"); - return; - } - - pStatus->version = htonl(tsVersion); - pStatus->dnodeId = htonl(dnodeGetDnodeId()); - tstrncpy(pStatus->dnodeEp, tsLocalEp, TSDB_EP_LEN); - pStatus->clusterId = htobe64(dnodeGetClusterId()); - pStatus->lastReboot = htonl(tsMsg.rebootTime); - pStatus->numOfCores = htonl(tsNumOfCores); - pStatus->diskAvailable = tsAvailDataDirGB; - - // fill cluster cfg parameters - pStatus->clusterCfg.statusInterval = htonl(tsStatusInterval); - pStatus->clusterCfg.checkTime = 0; - tstrncpy(pStatus->clusterCfg.timezone, tsTimezone, 64); - char timestr[32] = "1970-01-01 00:00:00.00"; - (void)taosParseTime(timestr, &pStatus->clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0); - tstrncpy(pStatus->clusterCfg.locale, tsLocale, TSDB_LOCALE_LEN); - tstrncpy(pStatus->clusterCfg.charset, tsCharset, TSDB_LOCALE_LEN); - - // vnodeGetStatus(NULL, pStatus); - // contLen = sizeof(SStatusMsg) + pStatus->openVnodes * sizeof(SVnodeLoad); - // pStatus->openVnodes = htons(pStatus->openVnodes); - - SRpcMsg rpcMsg = {.ahandle = NULL, .pCont = pStatus, .contLen = contLen, .msgType = TSDB_MSG_TYPE_DM_STATUS}; - - dnodeSendMsgToMnode(&rpcMsg); -} - -void dnodeProcessStatusRsp(SRpcMsg *pMsg) { - dTrace("status rsp is received, code:%s", tstrerror(pMsg->code)); - if (pMsg->code != TSDB_CODE_SUCCESS) return; - - SStatusRsp *pStatusRsp = pMsg->pCont; - - SDnodeCfg *pCfg = &pStatusRsp->dnodeCfg; - pCfg->dnodeId = htonl(pCfg->dnodeId); - pCfg->clusterId = htobe64(pCfg->clusterId); - pCfg->numOfVnodes = htonl(pCfg->numOfVnodes); - pCfg->numOfDnodes = htonl(pCfg->numOfDnodes); - dnodeUpdateCfg(pCfg); - - if (pCfg->dropped) { - dError("status rsp is received, and set dnode to drop status"); - return; - } - - // vnodeSetAccess(pStatusRsp->vgAccess, pCfg->numOfVnodes); - - SDnodeEps *eps = (SDnodeEps *)((char *)pStatusRsp->vgAccess + pCfg->numOfVnodes * sizeof(SVgroupAccess)); - eps->dnodeNum = htonl(eps->dnodeNum); - for (int32_t i = 0; i < eps->dnodeNum; ++i) { - eps->dnodeEps[i].dnodeId = htonl(eps->dnodeEps[i].dnodeId); - eps->dnodeEps[i].dnodePort = htons(eps->dnodeEps[i].dnodePort); - } - - dnodeUpdateDnodeEps(eps); -} - -static void *dnodeThreadRoutine(void *param) { - int32_t ms = tsStatusInterval * 1000; - while (!tsMsg.stop) { - taosMsleep(ms); - dnodeSendStatusMsg(); - } -} - -int32_t dnodeInitMsg() { - tsMsg.stop = false; - tsMsg.rebootTime = taosGetTimestampSec(); - tsMsg.threadId = taosCreateThread(dnodeThreadRoutine, NULL); - if (tsMsg.threadId == NULL) { - return -1; - } - - dInfo("dnode msg is initialized"); - return 0; -} - -void dnodeCleanupMsg() { - if (tsMsg.threadId != NULL) { - tsMsg.stop = true; - taosDestoryThread(tsMsg.threadId); - tsMsg.threadId = NULL; - } - - dInfo("dnode msg is cleanuped"); -} - -static int32_t dnodeStartMnode(SRpcMsg *pMsg) { - SCreateMnodeMsg *pCfg = pMsg->pCont; - pCfg->dnodeId = htonl(pCfg->dnodeId); - pCfg->mnodeNum = htonl(pCfg->mnodeNum); - for (int32_t i = 0; i < pCfg->mnodeNum; ++i) { - pCfg->mnodeEps[i].dnodeId = htonl(pCfg->mnodeEps[i].dnodeId); - pCfg->mnodeEps[i].dnodePort = htons(pCfg->mnodeEps[i].dnodePort); - } - - if (pCfg->dnodeId != dnodeGetDnodeId()) { - dDebug("dnode:%d, in create meps msg is not equal with saved dnodeId:%d", pCfg->dnodeId, dnodeGetDnodeId()); - return TSDB_CODE_MND_DNODE_ID_NOT_CONFIGURED; - } - - if (mnodeGetStatus() == MN_STATUS_READY) return 0; - - return mnodeDeploy(); -} - -void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg) { - int32_t code = dnodeStartMnode(pMsg); - - SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; - - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); -} - -void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg) { - SCfgDnodeMsg *pCfg = pMsg->pCont; - - int32_t code = taosCfgDynamicOptions(pCfg->config); - - SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; - - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); -} - -void dnodeProcessStartupReq(SRpcMsg *pMsg) { - dInfo("startup msg is received, cont:%s", (char *)pMsg->pCont); - - SStartupStep *pStep = rpcMallocCont(sizeof(SStartupStep)); - dnodeGetStartup(pStep); - - dDebug("startup msg is sent, step:%s desc:%s finished:%d", pStep->name, pStep->desc, pStep->finished); - - SRpcMsg rpcRsp = {.handle = pMsg->handle, .pCont = pStep, .contLen = sizeof(SStartupStep)}; - rpcSendResponse(&rpcRsp); - rpcFreeCont(pMsg->pCont); -} diff --git a/source/server/dnode/src/dnodeTrans.c b/source/server/dnode/src/dnodeTrans.c deleted file mode 100644 index a9e130cd58..0000000000 --- a/source/server/dnode/src/dnodeTrans.c +++ /dev/null @@ -1,380 +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 . - */ - -/* this file is mainly responsible for the communication between DNODEs. Each - * dnode works as both server and client. Dnode may send status, grant, config - * messages to mnode, mnode may send create/alter/drop table/vnode messages - * to dnode. All theses messages are handled from here - */ - -#define _DEFAULT_SOURCE -#include "dnodeTransport.h" -#include "dnodeConfig.h" -#include "dnodeDnode.h" -#include "mnode.h" -#include "vnode.h" - -typedef void (*MsgFp)(SRpcMsg *pMsg); - -static struct { - void *serverRpc; - void *clientRpc; - void *shellRpc; - MsgFp msgFp[TSDB_MSG_TYPE_MAX]; -} tsTrans; - -static void dnodeInitMsgFp() { - // msg from client to dnode - tsTrans.msgFp[TSDB_MSG_TYPE_SUBMIT] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_QUERY] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TABLE] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TABLE] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TABLE] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_TABLE_META] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_TABLES_META] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_QUERY] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONNECT] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_ACK] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_RESET] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_NETWORK_TEST] = dnodeProcessStartupReq; - - // msg from client to mnode - tsTrans.msgFp[TSDB_MSG_TYPE_CONNECT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_ACCT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_ACCT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_ACCT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_USER] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_USER] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_USER] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_DNODE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_DNODE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_DB] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_DB] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_USE_DB] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_DB] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_DB] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TOPIC] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TOPIC] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TOPIC] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_FUNCTION] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_FUNCTION] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_FUNCTION] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_KILL_QUERY] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_KILL_CONN] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_HEARTBEAT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SHOW] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE_FUNC] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE] = mnodeProcessMsg; - - // message from mnode to dnode - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN] = dnodeProcessCreateMnodeReq; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN] = NULL; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE_IN] = NULL; - tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE_IN_RSP] = mnodeProcessMsg; - - // message from dnode to mnode - tsTrans.msgFp[TSDB_MSG_TYPE_DM_AUTH] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DM_AUTH_RSP] = NULL; - tsTrans.msgFp[TSDB_MSG_TYPE_DM_GRANT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DM_GRANT_RSP] = NULL; - tsTrans.msgFp[TSDB_MSG_TYPE_DM_STATUS] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DM_STATUS_RSP] = dnodeProcessStatusRsp; -} - -static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0}; - int32_t msgType = pMsg->msgType; - - if (msgType == TSDB_MSG_TYPE_NETWORK_TEST) { - dnodeProcessStartupReq(pMsg); - return; - } - - if (dnodeGetRunStat() != DN_RUN_STAT_RUNNING) { - rspMsg.code = TSDB_CODE_APP_NOT_READY; - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); - dTrace("RPC %p, peer req:%s is ignored since dnode not running", pMsg->handle, taosMsg[msgType]); - return; - } - - if (pMsg->pCont == NULL) { - rspMsg.code = TSDB_CODE_DND_INVALID_MSG_LEN; - rpcSendResponse(&rspMsg); - return; - } - - MsgFp fp = tsTrans.msgFp[msgType]; - if (fp != NULL) { - dTrace("RPC %p, peer req:%s will be processed", pMsg->handle, taosMsg[msgType]); - (*fp)(pMsg); - } else { - dError("RPC %p, peer req:%s not processed", pMsg->handle, taosMsg[msgType]); - rspMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); - } -} - -static int32_t dnodeInitServer() { - SRpcInit rpcInit; - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = tsDnodeDnodePort; - rpcInit.label = "DND-S"; - rpcInit.numOfThreads = 1; - rpcInit.cfp = dnodeProcessPeerReq; - rpcInit.sessions = TSDB_MAX_VNODES << 4; - rpcInit.connType = TAOS_CONN_SERVER; - rpcInit.idleTime = tsShellActivityTimer * 1000; - - tsTrans.serverRpc = rpcOpen(&rpcInit); - if (tsTrans.serverRpc == NULL) { - dError("failed to init peer rpc server"); - return -1; - } - - dInfo("dnode peer rpc server is initialized"); - return 0; -} - -static void dnodeCleanupServer() { - if (tsTrans.serverRpc) { - rpcClose(tsTrans.serverRpc); - tsTrans.serverRpc = NULL; - dInfo("dnode peer server is closed"); - } -} - -static void dnodeProcessPeerRsp(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - int32_t msgType = pMsg->msgType; - - if (dnodeGetRunStat() == DN_RUN_STAT_STOPPED) { - if (pMsg == NULL || pMsg->pCont == NULL) return; - dTrace("RPC %p, peer rsp:%s is ignored since dnode is stopping", pMsg->handle, taosMsg[msgType]); - rpcFreeCont(pMsg->pCont); - return; - } - - if (msgType == TSDB_MSG_TYPE_DM_STATUS_RSP && pEpSet) { - dnodeUpdateMnodeEps(pEpSet); - } - - MsgFp fp = tsTrans.msgFp[msgType]; - if (fp != NULL) { - dTrace("RPC %p, peer rsp:%s will be processed", pMsg->handle, taosMsg[msgType]); - (*fp)(pMsg); - } else { - dDebug("RPC %p, peer rsp:%s not processed", pMsg->handle, taosMsg[msgType]); - } - - rpcFreeCont(pMsg->pCont); -} - -static int32_t dnodeInitClient() { - char secret[TSDB_KEY_LEN] = "secret"; - SRpcInit rpcInit; - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.label = "DND-C"; - rpcInit.numOfThreads = 1; - rpcInit.cfp = dnodeProcessPeerRsp; - rpcInit.sessions = TSDB_MAX_VNODES << 4; - rpcInit.connType = TAOS_CONN_CLIENT; - rpcInit.idleTime = tsShellActivityTimer * 1000; - rpcInit.user = "t"; - rpcInit.ckey = "key"; - rpcInit.secret = secret; - - tsTrans.clientRpc = rpcOpen(&rpcInit); - if (tsTrans.clientRpc == NULL) { - dError("failed to init peer rpc client"); - return -1; - } - - dInfo("dnode peer rpc client is initialized"); - return 0; -} - -static void dnodeCleanupClient() { - if (tsTrans.clientRpc) { - rpcClose(tsTrans.clientRpc); - tsTrans.clientRpc = NULL; - dInfo("dnode peer rpc client is closed"); - } -} - -static void dnodeProcessShellReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0}; - int32_t msgType = pMsg->msgType; - - if (dnodeGetRunStat() == DN_RUN_STAT_STOPPED) { - dError("RPC %p, shell req:%s is ignored since dnode exiting", pMsg->handle, taosMsg[msgType]); - rspMsg.code = TSDB_CODE_DND_EXITING; - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); - return; - } else if (dnodeGetRunStat() != DN_RUN_STAT_RUNNING) { - dError("RPC %p, shell req:%s is ignored since dnode not running", pMsg->handle, taosMsg[msgType]); - rspMsg.code = TSDB_CODE_APP_NOT_READY; - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); - return; - } - - if (pMsg->pCont == NULL) { - rspMsg.code = TSDB_CODE_DND_INVALID_MSG_LEN; - rpcSendResponse(&rspMsg); - return; - } - - MsgFp fp = tsTrans.msgFp[msgType]; - if (fp != NULL) { - dTrace("RPC %p, shell req:%s will be processed", pMsg->handle, taosMsg[msgType]); - (*fp)(pMsg); - } else { - dError("RPC %p, shell req:%s is not processed", pMsg->handle, taosMsg[msgType]); - rspMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); - } -} - -void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg) { rpcSendRequest(tsTrans.clientRpc, epSet, rpcMsg, NULL); } - -void dnodeSendMsgToMnode(SRpcMsg *rpcMsg) { - SRpcEpSet epSet = {0}; - dnodeGetEpSetForPeer(&epSet); - dnodeSendMsgToDnode(&epSet, rpcMsg); -} - -static void dnodeSendMsgToMnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp) { - SRpcEpSet epSet = {0}; - dnodeGetEpSetForPeer(&epSet); - rpcSendRecv(tsTrans.clientRpc, &epSet, rpcMsg, rpcRsp); -} - -static int32_t dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey) { - int32_t code = mnodeRetriveAuth(user, spi, encrypt, secret, ckey); - if (code != TSDB_CODE_APP_NOT_READY) return code; - - SAuthMsg *pMsg = rpcMallocCont(sizeof(SAuthMsg)); - tstrncpy(pMsg->user, user, sizeof(pMsg->user)); - - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pMsg; - rpcMsg.contLen = sizeof(SAuthMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_DM_AUTH; - - dDebug("user:%s, send auth msg to mnodes", user); - SRpcMsg rpcRsp = {0}; - dnodeSendMsgToMnodeRecv(&rpcMsg, &rpcRsp); - - if (rpcRsp.code != 0) { - dError("user:%s, auth msg received from mnodes, error:%s", user, tstrerror(rpcRsp.code)); - } else { - SAuthRsp *pRsp = rpcRsp.pCont; - dDebug("user:%s, auth msg received from mnodes", user); - memcpy(secret, pRsp->secret, TSDB_KEY_LEN); - memcpy(ckey, pRsp->ckey, TSDB_KEY_LEN); - *spi = pRsp->spi; - *encrypt = pRsp->encrypt; - } - - rpcFreeCont(rpcRsp.pCont); - return rpcRsp.code; -} - -static int32_t dnodeInitShell() { - int32_t numOfThreads = (int32_t)((tsNumOfCores * tsNumOfThreadsPerCore) / 2.0); - if (numOfThreads < 1) { - numOfThreads = 1; - } - - SRpcInit rpcInit; - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = tsDnodeShellPort; - rpcInit.label = "SHELL"; - rpcInit.numOfThreads = numOfThreads; - rpcInit.cfp = dnodeProcessShellReq; - rpcInit.sessions = tsMaxShellConns; - rpcInit.connType = TAOS_CONN_SERVER; - rpcInit.idleTime = tsShellActivityTimer * 1000; - rpcInit.afp = dnodeRetrieveUserAuthInfo; - - tsTrans.shellRpc = rpcOpen(&rpcInit); - if (tsTrans.shellRpc == NULL) { - dError("failed to init shell rpc server"); - return -1; - } - - dInfo("dnode shell rpc server is initialized"); - return 0; -} - -static void dnodeCleanupShell() { - if (tsTrans.shellRpc) { - rpcClose(tsTrans.shellRpc); - tsTrans.shellRpc = NULL; - } -} - -int32_t dnodeInitTrans() { - if (dnodeInitClient() != 0) { - return -1; - } - - if (dnodeInitServer() != 0) { - return -1; - } - - if (dnodeInitShell() != 0) { - return -1; - } - - return 0; -} - -void dnodeCleanupTrans() { - dnodeCleanupShell(); - dnodeCleanupServer(); - dnodeCleanupClient(); -} From 89a19fdbe19a9e4d11e938a6707e69a1d95248e4 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 1 Nov 2021 20:22:16 +0800 Subject: [PATCH 15/33] fix compile errors --- source/dnode/mgmt/inc/dnodeDnode.h | 4 ++-- source/dnode/mgmt/inc/dnodeVnodes.h | 4 ++-- source/dnode/mgmt/src/dnodeConfig.c | 2 +- source/dnode/mgmt/src/dnodeDnode.c | 26 +++++++++++++------------- source/dnode/mgmt/src/dnodeInt.c | 24 ++++++++++++------------ 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/source/dnode/mgmt/inc/dnodeDnode.h b/source/dnode/mgmt/inc/dnodeDnode.h index 35f68f0306..b126b39d1c 100644 --- a/source/dnode/mgmt/inc/dnodeDnode.h +++ b/source/dnode/mgmt/inc/dnodeDnode.h @@ -21,8 +21,8 @@ extern "C" { #endif #include "dnodeInt.h" -int32_t dnodeInitMsg(); -void dnodeCleanupMsg(); +int32_t dnodeInitDnode(); +void dnodeCleanupDnode(); void dnodeProcessStatusRsp(SRpcMsg *pMsg); void dnodeProcessStartupReq(SRpcMsg *pMsg); void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg); diff --git a/source/dnode/mgmt/inc/dnodeVnodes.h b/source/dnode/mgmt/inc/dnodeVnodes.h index d54fda654a..33848042bd 100644 --- a/source/dnode/mgmt/inc/dnodeVnodes.h +++ b/source/dnode/mgmt/inc/dnodeVnodes.h @@ -21,8 +21,8 @@ extern "C" { #endif #include "dnodeInt.h" -int32_t dnodeInitMsg(); -void dnodeCleanupMsg(); +int32_t dnodeInitDnode(); +void dnodeCleanupDnode(); void dnodeProcessStatusRsp(SRpcMsg *pMsg); void dnodeProcessStartupReq(SRpcMsg *pMsg); void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg); diff --git a/source/dnode/mgmt/src/dnodeConfig.c b/source/dnode/mgmt/src/dnodeConfig.c index d6189eb632..2a0374ffaf 100644 --- a/source/dnode/mgmt/src/dnodeConfig.c +++ b/source/dnode/mgmt/src/dnodeConfig.c @@ -30,7 +30,7 @@ static struct { pthread_mutex_t mutex; } tsConfig; -vstaticoid dnodeGetEpSetForPeer(SRpcEpSet *epSet) { +void dnodeGetEpSetForPeer(SRpcEpSet *epSet) { pthread_mutex_lock(&tsConfig.mutex); *epSet = tsConfig.mnodeEpSetForPeer; pthread_mutex_unlock(&tsConfig.mutex); diff --git a/source/dnode/mgmt/src/dnodeDnode.c b/source/dnode/mgmt/src/dnodeDnode.c index 0a598527b4..3c6a743e05 100644 --- a/source/dnode/mgmt/src/dnodeDnode.c +++ b/source/dnode/mgmt/src/dnodeDnode.c @@ -25,7 +25,7 @@ static struct { pthread_t *threadId; bool stop; uint32_t rebootTime; -} tsMsg; +} tsDnode; static void dnodeSendStatusMsg() { int32_t contLen = sizeof(SStatusMsg) + TSDB_MAX_VNODES * sizeof(SVnodeLoad); @@ -39,7 +39,7 @@ static void dnodeSendStatusMsg() { pStatus->dnodeId = htonl(dnodeGetDnodeId()); tstrncpy(pStatus->dnodeEp, tsLocalEp, TSDB_EP_LEN); pStatus->clusterId = htobe64(dnodeGetClusterId()); - pStatus->lastReboot = htonl(tsMsg.rebootTime); + pStatus->lastReboot = htonl(tsDnode.rebootTime); pStatus->numOfCores = htonl(tsNumOfCores); pStatus->diskAvailable = tsAvailDataDirGB; @@ -93,17 +93,17 @@ void dnodeProcessStatusRsp(SRpcMsg *pMsg) { static void *dnodeThreadRoutine(void *param) { int32_t ms = tsStatusInterval * 1000; - while (!tsMsg.stop) { + while (!tsDnode.stop) { taosMsleep(ms); dnodeSendStatusMsg(); } } -int32_t dnodeInitMsg() { - tsMsg.stop = false; - tsMsg.rebootTime = taosGetTimestampSec(); - tsMsg.threadId = taosCreateThread(dnodeThreadRoutine, NULL); - if (tsMsg.threadId == NULL) { +int32_t dnodeInitDnode() { + tsDnode.stop = false; + tsDnode.rebootTime = taosGetTimestampSec(); + tsDnode.threadId = taosCreateThread(dnodeThreadRoutine, NULL); + if (tsDnode.threadId == NULL) { return -1; } @@ -111,11 +111,11 @@ int32_t dnodeInitMsg() { return 0; } -void dnodeCleanupMsg() { - if (tsMsg.threadId != NULL) { - tsMsg.stop = true; - taosDestoryThread(tsMsg.threadId); - tsMsg.threadId = NULL; +void dnodeCleanupDnode() { + if (tsDnode.threadId != NULL) { + tsDnode.stop = true; + taosDestoryThread(tsDnode.threadId); + tsDnode.threadId = NULL; } dInfo("dnode msg is cleanuped"); diff --git a/source/dnode/mgmt/src/dnodeInt.c b/source/dnode/mgmt/src/dnodeInt.c index de1d03ae7a..6368fe95f7 100644 --- a/source/dnode/mgmt/src/dnodeInt.c +++ b/source/dnode/mgmt/src/dnodeInt.c @@ -31,27 +31,27 @@ static struct { EDnStat runStatus; SStartupStep startup; SSteps *steps; -} tsDnode; +} tsInt; -EDnStat dnodeGetRunStat() { return tsDnode.runStatus; } +EDnStat dnodeGetRunStat() { return tsInt.runStatus; } -void dnodeSetRunStat(EDnStat stat) { tsDnode.runStatus = stat; } +void dnodeSetRunStat(EDnStat stat) { tsInt.runStatus = stat; } void dnodeReportStartup(char *name, char *desc) { - SStartupStep *startup = &tsDnode.startup; + SStartupStep *startup = &tsInt.startup; tstrncpy(startup->name, name, strlen(startup->name)); tstrncpy(startup->desc, desc, strlen(startup->desc)); startup->finished = 0; } static void dnodeReportStartupFinished(char *name, char *desc) { - SStartupStep *startup = &tsDnode.startup; + SStartupStep *startup = &tsInt.startup; tstrncpy(startup->name, name, strlen(startup->name)); tstrncpy(startup->desc, desc, strlen(startup->desc)); startup->finished = 1; } -void dnodeGetStartup(SStartupStep *pStep) { memcpy(pStep, &tsDnode.startup, sizeof(SStartupStep)); } +void dnodeGetStartup(SStartupStep *pStep) { memcpy(pStep, &tsInt.startup, sizeof(SStartupStep)); } static int32_t dnodeInitVnode() { return vnodeInit(); @@ -72,7 +72,7 @@ static int32_t dnodeInitMnode() { static int32_t dnodeInitTfs() {} static int32_t dnodeInitMain() { - tsDnode.runStatus = DN_RUN_STAT_STOPPED; + tsInt.runStatus = DN_RUN_STAT_STOPPED; tscEmbedded = 1; taosIgnSIGPIPE(); taosBlockSIGPIPE(); @@ -175,10 +175,10 @@ int32_t dnodeInit() { taosStepAdd(steps, "dnode-vnode", dnodeInitVnode, vnodeCleanup); taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, mnodeCleanup); taosStepAdd(steps, "dnode-trans", dnodeInitTrans, dnodeCleanupTrans); - taosStepAdd(steps, "dnode-msg", dnodeInitMsg, dnodeCleanupMsg); + taosStepAdd(steps, "dnode-msg", dnodeInitDnode, dnodeCleanupDnode); - tsDnode.steps = steps; - taosStepExec(tsDnode.steps); + tsInt.steps = steps; + taosStepExec(tsInt.steps); dnodeSetRunStat(DN_RUN_STAT_RUNNING); dnodeReportStartupFinished("TDengine", "initialized successfully"); @@ -190,8 +190,8 @@ int32_t dnodeInit() { void dnodeCleanup() { if (dnodeGetRunStat() != DN_RUN_STAT_STOPPED) { dnodeSetRunStat(DN_RUN_STAT_STOPPED); - taosStepCleanup(tsDnode.steps); - tsDnode.steps = NULL; + taosStepCleanup(tsInt.steps); + tsInt.steps = NULL; } } From 7354b9e193ea2160d169209723c6a0c99b96d566 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 1 Nov 2021 20:38:50 +0800 Subject: [PATCH 16/33] minor changes --- source/dnode/mgmt/inc/dnodeInt.h | 1 - source/dnode/mgmt/inc/dnodeMnode.h | 3 +++ source/dnode/mgmt/inc/dnodeVnodes.h | 8 ++---- source/dnode/mgmt/src/dnodeInt.c | 42 ++++++----------------------- source/dnode/mgmt/src/dnodeMain.c | 6 ++--- source/dnode/mgmt/src/dnodeMnode.c | 34 +++++++++++++++++++++++ source/dnode/mgmt/src/dnodeVnodes.c | 22 +++++++++++++++ 7 files changed, 71 insertions(+), 45 deletions(-) create mode 100644 source/dnode/mgmt/src/dnodeMnode.c diff --git a/source/dnode/mgmt/inc/dnodeInt.h b/source/dnode/mgmt/inc/dnodeInt.h index 20257140d6..bccafcee85 100644 --- a/source/dnode/mgmt/inc/dnodeInt.h +++ b/source/dnode/mgmt/inc/dnodeInt.h @@ -24,7 +24,6 @@ extern "C" { #include "tglobal.h" #include "tlog.h" #include "trpc.h" -#include "ttimer.h" extern int32_t dDebugFlag; diff --git a/source/dnode/mgmt/inc/dnodeMnode.h b/source/dnode/mgmt/inc/dnodeMnode.h index 0dccb8c39d..00868535c1 100644 --- a/source/dnode/mgmt/inc/dnodeMnode.h +++ b/source/dnode/mgmt/inc/dnodeMnode.h @@ -21,6 +21,9 @@ extern "C" { #endif #include "dnodeInt.h" +int32_t dnodeInitMnode(); +void dnodeCleanupMnode(); + void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg); #ifdef __cplusplus diff --git a/source/dnode/mgmt/inc/dnodeVnodes.h b/source/dnode/mgmt/inc/dnodeVnodes.h index 33848042bd..fae1de7217 100644 --- a/source/dnode/mgmt/inc/dnodeVnodes.h +++ b/source/dnode/mgmt/inc/dnodeVnodes.h @@ -21,12 +21,8 @@ extern "C" { #endif #include "dnodeInt.h" -int32_t dnodeInitDnode(); -void dnodeCleanupDnode(); -void dnodeProcessStatusRsp(SRpcMsg *pMsg); -void dnodeProcessStartupReq(SRpcMsg *pMsg); -void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg); -void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg); +int32_t dnodeInitVnodes(); +void dnodeCleanupVnodes(); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/src/dnodeInt.c b/source/dnode/mgmt/src/dnodeInt.c index 6368fe95f7..df84896be7 100644 --- a/source/dnode/mgmt/src/dnodeInt.c +++ b/source/dnode/mgmt/src/dnodeInt.c @@ -17,14 +17,14 @@ #include "dnodeCheck.h" #include "dnodeConfig.h" #include "dnodeDnode.h" +#include "dnodeMnode.h" #include "dnodeTransport.h" -#include "mnode.h" +#include "dnodeVnodes.h" #include "sync.h" #include "tcache.h" #include "tconfig.h" #include "tnote.h" #include "tstep.h" -#include "vnode.h" #include "wal.h" static struct { @@ -37,7 +37,7 @@ EDnStat dnodeGetRunStat() { return tsInt.runStatus; } void dnodeSetRunStat(EDnStat stat) { tsInt.runStatus = stat; } -void dnodeReportStartup(char *name, char *desc) { +static void dnodeReportStartup(char *name, char *desc) { SStartupStep *startup = &tsInt.startup; tstrncpy(startup->name, name, strlen(startup->name)); tstrncpy(startup->desc, desc, strlen(startup->desc)); @@ -53,24 +53,6 @@ static void dnodeReportStartupFinished(char *name, char *desc) { void dnodeGetStartup(SStartupStep *pStep) { memcpy(pStep, &tsInt.startup, sizeof(SStartupStep)); } -static int32_t dnodeInitVnode() { - return vnodeInit(); -} - -static int32_t dnodeInitMnode() { - SMnodePara para; - para.fp.GetDnodeEp = dnodeGetEp; - para.fp.SendMsgToDnode = dnodeSendMsgToDnode; - para.fp.SendMsgToMnode = dnodeSendMsgToMnode; - para.fp.SendRedirectMsg = dnodeSendRedirectMsg; - para.dnodeId = dnodeGetDnodeId(); - para.clusterId = dnodeGetClusterId(); - - return mnodeInit(para); -} - -static int32_t dnodeInitTfs() {} - static int32_t dnodeInitMain() { tsInt.runStatus = DN_RUN_STAT_STOPPED; tscEmbedded = 1; @@ -168,14 +150,14 @@ int32_t dnodeInit() { taosStepAdd(steps, "dnode-dir", dnodeInitDir, dnodeCleanupDir); taosStepAdd(steps, "dnode-check", dnodeInitCheck, dnodeCleanupCheck); taosStepAdd(steps, "dnode-rpc", rpcInit, rpcCleanup); - taosStepAdd(steps, "dnode-tfs", dnodeInitTfs, NULL); + taosStepAdd(steps, "dnode-tfs", NULL, NULL); taosStepAdd(steps, "dnode-wal", walInit, walCleanUp); taosStepAdd(steps, "dnode-sync", syncInit, syncCleanUp); - taosStepAdd(steps, "dnode-eps", dnodeInitConfig, dnodeCleanupConfig); - taosStepAdd(steps, "dnode-vnode", dnodeInitVnode, vnodeCleanup); - taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, mnodeCleanup); + taosStepAdd(steps, "dnode-config", dnodeInitConfig, dnodeCleanupConfig); + taosStepAdd(steps, "dnode-vnodes", dnodeInitVnodes, dnodeCleanupVnodes); + taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, dnodeCleanupMnode); taosStepAdd(steps, "dnode-trans", dnodeInitTrans, dnodeCleanupTrans); - taosStepAdd(steps, "dnode-msg", dnodeInitDnode, dnodeCleanupDnode); + taosStepAdd(steps, "dnode-dnode", dnodeInitDnode, dnodeCleanupDnode); tsInt.steps = steps; taosStepExec(tsInt.steps); @@ -194,11 +176,3 @@ void dnodeCleanup() { tsInt.steps = NULL; } } - -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = vnodeProcessMgmtMsg; -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE] = vnodeProcessMgmtMsg; -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_SYNC_VNODE] = vnodeProcessMgmtMsg; -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_COMPACT_VNODE] = vnodeProcessMgmtMsg; -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_DROP_VNODE] = vnodeProcessMgmtMsg; -// tsVnode.msgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM] = vnodeProcessMgmtMsg; - \ No newline at end of file diff --git a/source/dnode/mgmt/src/dnodeMain.c b/source/dnode/mgmt/src/dnodeMain.c index 69960173f4..1fbeb1e732 100644 --- a/source/dnode/mgmt/src/dnodeMain.c +++ b/source/dnode/mgmt/src/dnodeMain.c @@ -12,8 +12,8 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#include "os.h" -#include "ulog.h" + +#define _DEFAULT_SOURCE #include "dnodeInt.h" static bool stop = false; @@ -35,8 +35,6 @@ int main(int argc, char const *argv[]) { exit(EXIT_FAILURE); } - dInfo("Started TDengine service successfully."); - while (!stop) { taosMsleep(100); } diff --git a/source/dnode/mgmt/src/dnodeMnode.c b/source/dnode/mgmt/src/dnodeMnode.c new file mode 100644 index 0000000000..47b1d89bb8 --- /dev/null +++ b/source/dnode/mgmt/src/dnodeMnode.c @@ -0,0 +1,34 @@ +/* + * 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 "dnodeMnode.h" +#include "dnodeConfig.h" +#include "dnodeTransport.h" +#include "mnode.h" + +int32_t dnodeInitMnode() { + SMnodePara para; + para.fp.GetDnodeEp = dnodeGetEp; + para.fp.SendMsgToDnode = dnodeSendMsgToDnode; + para.fp.SendMsgToMnode = dnodeSendMsgToMnode; + para.fp.SendRedirectMsg = dnodeSendRedirectMsg; + para.dnodeId = dnodeGetDnodeId(); + para.clusterId = dnodeGetClusterId(); + + return mnodeInit(para); +} + +void dnodeCleanupMnode() { mnodeCleanup(); } \ No newline at end of file diff --git a/source/dnode/mgmt/src/dnodeVnodes.c b/source/dnode/mgmt/src/dnodeVnodes.c index e69de29bb2..765bd6fc22 100644 --- a/source/dnode/mgmt/src/dnodeVnodes.c +++ b/source/dnode/mgmt/src/dnodeVnodes.c @@ -0,0 +1,22 @@ +/* + * 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 "dnodeDnode.h" +#include "vnode.h" + +int32_t dnodeInitVnodes() { return vnodeInit(); } + +void dnodeCleanupVnodes() { vnodeCleanup(); } \ No newline at end of file From da0a115c6e35e35d18703d714a39bfd67dcb341d Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 1 Nov 2021 20:51:34 +0800 Subject: [PATCH 17/33] rename files --- source/dnode/mgmt/inc/dnodeCheck.h | 31 -- source/dnode/mgmt/inc/dnodeConfig.h | 41 --- source/dnode/mgmt/inc/dnodeDnode.h | 14 + source/dnode/mgmt/src/dnodeCheck.c | 191 ----------- source/dnode/mgmt/src/dnodeConfig.c | 415 ----------------------- source/dnode/mgmt/src/dnodeDnode.c | 438 +++++++++++++++++++++++-- source/dnode/mgmt/src/dnodeInt.c | 93 +++--- source/dnode/mgmt/src/dnodeMnode.c | 32 +- source/dnode/mgmt/src/dnodeTransport.c | 1 - 9 files changed, 493 insertions(+), 763 deletions(-) delete mode 100644 source/dnode/mgmt/inc/dnodeCheck.h delete mode 100644 source/dnode/mgmt/inc/dnodeConfig.h delete mode 100644 source/dnode/mgmt/src/dnodeCheck.c delete mode 100644 source/dnode/mgmt/src/dnodeConfig.c diff --git a/source/dnode/mgmt/inc/dnodeCheck.h b/source/dnode/mgmt/inc/dnodeCheck.h deleted file mode 100644 index 97ac524b3f..0000000000 --- a/source/dnode/mgmt/inc/dnodeCheck.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_DNODE_CHECK_H_ -#define _TD_DNODE_CHECK_H_ - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitCheck(); -void dnodeCleanupCheck(); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_DNODE_CHECK_H_*/ diff --git a/source/dnode/mgmt/inc/dnodeConfig.h b/source/dnode/mgmt/inc/dnodeConfig.h deleted file mode 100644 index 71f3ac3f97..0000000000 --- a/source/dnode/mgmt/inc/dnodeConfig.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_DNODE_CONFIG_H_ -#define _TD_DNODE_CONFIG_H_ - -#ifdef __cplusplus -extern "C" { -#endif -#include "dnodeInt.h" - -int32_t dnodeInitConfig(); -void dnodeCleanupConfig(); - -void dnodeUpdateCfg(SDnodeCfg *data); -void dnodeUpdateDnodeEps(SDnodeEps *data); -void dnodeUpdateMnodeEps(SRpcEpSet *pEpSet); -int32_t dnodeGetDnodeId(); -int64_t dnodeGetClusterId(); -void dnodeGetEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port); - -void dnodeGetEpSetForPeer(SRpcEpSet *epSet); -void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_DNODE_CONFIG_H_*/ \ No newline at end of file diff --git a/source/dnode/mgmt/inc/dnodeDnode.h b/source/dnode/mgmt/inc/dnodeDnode.h index b126b39d1c..aba80752a2 100644 --- a/source/dnode/mgmt/inc/dnodeDnode.h +++ b/source/dnode/mgmt/inc/dnodeDnode.h @@ -27,6 +27,20 @@ void dnodeProcessStatusRsp(SRpcMsg *pMsg); void dnodeProcessStartupReq(SRpcMsg *pMsg); void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg); +int32_t dnodeInitConfig(); +void dnodeCleanupConfig(); + +void dnodeUpdateCfg(SDnodeCfg *data); +void dnodeUpdateDnodeEps(SDnodeEps *data); +void dnodeUpdateMnodeEps(SRpcEpSet *pEpSet); +int32_t dnodeGetDnodeId(); +int64_t dnodeGetClusterId(); +void dnodeGetEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port); + +void dnodeGetEpSetForPeer(SRpcEpSet *epSet); +void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell); + + #ifdef __cplusplus } #endif diff --git a/source/dnode/mgmt/src/dnodeCheck.c b/source/dnode/mgmt/src/dnodeCheck.c deleted file mode 100644 index b7bd930bb3..0000000000 --- a/source/dnode/mgmt/src/dnodeCheck.c +++ /dev/null @@ -1,191 +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 "dnodeCheck.h" - -#define MIN_AVAIL_MEMORY_MB 32 - -static int32_t dnodeBindTcpPort(uint16_t port) { -#if 0 - SOCKET serverSocket; - struct sockaddr_in server_addr; - - if ((serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { - dError("failed to create tcp socket since %s", strerror(errno)); - return -1; - } - - bzero(&server_addr, sizeof(server_addr)); - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(port); - server_addr.sin_addr.s_addr = htonl(INADDR_ANY); - - if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { - dError("failed to bind tcp port:%d since %s", port, strerror(errno)); - taosCloseSocket(serverSocket); - return -1; - } - - if (listen(serverSocket, 5) < 0) { - dError("failed to listen tcp port:%d since %s", port, strerror(errno)); - taosCloseSocket(serverSocket); - return -1; - } - - taosCloseSocket(serverSocket); -#endif - return 0; -} - -static int32_t dnodeBindUdpPort(int16_t port) { -#if 0 - SOCKET serverSocket; - struct sockaddr_in server_addr; - - if ((serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { - dError("failed to create udp socket since %s", strerror(errno)); - return -1; - } - - bzero(&server_addr, sizeof(server_addr)); - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(port); - server_addr.sin_addr.s_addr = htonl(INADDR_ANY); - - if (bind(serverSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { - dError("failed to bind udp port:%d since %s", port, strerror(errno)); - taosCloseSocket(serverSocket); - return -1; - } - - taosCloseSocket(serverSocket); -#endif - return 0; -} - -static int32_t dnodeCheckNetwork() { - int32_t ret; - uint16_t startPort = tsServerPort; - - for (uint16_t port = startPort; port < startPort + 12; port++) { - ret = dnodeBindTcpPort(port); - if (0 != ret) { - dError("failed to bind tcp port:%d", port); - return -1; - } - ret = dnodeBindUdpPort(port); - if (0 != ret) { - dError("failed to bind udp port:%d", port); - return -1; - } - } - - return 0; -} - -static int32_t dnodeCheckMem() { -#if 0 - float memoryUsedMB; - float memoryAvailMB; - if (true != taosGetSysMemory(&memoryUsedMB)) { - dError("failed to get system memory since %s, errno:%u,", strerror(errno), errno); - return -1; - } - - memoryAvailMB = (float)tsTotalMemoryMB - memoryUsedMB; - - if (memoryAvailMB < MIN_AVAIL_MEMORY_MB) { - dError("available memory %fMB less than the threshold %dMB", memoryAvailMB, MIN_AVAIL_MEMORY_MB); - return -1; - } -#endif - return 0; -} - -static int32_t dnodeCheckDisk() { -#if 0 - taosGetDisk(); - - if (tsAvailDataDirGB < tsMinimalDataDirGB) { - dError("dataDir disk size:%fGB less than threshold %fGB ", tsAvailDataDirGB, tsMinimalDataDirGB); - return -1; - } - - if (tsAvailLogDirGB < tsMinimalLogDirGB) { - dError("logDir disk size:%fGB less than threshold %fGB", tsAvailLogDirGB, tsMinimalLogDirGB); - return -1; - } - - if (tsAvailTmpDirectorySpace < tsReservedTmpDirectorySpace) { - dError("tmpDir disk size:%fGB less than threshold %fGB", tsAvailTmpDirectorySpace, tsReservedTmpDirectorySpace); - return -1; - } -#endif - return 0; -} - -static int32_t dnodeCheckCpu() { return 0; } -static int32_t dnodeCheckOs() { return 0; } -static int32_t dnodeCheckAccess() { return 0; } -static int32_t dnodeCheckVersion() { return 0; } -static int32_t dnodeCheckDatafile() { return 0; } - -int32_t dnodeInitCheck() { - if (dnodeCheckNetwork() != 0) { - dError("failed to check network"); - return -1; - } - - if (dnodeCheckMem() != 0) { - dError("failed to check memory"); - return -1; - } - - if (dnodeCheckCpu() != 0) { - dError("failed to check cpu"); - return -1; - } - - if (dnodeCheckDisk() != 0) { - dError("failed to check disk"); - return -1; - } - - if (dnodeCheckOs() != 0) { - dError("failed to check os"); - return -1; - } - - if (dnodeCheckAccess() != 0) { - dError("failed to check access"); - return -1; - } - - if (dnodeCheckVersion() != 0) { - dError("failed to check version"); - return -1; - } - - if (dnodeCheckDatafile() != 0) { - dError("failed to check datafile"); - return -1; - } - - dInfo("dnode check is finished"); - return 0; -} - -void dnodeCleanupCheck() {} \ No newline at end of file diff --git a/source/dnode/mgmt/src/dnodeConfig.c b/source/dnode/mgmt/src/dnodeConfig.c deleted file mode 100644 index 2a0374ffaf..0000000000 --- a/source/dnode/mgmt/src/dnodeConfig.c +++ /dev/null @@ -1,415 +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 "dnodeConfig.h" -#include "cJSON.h" -#include "thash.h" - -static struct { - int32_t dnodeId; - int32_t dropped; - int64_t clusterId; - SDnodeEps *dnodeEps; - SHashObj *dnodeHash; - SRpcEpSet mnodeEpSetForShell; - SRpcEpSet mnodeEpSetForPeer; - char file[PATH_MAX + 20]; - pthread_mutex_t mutex; -} tsConfig; - -void dnodeGetEpSetForPeer(SRpcEpSet *epSet) { - pthread_mutex_lock(&tsConfig.mutex); - *epSet = tsConfig.mnodeEpSetForPeer; - pthread_mutex_unlock(&tsConfig.mutex); -} - -static void dnodeGetEpSetForShell(SRpcEpSet *epSet) { - pthread_mutex_lock(&tsConfig.mutex); - *epSet = tsConfig.mnodeEpSetForShell; - pthread_mutex_unlock(&tsConfig.mutex); -} - -void dnodeUpdateMnodeEps(SRpcEpSet *ep) { - if (ep != NULL || ep->numOfEps <= 0) { - dError("mnode is changed, but content is invalid, discard it"); - return; - } - - pthread_mutex_lock(&tsConfig.mutex); - - dInfo("mnode is changed, num:%d use:%d", ep->numOfEps, ep->inUse); - - tsConfig.mnodeEpSetForPeer = *ep; - for (int32_t i = 0; i < ep->numOfEps; ++i) { - ep->port[i] -= TSDB_PORT_DNODEDNODE; - dInfo("mnode index:%d %s:%u", i, ep->fqdn[i], ep->port[i]); - } - tsConfig.mnodeEpSetForShell = *ep; - - pthread_mutex_unlock(&tsConfig.mutex); -} - -void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell) { - SRpcConnInfo connInfo = {0}; - rpcGetConnInfo(rpcMsg->handle, &connInfo); - - SRpcEpSet epSet = {0}; - if (forShell) { - dnodeGetEpSetForShell(&epSet); - } else { - dnodeGetEpSetForPeer(&epSet); - } - - dDebug("msg:%s will be redirected, num:%d use:%d", taosMsg[rpcMsg->msgType], epSet.numOfEps, epSet.inUse); - - for (int32_t i = 0; i < epSet.numOfEps; ++i) { - dDebug("mnode index:%d %s:%d", i, epSet.fqdn[i], epSet.port[i]); - if (strcmp(epSet.fqdn[i], tsLocalFqdn) == 0) { - if ((epSet.port[i] == tsServerPort + TSDB_PORT_DNODEDNODE && !forShell) || - (epSet.port[i] == tsServerPort && forShell)) { - epSet.inUse = (i + 1) % epSet.numOfEps; - dDebug("mnode index:%d %s:%d set inUse to %d", i, epSet.fqdn[i], epSet.port[i], epSet.inUse); - } - } - - epSet.port[i] = htons(epSet.port[i]); - } - - rpcSendRedirectRsp(rpcMsg->handle, &epSet); -} - -static void dnodePrintEps() { - dDebug("print dnode list, num:%d", tsConfig.dnodeEps->dnodeNum); - for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; i++) { - SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; - dDebug("dnode:%d, fqdn:%s port:%u isMnode:%d", ep->dnodeId, ep->dnodeFqdn, ep->dnodePort, ep->isMnode); - } -} - -static void dnodeResetEps(SDnodeEps *data) { - assert(data != NULL); - - int32_t size = sizeof(SDnodeEps) + data->dnodeNum * sizeof(SDnodeEp); - - if (data->dnodeNum > tsConfig.dnodeEps->dnodeNum) { - SDnodeEps *tmp = calloc(1, size); - if (tmp == NULL) return; - - tfree(tsConfig.dnodeEps); - tsConfig.dnodeEps = tmp; - } - - if (tsConfig.dnodeEps != data) { - memcpy(tsConfig.dnodeEps, data, size); - } - - tsConfig.mnodeEpSetForPeer.inUse = 0; - tsConfig.mnodeEpSetForShell.inUse = 0; - int32_t index = 0; - for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; i++) { - SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; - if (!ep->isMnode) continue; - if (index >= TSDB_MAX_REPLICA) continue; - strcpy(tsConfig.mnodeEpSetForShell.fqdn[index], ep->dnodeFqdn); - strcpy(tsConfig.mnodeEpSetForPeer.fqdn[index], ep->dnodeFqdn); - tsConfig.mnodeEpSetForShell.port[index] = ep->dnodePort; - tsConfig.mnodeEpSetForShell.port[index] = ep->dnodePort + tsDnodeDnodePort; - index++; - } - - for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; ++i) { - SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; - taosHashPut(tsConfig.dnodeHash, &ep->dnodeId, sizeof(int32_t), ep, sizeof(SDnodeEp)); - } - - dnodePrintEps(); -} - -static bool dnodeIsDnodeEpChanged(int32_t dnodeId, char *epstr) { - bool changed = false; - - pthread_mutex_lock(&tsConfig.mutex); - - SDnodeEp *ep = taosHashGet(tsConfig.dnodeHash, &dnodeId, sizeof(int32_t)); - if (ep != NULL) { - char epSaved[TSDB_EP_LEN + 1]; - snprintf(epSaved, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); - changed = strcmp(epstr, epSaved) != 0; - tstrncpy(epstr, epSaved, TSDB_EP_LEN); - } - - pthread_mutex_unlock(&tsConfig.mutex); - - return changed; -} - -static int32_t dnodeReadEps() { - int32_t len = 0; - int32_t maxLen = 30000; - char *content = calloc(1, maxLen + 1); - cJSON *root = NULL; - FILE *fp = NULL; - - fp = fopen(tsConfig.file, "r"); - if (!fp) { - dDebug("file %s not exist", tsConfig.file); - goto PRASE_EPS_OVER; - } - - len = (int32_t)fread(content, 1, maxLen, fp); - if (len <= 0) { - dError("failed to read %s since content is null", tsConfig.file); - goto PRASE_EPS_OVER; - } - - content[len] = 0; - root = cJSON_Parse(content); - if (root == NULL) { - dError("failed to read %s since invalid json format", tsConfig.file); - goto PRASE_EPS_OVER; - } - - cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId"); - if (!dnodeId || dnodeId->type != cJSON_String) { - dError("failed to read %s since dnodeId not found", tsConfig.file); - goto PRASE_EPS_OVER; - } - tsConfig.dnodeId = atoi(dnodeId->valuestring); - - cJSON *dropped = cJSON_GetObjectItem(root, "dropped"); - if (!dropped || dropped->type != cJSON_String) { - dError("failed to read %s since dropped not found", tsConfig.file); - goto PRASE_EPS_OVER; - } - tsConfig.dropped = atoi(dropped->valuestring); - - cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId"); - if (!clusterId || clusterId->type != cJSON_String) { - dError("failed to read %s since clusterId not found", tsConfig.file); - goto PRASE_EPS_OVER; - } - tsConfig.clusterId = atoll(clusterId->valuestring); - - cJSON *dnodeInfos = cJSON_GetObjectItem(root, "dnodeInfos"); - if (!dnodeInfos || dnodeInfos->type != cJSON_Array) { - dError("failed to read %s since dnodeInfos not found", tsConfig.file); - goto PRASE_EPS_OVER; - } - - int32_t dnodeInfosSize = cJSON_GetArraySize(dnodeInfos); - if (dnodeInfosSize <= 0) { - dError("failed to read %s since dnodeInfos size:%d invalid", tsConfig.file, dnodeInfosSize); - goto PRASE_EPS_OVER; - } - - tsConfig.dnodeEps = calloc(1, dnodeInfosSize * sizeof(SDnodeEp) + sizeof(SDnodeEps)); - if (tsConfig.dnodeEps == NULL) { - dError("failed to calloc dnodeEpList since %s", strerror(errno)); - goto PRASE_EPS_OVER; - } - tsConfig.dnodeEps->dnodeNum = dnodeInfosSize; - - for (int32_t i = 0; i < dnodeInfosSize; ++i) { - cJSON *dnodeInfo = cJSON_GetArrayItem(dnodeInfos, i); - if (dnodeInfo == NULL) break; - - SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; - - cJSON *dnodeId = cJSON_GetObjectItem(dnodeInfo, "dnodeId"); - if (!dnodeId || dnodeId->type != cJSON_String) { - dError("failed to read %s, dnodeId not found", tsConfig.file); - goto PRASE_EPS_OVER; - } - ep->dnodeId = atoi(dnodeId->valuestring); - - cJSON *isMnode = cJSON_GetObjectItem(dnodeInfo, "isMnode"); - if (!isMnode || isMnode->type != cJSON_String) { - dError("failed to read %s, isMnode not found", tsConfig.file); - goto PRASE_EPS_OVER; - } - ep->isMnode = atoi(isMnode->valuestring); - - cJSON *dnodeFqdn = cJSON_GetObjectItem(dnodeInfo, "dnodeFqdn"); - if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) { - dError("failed to read %s, dnodeFqdn not found", tsConfig.file); - goto PRASE_EPS_OVER; - } - tstrncpy(ep->dnodeFqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN); - - cJSON *dnodePort = cJSON_GetObjectItem(dnodeInfo, "dnodePort"); - if (!dnodePort || dnodePort->type != cJSON_String) { - dError("failed to read %s, dnodePort not found", tsConfig.file); - goto PRASE_EPS_OVER; - } - ep->dnodePort = atoi(dnodePort->valuestring); - } - - dInfo("succcessed to read file %s", tsConfig.file); - dnodePrintEps(); - -PRASE_EPS_OVER: - if (content != NULL) free(content); - if (root != NULL) cJSON_Delete(root); - if (fp != NULL) fclose(fp); - - if (dnodeIsDnodeEpChanged(tsConfig.dnodeId, tsLocalEp)) { - dError("dnode:%d, localEp %s different with dnodeEps.json and need reconfigured", tsConfig.dnodeId, tsLocalEp); - return -1; - } - - dnodeResetEps(tsConfig.dnodeEps); - - terrno = 0; - return 0; -} - -static int32_t dnodeWriteEps() { - FILE *fp = fopen(tsConfig.file, "w"); - if (!fp) { - dError("failed to write %s since %s", tsConfig.file, strerror(errno)); - return -1; - } - - int32_t len = 0; - int32_t maxLen = 30000; - char *content = calloc(1, maxLen + 1); - - len += snprintf(content + len, maxLen - len, "{\n"); - len += snprintf(content + len, maxLen - len, " \"dnodeId\": \"%d\",\n", tsConfig.dnodeId); - len += snprintf(content + len, maxLen - len, " \"dropped\": \"%d\",\n", tsConfig.dropped); - len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%" PRId64 "\",\n", tsConfig.clusterId); - len += snprintf(content + len, maxLen - len, " \"dnodeInfos\": [{\n"); - for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; ++i) { - SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; - len += snprintf(content + len, maxLen - len, " \"dnodeId\": \"%d\",\n", ep->dnodeId); - len += snprintf(content + len, maxLen - len, " \"isMnode\": \"%d\",\n", ep->isMnode); - len += snprintf(content + len, maxLen - len, " \"dnodeFqdn\": \"%s\",\n", ep->dnodeFqdn); - len += snprintf(content + len, maxLen - len, " \"dnodePort\": \"%u\"\n", ep->dnodePort); - if (i < tsConfig.dnodeEps->dnodeNum - 1) { - len += snprintf(content + len, maxLen - len, " },{\n"); - } else { - len += snprintf(content + len, maxLen - len, " }]\n"); - } - } - len += snprintf(content + len, maxLen - len, "}\n"); - - fwrite(content, 1, len, fp); - taosFsyncFile(fileno(fp)); - fclose(fp); - free(content); - terrno = 0; - - dInfo("successed to write %s", tsConfig.file); - return 0; -} - -int32_t dnodeInitConfig() { - tsConfig.dnodeId = 0; - tsConfig.dropped = 0; - tsConfig.clusterId = 0; - tsConfig.dnodeEps = NULL; - snprintf(tsConfig.file, sizeof(tsConfig.file), "%s/dnodeEps.json", tsDnodeDir); - pthread_mutex_init(&tsConfig.mutex, NULL); - - tsConfig.dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); - if (tsConfig.dnodeHash == NULL) return -1; - - int32_t ret = dnodeReadEps(); - if (ret == 0) { - dInfo("dnode eps is initialized"); - } - - return ret; -} - -void dnodeCleanupConfig() { - pthread_mutex_lock(&tsConfig.mutex); - - if (tsConfig.dnodeEps != NULL) { - free(tsConfig.dnodeEps); - tsConfig.dnodeEps = NULL; - } - - if (tsConfig.dnodeHash) { - taosHashCleanup(tsConfig.dnodeHash); - tsConfig.dnodeHash = NULL; - } - - pthread_mutex_unlock(&tsConfig.mutex); - pthread_mutex_destroy(&tsConfig.mutex); -} - -void dnodeUpdateDnodeEps(SDnodeEps *data) { - if (data == NULL || data->dnodeNum <= 0) return; - - pthread_mutex_lock(&tsConfig.mutex); - - if (data->dnodeNum != tsConfig.dnodeEps->dnodeNum) { - dnodeResetEps(data); - dnodeWriteEps(); - } else { - int32_t size = data->dnodeNum * sizeof(SDnodeEp) + sizeof(SDnodeEps); - if (memcmp(tsConfig.dnodeEps, data, size) != 0) { - dnodeResetEps(data); - dnodeWriteEps(); - } - } - - pthread_mutex_unlock(&tsConfig.mutex); -} - -void dnodeGetEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port) { - pthread_mutex_lock(&tsConfig.mutex); - - SDnodeEp *ep = taosHashGet(tsConfig.dnodeHash, &dnodeId, sizeof(int32_t)); - if (ep != NULL) { - if (port) *port = ep->dnodePort; - if (fqdn) tstrncpy(fqdn, ep->dnodeFqdn, TSDB_FQDN_LEN); - if (epstr) snprintf(epstr, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); - } - - pthread_mutex_unlock(&tsConfig.mutex); -} - -void dnodeUpdateCfg(SDnodeCfg *data) { - if (tsConfig.dnodeId != 0 && !data->dropped) return; - - pthread_mutex_lock(&tsConfig.mutex); - - tsConfig.dnodeId = data->dnodeId; - tsConfig.clusterId = data->clusterId; - tsConfig.dropped = data->dropped; - dInfo("dnodeId is set to %d, clusterId is set to %" PRId64, data->dnodeId, data->clusterId); - - dnodeWriteEps(); - pthread_mutex_unlock(&tsConfig.mutex); -} - -int32_t dnodeGetDnodeId() { - int32_t dnodeId = 0; - pthread_mutex_lock(&tsConfig.mutex); - dnodeId = tsConfig.dnodeId; - pthread_mutex_unlock(&tsConfig.mutex); - return dnodeId; -} - -int64_t dnodeGetClusterId() { - int64_t clusterId = 0; - pthread_mutex_lock(&tsConfig.mutex); - clusterId = tsConfig.clusterId; - pthread_mutex_unlock(&tsConfig.mutex); - return clusterId; -} diff --git a/source/dnode/mgmt/src/dnodeDnode.c b/source/dnode/mgmt/src/dnodeDnode.c index 3c6a743e05..722ef30099 100644 --- a/source/dnode/mgmt/src/dnodeDnode.c +++ b/source/dnode/mgmt/src/dnodeDnode.c @@ -15,15 +15,411 @@ #define _DEFAULT_SOURCE #include "dnodeDnode.h" -#include "dnodeConfig.h" -#include "mnode.h" +#include "dnodeTransport.h" #include "tthread.h" #include "ttime.h" -#include "vnode.h" +#include "cJSON.h" +#include "thash.h" + +static struct { + int32_t dnodeId; + int32_t dropped; + int64_t clusterId; + SDnodeEps *dnodeEps; + SHashObj *dnodeHash; + SRpcEpSet mnodeEpSetForShell; + SRpcEpSet mnodeEpSetForPeer; + char file[PATH_MAX + 20]; + pthread_mutex_t mutex; +} tsConfig; + +void dnodeGetEpSetForPeer(SRpcEpSet *epSet) { + pthread_mutex_lock(&tsConfig.mutex); + *epSet = tsConfig.mnodeEpSetForPeer; + pthread_mutex_unlock(&tsConfig.mutex); +} + +static void dnodeGetEpSetForShell(SRpcEpSet *epSet) { + pthread_mutex_lock(&tsConfig.mutex); + *epSet = tsConfig.mnodeEpSetForShell; + pthread_mutex_unlock(&tsConfig.mutex); +} + +void dnodeUpdateMnodeEps(SRpcEpSet *ep) { + if (ep != NULL || ep->numOfEps <= 0) { + dError("mnode is changed, but content is invalid, discard it"); + return; + } + + pthread_mutex_lock(&tsConfig.mutex); + + dInfo("mnode is changed, num:%d use:%d", ep->numOfEps, ep->inUse); + + tsConfig.mnodeEpSetForPeer = *ep; + for (int32_t i = 0; i < ep->numOfEps; ++i) { + ep->port[i] -= TSDB_PORT_DNODEDNODE; + dInfo("mnode index:%d %s:%u", i, ep->fqdn[i], ep->port[i]); + } + tsConfig.mnodeEpSetForShell = *ep; + + pthread_mutex_unlock(&tsConfig.mutex); +} + +void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell) { + SRpcConnInfo connInfo = {0}; + rpcGetConnInfo(rpcMsg->handle, &connInfo); + + SRpcEpSet epSet = {0}; + if (forShell) { + dnodeGetEpSetForShell(&epSet); + } else { + dnodeGetEpSetForPeer(&epSet); + } + + dDebug("msg:%s will be redirected, num:%d use:%d", taosMsg[rpcMsg->msgType], epSet.numOfEps, epSet.inUse); + + for (int32_t i = 0; i < epSet.numOfEps; ++i) { + dDebug("mnode index:%d %s:%d", i, epSet.fqdn[i], epSet.port[i]); + if (strcmp(epSet.fqdn[i], tsLocalFqdn) == 0) { + if ((epSet.port[i] == tsServerPort + TSDB_PORT_DNODEDNODE && !forShell) || + (epSet.port[i] == tsServerPort && forShell)) { + epSet.inUse = (i + 1) % epSet.numOfEps; + dDebug("mnode index:%d %s:%d set inUse to %d", i, epSet.fqdn[i], epSet.port[i], epSet.inUse); + } + } + + epSet.port[i] = htons(epSet.port[i]); + } + + rpcSendRedirectRsp(rpcMsg->handle, &epSet); +} + +static void dnodePrintEps() { + dDebug("print dnode list, num:%d", tsConfig.dnodeEps->dnodeNum); + for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; i++) { + SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; + dDebug("dnode:%d, fqdn:%s port:%u isMnode:%d", ep->dnodeId, ep->dnodeFqdn, ep->dnodePort, ep->isMnode); + } +} + +static void dnodeResetEps(SDnodeEps *data) { + assert(data != NULL); + + int32_t size = sizeof(SDnodeEps) + data->dnodeNum * sizeof(SDnodeEp); + + if (data->dnodeNum > tsConfig.dnodeEps->dnodeNum) { + SDnodeEps *tmp = calloc(1, size); + if (tmp == NULL) return; + + tfree(tsConfig.dnodeEps); + tsConfig.dnodeEps = tmp; + } + + if (tsConfig.dnodeEps != data) { + memcpy(tsConfig.dnodeEps, data, size); + } + + tsConfig.mnodeEpSetForPeer.inUse = 0; + tsConfig.mnodeEpSetForShell.inUse = 0; + int32_t index = 0; + for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; i++) { + SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; + if (!ep->isMnode) continue; + if (index >= TSDB_MAX_REPLICA) continue; + strcpy(tsConfig.mnodeEpSetForShell.fqdn[index], ep->dnodeFqdn); + strcpy(tsConfig.mnodeEpSetForPeer.fqdn[index], ep->dnodeFqdn); + tsConfig.mnodeEpSetForShell.port[index] = ep->dnodePort; + tsConfig.mnodeEpSetForShell.port[index] = ep->dnodePort + tsDnodeDnodePort; + index++; + } + + for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; ++i) { + SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; + taosHashPut(tsConfig.dnodeHash, &ep->dnodeId, sizeof(int32_t), ep, sizeof(SDnodeEp)); + } + + dnodePrintEps(); +} + +static bool dnodeIsDnodeEpChanged(int32_t dnodeId, char *epstr) { + bool changed = false; + + pthread_mutex_lock(&tsConfig.mutex); + + SDnodeEp *ep = taosHashGet(tsConfig.dnodeHash, &dnodeId, sizeof(int32_t)); + if (ep != NULL) { + char epSaved[TSDB_EP_LEN + 1]; + snprintf(epSaved, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); + changed = strcmp(epstr, epSaved) != 0; + tstrncpy(epstr, epSaved, TSDB_EP_LEN); + } + + pthread_mutex_unlock(&tsConfig.mutex); + + return changed; +} + +static int32_t dnodeReadEps() { + int32_t len = 0; + int32_t maxLen = 30000; + char *content = calloc(1, maxLen + 1); + cJSON *root = NULL; + FILE *fp = NULL; + + fp = fopen(tsConfig.file, "r"); + if (!fp) { + dDebug("file %s not exist", tsConfig.file); + goto PRASE_EPS_OVER; + } + + len = (int32_t)fread(content, 1, maxLen, fp); + if (len <= 0) { + dError("failed to read %s since content is null", tsConfig.file); + goto PRASE_EPS_OVER; + } + + content[len] = 0; + root = cJSON_Parse(content); + if (root == NULL) { + dError("failed to read %s since invalid json format", tsConfig.file); + goto PRASE_EPS_OVER; + } + + cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId"); + if (!dnodeId || dnodeId->type != cJSON_String) { + dError("failed to read %s since dnodeId not found", tsConfig.file); + goto PRASE_EPS_OVER; + } + tsConfig.dnodeId = atoi(dnodeId->valuestring); + + cJSON *dropped = cJSON_GetObjectItem(root, "dropped"); + if (!dropped || dropped->type != cJSON_String) { + dError("failed to read %s since dropped not found", tsConfig.file); + goto PRASE_EPS_OVER; + } + tsConfig.dropped = atoi(dropped->valuestring); + + cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId"); + if (!clusterId || clusterId->type != cJSON_String) { + dError("failed to read %s since clusterId not found", tsConfig.file); + goto PRASE_EPS_OVER; + } + tsConfig.clusterId = atoll(clusterId->valuestring); + + cJSON *dnodeInfos = cJSON_GetObjectItem(root, "dnodeInfos"); + if (!dnodeInfos || dnodeInfos->type != cJSON_Array) { + dError("failed to read %s since dnodeInfos not found", tsConfig.file); + goto PRASE_EPS_OVER; + } + + int32_t dnodeInfosSize = cJSON_GetArraySize(dnodeInfos); + if (dnodeInfosSize <= 0) { + dError("failed to read %s since dnodeInfos size:%d invalid", tsConfig.file, dnodeInfosSize); + goto PRASE_EPS_OVER; + } + + tsConfig.dnodeEps = calloc(1, dnodeInfosSize * sizeof(SDnodeEp) + sizeof(SDnodeEps)); + if (tsConfig.dnodeEps == NULL) { + dError("failed to calloc dnodeEpList since %s", strerror(errno)); + goto PRASE_EPS_OVER; + } + tsConfig.dnodeEps->dnodeNum = dnodeInfosSize; + + for (int32_t i = 0; i < dnodeInfosSize; ++i) { + cJSON *dnodeInfo = cJSON_GetArrayItem(dnodeInfos, i); + if (dnodeInfo == NULL) break; + + SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; + + cJSON *dnodeId = cJSON_GetObjectItem(dnodeInfo, "dnodeId"); + if (!dnodeId || dnodeId->type != cJSON_String) { + dError("failed to read %s, dnodeId not found", tsConfig.file); + goto PRASE_EPS_OVER; + } + ep->dnodeId = atoi(dnodeId->valuestring); + + cJSON *isMnode = cJSON_GetObjectItem(dnodeInfo, "isMnode"); + if (!isMnode || isMnode->type != cJSON_String) { + dError("failed to read %s, isMnode not found", tsConfig.file); + goto PRASE_EPS_OVER; + } + ep->isMnode = atoi(isMnode->valuestring); + + cJSON *dnodeFqdn = cJSON_GetObjectItem(dnodeInfo, "dnodeFqdn"); + if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) { + dError("failed to read %s, dnodeFqdn not found", tsConfig.file); + goto PRASE_EPS_OVER; + } + tstrncpy(ep->dnodeFqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN); + + cJSON *dnodePort = cJSON_GetObjectItem(dnodeInfo, "dnodePort"); + if (!dnodePort || dnodePort->type != cJSON_String) { + dError("failed to read %s, dnodePort not found", tsConfig.file); + goto PRASE_EPS_OVER; + } + ep->dnodePort = atoi(dnodePort->valuestring); + } + + dInfo("succcessed to read file %s", tsConfig.file); + dnodePrintEps(); + +PRASE_EPS_OVER: + if (content != NULL) free(content); + if (root != NULL) cJSON_Delete(root); + if (fp != NULL) fclose(fp); + + if (dnodeIsDnodeEpChanged(tsConfig.dnodeId, tsLocalEp)) { + dError("dnode:%d, localEp %s different with dnodeEps.json and need reconfigured", tsConfig.dnodeId, tsLocalEp); + return -1; + } + + dnodeResetEps(tsConfig.dnodeEps); + + terrno = 0; + return 0; +} + +static int32_t dnodeWriteEps() { + FILE *fp = fopen(tsConfig.file, "w"); + if (!fp) { + dError("failed to write %s since %s", tsConfig.file, strerror(errno)); + return -1; + } + + int32_t len = 0; + int32_t maxLen = 30000; + char *content = calloc(1, maxLen + 1); + + len += snprintf(content + len, maxLen - len, "{\n"); + len += snprintf(content + len, maxLen - len, " \"dnodeId\": \"%d\",\n", tsConfig.dnodeId); + len += snprintf(content + len, maxLen - len, " \"dropped\": \"%d\",\n", tsConfig.dropped); + len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%" PRId64 "\",\n", tsConfig.clusterId); + len += snprintf(content + len, maxLen - len, " \"dnodeInfos\": [{\n"); + for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; ++i) { + SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; + len += snprintf(content + len, maxLen - len, " \"dnodeId\": \"%d\",\n", ep->dnodeId); + len += snprintf(content + len, maxLen - len, " \"isMnode\": \"%d\",\n", ep->isMnode); + len += snprintf(content + len, maxLen - len, " \"dnodeFqdn\": \"%s\",\n", ep->dnodeFqdn); + len += snprintf(content + len, maxLen - len, " \"dnodePort\": \"%u\"\n", ep->dnodePort); + if (i < tsConfig.dnodeEps->dnodeNum - 1) { + len += snprintf(content + len, maxLen - len, " },{\n"); + } else { + len += snprintf(content + len, maxLen - len, " }]\n"); + } + } + len += snprintf(content + len, maxLen - len, "}\n"); + + fwrite(content, 1, len, fp); + taosFsyncFile(fileno(fp)); + fclose(fp); + free(content); + terrno = 0; + + dInfo("successed to write %s", tsConfig.file); + return 0; +} + +int32_t dnodeInitConfig() { + tsConfig.dnodeId = 0; + tsConfig.dropped = 0; + tsConfig.clusterId = 0; + tsConfig.dnodeEps = NULL; + snprintf(tsConfig.file, sizeof(tsConfig.file), "%s/dnodeEps.json", tsDnodeDir); + pthread_mutex_init(&tsConfig.mutex, NULL); + + tsConfig.dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); + if (tsConfig.dnodeHash == NULL) return -1; + + int32_t ret = dnodeReadEps(); + if (ret == 0) { + dInfo("dnode eps is initialized"); + } + + return ret; +} + +void dnodeCleanupConfig() { + pthread_mutex_lock(&tsConfig.mutex); + + if (tsConfig.dnodeEps != NULL) { + free(tsConfig.dnodeEps); + tsConfig.dnodeEps = NULL; + } + + if (tsConfig.dnodeHash) { + taosHashCleanup(tsConfig.dnodeHash); + tsConfig.dnodeHash = NULL; + } + + pthread_mutex_unlock(&tsConfig.mutex); + pthread_mutex_destroy(&tsConfig.mutex); +} + +void dnodeUpdateDnodeEps(SDnodeEps *data) { + if (data == NULL || data->dnodeNum <= 0) return; + + pthread_mutex_lock(&tsConfig.mutex); + + if (data->dnodeNum != tsConfig.dnodeEps->dnodeNum) { + dnodeResetEps(data); + dnodeWriteEps(); + } else { + int32_t size = data->dnodeNum * sizeof(SDnodeEp) + sizeof(SDnodeEps); + if (memcmp(tsConfig.dnodeEps, data, size) != 0) { + dnodeResetEps(data); + dnodeWriteEps(); + } + } + + pthread_mutex_unlock(&tsConfig.mutex); +} + +void dnodeGetEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port) { + pthread_mutex_lock(&tsConfig.mutex); + + SDnodeEp *ep = taosHashGet(tsConfig.dnodeHash, &dnodeId, sizeof(int32_t)); + if (ep != NULL) { + if (port) *port = ep->dnodePort; + if (fqdn) tstrncpy(fqdn, ep->dnodeFqdn, TSDB_FQDN_LEN); + if (epstr) snprintf(epstr, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); + } + + pthread_mutex_unlock(&tsConfig.mutex); +} + +void dnodeUpdateCfg(SDnodeCfg *data) { + if (tsConfig.dnodeId != 0 && !data->dropped) return; + + pthread_mutex_lock(&tsConfig.mutex); + + tsConfig.dnodeId = data->dnodeId; + tsConfig.clusterId = data->clusterId; + tsConfig.dropped = data->dropped; + dInfo("dnodeId is set to %d, clusterId is set to %" PRId64, data->dnodeId, data->clusterId); + + dnodeWriteEps(); + pthread_mutex_unlock(&tsConfig.mutex); +} + +int32_t dnodeGetDnodeId() { + int32_t dnodeId = 0; + pthread_mutex_lock(&tsConfig.mutex); + dnodeId = tsConfig.dnodeId; + pthread_mutex_unlock(&tsConfig.mutex); + return dnodeId; +} + +int64_t dnodeGetClusterId() { + int64_t clusterId = 0; + pthread_mutex_lock(&tsConfig.mutex); + clusterId = tsConfig.clusterId; + pthread_mutex_unlock(&tsConfig.mutex); + return clusterId; +} static struct { pthread_t *threadId; - bool stop; + bool threadStop; uint32_t rebootTime; } tsDnode; @@ -93,14 +489,14 @@ void dnodeProcessStatusRsp(SRpcMsg *pMsg) { static void *dnodeThreadRoutine(void *param) { int32_t ms = tsStatusInterval * 1000; - while (!tsDnode.stop) { + while (!tsDnode.threadStop) { taosMsleep(ms); dnodeSendStatusMsg(); } } int32_t dnodeInitDnode() { - tsDnode.stop = false; + tsDnode.threadStop = false; tsDnode.rebootTime = taosGetTimestampSec(); tsDnode.threadId = taosCreateThread(dnodeThreadRoutine, NULL); if (tsDnode.threadId == NULL) { @@ -113,7 +509,7 @@ int32_t dnodeInitDnode() { void dnodeCleanupDnode() { if (tsDnode.threadId != NULL) { - tsDnode.stop = true; + tsDnode.threadStop = true; taosDestoryThread(tsDnode.threadId); tsDnode.threadId = NULL; } @@ -121,34 +517,6 @@ void dnodeCleanupDnode() { dInfo("dnode msg is cleanuped"); } -static int32_t dnodeStartMnode(SRpcMsg *pMsg) { - SCreateMnodeMsg *pCfg = pMsg->pCont; - pCfg->dnodeId = htonl(pCfg->dnodeId); - pCfg->mnodeNum = htonl(pCfg->mnodeNum); - for (int32_t i = 0; i < pCfg->mnodeNum; ++i) { - pCfg->mnodeEps[i].dnodeId = htonl(pCfg->mnodeEps[i].dnodeId); - pCfg->mnodeEps[i].dnodePort = htons(pCfg->mnodeEps[i].dnodePort); - } - - if (pCfg->dnodeId != dnodeGetDnodeId()) { - dDebug("dnode:%d, in create meps msg is not equal with saved dnodeId:%d", pCfg->dnodeId, dnodeGetDnodeId()); - return TSDB_CODE_MND_DNODE_ID_NOT_CONFIGURED; - } - - if (mnodeGetStatus() == MN_STATUS_READY) return 0; - - return mnodeDeploy(); -} - -void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg) { - int32_t code = dnodeStartMnode(pMsg); - - SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; - - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); -} - void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg) { SCfgDnodeMsg *pCfg = pMsg->pCont; diff --git a/source/dnode/mgmt/src/dnodeInt.c b/source/dnode/mgmt/src/dnodeInt.c index df84896be7..a1e7bc1c7e 100644 --- a/source/dnode/mgmt/src/dnodeInt.c +++ b/source/dnode/mgmt/src/dnodeInt.c @@ -14,8 +14,6 @@ */ #define _DEFAULT_SOURCE -#include "dnodeCheck.h" -#include "dnodeConfig.h" #include "dnodeDnode.h" #include "dnodeMnode.h" #include "dnodeTransport.h" @@ -53,46 +51,6 @@ static void dnodeReportStartupFinished(char *name, char *desc) { void dnodeGetStartup(SStartupStep *pStep) { memcpy(pStep, &tsInt.startup, sizeof(SStartupStep)); } -static int32_t dnodeInitMain() { - tsInt.runStatus = DN_RUN_STAT_STOPPED; - tscEmbedded = 1; - taosIgnSIGPIPE(); - taosBlockSIGPIPE(); - taosResolveCRC(); - taosInitGlobalCfg(); - taosReadGlobalLogCfg(); - taosSetCoreDump(tsEnableCoreFile); - - if (!taosMkDir(tsLogDir)) { - printf("failed to create dir: %s, reason: %s\n", tsLogDir, strerror(errno)); - return -1; - } - - char temp[TSDB_FILENAME_LEN]; - sprintf(temp, "%s/taosdlog", tsLogDir); - if (taosInitLog(temp, tsNumOfLogLines, 1) < 0) { - printf("failed to init log file\n"); - } - - if (!taosReadGlobalCfg()) { - taosPrintGlobalCfg(); - dError("TDengine read global config failed"); - return -1; - } - - dInfo("start to initialize TDengine"); - - taosInitNotes(); - - return taosCheckGlobalCfg(); -} - -static void dnodeCleanupMain() { - taos_cleanup(); - taosCloseLog(); - taosStopCacheRefreshWorker(); -} - static int32_t dnodeCheckRunning(char *dir) { char filepath[256] = {0}; snprintf(filepath, sizeof(filepath), "%s/.running", dir); @@ -140,24 +98,65 @@ static int32_t dnodeInitDir() { return 0; } -static void dnodeCleanupDir() {} +static int32_t dnodeInitMain() { + tsInt.runStatus = DN_RUN_STAT_STOPPED; + tscEmbedded = 1; + taosIgnSIGPIPE(); + taosBlockSIGPIPE(); + taosResolveCRC(); + taosInitGlobalCfg(); + taosReadGlobalLogCfg(); + taosSetCoreDump(tsEnableCoreFile); + + if (!taosMkDir(tsLogDir)) { + printf("failed to create dir: %s, reason: %s\n", tsLogDir, strerror(errno)); + return -1; + } + + char temp[TSDB_FILENAME_LEN]; + sprintf(temp, "%s/taosdlog", tsLogDir); + if (taosInitLog(temp, tsNumOfLogLines, 1) < 0) { + printf("failed to init log file\n"); + } + + if (!taosReadGlobalCfg()) { + taosPrintGlobalCfg(); + dError("TDengine read global config failed"); + return -1; + } + + dInfo("start to initialize TDengine"); + + taosInitNotes(); + + if (taosCheckGlobalCfg() != 0) { + return -1; + } + + dnodeInitDir(); + + return -1; +} + +static void dnodeCleanupMain() { + taos_cleanup(); + taosCloseLog(); + taosStopCacheRefreshWorker(); +} int32_t dnodeInit() { SSteps *steps = taosStepInit(24, dnodeReportStartup); if (steps == NULL) return -1; taosStepAdd(steps, "dnode-main", dnodeInitMain, dnodeCleanupMain); - taosStepAdd(steps, "dnode-dir", dnodeInitDir, dnodeCleanupDir); - taosStepAdd(steps, "dnode-check", dnodeInitCheck, dnodeCleanupCheck); taosStepAdd(steps, "dnode-rpc", rpcInit, rpcCleanup); taosStepAdd(steps, "dnode-tfs", NULL, NULL); taosStepAdd(steps, "dnode-wal", walInit, walCleanUp); taosStepAdd(steps, "dnode-sync", syncInit, syncCleanUp); - taosStepAdd(steps, "dnode-config", dnodeInitConfig, dnodeCleanupConfig); + taosStepAdd(steps, "dnode-dnode", dnodeInitDnode, dnodeCleanupDnode); taosStepAdd(steps, "dnode-vnodes", dnodeInitVnodes, dnodeCleanupVnodes); taosStepAdd(steps, "dnode-mnode", dnodeInitMnode, dnodeCleanupMnode); taosStepAdd(steps, "dnode-trans", dnodeInitTrans, dnodeCleanupTrans); - taosStepAdd(steps, "dnode-dnode", dnodeInitDnode, dnodeCleanupDnode); tsInt.steps = steps; taosStepExec(tsInt.steps); diff --git a/source/dnode/mgmt/src/dnodeMnode.c b/source/dnode/mgmt/src/dnodeMnode.c index 47b1d89bb8..6019fbd1f6 100644 --- a/source/dnode/mgmt/src/dnodeMnode.c +++ b/source/dnode/mgmt/src/dnodeMnode.c @@ -15,7 +15,7 @@ #define _DEFAULT_SOURCE #include "dnodeMnode.h" -#include "dnodeConfig.h" +#include "dnodeDnode.h" #include "dnodeTransport.h" #include "mnode.h" @@ -31,4 +31,32 @@ int32_t dnodeInitMnode() { return mnodeInit(para); } -void dnodeCleanupMnode() { mnodeCleanup(); } \ No newline at end of file +void dnodeCleanupMnode() { mnodeCleanup(); } + +static int32_t dnodeStartMnode(SRpcMsg *pMsg) { + SCreateMnodeMsg *pCfg = pMsg->pCont; + pCfg->dnodeId = htonl(pCfg->dnodeId); + pCfg->mnodeNum = htonl(pCfg->mnodeNum); + for (int32_t i = 0; i < pCfg->mnodeNum; ++i) { + pCfg->mnodeEps[i].dnodeId = htonl(pCfg->mnodeEps[i].dnodeId); + pCfg->mnodeEps[i].dnodePort = htons(pCfg->mnodeEps[i].dnodePort); + } + + if (pCfg->dnodeId != dnodeGetDnodeId()) { + dDebug("dnode:%d, in create meps msg is not equal with saved dnodeId:%d", pCfg->dnodeId, dnodeGetDnodeId()); + return TSDB_CODE_MND_DNODE_ID_NOT_CONFIGURED; + } + + if (mnodeGetStatus() == MN_STATUS_READY) return 0; + + return mnodeDeploy(); +} + +void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg) { + int32_t code = dnodeStartMnode(pMsg); + + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; + + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); +} \ No newline at end of file diff --git a/source/dnode/mgmt/src/dnodeTransport.c b/source/dnode/mgmt/src/dnodeTransport.c index 68ec0a44e5..39747d4350 100644 --- a/source/dnode/mgmt/src/dnodeTransport.c +++ b/source/dnode/mgmt/src/dnodeTransport.c @@ -21,7 +21,6 @@ #define _DEFAULT_SOURCE #include "dnodeTransport.h" -#include "dnodeConfig.h" #include "dnodeDnode.h" #include "dnodeMnode.h" #include "dnodeVnodes.h" From 1284e413b0e306db0948789e4c7635045fbf1443 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 1 Nov 2021 20:53:46 +0800 Subject: [PATCH 18/33] minor changes --- source/dnode/mgmt/src/{dnodeMain.c => dnode.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename source/dnode/mgmt/src/{dnodeMain.c => dnode.c} (100%) diff --git a/source/dnode/mgmt/src/dnodeMain.c b/source/dnode/mgmt/src/dnode.c similarity index 100% rename from source/dnode/mgmt/src/dnodeMain.c rename to source/dnode/mgmt/src/dnode.c From 4690773668d69ef44ee4d2ed1c4c8e950165c6ba Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 2 Nov 2021 14:27:20 +0800 Subject: [PATCH 19/33] refact dnode-dnode file --- include/common/taosmsg.h | 143 +++---- include/common/tglobal.h | 2 +- include/server/vnode/vnode.h | 4 - source/common/src/tglobal.c | 10 +- source/dnode/mgmt/inc/dnodeDnode.h | 19 +- source/dnode/mgmt/inc/dnodeInt.h | 1 + source/dnode/mgmt/inc/dnodeMnode.h | 1 + source/dnode/mgmt/inc/dnodeVnodes.h | 2 + source/dnode/mgmt/src/dnodeDnode.c | 553 +++++++++++++------------ source/dnode/mgmt/src/dnodeMnode.c | 9 +- source/dnode/mgmt/src/dnodeTransport.c | 175 ++++---- source/dnode/mgmt/src/dnodeVnodes.c | 6 +- source/dnode/mnode/inc/mnodeDef.h | 2 +- source/dnode/mnode/src/mnodeWorker.c | 12 +- source/libs/transport/src/rpcMain.c | 2 +- 15 files changed, 462 insertions(+), 479 deletions(-) diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index dde737162b..c214c96f35 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -65,30 +65,6 @@ TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY7, "dummy7" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY8, "dummy8" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY9, "dummy9" ) -// message from mnode to dnode -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_STABLE_IN, "create-stable" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_STABLE_IN, "alter-stable" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_STABLE_IN, "drop-stable" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_VNODE_IN, "create-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_VNODE_IN, "alter-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_VNODE_IN, "drop-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SYNC_VNODE_IN, "sync-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_COMPACT_VNODE_IN, "compact-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_MNODE_IN, "create-mnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_MNODE_IN, "drop-mnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CONFIG_DNODE_IN, "config-dnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY10, "dummy10" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY11, "dummy11" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY12, "dummy12" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY13, "dummy13" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY14, "dummy14" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY15, "dummy15" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY16, "dummy16" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY17, "dummy17" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY18, "dummy18" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY19, "dummy19" ) - - // message from client to mnode TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CONNECT, "connect" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_ACCT, "create-acct" ) @@ -121,6 +97,29 @@ TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SHOW, "show" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SHOW_RETRIEVE, "retrieve" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SHOW_RETRIEVE_FUNC, "retrieve-func" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_COMPACT_VNODE, "compact-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY10, "dummy10" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY11, "dummy11" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY12, "dummy12" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY13, "dummy13" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY14, "dummy14" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY15, "dummy15" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY16, "dummy16" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY17, "dummy17" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY18, "dummy18" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY19, "dummy19" ) + +// message from mnode to dnode +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_STABLE_IN, "create-stable" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_STABLE_IN, "alter-stable" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_STABLE_IN, "drop-stable" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_VNODE_IN, "create-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_VNODE_IN, "alter-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_VNODE_IN, "drop-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SYNC_VNODE_IN, "sync-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_COMPACT_VNODE_IN, "compact-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_MNODE_IN, "create-mnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_MNODE_IN, "drop-mnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CONFIG_DNODE_IN, "config-dnode" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY20, "dummy20" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY21, "dummy21" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY22, "dummy22" ) @@ -133,9 +132,9 @@ TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY28, "dummy28" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY29, "dummy29" ) // message from dnode to mnode -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_STATUS, "status" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_GRANT, "grant" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DM_AUTH, "auth" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_STATUS, "status" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_GRANT, "grant" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_AUTH, "auth" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY30, "dummy30" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY31, "dummy31" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY32, "dummy32" ) @@ -585,20 +584,6 @@ typedef struct SRetrieveTableRsp { char data[]; } SRetrieveTableRsp; -typedef struct { - int32_t vgId; - int32_t dbCfgVersion; - int64_t totalStorage; - int64_t compStorage; - int64_t pointsWritten; - uint64_t vnodeVersion; - int32_t vgCfgVersion; - uint8_t status; - uint8_t role; - uint8_t replica; - uint8_t compact; -} SVnodeLoad; - typedef struct { char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; int32_t cacheBlockSize; //MB @@ -665,28 +650,47 @@ typedef struct { uint8_t ignoreNotExists; } SDropDbMsg, SUseDbMsg, SSyncDbMsg; -// IMPORTANT: sizeof(SVnodeStatisticInfo) should not exceed -// TSDB_FILE_HEADER_LEN/4 - TSDB_FILE_HEADER_VERSION_SIZE typedef struct { - int64_t pointsWritten; // In unit of points - int64_t totalStorage; // In unit of bytes - int64_t compStorage; // In unit of bytes - int64_t queryTime; // In unit of second ?? - char reserved[64]; -} SVnodeStatisticInfo; + int32_t statusInterval; + int8_t reserved[4]; + int64_t checkTime; // 1970-01-01 00:00:00.000 + char timezone[TSDB_TIMEZONE_LEN]; // tsTimezone + char locale[TSDB_LOCALE_LEN]; // tsLocale + char charset[TSDB_LOCALE_LEN]; // tsCharset +} SClusterCfg; -typedef struct SVgroupAccess { - int32_t vgId; - int8_t accessState; -} SVgroupAccess; +typedef struct { + int32_t vgId; + int8_t status; + int8_t role; + int8_t reserved[2]; + int64_t totalStorage; + int64_t compStorage; + int64_t pointsWritten; + int64_t tablesNum; +} SVnodeLoad; + +typedef struct { + int32_t vnodeNum; + SVnodeLoad vnodeLoads[]; +} SVnodeLoads; + +typedef struct SStatusMsg { + uint32_t sversion; + int32_t dnodeId; + int64_t clusterId; + uint32_t rebootTime; // time stamp for last reboot + int32_t numOfCores; + char dnodeEp[TSDB_EP_LEN]; + SClusterCfg clusterCfg; + SVnodeLoads vnodeLoads; +} SStatusMsg; typedef struct { int32_t dnodeId; int8_t dropped; - char reserved[19]; + char reserved[3]; int64_t clusterId; - int32_t numOfDnodes; - int32_t numOfVnodes; } SDnodeCfg; typedef struct { @@ -703,31 +707,8 @@ typedef struct { } SDnodeEps; typedef struct { - int32_t statusInterval; // tsStatusInterval - int8_t reserved[36]; - int64_t checkTime; // 1970-01-01 00:00:00.000 - char timezone[64]; // tsTimezone - char locale[TSDB_LOCALE_LEN]; // tsLocale - char charset[TSDB_LOCALE_LEN]; // tsCharset -} SClusterCfg; - -typedef struct SStatusMsg { - uint32_t version; - int32_t dnodeId; - uint32_t lastReboot; // time stamp for last reboot - int32_t openVnodes; - int32_t numOfCores; - float diskAvailable; - int8_t reserved[36]; - char dnodeEp[TSDB_EP_LEN]; - int64_t clusterId; - SClusterCfg clusterCfg; - SVnodeLoad load[]; -} SStatusMsg; - -typedef struct { - SDnodeCfg dnodeCfg; - SVgroupAccess vgAccess[]; + SDnodeCfg dnodeCfg; + SDnodeEps dnodeEps; } SStatusRsp; typedef struct { diff --git a/include/common/tglobal.h b/include/common/tglobal.h index 092f31cbde..f3fce8becd 100644 --- a/include/common/tglobal.h +++ b/include/common/tglobal.h @@ -198,7 +198,7 @@ extern SDiskCfg tsDiskCfg[]; void taosInitGlobalCfg(); int32_t taosCheckGlobalCfg(); -bool taosCfgDynamicOptions(char *msg); +int32_t taosCfgDynamicOptions(char *msg); int taosGetFqdnPortFromEp(const char *ep, char *fqdn, uint16_t *port); bool taosCheckBalanceCfgOptions(const char *option, int32_t *vnodeId, int32_t *dnodeId); void taosAddDataDir(int index, char *v1, int level, int primary); diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index 36112a2cf8..a9f40aad77 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -58,10 +58,6 @@ typedef struct { int8_t syncRole; } SVnodeStatus; -typedef struct { - int32_t accessState; -} SVnodeAccess; - typedef struct SVnodeMsg { int32_t msgType; int32_t code; diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index e74eb89aef..4f30327c06 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -277,13 +277,13 @@ void taosSetAllDebugFlag() { } } -bool taosCfgDynamicOptions(char *msg) { +int32_t taosCfgDynamicOptions(char *msg) { char *option, *value; int32_t olen, vlen; int32_t vint = 0; paGetToken(msg, &option, &olen); - if (olen == 0) return false;; + if (olen == 0) return -1;; paGetToken(option + olen + 1, &value, &vlen); if (vlen == 0) @@ -324,18 +324,18 @@ bool taosCfgDynamicOptions(char *msg) { uError("monitor can't be updated, for monitor not initialized"); } } - return true; + return 0; } if (strncasecmp(cfg->option, "debugFlag", olen) == 0) { taosSetAllDebugFlag(); } - return true; + return 0; } if (strncasecmp(option, "resetlog", 8) == 0) { taosResetLog(); taosPrintGlobalCfg(); - return true; + return 0; } if (strncasecmp(option, "resetQueryCache", 15) == 0) { diff --git a/source/dnode/mgmt/inc/dnodeDnode.h b/source/dnode/mgmt/inc/dnodeDnode.h index aba80752a2..9a2b564bd4 100644 --- a/source/dnode/mgmt/inc/dnodeDnode.h +++ b/source/dnode/mgmt/inc/dnodeDnode.h @@ -23,23 +23,14 @@ extern "C" { int32_t dnodeInitDnode(); void dnodeCleanupDnode(); -void dnodeProcessStatusRsp(SRpcMsg *pMsg); -void dnodeProcessStartupReq(SRpcMsg *pMsg); -void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg); +void dnodeProcessDnodeMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet); -int32_t dnodeInitConfig(); -void dnodeCleanupConfig(); - -void dnodeUpdateCfg(SDnodeCfg *data); -void dnodeUpdateDnodeEps(SDnodeEps *data); -void dnodeUpdateMnodeEps(SRpcEpSet *pEpSet); int32_t dnodeGetDnodeId(); int64_t dnodeGetClusterId(); -void dnodeGetEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port); - -void dnodeGetEpSetForPeer(SRpcEpSet *epSet); -void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell); - +void dnodeGetDnodeEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port); +void dnodeGetMnodeEpSetForPeer(SRpcEpSet *epSet); +void dnodeGetMnodeEpSetForShell(SRpcEpSet *epSet); +void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/inc/dnodeInt.h b/source/dnode/mgmt/inc/dnodeInt.h index bccafcee85..1306f2d700 100644 --- a/source/dnode/mgmt/inc/dnodeInt.h +++ b/source/dnode/mgmt/inc/dnodeInt.h @@ -35,6 +35,7 @@ extern int32_t dDebugFlag; #define dTrace(...) { if (dDebugFlag & DEBUG_TRACE) { taosPrintLog("DND ", dDebugFlag, __VA_ARGS__); }} typedef enum { DN_RUN_STAT_INIT, DN_RUN_STAT_RUNNING, DN_RUN_STAT_STOPPED } EDnStat; +typedef void (*MsgFp)(SRpcMsg *pMsg, SRpcEpSet *pEpSet); int32_t dnodeInit(); void dnodeCleanup(); diff --git a/source/dnode/mgmt/inc/dnodeMnode.h b/source/dnode/mgmt/inc/dnodeMnode.h index 00868535c1..e696acf425 100644 --- a/source/dnode/mgmt/inc/dnodeMnode.h +++ b/source/dnode/mgmt/inc/dnodeMnode.h @@ -23,6 +23,7 @@ extern "C" { int32_t dnodeInitMnode(); void dnodeCleanupMnode(); +void dnodeProcessMnodeMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet); void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg); diff --git a/source/dnode/mgmt/inc/dnodeVnodes.h b/source/dnode/mgmt/inc/dnodeVnodes.h index fae1de7217..46dcf549d4 100644 --- a/source/dnode/mgmt/inc/dnodeVnodes.h +++ b/source/dnode/mgmt/inc/dnodeVnodes.h @@ -23,6 +23,8 @@ extern "C" { int32_t dnodeInitVnodes(); void dnodeCleanupVnodes(); +void dnodeProcessVnodesMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet); +void dnodeGetVnodes(SVnodeLoads *pVloads); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/src/dnodeDnode.c b/source/dnode/mgmt/src/dnodeDnode.c index 722ef30099..64694e9a46 100644 --- a/source/dnode/mgmt/src/dnodeDnode.c +++ b/source/dnode/mgmt/src/dnodeDnode.c @@ -16,70 +16,83 @@ #define _DEFAULT_SOURCE #include "dnodeDnode.h" #include "dnodeTransport.h" -#include "tthread.h" -#include "ttime.h" +#include "dnodeVnodes.h" #include "cJSON.h" #include "thash.h" +#include "tthread.h" +#include "ttime.h" static struct { int32_t dnodeId; - int32_t dropped; int64_t clusterId; SDnodeEps *dnodeEps; SHashObj *dnodeHash; SRpcEpSet mnodeEpSetForShell; SRpcEpSet mnodeEpSetForPeer; char file[PATH_MAX + 20]; + uint32_t rebootTime; + int8_t dropped; + int8_t threadStop; + pthread_t *threadId; pthread_mutex_t mutex; -} tsConfig; + MsgFp msgFp[TSDB_MSG_TYPE_MAX]; +} tsDnode = {0}; -void dnodeGetEpSetForPeer(SRpcEpSet *epSet) { - pthread_mutex_lock(&tsConfig.mutex); - *epSet = tsConfig.mnodeEpSetForPeer; - pthread_mutex_unlock(&tsConfig.mutex); +int32_t dnodeGetDnodeId() { + int32_t dnodeId = 0; + pthread_mutex_lock(&tsDnode.mutex); + dnodeId = tsDnode.dnodeId; + pthread_mutex_unlock(&tsDnode.mutex); + return dnodeId; } -static void dnodeGetEpSetForShell(SRpcEpSet *epSet) { - pthread_mutex_lock(&tsConfig.mutex); - *epSet = tsConfig.mnodeEpSetForShell; - pthread_mutex_unlock(&tsConfig.mutex); +int64_t dnodeGetClusterId() { + int64_t clusterId = 0; + pthread_mutex_lock(&tsDnode.mutex); + clusterId = tsDnode.clusterId; + pthread_mutex_unlock(&tsDnode.mutex); + return clusterId; } -void dnodeUpdateMnodeEps(SRpcEpSet *ep) { - if (ep != NULL || ep->numOfEps <= 0) { - dError("mnode is changed, but content is invalid, discard it"); - return; +void dnodeGetDnodeEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port) { + pthread_mutex_lock(&tsDnode.mutex); + + SDnodeEp *pEp = taosHashGet(tsDnode.dnodeHash, &dnodeId, sizeof(int32_t)); + if (pEp != NULL) { + if (port) *port = pEp->dnodePort; + if (fqdn) tstrncpy(fqdn, pEp->dnodeFqdn, TSDB_FQDN_LEN); + if (ep) snprintf(ep, TSDB_EP_LEN, "%s:%u", pEp->dnodeFqdn, pEp->dnodePort); } - pthread_mutex_lock(&tsConfig.mutex); - - dInfo("mnode is changed, num:%d use:%d", ep->numOfEps, ep->inUse); - - tsConfig.mnodeEpSetForPeer = *ep; - for (int32_t i = 0; i < ep->numOfEps; ++i) { - ep->port[i] -= TSDB_PORT_DNODEDNODE; - dInfo("mnode index:%d %s:%u", i, ep->fqdn[i], ep->port[i]); - } - tsConfig.mnodeEpSetForShell = *ep; - - pthread_mutex_unlock(&tsConfig.mutex); + pthread_mutex_unlock(&tsDnode.mutex); } -void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell) { - SRpcConnInfo connInfo = {0}; - rpcGetConnInfo(rpcMsg->handle, &connInfo); +void dnodeGetMnodeEpSetForPeer(SRpcEpSet *pEpSet) { + pthread_mutex_lock(&tsDnode.mutex); + *pEpSet = tsDnode.mnodeEpSetForPeer; + pthread_mutex_unlock(&tsDnode.mutex); +} + +void dnodeGetMnodeEpSetForShell(SRpcEpSet *pEpSet) { + pthread_mutex_lock(&tsDnode.mutex); + *pEpSet = tsDnode.mnodeEpSetForShell; + pthread_mutex_unlock(&tsDnode.mutex); +} + +void dnodeSendRedirectMsg(SRpcMsg *pMsg, bool forShell) { + int32_t msgType = pMsg->msgType; SRpcEpSet epSet = {0}; if (forShell) { - dnodeGetEpSetForShell(&epSet); + dnodeGetMnodeEpSetForShell(&epSet); } else { - dnodeGetEpSetForPeer(&epSet); + dnodeGetMnodeEpSetForPeer(&epSet); } - dDebug("msg:%s will be redirected, num:%d use:%d", taosMsg[rpcMsg->msgType], epSet.numOfEps, epSet.inUse); + dDebug("RPC %p, msg:%s is redirected, num:%d use:%d", pMsg->handle, taosMsg[msgType], epSet.numOfEps, epSet.inUse); for (int32_t i = 0; i < epSet.numOfEps; ++i) { - dDebug("mnode index:%d %s:%d", i, epSet.fqdn[i], epSet.port[i]); + dDebug("mnode index:%d %s:%u", i, epSet.fqdn[i], epSet.port[i]); if (strcmp(epSet.fqdn[i], tsLocalFqdn) == 0) { if ((epSet.port[i] == tsServerPort + TSDB_PORT_DNODEDNODE && !forShell) || (epSet.port[i] == tsServerPort && forShell)) { @@ -91,71 +104,88 @@ void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell) { epSet.port[i] = htons(epSet.port[i]); } - rpcSendRedirectRsp(rpcMsg->handle, &epSet); + rpcSendRedirectRsp(pMsg->handle, &epSet); +} + +static void dnodeUpdateMnodeEpSet(SRpcEpSet *pEpSet) { + if (pEpSet == NULL || pEpSet->numOfEps <= 0) { + dError("mnode is changed, but content is invalid, discard it"); + return; + } else { + dInfo("mnode is changed, num:%d use:%d", pEpSet->numOfEps, pEpSet->inUse); + } + + pthread_mutex_lock(&tsDnode.mutex); + + tsDnode.mnodeEpSetForPeer = *pEpSet; + for (int32_t i = 0; i < pEpSet->numOfEps; ++i) { + pEpSet->port[i] -= TSDB_PORT_DNODEDNODE; + dInfo("mnode index:%d %s:%u", i, pEpSet->fqdn[i], pEpSet->port[i]); + } + tsDnode.mnodeEpSetForShell = *pEpSet; + + pthread_mutex_unlock(&tsDnode.mutex); } static void dnodePrintEps() { - dDebug("print dnode list, num:%d", tsConfig.dnodeEps->dnodeNum); - for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; i++) { - SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; + dDebug("print dnode endpoint list, num:%d", tsDnode.dnodeEps->dnodeNum); + for (int32_t i = 0; i < tsDnode.dnodeEps->dnodeNum; i++) { + SDnodeEp *ep = &tsDnode.dnodeEps->dnodeEps[i]; dDebug("dnode:%d, fqdn:%s port:%u isMnode:%d", ep->dnodeId, ep->dnodeFqdn, ep->dnodePort, ep->isMnode); } } -static void dnodeResetEps(SDnodeEps *data) { - assert(data != NULL); +static void dnodeResetEps(SDnodeEps *pEps) { + assert(pEps != NULL); + int32_t size = sizeof(SDnodeEps) + pEps->dnodeNum * sizeof(SDnodeEp); - int32_t size = sizeof(SDnodeEps) + data->dnodeNum * sizeof(SDnodeEp); - - if (data->dnodeNum > tsConfig.dnodeEps->dnodeNum) { + if (pEps->dnodeNum > tsDnode.dnodeEps->dnodeNum) { SDnodeEps *tmp = calloc(1, size); if (tmp == NULL) return; - tfree(tsConfig.dnodeEps); - tsConfig.dnodeEps = tmp; + tfree(tsDnode.dnodeEps); + tsDnode.dnodeEps = tmp; } - if (tsConfig.dnodeEps != data) { - memcpy(tsConfig.dnodeEps, data, size); + if (tsDnode.dnodeEps != pEps) { + memcpy(tsDnode.dnodeEps, pEps, size); } - tsConfig.mnodeEpSetForPeer.inUse = 0; - tsConfig.mnodeEpSetForShell.inUse = 0; - int32_t index = 0; - for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; i++) { - SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; + tsDnode.mnodeEpSetForPeer.inUse = 0; + tsDnode.mnodeEpSetForShell.inUse = 0; + + int32_t mIndex = 0; + for (int32_t i = 0; i < tsDnode.dnodeEps->dnodeNum; i++) { + SDnodeEp *ep = &tsDnode.dnodeEps->dnodeEps[i]; if (!ep->isMnode) continue; - if (index >= TSDB_MAX_REPLICA) continue; - strcpy(tsConfig.mnodeEpSetForShell.fqdn[index], ep->dnodeFqdn); - strcpy(tsConfig.mnodeEpSetForPeer.fqdn[index], ep->dnodeFqdn); - tsConfig.mnodeEpSetForShell.port[index] = ep->dnodePort; - tsConfig.mnodeEpSetForShell.port[index] = ep->dnodePort + tsDnodeDnodePort; - index++; + if (mIndex >= TSDB_MAX_REPLICA) continue; + strcpy(tsDnode.mnodeEpSetForShell.fqdn[mIndex], ep->dnodeFqdn); + strcpy(tsDnode.mnodeEpSetForPeer.fqdn[mIndex], ep->dnodeFqdn); + tsDnode.mnodeEpSetForShell.port[mIndex] = ep->dnodePort; + tsDnode.mnodeEpSetForShell.port[mIndex] = ep->dnodePort + tsDnodeDnodePort; + mIndex++; } - for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; ++i) { - SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; - taosHashPut(tsConfig.dnodeHash, &ep->dnodeId, sizeof(int32_t), ep, sizeof(SDnodeEp)); + for (int32_t i = 0; i < tsDnode.dnodeEps->dnodeNum; ++i) { + SDnodeEp *ep = &tsDnode.dnodeEps->dnodeEps[i]; + taosHashPut(tsDnode.dnodeHash, &ep->dnodeId, sizeof(int32_t), ep, sizeof(SDnodeEp)); } dnodePrintEps(); } -static bool dnodeIsDnodeEpChanged(int32_t dnodeId, char *epstr) { +static bool dnodeIsEpChanged(int32_t dnodeId, char *epStr) { bool changed = false; + pthread_mutex_lock(&tsDnode.mutex); - pthread_mutex_lock(&tsConfig.mutex); - - SDnodeEp *ep = taosHashGet(tsConfig.dnodeHash, &dnodeId, sizeof(int32_t)); - if (ep != NULL) { + SDnodeEp *pEp = taosHashGet(tsDnode.dnodeHash, &dnodeId, sizeof(int32_t)); + if (pEp != NULL) { char epSaved[TSDB_EP_LEN + 1]; - snprintf(epSaved, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); - changed = strcmp(epstr, epSaved) != 0; - tstrncpy(epstr, epSaved, TSDB_EP_LEN); + snprintf(epSaved, TSDB_EP_LEN, "%s:%u", pEp->dnodeFqdn, pEp->dnodePort); + changed = strcmp(epStr, epSaved) != 0; } - pthread_mutex_unlock(&tsConfig.mutex); - + pthread_mutex_unlock(&tsDnode.mutex); return changed; } @@ -166,101 +196,101 @@ static int32_t dnodeReadEps() { cJSON *root = NULL; FILE *fp = NULL; - fp = fopen(tsConfig.file, "r"); + fp = fopen(tsDnode.file, "r"); if (!fp) { - dDebug("file %s not exist", tsConfig.file); + dDebug("file %s not exist", tsDnode.file); goto PRASE_EPS_OVER; } len = (int32_t)fread(content, 1, maxLen, fp); if (len <= 0) { - dError("failed to read %s since content is null", tsConfig.file); + dError("failed to read %s since content is null", tsDnode.file); goto PRASE_EPS_OVER; } content[len] = 0; root = cJSON_Parse(content); if (root == NULL) { - dError("failed to read %s since invalid json format", tsConfig.file); + dError("failed to read %s since invalid json format", tsDnode.file); goto PRASE_EPS_OVER; } cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId"); if (!dnodeId || dnodeId->type != cJSON_String) { - dError("failed to read %s since dnodeId not found", tsConfig.file); + dError("failed to read %s since dnodeId not found", tsDnode.file); goto PRASE_EPS_OVER; } - tsConfig.dnodeId = atoi(dnodeId->valuestring); - - cJSON *dropped = cJSON_GetObjectItem(root, "dropped"); - if (!dropped || dropped->type != cJSON_String) { - dError("failed to read %s since dropped not found", tsConfig.file); - goto PRASE_EPS_OVER; - } - tsConfig.dropped = atoi(dropped->valuestring); + tsDnode.dnodeId = atoi(dnodeId->valuestring); cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId"); if (!clusterId || clusterId->type != cJSON_String) { - dError("failed to read %s since clusterId not found", tsConfig.file); + dError("failed to read %s since clusterId not found", tsDnode.file); goto PRASE_EPS_OVER; } - tsConfig.clusterId = atoll(clusterId->valuestring); + tsDnode.clusterId = atoll(clusterId->valuestring); + + cJSON *dropped = cJSON_GetObjectItem(root, "dropped"); + if (!dropped || dropped->type != cJSON_String) { + dError("failed to read %s since dropped not found", tsDnode.file); + goto PRASE_EPS_OVER; + } + tsDnode.dropped = atoi(dropped->valuestring); cJSON *dnodeInfos = cJSON_GetObjectItem(root, "dnodeInfos"); if (!dnodeInfos || dnodeInfos->type != cJSON_Array) { - dError("failed to read %s since dnodeInfos not found", tsConfig.file); + dError("failed to read %s since dnodeInfos not found", tsDnode.file); goto PRASE_EPS_OVER; } int32_t dnodeInfosSize = cJSON_GetArraySize(dnodeInfos); if (dnodeInfosSize <= 0) { - dError("failed to read %s since dnodeInfos size:%d invalid", tsConfig.file, dnodeInfosSize); + dError("failed to read %s since dnodeInfos size:%d invalid", tsDnode.file, dnodeInfosSize); goto PRASE_EPS_OVER; } - tsConfig.dnodeEps = calloc(1, dnodeInfosSize * sizeof(SDnodeEp) + sizeof(SDnodeEps)); - if (tsConfig.dnodeEps == NULL) { + tsDnode.dnodeEps = calloc(1, dnodeInfosSize * sizeof(SDnodeEp) + sizeof(SDnodeEps)); + if (tsDnode.dnodeEps == NULL) { dError("failed to calloc dnodeEpList since %s", strerror(errno)); goto PRASE_EPS_OVER; } - tsConfig.dnodeEps->dnodeNum = dnodeInfosSize; + tsDnode.dnodeEps->dnodeNum = dnodeInfosSize; for (int32_t i = 0; i < dnodeInfosSize; ++i) { cJSON *dnodeInfo = cJSON_GetArrayItem(dnodeInfos, i); if (dnodeInfo == NULL) break; - SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; + SDnodeEp *pEp = &tsDnode.dnodeEps->dnodeEps[i]; cJSON *dnodeId = cJSON_GetObjectItem(dnodeInfo, "dnodeId"); if (!dnodeId || dnodeId->type != cJSON_String) { - dError("failed to read %s, dnodeId not found", tsConfig.file); + dError("failed to read %s, dnodeId not found", tsDnode.file); goto PRASE_EPS_OVER; } - ep->dnodeId = atoi(dnodeId->valuestring); + pEp->dnodeId = atoi(dnodeId->valuestring); cJSON *isMnode = cJSON_GetObjectItem(dnodeInfo, "isMnode"); if (!isMnode || isMnode->type != cJSON_String) { - dError("failed to read %s, isMnode not found", tsConfig.file); + dError("failed to read %s, isMnode not found", tsDnode.file); goto PRASE_EPS_OVER; } - ep->isMnode = atoi(isMnode->valuestring); + pEp->isMnode = atoi(isMnode->valuestring); cJSON *dnodeFqdn = cJSON_GetObjectItem(dnodeInfo, "dnodeFqdn"); if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) { - dError("failed to read %s, dnodeFqdn not found", tsConfig.file); + dError("failed to read %s, dnodeFqdn not found", tsDnode.file); goto PRASE_EPS_OVER; } - tstrncpy(ep->dnodeFqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN); + tstrncpy(pEp->dnodeFqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN); cJSON *dnodePort = cJSON_GetObjectItem(dnodeInfo, "dnodePort"); if (!dnodePort || dnodePort->type != cJSON_String) { - dError("failed to read %s, dnodePort not found", tsConfig.file); + dError("failed to read %s, dnodePort not found", tsDnode.file); goto PRASE_EPS_OVER; } - ep->dnodePort = atoi(dnodePort->valuestring); + pEp->dnodePort = atoi(dnodePort->valuestring); } - dInfo("succcessed to read file %s", tsConfig.file); + dInfo("succcessed to read file %s", tsDnode.file); dnodePrintEps(); PRASE_EPS_OVER: @@ -268,21 +298,21 @@ PRASE_EPS_OVER: if (root != NULL) cJSON_Delete(root); if (fp != NULL) fclose(fp); - if (dnodeIsDnodeEpChanged(tsConfig.dnodeId, tsLocalEp)) { - dError("dnode:%d, localEp %s different with dnodeEps.json and need reconfigured", tsConfig.dnodeId, tsLocalEp); + if (dnodeIsEpChanged(tsDnode.dnodeId, tsLocalEp)) { + dError("localEp %s different with %s and need reconfigured", tsLocalEp, tsDnode.file); return -1; } - dnodeResetEps(tsConfig.dnodeEps); + dnodeResetEps(tsDnode.dnodeEps); terrno = 0; return 0; } static int32_t dnodeWriteEps() { - FILE *fp = fopen(tsConfig.file, "w"); + FILE *fp = fopen(tsDnode.file, "w"); if (!fp) { - dError("failed to write %s since %s", tsConfig.file, strerror(errno)); + dError("failed to write %s since %s", tsDnode.file, strerror(errno)); return -1; } @@ -291,17 +321,17 @@ static int32_t dnodeWriteEps() { char *content = calloc(1, maxLen + 1); len += snprintf(content + len, maxLen - len, "{\n"); - len += snprintf(content + len, maxLen - len, " \"dnodeId\": \"%d\",\n", tsConfig.dnodeId); - len += snprintf(content + len, maxLen - len, " \"dropped\": \"%d\",\n", tsConfig.dropped); - len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%" PRId64 "\",\n", tsConfig.clusterId); + len += snprintf(content + len, maxLen - len, " \"dnodeId\": \"%d\",\n", tsDnode.dnodeId); + len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%" PRId64 "\",\n", tsDnode.clusterId); + len += snprintf(content + len, maxLen - len, " \"dropped\": \"%d\",\n", tsDnode.dropped); len += snprintf(content + len, maxLen - len, " \"dnodeInfos\": [{\n"); - for (int32_t i = 0; i < tsConfig.dnodeEps->dnodeNum; ++i) { - SDnodeEp *ep = &tsConfig.dnodeEps->dnodeEps[i]; + for (int32_t i = 0; i < tsDnode.dnodeEps->dnodeNum; ++i) { + SDnodeEp *ep = &tsDnode.dnodeEps->dnodeEps[i]; len += snprintf(content + len, maxLen - len, " \"dnodeId\": \"%d\",\n", ep->dnodeId); len += snprintf(content + len, maxLen - len, " \"isMnode\": \"%d\",\n", ep->isMnode); len += snprintf(content + len, maxLen - len, " \"dnodeFqdn\": \"%s\",\n", ep->dnodeFqdn); len += snprintf(content + len, maxLen - len, " \"dnodePort\": \"%u\"\n", ep->dnodePort); - if (i < tsConfig.dnodeEps->dnodeNum - 1) { + if (i < tsDnode.dnodeEps->dnodeNum - 1) { len += snprintf(content + len, maxLen - len, " },{\n"); } else { len += snprintf(content + len, maxLen - len, " }]\n"); @@ -315,150 +345,76 @@ static int32_t dnodeWriteEps() { free(content); terrno = 0; - dInfo("successed to write %s", tsConfig.file); + dInfo("successed to write %s", tsDnode.file); return 0; } -int32_t dnodeInitConfig() { - tsConfig.dnodeId = 0; - tsConfig.dropped = 0; - tsConfig.clusterId = 0; - tsConfig.dnodeEps = NULL; - snprintf(tsConfig.file, sizeof(tsConfig.file), "%s/dnodeEps.json", tsDnodeDir); - pthread_mutex_init(&tsConfig.mutex, NULL); - - tsConfig.dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); - if (tsConfig.dnodeHash == NULL) return -1; - - int32_t ret = dnodeReadEps(); - if (ret == 0) { - dInfo("dnode eps is initialized"); - } - - return ret; -} - -void dnodeCleanupConfig() { - pthread_mutex_lock(&tsConfig.mutex); - - if (tsConfig.dnodeEps != NULL) { - free(tsConfig.dnodeEps); - tsConfig.dnodeEps = NULL; - } - - if (tsConfig.dnodeHash) { - taosHashCleanup(tsConfig.dnodeHash); - tsConfig.dnodeHash = NULL; - } - - pthread_mutex_unlock(&tsConfig.mutex); - pthread_mutex_destroy(&tsConfig.mutex); -} - -void dnodeUpdateDnodeEps(SDnodeEps *data) { - if (data == NULL || data->dnodeNum <= 0) return; - - pthread_mutex_lock(&tsConfig.mutex); - - if (data->dnodeNum != tsConfig.dnodeEps->dnodeNum) { - dnodeResetEps(data); - dnodeWriteEps(); - } else { - int32_t size = data->dnodeNum * sizeof(SDnodeEp) + sizeof(SDnodeEps); - if (memcmp(tsConfig.dnodeEps, data, size) != 0) { - dnodeResetEps(data); - dnodeWriteEps(); - } - } - - pthread_mutex_unlock(&tsConfig.mutex); -} - -void dnodeGetEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port) { - pthread_mutex_lock(&tsConfig.mutex); - - SDnodeEp *ep = taosHashGet(tsConfig.dnodeHash, &dnodeId, sizeof(int32_t)); - if (ep != NULL) { - if (port) *port = ep->dnodePort; - if (fqdn) tstrncpy(fqdn, ep->dnodeFqdn, TSDB_FQDN_LEN); - if (epstr) snprintf(epstr, TSDB_EP_LEN, "%s:%u", ep->dnodeFqdn, ep->dnodePort); - } - - pthread_mutex_unlock(&tsConfig.mutex); -} - -void dnodeUpdateCfg(SDnodeCfg *data) { - if (tsConfig.dnodeId != 0 && !data->dropped) return; - - pthread_mutex_lock(&tsConfig.mutex); - - tsConfig.dnodeId = data->dnodeId; - tsConfig.clusterId = data->clusterId; - tsConfig.dropped = data->dropped; - dInfo("dnodeId is set to %d, clusterId is set to %" PRId64, data->dnodeId, data->clusterId); - - dnodeWriteEps(); - pthread_mutex_unlock(&tsConfig.mutex); -} - -int32_t dnodeGetDnodeId() { - int32_t dnodeId = 0; - pthread_mutex_lock(&tsConfig.mutex); - dnodeId = tsConfig.dnodeId; - pthread_mutex_unlock(&tsConfig.mutex); - return dnodeId; -} - -int64_t dnodeGetClusterId() { - int64_t clusterId = 0; - pthread_mutex_lock(&tsConfig.mutex); - clusterId = tsConfig.clusterId; - pthread_mutex_unlock(&tsConfig.mutex); - return clusterId; -} - -static struct { - pthread_t *threadId; - bool threadStop; - uint32_t rebootTime; -} tsDnode; - static void dnodeSendStatusMsg() { - int32_t contLen = sizeof(SStatusMsg) + TSDB_MAX_VNODES * sizeof(SVnodeLoad); + int32_t contLen = sizeof(SStatusMsg) + TSDB_MAX_VNODES * sizeof(SVnodeLoad); + SStatusMsg *pStatus = rpcMallocCont(contLen); if (pStatus == NULL) { dError("failed to malloc status message"); return; } - pStatus->version = htonl(tsVersion); + pStatus->sversion = htonl(tsVersion); pStatus->dnodeId = htonl(dnodeGetDnodeId()); - tstrncpy(pStatus->dnodeEp, tsLocalEp, TSDB_EP_LEN); pStatus->clusterId = htobe64(dnodeGetClusterId()); - pStatus->lastReboot = htonl(tsDnode.rebootTime); + pStatus->rebootTime = htonl(tsDnode.rebootTime); pStatus->numOfCores = htonl(tsNumOfCores); - pStatus->diskAvailable = tsAvailDataDirGB; + tstrncpy(pStatus->dnodeEp, tsLocalEp, TSDB_EP_LEN); - // fill cluster cfg parameters pStatus->clusterCfg.statusInterval = htonl(tsStatusInterval); pStatus->clusterCfg.checkTime = 0; - tstrncpy(pStatus->clusterCfg.timezone, tsTimezone, 64); - char timestr[32] = "1970-01-01 00:00:00.00"; - (void)taosParseTime(timestr, &pStatus->clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0); + tstrncpy(pStatus->clusterCfg.timezone, tsTimezone, TSDB_TIMEZONE_LEN); tstrncpy(pStatus->clusterCfg.locale, tsLocale, TSDB_LOCALE_LEN); tstrncpy(pStatus->clusterCfg.charset, tsCharset, TSDB_LOCALE_LEN); + char timestr[32] = "1970-01-01 00:00:00.00"; + (void)taosParseTime(timestr, &pStatus->clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0); - // vnodeGetStatus(NULL, pStatus); - // contLen = sizeof(SStatusMsg) + pStatus->openVnodes * sizeof(SVnodeLoad); - // pStatus->openVnodes = htons(pStatus->openVnodes); - - SRpcMsg rpcMsg = {.ahandle = NULL, .pCont = pStatus, .contLen = contLen, .msgType = TSDB_MSG_TYPE_DM_STATUS}; + dnodeGetVnodes(&pStatus->vnodeLoads); + contLen = sizeof(SStatusMsg) + pStatus->vnodeLoads.vnodeNum * sizeof(SVnodeLoad); + SRpcMsg rpcMsg = {.pCont = pStatus, .contLen = contLen, .msgType = TSDB_MSG_TYPE_STATUS}; dnodeSendMsgToMnode(&rpcMsg); } -void dnodeProcessStatusRsp(SRpcMsg *pMsg) { - dTrace("status rsp is received, code:%s", tstrerror(pMsg->code)); +static void dnodeUpdateCfg(SDnodeCfg *pCfg) { + if (tsDnode.dnodeId == 0) return; + if (tsDnode.dropped) return; + + pthread_mutex_lock(&tsDnode.mutex); + + tsDnode.dnodeId = pCfg->dnodeId; + tsDnode.clusterId = pCfg->clusterId; + tsDnode.dropped = pCfg->dropped; + dInfo("dnodeId is set to %d, clusterId is set to %" PRId64, pCfg->dnodeId, pCfg->clusterId); + + dnodeWriteEps(); + pthread_mutex_unlock(&tsDnode.mutex); +} + +static void dnodeUpdateDnodeEps(SDnodeEps *pEps) { + if (pEps == NULL || pEps->dnodeNum <= 0) return; + + pthread_mutex_lock(&tsDnode.mutex); + + if (pEps->dnodeNum != tsDnode.dnodeEps->dnodeNum) { + dnodeResetEps(pEps); + dnodeWriteEps(); + } else { + int32_t size = pEps->dnodeNum * sizeof(SDnodeEp) + sizeof(SDnodeEps); + if (memcmp(tsDnode.dnodeEps, pEps, size) != 0) { + dnodeResetEps(pEps); + dnodeWriteEps(); + } + } + + pthread_mutex_unlock(&tsDnode.mutex); +} + +static void dnodeProcessStatusRsp(SRpcMsg *pMsg) { if (pMsg->code != TSDB_CODE_SUCCESS) return; SStatusRsp *pStatusRsp = pMsg->pCont; @@ -466,25 +422,40 @@ void dnodeProcessStatusRsp(SRpcMsg *pMsg) { SDnodeCfg *pCfg = &pStatusRsp->dnodeCfg; pCfg->dnodeId = htonl(pCfg->dnodeId); pCfg->clusterId = htobe64(pCfg->clusterId); - pCfg->numOfVnodes = htonl(pCfg->numOfVnodes); - pCfg->numOfDnodes = htonl(pCfg->numOfDnodes); dnodeUpdateCfg(pCfg); - if (pCfg->dropped) { - dError("status rsp is received, and set dnode to drop status"); - return; + if (pCfg->dropped) return; + + SDnodeEps *pEps = &pStatusRsp->dnodeEps; + pEps->dnodeNum = htonl(pEps->dnodeNum); + for (int32_t i = 0; i < pEps->dnodeNum; ++i) { + pEps->dnodeEps[i].dnodeId = htonl(pEps->dnodeEps[i].dnodeId); + pEps->dnodeEps[i].dnodePort = htons(pEps->dnodeEps[i].dnodePort); } - // vnodeSetAccess(pStatusRsp->vgAccess, pCfg->numOfVnodes); + dnodeUpdateDnodeEps(pEps); +} - SDnodeEps *eps = (SDnodeEps *)((char *)pStatusRsp->vgAccess + pCfg->numOfVnodes * sizeof(SVgroupAccess)); - eps->dnodeNum = htonl(eps->dnodeNum); - for (int32_t i = 0; i < eps->dnodeNum; ++i) { - eps->dnodeEps[i].dnodeId = htonl(eps->dnodeEps[i].dnodeId); - eps->dnodeEps[i].dnodePort = htons(eps->dnodeEps[i].dnodePort); - } +static void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg) { + SCfgDnodeMsg *pCfg = pMsg->pCont; - dnodeUpdateDnodeEps(eps); + int32_t code = taosCfgDynamicOptions(pCfg->config); + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); +} + +static void dnodeProcessStartupReq(SRpcMsg *pMsg) { + dInfo("startup msg is received, cont:%s", (char *)pMsg->pCont); + + SStartupStep *pStep = rpcMallocCont(sizeof(SStartupStep)); + dnodeGetStartup(pStep); + + dInfo("startup msg is sent, step:%s desc:%s finished:%d", pStep->name, pStep->desc, pStep->finished); + + SRpcMsg rpcRsp = {.handle = pMsg->handle, .pCont = pStep, .contLen = sizeof(SStartupStep)}; + rpcSendResponse(&rpcRsp); + rpcFreeCont(pMsg->pCont); } static void *dnodeThreadRoutine(void *param) { @@ -496,14 +467,34 @@ static void *dnodeThreadRoutine(void *param) { } int32_t dnodeInitDnode() { - tsDnode.threadStop = false; + tsDnode.dnodeId = 0; + tsDnode.clusterId = 0; + tsDnode.dnodeEps = NULL; + snprintf(tsDnode.file, sizeof(tsDnode.file), "%s/dnode.json", tsDnodeDir); tsDnode.rebootTime = taosGetTimestampSec(); - tsDnode.threadId = taosCreateThread(dnodeThreadRoutine, NULL); - if (tsDnode.threadId == NULL) { - return -1; + tsDnode.dropped = 0; + pthread_mutex_init(&tsDnode.mutex, NULL); + tsDnode.threadStop = false; + + tsDnode.dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); + if (tsDnode.dnodeHash == NULL) { + dError("failed to init dnode hash"); + return TSDB_CODE_DND_OUT_OF_MEMORY; } - dInfo("dnode msg is initialized"); + tsDnode.threadId = taosCreateThread(dnodeThreadRoutine, NULL); + if (tsDnode.threadId == NULL) { + dError("failed to init dnode thread"); + return TSDB_CODE_DND_OUT_OF_MEMORY; + } + + int32_t code = dnodeReadEps(); + if (code != 0) { + dError("failed to read dnode endpoint file since %s", tstrerror(code)); + return code; + } + + dInfo("dnode-dnode is initialized"); return 0; } @@ -514,29 +505,45 @@ void dnodeCleanupDnode() { tsDnode.threadId = NULL; } - dInfo("dnode msg is cleanuped"); + pthread_mutex_lock(&tsDnode.mutex); + + if (tsDnode.dnodeEps != NULL) { + free(tsDnode.dnodeEps); + tsDnode.dnodeEps = NULL; + } + + if (tsDnode.dnodeHash) { + taosHashCleanup(tsDnode.dnodeHash); + tsDnode.dnodeHash = NULL; + } + + pthread_mutex_unlock(&tsDnode.mutex); + pthread_mutex_destroy(&tsDnode.mutex); + + dInfo("dnode-dnode is cleaned up"); } -void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg) { - SCfgDnodeMsg *pCfg = pMsg->pCont; +void dnodeProcessDnodeMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { + int32_t msgType = pMsg->msgType; - int32_t code = taosCfgDynamicOptions(pCfg->config); + if (msgType == TSDB_MSG_TYPE_STATUS_RSP && pEpSet) { + dnodeUpdateMnodeEpSet(pEpSet); + } - SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; - - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); -} - -void dnodeProcessStartupReq(SRpcMsg *pMsg) { - dInfo("startup msg is received, cont:%s", (char *)pMsg->pCont); - - SStartupStep *pStep = rpcMallocCont(sizeof(SStartupStep)); - dnodeGetStartup(pStep); - - dDebug("startup msg is sent, step:%s desc:%s finished:%d", pStep->name, pStep->desc, pStep->finished); - - SRpcMsg rpcRsp = {.handle = pMsg->handle, .pCont = pStep, .contLen = sizeof(SStartupStep)}; - rpcSendResponse(&rpcRsp); - rpcFreeCont(pMsg->pCont); + switch (msgType) { + case TSDB_MSG_TYPE_NETWORK_TEST: + dnodeProcessStartupReq(pMsg); + break; + case TSDB_MSG_TYPE_CONFIG_DNODE_IN: + dnodeProcessConfigDnodeReq(pMsg); + break; + case TSDB_MSG_TYPE_STATUS_RSP: + dnodeProcessStatusRsp(pMsg); + break; + default: + dError("RPC %p, %s not processed", pMsg->handle, taosMsg[msgType]); + SRpcMsg rspMsg = {.handle = pMsg->handle, .code = TSDB_CODE_DND_MSG_NOT_PROCESSED}; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); + } } diff --git a/source/dnode/mgmt/src/dnodeMnode.c b/source/dnode/mgmt/src/dnodeMnode.c index 6019fbd1f6..6346b3386f 100644 --- a/source/dnode/mgmt/src/dnodeMnode.c +++ b/source/dnode/mgmt/src/dnodeMnode.c @@ -21,7 +21,7 @@ int32_t dnodeInitMnode() { SMnodePara para; - para.fp.GetDnodeEp = dnodeGetEp; + para.fp.GetDnodeEp = dnodeGetDnodeEp; para.fp.SendMsgToDnode = dnodeSendMsgToDnode; para.fp.SendMsgToMnode = dnodeSendMsgToMnode; para.fp.SendRedirectMsg = dnodeSendRedirectMsg; @@ -59,4 +59,11 @@ void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg) { rpcSendResponse(&rspMsg); rpcFreeCont(pMsg->pCont); +} + +void dnodeProcessMnodeMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { + mnodeProcessMsg(pMsg); + // tsDnode.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN] = dnodeProcessCreateMnodeReq; + + // tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN] = dnodeProcessDropMnodeReq; } \ No newline at end of file diff --git a/source/dnode/mgmt/src/dnodeTransport.c b/source/dnode/mgmt/src/dnodeTransport.c index 39747d4350..222d59ba55 100644 --- a/source/dnode/mgmt/src/dnodeTransport.c +++ b/source/dnode/mgmt/src/dnodeTransport.c @@ -26,9 +26,6 @@ #include "dnodeVnodes.h" #include "mnode.h" #include "vnode.h" - -typedef void (*MsgFp)(SRpcMsg *pMsg); - static struct { void *serverRpc; void *clientRpc; @@ -38,88 +35,88 @@ static struct { static void dnodeInitMsgFp() { // msg from client to dnode - tsTrans.msgFp[TSDB_MSG_TYPE_SUBMIT] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_QUERY] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_FETCH] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TABLE] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TABLE] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TABLE] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_TABLE_META] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_TABLES_META] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_QUERY] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONNECT] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_ACK] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_RESET] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_NETWORK_TEST] = dnodeProcessStartupReq; + tsTrans.msgFp[TSDB_MSG_TYPE_SUBMIT] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_QUERY] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_FETCH] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TABLE] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TABLE] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TABLE] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_TABLE_META] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_TABLES_META] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_QUERY] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONNECT] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_ACK] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_RESET] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_NETWORK_TEST] = dnodeProcessDnodeMsg; // msg from client to mnode - tsTrans.msgFp[TSDB_MSG_TYPE_CONNECT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_ACCT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_ACCT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_ACCT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_USER] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_USER] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_USER] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_DNODE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_DNODE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_DB] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_DB] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_USE_DB] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_DB] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_DB] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TOPIC] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TOPIC] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TOPIC] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_FUNCTION] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_FUNCTION] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_FUNCTION] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_KILL_QUERY] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_KILL_CONN] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_HEARTBEAT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SHOW] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE_FUNC] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CONNECT] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_ACCT] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_ACCT] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_ACCT] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_USER] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_USER] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_USER] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_DNODE] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_DNODE] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_DB] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_DB] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_USE_DB] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_DB] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_DB] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TOPIC] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TOPIC] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TOPIC] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_FUNCTION] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_FUNCTION] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_FUNCTION] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_KILL_QUERY] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_KILL_CONN] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_HEARTBEAT] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SHOW] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE_FUNC] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE] = dnodeProcessMnodeMsg; // message from mnode to dnode - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN] = vnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN] = dnodeProcessCreateMnodeReq; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN] = NULL; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN_RSP] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE_IN] = NULL; - tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE_IN_RSP] = mnodeProcessMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE_IN] = dnodeProcessDnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CONFIG_DNODE_IN_RSP] = dnodeProcessMnodeMsg; // message from dnode to mnode - tsTrans.msgFp[TSDB_MSG_TYPE_DM_AUTH] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DM_AUTH_RSP] = NULL; - tsTrans.msgFp[TSDB_MSG_TYPE_DM_GRANT] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DM_GRANT_RSP] = NULL; - tsTrans.msgFp[TSDB_MSG_TYPE_DM_STATUS] = mnodeProcessMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DM_STATUS_RSP] = dnodeProcessStatusRsp; + tsTrans.msgFp[TSDB_MSG_TYPE_AUTH] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_AUTH_RSP] = dnodeProcessDnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_GRANT] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_GRANT_RSP] = dnodeProcessDnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_STATUS] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_STATUS_RSP] = dnodeProcessDnodeMsg; } static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { @@ -127,7 +124,7 @@ static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { int32_t msgType = pMsg->msgType; if (msgType == TSDB_MSG_TYPE_NETWORK_TEST) { - dnodeProcessStartupReq(pMsg); + dnodeProcessDnodeMsg(pMsg, pEpSet); return; } @@ -148,7 +145,7 @@ static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { MsgFp fp = tsTrans.msgFp[msgType]; if (fp != NULL) { dTrace("RPC %p, peer req:%s will be processed", pMsg->handle, taosMsg[msgType]); - (*fp)(pMsg); + (*fp)(pMsg, pEpSet); } else { dError("RPC %p, peer req:%s not processed", pMsg->handle, taosMsg[msgType]); rspMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; @@ -196,14 +193,10 @@ static void dnodeProcessPeerRsp(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { return; } - if (msgType == TSDB_MSG_TYPE_DM_STATUS_RSP && pEpSet) { - dnodeUpdateMnodeEps(pEpSet); - } - MsgFp fp = tsTrans.msgFp[msgType]; if (fp != NULL) { - dTrace("RPC %p, peer rsp:%s will be processed", pMsg->handle, taosMsg[msgType]); - (*fp)(pMsg); + dTrace("RPC %p, peer rsp:%s will be processed, code:%s", pMsg->handle, taosMsg[msgType], tstrerror(pMsg->code)); + (*fp)(pMsg, pEpSet); } else { dDebug("RPC %p, peer rsp:%s not processed", pMsg->handle, taosMsg[msgType]); } @@ -270,7 +263,7 @@ static void dnodeProcessShellReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { MsgFp fp = tsTrans.msgFp[msgType]; if (fp != NULL) { dTrace("RPC %p, shell req:%s will be processed", pMsg->handle, taosMsg[msgType]); - (*fp)(pMsg); + (*fp)(pMsg, pEpSet); } else { dError("RPC %p, shell req:%s is not processed", pMsg->handle, taosMsg[msgType]); rspMsg.code = TSDB_CODE_DND_MSG_NOT_PROCESSED; @@ -283,13 +276,13 @@ void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg) { rpcSendRequest(tsT void dnodeSendMsgToMnode(SRpcMsg *rpcMsg) { SRpcEpSet epSet = {0}; - dnodeGetEpSetForPeer(&epSet); + dnodeGetMnodeEpSetForPeer(&epSet); dnodeSendMsgToDnode(&epSet, rpcMsg); } static void dnodeSendMsgToMnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp) { SRpcEpSet epSet = {0}; - dnodeGetEpSetForPeer(&epSet); + dnodeGetMnodeEpSetForPeer(&epSet); rpcSendRecv(tsTrans.clientRpc, &epSet, rpcMsg, rpcRsp); } @@ -303,7 +296,7 @@ static int32_t dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, c SRpcMsg rpcMsg = {0}; rpcMsg.pCont = pMsg; rpcMsg.contLen = sizeof(SAuthMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_DM_AUTH; + rpcMsg.msgType = TSDB_MSG_TYPE_AUTH; dDebug("user:%s, send auth msg to mnodes", user); SRpcMsg rpcRsp = {0}; diff --git a/source/dnode/mgmt/src/dnodeVnodes.c b/source/dnode/mgmt/src/dnodeVnodes.c index 765bd6fc22..f9032b419e 100644 --- a/source/dnode/mgmt/src/dnodeVnodes.c +++ b/source/dnode/mgmt/src/dnodeVnodes.c @@ -19,4 +19,8 @@ int32_t dnodeInitVnodes() { return vnodeInit(); } -void dnodeCleanupVnodes() { vnodeCleanup(); } \ No newline at end of file +void dnodeCleanupVnodes() { vnodeCleanup(); } + +void dnodeProcessVnodesMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { vnodeProcessMsg(NULL, NULL); } + +void dnodeGetVnodes(SVnodeLoads *pVloads) {} \ No newline at end of file diff --git a/source/dnode/mnode/inc/mnodeDef.h b/source/dnode/mnode/inc/mnodeDef.h index 2670432594..33606e8ee2 100644 --- a/source/dnode/mnode/inc/mnodeDef.h +++ b/source/dnode/mnode/inc/mnodeDef.h @@ -120,7 +120,7 @@ typedef struct SDnodeObj { int64_t createdTime; int64_t updateTime; int64_t lastAccess; - int64_t lastReboot; // time stamp for last reboot + int64_t rebootTime; // time stamp for last reboot char fqdn[TSDB_FQDN_LEN]; char ep[TSDB_EP_LEN]; uint16_t port; diff --git a/source/dnode/mnode/src/mnodeWorker.c b/source/dnode/mnode/src/mnodeWorker.c index a477a11f7c..5ac44a3a06 100644 --- a/source/dnode/mnode/src/mnodeWorker.c +++ b/source/dnode/mnode/src/mnodeWorker.c @@ -171,12 +171,12 @@ static void mnodeInitMsgFp() { // tsMworker.peerReqFp[TSDB_MSG_TYPE_DM_CONFIG_TABLE] = mnodeProcessTableCfgMsg; // tsMworker.msgFp[TSDB_MSG_TYPE_DM_CONFIG_VNODE] = mnodeDispatchToPeerQueue; // tsMworker.peerReqFp[TSDB_MSG_TYPE_DM_CONFIG_VNODE] = mnodeProcessVnodeCfgMsg; -// tsMworker.msgFp[TSDB_MSG_TYPE_DM_AUTH] = mnodeDispatchToPeerQueue; -// tsMworker.peerReqFp[TSDB_MSG_TYPE_DM_AUTH] = mnodeProcessAuthMsg; -// // tsMworker.msgFp[TSDB_MSG_TYPE_DM_GRANT] = mnodeDispatchToPeerQueue; -// // tsMworker.peerReqFp[TSDB_MSG_TYPE_DM_GRANT] = grantProcessMsgInMgmt; -// tsMworker.msgFp[TSDB_MSG_TYPE_DM_STATUS] = mnodeDispatchToPeerQueue; -// tsMworker.peerReqFp[TSDB_MSG_TYPE_DM_STATUS] = mnodeProcessDnodeStatusMsg; +// tsMworker.msgFp[TSDB_MSG_TYPE_AUTH] = mnodeDispatchToPeerQueue; +// tsMworker.peerReqFp[TSDB_MSG_TYPE_AUTH] = mnodeProcessAuthMsg; +// // tsMworker.msgFp[TSDB_MSG_TYPE_GRANT] = mnodeDispatchToPeerQueue; +// // tsMworker.peerReqFp[TSDB_MSG_TYPE_GRANT] = grantProcessMsgInMgmt; +// tsMworker.msgFp[TSDB_MSG_TYPE_STATUS] = mnodeDispatchToPeerQueue; +// tsMworker.peerReqFp[TSDB_MSG_TYPE_STATUS] = mnodeProcessDnodeStatusMsg; // // peer rsp // tsMworker.msgFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP] = mnodeDispatchToPeerRspQueue; diff --git a/source/libs/transport/src/rpcMain.c b/source/libs/transport/src/rpcMain.c index eecde288b1..e047964b94 100644 --- a/source/libs/transport/src/rpcMain.c +++ b/source/libs/transport/src/rpcMain.c @@ -406,7 +406,7 @@ void rpcSendRequest(void *shandle, const SRpcEpSet *pEpSet, SRpcMsg *pMsg, int64 if (type == TSDB_MSG_TYPE_QUERY || type == TSDB_MSG_TYPE_SHOW_RETRIEVE || type == TSDB_MSG_TYPE_FETCH || type == TSDB_MSG_TYPE_STABLE_VGROUP || type == TSDB_MSG_TYPE_TABLES_META || type == TSDB_MSG_TYPE_TABLE_META - || type == TSDB_MSG_TYPE_SHOW || type == TSDB_MSG_TYPE_DM_STATUS || type == TSDB_MSG_TYPE_ALTER_TABLE) + || type == TSDB_MSG_TYPE_SHOW || type == TSDB_MSG_TYPE_STATUS || type == TSDB_MSG_TYPE_ALTER_TABLE) pContext->connType = RPC_CONN_TCPC; pContext->rid = taosAddRef(tsRpcRefId, pContext); From f4e648b7f91d2ae0fff10e567a7793863a0222c7 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 2 Nov 2021 14:35:04 +0800 Subject: [PATCH 20/33] minor changes --- include/common/taosmsg.h | 3 +-- source/dnode/mgmt/inc/dnodeInt.h | 2 +- source/dnode/mgmt/src/dnodeDnode.c | 15 ++++++++++----- source/dnode/mgmt/src/dnodeInt.c | 30 +++++++++++++++--------------- src/kit/shell/src/tnettest.c | 10 +++++----- 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index c214c96f35..f9ad39fe78 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -925,8 +925,7 @@ typedef struct { int8_t reserved1[7]; char name[TSDB_STEP_NAME_LEN]; char desc[TSDB_STEP_DESC_LEN]; - char reserved2[64]; -} SStartupStep; +} SStartupMsg; // mq related typedef struct { diff --git a/source/dnode/mgmt/inc/dnodeInt.h b/source/dnode/mgmt/inc/dnodeInt.h index 1306f2d700..e3306ecfa4 100644 --- a/source/dnode/mgmt/inc/dnodeInt.h +++ b/source/dnode/mgmt/inc/dnodeInt.h @@ -42,7 +42,7 @@ void dnodeCleanup(); EDnStat dnodeGetRunStat(); void dnodeSetRunStat(); -void dnodeGetStartup(SStartupStep *); +void dnodeGetStartup(SStartupMsg *); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/src/dnodeDnode.c b/source/dnode/mgmt/src/dnodeDnode.c index 64694e9a46..701a190841 100644 --- a/source/dnode/mgmt/src/dnodeDnode.c +++ b/source/dnode/mgmt/src/dnodeDnode.c @@ -448,21 +448,26 @@ static void dnodeProcessConfigDnodeReq(SRpcMsg *pMsg) { static void dnodeProcessStartupReq(SRpcMsg *pMsg) { dInfo("startup msg is received, cont:%s", (char *)pMsg->pCont); - SStartupStep *pStep = rpcMallocCont(sizeof(SStartupStep)); - dnodeGetStartup(pStep); + SStartupMsg *pStartup = rpcMallocCont(sizeof(SStartupMsg)); + dnodeGetStartup(pStartup); - dInfo("startup msg is sent, step:%s desc:%s finished:%d", pStep->name, pStep->desc, pStep->finished); + dInfo("startup msg is sent, step:%s desc:%s finished:%d", pStartup->name, pStartup->desc, pStartup->finished); - SRpcMsg rpcRsp = {.handle = pMsg->handle, .pCont = pStep, .contLen = sizeof(SStartupStep)}; + SRpcMsg rpcRsp = {.handle = pMsg->handle, .pCont = pStartup, .contLen = sizeof(SStartupMsg)}; rpcSendResponse(&rpcRsp); rpcFreeCont(pMsg->pCont); } static void *dnodeThreadRoutine(void *param) { int32_t ms = tsStatusInterval * 1000; + while (!tsDnode.threadStop) { + if (dnodeGetRunStat() != DN_RUN_STAT_RUNNING) { + continue; + } else { + dnodeSendStatusMsg(); + } taosMsleep(ms); - dnodeSendStatusMsg(); } } diff --git a/source/dnode/mgmt/src/dnodeInt.c b/source/dnode/mgmt/src/dnodeInt.c index a1e7bc1c7e..e853ba7f14 100644 --- a/source/dnode/mgmt/src/dnodeInt.c +++ b/source/dnode/mgmt/src/dnodeInt.c @@ -26,30 +26,30 @@ #include "wal.h" static struct { - EDnStat runStatus; - SStartupStep startup; - SSteps *steps; + SStartupMsg startup; + EDnStat runStat; + SSteps *steps; } tsInt; -EDnStat dnodeGetRunStat() { return tsInt.runStatus; } +EDnStat dnodeGetRunStat() { return tsInt.runStat; } -void dnodeSetRunStat(EDnStat stat) { tsInt.runStatus = stat; } +void dnodeSetRunStat(EDnStat stat) { tsInt.runStat = stat; } static void dnodeReportStartup(char *name, char *desc) { - SStartupStep *startup = &tsInt.startup; - tstrncpy(startup->name, name, strlen(startup->name)); - tstrncpy(startup->desc, desc, strlen(startup->desc)); - startup->finished = 0; + SStartupMsg *pStartup = &tsInt.startup; + tstrncpy(pStartup->name, name, strlen(pStartup->name)); + tstrncpy(pStartup->desc, desc, strlen(pStartup->desc)); + pStartup->finished = 0; } static void dnodeReportStartupFinished(char *name, char *desc) { - SStartupStep *startup = &tsInt.startup; - tstrncpy(startup->name, name, strlen(startup->name)); - tstrncpy(startup->desc, desc, strlen(startup->desc)); - startup->finished = 1; + SStartupMsg *pStartup = &tsInt.startup; + tstrncpy(pStartup->name, name, strlen(pStartup->name)); + tstrncpy(pStartup->desc, desc, strlen(pStartup->desc)); + pStartup->finished = 1; } -void dnodeGetStartup(SStartupStep *pStep) { memcpy(pStep, &tsInt.startup, sizeof(SStartupStep)); } +void dnodeGetStartup(SStartupMsg *pStartup) { memcpy(pStartup, &tsInt.startup, sizeof(SStartupMsg)); } static int32_t dnodeCheckRunning(char *dir) { char filepath[256] = {0}; @@ -99,7 +99,7 @@ static int32_t dnodeInitDir() { } static int32_t dnodeInitMain() { - tsInt.runStatus = DN_RUN_STAT_STOPPED; + tsInt.runStat = DN_RUN_STAT_STOPPED; tscEmbedded = 1; taosIgnSIGPIPE(); taosBlockSIGPIPE(); diff --git a/src/kit/shell/src/tnettest.c b/src/kit/shell/src/tnettest.c index 2a147ee4f1..c8a6b36fc7 100644 --- a/src/kit/shell/src/tnettest.c +++ b/src/kit/shell/src/tnettest.c @@ -338,7 +338,7 @@ void *taosNetInitRpc(char *secretEncrypt, char spi) { return pRpcConn; } -static int32_t taosNetCheckRpc(const char* serverFqdn, uint16_t port, uint16_t pktLen, char spi, SStartupStep *pStep) { +static int32_t taosNetCheckRpc(const char* serverFqdn, uint16_t port, uint16_t pktLen, char spi, SStartupMsg *pStep) { SRpcEpSet epSet; SRpcMsg reqMsg; SRpcMsg rspMsg; @@ -374,7 +374,7 @@ static int32_t taosNetCheckRpc(const char* serverFqdn, uint16_t port, uint16_t p } int32_t code = 0; - if (pStep != NULL && rspMsg.pCont != NULL && rspMsg.contLen > 0 && rspMsg.contLen <= sizeof(SStartupStep)) { + if (pStep != NULL && rspMsg.pCont != NULL && rspMsg.contLen > 0 && rspMsg.contLen <= sizeof(SStartupMsg)) { memcpy(pStep, rspMsg.pCont, rspMsg.contLen); code = 1; } @@ -384,8 +384,8 @@ static int32_t taosNetCheckRpc(const char* serverFqdn, uint16_t port, uint16_t p return code; } -static int32_t taosNetParseStartup(SStartupStep *pCont) { - SStartupStep *pStep = pCont; +static int32_t taosNetParseStartup(SStartupMsg *pCont) { + SStartupMsg *pStep = pCont; uInfo("step:%s desc:%s", pStep->name, pStep->desc); if (pStep->finished) { @@ -398,7 +398,7 @@ static int32_t taosNetParseStartup(SStartupStep *pCont) { static void taosNetTestStartup(char *host, int32_t port) { uInfo("check startup, host:%s port:%d\n", host, port); - SStartupStep *pStep = malloc(sizeof(SStartupStep)); + SStartupMsg *pStep = malloc(sizeof(SStartupMsg)); while (1) { int32_t code = taosNetCheckRpc(host, port + TSDB_PORT_DNODEDNODE, 20, 0, pStep); if (code > 0) { From 72cb9c01ea200299b35f47af94ef70e77b0c0081 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 2 Nov 2021 14:53:09 +0800 Subject: [PATCH 21/33] minor changes --- source/dnode/mgmt/src/dnodeTransport.c | 63 ++++++++++++-------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/source/dnode/mgmt/src/dnodeTransport.c b/source/dnode/mgmt/src/dnodeTransport.c index 222d59ba55..8170c8c21d 100644 --- a/source/dnode/mgmt/src/dnodeTransport.c +++ b/source/dnode/mgmt/src/dnodeTransport.c @@ -25,11 +25,11 @@ #include "dnodeMnode.h" #include "dnodeVnodes.h" #include "mnode.h" -#include "vnode.h" + static struct { - void *serverRpc; - void *clientRpc; + void *peerRpc; void *shellRpc; + void *clientRpc; MsgFp msgFp[TSDB_MSG_TYPE_MAX]; } tsTrans; @@ -120,7 +120,7 @@ static void dnodeInitMsgFp() { } static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0}; + SRpcMsg rspMsg = {.handle = pMsg->handle}; int32_t msgType = pMsg->msgType; if (msgType == TSDB_MSG_TYPE_NETWORK_TEST) { @@ -154,7 +154,7 @@ static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { } } -static int32_t dnodeInitServer() { +static int32_t dnodeInitPeerServer() { SRpcInit rpcInit; memset(&rpcInit, 0, sizeof(rpcInit)); rpcInit.localPort = tsDnodeDnodePort; @@ -165,8 +165,8 @@ static int32_t dnodeInitServer() { rpcInit.connType = TAOS_CONN_SERVER; rpcInit.idleTime = tsShellActivityTimer * 1000; - tsTrans.serverRpc = rpcOpen(&rpcInit); - if (tsTrans.serverRpc == NULL) { + tsTrans.peerRpc = rpcOpen(&rpcInit); + if (tsTrans.peerRpc == NULL) { dError("failed to init peer rpc server"); return -1; } @@ -175,10 +175,10 @@ static int32_t dnodeInitServer() { return 0; } -static void dnodeCleanupServer() { - if (tsTrans.serverRpc) { - rpcClose(tsTrans.serverRpc); - tsTrans.serverRpc = NULL; +static void dnodeCleanupPeerServer() { + if (tsTrans.peerRpc) { + rpcClose(tsTrans.peerRpc); + tsTrans.peerRpc = NULL; dInfo("dnode peer server is closed"); } } @@ -205,7 +205,8 @@ static void dnodeProcessPeerRsp(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { } static int32_t dnodeInitClient() { - char secret[TSDB_KEY_LEN] = "secret"; + char secret[TSDB_KEY_LEN] = "secret"; + SRpcInit rpcInit; memset(&rpcInit, 0, sizeof(rpcInit)); rpcInit.label = "DND-C"; @@ -237,7 +238,7 @@ static void dnodeCleanupClient() { } static void dnodeProcessShellReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0}; + SRpcMsg rspMsg = {.handle = pMsg->handle}; int32_t msgType = pMsg->msgType; if (dnodeGetRunStat() == DN_RUN_STAT_STOPPED) { @@ -272,14 +273,6 @@ static void dnodeProcessShellReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { } } -void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg) { rpcSendRequest(tsTrans.clientRpc, epSet, rpcMsg, NULL); } - -void dnodeSendMsgToMnode(SRpcMsg *rpcMsg) { - SRpcEpSet epSet = {0}; - dnodeGetMnodeEpSetForPeer(&epSet); - dnodeSendMsgToDnode(&epSet, rpcMsg); -} - static void dnodeSendMsgToMnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp) { SRpcEpSet epSet = {0}; dnodeGetMnodeEpSetForPeer(&epSet); @@ -293,20 +286,16 @@ static int32_t dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, c SAuthMsg *pMsg = rpcMallocCont(sizeof(SAuthMsg)); tstrncpy(pMsg->user, user, sizeof(pMsg->user)); - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = pMsg; - rpcMsg.contLen = sizeof(SAuthMsg); - rpcMsg.msgType = TSDB_MSG_TYPE_AUTH; - dDebug("user:%s, send auth msg to mnodes", user); + SRpcMsg rpcMsg = {.pCont = pMsg, .contLen = sizeof(SAuthMsg), .msgType = TSDB_MSG_TYPE_AUTH}; SRpcMsg rpcRsp = {0}; dnodeSendMsgToMnodeRecv(&rpcMsg, &rpcRsp); if (rpcRsp.code != 0) { dError("user:%s, auth msg received from mnodes, error:%s", user, tstrerror(rpcRsp.code)); } else { - SAuthRsp *pRsp = rpcRsp.pCont; dDebug("user:%s, auth msg received from mnodes", user); + SAuthRsp *pRsp = rpcRsp.pCont; memcpy(secret, pRsp->secret, TSDB_KEY_LEN); memcpy(ckey, pRsp->ckey, TSDB_KEY_LEN); *spi = pRsp->spi; @@ -317,7 +306,7 @@ static int32_t dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, c return rpcRsp.code; } -static int32_t dnodeInitShell() { +static int32_t dnodeInitShellServer() { int32_t numOfThreads = (int32_t)((tsNumOfCores * tsNumOfThreadsPerCore) / 2.0); if (numOfThreads < 1) { numOfThreads = 1; @@ -344,7 +333,7 @@ static int32_t dnodeInitShell() { return 0; } -static void dnodeCleanupShell() { +static void dnodeCleanupShellServer() { if (tsTrans.shellRpc) { rpcClose(tsTrans.shellRpc); tsTrans.shellRpc = NULL; @@ -356,11 +345,11 @@ int32_t dnodeInitTrans() { return -1; } - if (dnodeInitServer() != 0) { + if (dnodeInitPeerServer() != 0) { return -1; } - if (dnodeInitShell() != 0) { + if (dnodeInitShellServer() != 0) { return -1; } @@ -368,7 +357,15 @@ int32_t dnodeInitTrans() { } void dnodeCleanupTrans() { - dnodeCleanupShell(); - dnodeCleanupServer(); + dnodeCleanupShellServer(); + dnodeCleanupPeerServer(); dnodeCleanupClient(); } + +void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg) { rpcSendRequest(tsTrans.clientRpc, epSet, rpcMsg, NULL); } + +void dnodeSendMsgToMnode(SRpcMsg *rpcMsg) { + SRpcEpSet epSet = {0}; + dnodeGetMnodeEpSetForPeer(&epSet); + dnodeSendMsgToDnode(&epSet, rpcMsg); +} \ No newline at end of file From 84effaaba904024da6545d197cf94de0911d68d8 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 2 Nov 2021 15:24:07 +0800 Subject: [PATCH 22/33] rename SRpcEpset to SEpSet --- include/common/taosmsg.h | 11 +--------- include/libs/transport/trpc.h | 18 ++++++---------- include/server/mnode/mnode.h | 2 +- source/dnode/mgmt/inc/dnodeDnode.h | 6 +++--- source/dnode/mgmt/inc/dnodeInt.h | 2 +- source/dnode/mgmt/inc/dnodeMnode.h | 5 ++--- source/dnode/mgmt/inc/dnodeTransport.h | 2 +- source/dnode/mgmt/inc/dnodeVnodes.h | 2 +- source/dnode/mgmt/src/dnodeDnode.c | 14 ++++++------ source/dnode/mgmt/src/dnodeMnode.c | 27 +++++++++++++++++++---- source/dnode/mgmt/src/dnodeTransport.c | 15 ++++++------- source/dnode/mgmt/src/dnodeVnodes.c | 2 +- source/dnode/mnode/inc/mnodeInt.h | 2 +- source/dnode/mnode/inc/mnodeMnode.h | 4 ++-- source/dnode/mnode/src/mnodeMnode.c | 4 ++-- source/dnode/mnode/src/mnodeWorker.c | 12 +++++------ source/dnode/mnode/src/mondeInt.c | 2 +- source/libs/transport/src/rpcMain.c | 30 +++++++++++++------------- 18 files changed, 81 insertions(+), 79 deletions(-) diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index f9ad39fe78..9dc1551970 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -901,16 +901,7 @@ typedef struct { typedef struct { char queryId[TSDB_KILL_MSG_LEN + 1]; -} SKillQueryMsg, SKillStreamMsg, SKillConnMsg; - -typedef struct { - int32_t vnode; - int32_t sid; - uint64_t uid; - uint64_t stime; // stream starting time - int32_t status; - char tableFname[TSDB_TABLE_FNAME_LEN]; -} SAlterStreamMsg; +} SKillQueryMsg, SKillConnMsg; typedef struct { char user[TSDB_USER_LEN]; diff --git a/include/libs/transport/trpc.h b/include/libs/transport/trpc.h index 0ce2e3da14..5460ae5401 100644 --- a/include/libs/transport/trpc.h +++ b/include/libs/transport/trpc.h @@ -22,22 +22,16 @@ extern "C" { #include #include #include "taosdef.h" +#include "taosmsg.h" #define TAOS_CONN_SERVER 0 #define TAOS_CONN_CLIENT 1 extern int tsRpcHeadSize; -typedef struct SRpcEpSet { - int8_t inUse; - int8_t numOfEps; - uint16_t port[TSDB_MAX_REPLICA]; - char fqdn[TSDB_MAX_REPLICA][TSDB_FQDN_LEN]; -} SRpcEpSet; - typedef struct SRpcCorEpSet { int32_t version; - SRpcEpSet epSet; + SEpSet epSet; } SRpcCorEpSet; typedef struct SRpcConnInfo { @@ -72,7 +66,7 @@ typedef struct SRpcInit { char *ckey; // ciphering key // call back to process incoming msg, code shall be ignored by server app - void (*cfp)(SRpcMsg *, SRpcEpSet *); + void (*cfp)(SRpcMsg *, SEpSet *); // call back to retrieve the client auth info, for server app only int (*afp)(char *tableId, char *spi, char *encrypt, char *secret, char *ckey); @@ -85,11 +79,11 @@ void rpcClose(void *); void *rpcMallocCont(int contLen); void rpcFreeCont(void *pCont); void *rpcReallocCont(void *ptr, int contLen); -void rpcSendRequest(void *thandle, const SRpcEpSet *pEpSet, SRpcMsg *pMsg, int64_t *rid); +void rpcSendRequest(void *thandle, const SEpSet *pEpSet, SRpcMsg *pMsg, int64_t *rid); void rpcSendResponse(const SRpcMsg *pMsg); -void rpcSendRedirectRsp(void *pConn, const SRpcEpSet *pEpSet); +void rpcSendRedirectRsp(void *pConn, const SEpSet *pEpSet); int rpcGetConnInfo(void *thandle, SRpcConnInfo *pInfo); -void rpcSendRecv(void *shandle, SRpcEpSet *pEpSet, SRpcMsg *pReq, SRpcMsg *pRsp); +void rpcSendRecv(void *shandle, SEpSet *pEpSet, SRpcMsg *pReq, SRpcMsg *pRsp); int rpcReportProgress(void *pConn, char *pCont, int contLen); void rpcCancelRequest(int64_t rid); diff --git a/include/server/mnode/mnode.h b/include/server/mnode/mnode.h index 9bbc2e4b10..e41d157057 100644 --- a/include/server/mnode/mnode.h +++ b/include/server/mnode/mnode.h @@ -29,7 +29,7 @@ typedef struct { * @param epSet, the endpoint list of the dnodes. * @param rpcMsg, message to be sent. */ - void (*SendMsgToDnode)(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg); + void (*SendMsgToDnode)(struct SEpSet *epSet, struct SRpcMsg *rpcMsg); /** * Send messages to mnode, such as config message. diff --git a/source/dnode/mgmt/inc/dnodeDnode.h b/source/dnode/mgmt/inc/dnodeDnode.h index 9a2b564bd4..2ca1368e63 100644 --- a/source/dnode/mgmt/inc/dnodeDnode.h +++ b/source/dnode/mgmt/inc/dnodeDnode.h @@ -23,13 +23,13 @@ extern "C" { int32_t dnodeInitDnode(); void dnodeCleanupDnode(); -void dnodeProcessDnodeMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet); +void dnodeProcessDnodeMsg(SRpcMsg *pMsg, SEpSet *pEpSet); int32_t dnodeGetDnodeId(); int64_t dnodeGetClusterId(); void dnodeGetDnodeEp(int32_t dnodeId, char *epstr, char *fqdn, uint16_t *port); -void dnodeGetMnodeEpSetForPeer(SRpcEpSet *epSet); -void dnodeGetMnodeEpSetForShell(SRpcEpSet *epSet); +void dnodeGetMnodeEpSetForPeer(SEpSet *epSet); +void dnodeGetMnodeEpSetForShell(SEpSet *epSet); void dnodeSendRedirectMsg(SRpcMsg *rpcMsg, bool forShell); #ifdef __cplusplus diff --git a/source/dnode/mgmt/inc/dnodeInt.h b/source/dnode/mgmt/inc/dnodeInt.h index e3306ecfa4..472c467ad6 100644 --- a/source/dnode/mgmt/inc/dnodeInt.h +++ b/source/dnode/mgmt/inc/dnodeInt.h @@ -35,7 +35,7 @@ extern int32_t dDebugFlag; #define dTrace(...) { if (dDebugFlag & DEBUG_TRACE) { taosPrintLog("DND ", dDebugFlag, __VA_ARGS__); }} typedef enum { DN_RUN_STAT_INIT, DN_RUN_STAT_RUNNING, DN_RUN_STAT_STOPPED } EDnStat; -typedef void (*MsgFp)(SRpcMsg *pMsg, SRpcEpSet *pEpSet); +typedef void (*MsgFp)(SRpcMsg *pMsg, SEpSet *pEpSet); int32_t dnodeInit(); void dnodeCleanup(); diff --git a/source/dnode/mgmt/inc/dnodeMnode.h b/source/dnode/mgmt/inc/dnodeMnode.h index e696acf425..28702dcb84 100644 --- a/source/dnode/mgmt/inc/dnodeMnode.h +++ b/source/dnode/mgmt/inc/dnodeMnode.h @@ -23,9 +23,8 @@ extern "C" { int32_t dnodeInitMnode(); void dnodeCleanupMnode(); -void dnodeProcessMnodeMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet); - -void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg); +void dnodeProcessMnodeMsg(SRpcMsg *pMsg, SEpSet *pEpSet); +int32_t dnodeGetUserAuthFromMnode(char *user, char *spi, char *encrypt, char *secret, char *ckey); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/inc/dnodeTransport.h b/source/dnode/mgmt/inc/dnodeTransport.h index e8223f4c06..95ca1b81e5 100644 --- a/source/dnode/mgmt/inc/dnodeTransport.h +++ b/source/dnode/mgmt/inc/dnodeTransport.h @@ -24,7 +24,7 @@ extern "C" { int32_t dnodeInitTrans(); void dnodeCleanupTrans(); void dnodeSendMsgToMnode(SRpcMsg *rpcMsg); -void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg); +void dnodeSendMsgToDnode(SEpSet *epSet, SRpcMsg *rpcMsg); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/inc/dnodeVnodes.h b/source/dnode/mgmt/inc/dnodeVnodes.h index 46dcf549d4..6e9bce9ae5 100644 --- a/source/dnode/mgmt/inc/dnodeVnodes.h +++ b/source/dnode/mgmt/inc/dnodeVnodes.h @@ -23,7 +23,7 @@ extern "C" { int32_t dnodeInitVnodes(); void dnodeCleanupVnodes(); -void dnodeProcessVnodesMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet); +void dnodeProcessVnodesMsg(SRpcMsg *pMsg, SEpSet *pEpSet); void dnodeGetVnodes(SVnodeLoads *pVloads); #ifdef __cplusplus diff --git a/source/dnode/mgmt/src/dnodeDnode.c b/source/dnode/mgmt/src/dnodeDnode.c index 701a190841..37dc9f05a1 100644 --- a/source/dnode/mgmt/src/dnodeDnode.c +++ b/source/dnode/mgmt/src/dnodeDnode.c @@ -27,8 +27,8 @@ static struct { int64_t clusterId; SDnodeEps *dnodeEps; SHashObj *dnodeHash; - SRpcEpSet mnodeEpSetForShell; - SRpcEpSet mnodeEpSetForPeer; + SEpSet mnodeEpSetForShell; + SEpSet mnodeEpSetForPeer; char file[PATH_MAX + 20]; uint32_t rebootTime; int8_t dropped; @@ -67,13 +67,13 @@ void dnodeGetDnodeEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port) { pthread_mutex_unlock(&tsDnode.mutex); } -void dnodeGetMnodeEpSetForPeer(SRpcEpSet *pEpSet) { +void dnodeGetMnodeEpSetForPeer(SEpSet *pEpSet) { pthread_mutex_lock(&tsDnode.mutex); *pEpSet = tsDnode.mnodeEpSetForPeer; pthread_mutex_unlock(&tsDnode.mutex); } -void dnodeGetMnodeEpSetForShell(SRpcEpSet *pEpSet) { +void dnodeGetMnodeEpSetForShell(SEpSet *pEpSet) { pthread_mutex_lock(&tsDnode.mutex); *pEpSet = tsDnode.mnodeEpSetForShell; pthread_mutex_unlock(&tsDnode.mutex); @@ -82,7 +82,7 @@ void dnodeGetMnodeEpSetForShell(SRpcEpSet *pEpSet) { void dnodeSendRedirectMsg(SRpcMsg *pMsg, bool forShell) { int32_t msgType = pMsg->msgType; - SRpcEpSet epSet = {0}; + SEpSet epSet = {0}; if (forShell) { dnodeGetMnodeEpSetForShell(&epSet); } else { @@ -107,7 +107,7 @@ void dnodeSendRedirectMsg(SRpcMsg *pMsg, bool forShell) { rpcSendRedirectRsp(pMsg->handle, &epSet); } -static void dnodeUpdateMnodeEpSet(SRpcEpSet *pEpSet) { +static void dnodeUpdateMnodeEpSet(SEpSet *pEpSet) { if (pEpSet == NULL || pEpSet->numOfEps <= 0) { dError("mnode is changed, but content is invalid, discard it"); return; @@ -528,7 +528,7 @@ void dnodeCleanupDnode() { dInfo("dnode-dnode is cleaned up"); } -void dnodeProcessDnodeMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { +void dnodeProcessDnodeMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { int32_t msgType = pMsg->msgType; if (msgType == TSDB_MSG_TYPE_STATUS_RSP && pEpSet) { diff --git a/source/dnode/mgmt/src/dnodeMnode.c b/source/dnode/mgmt/src/dnodeMnode.c index 6346b3386f..54fa847790 100644 --- a/source/dnode/mgmt/src/dnodeMnode.c +++ b/source/dnode/mgmt/src/dnodeMnode.c @@ -61,9 +61,28 @@ void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg) { rpcFreeCont(pMsg->pCont); } -void dnodeProcessMnodeMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - mnodeProcessMsg(pMsg); - // tsDnode.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN] = dnodeProcessCreateMnodeReq; +void dnodeProcessDropMnodeReq(SRpcMsg *pMsg) { + int32_t code = dnodeStartMnode(pMsg); - // tsTrans.msgFp[TSDB_MSG_TYPE_DROP_MNODE_IN] = dnodeProcessDropMnodeReq; + SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; + + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); +} + +void dnodeProcessMnodeMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { + switch (pMsg->msgType) { + case TSDB_MSG_TYPE_CREATE_MNODE_IN: + dnodeProcessCreateMnodeReq(pMsg); + break; + case TSDB_MSG_TYPE_DROP_MNODE_IN: + dnodeProcessDropMnodeReq(pMsg); + break; + default: + mnodeProcessMsg(pMsg); + } +} + +int32_t dnodeGetUserAuthFromMnode(char *user, char *spi, char *encrypt, char *secret, char *ckey) { + return mnodeRetriveAuth(user, spi, encrypt, secret, ckey); } \ No newline at end of file diff --git a/source/dnode/mgmt/src/dnodeTransport.c b/source/dnode/mgmt/src/dnodeTransport.c index 8170c8c21d..18e97e2b9f 100644 --- a/source/dnode/mgmt/src/dnodeTransport.c +++ b/source/dnode/mgmt/src/dnodeTransport.c @@ -24,7 +24,6 @@ #include "dnodeDnode.h" #include "dnodeMnode.h" #include "dnodeVnodes.h" -#include "mnode.h" static struct { void *peerRpc; @@ -119,7 +118,7 @@ static void dnodeInitMsgFp() { tsTrans.msgFp[TSDB_MSG_TYPE_STATUS_RSP] = dnodeProcessDnodeMsg; } -static void dnodeProcessPeerReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { +static void dnodeProcessPeerReq(SRpcMsg *pMsg, SEpSet *pEpSet) { SRpcMsg rspMsg = {.handle = pMsg->handle}; int32_t msgType = pMsg->msgType; @@ -183,7 +182,7 @@ static void dnodeCleanupPeerServer() { } } -static void dnodeProcessPeerRsp(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { +static void dnodeProcessPeerRsp(SRpcMsg *pMsg, SEpSet *pEpSet) { int32_t msgType = pMsg->msgType; if (dnodeGetRunStat() == DN_RUN_STAT_STOPPED) { @@ -237,7 +236,7 @@ static void dnodeCleanupClient() { } } -static void dnodeProcessShellReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { +static void dnodeProcessShellReq(SRpcMsg *pMsg, SEpSet *pEpSet) { SRpcMsg rspMsg = {.handle = pMsg->handle}; int32_t msgType = pMsg->msgType; @@ -274,13 +273,13 @@ static void dnodeProcessShellReq(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { } static void dnodeSendMsgToMnodeRecv(SRpcMsg *rpcMsg, SRpcMsg *rpcRsp) { - SRpcEpSet epSet = {0}; + SEpSet epSet = {0}; dnodeGetMnodeEpSetForPeer(&epSet); rpcSendRecv(tsTrans.clientRpc, &epSet, rpcMsg, rpcRsp); } static int32_t dnodeRetrieveUserAuthInfo(char *user, char *spi, char *encrypt, char *secret, char *ckey) { - int32_t code = mnodeRetriveAuth(user, spi, encrypt, secret, ckey); + int32_t code = dnodeGetUserAuthFromMnode(user, spi, encrypt, secret, ckey); if (code != TSDB_CODE_APP_NOT_READY) return code; SAuthMsg *pMsg = rpcMallocCont(sizeof(SAuthMsg)); @@ -362,10 +361,10 @@ void dnodeCleanupTrans() { dnodeCleanupClient(); } -void dnodeSendMsgToDnode(SRpcEpSet *epSet, SRpcMsg *rpcMsg) { rpcSendRequest(tsTrans.clientRpc, epSet, rpcMsg, NULL); } +void dnodeSendMsgToDnode(SEpSet *epSet, SRpcMsg *rpcMsg) { rpcSendRequest(tsTrans.clientRpc, epSet, rpcMsg, NULL); } void dnodeSendMsgToMnode(SRpcMsg *rpcMsg) { - SRpcEpSet epSet = {0}; + SEpSet epSet = {0}; dnodeGetMnodeEpSetForPeer(&epSet); dnodeSendMsgToDnode(&epSet, rpcMsg); } \ No newline at end of file diff --git a/source/dnode/mgmt/src/dnodeVnodes.c b/source/dnode/mgmt/src/dnodeVnodes.c index f9032b419e..59f5cf0fd0 100644 --- a/source/dnode/mgmt/src/dnodeVnodes.c +++ b/source/dnode/mgmt/src/dnodeVnodes.c @@ -21,6 +21,6 @@ int32_t dnodeInitVnodes() { return vnodeInit(); } void dnodeCleanupVnodes() { vnodeCleanup(); } -void dnodeProcessVnodesMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { vnodeProcessMsg(NULL, NULL); } +void dnodeProcessVnodesMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { vnodeProcessMsg(NULL, NULL); } void dnodeGetVnodes(SVnodeLoads *pVloads) {} \ No newline at end of file diff --git a/source/dnode/mnode/inc/mnodeInt.h b/source/dnode/mnode/inc/mnodeInt.h index 0ce47cbe36..5b552e5c51 100644 --- a/source/dnode/mnode/inc/mnodeInt.h +++ b/source/dnode/mnode/inc/mnodeInt.h @@ -27,7 +27,7 @@ int32_t mnodeGetDnodeId(); int64_t mnodeGetClusterId(); EMnStatus mnodeGetStatus(); -void mnodeSendMsgToDnode(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg); +void mnodeSendMsgToDnode(struct SEpSet *epSet, struct SRpcMsg *rpcMsg); void mnodeSendMsgToMnode(struct SRpcMsg *rpcMsg); void mnodeSendRedirectMsg(struct SRpcMsg *rpcMsg, bool forShell); void mnodeGetDnodeEp(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); diff --git a/source/dnode/mnode/inc/mnodeMnode.h b/source/dnode/mnode/inc/mnodeMnode.h index cee96c7bf6..0c8069a917 100644 --- a/source/dnode/mnode/inc/mnodeMnode.h +++ b/source/dnode/mnode/inc/mnodeMnode.h @@ -24,8 +24,8 @@ extern "C" { int32_t mnodeInitMnode(); void mnodeCleanupMnode(); -void mnodeGetMnodeEpSetForPeer(SRpcEpSet *epSet, bool redirect); -void mnodeGetMnodeEpSetForShell(SRpcEpSet *epSet, bool redirect); +void mnodeGetMnodeEpSetForPeer(SEpSet *epSet, bool redirect); +void mnodeGetMnodeEpSetForShell(SEpSet *epSet, bool redirect); #ifdef __cplusplus } diff --git a/source/dnode/mnode/src/mnodeMnode.c b/source/dnode/mnode/src/mnodeMnode.c index d2bcd25fc7..60e1627ad4 100644 --- a/source/dnode/mnode/src/mnodeMnode.c +++ b/source/dnode/mnode/src/mnodeMnode.c @@ -20,5 +20,5 @@ int32_t mnodeInitMnode() { return 0; } void mnodeCleanupMnode() {} -void mnodeGetMnodeEpSetForPeer(SRpcEpSet *epSet, bool redirect) {} -void mnodeGetMnodeEpSetForShell(SRpcEpSet *epSet, bool redirect) {} \ No newline at end of file +void mnodeGetMnodeEpSetForPeer(SEpSet *epSet, bool redirect) {} +void mnodeGetMnodeEpSetForShell(SEpSet *epSet, bool redirect) {} \ No newline at end of file diff --git a/source/dnode/mnode/src/mnodeWorker.c b/source/dnode/mnode/src/mnodeWorker.c index 5ac44a3a06..e4b4c47ddf 100644 --- a/source/dnode/mnode/src/mnodeWorker.c +++ b/source/dnode/mnode/src/mnodeWorker.c @@ -297,10 +297,10 @@ static void mnodeProcessWriteReq(SMnMsg *pMsg, void *unused) { if (!mnodeIsMaster()) { SMnRsp *rpcRsp = &pMsg->rpcRsp; - SRpcEpSet *epSet = rpcMallocCont(sizeof(SRpcEpSet)); + SEpSet *epSet = rpcMallocCont(sizeof(SEpSet)); mnodeGetMnodeEpSetForShell(epSet, true); rpcRsp->rsp = epSet; - rpcRsp->len = sizeof(SRpcEpSet); + rpcRsp->len = sizeof(SEpSet); mDebug("msg:%p, app:%p type:%s in write queue, is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, taosMsg[msgType], epSet->numOfEps, epSet->inUse); @@ -334,14 +334,14 @@ static void mnodeProcessReadReq(SMnMsg *pMsg, void *unused) { if (!mnodeIsMaster()) { SMnRsp *rpcRsp = &pMsg->rpcRsp; - SRpcEpSet *epSet = rpcMallocCont(sizeof(SRpcEpSet)); + SEpSet *epSet = rpcMallocCont(sizeof(SEpSet)); if (!epSet) { code = TSDB_CODE_MND_OUT_OF_MEMORY; goto PROCESS_READ_REQ_END; } mnodeGetMnodeEpSetForShell(epSet, true); rpcRsp->rsp = epSet; - rpcRsp->len = sizeof(SRpcEpSet); + rpcRsp->len = sizeof(SEpSet); mDebug("msg:%p, app:%p type:%s in mread queue is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, taosMsg[msgType], epSet->numOfEps, epSet->inUse); @@ -375,10 +375,10 @@ static void mnodeProcessPeerReq(SMnMsg *pMsg, void *unused) { if (!mnodeIsMaster()) { SMnRsp *rpcRsp = &pMsg->rpcRsp; - SRpcEpSet *epSet = rpcMallocCont(sizeof(SRpcEpSet)); + SEpSet *epSet = rpcMallocCont(sizeof(SEpSet)); mnodeGetMnodeEpSetForPeer(epSet, true); rpcRsp->rsp = epSet; - rpcRsp->len = sizeof(SRpcEpSet); + rpcRsp->len = sizeof(SEpSet); mDebug("msg:%p, ahandle:%p type:%s in mpeer queue is redirected, numOfEps:%d inUse:%d", pMsg, ahandle, taosMsg[msgType], epSet->numOfEps, epSet->inUse); diff --git a/source/dnode/mnode/src/mondeInt.c b/source/dnode/mnode/src/mondeInt.c index 343384ba67..2b17da2475 100644 --- a/source/dnode/mnode/src/mondeInt.c +++ b/source/dnode/mnode/src/mondeInt.c @@ -54,7 +54,7 @@ int64_t mnodeGetClusterId() { return tsMint.clusterId; } EMnStatus mnodeGetStatus() { return tsMint.state; } -void mnodeSendMsgToDnode(struct SRpcEpSet *epSet, struct SRpcMsg *rpcMsg) { +void mnodeSendMsgToDnode(struct SEpSet *epSet, struct SRpcMsg *rpcMsg) { (*tsMint.fp.SendMsgToDnode)(epSet, rpcMsg); } diff --git a/source/libs/transport/src/rpcMain.c b/source/libs/transport/src/rpcMain.c index e047964b94..fa49c20c77 100644 --- a/source/libs/transport/src/rpcMain.c +++ b/source/libs/transport/src/rpcMain.c @@ -54,7 +54,7 @@ typedef struct { char secret[TSDB_KEY_LEN]; // secret for the link char ckey[TSDB_KEY_LEN]; // ciphering key - void (*cfp)(SRpcMsg *, SRpcEpSet *); + void (*cfp)(SRpcMsg *, SEpSet *); int (*afp)(char *user, char *spi, char *encrypt, char *secret, char *ckey); int32_t refCount; @@ -70,7 +70,7 @@ typedef struct { typedef struct { SRpcInfo *pRpc; // associated SRpcInfo - SRpcEpSet epSet; // ip list provided by app + SEpSet epSet; // ip list provided by app void *ahandle; // handle provided by app struct SRpcConn *pConn; // pConn allocated char msgType; // message type @@ -84,7 +84,7 @@ typedef struct { int64_t rid; // refId returned by taosAddRef SRpcMsg *pRsp; // for synchronous API tsem_t *pSem; // for synchronous API - SRpcEpSet *pSet; // for synchronous API + SEpSet *pSet; // for synchronous API char msg[0]; // RpcHead starts from here } SRpcReqContext; @@ -383,7 +383,7 @@ void *rpcReallocCont(void *ptr, int contLen) { return start + sizeof(SRpcReqContext) + sizeof(SRpcHead); } -void rpcSendRequest(void *shandle, const SRpcEpSet *pEpSet, SRpcMsg *pMsg, int64_t *pRid) { +void rpcSendRequest(void *shandle, const SEpSet *pEpSet, SRpcMsg *pMsg, int64_t *pRid) { SRpcInfo *pRpc = (SRpcInfo *)shandle; SRpcReqContext *pContext; @@ -486,15 +486,15 @@ void rpcSendResponse(const SRpcMsg *pRsp) { return; } -void rpcSendRedirectRsp(void *thandle, const SRpcEpSet *pEpSet) { +void rpcSendRedirectRsp(void *thandle, const SEpSet *pEpSet) { SRpcMsg rpcMsg; memset(&rpcMsg, 0, sizeof(rpcMsg)); - rpcMsg.contLen = sizeof(SRpcEpSet); + rpcMsg.contLen = sizeof(SEpSet); rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); if (rpcMsg.pCont == NULL) return; - memcpy(rpcMsg.pCont, pEpSet, sizeof(SRpcEpSet)); + memcpy(rpcMsg.pCont, pEpSet, sizeof(SEpSet)); rpcMsg.code = TSDB_CODE_RPC_REDIRECT; rpcMsg.handle = thandle; @@ -516,7 +516,7 @@ int rpcGetConnInfo(void *thandle, SRpcConnInfo *pInfo) { return 0; } -void rpcSendRecv(void *shandle, SRpcEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) { +void rpcSendRecv(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) { SRpcReqContext *pContext; pContext = (SRpcReqContext *) ((char*)pMsg->pCont-sizeof(SRpcHead)-sizeof(SRpcReqContext)); @@ -794,9 +794,9 @@ static SRpcConn *rpcGetConnObj(SRpcInfo *pRpc, int sid, SRecvInfo *pRecv) { } static SRpcConn *rpcSetupConnToServer(SRpcReqContext *pContext) { - SRpcConn *pConn; - SRpcInfo *pRpc = pContext->pRpc; - SRpcEpSet *pEpSet = &pContext->epSet; + SRpcConn *pConn; + SRpcInfo *pRpc = pContext->pRpc; + SEpSet *pEpSet = &pContext->epSet; pConn = rpcGetConnFromCache(pRpc->pCache, pEpSet->fqdn[pEpSet->inUse], pEpSet->port[pEpSet->inUse], pContext->connType); if ( pConn == NULL || pConn->user[0] == 0) { @@ -926,7 +926,7 @@ static int rpcProcessRspHead(SRpcConn *pConn, SRpcHead *pHead) { SRpcReqContext *pContext = pConn->pContext; if (pHead->code == TSDB_CODE_RPC_REDIRECT) { - if (rpcContLenFromMsg(pHead->msgLen) < sizeof(SRpcEpSet)) { + if (rpcContLenFromMsg(pHead->msgLen) < sizeof(SEpSet)) { // if EpSet is not included in the msg, treat it as NOT_READY pHead->code = TSDB_CODE_RPC_NOT_READY; } else { @@ -1126,12 +1126,12 @@ static void rpcNotifyClient(SRpcReqContext *pContext, SRpcMsg *pMsg) { pContext->pConn = NULL; if (pContext->pRsp) { // for synchronous API - memcpy(pContext->pSet, &pContext->epSet, sizeof(SRpcEpSet)); + memcpy(pContext->pSet, &pContext->epSet, sizeof(SEpSet)); memcpy(pContext->pRsp, pMsg, sizeof(SRpcMsg)); tsem_post(pContext->pSem); } else { // for asynchronous API - SRpcEpSet *pEpSet = NULL; + SEpSet *pEpSet = NULL; if (pContext->epSet.inUse != pContext->oldInUse || pContext->redirect) pEpSet = &pContext->epSet; @@ -1175,7 +1175,7 @@ static void rpcProcessIncomingMsg(SRpcConn *pConn, SRpcHead *pHead, SRpcReqConte if (pHead->code == TSDB_CODE_RPC_REDIRECT) { pContext->numOfTry = 0; - SRpcEpSet *pEpSet = (SRpcEpSet*)pHead->content; + SEpSet *pEpSet = (SEpSet*)pHead->content; if (pEpSet->numOfEps > 0) { memcpy(&pContext->epSet, pHead->content, sizeof(pContext->epSet)); tDebug("%s, redirect is received, numOfEps:%d inUse:%d", pConn->info, pContext->epSet.numOfEps, From f36d487e7b91bcfe8d3b36ae28d5222142b2d642 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Tue, 2 Nov 2021 17:27:19 +0800 Subject: [PATCH 23/33] implement tq meta file store --- include/server/vnode/tq/tq.h | 3 + source/server/vnode/tq/inc/tqMetaStore.h | 18 ++- source/server/vnode/tq/src/tq.c | 10 +- source/server/vnode/tq/src/tqMetaStore.c | 152 +++++++++++++++++------ 4 files changed, 140 insertions(+), 43 deletions(-) diff --git a/include/server/vnode/tq/tq.h b/include/server/vnode/tq/tq.h index 3aeaf9acb6..4a93491056 100644 --- a/include/server/vnode/tq/tq.h +++ b/include/server/vnode/tq/tq.h @@ -19,6 +19,9 @@ #include "os.h" #include "tutil.h" +#define TQ_ACTION_INSERT 0x7f7f7f7fULL +#define TQ_ACTION_DELETE 0x80808080ULL + #ifdef __cplusplus extern "C" { #endif diff --git a/source/server/vnode/tq/inc/tqMetaStore.h b/source/server/vnode/tq/inc/tqMetaStore.h index 977980b2ae..65429f7af2 100644 --- a/source/server/vnode/tq/inc/tqMetaStore.h +++ b/source/server/vnode/tq/inc/tqMetaStore.h @@ -20,6 +20,17 @@ #include "tq.h" #define TQ_BUCKET_SIZE 0xFF +#define TQ_PAGE_SIZE 4096 +//key + offset + size +#define TQ_IDX_ENTRY_SIZE 24 + +inline static int TqMaxEntryOnePage() { //170 + return TQ_PAGE_SIZE / TQ_IDX_ENTRY_SIZE; +} + +inline static int TqEmptyTail() { //16 + return TQ_PAGE_SIZE - TqMaxEntryOnePage(); +} #ifdef __cplusplus extern "C" { @@ -28,8 +39,9 @@ extern "C" { typedef struct TqMetaHandle { int64_t key; int64_t offset; - void *valueInUse; - void *valueInTxn; + int64_t serializedSize; + void* valueInUse; + void* valueInTxn; } TqMetaHandle; typedef struct TqMetaList { @@ -43,7 +55,7 @@ typedef struct TqMetaList { typedef struct TqMetaStore { TqMetaList* bucket[TQ_BUCKET_SIZE]; - //a table head, key is empty + //a table head TqMetaList* unpersistHead; int fileFd; //TODO:temporaral use, to be replaced by unified tfile int idxFd; //TODO:temporaral use, to be replaced by unified tfile diff --git a/source/server/vnode/tq/src/tq.c b/source/server/vnode/tq/src/tq.c index e8076515a7..1aa8f231c3 100644 --- a/source/server/vnode/tq/src/tq.c +++ b/source/server/vnode/tq/src/tq.c @@ -26,12 +26,12 @@ static int tqProtoCheck(TmqMsgHead *pMsg) { return pMsg->protoVer == 0; } -static int tqAckOneTopic(TqBufferHandle *bhandle, TmqOneAck *pAck, TqQueryMsg** ppQuery) { +static int tqAckOneTopic(TqBufferHandle *bHandle, TmqOneAck *pAck, TqQueryMsg** ppQuery) { //clean old item and move forward int32_t consumeOffset = pAck->consumeOffset; int idx = consumeOffset % TQ_BUFFER_SIZE; - ASSERT(bhandle->buffer[idx].content && bhandle->buffer[idx].executor); - tfree(bhandle->buffer[idx].content); + ASSERT(bHandle->buffer[idx].content && bHandle->buffer[idx].executor); + tfree(bHandle->buffer[idx].content); if( 1 /* TODO: need to launch new query */) { TqQueryMsg* pNewQuery = malloc(sizeof(TqQueryMsg)); if(pNewQuery == NULL) { @@ -39,10 +39,10 @@ static int tqAckOneTopic(TqBufferHandle *bhandle, TmqOneAck *pAck, TqQueryMsg** return -1; } //TODO: lock executor - pNewQuery->exec->executor = bhandle->buffer[idx].executor; + pNewQuery->exec->executor = bHandle->buffer[idx].executor; //TODO: read from wal and assign to src pNewQuery->exec->src = 0; - pNewQuery->exec->dest = &bhandle->buffer[idx]; + pNewQuery->exec->dest = &bHandle->buffer[idx]; pNewQuery->next = *ppQuery; *ppQuery = pNewQuery; } diff --git a/source/server/vnode/tq/src/tqMetaStore.c b/source/server/vnode/tq/src/tqMetaStore.c index 90c2ad824b..72edf10a1f 100644 --- a/source/server/vnode/tq/src/tqMetaStore.c +++ b/source/server/vnode/tq/src/tqMetaStore.c @@ -13,12 +13,11 @@ * along with this program. If not, see . */ #include "tqMetaStore.h" -//TODO:replace by a abstract file layer +//TODO:replace by an abstract file layer #include #include #include -#define TQ_PAGE_SIZE 4096 #define TQ_META_NAME "tq.meta" #define TQ_IDX_NAME "tq.idx" @@ -35,21 +34,16 @@ TqMetaStore* tqStoreOpen(const char* path, int serializer(TqGroupHandle*, void**), const void* deserializer(const void*, TqGroupHandle*), void deleter(void*)) { - //concat data file name and index file name - size_t pathLen = strlen(path); - char name[pathLen+10]; - strcpy(name, path); - strcat(name, "/" TQ_META_NAME); - int fileFd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0755); - if(fileFd < 0) return NULL; TqMetaStore* pMeta = malloc(sizeof(TqMetaStore)); if(pMeta == NULL) { //close return NULL; } - memset(pMeta, 0, sizeof(TqMetaStore)); - pMeta->fileFd = fileFd; - + + //concat data file name and index file name + size_t pathLen = strlen(path); + char name[pathLen+10]; + strcpy(name, path); strcat(name, "/" TQ_IDX_NAME); int idxFd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0755); @@ -58,6 +52,7 @@ TqMetaStore* tqStoreOpen(const char* path, //free memory return NULL; } + pMeta->idxFd = idxFd; pMeta->unpersistHead = malloc(sizeof(TqMetaList)); if(pMeta->unpersistHead == NULL) { @@ -65,9 +60,37 @@ TqMetaStore* tqStoreOpen(const char* path, //free memory return NULL; } + + strcpy(name, path); + strcat(name, "/" TQ_META_NAME); + int fileFd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0755); + if(fileFd < 0) return NULL; + + memset(pMeta, 0, sizeof(TqMetaStore)); + pMeta->fileFd = fileFd; + pMeta->serializer = serializer; pMeta->deserializer = deserializer; pMeta->deleter = deleter; + + //read idx file and load into memory + char readBuf[TQ_PAGE_SIZE]; + int readSize; + while((readSize = read(idxFd, readBuf, TQ_PAGE_SIZE)) != -1) { + //loop read every entry + for(int i = 0; i < readSize; i += TQ_IDX_ENTRY_SIZE) { + TqMetaList *pNode = malloc(sizeof(TqMetaHandle)); + memset(pNode, 0, sizeof(TqMetaList)); + if(pNode == NULL) { + //TODO: free memory and return error + } + memcpy(&pNode->handle, &readBuf[i], TQ_IDX_ENTRY_SIZE); + int bucketKey = pNode->handle.key & TQ_BUCKET_SIZE; + pNode->next = pMeta->bucket[bucketKey]; + pMeta->bucket[bucketKey] = pNode; + } + } + return pMeta; } @@ -106,30 +129,66 @@ int32_t tqStoreDelete(TqMetaStore* pMeta) { return 0; } +//TODO: wrap in tfile int32_t tqStorePersist(TqMetaStore* pMeta) { - int64_t idxBuf[3]; + char writeBuf[TQ_PAGE_SIZE]; + int64_t* bufPtr = (int64_t*)writeBuf; TqMetaList *pHead = pMeta->unpersistHead; TqMetaList *pNode = pHead->unpersistNext; while(pHead != pNode) { - ASSERT(pNode->handle.valueInUse != NULL); + if(pNode->handle.valueInUse == NULL) { + //put delete token in data file + uint32_t delete = TQ_ACTION_DELETE; + int nBytes = write(pMeta->fileFd, &delete, sizeof(uint32_t)); + ASSERT(nBytes == sizeof(uint32_t)); + + //remove from list + int bucketKey = pNode->handle.key & TQ_BUCKET_SIZE; + TqMetaList* pBucketHead = pMeta->bucket[bucketKey]; + if(pBucketHead == pNode) { + pMeta->bucket[bucketKey] = pBucketHead->next; + } else { + TqMetaList* pBucketNode = pBucketHead; + while(pBucketNode->next != NULL + && pBucketNode->next != pNode) { + pBucketNode = pBucketNode->next; + } + if(pBucketNode->next != NULL) { + ASSERT(pBucketNode->next == pNode); + pBucketNode->next = pNode->next; + if(pNode->handle.valueInUse) { + pMeta->deleter(pNode->handle.valueInUse); + } + free(pNode); + } + } + } //serialize void* pBytes = NULL; int sz = pMeta->serializer(pNode->handle.valueInUse, &pBytes); ASSERT(pBytes != NULL); //get current offset //append data + int64_t offset = lseek(pMeta->fileFd, 0, SEEK_CUR); int nBytes = write(pMeta->fileFd, pBytes, sz); //TODO: handle error in tfile ASSERT(nBytes == sz); + pNode->handle.offset = offset; + pNode->handle.serializedSize = sz; + //write idx //TODO: endian check and convert - idxBuf[0] = pNode->handle.key; - idxBuf[1] = pNode->handle.offset; - idxBuf[2] = (int64_t)sz; - nBytes = write(pMeta->idxFd, idxBuf, sizeof(idxBuf)); - //TODO: handle error in tfile - ASSERT(nBytes == sizeof(idxBuf)); + *(bufPtr++) = pNode->handle.key; + *(bufPtr++) = pNode->handle.offset; + *(bufPtr++) = (int64_t)sz; + if((char*)(bufPtr + 3) > writeBuf + TQ_PAGE_SIZE) { + nBytes = write(pMeta->idxFd, writeBuf, sizeof(writeBuf)); + //TODO: handle error in tfile + ASSERT(nBytes == sizeof(writeBuf)); + memset(writeBuf, 0, TQ_PAGE_SIZE); + bufPtr = (int64_t*)writeBuf; + } //remove from unpersist list pHead->unpersistNext = pNode->unpersistNext; @@ -138,7 +197,16 @@ int32_t tqStorePersist(TqMetaStore* pMeta) { pNode->unpersistPrev = pNode->unpersistNext = NULL; pNode = pHead->unpersistNext; } - //TODO:fsync and return upper layer + //write left bytes + if((char*)bufPtr != writeBuf) { + int used = (char*)bufPtr - writeBuf; + int nBytes = write(pMeta->idxFd, writeBuf, used); + //TODO: handle error in tfile + ASSERT(nBytes == used); + } + //TODO: using fsync in tfile + fsync(pMeta->idxFd); + fsync(pMeta->fileFd); return 0; } @@ -151,10 +219,24 @@ static int32_t tqHandlePutCommitted(TqMetaStore* pMeta, int64_t key, void* value pMeta->deleter(pNode->handle.valueInUse); //change pointer ownership pNode->handle.valueInUse = value; + return 0; } else { pNode = pNode->next; } } + TqMetaList *pNewNode = malloc(sizeof(TqMetaList)); + if(pNewNode == NULL) { + //TODO: memory error + return -1; + } + memset(pNewNode, 0, sizeof(TqMetaList)); + pNewNode->handle.key = key; + pNewNode->handle.valueInUse = value; + //put into unpersist list + pNewNode->unpersistPrev = pMeta->unpersistHead; + pNewNode->unpersistNext = pMeta->unpersistHead->unpersistNext; + pMeta->unpersistHead->unpersistNext->unpersistPrev = pNewNode; + pMeta->unpersistHead->unpersistNext = pNewNode; return 0; } @@ -184,10 +266,19 @@ int32_t tqHandlePut(TqMetaStore* pMeta, int64_t key, void* value) { pMeta->deleter(pNode->handle.valueInTxn); //change pointer ownership pNode->handle.valueInTxn = value; + return 0; } else { pNode = pNode->next; } } + TqMetaList *pNewNode = malloc(sizeof(TqMetaList)); + if(pNewNode == NULL) { + //TODO: memory error + return -1; + } + memset(pNewNode, 0, sizeof(TqMetaList)); + pNewNode->handle.key = key; + pNewNode->handle.valueInTxn = value; return 0; } @@ -254,24 +345,15 @@ int32_t tqHandleDel(TqMetaStore* pMeta, int64_t key) { TqMetaList* pNode = pMeta->bucket[bucketKey]; while(pNode) { if(pNode->handle.key == key) { - if(pNode->handle.valueInUse != NULL) { - pMeta->deleter(pNode->handle.valueInUse); - pNode->handle.valueInUse = NULL; - //if not in unpersist, put into unpersist - if(pNode->unpersistNext == NULL) { - pNode->unpersistNext = pMeta->unpersistHead->unpersistNext; - pNode->unpersistPrev = pMeta->unpersistHead; - pMeta->unpersistHead->unpersistNext->unpersistPrev = pNode; - pMeta->unpersistHead->unpersistNext = pNode; - } - return 0; - } - return -1; + pMeta->deleter(pNode->handle.valueInTxn); + pNode->handle.valueInTxn = NULL; + return 0; } else { pNode = pNode->next; } } - return -2; + //no such key + return -1; } int32_t tqHandleClear(TqMetaStore* pMeta, int64_t key) { From 99c716281654d7758309cc455685c66f98c8401e Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 2 Nov 2021 17:46:47 +0800 Subject: [PATCH 24/33] refact dnode-mnode --- include/common/taosmsg.h | 6 +- include/server/mnode/mnode.h | 119 ++--------- include/server/vnode/vnode.h | 40 +--- include/util/taoserror.h | 12 +- source/dnode/mgmt/src/dnodeDnode.c | 57 +++--- source/dnode/mgmt/src/dnodeMnode.c | 269 ++++++++++++++++++++++--- source/dnode/mnode/inc/mnodeInt.h | 2 + source/dnode/mnode/src/mondeInt.c | 3 + source/dnode/vnode/impl/src/vnodeInt.c | 2 +- source/util/src/terror.c | 2 + 10 files changed, 304 insertions(+), 208 deletions(-) diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index 9dc1551970..e798a0c42a 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -832,10 +832,8 @@ typedef struct { } SCreateDnodeMsg, SDropDnodeMsg; typedef struct { - int32_t dnodeId; - int32_t mnodeNum; - SDnodeEp mnodeEps[]; -} SCreateMnodeMsg; + int32_t dnodeId; +} SCreateMnodeMsg, SDropMnodeMsg; typedef struct { int32_t dnodeId; diff --git a/include/server/mnode/mnode.h b/include/server/mnode/mnode.h index e41d157057..e0fcdec560 100644 --- a/include/server/mnode/mnode.h +++ b/include/server/mnode/mnode.h @@ -20,82 +20,6 @@ extern "C" { #endif -typedef enum { MN_STATUS_UNINIT = 0, MN_STATUS_INIT = 1, MN_STATUS_READY = 2, MN_STATUS_CLOSING = 3 } EMnStatus; - -typedef struct { - /** - * Send messages to other dnodes, such as create vnode message. - * - * @param epSet, the endpoint list of the dnodes. - * @param rpcMsg, message to be sent. - */ - void (*SendMsgToDnode)(struct SEpSet *epSet, struct SRpcMsg *rpcMsg); - - /** - * Send messages to mnode, such as config message. - * - * @param rpcMsg, message to be sent. - */ - void (*SendMsgToMnode)(struct SRpcMsg *rpcMsg); - - /** - * Send redirect message to dnode or shell. - * - * @param rpcMsg, message to be sent. - * @param forShell, used to identify whether to send to shell or dnode. - */ - void (*SendRedirectMsg)(struct SRpcMsg *rpcMsg, bool forShell); - - /** - * Get the corresponding endpoint information from dnodeId. - * - * @param dnode, the instance of dDnode module. - * @param dnodeId, the id ot dnode. - * @param ep, the endpoint of dnode. - * @param fqdn, the fqdn of dnode. - * @param port, the port of dnode. - */ - void (*GetDnodeEp)(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); -} SMnodeFp; - -typedef struct { - SMnodeFp fp; - int64_t clusterId; - int32_t dnodeId; -} SMnodePara; - -/** - * Initialize and start mnode module. - * - * @param para, initialization parameters. - * @return Error code. - */ -int32_t mnodeInit(SMnodePara para); - -/** - * Stop and cleanup mnode module. - */ -void mnodeCleanup(); - -/** - * Deploy mnode instances in dnode. - * - * @return Error Code. - */ -int32_t mnodeDeploy(); - -/** - * Delete the mnode instance deployed in dnode. - */ -void mnodeUnDeploy(); - -/** - * Whether the mnode is in service. - * - * @return Server status. - */ -EMnStatus mnodeGetStatus(); - typedef struct { int64_t numOfDnode; int64_t numOfMnode; @@ -109,32 +33,29 @@ typedef struct { int64_t compStorage; } SMnodeStat; -/** - * Get the statistical information of Mnode. - * - * @param stat, statistical information. - * @return Error Code. - */ -int32_t mnodeGetStatistics(SMnodeStat *stat); +typedef struct { + void (*SendMsgToDnode)(struct SEpSet *epSet, struct SRpcMsg *rpcMsg); + void (*SendMsgToMnode)(struct SRpcMsg *rpcMsg); + void (*SendRedirectMsg)(struct SRpcMsg *rpcMsg, bool forShell); + void (*GetDnodeEp)(int32_t dnodeId, char *ep, char *fqdn, uint16_t *port); +} SMnodeFp; -/** - * Get the auth information of Mnode. - * - * @param user, username. - * @param spi, security parameter index. - * @param encrypt, encrypt algorithm. - * @param secret, key for authentication. - * @param ckey, ciphering key. - * @return Error Code. - */ +typedef struct { + SMnodeFp fp; + int64_t clusterId; + int32_t dnodeId; +} SMnodePara; + +int32_t mnodeInit(SMnodePara para); +void mnodeCleanup(); +int32_t mnodeDeploy(); +void mnodeUnDeploy(); +int32_t mnodeStart(); +void mnodeStop(); + +int32_t mnodeGetStatistics(SMnodeStat *stat); int32_t mnodeRetriveAuth(char *user, char *spi, char *encrypt, char *secret, char *ckey); -/** - * Interface for processing messages. - * - * @param rpcMsg, message to be processed. - * @return Error code. - */ void mnodeProcessMsg(SRpcMsg *rpcMsg); #ifdef __cplusplus diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index a9f40aad77..c4f8df79c1 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -66,41 +66,12 @@ typedef struct SVnodeMsg { char pCont[]; } SVnodeMsg; -/** - * Start initialize vnode module. - * - * @return Error code. - */ int32_t vnodeInit(); +void vnodeCleanup(); -/** - * Cleanup vnode module. - */ -void vnodeCleanup(); - -/** - * Get the statistical information of vnode. - * - * @param pVnode, - * @param pStat, statistical information. - * @return Error Code. - */ int32_t vnodeGetStatistics(SVnode *pVnode, SVnodeStatisic *pStat); - -/** - * Get the status of all vnodes. - * - * @param pVnode, - * @param status, status information. - * @return Error Code. - */ int32_t vnodeGetStatus(SVnode *pVnode, SVnodeStatus *pStatus); -/** - * Operation functions of vnode - * - * @return Error Code. - */ SVnode *vnodeOpen(int32_t vgId, const char *path); void vnodeClose(SVnode *pVnode); int32_t vnodeAlter(SVnode *pVnode, const SVnodeCfg *pCfg); @@ -109,14 +80,7 @@ int32_t vnodeDrop(SVnode *pVnode); int32_t vnodeCompact(SVnode *pVnode); int32_t vnodeSync(SVnode *pVnode); -/** - * Interface for processing messages. - * - * @param pVnode, - * @param pMsg, message to be processed. - * - */ -int32_t vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg); +void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg); #ifdef __cplusplus } diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 76c5f575a5..0579ae46bc 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -216,11 +216,13 @@ int32_t* taosGetErrno(); // dnode #define TSDB_CODE_DND_MSG_NOT_PROCESSED TAOS_DEF_ERROR_CODE(0, 0x0400) //"Message not processed") #define TSDB_CODE_DND_OUT_OF_MEMORY TAOS_DEF_ERROR_CODE(0, 0x0401) //"Dnode out of memory") -#define TSDB_CODE_DND_NO_WRITE_ACCESS TAOS_DEF_ERROR_CODE(0, 0x0402) //"No permission for disk files in dnode") -#define TSDB_CODE_DND_INVALID_MSG_LEN TAOS_DEF_ERROR_CODE(0, 0x0403) //"Invalid message length") -#define TSDB_CODE_DND_ACTION_IN_PROGRESS TAOS_DEF_ERROR_CODE(0, 0x0404) //"Action in progress") -#define TSDB_CODE_DND_TOO_MANY_VNODES TAOS_DEF_ERROR_CODE(0, 0x0405) //"Too many vnode directories") -#define TSDB_CODE_DND_EXITING TAOS_DEF_ERROR_CODE(0, 0x0406) //"Dnode is exiting" +#define TSDB_CODE_DND_DNODE_ID_NOT_MATCHED TAOS_DEF_ERROR_CODE(0, 0x0402) //"Dnode Id not matched") +#define TSDB_CODE_DND_MNODE_ALREADY_DROPPED TAOS_DEF_ERROR_CODE(0, 0x0403) //"Mnode already deployed") +#define TSDB_CODE_DND_NO_WRITE_ACCESS TAOS_DEF_ERROR_CODE(0, 0x0404) //"No permission for disk files in dnode") +#define TSDB_CODE_DND_INVALID_MSG_LEN TAOS_DEF_ERROR_CODE(0, 0x0405) //"Invalid message length") +#define TSDB_CODE_DND_ACTION_IN_PROGRESS TAOS_DEF_ERROR_CODE(0, 0x0406) //"Action in progress") +#define TSDB_CODE_DND_TOO_MANY_VNODES TAOS_DEF_ERROR_CODE(0, 0x0407) //"Too many vnode directories") +#define TSDB_CODE_DND_EXITING TAOS_DEF_ERROR_CODE(0, 0x0408) //"Dnode is exiting" // vnode #define TSDB_CODE_VND_ACTION_IN_PROGRESS TAOS_DEF_ERROR_CODE(0, 0x0500) //"Action in progress") diff --git a/source/dnode/mgmt/src/dnodeDnode.c b/source/dnode/mgmt/src/dnodeDnode.c index 37dc9f05a1..5bf5b1d56a 100644 --- a/source/dnode/mgmt/src/dnodeDnode.c +++ b/source/dnode/mgmt/src/dnodeDnode.c @@ -35,7 +35,6 @@ static struct { int8_t threadStop; pthread_t *threadId; pthread_mutex_t mutex; - MsgFp msgFp[TSDB_MSG_TYPE_MAX]; } tsDnode = {0}; int32_t dnodeGetDnodeId() { @@ -127,7 +126,7 @@ static void dnodeUpdateMnodeEpSet(SEpSet *pEpSet) { pthread_mutex_unlock(&tsDnode.mutex); } -static void dnodePrintEps() { +static void dnodePrintDnodes() { dDebug("print dnode endpoint list, num:%d", tsDnode.dnodeEps->dnodeNum); for (int32_t i = 0; i < tsDnode.dnodeEps->dnodeNum; i++) { SDnodeEp *ep = &tsDnode.dnodeEps->dnodeEps[i]; @@ -135,7 +134,7 @@ static void dnodePrintEps() { } } -static void dnodeResetEps(SDnodeEps *pEps) { +static void dnodeResetDnodes(SDnodeEps *pEps) { assert(pEps != NULL); int32_t size = sizeof(SDnodeEps) + pEps->dnodeNum * sizeof(SDnodeEp); @@ -171,7 +170,7 @@ static void dnodeResetEps(SDnodeEps *pEps) { taosHashPut(tsDnode.dnodeHash, &ep->dnodeId, sizeof(int32_t), ep, sizeof(SDnodeEp)); } - dnodePrintEps(); + dnodePrintDnodes(); } static bool dnodeIsEpChanged(int32_t dnodeId, char *epStr) { @@ -189,7 +188,7 @@ static bool dnodeIsEpChanged(int32_t dnodeId, char *epStr) { return changed; } -static int32_t dnodeReadEps() { +static int32_t dnodeReadDnodes() { int32_t len = 0; int32_t maxLen = 30000; char *content = calloc(1, maxLen + 1); @@ -199,59 +198,59 @@ static int32_t dnodeReadEps() { fp = fopen(tsDnode.file, "r"); if (!fp) { dDebug("file %s not exist", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } len = (int32_t)fread(content, 1, maxLen, fp); if (len <= 0) { dError("failed to read %s since content is null", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } content[len] = 0; root = cJSON_Parse(content); if (root == NULL) { dError("failed to read %s since invalid json format", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId"); if (!dnodeId || dnodeId->type != cJSON_String) { dError("failed to read %s since dnodeId not found", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } tsDnode.dnodeId = atoi(dnodeId->valuestring); cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId"); if (!clusterId || clusterId->type != cJSON_String) { dError("failed to read %s since clusterId not found", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } tsDnode.clusterId = atoll(clusterId->valuestring); cJSON *dropped = cJSON_GetObjectItem(root, "dropped"); if (!dropped || dropped->type != cJSON_String) { dError("failed to read %s since dropped not found", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } tsDnode.dropped = atoi(dropped->valuestring); cJSON *dnodeInfos = cJSON_GetObjectItem(root, "dnodeInfos"); if (!dnodeInfos || dnodeInfos->type != cJSON_Array) { dError("failed to read %s since dnodeInfos not found", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } int32_t dnodeInfosSize = cJSON_GetArraySize(dnodeInfos); if (dnodeInfosSize <= 0) { dError("failed to read %s since dnodeInfos size:%d invalid", tsDnode.file, dnodeInfosSize); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } tsDnode.dnodeEps = calloc(1, dnodeInfosSize * sizeof(SDnodeEp) + sizeof(SDnodeEps)); if (tsDnode.dnodeEps == NULL) { dError("failed to calloc dnodeEpList since %s", strerror(errno)); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } tsDnode.dnodeEps->dnodeNum = dnodeInfosSize; @@ -264,36 +263,36 @@ static int32_t dnodeReadEps() { cJSON *dnodeId = cJSON_GetObjectItem(dnodeInfo, "dnodeId"); if (!dnodeId || dnodeId->type != cJSON_String) { dError("failed to read %s, dnodeId not found", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } pEp->dnodeId = atoi(dnodeId->valuestring); cJSON *isMnode = cJSON_GetObjectItem(dnodeInfo, "isMnode"); if (!isMnode || isMnode->type != cJSON_String) { dError("failed to read %s, isMnode not found", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } pEp->isMnode = atoi(isMnode->valuestring); cJSON *dnodeFqdn = cJSON_GetObjectItem(dnodeInfo, "dnodeFqdn"); if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) { dError("failed to read %s, dnodeFqdn not found", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } tstrncpy(pEp->dnodeFqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN); cJSON *dnodePort = cJSON_GetObjectItem(dnodeInfo, "dnodePort"); if (!dnodePort || dnodePort->type != cJSON_String) { dError("failed to read %s, dnodePort not found", tsDnode.file); - goto PRASE_EPS_OVER; + goto PRASE_DNODE_OVER; } pEp->dnodePort = atoi(dnodePort->valuestring); } dInfo("succcessed to read file %s", tsDnode.file); - dnodePrintEps(); + dnodePrintDnodes(); -PRASE_EPS_OVER: +PRASE_DNODE_OVER: if (content != NULL) free(content); if (root != NULL) cJSON_Delete(root); if (fp != NULL) fclose(fp); @@ -303,13 +302,13 @@ PRASE_EPS_OVER: return -1; } - dnodeResetEps(tsDnode.dnodeEps); + dnodeResetDnodes(tsDnode.dnodeEps); terrno = 0; return 0; } -static int32_t dnodeWriteEps() { +static int32_t dnodeWriteDnodes() { FILE *fp = fopen(tsDnode.file, "w"); if (!fp) { dError("failed to write %s since %s", tsDnode.file, strerror(errno)); @@ -391,7 +390,7 @@ static void dnodeUpdateCfg(SDnodeCfg *pCfg) { tsDnode.dropped = pCfg->dropped; dInfo("dnodeId is set to %d, clusterId is set to %" PRId64, pCfg->dnodeId, pCfg->clusterId); - dnodeWriteEps(); + dnodeWriteDnodes(); pthread_mutex_unlock(&tsDnode.mutex); } @@ -401,13 +400,13 @@ static void dnodeUpdateDnodeEps(SDnodeEps *pEps) { pthread_mutex_lock(&tsDnode.mutex); if (pEps->dnodeNum != tsDnode.dnodeEps->dnodeNum) { - dnodeResetEps(pEps); - dnodeWriteEps(); + dnodeResetDnodes(pEps); + dnodeWriteDnodes(); } else { int32_t size = pEps->dnodeNum * sizeof(SDnodeEp) + sizeof(SDnodeEps); if (memcmp(tsDnode.dnodeEps, pEps, size) != 0) { - dnodeResetEps(pEps); - dnodeWriteEps(); + dnodeResetDnodes(pEps); + dnodeWriteDnodes(); } } @@ -493,9 +492,9 @@ int32_t dnodeInitDnode() { return TSDB_CODE_DND_OUT_OF_MEMORY; } - int32_t code = dnodeReadEps(); + int32_t code = dnodeReadDnodes(); if (code != 0) { - dError("failed to read dnode endpoint file since %s", tstrerror(code)); + dError("failed to read file:%s since %s", tsDnode.file, tstrerror(code)); return code; } diff --git a/source/dnode/mgmt/src/dnodeMnode.c b/source/dnode/mgmt/src/dnodeMnode.c index 54fa847790..4414d57f3e 100644 --- a/source/dnode/mgmt/src/dnodeMnode.c +++ b/source/dnode/mgmt/src/dnodeMnode.c @@ -17,9 +17,211 @@ #include "dnodeMnode.h" #include "dnodeDnode.h" #include "dnodeTransport.h" +#include "cJSON.h" #include "mnode.h" +static struct { + int8_t deployed; + int8_t dropped; + char file[PATH_MAX + 20]; + pthread_mutex_t mutex; +} tsMnode = {0}; + +static int32_t dnodeReadMnode() { + int32_t len = 0; + int32_t maxLen = 300; + char *content = calloc(1, maxLen + 1); + cJSON *root = NULL; + FILE *fp = NULL; + + fp = fopen(tsMnode.file, "r"); + if (!fp) { + dDebug("file %s not exist", tsMnode.file); + goto PRASE_MNODE_OVER; + } + + len = (int32_t)fread(content, 1, maxLen, fp); + if (len <= 0) { + dError("failed to read %s since content is null", tsMnode.file); + goto PRASE_MNODE_OVER; + } + + content[len] = 0; + root = cJSON_Parse(content); + if (root == NULL) { + dError("failed to read %s since invalid json format", tsMnode.file); + goto PRASE_MNODE_OVER; + } + + cJSON *deployed = cJSON_GetObjectItem(root, "deployed"); + if (!deployed || deployed->type != cJSON_String) { + dError("failed to read %s since deployed not found", tsMnode.file); + goto PRASE_MNODE_OVER; + } + tsMnode.deployed = atoi(deployed->valuestring); + + cJSON *dropped = cJSON_GetObjectItem(root, "dropped"); + if (!dropped || dropped->type != cJSON_String) { + dError("failed to read %s since dropped not found", tsMnode.file); + goto PRASE_MNODE_OVER; + } + tsMnode.dropped = atoi(dropped->valuestring); + + dInfo("succcessed to read file %s", tsMnode.file); + +PRASE_MNODE_OVER: + if (content != NULL) free(content); + if (root != NULL) cJSON_Delete(root); + if (fp != NULL) fclose(fp); + + return 0; +} + +static int32_t dnodeWriteMnode() { + FILE *fp = fopen(tsMnode.file, "w"); + if (!fp) { + dError("failed to write %s since %s", tsMnode.file, strerror(errno)); + return -1; + } + + int32_t len = 0; + int32_t maxLen = 300; + char *content = calloc(1, maxLen + 1); + + len += snprintf(content + len, maxLen - len, "{\n"); + len += snprintf(content + len, maxLen - len, " \"deployed\": \"%d\",\n", tsMnode.dropped); + len += snprintf(content + len, maxLen - len, " \"dropped\": \"%d\",\n", tsMnode.dropped); + len += snprintf(content + len, maxLen - len, "}\n"); + + fwrite(content, 1, len, fp); + taosFsyncFile(fileno(fp)); + fclose(fp); + free(content); + terrno = 0; + + dInfo("successed to write %s", tsMnode.file); + return 0; +} + +static int32_t dnodeStartMnode(SCreateMnodeMsg *pCfg) { + int32_t code = 0; + + if (pCfg->dnodeId != dnodeGetDnodeId()) { + code = TSDB_CODE_DND_DNODE_ID_NOT_MATCHED; + dError("failed to start mnode since %s", tstrerror(code)); + return code; + } + + if (tsMnode.dropped) { + code = TSDB_CODE_DND_MNODE_ALREADY_DROPPED; + dError("failed to start mnode since %s", tstrerror(code)); + return code; + } + + if (tsMnode.deployed) { + dError("failed to start mnode since its already deployed"); + return 0; + } + + tsMnode.deployed = 1; + tsMnode.dropped = 0; + + code = dnodeWriteMnode(); + if (code != 0) { + tsMnode.deployed = 0; + dError("failed to start mnode since %s", tstrerror(code)); + return code; + } + + code = mnodeDeploy(); + if (code != 0) { + tsMnode.deployed = 0; + dError("failed to start mnode since %s", tstrerror(code)); + return code; + } + + code = mnodeStart(); + if (code != 0) { + tsMnode.deployed = 0; + dError("failed to start mnode since %s", tstrerror(code)); + return code; + } + + tsMnode.deployed = 1; + return 0; +} + +static int32_t dnodeDropMnode(SDropMnodeMsg *pCfg) { + int32_t code = 0; + + if (pCfg->dnodeId != dnodeGetDnodeId()) { + code = TSDB_CODE_DND_DNODE_ID_NOT_MATCHED; + dError("failed to drop mnode since %s", tstrerror(code)); + return code; + } + + if (tsMnode.dropped) { + code = TSDB_CODE_DND_MNODE_ALREADY_DROPPED; + dError("failed to drop mnode since %s", tstrerror(code)); + return code; + } + + if (!tsMnode.deployed) { + dError("failed to drop mnode since not deployed"); + return 0; + } + + mnodeStop(); + + tsMnode.deployed = 0; + tsMnode.dropped = 1; + + code = dnodeWriteMnode(); + if (code != 0) { + tsMnode.deployed = 1; + tsMnode.dropped = 0; + dError("failed to drop mnode since %s", tstrerror(code)); + return code; + } + + mnodeUnDeploy(); + + tsMnode.deployed = 0; + return 0; +} + +static void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg) { + SCreateMnodeMsg *pCfg = pMsg->pCont; + pCfg->dnodeId = htonl(pCfg->dnodeId); + + int32_t code = dnodeStartMnode(pCfg); + SRpcMsg rspMsg = {.handle = pMsg->handle, .code = code}; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); +} + +static void dnodeProcessDropMnodeReq(SRpcMsg *pMsg) { + SDropMnodeMsg *pCfg = pMsg->pCont; + pCfg->dnodeId = htonl(pCfg->dnodeId); + + int32_t code = dnodeDropMnode(pCfg); + SRpcMsg rspMsg = {.handle = pMsg->handle, .code = code}; + rpcSendResponse(&rspMsg); + rpcFreeCont(pMsg->pCont); +} + +static bool dnodeNeedDeployMnode() { + if (dnodeGetDnodeId() > 0) return false; + if (dnodeGetClusterId() > 0) return false; + if (strcmp(tsFirst, tsLocalEp) != 0) return false; + return true; +} + int32_t dnodeInitMnode() { + tsMnode.dropped = 0; + tsMnode.deployed = 0; + snprintf(tsMnode.file, sizeof(tsMnode.file), "%s/mnode.json", tsDnodeDir); + SMnodePara para; para.fp.GetDnodeEp = dnodeGetDnodeEp; para.fp.SendMsgToDnode = dnodeSendMsgToDnode; @@ -28,46 +230,49 @@ int32_t dnodeInitMnode() { para.dnodeId = dnodeGetDnodeId(); para.clusterId = dnodeGetClusterId(); - return mnodeInit(para); -} - -void dnodeCleanupMnode() { mnodeCleanup(); } - -static int32_t dnodeStartMnode(SRpcMsg *pMsg) { - SCreateMnodeMsg *pCfg = pMsg->pCont; - pCfg->dnodeId = htonl(pCfg->dnodeId); - pCfg->mnodeNum = htonl(pCfg->mnodeNum); - for (int32_t i = 0; i < pCfg->mnodeNum; ++i) { - pCfg->mnodeEps[i].dnodeId = htonl(pCfg->mnodeEps[i].dnodeId); - pCfg->mnodeEps[i].dnodePort = htons(pCfg->mnodeEps[i].dnodePort); + int32_t code = mnodeInit(para); + if (code != 0) { + dError("failed to init mnode module since %s", tstrerror(code)); + return code; } - if (pCfg->dnodeId != dnodeGetDnodeId()) { - dDebug("dnode:%d, in create meps msg is not equal with saved dnodeId:%d", pCfg->dnodeId, dnodeGetDnodeId()); - return TSDB_CODE_MND_DNODE_ID_NOT_CONFIGURED; + code = dnodeReadMnode(); + if (code != 0) { + dError("failed to read file:%s since %s", tsMnode.file, tstrerror(code)); + return code; } - if (mnodeGetStatus() == MN_STATUS_READY) return 0; + if (tsMnode.dropped) { + dError("mnode already dropped, undeploy it"); + mnodeUnDeploy(); + return 0; + } - return mnodeDeploy(); + if (!tsMnode.deployed) { + bool needDeploy = dnodeNeedDeployMnode(); + if (needDeploy) { + code = mnodeDeploy(); + } else { + return 0; + } + + if (code != 0) { + dError("failed to deploy mnode since %s", tstrerror(code)); + return code; + } + + tsMnode.deployed = 1; + } + + return mnodeStart(); } -void dnodeProcessCreateMnodeReq(SRpcMsg *pMsg) { - int32_t code = dnodeStartMnode(pMsg); +void dnodeCleanupMnode() { + if (tsMnode.deployed) { + mnodeStop(); + } - SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; - - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); -} - -void dnodeProcessDropMnodeReq(SRpcMsg *pMsg) { - int32_t code = dnodeStartMnode(pMsg); - - SRpcMsg rspMsg = {.handle = pMsg->handle, .pCont = NULL, .contLen = 0, .code = code}; - - rpcSendResponse(&rspMsg); - rpcFreeCont(pMsg->pCont); + mnodeCleanup(); } void dnodeProcessMnodeMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { diff --git a/source/dnode/mnode/inc/mnodeInt.h b/source/dnode/mnode/inc/mnodeInt.h index 5b552e5c51..5fcd7173ee 100644 --- a/source/dnode/mnode/inc/mnodeInt.h +++ b/source/dnode/mnode/inc/mnodeInt.h @@ -22,6 +22,8 @@ extern "C" { #endif +typedef enum { MN_STATUS_UNINIT = 0, MN_STATUS_INIT = 1, MN_STATUS_READY = 2, MN_STATUS_CLOSING = 3 } EMnStatus; + tmr_h mnodeGetTimer(); int32_t mnodeGetDnodeId(); int64_t mnodeGetClusterId(); diff --git a/source/dnode/mnode/src/mondeInt.c b/source/dnode/mnode/src/mondeInt.c index 2b17da2475..669273d8dc 100644 --- a/source/dnode/mnode/src/mondeInt.c +++ b/source/dnode/mnode/src/mondeInt.c @@ -250,3 +250,6 @@ void mnodeCleanup() { mInfo("mnode is cleaned up"); } } + +int32_t mnodeStart() { return 0; } +void mnodeStop() {} \ No newline at end of file diff --git a/source/dnode/vnode/impl/src/vnodeInt.c b/source/dnode/vnode/impl/src/vnodeInt.c index 7a395706e9..6f83542cef 100644 --- a/source/dnode/vnode/impl/src/vnodeInt.c +++ b/source/dnode/vnode/impl/src/vnodeInt.c @@ -30,4 +30,4 @@ int32_t vnodeDrop(SVnode *pVnode) { return 0; } int32_t vnodeCompact(SVnode *pVnode) { return 0; } int32_t vnodeSync(SVnode *pVnode) { return 0; } -int32_t vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg) { return 0; } +void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg) {} diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 8e5d7a47fd..22fbeb1883 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -228,6 +228,8 @@ TAOS_DEFINE_ERROR(TSDB_CODE_MND_TOPIC_ALREADY_EXIST, "Topic already exists" // dnode TAOS_DEFINE_ERROR(TSDB_CODE_DND_MSG_NOT_PROCESSED, "Message not processed") TAOS_DEFINE_ERROR(TSDB_CODE_DND_OUT_OF_MEMORY, "Dnode out of memory") +TAOS_DEFINE_ERROR(TSDB_CODE_DND_DNODE_ID_NOT_MATCHED, "Dnode Id not matched") +TAOS_DEFINE_ERROR(TSDB_CODE_DND_MNODE_ALREADY_DROPPED, "Mnode already deployed") TAOS_DEFINE_ERROR(TSDB_CODE_DND_NO_WRITE_ACCESS, "No permission for disk files in dnode") TAOS_DEFINE_ERROR(TSDB_CODE_DND_INVALID_MSG_LEN, "Invalid message length") TAOS_DEFINE_ERROR(TSDB_CODE_DND_ACTION_IN_PROGRESS, "Action in progress") From cfae3b94523610678dd5eb97b9e7d8b774a06555 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 2 Nov 2021 18:26:58 +0800 Subject: [PATCH 25/33] remove files --- src/plugins/CMakeLists.txt | 8 - src/plugins/http/CMakeLists.txt | 24 - src/plugins/http/inc/httpAuth.h | 23 - src/plugins/http/inc/httpContext.h | 34 - src/plugins/http/inc/httpGcHandle.h | 33 - src/plugins/http/inc/httpGcJson.h | 31 - src/plugins/http/inc/httpGzip.h | 43 - src/plugins/http/inc/httpHandle.h | 23 - src/plugins/http/inc/httpInt.h | 204 --- src/plugins/http/inc/httpJson.h | 106 -- src/plugins/http/inc/httpLog.h | 31 - src/plugins/http/inc/httpMetricsHandle.h | 27 - src/plugins/http/inc/httpParser.h | 184 --- src/plugins/http/inc/httpQueue.h | 35 - src/plugins/http/inc/httpResp.h | 40 - src/plugins/http/inc/httpRestHandle.h | 33 - src/plugins/http/inc/httpRestJson.h | 56 - src/plugins/http/inc/httpServer.h | 27 - src/plugins/http/inc/httpSession.h | 27 - src/plugins/http/inc/httpSql.h | 41 - src/plugins/http/inc/httpSystem.h | 34 - src/plugins/http/inc/httpTgHandle.h | 35 - src/plugins/http/inc/httpTgJson.h | 31 - src/plugins/http/inc/httpUtil.h | 36 - src/plugins/http/src/httpAuth.c | 117 -- src/plugins/http/src/httpContext.c | 240 ---- src/plugins/http/src/httpGcHandle.c | 304 ----- src/plugins/http/src/httpGcJson.c | 272 ---- src/plugins/http/src/httpGzip.c | 165 --- src/plugins/http/src/httpHandle.c | 60 - src/plugins/http/src/httpJson.c | 534 -------- src/plugins/http/src/httpMetricsHandle.c | 184 --- src/plugins/http/src/httpParser.c | 1182 ----------------- src/plugins/http/src/httpQueue.c | 157 --- src/plugins/http/src/httpResp.c | 211 --- src/plugins/http/src/httpRestHandle.c | 251 ---- src/plugins/http/src/httpRestJson.c | 264 ---- src/plugins/http/src/httpServer.c | 417 ------ src/plugins/http/src/httpSession.c | 125 -- src/plugins/http/src/httpSql.c | 491 ------- src/plugins/http/src/httpSystem.c | 123 -- src/plugins/http/src/httpTgHandle.c | 917 ------------- src/plugins/http/src/httpTgJson.c | 151 --- src/plugins/http/src/httpUtil.c | 497 ------- src/plugins/mqtt/CMakeLists.txt | 32 - src/plugins/mqtt/inc/mqttInit.h | 77 -- src/plugins/mqtt/inc/mqttLog.h | 30 - src/plugins/mqtt/inc/mqttPayload.h | 29 - src/plugins/mqtt/src/mqttPayload.c | 159 --- src/plugins/mqtt/src/mqttSystem.c | 146 --- src/sync/CMakeLists.txt | 16 - src/sync/inc/syncInt.h | 143 -- src/sync/inc/syncMain.h | 33 - src/sync/inc/syncMsg.h | 141 -- src/sync/inc/syncTcp.h | 43 - src/sync/src/syncArbitrator.c | 189 --- src/sync/src/syncMain.c | 1519 ---------------------- src/sync/src/syncMsg.c | 110 -- src/sync/src/syncRestore.c | 312 ----- src/sync/src/syncRetrieve.c | 472 ------- src/sync/src/syncTcp.c | 338 ----- src/sync/test/CMakeLists.txt | 15 - src/sync/test/syncClient.c | 194 --- src/sync/test/syncServer.c | 472 ------- 64 files changed, 12298 deletions(-) delete mode 100644 src/plugins/CMakeLists.txt delete mode 100644 src/plugins/http/CMakeLists.txt delete mode 100644 src/plugins/http/inc/httpAuth.h delete mode 100644 src/plugins/http/inc/httpContext.h delete mode 100644 src/plugins/http/inc/httpGcHandle.h delete mode 100644 src/plugins/http/inc/httpGcJson.h delete mode 100644 src/plugins/http/inc/httpGzip.h delete mode 100644 src/plugins/http/inc/httpHandle.h delete mode 100644 src/plugins/http/inc/httpInt.h delete mode 100644 src/plugins/http/inc/httpJson.h delete mode 100644 src/plugins/http/inc/httpLog.h delete mode 100644 src/plugins/http/inc/httpMetricsHandle.h delete mode 100644 src/plugins/http/inc/httpParser.h delete mode 100644 src/plugins/http/inc/httpQueue.h delete mode 100644 src/plugins/http/inc/httpResp.h delete mode 100644 src/plugins/http/inc/httpRestHandle.h delete mode 100644 src/plugins/http/inc/httpRestJson.h delete mode 100644 src/plugins/http/inc/httpServer.h delete mode 100644 src/plugins/http/inc/httpSession.h delete mode 100644 src/plugins/http/inc/httpSql.h delete mode 100644 src/plugins/http/inc/httpSystem.h delete mode 100644 src/plugins/http/inc/httpTgHandle.h delete mode 100644 src/plugins/http/inc/httpTgJson.h delete mode 100644 src/plugins/http/inc/httpUtil.h delete mode 100644 src/plugins/http/src/httpAuth.c delete mode 100644 src/plugins/http/src/httpContext.c delete mode 100644 src/plugins/http/src/httpGcHandle.c delete mode 100644 src/plugins/http/src/httpGcJson.c delete mode 100644 src/plugins/http/src/httpGzip.c delete mode 100644 src/plugins/http/src/httpHandle.c delete mode 100644 src/plugins/http/src/httpJson.c delete mode 100644 src/plugins/http/src/httpMetricsHandle.c delete mode 100644 src/plugins/http/src/httpParser.c delete mode 100644 src/plugins/http/src/httpQueue.c delete mode 100644 src/plugins/http/src/httpResp.c delete mode 100644 src/plugins/http/src/httpRestHandle.c delete mode 100644 src/plugins/http/src/httpRestJson.c delete mode 100644 src/plugins/http/src/httpServer.c delete mode 100644 src/plugins/http/src/httpSession.c delete mode 100644 src/plugins/http/src/httpSql.c delete mode 100644 src/plugins/http/src/httpSystem.c delete mode 100644 src/plugins/http/src/httpTgHandle.c delete mode 100644 src/plugins/http/src/httpTgJson.c delete mode 100644 src/plugins/http/src/httpUtil.c delete mode 100644 src/plugins/mqtt/CMakeLists.txt delete mode 100644 src/plugins/mqtt/inc/mqttInit.h delete mode 100644 src/plugins/mqtt/inc/mqttLog.h delete mode 100644 src/plugins/mqtt/inc/mqttPayload.h delete mode 100644 src/plugins/mqtt/src/mqttPayload.c delete mode 100644 src/plugins/mqtt/src/mqttSystem.c delete mode 100644 src/sync/CMakeLists.txt delete mode 100644 src/sync/inc/syncInt.h delete mode 100644 src/sync/inc/syncMain.h delete mode 100644 src/sync/inc/syncMsg.h delete mode 100644 src/sync/inc/syncTcp.h delete mode 100644 src/sync/src/syncArbitrator.c delete mode 100644 src/sync/src/syncMain.c delete mode 100644 src/sync/src/syncMsg.c delete mode 100644 src/sync/src/syncRestore.c delete mode 100644 src/sync/src/syncRetrieve.c delete mode 100644 src/sync/src/syncTcp.c delete mode 100644 src/sync/test/CMakeLists.txt delete mode 100644 src/sync/test/syncClient.c delete mode 100644 src/sync/test/syncServer.c diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt deleted file mode 100644 index 320445f7f7..0000000000 --- a/src/plugins/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -ADD_SUBDIRECTORY(monitor) -ADD_SUBDIRECTORY(http) -IF (TD_LINUX AND TD_MQTT) - ADD_SUBDIRECTORY(mqtt) -ENDIF () \ No newline at end of file diff --git a/src/plugins/http/CMakeLists.txt b/src/plugins/http/CMakeLists.txt deleted file mode 100644 index 89fdc141b6..0000000000 --- a/src/plugins/http/CMakeLists.txt +++ /dev/null @@ -1,24 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/zlib-1.2.11/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/cJson/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/lz4/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/query/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/common/inc) -INCLUDE_DIRECTORIES(inc) -AUX_SOURCE_DIRECTORY(src SRC) - -ADD_LIBRARY(http ${SRC}) -TARGET_LINK_LIBRARIES(http z) - -IF (TD_SOMODE_STATIC) - TARGET_LINK_LIBRARIES(http taos_static) -ELSE () - TARGET_LINK_LIBRARIES(http taos) -ENDIF () - -IF (TD_ADMIN) - TARGET_LINK_LIBRARIES(http admin) -ENDIF () diff --git a/src/plugins/http/inc/httpAuth.h b/src/plugins/http/inc/httpAuth.h deleted file mode 100644 index 2ce9725d4b..0000000000 --- a/src/plugins/http/inc/httpAuth.h +++ /dev/null @@ -1,23 +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_HTTP_TOKEN_H -#define TDENGINE_HTTP_TOKEN_H - -int32_t httpParseBasicAuthToken(HttpContext *pContext, char *token, int32_t len); -int32_t httpParseTaosdAuthToken(HttpContext *pContext, char *token, int32_t len); -int32_t httpGenTaosdAuthToken(HttpContext *pContext, char *token, int32_t maxLen); - -#endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpContext.h b/src/plugins/http/inc/httpContext.h deleted file mode 100644 index af52fdd1eb..0000000000 --- a/src/plugins/http/inc/httpContext.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef TDENGINE_HTTP_CONTEXT_H -#define TDENGINE_HTTP_CONTEXT_H - -#include "httpInt.h" - -bool httpInitContexts(); -void httpCleanupContexts(); -const char *httpContextStateStr(HttpContextState state); - -HttpContext *httpCreateContext(SOCKET fd); -bool httpInitContext(HttpContext *pContext); -HttpContext *httpGetContext(void * pContext); -void httpReleaseContext(HttpContext *pContext/*, bool clearRes*/); -void httpCloseContextByServer(HttpContext *pContext); -void httpCloseContextByApp(HttpContext *pContext); -void httpNotifyContextClose(HttpContext *pContext); -bool httpAlterContextState(HttpContext *pContext, HttpContextState srcState, HttpContextState destState); - -#endif diff --git a/src/plugins/http/inc/httpGcHandle.h b/src/plugins/http/inc/httpGcHandle.h deleted file mode 100644 index a3688c6c38..0000000000 --- a/src/plugins/http/inc/httpGcHandle.h +++ /dev/null @@ -1,33 +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_GC_HANDLE_H -#define TDENGINE_GC_HANDLE_H - -#include "http.h" -#include "httpInt.h" -#include "httpUtil.h" -#include "httpResp.h" -#include "httpSql.h" - -#define GC_ROOT_URL_POS 0 -#define GC_ACTION_URL_POS 1 -#define GC_USER_URL_POS 2 -#define GC_PASS_URL_POS 3 - -void gcInitHandle(HttpServer* pServer); -bool gcProcessRequest(struct HttpContext* pContext); - -#endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpGcJson.h b/src/plugins/http/inc/httpGcJson.h deleted file mode 100644 index eb79528c71..0000000000 --- a/src/plugins/http/inc/httpGcJson.h +++ /dev/null @@ -1,31 +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_GC_JSON_H -#define TDENGINE_GC_JSON_H -#include "../../../../include/client/taos.h" -#include "httpHandle.h" -#include "httpJson.h" - -void gcInitQueryJson(HttpContext *pContext); -void gcCleanQueryJson(HttpContext *pContext); - -void gcStartQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result); -void gcStopQueryJson(HttpContext *pContext, HttpSqlCmd *cmd); -bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows); - -void gcSendHeartBeatResp(HttpContext *pContext, HttpSqlCmd *cmd); - -#endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpGzip.h b/src/plugins/http/inc/httpGzip.h deleted file mode 100644 index aeac79c975..0000000000 --- a/src/plugins/http/inc/httpGzip.h +++ /dev/null @@ -1,43 +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 HTTP_GZIP_H -#define HTTP_GZIP_H - -#define EHTTP_GZIP_CHUNK_SIZE_DEFAULT (1024*16) - -typedef struct ehttp_gzip_s ehttp_gzip_t; - -typedef struct ehttp_gzip_callbacks_s ehttp_gzip_callbacks_t; -typedef struct ehttp_gzip_conf_s ehttp_gzip_conf_t; - -struct ehttp_gzip_callbacks_s { - void (*on_data)(ehttp_gzip_t *gzip, void *arg, const char *buf, int32_t len); -}; - -struct ehttp_gzip_conf_s { - int32_t get_header:2; // 0: not fetching header info - int32_t chunk_size; // 0: fallback to default: EHTTP_GZIP_CHUNK_SIZE_DEFAULT -}; - -ehttp_gzip_t* ehttp_gzip_create_decompressor(ehttp_gzip_conf_t conf, ehttp_gzip_callbacks_t callbacks, void *arg); -ehttp_gzip_t* ehttp_gzip_create_compressor(ehttp_gzip_conf_t conf, ehttp_gzip_callbacks_t callbacks, void *arg); -void ehttp_gzip_destroy(ehttp_gzip_t *gzip); - -int32_t ehttp_gzip_write(ehttp_gzip_t *gzip, const char *buf, int32_t len); -int32_t ehttp_gzip_finish(ehttp_gzip_t *gzip); - -#endif // _ehttp_gzip_h_9196791b_ac2a_4d73_9979_f4b41abbc4c0_ - diff --git a/src/plugins/http/inc/httpHandle.h b/src/plugins/http/inc/httpHandle.h deleted file mode 100644 index 3e6356d805..0000000000 --- a/src/plugins/http/inc/httpHandle.h +++ /dev/null @@ -1,23 +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_HTTP_HANDLE_H -#define TDENGINE_HTTP_HANDLE_H - -// http request handler -void httpProcessRequest(HttpContext *pContext); -bool httpProcessData(HttpContext *pContext); - -#endif diff --git a/src/plugins/http/inc/httpInt.h b/src/plugins/http/inc/httpInt.h deleted file mode 100644 index 6c567e23bc..0000000000 --- a/src/plugins/http/inc/httpInt.h +++ /dev/null @@ -1,204 +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_HTTP_INT_H -#define TDENGINE_HTTP_INT_H - -#include "os.h" -#include -#include "pthread.h" -#include "semaphore.h" -#include "tmempool.h" -#include "taosdef.h" -#include "tutil.h" -#include "zlib.h" -#include "http.h" -#include "httpLog.h" -#include "httpJson.h" -#include "httpParser.h" - -#define HTTP_MAX_CMD_SIZE 1024 -#define HTTP_MAX_BUFFER_SIZE 1024*1024*8 -#define HTTP_LABEL_SIZE 8 -#define HTTP_MAX_EVENTS 10 -#define HTTP_BUFFER_INIT 4096 -#define HTTP_BUFFER_SIZE 8388608 -#define HTTP_STEP_SIZE 4096 //http message get process step by step -#define HTTP_METHOD_SCANNER_SIZE 7 //http method fp size -#define HTTP_GC_TARGET_SIZE 512 -#define HTTP_WRITE_RETRY_TIMES 500 -#define HTTP_WRITE_WAIT_TIME_MS 5 -#define HTTP_PASSWORD_LEN TSDB_UNI_LEN -#define HTTP_SESSION_ID_LEN (TSDB_USER_LEN + HTTP_PASSWORD_LEN) - -typedef enum HttpReqType { - HTTP_REQTYPE_OTHERS = 0, - HTTP_REQTYPE_LOGIN = 1, - HTTP_REQTYPE_HEARTBEAT = 2, - HTTP_REQTYPE_SINGLE_SQL = 3, - HTTP_REQTYPE_MULTI_SQL = 4 -} HttpReqType; - -typedef enum { - HTTP_SERVER_INIT, - HTTP_SERVER_RUNNING, - HTTP_SERVER_CLOSING, - HTTP_SERVER_CLOSED -} HttpServerStatus; - -typedef enum { - HTTP_CONTEXT_STATE_READY, - HTTP_CONTEXT_STATE_HANDLING, - HTTP_CONTEXT_STATE_DROPPING, - HTTP_CONTEXT_STATE_CLOSED -} HttpContextState; - -typedef enum { - HTTP_CMD_TYPE_UN_SPECIFIED, - HTTP_CMD_TYPE_CREATE_DB, - HTTP_CMD_TYPE_CREATE_STBALE, - HTTP_CMD_TYPE_INSERT -} HttpSqlCmdType; - -typedef enum { HTTP_CMD_STATE_NOT_RUN_YET, HTTP_CMD_STATE_RUN_FINISHED } HttpSqlCmdState; - -typedef enum { HTTP_CMD_RETURN_TYPE_WITH_RETURN, HTTP_CMD_RETURN_TYPE_NO_RETURN } HttpSqlCmdReturnType; - -struct HttpContext; -struct HttpThread; - -typedef struct { - char id[HTTP_SESSION_ID_LEN]; - int32_t refCount; - void * taos; -} HttpSession; - -typedef struct { - // used by single cmd - char *nativSql; - int32_t numOfRows; - int32_t code; - - // these are the locations in the buffer - int32_t tagNames[TSDB_MAX_TAGS]; - int32_t tagValues[TSDB_MAX_TAGS]; - int32_t timestamp; - int32_t metric; - int32_t stable; - int32_t table; - int32_t values; - int32_t sql; - - // used by multi-cmd - int8_t cmdType; - int8_t cmdReturnType; - int8_t cmdState; - int8_t tagNum; -} HttpSqlCmd; - -typedef struct { - HttpSqlCmd *cmds; - int16_t pos; - int16_t size; - int16_t maxSize; - int32_t bufferPos; - int32_t bufferSize; - char * buffer; -} HttpSqlCmds; - -typedef struct { - char *module; - bool (*fpDecode)(struct HttpContext *pContext); -} HttpDecodeMethod; - -typedef struct { - void (*startJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result); - void (*stopJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd); - bool (*buildQueryJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int numOfRows); - void (*buildAffectRowJsonFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, int affectRows); - void (*initJsonFp)(struct HttpContext *pContext); - void (*cleanJsonFp)(struct HttpContext *pContext); - bool (*checkFinishedFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, int code); - void (*setNextCmdFp)(struct HttpContext *pContext, HttpSqlCmd *cmd, int code); -} HttpEncodeMethod; - -typedef enum { - EHTTP_CONTEXT_PROCESS_FAILED = 0x01, - EHTTP_CONTEXT_PARSER_FAILED = 0x02 -} EHTTP_CONTEXT_FAILED_CAUSE; - -typedef struct HttpContext { - int32_t refCount; - SOCKET fd; - uint32_t accessTimes; - uint32_t lastAccessTime; - int32_t state; - uint8_t reqType; - uint8_t parsed; - uint8_t error; - char ipstr[22]; - char user[TSDB_USER_LEN]; // parsed from auth token or login message - char pass[HTTP_PASSWORD_LEN]; - char db[/*TSDB_ACCT_ID_LEN + */TSDB_DB_NAME_LEN]; - TAOS * taos; - void * ppContext; - HttpSession *session; - z_stream gzipStream; - HttpParser *parser; - HttpSqlCmd singleCmd; - HttpSqlCmds *multiCmds; - JsonBuf * jsonBuf; - HttpEncodeMethod *encodeMethod; - HttpDecodeMethod *decodeMethod; - struct HttpThread *pThread; -} HttpContext; - -typedef struct HttpThread { - pthread_t thread; - HttpContext * pHead; - pthread_mutex_t threadMutex; - bool stop; - EpollFd pollFd; - int32_t numOfContexts; - int32_t threadId; - char label[HTTP_LABEL_SIZE << 1]; - bool (*processData)(HttpContext *pContext); -} HttpThread; - -typedef struct HttpServer { - char label[HTTP_LABEL_SIZE]; - uint32_t serverIp; - uint16_t serverPort; - int8_t stop; - int8_t reserve; - SOCKET fd; - int32_t numOfThreads; - int32_t methodScannerLen; - int32_t requestNum; - int32_t status; - pthread_t thread; - HttpThread * pThreads; - void * contextCache; - void * sessionCache; - pthread_mutex_t serverMutex; - HttpDecodeMethod *methodScanner[HTTP_METHOD_SCANNER_SIZE]; - bool (*processData)(HttpContext *pContext); -} HttpServer; - -extern const char *httpKeepAliveStr[]; -extern const char *httpVersionStr[]; -extern HttpServer tsHttpServer; - -#endif diff --git a/src/plugins/http/inc/httpJson.h b/src/plugins/http/inc/httpJson.h deleted file mode 100644 index 3595ad926f..0000000000 --- a/src/plugins/http/inc/httpJson.h +++ /dev/null @@ -1,106 +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_HTTP_JSON_H -#define TDENGINE_HTTP_JSON_H - -#include -#include - -#define JSON_BUFFER_SIZE 16384 -struct HttpContext; - -enum { JsonNumber, JsonString, JsonBoolean, JsonArray, JsonObject, JsonNull }; - -extern char JsonItmTkn; -extern char JsonObjStt; -extern char JsonObjEnd; -extern char JsonArrStt; -extern char JsonArrEnd; -extern char JsonStrStt; -extern char JsonStrEnd; -extern char JsonPairTkn; -extern char JsonNulTkn[]; -extern char JsonTrueTkn[]; -extern char JsonFalseTkn[]; - -typedef struct { - int32_t size; - int32_t total; - char* lst; - char buf[JSON_BUFFER_SIZE]; - struct HttpContext* pContext; -} JsonBuf; - -// http response -int32_t httpWriteBuf(struct HttpContext* pContext, const char* buf, int32_t sz); -int32_t httpWriteBufNoTrace(struct HttpContext* pContext, const char* buf, int32_t sz); -int32_t httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int32_t sz); - -// builder callback -typedef void (*httpJsonBuilder)(JsonBuf* buf, void* jsnHandle); - -// buffer -void httpInitJsonBuf(JsonBuf* buf, struct HttpContext* pContext); -void httpWriteJsonBufHead(JsonBuf* buf); -int32_t httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast); -void httpWriteJsonBufEnd(JsonBuf* buf); - -// value -void httpJsonString(JsonBuf* buf, char* sVal, int32_t len); -void httpJsonOriginString(JsonBuf* buf, char* sVal, int32_t len); -void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int32_t maxLen); -void httpJsonInt64(JsonBuf* buf, int64_t num); -void httpJsonUInt64(JsonBuf* buf, uint64_t num); -void httpJsonTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision); -void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision); -void httpJsonInt(JsonBuf* buf, int32_t num); -void httpJsonUInt(JsonBuf* buf, uint32_t num); -void httpJsonFloat(JsonBuf* buf, float num); -void httpJsonDouble(JsonBuf* buf, double num); -void httpJsonNull(JsonBuf* buf); -void httpJsonBool(JsonBuf* buf, int32_t val); - -// pair -void httpJsonPair(JsonBuf* buf, char* name, int32_t nameLen, char* sVal, int32_t valLen); -void httpJsonPairOriginString(JsonBuf* buf, char* name, int32_t nameLen, char* sVal, int32_t valLen); -void httpJsonPairHead(JsonBuf* buf, char* name, int32_t len); -void httpJsonPairIntVal(JsonBuf* buf, char* name, int32_t nNameLen, int32_t num); -void httpJsonPairInt64Val(JsonBuf* buf, char* name, int32_t nNameLen, int64_t num); -void httpJsonPairBoolVal(JsonBuf* buf, char* name, int32_t nNameLen, int32_t num); -void httpJsonPairFloatVal(JsonBuf* buf, char* name, int32_t nNameLen, float num); -void httpJsonPairDoubleVal(JsonBuf* buf, char* name, int32_t nNameLen, double num); -void httpJsonPairNullVal(JsonBuf* buf, char* name, int32_t nNameLen); - -// object -void httpJsonPairArray(JsonBuf* buf, char* name, int32_t nLen, httpJsonBuilder builder, void* dsHandle); -void httpJsonPairObject(JsonBuf* buf, char* name, int32_t nLen, httpJsonBuilder builder, void* dsHandle); -void httpJsonObject(JsonBuf* buf, httpJsonBuilder fnBuilder, void* dsHandle); -void httpJsonArray(JsonBuf* buf, httpJsonBuilder fnBuidler, void* jsonHandle); - -// print -void httpJsonTestBuf(JsonBuf* buf, int32_t safety); -void httpJsonToken(JsonBuf* buf, char c); -void httpJsonItemToken(JsonBuf* buf); -void httpJsonPrint(JsonBuf* buf, const char* json, int32_t len); - -// quick -void httpJsonPairStatus(JsonBuf* buf, int32_t code); - -// http json printer -JsonBuf* httpMallocJsonBuf(struct HttpContext* pContext); -void httpFreeJsonBuf(struct HttpContext* pContext); - -#endif diff --git a/src/plugins/http/inc/httpLog.h b/src/plugins/http/inc/httpLog.h deleted file mode 100644 index 9c145a43e8..0000000000 --- a/src/plugins/http/inc/httpLog.h +++ /dev/null @@ -1,31 +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_HTTP_LOG_H -#define TDENGINE_HTTP_LOG_H - -#include "tlog.h" - -extern int32_t httpDebugFlag; - -#define httpFatal(...) { if (httpDebugFlag & DEBUG_FATAL) { taosPrintLog("HTP FATAL ", 255, __VA_ARGS__); }} -#define httpError(...) { if (httpDebugFlag & DEBUG_ERROR) { taosPrintLog("HTP ERROR ", 255, __VA_ARGS__); }} -#define httpWarn(...) { if (httpDebugFlag & DEBUG_WARN) { taosPrintLog("HTP WARN ", 255, __VA_ARGS__); }} -#define httpInfo(...) { if (httpDebugFlag & DEBUG_INFO) { taosPrintLog("HTP ", 255, __VA_ARGS__); }} -#define httpDebug(...) { if (httpDebugFlag & DEBUG_DEBUG) { taosPrintLog("HTP ", httpDebugFlag, __VA_ARGS__); }} -#define httpTrace(...) { if (httpDebugFlag & DEBUG_TRACE) { taosPrintLog("HTP ", httpDebugFlag, __VA_ARGS__); }} -#define httpTraceL(...){ if (httpDebugFlag & DEBUG_TRACE) { taosPrintLongString("HTP ", httpDebugFlag, __VA_ARGS__); }} - -#endif diff --git a/src/plugins/http/inc/httpMetricsHandle.h b/src/plugins/http/inc/httpMetricsHandle.h deleted file mode 100644 index e05a8ce687..0000000000 --- a/src/plugins/http/inc/httpMetricsHandle.h +++ /dev/null @@ -1,27 +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_HTTPMETRICSHANDLE_H -#define TDENGINE_HTTPMETRICSHANDLE_H - -#include "http.h" -#include "httpInt.h" -#include "httpUtil.h" -#include "httpResp.h" - -void metricsInitHandle(HttpServer* httpServer); - -bool metricsProcessRequest(struct HttpContext* httpContext); - -#endif // TDENGINE_HTTPMETRICHANDLE_H diff --git a/src/plugins/http/inc/httpParser.h b/src/plugins/http/inc/httpParser.h deleted file mode 100644 index f7b55958c8..0000000000 --- a/src/plugins/http/inc/httpParser.h +++ /dev/null @@ -1,184 +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 HTTP_PARSER_H -#define HTTP_PARSER_H -#include "httpGzip.h" - -#define HTTP_MAX_URL 6 // http url stack size - -#define HTTP_CODE_CONTINUE 100 -#define HTTP_CODE_SWITCHING_PROTOCOL 101 -#define HTTP_CODE_PROCESSING 102 -#define HTTP_CODE_EARLY_HINTS 103 -#define HTTP_CODE_OK 200 -#define HTTP_CODE_CREATED 201 -#define HTTP_CODE_ACCEPTED 202 -#define HTTP_CODE_NON_AUTHORITATIVE_INFO 203 -#define HTTP_CODE_NO_CONTENT 204 -#define HTTP_CODE_RESET_CONTENT 205 -#define HTTP_CODE_PARTIAL_CONTENT 206 -#define HTTP_CODE_MULTI_STATUS 207 -#define HTTP_CODE_ALREADY_REPORTED 208 -#define HTTP_CODE_IM_USED 226 -#define HTTP_CODE_MULTIPLE_CHOICE 300 -#define HTTP_CODE_MOVED_PERMANENTLY 301 -#define HTTP_CODE_FOUND 302 -#define HTTP_CODE_SEE_OTHER 303 -#define HTTP_CODE_NOT_MODIFIED 304 -#define HTTP_CODE_USE_PROXY 305 -#define HTTP_CODE_UNUSED 306 -#define HTTP_CODE_TEMPORARY_REDIRECT 307 -#define HTTP_CODE_PERMANENT_REDIRECT 308 -#define HTTP_CODE_BAD_REQUEST 400 -#define HTTP_CODE_UNAUTHORIZED 401 -#define HTTP_CODE_PAYMENT_REQUIRED 402 -#define HTTP_CODE_FORBIDDEN 403 -#define HTTP_CODE_NOT_FOUND 404 -#define HTTP_CODE_METHOD_NOT_ALLOWED 405 -#define HTTP_CODE_NOT_ACCEPTABLE 406 -#define HTTP_CODE_PROXY_AUTH_REQUIRED 407 -#define HTTP_CODE_REQUEST_TIMEOUT 408 -#define HTTP_CODE_CONFLICT 409 -#define HTTP_CODE_GONE 410 -#define HTTP_CODE_LENGTH_REQUIRED 411 -#define HTTP_CODE_PRECONDITION_FAILED 412 -#define HTTP_CODE_PAYLOAD_TOO_LARGE 413 -#define HTTP_CODE_URI_TOO_LARGE 414 -#define HTTP_CODE_UNSUPPORTED_MEDIA_TYPE 415 -#define HTTP_CODE_RANGE_NOT_SATISFIABLE 416 -#define HTTP_CODE_EXPECTATION_FAILED 417 -#define HTTP_CODE_IM_A_TEAPOT 418 -#define HTTP_CODE_MISDIRECTED_REQUEST 421 -#define HTTP_CODE_UNPROCESSABLE_ENTITY 422 -#define HTTP_CODE_LOCKED 423 -#define HTTP_CODE_FAILED_DEPENDENCY 424 -#define HTTP_CODE_TOO_EARLY 425 -#define HTTP_CODE_UPGRADE_REQUIRED 426 -#define HTTP_CODE_PRECONDITION_REQUIRED 428 -#define HTTP_CODE_TOO_MANY_REQUESTS 429 -#define HTTP_CODE_REQ_HDR_FIELDS_TOO_LARGE 431 -#define HTTP_CODE_UNAVAIL_4_LEGAL_REASONS 451 -#define HTTP_CODE_INTERNAL_SERVER_ERROR 500 -#define HTTP_CODE_NOT_IMPLEMENTED 501 -#define HTTP_CODE_BAD_GATEWAY 502 -#define HTTP_CODE_SERVICE_UNAVAILABLE 503 -#define HTTP_CODE_GATEWAY_TIMEOUT 504 -#define HTTP_CODE_HTTP_VER_NOT_SUPPORTED 505 -#define HTTP_CODE_VARIANT_ALSO_NEGOTIATES 506 -#define HTTP_CODE_INSUFFICIENT_STORAGE 507 -#define HTTP_CODE_LOOP_DETECTED 508 -#define HTTP_CODE_NOT_EXTENDED 510 -#define HTTP_CODE_NETWORK_AUTH_REQUIRED 511 - -typedef enum HTTP_PARSER_STATE { - HTTP_PARSER_BEGIN, - HTTP_PARSER_REQUEST_OR_RESPONSE, - HTTP_PARSER_METHOD, - HTTP_PARSER_TARGET, - HTTP_PARSER_HTTP_VERSION, - HTTP_PARSER_SP, - HTTP_PARSER_STATUS_CODE, - HTTP_PARSER_REASON_PHRASE, - HTTP_PARSER_CRLF, - HTTP_PARSER_HEADER, - HTTP_PARSER_HEADER_KEY, - HTTP_PARSER_HEADER_VAL, - HTTP_PARSER_CHUNK_SIZE, - HTTP_PARSER_CHUNK, - HTTP_PARSER_END, - HTTP_PARSER_ERROR, - HTTP_PARSER_OPTIONAL_SP -} HTTP_PARSER_STATE; - -typedef enum HTTP_AUTH_TYPE { - HTTP_INVALID_AUTH, - HTTP_BASIC_AUTH, - HTTP_TAOSD_AUTH -} HTTP_AUTH_TYPE; - -typedef enum HTTP_VERSION { - HTTP_VERSION_10 = 0, - HTTP_VERSION_11 = 1, - HTTP_VERSION_12 = 2, - HTTP_INVALID_VERSION -} HTTP_VERSION; - -typedef enum HTTP_KEEPALIVE { - HTTP_KEEPALIVE_NO_INPUT = 0, - HTTP_KEEPALIVE_ENABLE = 1, - HTTP_KEEPALIVE_DISABLE = 2 -} HTTP_KEEPALIVE; - -typedef struct HttpString { - char * str; - int32_t pos; - int32_t size; -} HttpString; - -typedef struct HttpStatus { - int32_t code; - char * desc; -} HttpStatus; - -typedef struct HttpStack{ - int8_t *stacks; - int32_t pos; - int32_t size; -} HttpStack; - -struct HttpContext; -typedef struct HttpParser { - struct HttpContext *pContext; - ehttp_gzip_t *gzip; - HttpStack stacks; - HttpString str; - HttpString body; - HttpString path[HTTP_MAX_URL]; - char * method; - char * target; - char * version; - char * reasonPhrase; - char * key; - char * val; - char * authContent; - int8_t httpVersion; - int8_t acceptEncodingGzip; - int8_t acceptEncodingChunked; - int8_t contentLengthSpecified; - int8_t contentChunked; - int8_t transferGzip; - int8_t transferChunked; - int8_t keepAlive; - int8_t authType; - int32_t contentLength; - int32_t chunkSize; - int32_t receivedChunkSize; - int32_t receivedSize; - int32_t statusCode; - int8_t inited; - int8_t parsed; - int16_t httpCode; - int32_t parseCode; -} HttpParser; - -void httpInitParser(HttpParser *parser); -HttpParser *httpCreateParser(struct HttpContext *pContext); -void httpClearParser(HttpParser *parser); -void httpDestroyParser(HttpParser *parser); -int32_t httpParseBuf(HttpParser *parser, const char *buf, int32_t len); -char * httpGetStatusDesc(int32_t statusCode); - -#endif diff --git a/src/plugins/http/inc/httpQueue.h b/src/plugins/http/inc/httpQueue.h deleted file mode 100644 index 1ffbd51481..0000000000 --- a/src/plugins/http/inc/httpQueue.h +++ /dev/null @@ -1,35 +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_HTTP_QUEUE_H -#define TDENGINE_HTTP_QUEUE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -typedef void (*FHttpResultFp)(void *param, void *result, int32_t code, int32_t rows); - -bool httpInitResultQueue(); -void httpCleanupResultQueue(); -void httpDispatchToResultQueue(void *param, TAOS_RES *result, int32_t code, int32_t rows, FHttpResultFp fp); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/plugins/http/inc/httpResp.h b/src/plugins/http/inc/httpResp.h deleted file mode 100644 index a528bcc39e..0000000000 --- a/src/plugins/http/inc/httpResp.h +++ /dev/null @@ -1,40 +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_HTTP_RESP_H -#define TDENGINE_HTTP_RESP_H - -#include "httpInt.h" - -enum _httpRespTempl { - HTTP_RESPONSE_JSON_OK, - HTTP_RESPONSE_JSON_ERROR, - HTTP_RESPONSE_OK, - HTTP_RESPONSE_ERROR, - HTTP_RESPONSE_CHUNKED_UN_COMPRESS, - HTTP_RESPONSE_CHUNKED_COMPRESS, - HTTP_RESPONSE_OPTIONS, - HTTP_RESPONSE_GRAFANA, - HTTP_RESP_END -}; - -extern const char *httpRespTemplate[]; - -void httpSendErrorResp(HttpContext *pContext, int32_t errNo); -void httpSendTaosdInvalidSqlErrorResp(HttpContext *pContext, char* errMsg); -void httpSendSuccResp(HttpContext *pContext, char *desc); -void httpSendOptionResp(HttpContext *pContext, char *desc); - -#endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpRestHandle.h b/src/plugins/http/inc/httpRestHandle.h deleted file mode 100644 index df405685e9..0000000000 --- a/src/plugins/http/inc/httpRestHandle.h +++ /dev/null @@ -1,33 +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_REST_HANDLE_H -#define TDENGINE_REST_HANDLE_H - -#include "http.h" -#include "httpInt.h" -#include "httpUtil.h" -#include "httpResp.h" -#include "httpSql.h" - -#define REST_ROOT_URL_POS 0 -#define REST_ACTION_URL_POS 1 -#define REST_USER_USEDB_URL_POS 2 -#define REST_PASS_URL_POS 3 - -void restInitHandle(HttpServer* pServer); -bool restProcessRequest(struct HttpContext* pContext); - -#endif diff --git a/src/plugins/http/inc/httpRestJson.h b/src/plugins/http/inc/httpRestJson.h deleted file mode 100644 index d1f5a45521..0000000000 --- a/src/plugins/http/inc/httpRestJson.h +++ /dev/null @@ -1,56 +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_REST_JSON_H -#define TDENGINE_REST_JSON_H -#include -#include "../../../../include/client/taos.h" -#include "httpHandle.h" -#include "httpJson.h" - -#define REST_JSON_SUCCESS "succ" -#define REST_JSON_SUCCESS_LEN 4 -#define REST_JSON_FAILURE "error" -#define REST_JSON_FAILURE_LEN 5 -#define REST_JSON_STATUS "status" -#define REST_JSON_STATUS_LEN 6 -#define REST_JSON_CODE "code" -#define REST_JSON_CODE_LEN 4 -#define REST_JSON_DESC "desc" -#define REST_JSON_DESC_LEN 4 -#define REST_JSON_DATA "data" -#define REST_JSON_DATA_LEN 4 -#define REST_JSON_HEAD "head" -#define REST_JSON_HEAD_LEN 4 -#define REST_JSON_HEAD_INFO "column_meta" -#define REST_JSON_HEAD_INFO_LEN 11 -#define REST_JSON_ROWS "rows" -#define REST_JSON_ROWS_LEN 4 -#define REST_JSON_AFFECT_ROWS "affected_rows" -#define REST_JSON_AFFECT_ROWS_LEN 13 - -#define REST_TIMESTAMP_FMT_LOCAL_STRING 0 -#define REST_TIMESTAMP_FMT_TIMESTAMP 1 -#define REST_TIMESTAMP_FMT_UTC_STRING 2 - -void restBuildSqlAffectRowsJson(HttpContext *pContext, HttpSqlCmd *cmd, int32_t affect_rows); - -void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result); -bool restBuildSqlTimestampJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows); -bool restBuildSqlLocalTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows); -bool restBuildSqlUtcTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows); -void restStopSqlJson(HttpContext *pContext, HttpSqlCmd *cmd); - -#endif diff --git a/src/plugins/http/inc/httpServer.h b/src/plugins/http/inc/httpServer.h deleted file mode 100644 index 58ed3545f3..0000000000 --- a/src/plugins/http/inc/httpServer.h +++ /dev/null @@ -1,27 +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_HTTP_SERVER_H -#define TDENGINE_HTTP_SERVER_H - -#include "httpInt.h" - -bool httpInitConnect(); -void httpCleanUpConnect(); - -void *httpInitServer(char *ip, uint16_t port, char *label, int32_t numOfThreads, void *fp, void *shandle); -void httpCleanUpServer(HttpServer *pServer); - -#endif diff --git a/src/plugins/http/inc/httpSession.h b/src/plugins/http/inc/httpSession.h deleted file mode 100644 index 393e720f69..0000000000 --- a/src/plugins/http/inc/httpSession.h +++ /dev/null @@ -1,27 +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_HTTP_SESSION_H -#define TDENGINE_HTTP_SESSION_H - -bool httpInitSessions(); -void httpCleanUpSessions(); - -// http session method -void httpCreateSession(HttpContext *pContext, void *taos); -void httpGetSession(HttpContext *pContext); -void httpReleaseSession(HttpContext *pContext); - -#endif diff --git a/src/plugins/http/inc/httpSql.h b/src/plugins/http/inc/httpSql.h deleted file mode 100644 index 325545af47..0000000000 --- a/src/plugins/http/inc/httpSql.h +++ /dev/null @@ -1,41 +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_HTTP_SQL_H -#define TDENGINE_HTTP_SQL_H - - -int32_t httpAddToSqlCmdBuffer(HttpContext *pContext, const char *const format, ...); -int32_t httpAddToSqlCmdBufferNoTerminal(HttpContext *pContext, const char *const format, ...); -int32_t httpAddToSqlCmdBufferWithSize(HttpContext *pContext, int32_t mallocSize); -int32_t httpAddToSqlCmdBufferTerminal(HttpContext *pContext); - -bool httpMallocMultiCmds(HttpContext *pContext, int32_t cmdSize, int32_t bufferSize); -bool httpReMallocMultiCmdsSize(HttpContext *pContext, int32_t cmdSize); -bool httpReMallocMultiCmdsBuffer(HttpContext *pContext, int32_t bufferSize); -void httpFreeMultiCmds(HttpContext *pContext); - -HttpSqlCmd *httpNewSqlCmd(HttpContext *pContext); -HttpSqlCmd *httpCurrSqlCmd(HttpContext *pContext); -int32_t httpCurSqlCmdPos(HttpContext *pContext); - -void httpTrimTableName(char *name); -int32_t httpShrinkTableName(HttpContext *pContext, int32_t pos, char *name); -char * httpGetCmdsString(HttpContext *pContext, int32_t pos); - -int32_t httpCheckAllocEscapeSql(char *oldSql, char **newSql); -void httpCheckFreeEscapedSql(char *oldSql, char *newSql); - -#endif diff --git a/src/plugins/http/inc/httpSystem.h b/src/plugins/http/inc/httpSystem.h deleted file mode 100644 index afe49edb2f..0000000000 --- a/src/plugins/http/inc/httpSystem.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef TDENGINE_HTTP_SYSTEM_H -#define TDENGINE_HTTP_SYSTEM_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -int32_t httpInitSystem(); -int32_t httpStartSystem(); -void httpStopSystem(); -void httpCleanUpSystem(); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/plugins/http/inc/httpTgHandle.h b/src/plugins/http/inc/httpTgHandle.h deleted file mode 100644 index 6a3a7bfa4a..0000000000 --- a/src/plugins/http/inc/httpTgHandle.h +++ /dev/null @@ -1,35 +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_TG_HANDLE_H -#define TDENGINE_TG_HANDLE_H - -#include "http.h" -#include "httpInt.h" -#include "httpUtil.h" -#include "httpResp.h" -#include "httpSql.h" - -#define TG_ROOT_URL_POS 0 -#define TG_DB_URL_POS 1 -#define TG_USER_URL_POS 2 -#define TG_PASS_URL_POS 3 - -void tgInitHandle(HttpServer *pServer); -void tgCleanupHandle(); - -bool tgProcessRquest(struct HttpContext *pContext); - -#endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpTgJson.h b/src/plugins/http/inc/httpTgJson.h deleted file mode 100644 index a7ec4f8cb6..0000000000 --- a/src/plugins/http/inc/httpTgJson.h +++ /dev/null @@ -1,31 +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_TG_JSON_H -#define TDENGINE_TG_JSON_H - -#include "../../../../include/client/taos.h" -#include "httpHandle.h" -#include "httpJson.h" - -void tgInitQueryJson(HttpContext *pContext); -void tgCleanQueryJson(HttpContext *pContext); -void tgStartQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result); -void tgStopQueryJson(HttpContext *pContext, HttpSqlCmd *cmd); -void tgBuildSqlAffectRowsJson(HttpContext *pContext, HttpSqlCmd *cmd, int32_t affect_rows); -bool tgCheckFinished(struct HttpContext *pContext, HttpSqlCmd *cmd, int32_t code); -void tgSetNextCmd(struct HttpContext *pContext, HttpSqlCmd *cmd, int32_t code); - -#endif \ No newline at end of file diff --git a/src/plugins/http/inc/httpUtil.h b/src/plugins/http/inc/httpUtil.h deleted file mode 100644 index 21690ebca9..0000000000 --- a/src/plugins/http/inc/httpUtil.h +++ /dev/null @@ -1,36 +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_HTTP_UTIL_H -#define TDENGINE_HTTP_UTIL_H - -bool httpCheckUsedbSql(char *sql); -bool httpCheckAlterSql(char *sql); -void httpTimeToString(int32_t t, char *buf, int32_t buflen); - -bool httpUrlMatch(HttpContext *pContext, int32_t pos, char *cmp); -bool httpParseRequest(HttpContext *pContext); -int32_t httpCheckReadCompleted(HttpContext *pContext); -void httpReadDirtyData(HttpContext *pContext); - -int32_t httpGzipDeCompress(char *srcData, int32_t nSrcData, char *destData, int32_t *nDestData); -int32_t httpGzipCompressInit(HttpContext *pContext); -int32_t httpGzipCompress(HttpContext *pContext, char *inSrcData, int32_t inSrcDataLen, - char *outDestData, int32_t *outDestDataLen, bool isTheLast); - -// http request parser -void httpAddMethod(HttpServer *pServer, HttpDecodeMethod *pMethod); - -#endif diff --git a/src/plugins/http/src/httpAuth.c b/src/plugins/http/src/httpAuth.c deleted file mode 100644 index 2ef5406823..0000000000 --- a/src/plugins/http/src/httpAuth.c +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tkey.h" -#include "tutil.h" -#include "http.h" -#include "httpInt.h" -#include "httpAuth.h" - -#define KEY_DES_4 4971256377704625728L - -int32_t httpParseBasicAuthToken(HttpContext *pContext, char *token, int32_t len) { - token[len] = '\0'; - int32_t outlen = 0; - char * base64 = (char *)base64_decode(token, len, &outlen); - if (base64 == NULL || outlen == 0) { - httpError("context:%p, fd:%d, basic token:%s parsed error", pContext, pContext->fd, token); - free(base64); - return -1; - } - - char *user = strstr(base64, ":"); - if (user == NULL) { - httpError("context:%p, fd:%d, basic token:%s invalid format", pContext, pContext->fd, token); - free(base64); - return -1; - } - - int32_t user_len = (int32_t)(user - base64); - if (user_len < 1 || user_len >= TSDB_USER_LEN) { - httpError("context:%p, fd:%d, basic token:%s parse user error", pContext, pContext->fd, token); - free(base64); - return -1; - } - strncpy(pContext->user, base64, (size_t)user_len); - pContext->user[user_len] = 0; - - char * password = user + 1; - int32_t pass_len = (int32_t)((base64 + outlen) - password); - if (pass_len < 1 || pass_len >= HTTP_PASSWORD_LEN) { - httpError("context:%p, fd:%d, basic token:%s parse password error", pContext, pContext->fd, token); - free(base64); - return -1; - } - strncpy(pContext->pass, password, (size_t)pass_len); - pContext->pass[pass_len] = 0; - - free(base64); - httpDebug("context:%p, fd:%d, basic token parsed success, user:%s", pContext, pContext->fd, pContext->user); - return 0; -} - -int32_t httpParseTaosdAuthToken(HttpContext *pContext, char *token, int32_t len) { - token[len] = '\0'; - int32_t outlen = 0; - unsigned char *base64 = base64_decode(token, len, &outlen); - if (base64 == NULL || outlen == 0) { - httpError("context:%p, fd:%d, taosd token:%s parsed error", pContext, pContext->fd, token); - if (base64) free(base64); - return 01; - } - if (outlen != (TSDB_USER_LEN + HTTP_PASSWORD_LEN)) { - httpError("context:%p, fd:%d, taosd token:%s length error", pContext, pContext->fd, token); - free(base64); - return -1; - } - - char *descrypt = taosDesDecode(KEY_DES_4, (char *)base64, outlen); - if (descrypt == NULL) { - httpError("context:%p, fd:%d, taosd token:%s descrypt error", pContext, pContext->fd, token); - free(base64); - return -1; - } else { - tstrncpy(pContext->user, descrypt, sizeof(pContext->user)); - tstrncpy(pContext->pass, descrypt + TSDB_USER_LEN, sizeof(pContext->pass)); - - httpDebug("context:%p, fd:%d, taosd token:%s parsed success, user:%s", pContext, pContext->fd, token, - pContext->user); - free(base64); - free(descrypt); - return 0; - } -} - -int32_t httpGenTaosdAuthToken(HttpContext *pContext, char *token, int32_t maxLen) { - char buffer[sizeof(pContext->user) + sizeof(pContext->pass)] = {0}; - size_t size = sizeof(pContext->user); - tstrncpy(buffer, pContext->user, size); - size = sizeof(pContext->pass); - tstrncpy(buffer + sizeof(pContext->user), pContext->pass, size); - - char *encrypt = taosDesEncode(KEY_DES_4, buffer, TSDB_USER_LEN + HTTP_PASSWORD_LEN); - char *base64 = base64_encode((const unsigned char *)encrypt, TSDB_USER_LEN + HTTP_PASSWORD_LEN); - - size_t len = strlen(base64); - tstrncpy(token, base64, len + 1); - free(encrypt); - free(base64); - - httpDebug("context:%p, fd:%d, generate taosd token:%s", pContext, pContext->fd, token); - - return 0; -} diff --git a/src/plugins/http/src/httpContext.c b/src/plugins/http/src/httpContext.c deleted file mode 100644 index 11945453c5..0000000000 --- a/src/plugins/http/src/httpContext.c +++ /dev/null @@ -1,240 +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 "taosmsg.h" -#include "tsocket.h" -#include "tutil.h" -#include "ttimer.h" -#include "tglobal.h" -#include "tcache.h" -#include "hash.h" -#include "httpInt.h" -#include "httpResp.h" -#include "httpSql.h" -#include "httpSession.h" -#include "httpContext.h" -#include "httpParser.h" - -static void httpDestroyContext(void *data); - -static void httpRemoveContextFromEpoll(HttpContext *pContext) { - HttpThread *pThread = pContext->pThread; - if (pContext->fd >= 0) { - epoll_ctl(pThread->pollFd, EPOLL_CTL_DEL, pContext->fd, NULL); -#ifdef WINDOWS - SOCKET fd = atomic_val_compare_exchange_32(&pContext->fd, pContext->fd, -1); -#else - SOCKET fd = atomic_val_compare_exchange_64(&pContext->fd, pContext->fd, -1); -#endif - taosCloseSocket(fd); - } -} - -static void httpDestroyContext(void *data) { - HttpContext *pContext = *(HttpContext **)data; - if (pContext->fd > 0) taosCloseSocket(pContext->fd); - - HttpThread *pThread = pContext->pThread; - httpRemoveContextFromEpoll(pContext); - httpReleaseSession(pContext); - atomic_sub_fetch_32(&pThread->numOfContexts, 1); - - httpDebug("context:%p, is destroyed, refCount:%d data:%p thread:%s numOfContexts:%d", pContext, pContext->refCount, - data, pContext->pThread->label, pContext->pThread->numOfContexts); - pContext->pThread = 0; - pContext->state = HTTP_CONTEXT_STATE_CLOSED; - - // avoid double free - httpFreeJsonBuf(pContext); - httpFreeMultiCmds(pContext); - - if (pContext->parser) { - httpDestroyParser(pContext->parser); - pContext->parser = NULL; - } - - tfree(pContext); -} - -bool httpInitContexts() { - tsHttpServer.contextCache = taosCacheInit(TSDB_CACHE_PTR_KEY, 2, true, httpDestroyContext, "restc"); - if (tsHttpServer.contextCache == NULL) { - httpError("failed to init context cache"); - return false; - } - - return true; -} - -void httpCleanupContexts() { - if (tsHttpServer.contextCache != NULL) { - SCacheObj *cache = tsHttpServer.contextCache; - httpInfo("context cache is cleanuping, size:%d", taosHashGetSize(cache->pHashTable)); - taosCacheCleanup(tsHttpServer.contextCache); - tsHttpServer.contextCache = NULL; - } -} - -const char *httpContextStateStr(HttpContextState state) { - switch (state) { - case HTTP_CONTEXT_STATE_READY: - return "ready"; - case HTTP_CONTEXT_STATE_HANDLING: - return "handling"; - case HTTP_CONTEXT_STATE_DROPPING: - return "dropping"; - case HTTP_CONTEXT_STATE_CLOSED: - return "closed"; - default: - return "unknown"; - } -} - -void httpNotifyContextClose(HttpContext *pContext) { shutdown(pContext->fd, SHUT_WR); } - -bool httpAlterContextState(HttpContext *pContext, HttpContextState srcState, HttpContextState destState) { - return (atomic_val_compare_exchange_32(&pContext->state, srcState, destState) == srcState); -} - -HttpContext *httpCreateContext(SOCKET fd) { - HttpContext *pContext = calloc(1, sizeof(HttpContext)); - if (pContext == NULL) return NULL; - - pContext->fd = fd; - pContext->lastAccessTime = taosGetTimestampSec(); - pContext->state = HTTP_CONTEXT_STATE_READY; - pContext->parser = httpCreateParser(pContext); - - TSDB_CACHE_PTR_TYPE handleVal = (TSDB_CACHE_PTR_TYPE)pContext; - HttpContext **ppContext = taosCachePut(tsHttpServer.contextCache, &handleVal, sizeof(TSDB_CACHE_PTR_TYPE), &pContext, - sizeof(TSDB_CACHE_PTR_TYPE), 3000); - pContext->ppContext = ppContext; - httpDebug("context:%p, fd:%d, is created, data:%p", pContext, fd, ppContext); - - // set the ref to 0 - taosCacheRelease(tsHttpServer.contextCache, (void **)&ppContext, false); - - return pContext; -} - -HttpContext *httpGetContext(void *ptr) { - TSDB_CACHE_PTR_TYPE handleVal = (TSDB_CACHE_PTR_TYPE)ptr; - HttpContext **ppContext = taosCacheAcquireByKey(tsHttpServer.contextCache, &handleVal, sizeof(TSDB_CACHE_PTR_TYPE)); - - if (ppContext) { - HttpContext *pContext = *ppContext; - if (pContext) { - int32_t refCount = atomic_add_fetch_32(&pContext->refCount, 1); - httpTrace("context:%p, fd:%d, is accquired, data:%p refCount:%d", pContext, pContext->fd, ppContext, refCount); - return pContext; - } - } - return NULL; -} - -void httpReleaseContext(HttpContext *pContext/*, bool clearRes*/) { - int32_t refCount = atomic_sub_fetch_32(&pContext->refCount, 1); - if (refCount < 0) { - httpError("context:%p, is already released, refCount:%d", pContext, refCount); - return; - } - /* - if (clearRes) { - if (pContext->parser) { - httpClearParser(pContext->parser); - } - memset(&pContext->singleCmd, 0, sizeof(HttpSqlCmd)); - } - */ - HttpContext **ppContext = pContext->ppContext; - httpTrace("context:%p, is released, data:%p refCount:%d", pContext, ppContext, refCount); - - if (tsHttpServer.contextCache != NULL) { - taosCacheRelease(tsHttpServer.contextCache, (void **)(&ppContext), false); - } else { - httpDebug("context:%p, won't be destroyed for cache is already released", pContext); - // httpDestroyContext((void **)(&ppContext)); - } -} - -bool httpInitContext(HttpContext *pContext) { - pContext->accessTimes++; - pContext->lastAccessTime = taosGetTimestampSec(); - - pContext->reqType = HTTP_REQTYPE_OTHERS; - pContext->encodeMethod = NULL; - memset(&pContext->singleCmd, 0, sizeof(HttpSqlCmd)); - - httpTrace("context:%p, fd:%d, parsed:%d", pContext, pContext->fd, pContext->parsed); - return true; -} - -void httpCloseContextByApp(HttpContext *pContext) { - HttpParser *parser = pContext->parser; - pContext->parsed = false; - bool keepAlive = true; - - if (pContext->error == true) { - keepAlive = false; - } else if (parser && parser->httpVersion == HTTP_VERSION_10 && parser->keepAlive != HTTP_KEEPALIVE_ENABLE) { - keepAlive = false; - } else if (parser && parser->httpVersion != HTTP_VERSION_10 && parser->keepAlive == HTTP_KEEPALIVE_DISABLE) { - keepAlive = false; - } - - if (keepAlive) { - if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_HANDLING, HTTP_CONTEXT_STATE_READY)) { - httpTrace("context:%p, fd:%d, last state:handling, keepAlive:true, reuse context", pContext, pContext->fd); - } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_DROPPING, HTTP_CONTEXT_STATE_CLOSED)) { - httpRemoveContextFromEpoll(pContext); - httpTrace("context:%p, fd:%d, ast state:dropping, keepAlive:true, close connect", pContext, pContext->fd); - } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_READY)) { - httpTrace("context:%p, fd:%d, last state:ready, keepAlive:true, reuse context", pContext, pContext->fd); - } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_CLOSED, HTTP_CONTEXT_STATE_CLOSED)) { - httpRemoveContextFromEpoll(pContext); - httpTrace("context:%p, fd:%d, last state:ready, keepAlive:true, close connect", pContext, pContext->fd); - } else { - httpRemoveContextFromEpoll(pContext); - httpError("context:%p, fd:%d, last state:%s:%d, keepAlive:true, close connect", pContext, pContext->fd, - httpContextStateStr(pContext->state), pContext->state); - } - } else { - httpRemoveContextFromEpoll(pContext); - httpTrace("context:%p, fd:%d, ilast state:%s:%d, keepAlive:false, close context", pContext, pContext->fd, - httpContextStateStr(pContext->state), pContext->state); - } - - httpReleaseContext(pContext/*, true*/); -} - -void httpCloseContextByServer(HttpContext *pContext) { - if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_HANDLING, HTTP_CONTEXT_STATE_DROPPING)) { - httpTrace("context:%p, fd:%d, epoll finished, still used by app", pContext, pContext->fd); - } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_DROPPING, HTTP_CONTEXT_STATE_DROPPING)) { - httpTrace("context:%p, fd:%d, epoll already finished, wait app finished", pContext, pContext->fd); - } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_CLOSED)) { - httpTrace("context:%p, fd:%d, epoll finished, close connect", pContext, pContext->fd); - } else if (httpAlterContextState(pContext, HTTP_CONTEXT_STATE_CLOSED, HTTP_CONTEXT_STATE_CLOSED)) { - httpTrace("context:%p, fd:%d, epoll finished, will be closed soon", pContext, pContext->fd); - } else { - httpError("context:%p, fd:%d, unknown state:%d", pContext, pContext->fd, pContext->state); - } - - pContext->parsed = false; - httpRemoveContextFromEpoll(pContext); - httpReleaseContext(pContext/*, true*/); -} diff --git a/src/plugins/http/src/httpGcHandle.c b/src/plugins/http/src/httpGcHandle.c deleted file mode 100644 index 883afcc4ec..0000000000 --- a/src/plugins/http/src/httpGcHandle.c +++ /dev/null @@ -1,304 +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 "taosdef.h" -#include "taoserror.h" -#include "cJSON.h" -#include "httpLog.h" -#include "httpGcHandle.h" -#include "httpGcJson.h" - -static HttpDecodeMethod gcDecodeMethod = {"grafana", gcProcessRequest}; -static HttpEncodeMethod gcHeartBeatMethod = { - .startJsonFp = NULL, - .stopJsonFp = gcSendHeartBeatResp, - .buildQueryJsonFp = NULL, - .buildAffectRowJsonFp = NULL, - .initJsonFp = NULL, - .cleanJsonFp = NULL, - .checkFinishedFp = NULL, - .setNextCmdFp = NULL -}; - -static HttpEncodeMethod gcQueryMethod = { - .startJsonFp = NULL, - .stopJsonFp = gcStopQueryJson, - .buildQueryJsonFp = gcBuildQueryJson, - .buildAffectRowJsonFp = NULL, - .initJsonFp = gcInitQueryJson, - .cleanJsonFp = gcCleanQueryJson, - .checkFinishedFp = NULL, - .setNextCmdFp = NULL -}; - -void gcInitHandle(HttpServer* pServer) { httpAddMethod(pServer, &gcDecodeMethod); } - -bool gcGetUserFromUrl(HttpContext* pContext) { - HttpParser* pParser = pContext->parser; - if (pParser->path[GC_USER_URL_POS].pos >= TSDB_USER_LEN || pParser->path[GC_USER_URL_POS].pos <= 0) { - return false; - } - - tstrncpy(pContext->user, pParser->path[GC_USER_URL_POS].str, TSDB_USER_LEN); - return true; -} - -bool gcGetPassFromUrl(HttpContext* pContext) { - HttpParser* pParser = pContext->parser; - if (pParser->path[GC_PASS_URL_POS].pos >= HTTP_PASSWORD_LEN || pParser->path[GC_PASS_URL_POS].pos <= 0) { - return false; - } - - tstrncpy(pContext->pass, pParser->path[GC_PASS_URL_POS].str, HTTP_PASSWORD_LEN); - return true; -} - -bool gcProcessLoginRequest(HttpContext* pContext) { - httpDebug("context:%p, fd:%d, user:%s, process grafana login msg", pContext, pContext->fd, pContext->user); - pContext->reqType = HTTP_REQTYPE_LOGIN; - return true; -} - -/** - * Process the query request - * @param fd for http send back - * @param context is taos conn - * @param filter, the request format is json, such as - */ - -// https://github.com/grafana/grafana/blob/master/docs/sources/plugins/developing/datasources.md -// input -//[{ -// "refId": "A", -// "alias" : "taosd", -// "sql" : "select first(taosd) from sys.mem where ts > now-6h and ts < now interval(20000a)" -//}, -//{ -// "refId": "B", -// "alias" : "system", -// "sql" : "select first(taosd) from sys.mem where ts > now-6h and ts < now interval(20000a)" -//}] -// output -//[{ -// "datapoints": [[339.386719, -// 1537873132000], -// [339.656250, -// 1537873162400], -// [339.656250, -// 1537873192600], -// [339.656250, -// 1537873222800], -// [339.589844, -// 1537873253200], -// [339.964844, -// 1537873283400], -// [340.093750, -// 1537873313800], -// [340.093750, -// 1537873344000], -// [340.093750, -// 1537873374200], -// [340.093750, -// 1537873404600]], -// "refId": "A", -// "target" : "taosd" -//}, -//{ -// "datapoints": [[339.386719, -// 1537873132000], -// [339.656250, -// 1537873162400], -// [339.656250, -// 1537873192600], -// [339.656250, -// 1537873222800], -// [339.589844, -// 1537873253200], -// [339.964844, -// 1537873283400], -// [340.093750, -// 1537873313800], -// [340.093750, -// 1537873344000], -// [340.093750, -// 1537873374200], -// [340.093750, -// 1537873404600]], -// "refId": "B", -// "target" : "system" -//}] - -bool gcProcessQueryRequest(HttpContext* pContext) { - httpDebug("context:%p, fd:%d, process grafana query msg", pContext, pContext->fd); - - char* filter = pContext->parser->body.str; - if (filter == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_MSG_INPUT); - return false; - } - - cJSON* root = cJSON_Parse(filter); - if (root == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_GC_REQ_PARSE_ERROR); - return false; - } - - int32_t size = cJSON_GetArraySize(root); - if (size <= 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_GC_QUERY_NULL); - cJSON_Delete(root); - return false; - } - - if (size > 100) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_GC_QUERY_SIZE); - cJSON_Delete(root); - return false; - } - - if (!httpMallocMultiCmds(pContext, size, HTTP_BUFFER_SIZE)) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); - cJSON_Delete(root); - return false; - } - -#define ESCAPE_ERROR_PROC(code, context, root) \ - do { \ - if (code != TSDB_CODE_SUCCESS) { \ - httpSendErrorResp(context, code); \ - \ - cJSON_Delete(root); \ - return false; \ - } \ - } while (0) - - for (int32_t i = 0; i < size; ++i) { - cJSON* query = cJSON_GetArrayItem(root, i); - if (query == NULL) continue; - - cJSON* refId = cJSON_GetObjectItem(query, "refId"); - if (refId == NULL || refId->valuestring == NULL || strlen(refId->valuestring) == 0) { - httpDebug("context:%p, fd:%d, user:%s, refId is null", pContext, pContext->fd, pContext->user); - continue; - } - - char *newStr = NULL; - int32_t retCode = 0; - - retCode = httpCheckAllocEscapeSql(refId->valuestring, &newStr); - ESCAPE_ERROR_PROC(retCode, pContext, root); - - int32_t refIdBuffer = httpAddToSqlCmdBuffer(pContext, newStr); - httpCheckFreeEscapedSql(refId->valuestring, newStr); - if (refIdBuffer == -1) { - httpWarn("context:%p, fd:%d, user:%s, refId buffer is full", pContext, pContext->fd, pContext->user); - break; - } - - cJSON* alias = cJSON_GetObjectItem(query, "alias"); - int32_t aliasBuffer = -1; - if (!(alias == NULL || alias->valuestring == NULL || strlen(alias->valuestring) == 0)) { - retCode = httpCheckAllocEscapeSql(alias->valuestring, &newStr); - ESCAPE_ERROR_PROC(retCode, pContext, root); - - aliasBuffer = httpAddToSqlCmdBuffer(pContext, newStr); - httpCheckFreeEscapedSql(alias->valuestring, newStr); - if (aliasBuffer == -1) { - httpWarn("context:%p, fd:%d, user:%s, alias buffer is full", pContext, pContext->fd, pContext->user); - break; - } - } - if (aliasBuffer == -1) { - aliasBuffer = httpAddToSqlCmdBuffer(pContext, ""); - } - - cJSON* sql = cJSON_GetObjectItem(query, "sql"); - if (sql == NULL || sql->valuestring == NULL || strlen(sql->valuestring) == 0) { - httpDebug("context:%p, fd:%d, user:%s, sql is null", pContext, pContext->fd, pContext->user); - continue; - } - - retCode = httpCheckAllocEscapeSql(sql->valuestring, &newStr); - ESCAPE_ERROR_PROC(retCode, pContext, root); - - int32_t sqlBuffer = httpAddToSqlCmdBuffer(pContext, newStr); - httpCheckFreeEscapedSql(sql->valuestring, newStr); - if (sqlBuffer == -1) { - httpWarn("context:%p, fd:%d, user:%s, sql buffer is full", pContext, pContext->fd, pContext->user); - break; - } - - HttpSqlCmd* cmd = httpNewSqlCmd(pContext); - if (cmd == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); - cJSON_Delete(root); - return false; - } - - cmd->sql = sqlBuffer; - cmd->values = refIdBuffer; - cmd->table = aliasBuffer; - cmd->numOfRows = 0; // hack way as target flags - cmd->timestamp = httpAddToSqlCmdBufferWithSize(pContext, HTTP_GC_TARGET_SIZE + 1); // hack way - - if (cmd->timestamp == -1) { - httpWarn("context:%p, fd:%d, user:%s, cant't malloc target size, sql buffer is full", pContext, pContext->fd, - pContext->user); - break; - } - } - -#undef ESCAPE_ERROR_PROC - - pContext->reqType = HTTP_REQTYPE_MULTI_SQL; - pContext->encodeMethod = &gcQueryMethod; - pContext->multiCmds->pos = 0; - - return true; -} - -bool gcProcessHeartbeatRequest(HttpContext* pContext) { - httpDebug("context:%p, fd:%d, process grafana heartbeat msg", pContext, pContext->fd); - pContext->reqType = HTTP_REQTYPE_HEARTBEAT; - pContext->encodeMethod = &gcHeartBeatMethod; - return true; -} - -/** - * Process get/post/options msg, such as login and logout - */ -bool gcProcessRequest(struct HttpContext* pContext) { - if (httpUrlMatch(pContext, GC_ACTION_URL_POS, "login")) { - gcGetUserFromUrl(pContext); - gcGetPassFromUrl(pContext); - } - - if (strlen(pContext->user) == 0 || strlen(pContext->pass) == 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_AUTH_INFO); - return false; - } - - if (httpUrlMatch(pContext, GC_ACTION_URL_POS, "query")) { - return gcProcessQueryRequest(pContext); - } else if (httpUrlMatch(pContext, GC_ACTION_URL_POS, "heartbeat")) { - return gcProcessHeartbeatRequest(pContext); - } else if (httpUrlMatch(pContext, GC_ACTION_URL_POS, "login")) { - return gcProcessLoginRequest(pContext); - } else { - return gcProcessHeartbeatRequest(pContext); - } -} diff --git a/src/plugins/http/src/httpGcJson.c b/src/plugins/http/src/httpGcJson.c deleted file mode 100644 index 2d361d3794..0000000000 --- a/src/plugins/http/src/httpGcJson.c +++ /dev/null @@ -1,272 +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 "httpGcHandle.h" -#include "httpGcJson.h" -#include "httpJson.h" -#include "httpResp.h" - -unsigned char *base64_decode(const char *value, int inlen, int *outlen); - -void gcInitQueryJson(HttpContext *pContext) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - httpInitJsonBuf(jsonBuf, pContext); - httpWriteJsonBufHead(jsonBuf); - - // data array begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); -} - -void gcCleanQueryJson(HttpContext *pContext) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - // array end - httpJsonToken(jsonBuf, JsonArrEnd); - - httpWriteJsonBufEnd(jsonBuf); -} - -void gcWriteTargetStartJson(JsonBuf *jsonBuf, char *refId, char *target) { - if (strlen(target) == 0) { - target = refId; - } - - // object begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonObjStt); - - // target section - httpJsonPair(jsonBuf, "refId", 5, refId, (int32_t)strlen(refId)); - httpJsonPair(jsonBuf, "target", 6, target, (int32_t)strlen(target)); - - // data begin - httpJsonPairHead(jsonBuf, "datapoints", 10); - - // data array begin - httpJsonToken(jsonBuf, JsonArrStt); -} - -void gcWriteTargetEndJson(JsonBuf *jsonBuf) { - // data array end - httpJsonToken(jsonBuf, JsonArrEnd); - - // object end - httpJsonToken(jsonBuf, JsonObjEnd); -} - -void gcStopQueryJson(HttpContext *pContext, HttpSqlCmd *cmd) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - // write end of target - if (cmd->numOfRows != 0) { - gcWriteTargetEndJson(jsonBuf); - } -} - -bool gcBuildQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return false; - - int32_t num_fields = taos_num_fields(result); - TAOS_FIELD *fields = taos_fetch_fields(result); - if (num_fields == 0) { - return false; - } - - int32_t precision = taos_result_precision(result); - - // such as select count(*) from sys.cpu - // such as select count(*) from sys.cpu group by ipaddr - // such as select count(*) from sys.cpu interval(1d) - // such as select count(*) from sys.cpu interval(1d) group by ipaddr - // such as select count(*) count(*) from sys.cpu group by ipaddr interval(1d) - int32_t dataFields = -1; - int32_t groupFields = -1; - bool hasTimestamp = fields[0].type == TSDB_DATA_TYPE_TIMESTAMP; - if (hasTimestamp) { - dataFields = 1; - if (num_fields > 2) groupFields = num_fields - 1; - } else { - dataFields = 0; - if (num_fields > 1) groupFields = num_fields - 1; - } - - char *refIdBuffer = httpGetCmdsString(pContext, cmd->values); - char *aliasBuffer = httpGetCmdsString(pContext, cmd->table); - char *targetBuffer = httpGetCmdsString(pContext, cmd->timestamp); - - if (groupFields == -1 && cmd->numOfRows == 0) { - gcWriteTargetStartJson(jsonBuf, refIdBuffer, aliasBuffer); - } - cmd->numOfRows += numOfRows; - - for (int32_t k = 0; k < numOfRows; ++k) { - TAOS_ROW row = taos_fetch_row(result); - if (row == NULL) { - cmd->numOfRows--; - continue; - } - int32_t *length = taos_fetch_lengths(result); - - // for group by - if (groupFields != -1) { - char target[HTTP_GC_TARGET_SIZE] = {0}; - int32_t len; - len = snprintf(target, HTTP_GC_TARGET_SIZE, "%s{", aliasBuffer); - for (int32_t i = dataFields + 1; i < num_fields; i++) { - if (row[i] == NULL) { - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:nil", fields[i].name); - - if (i < num_fields - 1) { - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, ", "); - } - - continue; - } - - switch (fields[i].type) { - case TSDB_DATA_TYPE_BOOL: - case TSDB_DATA_TYPE_TINYINT: - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:%d", fields[i].name, *((int8_t *)row[i])); - break; - case TSDB_DATA_TYPE_SMALLINT: - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:%d", fields[i].name, *((int16_t *)row[i])); - break; - case TSDB_DATA_TYPE_INT: - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:%d,", fields[i].name, *((int32_t *)row[i])); - break; - case TSDB_DATA_TYPE_BIGINT: - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:%" PRId64, fields[i].name, *((int64_t *)row[i])); - break; - case TSDB_DATA_TYPE_FLOAT: - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:%.5f", fields[i].name, GET_FLOAT_VAL(row[i])); - break; - case TSDB_DATA_TYPE_DOUBLE: - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:%.9f", fields[i].name, GET_DOUBLE_VAL(row[i])); - break; - case TSDB_DATA_TYPE_BINARY: - case TSDB_DATA_TYPE_NCHAR: - if (row[i] != NULL) { - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:", fields[i].name); - memcpy(target + len, (char *)row[i], length[i]); - len = (int32_t)strlen(target); - } - break; - default: - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "%s:%s", fields[i].name, "-"); - break; - } - if (i < num_fields - 1) { - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, ", "); - } - } - len += snprintf(target + len, HTTP_GC_TARGET_SIZE - len, "}"); - - if (strcmp(target, targetBuffer) != 0) { - // first target not write this section - if (strlen(targetBuffer) != 0) { - gcWriteTargetEndJson(jsonBuf); - } - - // start new target - gcWriteTargetStartJson(jsonBuf, refIdBuffer, target); - strncpy(targetBuffer, target, HTTP_GC_TARGET_SIZE); - } - } // end of group by - - // data row array begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); - - for (int32_t i = dataFields; i >= 0; i--) { - httpJsonItemToken(jsonBuf); - if (row == NULL || i >= num_fields || row[i] == NULL) { - httpJsonOriginString(jsonBuf, "null", 4); - continue; - } - - switch (fields[i].type) { - case TSDB_DATA_TYPE_BOOL: - case TSDB_DATA_TYPE_TINYINT: - httpJsonInt(jsonBuf, *((int8_t *)row[i])); - break; - case TSDB_DATA_TYPE_SMALLINT: - httpJsonInt(jsonBuf, *((int16_t *)row[i])); - break; - case TSDB_DATA_TYPE_INT: - httpJsonInt(jsonBuf, *((int32_t *)row[i])); - break; - case TSDB_DATA_TYPE_BIGINT: - httpJsonInt64(jsonBuf, *((int64_t *)row[i])); - break; - case TSDB_DATA_TYPE_FLOAT: - httpJsonFloat(jsonBuf, GET_FLOAT_VAL(row[i])); - break; - case TSDB_DATA_TYPE_DOUBLE: - httpJsonDouble(jsonBuf, GET_DOUBLE_VAL(row[i])); - break; - case TSDB_DATA_TYPE_BINARY: - case TSDB_DATA_TYPE_NCHAR: - httpJsonStringForTransMean(jsonBuf, (char *)row[i], fields[i].bytes); - break; - case TSDB_DATA_TYPE_TIMESTAMP: { - int64_t ts = convertTimePrecision(*((int64_t *)row[i]), precision, TSDB_TIME_PRECISION_MILLI); - httpJsonInt64(jsonBuf, ts); - break; - } - default: - httpJsonString(jsonBuf, "-", 1); - break; - } - } - - if (dataFields == 0) { - httpJsonItemToken(jsonBuf); - httpJsonString(jsonBuf, "-", 1); - } - - // data row array end - httpJsonToken(jsonBuf, JsonArrEnd); - } - - return true; -} - -void gcSendHeartBeatResp(HttpContext *pContext, HttpSqlCmd *cmd) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - char *desc = "Grafana server receive a quest from you!"; - - httpInitJsonBuf(jsonBuf, pContext); - - httpJsonToken(jsonBuf, JsonObjStt); - httpJsonPair(jsonBuf, "message", (int32_t)strlen("message"), desc, (int32_t)strlen(desc)); - httpJsonToken(jsonBuf, JsonObjEnd); - - char head[1024]; - - int32_t hLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_GRAFANA], httpVersionStr[pContext->parser->httpVersion], - httpKeepAliveStr[pContext->parser->keepAlive], (jsonBuf->lst - jsonBuf->buf)); - httpWriteBuf(pContext, head, hLen); - httpWriteBuf(pContext, jsonBuf->buf, (int32_t)(jsonBuf->lst - jsonBuf->buf)); -} diff --git a/src/plugins/http/src/httpGzip.c b/src/plugins/http/src/httpGzip.c deleted file mode 100644 index 6a6e995c18..0000000000 --- a/src/plugins/http/src/httpGzip.c +++ /dev/null @@ -1,165 +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 "zlib.h" -#include "httpGzip.h" - -typedef enum { - EHTTP_GZIP_INITING, - EHTTP_GZIP_READY, - EHTTP_GZIP_CLOSED, -} EHTTP_GZIP_STATE; - -struct ehttp_gzip_s { - ehttp_gzip_conf_t conf; - ehttp_gzip_callbacks_t callbacks; - void * arg; - z_stream * gzip; - gz_header * header; - char * chunk; - int32_t state; -}; - -static void dummy_on_data(ehttp_gzip_t *gzip, void *arg, const char *buf, int32_t len) {} - -static void ehttp_gzip_cleanup(ehttp_gzip_t *gzip) { - switch (gzip->state) { - case EHTTP_GZIP_READY: { - inflateEnd(gzip->gzip); - } break; - default: - break; - } - if (gzip->gzip) { - free(gzip->gzip); - gzip->gzip = NULL; - } - if (gzip->header) { - free(gzip->header); - gzip->header = NULL; - } - if (gzip->chunk) { - free(gzip->chunk); - gzip->chunk = NULL; - } - gzip->state = EHTTP_GZIP_CLOSED; -} - -ehttp_gzip_t *ehttp_gzip_create_decompressor(ehttp_gzip_conf_t conf, ehttp_gzip_callbacks_t callbacks, void *arg) { - ehttp_gzip_t *gzip = (ehttp_gzip_t *)calloc(1, sizeof(*gzip)); - if (!gzip) return NULL; - - do { - gzip->conf = conf; - gzip->callbacks = callbacks; - gzip->arg = arg; - if (gzip->callbacks.on_data == NULL) gzip->callbacks.on_data = dummy_on_data; - gzip->gzip = (z_stream *)calloc(1, sizeof(*gzip->gzip)); - if (gzip->conf.get_header) { - gzip->header = (gz_header *)calloc(1, sizeof(*gzip->header)); - } - if (gzip->conf.chunk_size <= 0) gzip->conf.chunk_size = EHTTP_GZIP_CHUNK_SIZE_DEFAULT; - gzip->chunk = (char *)malloc(gzip->conf.chunk_size); - if (!gzip->gzip || (gzip->conf.get_header && !gzip->header) || !gzip->chunk) break; - gzip->gzip->zalloc = Z_NULL; - gzip->gzip->zfree = Z_NULL; - gzip->gzip->opaque = Z_NULL; - - // 863 windowBits can also be greater than 15 for optional gzip decoding. Add - // 864 32 to windowBits to enable zlib and gzip decoding with automatic header - // 865 detection, or add 16 to decode only the gzip format (the zlib format will - // 866 return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a - // 867 CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see - // 868 below), inflate() will not automatically decode concatenated gzip streams. - // 869 inflate() will return Z_STREAM_END at the end of the gzip stream. The state - // 870 would need to be reset to continue decoding a subsequent gzip stream. - int32_t ret = inflateInit2(gzip->gzip, 32); // 32/16? 32/16 + MAX_WBITS - if (ret != Z_OK) break; - if (gzip->header) { - ret = inflateGetHeader(gzip->gzip, gzip->header); - } - if (ret != Z_OK) break; - - gzip->gzip->next_out = (z_const Bytef *)gzip->chunk; - gzip->gzip->avail_out = gzip->conf.chunk_size; - gzip->state = EHTTP_GZIP_READY; - return gzip; - } while (0); - - ehttp_gzip_destroy(gzip); - return NULL; -} - -ehttp_gzip_t *ehttp_gzip_create_compressor(ehttp_gzip_conf_t conf, ehttp_gzip_callbacks_t callbacks, void *arg); - -void ehttp_gzip_destroy(ehttp_gzip_t *gzip) { - ehttp_gzip_cleanup(gzip); - - free(gzip); -} - -int32_t ehttp_gzip_write(ehttp_gzip_t *gzip, const char *buf, int32_t len) { - if (gzip->state != EHTTP_GZIP_READY) return -1; - if (len <= 0) return 0; - - gzip->gzip->next_in = (z_const Bytef*)buf; - gzip->gzip->avail_in = len; - - while (gzip->gzip->avail_in) { - int32_t ret; - if (gzip->header) { - ret = inflate(gzip->gzip, Z_BLOCK); - } else { - ret = inflate(gzip->gzip, Z_SYNC_FLUSH); - } - if (ret != Z_OK && ret != Z_STREAM_END) return -1; - - if (gzip->gzip->avail_out > 0) { - if (ret != Z_STREAM_END) continue; - } - - int32_t _len = (int32_t)(gzip->gzip->next_out - (z_const Bytef *)gzip->chunk); - - gzip->gzip->next_out[0] = '\0'; - gzip->callbacks.on_data(gzip, gzip->arg, gzip->chunk, _len); - gzip->gzip->next_out = (z_const Bytef *)gzip->chunk; - gzip->gzip->avail_out = gzip->conf.chunk_size; - } - - return 0; -} - -int32_t ehttp_gzip_finish(ehttp_gzip_t *gzip) { - if (gzip->state != EHTTP_GZIP_READY) return -1; - - gzip->gzip->next_in = NULL; - gzip->gzip->avail_in = 0; - - int32_t ret; - ret = inflate(gzip->gzip, Z_FINISH); - - if (ret != Z_STREAM_END) return -1; - - int32_t len = (int32_t)(gzip->gzip->next_out - (z_const Bytef *)gzip->chunk); - - gzip->gzip->next_out[0] = '\0'; - gzip->callbacks.on_data(gzip, gzip->arg, gzip->chunk, len); - gzip->gzip->next_out = NULL; - gzip->gzip->avail_out = 0; - - return 0; -} diff --git a/src/plugins/http/src/httpHandle.c b/src/plugins/http/src/httpHandle.c deleted file mode 100644 index 9719d93824..0000000000 --- a/src/plugins/http/src/httpHandle.c +++ /dev/null @@ -1,60 +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 "httpInt.h" -#include "httpResp.h" -#include "httpContext.h" -#include "httpHandle.h" - -bool httpDecodeRequest(HttpContext* pContext) { - if (pContext->decodeMethod->fpDecode == NULL) { - return false; - } - - return (*pContext->decodeMethod->fpDecode)(pContext); -} - -/** - * Process the request from http pServer - */ -bool httpProcessData(HttpContext* pContext) { - if (!httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_HANDLING)) { - httpTrace("context:%p, fd:%d, state:%s not in ready state, stop process request", pContext, pContext->fd, - httpContextStateStr(pContext->state)); - pContext->error = true; - httpCloseContextByApp(pContext); - return false; - } - - // handle Cross-domain request - if (strcmp(pContext->parser->method, "OPTIONS") == 0) { - httpTrace("context:%p, fd:%d, process options request", pContext, pContext->fd); - httpSendOptionResp(pContext, "process options request success"); - } else { - if (!httpDecodeRequest(pContext)) { - /* - * httpCloseContextByApp has been called when parsing the error - */ - // httpCloseContextByApp(pContext); - } else { - httpClearParser(pContext->parser); - httpProcessRequest(pContext); - } - } - - return true; -} diff --git a/src/plugins/http/src/httpJson.c b/src/plugins/http/src/httpJson.c deleted file mode 100644 index 86e0f2f40b..0000000000 --- a/src/plugins/http/src/httpJson.c +++ /dev/null @@ -1,534 +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 "taosmsg.h" -#include "taoserror.h" -#include "tglobal.h" -#include "http.h" -#include "httpLog.h" -#include "httpJson.h" -#include "httpResp.h" -#include "httpUtil.h" - -#define MAX_NUM_STR_SZ 25 - -char JsonItmTkn = ','; -char JsonObjStt = '{'; -char JsonObjEnd = '}'; -char JsonArrStt = '['; -char JsonArrEnd = ']'; -char JsonStrStt = '\"'; -char JsonStrEnd = '\"'; -char JsonPairTkn = ':'; -char JsonNulTkn[] = "null"; -char JsonTrueTkn[] = "true"; -char JsonFalseTkn[] = "false"; - -int32_t httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int32_t sz) { - int32_t len; - int32_t countWait = 0; - int32_t writeLen = 0; - - do { - if (pContext->fd > 2) { - len = (int32_t)taosSend(pContext->fd, buf + writeLen, (size_t)(sz - writeLen), MSG_NOSIGNAL); - } else { - return sz; - } - - if (len < 0) { - httpDebug("context:%p, fd:%d, socket write errno:%d:%s, times:%d", pContext, pContext->fd, errno, strerror(errno), - countWait); - if (++countWait > HTTP_WRITE_RETRY_TIMES) break; - taosMsleep(HTTP_WRITE_WAIT_TIME_MS); - continue; - } else if (len == 0) { - httpDebug("context:%p, fd:%d, socket write errno:%d:%s, connect already closed", pContext, pContext->fd, errno, - strerror(errno)); - break; - } else { - countWait = 0; - writeLen += len; - } - } while (writeLen < sz); - - return writeLen; -} - -int32_t httpWriteBuf(struct HttpContext* pContext, const char* buf, int32_t sz) { - int32_t writeSz = httpWriteBufByFd(pContext, buf, sz); - if (writeSz != sz) { - httpError("context:%p, fd:%d, dataSize:%d, writeSize:%d, failed to send response:\n%s", pContext, pContext->fd, sz, - writeSz, buf); - } else { - httpTrace("context:%p, fd:%d, dataSize:%d, writeSize:%d, response:\n%s", pContext, pContext->fd, sz, writeSz, buf); - } - - return writeSz; -} - -int32_t httpWriteBufNoTrace(struct HttpContext* pContext, const char* buf, int32_t sz) { - int32_t writeSz = httpWriteBufByFd(pContext, buf, sz); - if (writeSz != sz) { - httpError("context:%p, fd:%d, dataSize:%d, writeSize:%d, failed to send response", pContext, pContext->fd, sz, - writeSz); - } - - return writeSz; -} - -int32_t httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast) { - int32_t remain = 0; - char sLen[24]; - int32_t srcLen = (int32_t)(buf->lst - buf->buf); - - if (buf->pContext->fd <= 0) { - httpTrace("context:%p, fd:%d, write json body error", buf->pContext, buf->pContext->fd); - buf->pContext->fd = -1; - } - - /* - * HTTP servers often use compression to optimize transmission, for example - * with Content-Encoding: gzip or Content-Encoding: deflate. - * If both compression and chunked encoding are enabled, then the content stream is first compressed, then chunked; - * so the chunk encoding itself is not compressed, and the data in each chunk is not compressed individually. - * The remote endpoint then decodes the stream by concatenating the chunks and uncompressing the result. - */ - - if (buf->pContext->parser->acceptEncodingGzip == 0 || !tsHttpEnableCompress) { - if (buf->lst == buf->buf) { - httpTrace("context:%p, fd:%d, no data need dump", buf->pContext, buf->pContext->fd); - return 0; // there is no data to dump. - } else { - int32_t len = sprintf(sLen, "%x\r\n", srcLen); - httpTrace("context:%p, fd:%d, write body, chunkSize:%d, response:\n%s", buf->pContext, buf->pContext->fd, srcLen, - buf->buf); - httpWriteBufNoTrace(buf->pContext, sLen, len); - remain = httpWriteBufNoTrace(buf->pContext, buf->buf, srcLen); - } - } else { - char compressBuf[JSON_BUFFER_SIZE] = {0}; - int32_t compressBufLen = JSON_BUFFER_SIZE; - int32_t ret = httpGzipCompress(buf->pContext, buf->buf, srcLen, compressBuf, &compressBufLen, isTheLast); - if (ret == 0) { - if (compressBufLen > 0) { - int32_t len = sprintf(sLen, "%x\r\n", compressBufLen); - httpTrace("context:%p, fd:%d, write body, chunkSize:%d, compressSize:%d, last:%d, response:\n%s", buf->pContext, - buf->pContext->fd, srcLen, compressBufLen, isTheLast, buf->buf); - httpWriteBufNoTrace(buf->pContext, sLen, len); - remain = httpWriteBufNoTrace(buf->pContext, (const char*)compressBuf, compressBufLen); - } else { - httpDebug("context:%p, fd:%d, last:%d, compress already dumped, response:\n%s", buf->pContext, - buf->pContext->fd, isTheLast, buf->buf); - remain = 0; // there is no data to dump. - } - } else { - httpError("context:%p, fd:%d, failed to compress data, chunkSize:%d, last:%d, error:%d, response:\n%s", - buf->pContext, buf->pContext->fd, srcLen, isTheLast, ret, buf->buf); - remain = 0; - } - } - - httpWriteBufNoTrace(buf->pContext, "\r\n", 2); - buf->total += (int32_t)(buf->lst - buf->buf); - buf->lst = buf->buf; - memset(buf->buf, 0, (size_t)buf->size); - return remain; -} - -void httpWriteJsonBufHead(JsonBuf* buf) { - if (buf->pContext->fd <= 0) { - buf->pContext->fd = -1; - } - - char msg[1024] = {0}; - int32_t len = -1; - - if (buf->pContext->parser->acceptEncodingGzip == 0 || !tsHttpEnableCompress) { - len = sprintf(msg, httpRespTemplate[HTTP_RESPONSE_CHUNKED_UN_COMPRESS], httpVersionStr[buf->pContext->parser->httpVersion], - httpKeepAliveStr[buf->pContext->parser->keepAlive]); - } else { - len = sprintf(msg, httpRespTemplate[HTTP_RESPONSE_CHUNKED_COMPRESS], httpVersionStr[buf->pContext->parser->httpVersion], - httpKeepAliveStr[buf->pContext->parser->keepAlive]); - } - - httpWriteBuf(buf->pContext, (const char*)msg, len); -} - -void httpWriteJsonBufEnd(JsonBuf* buf) { - if (buf->pContext->fd <= 0) { - httpTrace("context:%p, fd:%d, json buf fd is 0", buf->pContext, buf->pContext->fd); - buf->pContext->fd = -1; - } - - httpWriteJsonBufBody(buf, true); - httpWriteBufNoTrace(buf->pContext, "0\r\n\r\n", 5); // end of chunked resp -} - -void httpInitJsonBuf(JsonBuf* buf, struct HttpContext* pContext) { - buf->lst = buf->buf; - buf->total = 0; - buf->size = JSON_BUFFER_SIZE; // option setting - buf->pContext = pContext; - memset(buf->lst, 0, JSON_BUFFER_SIZE); - - if (pContext->parser->acceptEncodingGzip == 1 && tsHttpEnableCompress) { - httpGzipCompressInit(buf->pContext); - } - - httpTrace("context:%p, fd:%d, json buffer initialized", buf->pContext, buf->pContext->fd); -} - -void httpJsonItemToken(JsonBuf* buf) { - char c = *(buf->lst - 1); - if (c == JsonArrStt || c == JsonObjStt || c == JsonPairTkn || c == JsonItmTkn) { - return; - } - if (buf->lst > buf->buf) httpJsonToken(buf, JsonItmTkn); -} - -void httpJsonString(JsonBuf* buf, char* sVal, int32_t len) { - httpJsonItemToken(buf); - httpJsonToken(buf, JsonStrStt); - httpJsonPrint(buf, sVal, len); - httpJsonToken(buf, JsonStrEnd); -} - -void httpJsonOriginString(JsonBuf* buf, char* sVal, int32_t len) { - httpJsonItemToken(buf); - httpJsonPrint(buf, sVal, len); -} - -void httpJsonStringForTransMean(JsonBuf* buf, char* sVal, int32_t maxLen) { - httpJsonItemToken(buf); - httpJsonToken(buf, JsonStrStt); - - if (sVal != NULL) { - // dispose transferred meaning byte - char* lastPos = sVal; - char* curPos = sVal; - - for (int32_t i = 0; i < maxLen; ++i) { - if (*curPos == 0) { - break; - } - - if (*curPos == '\"') { - httpJsonPrint(buf, lastPos, (int32_t)(curPos - lastPos)); - curPos++; - lastPos = curPos; - httpJsonPrint(buf, "\\\"", 2); - } else if (*curPos == '\\') { - httpJsonPrint(buf, lastPos, (int32_t)(curPos - lastPos)); - curPos++; - lastPos = curPos; - httpJsonPrint(buf, "\\\\", 2); - } else { - curPos++; - } - } - - if (*lastPos) { - httpJsonPrint(buf, lastPos, (int32_t)(curPos - lastPos)); - } - } - - httpJsonToken(buf, JsonStrEnd); -} - -void httpJsonInt64(JsonBuf* buf, int64_t num) { - httpJsonItemToken(buf); - httpJsonTestBuf(buf, MAX_NUM_STR_SZ); - buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%" PRId64, num); -} - -void httpJsonUInt64(JsonBuf* buf, uint64_t num) { - httpJsonItemToken(buf); - httpJsonTestBuf(buf, MAX_NUM_STR_SZ); - buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%" PRIu64, num); -} - -void httpJsonTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { - char ts[35] = {0}; - - int32_t fractionLen; - char* format = NULL; - time_t quot = 0; - int64_t mod = 0; - - switch (timePrecision) { - case TSDB_TIME_PRECISION_MILLI: { - mod = ((t) % 1000 + 1000) % 1000; - if (t < 0 && mod != 0) { - t -= 1000; - } - quot = t / 1000; - fractionLen = 5; - format = ".%03" PRId64; - break; - } - - case TSDB_TIME_PRECISION_MICRO: { - mod = ((t) % 1000000 + 1000000) % 1000000; - if (t < 0 && mod != 0) { - t -= 1000000; - } - quot = t / 1000000; - fractionLen = 8; - format = ".%06" PRId64; - break; - } - - case TSDB_TIME_PRECISION_NANO: { - mod = ((t) % 1000000000 + 1000000000) % 1000000000; - if (t < 0 && mod != 0) { - t -= 1000000000; - } - quot = t / 1000000000; - fractionLen = 11; - format = ".%09" PRId64; - break; - } - - default: - fractionLen = 0; - assert(false); - } - - struct tm ptm = {0}; - localtime_r(", &ptm); - int32_t length = (int32_t)strftime(ts, 35, "%Y-%m-%d %H:%M:%S", &ptm); - length += snprintf(ts + length, fractionLen, format, mod); - - httpJsonString(buf, ts, length); -} - -void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { - char ts[40] = {0}; - struct tm* ptm; - - int32_t fractionLen; - char* format = NULL; - time_t quot = 0; - long mod = 0; - - switch (timePrecision) { - case TSDB_TIME_PRECISION_MILLI: { - mod = ((t) % 1000 + 1000) % 1000; - if (t < 0 && mod != 0) { - t -= 1000; - } - quot = t / 1000; - fractionLen = 5; - format = ".%03" PRId64; - break; - } - - case TSDB_TIME_PRECISION_MICRO: { - mod = ((t) % 1000000 + 1000000) % 1000000; - if (t < 0 && mod != 0) { - t -= 1000000; - } - quot = t / 1000000; - fractionLen = 8; - format = ".%06" PRId64; - break; - } - - case TSDB_TIME_PRECISION_NANO: { - mod = ((t) % 1000000000 + 1000000000) % 1000000000; - if (t < 0 && mod != 0) { - t -= 1000000000; - } - quot = t / 1000000000; - fractionLen = 11; - format = ".%09" PRId64; - break; - } - - default: - fractionLen = 0; - assert(false); - } - - ptm = localtime("); - int32_t length = (int32_t)strftime(ts, 40, "%Y-%m-%dT%H:%M:%S", ptm); - length += snprintf(ts + length, fractionLen, format, mod); - length += (int32_t)strftime(ts + length, 40 - length, "%z", ptm); - - httpJsonString(buf, ts, length); -} - -void httpJsonInt(JsonBuf* buf, int32_t num) { - httpJsonItemToken(buf); - httpJsonTestBuf(buf, MAX_NUM_STR_SZ); - buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%d", num); -} - -void httpJsonUInt(JsonBuf* buf, uint32_t num) { - httpJsonItemToken(buf); - httpJsonTestBuf(buf, MAX_NUM_STR_SZ); - buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%u", num); -} - -void httpJsonFloat(JsonBuf* buf, float num) { - httpJsonItemToken(buf); - httpJsonTestBuf(buf, MAX_NUM_STR_SZ); - if (isinf(num) || isnan(num)) { - buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "null"); - } else if (num > 1E10 || num < -1E10) { - buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%.5e", num); - } else { - buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%.5f", num); - } -} - -void httpJsonDouble(JsonBuf* buf, double num) { - httpJsonItemToken(buf); - httpJsonTestBuf(buf, MAX_NUM_STR_SZ); - if (isinf(num) || isnan(num)) { - buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "null"); - } else if (num > 1E10 || num < -1E10) { - buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%.9e", num); - } else { - buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%.9f", num); - } -} - -void httpJsonNull(JsonBuf* buf) { httpJsonString(buf, "null", 4); } - -void httpJsonBool(JsonBuf* buf, int32_t val) { - if (val == 0) - httpJsonPrint(buf, JsonFalseTkn, sizeof(JsonFalseTkn)); - else - httpJsonPrint(buf, JsonTrueTkn, sizeof(JsonTrueTkn)); -} - -void httpJsonPairHead(JsonBuf* buf, char* name, int32_t len) { - httpJsonItemToken(buf); - httpJsonString(buf, name, len); - httpJsonToken(buf, JsonPairTkn); -} - -void httpJsonPair(JsonBuf* buf, char* name, int32_t nameLen, char* sVal, int32_t valLen) { - httpJsonPairHead(buf, name, nameLen); - httpJsonString(buf, sVal, valLen); -} - -void httpJsonPairOriginString(JsonBuf* buf, char* name, int32_t nameLen, char* sVal, int32_t valLen) { - httpJsonPairHead(buf, name, nameLen); - httpJsonOriginString(buf, sVal, valLen); -} - -void httpJsonPairIntVal(JsonBuf* buf, char* name, int32_t nNameLen, int32_t num) { - httpJsonPairHead(buf, name, nNameLen); - httpJsonInt(buf, num); -} - -void httpJsonPairInt64Val(JsonBuf* buf, char* name, int32_t nNameLen, int64_t num) { - httpJsonPairHead(buf, name, nNameLen); - httpJsonInt64(buf, num); -} - -void httpJsonPairBoolVal(JsonBuf* buf, char* name, int32_t nNameLen, int32_t num) { - httpJsonPairHead(buf, name, nNameLen); - httpJsonBool(buf, num); -} - -void httpJsonPairFloatVal(JsonBuf* buf, char* name, int32_t nNameLen, float num) { - httpJsonPairHead(buf, name, nNameLen); - httpJsonFloat(buf, num); -} - -void httpJsonPairDoubleVal(JsonBuf* buf, char* name, int32_t nNameLen, double num) { - httpJsonPairHead(buf, name, nNameLen); - httpJsonDouble(buf, num); -} - -void httpJsonPairNullVal(JsonBuf* buf, char* name, int32_t nNameLen) { - httpJsonPairHead(buf, name, nNameLen); - httpJsonNull(buf); -} - -void httpJsonPairArray(JsonBuf* buf, char* name, int32_t len, httpJsonBuilder fnBuilder, void* dsHandle) { - httpJsonPairHead(buf, name, len); - httpJsonArray(buf, fnBuilder, dsHandle); -} - -void httpJsonPairObject(JsonBuf* buf, char* name, int32_t len, httpJsonBuilder fnBuilder, void* dsHandle) { - httpJsonPairHead(buf, name, len); - httpJsonObject(buf, fnBuilder, dsHandle); -} - -void httpJsonObject(JsonBuf* buf, httpJsonBuilder fnBuilder, void* dsHandle) { - httpJsonItemToken(buf); - httpJsonToken(buf, JsonObjStt); - (*fnBuilder)(buf, dsHandle); - httpJsonToken(buf, JsonObjEnd); -} - -void httpJsonArray(JsonBuf* buf, httpJsonBuilder fnBuilder, void* jsonHandle) { - httpJsonItemToken(buf); - httpJsonToken(buf, JsonArrStt); - (*fnBuilder)(buf, jsonHandle); - httpJsonToken(buf, JsonArrEnd); -} - -void httpJsonTestBuf(JsonBuf* buf, int32_t safety) { - if ((buf->lst - buf->buf + safety) < buf->size) return; - // buf->slot = *buf->lst; - httpWriteJsonBufBody(buf, false); -} - -void httpJsonToken(JsonBuf* buf, char c) { - httpJsonTestBuf(buf, MAX_NUM_STR_SZ); // maybe object stack - *buf->lst++ = c; -} - -void httpJsonPrint(JsonBuf* buf, const char* json, int32_t len) { - if (len == 0 || len >= JSON_BUFFER_SIZE) { - return; - } - - if (len > buf->size) { - httpWriteJsonBufBody(buf, false); - httpJsonPrint(buf, json, len); - // buf->slot = json[len - 1]; - return; - } - httpJsonTestBuf(buf, len + 2); - memcpy(buf->lst, json, (size_t)len); - buf->lst += len; -} - -void httpJsonPairStatus(JsonBuf* buf, int32_t code) { - if (code == 0) { - httpJsonPair(buf, "status", 6, "succ", 4); - } else { - httpJsonPair(buf, "status", 6, "error", 5); - httpJsonItemToken(buf); - httpJsonPairIntVal(buf, "code", 4, code & 0XFFFF); - httpJsonItemToken(buf); - if (code == TSDB_CODE_MND_DB_NOT_SELECTED) { - httpJsonPair(buf, "desc", 4, "failed to create database", 23); - } else if (code == TSDB_CODE_MND_INVALID_TABLE_NAME) { - httpJsonPair(buf, "desc", 4, "failed to create table", 22); - } else { - httpJsonPair(buf, "desc", 4, (char*)tstrerror(code), (int32_t)strlen(tstrerror(code))); - } - } -} diff --git a/src/plugins/http/src/httpMetricsHandle.c b/src/plugins/http/src/httpMetricsHandle.c deleted file mode 100644 index dbabd48774..0000000000 --- a/src/plugins/http/src/httpMetricsHandle.c +++ /dev/null @@ -1,184 +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 "tfs.h" - -#include "httpMetricsHandle.h" -#include "dnode.h" -#include "httpLog.h" - -static HttpDecodeMethod metricsDecodeMethod = {"metrics", metricsProcessRequest}; - -void metricsInitHandle(HttpServer* pServer) { - httpAddMethod(pServer, &metricsDecodeMethod); -} - -bool metricsProcessRequest(HttpContext* pContext) { - httpDebug("context:%p, fd:%d, user:%s, process admin grant msg", pContext, pContext->fd, pContext->user); - - JsonBuf* jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) { - httpError("failed to allocate memory for metrics"); - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); - return false; - } - - httpInitJsonBuf(jsonBuf, pContext); - httpWriteJsonBufHead(jsonBuf); - - httpJsonToken(jsonBuf, JsonObjStt); - { - char* keyDisks = "tags"; - httpJsonPairHead(jsonBuf, keyDisks, (int32_t)strlen(keyDisks)); - httpJsonToken(jsonBuf, JsonArrStt); - { - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonObjStt); - char* keyTagName = "name"; - char* keyTagValue = "value"; - httpJsonPairOriginString(jsonBuf, keyTagName, (int32_t)strlen(keyTagName), "\"dnode_id\"", - (int32_t)strlen("\"dnode_id\"")); - int32_t dnodeId = dnodeGetDnodeId(); - httpJsonPairIntVal(jsonBuf, keyTagValue, (int32_t)strlen(keyTagValue), dnodeId); - httpJsonToken(jsonBuf, JsonObjEnd); - } - httpJsonToken(jsonBuf, JsonArrEnd); - } - - { - if (tsDnodeStartTime != 0) { - int64_t now = taosGetTimestampMs(); - int64_t upTime = now-tsDnodeStartTime; - char* keyUpTime = "up_time"; - httpJsonPairInt64Val(jsonBuf, keyUpTime, (int32_t)strlen(keyUpTime), upTime); - } - } - - { - int32_t cpuCores = taosGetCpuCores(); - char* keyCpuCores = "cpu_cores"; - httpJsonPairIntVal(jsonBuf, keyCpuCores, (int32_t)strlen(keyCpuCores), cpuCores); - - float sysCpuUsage = 0; - float procCpuUsage = 0; - bool succeeded = taosGetCpuUsage(&sysCpuUsage, &procCpuUsage); - if (!succeeded) { - httpError("failed to get cpu usage"); - } else { - if (sysCpuUsage <= procCpuUsage) { - sysCpuUsage = procCpuUsage + 0.1f; - } - char* keyCpuSystem = "cpu_system"; - char* keyCpuEngine = "cpu_engine"; - httpJsonPairFloatVal(jsonBuf, keyCpuSystem, (int32_t)strlen(keyCpuSystem), sysCpuUsage); - httpJsonPairFloatVal(jsonBuf, keyCpuEngine, (int32_t)strlen(keyCpuEngine), procCpuUsage); - } - } - - { - float sysMemoryUsedMB = 0; - bool succeeded = taosGetSysMemory(&sysMemoryUsedMB); - if (!succeeded) { - httpError("failed to get sys memory info"); - } else { - char* keyMemSystem = "mem_system"; - httpJsonPairFloatVal(jsonBuf, keyMemSystem, (int32_t)strlen(keyMemSystem), sysMemoryUsedMB); - } - - float procMemoryUsedMB = 0; - succeeded = taosGetProcMemory(&procMemoryUsedMB); - if (!succeeded) { - httpError("failed to get proc memory info"); - } else { - char* keyMemEngine = "mem_engine"; - httpJsonPairFloatVal(jsonBuf, keyMemEngine, (int32_t)strlen(keyMemEngine), procMemoryUsedMB); - } - } - - { - int64_t bytes = 0, rbytes = 0, tbytes = 0; - bool succeeded = taosGetCardInfo(&bytes, &rbytes, &tbytes); - if (!succeeded) { - httpError("failed to get network info"); - } else { - char* keyNetIn = "net_in"; - char* keyNetOut = "net_out"; - httpJsonPairInt64Val(jsonBuf, keyNetIn, (int32_t)strlen(keyNetIn), rbytes); - httpJsonPairInt64Val(jsonBuf, keyNetOut, (int32_t)strlen(keyNetOut), tbytes); - } - } - - { - int64_t rchars = 0; - int64_t wchars = 0; - bool succeeded = taosReadProcIO(&rchars, &wchars); - if (!succeeded) { - httpError("failed to get io info"); - } else { - char* keyIORead = "io_read"; - char* keyIOWrite = "io_write"; - httpJsonPairInt64Val(jsonBuf, keyIORead, (int32_t)strlen(keyIORead), rchars); - httpJsonPairInt64Val(jsonBuf, keyIOWrite, (int32_t)strlen(keyIOWrite), wchars); - } - } - - { - const int8_t numTiers = 3; - SFSMeta fsMeta; - STierMeta* tierMetas = calloc(numTiers, sizeof(STierMeta)); - tfsUpdateInfo(&fsMeta, tierMetas, numTiers); - { - char* keyDiskUsed = "disk_used"; - char* keyDiskTotal = "disk_total"; - httpJsonPairInt64Val(jsonBuf, keyDiskTotal, (int32_t)strlen(keyDiskTotal), fsMeta.tsize); - httpJsonPairInt64Val(jsonBuf, keyDiskUsed, (int32_t)strlen(keyDiskUsed), fsMeta.used); - char* keyDisks = "disks"; - httpJsonPairHead(jsonBuf, keyDisks, (int32_t)strlen(keyDisks)); - httpJsonToken(jsonBuf, JsonArrStt); - for (int i = 0; i < numTiers; ++i) { - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonObjStt); - char* keyDataDirLevelUsed = "datadir_used"; - char* keyDataDirLevelTotal = "datadir_total"; - httpJsonPairInt64Val(jsonBuf, keyDataDirLevelUsed, (int32_t)strlen(keyDataDirLevelUsed), tierMetas[i].used); - httpJsonPairInt64Val(jsonBuf, keyDataDirLevelTotal, (int32_t)strlen(keyDataDirLevelTotal), tierMetas[i].size); - httpJsonToken(jsonBuf, JsonObjEnd); - } - httpJsonToken(jsonBuf, JsonArrEnd); - } - free(tierMetas); - } - - { - SStatisInfo info = dnodeGetStatisInfo(); - { - char* keyReqHttp = "req_http"; - char* keyReqSelect = "req_select"; - char* keyReqInsert = "req_insert"; - httpJsonPairInt64Val(jsonBuf, keyReqHttp, (int32_t)strlen(keyReqHttp), info.httpReqNum); - httpJsonPairInt64Val(jsonBuf, keyReqSelect, (int32_t)strlen(keyReqSelect), info.queryReqNum); - httpJsonPairInt64Val(jsonBuf, keyReqInsert, (int32_t)strlen(keyReqInsert), info.submitReqNum); - } - } - - httpJsonToken(jsonBuf, JsonObjEnd); - - httpWriteJsonBufEnd(jsonBuf); - pContext->reqType = HTTP_REQTYPE_OTHERS; - httpFreeJsonBuf(pContext); - return false; -} \ No newline at end of file diff --git a/src/plugins/http/src/httpParser.c b/src/plugins/http/src/httpParser.c deleted file mode 100644 index 7066f19769..0000000000 --- a/src/plugins/http/src/httpParser.c +++ /dev/null @@ -1,1182 +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 "httpLog.h" -#include "httpContext.h" -#include "httpParser.h" -#include "httpGzip.h" -#include "httpAuth.h" - -static void httpOnData(ehttp_gzip_t *gzip, void *arg, const char *buf, int32_t len); - -static HttpStatus httpStatusCodes[] = { - {HTTP_CODE_CONTINUE, "Continue"}, - {HTTP_CODE_SWITCHING_PROTOCOL, "Switching Protocol"}, - {HTTP_CODE_PROCESSING, "Processing (WebDAV)"}, - {HTTP_CODE_EARLY_HINTS, "Early Hints"}, - {HTTP_CODE_OK, "OK"}, - {HTTP_CODE_CREATED, "Created"}, - {HTTP_CODE_ACCEPTED, "Accepted"}, - {HTTP_CODE_NON_AUTHORITATIVE_INFO, "Non-Authoritative Information"}, - {HTTP_CODE_NO_CONTENT, "No Content"}, - {HTTP_CODE_RESET_CONTENT, "Reset Content"}, - {HTTP_CODE_PARTIAL_CONTENT, "Partial Content"}, - {HTTP_CODE_MULTI_STATUS, "Multi-Status (WebDAV)"}, - {HTTP_CODE_ALREADY_REPORTED, "Already Reported (WebDAV)"}, - {HTTP_CODE_IM_USED, "IM Used (HTTP Delta encoding)"}, - {HTTP_CODE_MULTIPLE_CHOICE, "Multiple Choice"}, - {HTTP_CODE_MOVED_PERMANENTLY, "Moved Permanently"}, - {HTTP_CODE_FOUND, "Found"}, - {HTTP_CODE_SEE_OTHER, "See Other"}, - {HTTP_CODE_NOT_MODIFIED, "Not Modified"}, - {HTTP_CODE_USE_PROXY, "Use Proxy"}, - {HTTP_CODE_UNUSED, "unused"}, - {HTTP_CODE_TEMPORARY_REDIRECT, "Temporary Redirect"}, - {HTTP_CODE_PERMANENT_REDIRECT, "Permanent Redirect"}, - {HTTP_CODE_BAD_REQUEST, "Bad Request"}, - {HTTP_CODE_UNAUTHORIZED, "Unauthorized"}, - {HTTP_CODE_PAYMENT_REQUIRED, "Payment Required"}, - {HTTP_CODE_FORBIDDEN, "Forbidden"}, - {HTTP_CODE_NOT_FOUND, "Not Found"}, - {HTTP_CODE_METHOD_NOT_ALLOWED, "Method Not Allowed"}, - {HTTP_CODE_NOT_ACCEPTABLE, "Not Acceptable"}, - {HTTP_CODE_PROXY_AUTH_REQUIRED, "Proxy Authentication Required"}, - {HTTP_CODE_REQUEST_TIMEOUT, "Request Timeout"}, - {HTTP_CODE_CONFLICT, "Conflict"}, - {HTTP_CODE_GONE, "Gone"}, - {HTTP_CODE_LENGTH_REQUIRED, "Length Required"}, - {HTTP_CODE_PRECONDITION_FAILED, "Precondition Failed"}, - {HTTP_CODE_PAYLOAD_TOO_LARGE, "Payload Too Large"}, - {HTTP_CODE_URI_TOO_LARGE, "URI Too Long"}, - {HTTP_CODE_UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type"}, - {HTTP_CODE_RANGE_NOT_SATISFIABLE, "Range Not Satisfiable"}, - {HTTP_CODE_EXPECTATION_FAILED, "Expectation Failed"}, - {HTTP_CODE_IM_A_TEAPOT, "I'm a teapot"}, - {HTTP_CODE_MISDIRECTED_REQUEST, "Misdirected Request"}, - {HTTP_CODE_UNPROCESSABLE_ENTITY, "Unprocessable Entity (WebDAV)"}, - {HTTP_CODE_LOCKED, "Locked (WebDAV)"}, - {HTTP_CODE_FAILED_DEPENDENCY, "Failed Dependency (WebDAV)"}, - {HTTP_CODE_TOO_EARLY, "Too Early"}, - {HTTP_CODE_UPGRADE_REQUIRED, "Upgrade Required"}, - {HTTP_CODE_PRECONDITION_REQUIRED, "Precondition Required"}, - {HTTP_CODE_TOO_MANY_REQUESTS, "Too Many Requests"}, - {HTTP_CODE_REQ_HDR_FIELDS_TOO_LARGE,"Request Header Fields Too Large"}, - {HTTP_CODE_UNAVAIL_4_LEGAL_REASONS, "Unavailable For Legal Reasons"}, - {HTTP_CODE_INTERNAL_SERVER_ERROR, "Internal Server Error"}, - {HTTP_CODE_NOT_IMPLEMENTED, "Not Implemented"}, - {HTTP_CODE_BAD_GATEWAY, "Bad Gateway"}, - {HTTP_CODE_SERVICE_UNAVAILABLE, "Service Unavailable"}, - {HTTP_CODE_GATEWAY_TIMEOUT, "Gateway Timeout"}, - {HTTP_CODE_HTTP_VER_NOT_SUPPORTED, "HTTP Version Not Supported"}, - {HTTP_CODE_VARIANT_ALSO_NEGOTIATES, "Variant Also Negotiates"}, - {HTTP_CODE_INSUFFICIENT_STORAGE, "Insufficient Storage"}, - {HTTP_CODE_LOOP_DETECTED, "Loop Detected (WebDAV)"}, - {HTTP_CODE_NOT_EXTENDED, "Not Extended"}, - {HTTP_CODE_NETWORK_AUTH_REQUIRED, "Network Authentication Required"}, - {0, NULL} -}; - -char *httpGetStatusDesc(int32_t statusCode) { - HttpStatus *p = httpStatusCodes; - while (p->code != 0) { - if (p->code == statusCode) return p->desc; - ++p; - } - return "Unknow status code"; -} - -static void httpCleanupString(HttpString *str) { - if (str->str) { - free(str->str); - str->str = NULL; - str->pos = 0; - str->size = 0; - } -} - -static int32_t httpAppendString(HttpString *str, const char *s, int32_t len) { - char *new_str = NULL; - - if (str->size == 0) { - str->pos = 0; - str->size = len + 1; - str->str = malloc(str->size); - } else if (str->pos + len + 1 >= str->size) { - str->size += len; - str->size *= 4; - - new_str = realloc(str->str, str->size); - if (new_str == NULL && str->str) { - // if str->str was not NULL originally, - // the old allocated memory was left unchanged, - // see man 3 realloc - free(str->str); - } - - str->str = new_str; - } else { - } - - if (str->str == NULL) return -1; - - memcpy(str->str + str->pos, s, len); - str->pos += len; - str->str[str->pos] = 0; - return 0; -} - -static void httpClearString(HttpString *str) { - if (str->str) { - str->str[0] = '\0'; - str->pos = 0; - } -} - -static int32_t httpOnError(HttpParser *parser, int32_t httpCode, int32_t parseCode) { - HttpContext *pContext = parser->pContext; - if (httpCode != 0) parser->httpCode = httpCode; - if (parseCode != 0) parser->parseCode = parseCode; - - httpError("context:%p, fd:%d, parse failed, httpCode:%d parseCode:%d reason:%s", pContext, pContext->fd, httpCode, - parseCode & 0XFFFF, tstrerror(parseCode)); - return 0; -} - -static int32_t httpOnRequestLine(HttpParser *pParser, char *method, char *target, char *version) { - HttpContext *pContext = pParser->pContext; - httpDebug("context:%p, fd:%d, method:%s target:%s version:%s", pContext, pContext->fd, method, target, version); - - // parse url - char *pStart = target + 1; - for (int32_t i = 0; i < HTTP_MAX_URL; i++) { - char *pSeek = strchr(pStart, '/'); - if (pSeek == NULL) { - (void)httpAppendString(pParser->path + i, pStart, (int32_t)strlen(pStart)); - break; - } else { - (void)httpAppendString(pParser->path + i, pStart, (int32_t)(pSeek - pStart)); - } - pStart = pSeek + 1; - } - - // parse decode method - for (int32_t i = 0; i < tsHttpServer.methodScannerLen; i++) { - HttpDecodeMethod *_method = tsHttpServer.methodScanner[i]; - if (strcmp(_method->module, pParser->path[0].str) == 0) { - pContext->decodeMethod = _method; - break; - } - } - - if (pContext->decodeMethod != NULL) { - httpTrace("context:%p, fd:%d, decode method is %s", pContext, pContext->fd, pContext->decodeMethod->module); - } else { - httpError("context:%p, fd:%d, the url is not support, target:%s", pContext, pContext->fd, target); - httpOnError(pParser, 0, TSDB_CODE_HTTP_UNSUPPORT_URL); - return -1; - } - - // parse version - if (pParser->httpVersion < HTTP_VERSION_10 || pParser->httpVersion > HTTP_VERSION_12) { - httpError("context:%p, fd:%d, unsupport httpVersion %d", pContext, pContext->fd, pParser->httpVersion); - httpOnError(pParser, 0, TSDB_CODE_HTTP_INVALID_VERSION); - } else { - httpTrace("context:%p, fd:%d, httpVersion:1.%d", pContext, pContext->fd, pParser->httpVersion); - } - - return 0; -} - -static int32_t httpOnStatusLine(HttpParser *pParser, int32_t code, const char *reason) { - HttpContext *pContext = pParser->pContext; - httpError("context:%p, fd:%d, status line, code:%d reason:%s", pContext, pContext->fd, code, reason); - return 0; -} - -static int32_t httpOnParseHeaderField(HttpParser *parser, const char *key, const char *val) { - HttpContext *pContext = parser->pContext; - httpTrace("context:%p, fd:%d, key:%s val:%s", pContext, pContext->fd, key, val); - - if (0 == strcasecmp(key, "Content-Length")) { - int32_t len = 0; - int32_t bytes = 0; - int32_t n = sscanf(val, "%d%n", &len, &bytes); - if (n == 1 && bytes == strlen(val)) { - parser->contentLength = len; - parser->chunkSize = len; - parser->contentLengthSpecified = 1; - httpTrace("context:%p, fd:%d, contentLength:%d chunkSize:%d contentLengthSpecified:%d", pContext, pContext->fd, - parser->contentLength, parser->chunkSize, parser->contentLengthSpecified); - return 0; - } else { - httpError("context:%p, fd:%d, failed to parser %s:%s", pContext, pContext->fd, key, val); - httpOnError(parser, 0, TSDB_CODE_HTTP_INVALID_CONTENT_LENGTH); - return -1; - } - } - - else if (0 == strcasecmp(key, "Accept-Encoding")) { - if (strstr(val, "gzip")) { - parser->acceptEncodingGzip = 1; - httpTrace("context:%p, fd:%d, acceptEncodingGzip:%d", pContext, pContext->fd, parser->acceptEncodingGzip); - } - if (strstr(val, "chunked")) { - parser->acceptEncodingChunked = 1; - httpTrace("context:%p, fd:%d, acceptEncodingChunked:%d", pContext, pContext->fd, parser->acceptEncodingChunked); - } - return 0; - } - - else if (strncasecmp(key, "Connection", 10) == 0) { - if (strncasecmp(val, "Keep-Alive", 10) == 0) { - parser->keepAlive = HTTP_KEEPALIVE_ENABLE; - } else { - parser->keepAlive = HTTP_KEEPALIVE_DISABLE; - } - httpTrace("context:%p, fd:%d, keepAlive:%d", pContext, pContext->fd, pContext->parser->keepAlive); - } -#if 0 - else if (0 == strcasecmp(key, "Content-Encoding")) { - if (0 == strcmp(val, "gzip")) { - parser->contentChunked = 1; - httpTrace("context:%p, fd:%d, contentChunked:%d", pContext, pContext->fd, parser->contentChunked); - } - return 0; - } -#endif - - else if (0 == strcasecmp(key, "Transfer-Encoding") || 0 == strcasecmp(key, "Content-Encoding")) { - if (strstr(val, "gzip")) { - parser->transferGzip = 1; - ehttp_gzip_conf_t conf = {0}; - ehttp_gzip_callbacks_t callbacks = {0}; - - callbacks.on_data = httpOnData; - - parser->gzip = ehttp_gzip_create_decompressor(conf, callbacks, parser); - - if (!parser->gzip) { - httpError("context:%p, fd:%d, failed to create gzip decompressor", pContext, pContext->fd); - httpOnError(parser, 0, TSDB_CODE_HTTP_CREATE_GZIP_FAILED); - return -1; - } - } - if (strstr(val, "chunked")) { - parser->transferChunked = 1; - httpTrace("context:%p, fd:%d, transferChunked:%d", pContext, pContext->fd, parser->transferChunked); - } - return 0; - } - - else if (0 == strcasecmp(key, "Authorization")) { - char t[6] = {0}; - char s[129] = {0}; - int32_t bytes = 0; - int32_t n = sscanf(val, "%5s %128s%n", t, s, &bytes); - if (n == 2 && t[0] && s[0] && bytes == strlen(val)) { - if (strcmp(t, "Basic") == 0) { - free(parser->authContent); - parser->authContent = strdup(s); - parser->authType = HTTP_BASIC_AUTH; - httpTrace("context:%p, fd:%d, basic auth:%s", pContext, pContext->fd, parser->authContent); - int32_t ok = httpParseBasicAuthToken(pContext, parser->authContent, (int32_t)strlen(parser->authContent)); - if (ok != 0) { - httpOnError(parser, 0, TSDB_CODE_HTTP_INVALID_BASIC_AUTH); - return -1; - } - return 0; - } else if (strcmp(t, "Taosd") == 0) { - free(parser->authContent); - parser->authContent = strdup(s); - parser->authType = HTTP_TAOSD_AUTH; - httpTrace("context:%p, fd:%d, taosd auth:%s", pContext, pContext->fd, parser->authContent); - int32_t ok = httpParseTaosdAuthToken(pContext, parser->authContent, (int32_t)strlen(parser->authContent)); - if (ok != 0) { - httpOnError(parser, 0, TSDB_CODE_HTTP_INVALID_TAOSD_AUTH); - return -1; - } - return 0; - } else { - parser->authType = HTTP_INVALID_AUTH; - httpError("context:%p, fd:%d, invalid auth, t:%s s:%s", pContext, pContext->fd, t, s); - httpOnError(parser, 0, TSDB_CODE_HTTP_INVALID_AUTH_TYPE); - return -1; - } - } else { - parser->authType = HTTP_INVALID_AUTH; - httpError("context:%p, fd:%d, parse auth failed, t:%s s:%s", pContext, pContext->fd, t, s); - httpOnError(parser, 0, TSDB_CODE_HTTP_INVALID_AUTH_FORMAT); - return -1; - } - } - - return 0; -} - -static int32_t httpOnBody(HttpParser *parser, const char *chunk, int32_t len) { - HttpContext *pContext = parser->pContext; - HttpString *buf = &parser->body; - if (parser->parseCode != TSDB_CODE_SUCCESS) return -1; - - if (buf->size <= 0) { - buf->size = MIN(len + 2, HTTP_BUFFER_SIZE); - buf->str = malloc(buf->size); - } - - int32_t newSize = buf->pos + len + 1; - char *newStr = NULL; - if (newSize >= buf->size) { - if (buf->size >= HTTP_BUFFER_SIZE) { - httpError("context:%p, fd:%d, failed parse body, exceeding buffer size %d", pContext, pContext->fd, buf->size); - httpOnError(parser, 0, TSDB_CODE_HTTP_REQUSET_TOO_BIG); - return -1; - } - - newSize = MAX(newSize, HTTP_BUFFER_INIT); - newSize *= 4; - newSize = MIN(newSize, HTTP_BUFFER_SIZE); - newStr = realloc(buf->str, newSize); - if (newStr == NULL && buf->str) { - free(buf->str); - } - - buf->str = newStr; - buf->size = newSize; - - if (buf->str == NULL) { - httpError("context:%p, fd:%d, failed parse body, realloc %d failed", pContext, pContext->fd, buf->size); - httpOnError(parser, 0, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); - return -1; - } - } - - memcpy(buf->str + buf->pos, chunk, len); - buf->pos += len; - buf->str[buf->pos] = 0; - - return 0; -} - -static int32_t httpOnEnd(HttpParser *parser) { - HttpContext *pContext = parser->pContext; - parser->parsed = true; - - if (parser->parseCode != TSDB_CODE_SUCCESS) { - return -1; - } - - httpTrace("context:%p, fd:%d, parse success", pContext, pContext->fd); - return 0; -} - -static HTTP_PARSER_STATE httpTopStack(HttpParser *parser) { - HttpStack *stack = &parser->stacks; - ASSERT(stack->pos >= 1); - - return stack->stacks[stack->pos - 1]; -} - -static int32_t httpPushStack(HttpParser *parser, HTTP_PARSER_STATE state) { - HttpStack *stack = &parser->stacks; - int8_t *newStacks = NULL; - if (stack->size == 0) { - stack->pos = 0; - stack->size = 32; - stack->stacks = malloc(stack->size * sizeof(int8_t)); - } else if (stack->pos + 1 > stack->size) { - stack->size *= 2; - - newStacks = realloc(stack->stacks, stack->size * sizeof(int8_t)); - if (newStacks == NULL && stack->stacks) { - free(stack->stacks); - } - - stack->stacks = newStacks; - } else { - } - - if (stack->stacks == NULL) return -1; - - stack->stacks[stack->pos] = state; - stack->pos++; - - return 0; -} - -static int32_t httpPopStack(HttpParser *parser) { - HttpStack *stack = &parser->stacks; - ASSERT(stack->pos >= 1); - stack->pos--; - return 0; -} - -static void httpClearStack(HttpStack *stack) { stack->pos = 0; } - -static int32_t httpCleanupStack(HttpStack *stack) { - free(stack->stacks); - memset(stack, 0, sizeof(HttpStack)); - - return 0; -} - -void httpInitParser(HttpParser *parser) { - HttpContext *pContext = parser->pContext; - httpTrace("context:%p, fd:%d, init parser", pContext, pContext->fd); - - parser->parsed = false; - parser->inited = 1; - parser->httpVersion = 0; - parser->acceptEncodingGzip = 0; - parser->acceptEncodingChunked = 0; - parser->contentLengthSpecified = 0; - parser->contentChunked = 0; - parser->transferGzip = 0; - parser->transferChunked = 0; - parser->keepAlive = 0; - parser->authType = 0; - parser->contentLength = 0; - parser->chunkSize = 0; - parser->receivedChunkSize = 0; - parser->receivedSize = 0; - parser->statusCode = 0; - parser->httpCode = 0; - parser->parseCode = 0; - - free(parser->method); parser->method = NULL; - free(parser->target); parser->target = NULL; - free(parser->version); parser->version = NULL; - free(parser->reasonPhrase); parser->reasonPhrase = NULL; - free(parser->key); parser->key = NULL; - free(parser->val); parser->val = NULL; - free(parser->authContent); parser->authContent = NULL; - - httpClearStack(&parser->stacks); - httpClearString(&parser->str); - httpClearString(&parser->body); - for (int32_t i = 0; i < HTTP_MAX_URL; ++i) { - httpClearString(&parser->path[i]); - } - - if (parser->gzip != NULL) { - ehttp_gzip_destroy(parser->gzip); - parser->gzip = NULL; - } - - httpPushStack(parser, HTTP_PARSER_BEGIN); -} - -HttpParser *httpCreateParser(HttpContext *pContext) { - HttpParser *parser = calloc(1, sizeof(HttpParser)); - if (!parser) return NULL; - httpTrace("context:%p, fd:%d, create parser", pContext, pContext->fd); - - parser->pContext = pContext; - return parser; -} - -void httpClearParser(HttpParser *parser) { - HttpContext *pContext = parser->pContext; - httpTrace("context:%p, fd:%d, clear parser", pContext, pContext->fd); - - pContext->parser->inited = 0; - pContext->parser->parsed = false; -} - -void httpDestroyParser(HttpParser *parser) { - if (!parser) return; - - HttpContext *pContext = parser->pContext; - httpTrace("context:%p, fd:%d, destroy parser", pContext, pContext->fd); - - free(parser->method); parser->method = NULL; - free(parser->target); parser->target = NULL; - free(parser->version); parser->version = NULL; - free(parser->reasonPhrase); parser->reasonPhrase = NULL; - free(parser->key); parser->key = NULL; - free(parser->val); parser->val = NULL; - free(parser->authContent); parser->authContent = NULL; - - httpCleanupStack(&parser->stacks); - httpCleanupString(&parser->str); - httpCleanupString(&parser->body); - for (int32_t i = 0; i < HTTP_MAX_URL; ++i) { - httpCleanupString(&parser->path[i]); - } - - if (parser->gzip != NULL) { - ehttp_gzip_destroy(parser->gzip); - parser->gzip = NULL; - } - - free(parser); -} - -#define is_token(c) (strchr("!#$%&'*+-.^_`|~", c) || isdigit(c) || isalpha(c)) - -char *httpDecodeUrl(const char *enc) { - int32_t ok = 1; - HttpString str = {0}; - while (*enc) { - char *p = strchr(enc, '%'); - if (!p) break; - int32_t hex, cnt; - int32_t n = sscanf(p + 1, "%2x%n", &hex, &cnt); - if (n != 1 && cnt != 2) { - ok = 0; - break; - } - if (httpAppendString(&str, enc, (int32_t)(p - enc))) { - ok = 0; - break; - } - char c = (char)hex; - if (httpAppendString(&str, &c, 1)) { - ok = 0; - break; - } - enc = p + 3; - } - char *dec = NULL; - if (ok && *enc) { - if (httpAppendString(&str, enc, (int32_t)strlen(enc))) { - ok = 0; - } - } - if (ok) { - dec = str.str; - str.str = NULL; - } - //httpCleanupString(&str); - return dec; -} - -static void httpOnData(ehttp_gzip_t *gzip, void *arg, const char *buf, int32_t len) { - HttpParser *parser = (HttpParser *)arg; - httpOnBody(parser, buf, len); -} - -static int32_t httpParserOnBegin(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (c == 'G' || c == 'P' || c == 'H' || c == 'D' || c == 'C' || c == 'O' || c == 'T') { - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_METHOD_FAILED); - break; - } - httpPopStack(parser); - httpPushStack(parser, HTTP_PARSER_REQUEST_OR_RESPONSE); - break; - } - httpError("context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_BAD_REQUEST, TSDB_CODE_HTTP_PARSE_METHOD_FAILED); - } while (0); - return ok; -} - -static int32_t httpParserOnRquestOrResponse(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (parser->str.pos == 1) { - if (c == 'T' && parser->str.str[0] == 'H') { - httpPopStack(parser); - httpPushStack(parser, HTTP_PARSER_END); - httpPushStack(parser, HTTP_PARSER_HEADER); - httpPushStack(parser, HTTP_PARSER_CRLF); - httpPushStack(parser, HTTP_PARSER_REASON_PHRASE); - httpPushStack(parser, HTTP_PARSER_SP); - httpPushStack(parser, HTTP_PARSER_STATUS_CODE); - httpPushStack(parser, HTTP_PARSER_SP); - httpPushStack(parser, HTTP_PARSER_HTTP_VERSION); - *again = 1; - break; - } - httpPopStack(parser); - httpPushStack(parser, HTTP_PARSER_END); - httpPushStack(parser, HTTP_PARSER_HEADER); - httpPushStack(parser, HTTP_PARSER_CRLF); - httpPushStack(parser, HTTP_PARSER_HTTP_VERSION); - httpPushStack(parser, HTTP_PARSER_SP); - httpPushStack(parser, HTTP_PARSER_TARGET); - httpPushStack(parser, HTTP_PARSER_SP); - httpPushStack(parser, HTTP_PARSER_METHOD); - *again = 1; - break; - } - - httpError("context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_BAD_REQUEST, TSDB_CODE_HTTP_PARSE_METHOD_FAILED); - } while (0); - return ok; -} - -static int32_t httpParserOnMethod(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (isalnum(c) || strchr("!#$%&'*+-.^_`|~", c)) { - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_METHOD_FAILED); - break; - } - break; - } - parser->method = strdup(parser->str.str); - if (!parser->method) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_METHOD_FAILED); - break; - } else { - httpTrace("context:%p, fd:%d, httpMethod:%s", pContext, pContext->fd, parser->method); - } - httpClearString(&parser->str); - httpPopStack(parser); - *again = 1; - } while (0); - return ok; -} - -static int32_t httpParserOnTarget(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (!isspace(c)) { - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_TARGET_FAILED); - break; - } - break; - } - parser->target = httpDecodeUrl(parser->str.str); - if (!parser->target) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_TARGET_FAILED); - break; - } - httpClearString(&parser->str); - httpPopStack(parser); - *again = 1; - } while (0); - return ok; -} - -static int32_t httpParserOnVersion(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - const char *prefix = "HTTP/1."; - int32_t len = (int32_t)strlen(prefix); - if (parser->str.pos < len) { - if (prefix[parser->str.pos] != c) { - httpError("context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_BAD_REQUEST, TSDB_CODE_HTTP_PARSE_VERSION_FAILED); - break; - } - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_VERSION_FAILED); - break; - } - break; - } - - if (c != '0' && c != '1' && c != '2') { - httpError("context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_BAD_REQUEST, TSDB_CODE_HTTP_PARSE_VERSION_FAILED); - break; - } - - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_VERSION_FAILED); - break; - } - - if (c == '0') - parser->httpVersion = HTTP_VERSION_10; - else if (c == '1') - parser->httpVersion = HTTP_VERSION_11; - else if (c == '2') - parser->httpVersion = HTTP_VERSION_12; - else { - } - - parser->version = strdup(parser->str.str); - if (!parser->version) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_VERSION_FAILED); - break; - } - - if (parser->method) { - ok = httpOnRequestLine(parser, parser->method, parser->target, parser->version); - /* - if (parser->target) { - free(parser->target); - parser->target = NULL; - } - */ - } - - httpClearString(&parser->str); - httpPopStack(parser); - } while (0); - return ok; -} - -static int32_t httpParserOnSp(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (c == ' ') { - httpPopStack(parser); - break; - } - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_SP_FAILED); - } while (0); - return ok; -} - -static int32_t httpParserOnOptionalSp(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - int32_t ok = 0; - if (c != ' ') { - *again = 1; - httpPopStack(parser); - } - return ok; -} - -static int32_t httpParserOnStatusCode(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (isdigit(c)) { - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_STATUS_FAILED); - break; - } - if (parser->str.pos < 3) break; - - sscanf(parser->str.str, "%d", &parser->statusCode); - httpClearString(&parser->str); - httpPopStack(parser); - break; - } - httpError("context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_BAD_REQUEST, TSDB_CODE_HTTP_PARSE_STATUS_FAILED); - } while (0); - return ok; -} - -static int32_t httpParserOnReasonPhrase(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (c == '\r') { - parser->reasonPhrase = strdup(parser->str.str); - if (!parser->reasonPhrase) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_PHRASE_FAILED); - break; - } - ok = httpOnStatusLine(parser, parser->statusCode, parser->reasonPhrase); - httpClearString(&parser->str); - httpPopStack(parser); - *again = 1; - break; - } - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_PHRASE_FAILED); - break; - } - } while (0); - return ok; -} - -static int32_t httpParserPostProcess(HttpParser *parser) { - HttpContext *pContext = parser->pContext; - if (parser->gzip) { - if (ehttp_gzip_finish(parser->gzip)) { - httpError("context:%p, fd:%d, gzip failed", pContext, pContext->fd); - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_FINISH_GZIP_FAILED); - return -1; - } - } - httpOnEnd(parser); - return 0; -} - -static int32_t httpParserOnCrlf(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - const char *s = "\r\n"; - int32_t len = (int32_t)strlen(s); - if (s[parser->str.pos] != c) { - httpError("context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_BAD_REQUEST, TSDB_CODE_HTTP_PARSE_CRLF_FAILED); - break; - } - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_CRLF_FAILED); - break; - } - if (parser->str.pos == len) { - httpClearString(&parser->str); - httpPopStack(parser); - if (httpTopStack(parser) == HTTP_PARSER_END) { - ok = httpParserPostProcess(parser); - } - } - break; - } while (0); - return ok; -} - -static int32_t httpParserOnHeader(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (c == '\r') { - httpPopStack(parser); - if (parser->transferChunked) { - httpPushStack(parser, HTTP_PARSER_CHUNK_SIZE); - httpPushStack(parser, HTTP_PARSER_CRLF); - } else { - if (parser->contentLength > 0) { - httpPushStack(parser, HTTP_PARSER_CHUNK); - } - httpPushStack(parser, HTTP_PARSER_CRLF); - } - *again = 1; - break; - } - if (c != ' ' && c != '\t' && c != ':') { - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_HEADER_FAILED); - break; - } - httpPushStack(parser, HTTP_PARSER_CRLF); - httpPushStack(parser, HTTP_PARSER_HEADER_VAL); - httpPushStack(parser, HTTP_PARSER_OPTIONAL_SP); - httpPushStack(parser, HTTP_PARSER_HEADER_KEY); - break; - } - httpError("context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_BAD_REQUEST, TSDB_CODE_HTTP_PARSE_HEADER_FAILED); - } while (0); - return ok; -} - -static int32_t httpParserOnHeaderKey(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (isalnum(c) || strchr("!#$%&'*+-.^_`|~", c)) { - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_HEADER_KEY_FAILED); - break; - } - break; - } - if (c == ':') { - parser->key = strdup(parser->str.str); - if (!parser->key) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_HEADER_KEY_FAILED); - break; - } - httpClearString(&parser->str); - httpPopStack(parser); - break; - } - httpError("context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_BAD_REQUEST, TSDB_CODE_HTTP_PARSE_HEADER_KEY_FAILED); - } while (0); - return ok; -} - -static int32_t httpParserOnHeaderVal(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (c != '\r' && c != '\n' && (!isspace(c) || parser->str.pos > 0)) { - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - parser->parseCode = TSDB_CODE_HTTP_PARSE_HEADER_VAL_FAILED; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_HEADER_VAL_FAILED); - break; - } - break; - } - const char *val = parser->str.str; - ok = httpOnParseHeaderField(parser, parser->key, val); - free(parser->key); - parser->key = NULL; - val = NULL; - if (ok == -1) break; - httpClearString(&parser->str); - httpPopStack(parser); - *again = 1; - } while (0); - return ok; -} - -static int32_t httpParserOnChunkSize(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - int32_t bytes; - int32_t len; - int32_t n; - do { - if (isxdigit(c)) { - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_CHUNK_SIZE_FAILED); - break; - } - break; - } - if (c == '\r') { - n = sscanf(parser->str.str, "%x%n", &len, &bytes); - if (n == 1 && bytes == strlen(parser->str.str) && len >= 0) { - if (len == 0) { - if (parser->contentLengthSpecified == 0 || parser->receivedSize == parser->contentLength) { - httpClearString(&parser->str); - httpPopStack(parser); - httpPushStack(parser, HTTP_PARSER_CRLF); - httpPushStack(parser, HTTP_PARSER_CRLF); - *again = 1; - break; - } - } else { - if (parser->contentLengthSpecified == 0 || parser->receivedSize + len <= parser->contentLength) { - parser->chunkSize = len; - httpClearString(&parser->str); - httpPopStack(parser); - httpPushStack(parser, HTTP_PARSER_CHUNK_SIZE); - httpPushStack(parser, HTTP_PARSER_CRLF); - httpPushStack(parser, HTTP_PARSER_CHUNK); - httpPushStack(parser, HTTP_PARSER_CRLF); - *again = 1; - break; - } - } - } - } - httpError("context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_BAD_REQUEST, TSDB_CODE_HTTP_PARSE_CHUNK_SIZE_FAILED); - } while (0); - return ok; -} - -static int32_t httpParserOnChunk(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - if (httpAppendString(&parser->str, &c, 1)) { - httpError("context:%p, fd:%d, parser state:%d, char:[%c]%02x, oom", pContext, pContext->fd, state, c, c); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_CHUNK_FAILED); - break; - } - ++parser->receivedSize; - ++parser->receivedChunkSize; - if (parser->receivedChunkSize < parser->chunkSize) break; - - if (parser->gzip) { - if (ehttp_gzip_write(parser->gzip, parser->str.str, parser->str.pos)) { - httpError("context:%p, fd:%d, gzip failed", pContext, pContext->fd); - ok = -1; - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_CHUNK_FAILED); - break; - } - } else { - httpOnBody(parser, parser->str.str, parser->str.pos); - } - parser->receivedChunkSize = 0; - httpClearString(&parser->str); - httpPopStack(parser); - if (httpTopStack(parser) == HTTP_PARSER_END) { - ok = httpParserPostProcess(parser); - } - } while (0); - return ok; -} - -static int32_t httpParserOnEnd(HttpParser *parser, HTTP_PARSER_STATE state, const char c, int32_t *again) { - HttpContext *pContext = parser->pContext; - int32_t ok = 0; - do { - ok = -1; - httpError("context:%p, fd:%d, parser state:%d, unexpected char:[%c]%02x", pContext, pContext->fd, state, c, c); - httpOnError(parser, HTTP_CODE_INSUFFICIENT_STORAGE, TSDB_CODE_HTTP_PARSE_END_FAILED); - } while (0); - return ok; -} - -static int32_t httpParseChar(HttpParser *parser, const char c, int32_t *again) { - HttpContext * pContext = parser->pContext; - int32_t ok = 0; - HTTP_PARSER_STATE state = httpTopStack(parser); - do { - if (state == HTTP_PARSER_BEGIN) { - ok = httpParserOnBegin(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_REQUEST_OR_RESPONSE) { - ok = httpParserOnRquestOrResponse(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_METHOD) { - ok = httpParserOnMethod(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_TARGET) { - ok = httpParserOnTarget(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_HTTP_VERSION) { - ok = httpParserOnVersion(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_SP) { - ok = httpParserOnSp(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_OPTIONAL_SP) { - ok = httpParserOnOptionalSp(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_STATUS_CODE) { - ok = httpParserOnStatusCode(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_REASON_PHRASE) { - ok = httpParserOnReasonPhrase(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_CRLF) { - ok = httpParserOnCrlf(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_HEADER) { - ok = httpParserOnHeader(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_HEADER_KEY) { - ok = httpParserOnHeaderKey(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_HEADER_VAL) { - ok = httpParserOnHeaderVal(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_CHUNK_SIZE) { - ok = httpParserOnChunkSize(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_CHUNK) { - ok = httpParserOnChunk(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_END) { - ok = httpParserOnEnd(parser, state, c, again); - break; - } - if (state == HTTP_PARSER_ERROR) { - ok = -2; - break; - } - - ok = -1; - httpError("context:%p, fd:%d, unknown parse state:%d", pContext, pContext->fd, state); - httpOnError(parser, HTTP_CODE_INTERNAL_SERVER_ERROR, TSDB_CODE_HTTP_PARSE_INVALID_STATE); - } while (0); - - if (ok == -1) { - httpError("context:%p, fd:%d, failed to parse, state:%d", pContext, pContext->fd, state); - httpPushStack(parser, HTTP_PARSER_ERROR); - } - - if (ok == -2) { - ok = -1; - httpError("context:%p, fd:%d, failed to parse, invalid state", pContext, pContext->fd); - httpOnError(parser, HTTP_CODE_INTERNAL_SERVER_ERROR, TSDB_CODE_HTTP_PARSE_ERROR_STATE); - } - - return ok; -} - -int32_t httpParseBuf(HttpParser *parser, const char *buf, int32_t len) { - HttpContext *pContext = parser->pContext; - const char * p = buf; - int32_t ret = 0; - int32_t i = 0; - - while (i < len) { - int32_t again = 0; - ret = httpParseChar(parser, *p, &again); - if (ret != 0) { - httpError("context:%p, fd:%d, parse failed, ret:%d i:%d len:%d buf:%s", pContext, pContext->fd, ret, i, len, buf); - break; - } - if (again) continue; - ++p; - ++i; - } - - return ret; -} diff --git a/src/plugins/http/src/httpQueue.c b/src/plugins/http/src/httpQueue.c deleted file mode 100644 index 917179078d..0000000000 --- a/src/plugins/http/src/httpQueue.c +++ /dev/null @@ -1,157 +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 "httpQueue.h" -#include "../../../../include/client/taos.h" -#include "httpAuth.h" -#include "httpContext.h" -#include "httpInt.h" -#include "httpResp.h" -#include "httpSession.h" -#include "httpSql.h" -#include "os.h" -#include "tnote.h" -#include "tqueue.h" -#include "tsclient.h" - -typedef struct { - pthread_t thread; - int32_t workerId; -} SHttpWorker; - -typedef struct { - int32_t num; - SHttpWorker *httpWorker; -} SHttpWorkerPool; - -typedef struct { - void * param; - void * result; - int32_t code; - int32_t rows; - FHttpResultFp fp; -} SHttpResult; - -static SHttpWorkerPool tsHttpPool; -static taos_qset tsHttpQset; -static taos_queue tsHttpQueue; - -void httpDispatchToResultQueue(void *param, TAOS_RES *result, int32_t code, int32_t rows, FHttpResultFp fp) { - if (tsHttpQueue != NULL) { - SHttpResult *pMsg = taosAllocateQitem(sizeof(SHttpResult)); - pMsg->param = param; - pMsg->result = result; - pMsg->code = code; - pMsg->rows = rows; - pMsg->fp = fp; - taosWriteQitem(tsHttpQueue, TAOS_QTYPE_RPC, pMsg); - } else { - taos_stop_query(result); - taos_free_result(result); - //(*fp)(param, result, code, rows); - } -} - -static void *httpProcessResultQueue(void *param) { - SHttpResult *pMsg; - int32_t type; - void * unUsed; - - setThreadName("httpResultQ"); - - while (1) { - if (taosReadQitemFromQset(tsHttpQset, &type, (void **)&pMsg, &unUsed) == 0) { - httpDebug("qset:%p, http queue got no message from qset, exiting", tsHttpQset); - break; - } - - httpTrace("context:%p, res:%p will be processed in result queue, code:%d rows:%d", pMsg->param, pMsg->result, - pMsg->code, pMsg->rows); - (*pMsg->fp)(pMsg->param, pMsg->result, pMsg->code, pMsg->rows); - taosFreeQitem(pMsg); - } - - return NULL; -} - -static bool httpAllocateResultQueue() { - tsHttpQueue = taosOpenQueue(); - if (tsHttpQueue == NULL) return false; - - taosAddIntoQset(tsHttpQset, tsHttpQueue, NULL); - - for (int32_t i = 0; i < tsHttpPool.num; ++i) { - SHttpWorker *pWorker = tsHttpPool.httpWorker + i; - pWorker->workerId = i; - - pthread_attr_t thAttr; - pthread_attr_init(&thAttr); - pthread_attr_setdetachstate(&thAttr, PTHREAD_CREATE_JOINABLE); - - if (pthread_create(&pWorker->thread, &thAttr, httpProcessResultQueue, pWorker) != 0) { - httpError("failed to create thread to process http result queue, reason:%s", strerror(errno)); - } - - pthread_attr_destroy(&thAttr); - httpDebug("http result worker:%d is launched, total:%d", pWorker->workerId, tsHttpPool.num); - } - - httpInfo("http result queue is opened"); - return true; -} - -static void httpFreeResultQueue() { - taosCloseQueue(tsHttpQueue); - tsHttpQueue = NULL; -} - -bool httpInitResultQueue() { - tsHttpQset = taosOpenQset(); - - tsHttpPool.num = tsHttpMaxThreads; - tsHttpPool.httpWorker = (SHttpWorker *)calloc(sizeof(SHttpWorker), tsHttpPool.num); - - if (tsHttpPool.httpWorker == NULL) return -1; - for (int32_t i = 0; i < tsHttpPool.num; ++i) { - SHttpWorker *pWorker = tsHttpPool.httpWorker + i; - pWorker->workerId = i; - } - - return httpAllocateResultQueue(); -} - -void httpCleanupResultQueue() { - httpFreeResultQueue(); - - for (int32_t i = 0; i < tsHttpPool.num; ++i) { - SHttpWorker *pWorker = tsHttpPool.httpWorker + i; - if (taosCheckPthreadValid(pWorker->thread)) { - taosQsetThreadResume(tsHttpQset); - } - } - - for (int32_t i = 0; i < tsHttpPool.num; ++i) { - SHttpWorker *pWorker = tsHttpPool.httpWorker + i; - if (taosCheckPthreadValid(pWorker->thread)) { - pthread_join(pWorker->thread, NULL); - } - } - - taosCloseQset(tsHttpQset); - free(tsHttpPool.httpWorker); - - httpInfo("http result queue is closed"); -} diff --git a/src/plugins/http/src/httpResp.c b/src/plugins/http/src/httpResp.c deleted file mode 100644 index 1d05b455cb..0000000000 --- a/src/plugins/http/src/httpResp.c +++ /dev/null @@ -1,211 +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 "taosmsg.h" -#include "httpLog.h" -#include "httpResp.h" -#include "httpJson.h" -#include "httpContext.h" - -const char *httpKeepAliveStr[] = {"", "Connection: Keep-Alive\r\n", "Connection: Close\r\n"}; - -const char *httpVersionStr[] = {"HTTP/1.0", "HTTP/1.1", "HTTP/1.2"}; - -const char *httpRespTemplate[] = { - // HTTP_RESPONSE_JSON_OK - // HTTP_RESPONSE_JSON_ERROR - "{\"status\":\"succ\",\"code\":%d,\"desc\":\"%s\"}", - "{\"status\":\"error\",\"code\":%d,\"desc\":\"%s\"}", - // HTTP_RESPONSE_OK - // HTTP_RESPONSE_ERROR - "%s 200 OK\r\nAccess-Control-Allow-Origin:*\r\n%sContent-Type: application/json;charset=utf-8\r\nContent-Length: %d\r\n\r\n", - "%s %d %s\r\nAccess-Control-Allow-Origin:*\r\n%sContent-Type: application/json;charset=utf-8\r\nContent-Length: %d\r\n\r\n", - // HTTP_RESPONSE_CHUNKED_UN_COMPRESS, HTTP_RESPONSE_CHUNKED_COMPRESS - "%s 200 OK\r\nAccess-Control-Allow-Origin:*\r\n%sContent-Type: application/json;charset=utf-8\r\nTransfer-Encoding: chunked\r\n\r\n", - "%s 200 OK\r\nAccess-Control-Allow-Origin:*\r\n%sContent-Type: application/json;charset=utf-8\r\nContent-Encoding: gzip\r\nTransfer-Encoding: chunked\r\n\r\n", - // HTTP_RESPONSE_OPTIONS - "%s 200 OK\r\nAccess-Control-Allow-Origin:*\r\n%sContent-Type: application/json;charset=utf-8\r\nContent-Length: %d\r\nAccess-Control-Allow-Methods: *\r\nAccess-Control-Max-Age: 3600\r\nAccess-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authorization\r\n\r\n", - // HTTP_RESPONSE_GRAFANA - "%s 200 OK\r\nAccess-Control-Allow-Origin:*\r\n%sAccess-Control-Allow-Methods:POST, GET, OPTIONS, DELETE, PUT\r\nAccess-Control-Allow-Headers:Accept, Content-Type\r\nContent-Type: application/json;charset=utf-8\r\nContent-Length: %d\r\n\r\n" -}; - -static void httpSendErrorRespImp(HttpContext *pContext, int32_t httpCode, char *httpCodeStr, int32_t errNo, const char *desc) { - httpError("context:%p, fd:%d, code:%d, error:%s", pContext, pContext->fd, httpCode, desc); - - char head[512] = {0}; - char body[512] = {0}; - - int8_t httpVersion = 0; - int8_t keepAlive = 0; - if (pContext->parser != NULL) { - httpVersion = pContext->parser->httpVersion; - keepAlive = pContext->parser->keepAlive; - } - - int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_ERROR], errNo, desc); - int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_ERROR], httpVersionStr[httpVersion], httpCode, - httpCodeStr, httpKeepAliveStr[keepAlive], bodyLen); - - httpWriteBuf(pContext, head, headLen); - httpWriteBuf(pContext, body, bodyLen); - httpCloseContextByApp(pContext); -} - -void httpSendErrorResp(HttpContext *pContext, int32_t errNo) { - int32_t httpCode = HTTP_CODE_INTERNAL_SERVER_ERROR; - if (errNo == TSDB_CODE_SUCCESS) - httpCode = HTTP_CODE_OK; - else if (errNo == TSDB_CODE_HTTP_SERVER_OFFLINE) - httpCode = HTTP_CODE_NOT_FOUND; - else if (errNo == TSDB_CODE_HTTP_UNSUPPORT_URL) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_INVALID_URL) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_NO_ENOUGH_MEMORY) - httpCode = HTTP_CODE_INSUFFICIENT_STORAGE; - else if (errNo == TSDB_CODE_HTTP_REQUSET_TOO_BIG) - httpCode = HTTP_CODE_PAYLOAD_TOO_LARGE; - else if (errNo == TSDB_CODE_HTTP_NO_AUTH_INFO) - httpCode = HTTP_CODE_UNAUTHORIZED; - else if (errNo == TSDB_CODE_HTTP_NO_MSG_INPUT) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_NO_SQL_INPUT) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_NO_EXEC_USEDB) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_SESSION_FULL) - httpCode = HTTP_CODE_INTERNAL_SERVER_ERROR; - else if (errNo == TSDB_CODE_HTTP_GEN_TAOSD_TOKEN_ERR) - httpCode = HTTP_CODE_INTERNAL_SERVER_ERROR; - else if (errNo == TSDB_CODE_HTTP_INVALID_MULTI_REQUEST) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_CREATE_GZIP_FAILED) - httpCode = HTTP_CODE_INTERNAL_SERVER_ERROR; - else if (errNo == TSDB_CODE_HTTP_FINISH_GZIP_FAILED) - httpCode = HTTP_CODE_INTERNAL_SERVER_ERROR; - else if (errNo == TSDB_CODE_HTTP_INVALID_VERSION) - httpCode = HTTP_CODE_HTTP_VER_NOT_SUPPORTED; - else if (errNo == TSDB_CODE_HTTP_INVALID_CONTENT_LENGTH) - httpCode = HTTP_CODE_LENGTH_REQUIRED; - else if (errNo == TSDB_CODE_HTTP_INVALID_AUTH_TYPE) - httpCode = HTTP_CODE_UNAUTHORIZED; - else if (errNo == TSDB_CODE_HTTP_INVALID_AUTH_FORMAT) - httpCode = HTTP_CODE_UNAUTHORIZED; - else if (errNo == TSDB_CODE_HTTP_INVALID_BASIC_AUTH) - httpCode = HTTP_CODE_UNAUTHORIZED; - else if (errNo == TSDB_CODE_HTTP_INVALID_TAOSD_AUTH) - httpCode = HTTP_CODE_UNAUTHORIZED; - else if (errNo == TSDB_CODE_HTTP_PARSE_METHOD_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_TARGET_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_VERSION_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_SP_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_STATUS_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_PHRASE_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_CRLF_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_HEADER_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_HEADER_KEY_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_HEADER_VAL_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_CHUNK_SIZE_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_CHUNK_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_END_FAILED) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_INVALID_STATE) - httpCode = HTTP_CODE_BAD_REQUEST; - else if (errNo == TSDB_CODE_HTTP_PARSE_ERROR_STATE) - httpCode = HTTP_CODE_BAD_REQUEST; - else - httpCode = HTTP_CODE_BAD_REQUEST; - - if (pContext->parser && pContext->parser->httpCode != 0) { - httpCode = pContext->parser->httpCode; - } - - pContext->error = true; - - char *httpCodeStr = httpGetStatusDesc(httpCode); - httpSendErrorRespImp(pContext, httpCode, httpCodeStr, errNo & 0XFFFF, tstrerror(errNo)); -} - -void httpSendTaosdInvalidSqlErrorResp(HttpContext *pContext, char *errMsg) { - int32_t httpCode = HTTP_CODE_BAD_REQUEST; - char temp[512] = {0}; - int32_t len = sprintf(temp, "invalid SQL: %s", errMsg); - - for (int32_t i = 0; i < len; ++i) { - if (temp[i] == '\"') { - temp[i] = '\''; - } else if (temp[i] == '\n') { - temp[i] = ' '; - } else { - } - } - - httpSendErrorRespImp(pContext, httpCode, "Bad Request", TSDB_CODE_TSC_INVALID_OPERATION & 0XFFFF, temp); -} - -void httpSendSuccResp(HttpContext *pContext, char *desc) { - char head[1024] = {0}; - char body[1024] = {0}; - - int8_t httpVersion = 0; - int8_t keepAlive = 0; - if (pContext->parser != NULL) { - httpVersion = pContext->parser->httpVersion; - keepAlive = pContext->parser->keepAlive; - } - - int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_OK], TSDB_CODE_SUCCESS, desc); - int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_OK], httpVersionStr[httpVersion], - httpKeepAliveStr[keepAlive], bodyLen); - - httpWriteBuf(pContext, head, headLen); - httpWriteBuf(pContext, body, bodyLen); - httpCloseContextByApp(pContext); -} - -void httpSendOptionResp(HttpContext *pContext, char *desc) { - char head[1024] = {0}; - char body[1024] = {0}; - - int8_t httpVersion = 0; - int8_t keepAlive = 0; - if (pContext->parser != NULL) { - httpVersion = pContext->parser->httpVersion; - keepAlive = pContext->parser->keepAlive; - } - - int32_t bodyLen = sprintf(body, httpRespTemplate[HTTP_RESPONSE_JSON_OK], TSDB_CODE_SUCCESS, desc); - int32_t headLen = sprintf(head, httpRespTemplate[HTTP_RESPONSE_OPTIONS], httpVersionStr[httpVersion], - httpKeepAliveStr[keepAlive], bodyLen); - - httpWriteBuf(pContext, head, headLen); - httpWriteBuf(pContext, body, bodyLen); - httpCloseContextByApp(pContext); -} diff --git a/src/plugins/http/src/httpRestHandle.c b/src/plugins/http/src/httpRestHandle.c deleted file mode 100644 index 24e4f90244..0000000000 --- a/src/plugins/http/src/httpRestHandle.c +++ /dev/null @@ -1,251 +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 "httpLog.h" -#include "httpRestHandle.h" -#include "httpRestJson.h" -#include "tglobal.h" - -static HttpDecodeMethod restDecodeMethod = {"rest", restProcessRequest}; -static HttpDecodeMethod restDecodeMethod2 = {"restful", restProcessRequest}; -static HttpEncodeMethod restEncodeSqlTimestampMethod = { - .startJsonFp = restStartSqlJson, - .stopJsonFp = restStopSqlJson, - .buildQueryJsonFp = restBuildSqlTimestampJson, - .buildAffectRowJsonFp = restBuildSqlAffectRowsJson, - .initJsonFp = NULL, - .cleanJsonFp = NULL, - .checkFinishedFp = NULL, - .setNextCmdFp = NULL -}; - -static HttpEncodeMethod restEncodeSqlLocalTimeStringMethod = { - .startJsonFp = restStartSqlJson, - .stopJsonFp = restStopSqlJson, - .buildQueryJsonFp = restBuildSqlLocalTimeStringJson, - .buildAffectRowJsonFp = restBuildSqlAffectRowsJson, - .initJsonFp = NULL, - .cleanJsonFp = NULL, - .checkFinishedFp = NULL, - .setNextCmdFp = NULL -}; - -static HttpEncodeMethod restEncodeSqlUtcTimeStringMethod = { - .startJsonFp = restStartSqlJson, - .stopJsonFp = restStopSqlJson, - .buildQueryJsonFp = restBuildSqlUtcTimeStringJson, - .buildAffectRowJsonFp = restBuildSqlAffectRowsJson, - .initJsonFp = NULL, - .cleanJsonFp = NULL, - .checkFinishedFp = NULL, - .setNextCmdFp = NULL -}; - -void restInitHandle(HttpServer* pServer) { - httpAddMethod(pServer, &restDecodeMethod); - httpAddMethod(pServer, &restDecodeMethod2); -} - -bool restGetUserFromUrl(HttpContext* pContext) { - HttpParser* pParser = pContext->parser; - if (pParser->path[REST_USER_USEDB_URL_POS].pos >= TSDB_USER_LEN || pParser->path[REST_USER_USEDB_URL_POS].pos <= 0) { - return false; - } - - tstrncpy(pContext->user, pParser->path[REST_USER_USEDB_URL_POS].str, TSDB_USER_LEN); - return true; -} - -bool restGetPassFromUrl(HttpContext* pContext) { - HttpParser* pParser = pContext->parser; - if (pParser->path[REST_PASS_URL_POS].pos >= HTTP_PASSWORD_LEN || pParser->path[REST_PASS_URL_POS].pos <= 0) { - return false; - } - - tstrncpy(pContext->pass, pParser->path[REST_PASS_URL_POS].str, HTTP_PASSWORD_LEN); - return true; -} - -bool restProcessLoginRequest(HttpContext* pContext) { - httpDebug("context:%p, fd:%d, user:%s, process restful login msg", pContext, pContext->fd, pContext->user); - pContext->reqType = HTTP_REQTYPE_LOGIN; - return true; -} - -bool restProcessSqlRequest(HttpContext* pContext, int32_t timestampFmt) { - httpDebug("context:%p, fd:%d, user:%s, process restful sql msg", pContext, pContext->fd, pContext->user); - - char* sql = pContext->parser->body.str; - if (sql == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_SQL_INPUT); - return false; - } - - /* - * for async test - * - if (httpCheckUsedbSql(sql)) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_EXEC_USEDB); - return false; - } - */ - - HttpSqlCmd* cmd = &(pContext->singleCmd); - cmd->nativSql = sql; - - /* find if there is db_name in url */ - pContext->db[0] = '\0'; - - HttpString *path = &pContext->parser->path[REST_USER_USEDB_URL_POS]; - if (tsHttpDbNameMandatory) { - if (path->pos == 0) { - httpError("context:%p, fd:%d, user:%s, database name is mandatory", pContext, pContext->fd, pContext->user); - httpSendErrorResp(pContext, TSDB_CODE_HTTP_INVALID_URL); - return false; - } - } - - if (path->pos > 0 && !(strlen(sql) > 4 && (sql[0] == 'u' || sql[0] == 'U') && - (sql[1] == 's' || sql[1] == 'S') && (sql[2] == 'e' || sql[2] == 'E') && sql[3] == ' ')) - { - snprintf(pContext->db, /*TSDB_ACCT_ID_LEN + */TSDB_DB_NAME_LEN, "%s", path->str); - } - - pContext->reqType = HTTP_REQTYPE_SINGLE_SQL; - if (timestampFmt == REST_TIMESTAMP_FMT_LOCAL_STRING) { - pContext->encodeMethod = &restEncodeSqlLocalTimeStringMethod; - } else if (timestampFmt == REST_TIMESTAMP_FMT_TIMESTAMP) { - pContext->encodeMethod = &restEncodeSqlTimestampMethod; - } else if (timestampFmt == REST_TIMESTAMP_FMT_UTC_STRING) { - pContext->encodeMethod = &restEncodeSqlUtcTimeStringMethod; - } - - return true; -} - -#define REST_FUNC_URL_POS 2 -#define REST_OUTP_URL_POS 3 -#define REST_AGGR_URL_POS 4 -#define REST_BUFF_URL_POS 5 - -#define HTTP_FUNC_LEN 32 -#define HTTP_OUTP_LEN 16 -#define HTTP_AGGR_LEN 2 -#define HTTP_BUFF_LEN 32 - -static int udfSaveFile(const char *fname, const char *content, long len) { - int fd = open(fname, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0755); - if (fd < 0) - return -1; - if (taosWrite(fd, (void *)content, len) < 0) - return -1; - - return 0; -} - -static bool restProcessUdfRequest(HttpContext* pContext) { - HttpParser* pParser = pContext->parser; - if (pParser->path[REST_FUNC_URL_POS].pos >= HTTP_FUNC_LEN || pParser->path[REST_FUNC_URL_POS].pos <= 0) { - return false; - } - - if (pParser->path[REST_OUTP_URL_POS].pos >= HTTP_OUTP_LEN || pParser->path[REST_OUTP_URL_POS].pos <= 0) { - return false; - } - - if (pParser->path[REST_AGGR_URL_POS].pos >= HTTP_AGGR_LEN || pParser->path[REST_AGGR_URL_POS].pos <= 0) { - return false; - } - - if (pParser->path[REST_BUFF_URL_POS].pos >= HTTP_BUFF_LEN || pParser->path[REST_BUFF_URL_POS].pos <= 0) { - return false; - } - - char* sql = pContext->parser->body.str; - int len = pContext->parser->body.pos; - if (sql == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_SQL_INPUT); - return false; - } - - char udfDir[256] = {0}; - char buf[64] = "udf-"; - char funcName[64] = {0}; - int aggr = 0; - char outputType[16] = {0}; - char buffSize[32] = {0}; - - tstrncpy(funcName, pParser->path[REST_FUNC_URL_POS].str, HTTP_FUNC_LEN); - tstrncpy(buf + 4, funcName, HTTP_FUNC_LEN); - - if (pParser->path[REST_AGGR_URL_POS].str[0] != '0') { - aggr = 1; - } - - tstrncpy(outputType, pParser->path[REST_OUTP_URL_POS].str, HTTP_OUTP_LEN); - tstrncpy(buffSize, pParser->path[REST_BUFF_URL_POS].str, HTTP_BUFF_LEN); - - taosGetTmpfilePath(funcName, udfDir); - - udfSaveFile(udfDir, sql, len); - - tfree(sql); - pContext->parser->body.str = malloc(1024); - sql = pContext->parser->body.str; - int sqlLen = sprintf(sql, "create %s function %s as \"%s\" outputtype %s bufsize %s", - aggr == 1 ? "aggregate" : " ", funcName, udfDir, outputType, buffSize); - - pContext->parser->body.pos = sqlLen; - pContext->parser->body.size = sqlLen + 1; - - HttpSqlCmd* cmd = &(pContext->singleCmd); - cmd->nativSql = sql; - - pContext->reqType = HTTP_REQTYPE_SINGLE_SQL; - pContext->encodeMethod = &restEncodeSqlLocalTimeStringMethod; - - return true; -} - -bool restProcessRequest(struct HttpContext* pContext) { - if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "login")) { - restGetUserFromUrl(pContext); - restGetPassFromUrl(pContext); - } - - if (strlen(pContext->user) == 0 || strlen(pContext->pass) == 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_AUTH_INFO); - return false; - } - - if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "sql")) { - return restProcessSqlRequest(pContext, REST_TIMESTAMP_FMT_LOCAL_STRING); - } else if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "sqlt")) { - return restProcessSqlRequest(pContext, REST_TIMESTAMP_FMT_TIMESTAMP); - } else if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "sqlutc")) { - return restProcessSqlRequest(pContext, REST_TIMESTAMP_FMT_UTC_STRING); - } else if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "login")) { - return restProcessLoginRequest(pContext); - } else if (httpUrlMatch(pContext, REST_ACTION_URL_POS, "udf")) { - return restProcessUdfRequest(pContext); - } else { - } - - httpSendErrorResp(pContext, TSDB_CODE_HTTP_INVALID_URL); - return false; -} diff --git a/src/plugins/http/src/httpRestJson.c b/src/plugins/http/src/httpRestJson.c deleted file mode 100644 index 13596b0e8a..0000000000 --- a/src/plugins/http/src/httpRestJson.c +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tglobal.h" -#include "tsclient.h" -#include "httpLog.h" -#include "httpJson.h" -#include "httpRestHandle.h" -#include "httpRestJson.h" - -void restBuildSqlAffectRowsJson(HttpContext *pContext, HttpSqlCmd *cmd, int32_t affect_rows) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - // data row array begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); - - httpJsonItemToken(jsonBuf); - httpJsonInt(jsonBuf, affect_rows); - - // data row array end - httpJsonToken(jsonBuf, JsonArrEnd); - - cmd->numOfRows = 1; -} - -void restStartSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - TAOS_FIELD *fields = taos_fetch_fields(result); - int32_t num_fields = taos_num_fields(result); - - httpInitJsonBuf(jsonBuf, pContext); - httpWriteJsonBufHead(jsonBuf); - - // object begin - httpJsonToken(jsonBuf, JsonObjStt); - - // status, and data - httpJsonItemToken(jsonBuf); - httpJsonPair(jsonBuf, REST_JSON_STATUS, REST_JSON_STATUS_LEN, REST_JSON_SUCCESS, REST_JSON_SUCCESS_LEN); - - // head begin - httpJsonItemToken(jsonBuf); - httpJsonPairHead(jsonBuf, REST_JSON_HEAD, REST_JSON_HEAD_LEN); - // head array begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); - - SSqlObj *pObj = (SSqlObj *) result; - bool isAlterSql = (pObj->sqlstr == NULL) ? false : httpCheckAlterSql(pObj->sqlstr); - - if (num_fields == 0) { - httpJsonItemToken(jsonBuf); - httpJsonString(jsonBuf, REST_JSON_AFFECT_ROWS, REST_JSON_AFFECT_ROWS_LEN); - } else { - if (isAlterSql == true) { - httpJsonItemToken(jsonBuf); - httpJsonString(jsonBuf, REST_JSON_AFFECT_ROWS, REST_JSON_AFFECT_ROWS_LEN); - } else { - for (int32_t i = 0; i < num_fields; ++i) { - httpJsonItemToken(jsonBuf); - httpJsonString(jsonBuf, fields[i].name, (int32_t)strlen(fields[i].name)); - } - } - } - - // head array end - httpJsonToken(jsonBuf, JsonArrEnd); - - // column_meta begin - httpJsonItemToken(jsonBuf); - httpJsonPairHead(jsonBuf, REST_JSON_HEAD_INFO, REST_JSON_HEAD_INFO_LEN); - // column_meta array begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); - - if (num_fields == 0) { - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); - - httpJsonItemToken(jsonBuf); - httpJsonString(jsonBuf, REST_JSON_AFFECT_ROWS, REST_JSON_AFFECT_ROWS_LEN); - httpJsonItemToken(jsonBuf); - httpJsonInt(jsonBuf, TSDB_DATA_TYPE_INT); - httpJsonItemToken(jsonBuf); - httpJsonInt(jsonBuf, 4); - - httpJsonToken(jsonBuf, JsonArrEnd); - } else { - for (int32_t i = 0; i < num_fields; ++i) { - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); - - if (isAlterSql == true) { - httpJsonItemToken(jsonBuf); - httpJsonString(jsonBuf, REST_JSON_AFFECT_ROWS, REST_JSON_AFFECT_ROWS_LEN); - } else { - httpJsonItemToken(jsonBuf); - httpJsonString(jsonBuf, fields[i].name, (int32_t)strlen(fields[i].name)); - } - - httpJsonItemToken(jsonBuf); - httpJsonInt(jsonBuf, fields[i].type); - httpJsonItemToken(jsonBuf); - httpJsonInt(jsonBuf, fields[i].bytes); - - httpJsonToken(jsonBuf, JsonArrEnd); - } - } - - // column_meta array end - httpJsonToken(jsonBuf, JsonArrEnd); - - // data begin - httpJsonItemToken(jsonBuf); - httpJsonPairHead(jsonBuf, REST_JSON_DATA, REST_JSON_DATA_LEN); - // data array begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); -} - -bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows, - int32_t timestampFormat) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return false; - - int32_t num_fields = taos_num_fields(result); - TAOS_FIELD *fields = taos_fetch_fields(result); - - for (int32_t k = 0; k < numOfRows; ++k) { - TAOS_ROW row = taos_fetch_row(result); - if (row == NULL) { - continue; - } - int32_t *length = taos_fetch_lengths(result); - - // data row array begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); - - for (int32_t i = 0; i < num_fields; i++) { - httpJsonItemToken(jsonBuf); - - if (row[i] == NULL) { - httpJsonOriginString(jsonBuf, "null", 4); - continue; - } - - switch (fields[i].type) { - case TSDB_DATA_TYPE_BOOL: - case TSDB_DATA_TYPE_TINYINT: - httpJsonInt(jsonBuf, *((int8_t *)row[i])); - break; - case TSDB_DATA_TYPE_SMALLINT: - httpJsonInt(jsonBuf, *((int16_t *)row[i])); - break; - case TSDB_DATA_TYPE_INT: - httpJsonInt(jsonBuf, *((int32_t *)row[i])); - break; - case TSDB_DATA_TYPE_BIGINT: - httpJsonInt64(jsonBuf, *((int64_t *)row[i])); - break; - case TSDB_DATA_TYPE_UTINYINT: - httpJsonUInt(jsonBuf, *((uint8_t *)row[i])); - break; - case TSDB_DATA_TYPE_USMALLINT: - httpJsonUInt(jsonBuf, *((uint16_t *)row[i])); - break; - case TSDB_DATA_TYPE_UINT: - httpJsonUInt(jsonBuf, *((uint32_t *)row[i])); - break; - case TSDB_DATA_TYPE_UBIGINT: - httpJsonUInt64(jsonBuf, *((uint64_t *)row[i])); - break; - case TSDB_DATA_TYPE_FLOAT: - httpJsonFloat(jsonBuf, GET_FLOAT_VAL(row[i])); - break; - case TSDB_DATA_TYPE_DOUBLE: - httpJsonDouble(jsonBuf, GET_DOUBLE_VAL(row[i])); - break; - case TSDB_DATA_TYPE_BINARY: - case TSDB_DATA_TYPE_NCHAR: - httpJsonStringForTransMean(jsonBuf, (char *)row[i], length[i]); - break; - case TSDB_DATA_TYPE_TIMESTAMP: - if (timestampFormat == REST_TIMESTAMP_FMT_LOCAL_STRING) { - httpJsonTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result)); - } else if (timestampFormat == REST_TIMESTAMP_FMT_TIMESTAMP) { - httpJsonInt64(jsonBuf, *((int64_t *)row[i])); - } else { - httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result)); - } - break; - default: - break; - } - } - - // data row array end - httpJsonToken(jsonBuf, JsonArrEnd); - cmd->numOfRows++; - - if (pContext->fd <= 0) { - httpError("context:%p, fd:%d, user:%s, conn closed, abort retrieve", pContext, pContext->fd, pContext->user); - return false; - } - - if (cmd->numOfRows >= tsRestRowLimit) { - httpDebug("context:%p, fd:%d, user:%s, retrieve rows:%d larger than limit:%d, abort retrieve", pContext, - pContext->fd, pContext->user, cmd->numOfRows, tsRestRowLimit); - return false; - } - } - - httpDebug("context:%p, fd:%d, user:%s, retrieved row:%d", pContext, pContext->fd, pContext->user, cmd->numOfRows); - return true; -} - -bool restBuildSqlTimestampJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows) { - return restBuildSqlJson(pContext, cmd, result, numOfRows, REST_TIMESTAMP_FMT_TIMESTAMP); -} - -bool restBuildSqlLocalTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows) { - return restBuildSqlJson(pContext, cmd, result, numOfRows, REST_TIMESTAMP_FMT_LOCAL_STRING); -} - -bool restBuildSqlUtcTimeStringJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, int32_t numOfRows) { - return restBuildSqlJson(pContext, cmd, result, numOfRows, REST_TIMESTAMP_FMT_UTC_STRING); -} - -void restStopSqlJson(HttpContext *pContext, HttpSqlCmd *cmd) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - // data array end - httpJsonToken(jsonBuf, JsonArrEnd); - - // rows - httpJsonItemToken(jsonBuf); - httpJsonPairHead(jsonBuf, REST_JSON_ROWS, REST_JSON_ROWS_LEN); - httpJsonInt64(jsonBuf, cmd->numOfRows); - - // object end - httpJsonToken(jsonBuf, JsonObjEnd); - - httpWriteJsonBufEnd(jsonBuf); -} diff --git a/src/plugins/http/src/httpServer.c b/src/plugins/http/src/httpServer.c deleted file mode 100644 index 13a0835c39..0000000000 --- a/src/plugins/http/src/httpServer.c +++ /dev/null @@ -1,417 +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 "taosmsg.h" -#include "tsocket.h" -#include "tutil.h" -#include "ttimer.h" -#include "tglobal.h" -#include "httpInt.h" -#include "httpContext.h" -#include "httpResp.h" -#include "httpUtil.h" - -static bool httpReadData(HttpContext *pContext); - -#ifdef __APPLE__ -static int sv_dummy = 0; -#endif - -static void httpStopThread(HttpThread *pThread) { - pThread->stop = true; - - // signal the thread to stop, try graceful method first, - // and use pthread_cancel when failed -#ifdef __APPLE__ - int sv[2]; - sv[0] = sv[1] = -1; - int r = socketpair(PF_LOCAL, SOCK_STREAM, 0, sv); - do { - if (r) break; - struct epoll_event ev = {0}; - ev.events = EPOLLIN; - ev.data.ptr = &sv_dummy; - pThread->stop = true; - r = epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, sv[0], &ev); - if (r) break; - if (1 != send(sv[1], "1", 1, 0)) { - r = -1; - break; - } - } while (0); - if (r) { - pthread_cancel(pThread->thread); - } -#else - struct epoll_event event = {.events = EPOLLIN}; - eventfd_t fd = eventfd(1, 0); - if (fd == -1) { - httpError("%s, failed to create eventfd, will call pthread_cancel instead, which may result in data corruption: %s", - pThread->label, strerror(errno)); - pThread->stop = true; - pthread_cancel(pThread->thread); - } else if (epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, fd, &event) < 0) { - httpError("%s, failed to call epoll_ctl, will call pthread_cancel instead, which may result in data corruption: %s", - pThread->label, strerror(errno)); - pthread_cancel(pThread->thread); - } -#endif // __APPLE__ - - pthread_join(pThread->thread, NULL); - -#ifdef __APPLE__ - if (sv[0] != -1) { - close(sv[0]); - sv[0] = -1; - } - if (sv[1] != -1) { - close(sv[1]); - sv[1] = -1; - } -#else // __APPLE__ - if (fd != -1) { - taosCloseSocket(fd); - } -#endif // __APPLE__ - - EpollClose(pThread->pollFd); - pthread_mutex_destroy(&(pThread->threadMutex)); -} - -void httpCleanUpConnect() { - HttpServer *pServer = &tsHttpServer; - if (pServer->pThreads == NULL) return; - - if (taosCheckPthreadValid(pServer->thread)) { - pthread_join(pServer->thread, NULL); - } - - for (int32_t i = 0; i < pServer->numOfThreads; ++i) { - HttpThread *pThread = pServer->pThreads + i; - if (pThread != NULL) { - httpStopThread(pThread); - } - } - - httpDebug("http server:%s is cleaned up", pServer->label); -} - -static void httpProcessHttpData(void *param) { - HttpServer * pServer = &tsHttpServer; - HttpThread * pThread = (HttpThread *)param; - HttpContext *pContext; - int32_t fdNum; - - taosSetMaskSIGPIPE(); - setThreadName("httpData"); - - while (1) { - struct epoll_event events[HTTP_MAX_EVENTS]; - //-1 means uncertainty, 0-nowait, 1-wait 1 ms, set it from -1 to 1 - fdNum = epoll_wait(pThread->pollFd, events, HTTP_MAX_EVENTS, TAOS_EPOLL_WAIT_TIME); - if (pThread->stop) { - httpDebug("%p, http thread get stop event, exiting...", pThread); - break; - } - if (fdNum <= 0) continue; - - for (int32_t i = 0; i < fdNum; ++i) { -#ifdef __APPLE__ - if (events[i].data.ptr == &sv_dummy) { - // no need to drain the recv buffer of sv[0] - // since there's only one time to send at most 1 byte to sv[0] - // btw, pThread->stop shall be already set, thus never reached here - httpDebug("if you see this line, there's internal logic error"); - continue; - } -#endif // __APPLE__ - pContext = httpGetContext(events[i].data.ptr); - if (pContext == NULL) { - httpError("context:%p, is already released, close connect", events[i].data.ptr); - // epoll_ctl(pThread->pollFd, EPOLL_CTL_DEL, events[i].data.fd, NULL); - // taosClose(events[i].data.fd); - continue; - } - - if (events[i].events & EPOLLPRI) { - httpDebug("context:%p, fd:%d, state:%s, EPOLLPRI events occured, accessed:%d, close connect", pContext, - pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); - httpCloseContextByServer(pContext); - continue; - } - - if (events[i].events & EPOLLRDHUP) { - httpDebug("context:%p, fd:%d, state:%s, EPOLLRDHUP events occured, accessed:%d, close connect", pContext, - pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); - httpCloseContextByServer(pContext); - continue; - } - - if (events[i].events & EPOLLERR) { - httpDebug("context:%p, fd:%d, state:%s, EPOLLERR events occured, accessed:%d, close connect", pContext, - pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); - httpCloseContextByServer(pContext); - continue; - } - - if (events[i].events & EPOLLHUP) { - httpDebug("context:%p, fd:%d, state:%s, EPOLLHUP events occured, accessed:%d, close connect", pContext, - pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); - httpCloseContextByServer(pContext); - continue; - } - - if (!httpAlterContextState(pContext, HTTP_CONTEXT_STATE_READY, HTTP_CONTEXT_STATE_READY)) { - httpDebug("context:%p, fd:%d, state:%s, not in ready state, ignore read events", pContext, pContext->fd, - httpContextStateStr(pContext->state)); - httpReleaseContext(pContext/*, true*/); - continue; - } - - if (pServer->status != HTTP_SERVER_RUNNING) { - httpDebug("context:%p, fd:%d, state:%s, server is not running, accessed:%d, close connect", pContext, - pContext->fd, httpContextStateStr(pContext->state), pContext->accessTimes); - httpSendErrorResp(pContext, TSDB_CODE_HTTP_SERVER_OFFLINE); - httpNotifyContextClose(pContext); - } else { - if (httpReadData(pContext)) { - (*(pThread->processData))(pContext); - atomic_fetch_add_32(&pServer->requestNum, 1); - } - } - } - } -} - -static void *httpAcceptHttpConnection(void *arg) { - SOCKET connFd = -1; - struct sockaddr_in clientAddr; - int32_t threadId = 0; - HttpServer * pServer = &tsHttpServer; - HttpThread * pThread = NULL; - HttpContext * pContext = NULL; - int32_t totalFds = 0; - - taosSetMaskSIGPIPE(); - setThreadName("httpAcceptConn"); - - pServer->fd = taosOpenTcpServerSocket(pServer->serverIp, pServer->serverPort); - - if (pServer->fd < 0) { - httpError("http server:%s, failed to open http socket, ip:%s:%u error:%s", pServer->label, - taosIpStr(pServer->serverIp), pServer->serverPort, strerror(errno)); - return NULL; - } else { - httpInfo("http server init success at %u", pServer->serverPort); - pServer->status = HTTP_SERVER_RUNNING; - } - - while (1) { - socklen_t addrlen = sizeof(clientAddr); - connFd = accept(pServer->fd, (struct sockaddr *)&clientAddr, &addrlen); - if (pServer->stop) { - httpDebug("http server:%s socket stop, exiting...", pServer->label); - break; - } - - if (connFd == -1) { - if (errno == EINVAL) { - httpDebug("http server:%s socket was shutdown, exiting...", pServer->label); - break; - } - httpError("http server:%s, accept connect failure, errno:%d reason:%s", pServer->label, errno, strerror(errno)); - continue; - } - - totalFds = 1; - for (int32_t i = 0; i < pServer->numOfThreads; ++i) { - totalFds += pServer->pThreads[i].numOfContexts; - } - -#if 0 - if (totalFds > tsHttpCacheSessions * 100) { - httpError("fd:%d, ip:%s:%u, totalFds:%d larger than httpCacheSessions:%d*100, refuse connection", connFd, - taosInetNtoa(clientAddr.sin_addr), htons(clientAddr.sin_port), totalFds, tsHttpCacheSessions); - taosCloseSocket(connFd); - continue; - } -#endif - - taosKeepTcpAlive(connFd); - taosSetNonblocking(connFd, 1); - - // pick up the thread to handle this connection - pThread = pServer->pThreads + threadId; - - pContext = httpCreateContext(connFd); - if (pContext == NULL) { - httpError("fd:%d, ip:%s:%u, no enough resource to allocate http context", connFd, - taosInetNtoa(clientAddr.sin_addr), htons(clientAddr.sin_port)); - taosCloseSocket(connFd); - continue; - } - - pContext->pThread = pThread; - sprintf(pContext->ipstr, "%s:%u", taosInetNtoa(clientAddr.sin_addr), htons(clientAddr.sin_port)); - - struct epoll_event event; -#ifndef _TD_NINGSI_60 - event.events = EPOLLIN | EPOLLPRI | EPOLLWAKEUP | EPOLLERR | EPOLLHUP | EPOLLRDHUP; -#else - event.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP | EPOLLRDHUP; -#endif - event.data.ptr = pContext; - if (epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, connFd, &event) < 0) { - httpError("context:%p, fd:%d, ip:%s, thread:%s, failed to add http fd for epoll, error:%s", pContext, connFd, - pContext->ipstr, pThread->label, strerror(errno)); - taosCloseSocket(pContext->fd); - httpReleaseContext(pContext/*, true*/); - continue; - } - - // notify the data process, add into the FdObj list - atomic_add_fetch_32(&pThread->numOfContexts, 1); - httpDebug("context:%p, fd:%d, ip:%s, thread:%s numOfContexts:%d totalContext:%d, accept a new connection", pContext, - connFd, pContext->ipstr, pThread->label, pThread->numOfContexts, totalFds); - - // pick up next thread for next connection - threadId++; - threadId = threadId % pServer->numOfThreads; - } - - taosCloseSocket(pServer->fd); - return NULL; -} - -bool httpInitConnect() { - HttpServer *pServer = &tsHttpServer; - pServer->pThreads = calloc(pServer->numOfThreads, sizeof(HttpThread)); - if (pServer->pThreads == NULL) { - httpError("init error no enough memory"); - return false; - } - - HttpThread *pThread = pServer->pThreads; - for (int32_t i = 0; i < pServer->numOfThreads; ++i) { - sprintf(pThread->label, "%s%d", pServer->label, i); - pThread->processData = pServer->processData; - pThread->threadId = i; - - if (pthread_mutex_init(&(pThread->threadMutex), NULL) < 0) { - httpError("http thread:%s, failed to init HTTP process data mutex, reason:%s", pThread->label, strerror(errno)); - return false; - } - - pThread->pollFd = (EpollFd)epoll_create(HTTP_MAX_EVENTS); // size does not matter - if (pThread->pollFd <= 0) { - httpError("http thread:%s, failed to create HTTP epoll", pThread->label); - pthread_mutex_destroy(&(pThread->threadMutex)); - return false; - } - - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&(pThread->thread), &thattr, (void *)httpProcessHttpData, (void *)(pThread)) != 0) { - httpError("http thread:%s, failed to create HTTP process data thread, reason:%s", pThread->label, - strerror(errno)); - pthread_mutex_destroy(&(pThread->threadMutex)); - return false; - } - pthread_attr_destroy(&thattr); - - httpDebug("http thread:%p:%s, initialized", pThread, pThread->label); - pThread++; - } - - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&(pServer->thread), &thattr, (void *)httpAcceptHttpConnection, (void *)(pServer)) != 0) { - httpError("http server:%s, failed to create Http accept thread, reason:%s", pServer->label, strerror(errno)); - httpCleanUpConnect(); - return false; - } - pthread_attr_destroy(&thattr); - - httpDebug("http server:%s, initialized, ip:%s:%u, numOfThreads:%d", pServer->label, taosIpStr(pServer->serverIp), - pServer->serverPort, pServer->numOfThreads); - return true; -} - -static bool httpReadData(HttpContext *pContext) { - HttpParser *pParser = pContext->parser; - if (!pParser->inited) { - httpInitParser(pParser); - } - - if (pParser->parsed) { - httpDebug("context:%p, fd:%d, not in ready state, parsed:%d", pContext, pContext->fd, pParser->parsed); - return false; - } - - pContext->accessTimes++; - pContext->lastAccessTime = taosGetTimestampSec(); - char buf[HTTP_STEP_SIZE + 1] = {0}; - - while (1) { - int32_t nread = (int32_t)taosReadSocket(pContext->fd, buf, HTTP_STEP_SIZE); - if (nread > 0) { - buf[nread] = '\0'; - httpTraceL("context:%p, fd:%d, nread:%d content:%s", pContext, pContext->fd, nread, buf); - int32_t ok = httpParseBuf(pParser, buf, nread); - - if (ok) { - httpError("context:%p, fd:%d, parse failed, ret:%d code:%d close connect", pContext, pContext->fd, ok, - pParser->parseCode); - httpSendErrorResp(pContext, pParser->parseCode); - httpNotifyContextClose(pContext); - return false; - } - - if (pParser->parseCode) { - httpError("context:%p, fd:%d, parse failed, code:%d close connect", pContext, pContext->fd, pParser->parseCode); - httpSendErrorResp(pContext, pParser->parseCode); - httpNotifyContextClose(pContext); - return false; - } - - if (!pParser->parsed) { - httpTrace("context:%p, fd:%d, read not finished", pContext, pContext->fd); - continue; - } else { - httpDebug("context:%p, fd:%d, bodyLen:%d", pContext, pContext->fd, pParser->body.pos); - return true; - } - } else if (nread < 0) { - if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { - httpDebug("context:%p, fd:%d, read from socket error:%d, wait another event", pContext, pContext->fd, errno); - continue; // later again - } else { - httpError("context:%p, fd:%d, read from socket error:%d, close connect", pContext, pContext->fd, errno); - taosCloseSocket(pContext->fd); - httpReleaseContext(pContext/*, false */); - return false; - } - } else { - httpError("context:%p, fd:%d, nread:%d, wait another event", pContext, pContext->fd, nread); - taosCloseSocket(pContext->fd); - httpReleaseContext(pContext/*, false */); - return false; - } - } -} diff --git a/src/plugins/http/src/httpSession.c b/src/plugins/http/src/httpSession.c deleted file mode 100644 index 17c12d36a3..0000000000 --- a/src/plugins/http/src/httpSession.c +++ /dev/null @@ -1,125 +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 "httpSession.h" -#include "../../../../include/client/taos.h" -#include "httpContext.h" -#include "httpInt.h" -#include "os.h" -#include "taoserror.h" -#include "tcache.h" -#include "tglobal.h" - -void httpCreateSession(HttpContext *pContext, void *taos) { - HttpServer *server = &tsHttpServer; - httpReleaseSession(pContext); - - pthread_mutex_lock(&server->serverMutex); - - HttpSession session; - memset(&session, 0, sizeof(HttpSession)); - session.taos = taos; - session.refCount = 1; - int32_t len = snprintf(session.id, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass); - - pContext->session = - taosCachePut(server->sessionCache, session.id, len, &session, sizeof(HttpSession), tsHttpSessionExpire * 1000); - // void *temp = pContext->session; - // taosCacheRelease(server->sessionCache, (void **)&temp, false); - - if (pContext->session == NULL) { - httpError("context:%p, fd:%d, user:%s, error:%s", pContext, pContext->fd, pContext->user, - tstrerror(TSDB_CODE_HTTP_SESSION_FULL)); - taos_close(taos); - pthread_mutex_unlock(&server->serverMutex); - return; - } - - httpDebug("context:%p, fd:%d, user:%s, create a new session:%p:%p sessionRef:%d", pContext, pContext->fd, - pContext->user, pContext->session, pContext->session->taos, pContext->session->refCount); - pthread_mutex_unlock(&server->serverMutex); -} - -static void httpFetchSessionImp(HttpContext *pContext) { - HttpServer *server = &tsHttpServer; - pthread_mutex_lock(&server->serverMutex); - - char sessionId[HTTP_SESSION_ID_LEN]; - int32_t len = snprintf(sessionId, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass); - - pContext->session = taosCacheAcquireByKey(server->sessionCache, sessionId, len); - if (pContext->session != NULL) { - atomic_add_fetch_32(&pContext->session->refCount, 1); - httpDebug("context:%p, fd:%d, user:%s, find an exist session:%p:%p, sessionRef:%d", pContext, pContext->fd, - pContext->user, pContext->session, pContext->session->taos, pContext->session->refCount); - } else { - httpDebug("context:%p, fd:%d, user:%s, session not found", pContext, pContext->fd, pContext->user); - } - - pthread_mutex_unlock(&server->serverMutex); -} - -void httpGetSession(HttpContext *pContext) { - if (pContext->session == NULL) { - httpFetchSessionImp(pContext); - } else { - char sessionId[HTTP_SESSION_ID_LEN]; - snprintf(sessionId, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass); - httpReleaseSession(pContext); - httpFetchSessionImp(pContext); - } -} - -void httpReleaseSession(HttpContext *pContext) { - if (pContext == NULL || pContext->session == NULL) return; - - int32_t refCount = atomic_sub_fetch_32(&pContext->session->refCount, 1); - assert(refCount >= 0); - httpDebug("context:%p, release session:%p:%p, sessionRef:%d", pContext, pContext->session, pContext->session->taos, - pContext->session->refCount); - - taosCacheRelease(tsHttpServer.sessionCache, (void **)&pContext->session, false); - pContext->session = NULL; -} - -static void httpDestroySession(void *data) { - HttpSession *session = data; - httpDebug("session:%p:%p, is destroyed, sessionRef:%d", session, session->taos, session->refCount); - - if (session->taos != NULL) { - taos_close(session->taos); - session->taos = NULL; - } -} - -void httpCleanUpSessions() { - if (tsHttpServer.sessionCache != NULL) { - SCacheObj *cache = tsHttpServer.sessionCache; - httpInfo("session cache is cleanuping, size:%d", taosHashGetSize(cache->pHashTable)); - taosCacheCleanup(tsHttpServer.sessionCache); - tsHttpServer.sessionCache = NULL; - } -} - -bool httpInitSessions() { - tsHttpServer.sessionCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, 5, false, httpDestroySession, "rests"); - if (tsHttpServer.sessionCache == NULL) { - httpError("failed to init session cache"); - return false; - } - - return true; -} diff --git a/src/plugins/http/src/httpSql.c b/src/plugins/http/src/httpSql.c deleted file mode 100644 index 28127f582a..0000000000 --- a/src/plugins/http/src/httpSql.c +++ /dev/null @@ -1,491 +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 "httpSql.h" -#include "../../../../include/client/taos.h" -#include "httpAuth.h" -#include "httpContext.h" -#include "httpInt.h" -#include "httpQueue.h" -#include "httpResp.h" -#include "httpSession.h" -#include "os.h" -#include "taoserror.h" -#include "tnote.h" -#include "tsclient.h" - -void httpProcessMultiSql(HttpContext *pContext); - -void httpProcessMultiSqlRetrieveCallBack(void *param, TAOS_RES *result, int32_t numOfRows); - -void httpProcessMultiSqlRetrieveCallBackImp(void *param, TAOS_RES *result, int32_t code, int32_t numOfRows) { - HttpContext *pContext = (HttpContext *)param; - if (pContext == NULL) return; - - HttpSqlCmds * multiCmds = pContext->multiCmds; - HttpEncodeMethod *encode = pContext->encodeMethod; - - HttpSqlCmd *singleCmd = multiCmds->cmds + multiCmds->pos; - char * sql = httpGetCmdsString(pContext, singleCmd->sql); - - bool isContinue = false; - - if (code == TSDB_CODE_SUCCESS && numOfRows > 0) { - if (singleCmd->cmdReturnType == HTTP_CMD_RETURN_TYPE_WITH_RETURN && encode->buildQueryJsonFp) { - isContinue = (encode->buildQueryJsonFp)(pContext, singleCmd, result, numOfRows); - } - } - - if (isContinue) { - // retrieve next batch of rows - httpDebug("context:%p, fd:%d, user:%s, process pos:%d, continue retrieve, numOfRows:%d, sql:%s", pContext, - pContext->fd, pContext->user, multiCmds->pos, numOfRows, sql); - taos_fetch_rows_a(result, httpProcessMultiSqlRetrieveCallBack, param); - } else { - httpDebug("context:%p, fd:%d, user:%s, process pos:%d, stop retrieve, numOfRows:%d, sql:%s", pContext, pContext->fd, - pContext->user, multiCmds->pos, numOfRows, sql); - - if (code < 0) { - httpError("context:%p, fd:%d, user:%s, process pos:%d, retrieve failed code:%s, sql:%s", pContext, pContext->fd, - pContext->user, multiCmds->pos, tstrerror(code), sql); - } - - taos_free_result(result); - - if (singleCmd->cmdReturnType == HTTP_CMD_RETURN_TYPE_WITH_RETURN && encode->stopJsonFp) { - (encode->stopJsonFp)(pContext, singleCmd); - } - multiCmds->pos++; - httpProcessMultiSql(pContext); - } -} - -void httpProcessMultiSqlRetrieveCallBack(void *param, TAOS_RES *result, int32_t numOfRows) { - int32_t code = taos_errno(result); - httpDispatchToResultQueue(param, result, code, numOfRows, httpProcessMultiSqlRetrieveCallBackImp); -} - -void httpProcessMultiSqlCallBackImp(void *param, TAOS_RES *result, int32_t code, int32_t affectRowsInput) { - HttpContext *pContext = (HttpContext *)param; - if (pContext == NULL) return; - - HttpSqlCmds * multiCmds = pContext->multiCmds; - HttpEncodeMethod *encode = pContext->encodeMethod; - - HttpSqlCmd *singleCmd = multiCmds->cmds + multiCmds->pos; - char * sql = httpGetCmdsString(pContext, singleCmd->sql); - - if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) { - httpWarn("context:%p, fd:%d, user:%s, process pos:%d, code:%s:inprogress, sql:%s", pContext, pContext->fd, - pContext->user, multiCmds->pos, tstrerror(code), sql); - return; - } - - if (code != TSDB_CODE_SUCCESS) { - if (encode->checkFinishedFp != NULL && !encode->checkFinishedFp(pContext, singleCmd, code)) { - singleCmd->code = code; - httpDebug("context:%p, fd:%d, user:%s, process pos jump to:%d, last code:%s, last sql:%s", pContext, pContext->fd, - pContext->user, multiCmds->pos + 1, tstrerror(code), sql); - } else { - singleCmd->code = code; - httpError("context:%p, fd:%d, user:%s, process pos:%d, error code:%s, sql:%s", pContext, pContext->fd, - pContext->user, multiCmds->pos, tstrerror(code), sql); - - if (singleCmd->cmdReturnType == HTTP_CMD_RETURN_TYPE_WITH_RETURN) { - if (encode->startJsonFp) (encode->startJsonFp)(pContext, singleCmd, result); - if (encode->stopJsonFp) (encode->stopJsonFp)(pContext, singleCmd); - } - } - multiCmds->pos++; - httpProcessMultiSql(pContext); - - taos_free_result(result); - return; - } - - bool isUpdate = tscIsUpdateQuery(result); - if (isUpdate) { - // not select or show commands - int32_t affectRows = taos_affected_rows(result); - httpDebug("context:%p, fd:%d, user:%s, process pos:%d, affect rows:%d, sql:%s", pContext, pContext->fd, - pContext->user, multiCmds->pos, affectRows, sql); - - singleCmd->code = 0; - - if (singleCmd->cmdReturnType == HTTP_CMD_RETURN_TYPE_WITH_RETURN && encode->startJsonFp) { - (encode->startJsonFp)(pContext, singleCmd, result); - } - - if (singleCmd->cmdReturnType == HTTP_CMD_RETURN_TYPE_WITH_RETURN && encode->buildAffectRowJsonFp) { - (encode->buildAffectRowJsonFp)(pContext, singleCmd, affectRows); - } - - if (singleCmd->cmdReturnType == HTTP_CMD_RETURN_TYPE_WITH_RETURN && encode->stopJsonFp) { - (encode->stopJsonFp)(pContext, singleCmd); - } - - if (encode->setNextCmdFp) { - (encode->setNextCmdFp)(pContext, singleCmd, code); - } else { - multiCmds->pos++; - } - - taos_free_result(result); - httpProcessMultiSql(pContext); - } else { - httpDebug("context:%p, fd:%d, user:%s, process pos:%d, start retrieve, sql:%s", pContext, pContext->fd, - pContext->user, multiCmds->pos, sql); - - if (singleCmd->cmdReturnType == HTTP_CMD_RETURN_TYPE_WITH_RETURN && encode->startJsonFp) { - (encode->startJsonFp)(pContext, singleCmd, result); - } - taos_fetch_rows_a(result, httpProcessMultiSqlRetrieveCallBack, pContext); - } -} - -void httpProcessMultiSqlCallBack(void *param, TAOS_RES *result, int32_t unUsedCode) { - int32_t code = taos_errno(result); - int32_t affectRows = taos_affected_rows(result); - httpDispatchToResultQueue(param, result, code, affectRows, httpProcessMultiSqlCallBackImp); -} - -void httpProcessMultiSql(HttpContext *pContext) { - HttpSqlCmds * multiCmds = pContext->multiCmds; - HttpEncodeMethod *encode = pContext->encodeMethod; - - if (multiCmds->pos >= multiCmds->size) { - httpDebug("context:%p, fd:%d, user:%s, process pos:%d, size:%d, stop mulit-querys", pContext, pContext->fd, - pContext->user, multiCmds->pos, multiCmds->size); - if (encode->cleanJsonFp) { - (encode->cleanJsonFp)(pContext); - } - httpCloseContextByApp(pContext); - return; - } - - HttpSqlCmd *cmd = multiCmds->cmds + multiCmds->pos; - - char *sql = httpGetCmdsString(pContext, cmd->sql); - httpTraceL("context:%p, fd:%d, user:%s, process pos:%d, start query, sql:%s", pContext, pContext->fd, pContext->user, - multiCmds->pos, sql); - nPrintHttp("%s", sql); - taos_query_a(pContext->session->taos, sql, httpProcessMultiSqlCallBack, (void *)pContext); -} - -void httpProcessMultiSqlCmd(HttpContext *pContext) { - if (pContext == NULL) return; - - HttpSqlCmds *multiCmds = pContext->multiCmds; - if (multiCmds == NULL || multiCmds->size <= 0 || multiCmds->pos >= multiCmds->size || multiCmds->pos < 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_INVALID_MULTI_REQUEST); - return; - } - - httpDebug("context:%p, fd:%d, user:%s, start multi-querys pos:%d, size:%d", pContext, pContext->fd, pContext->user, - multiCmds->pos, multiCmds->size); - HttpEncodeMethod *encode = pContext->encodeMethod; - if (encode->initJsonFp) { - (encode->initJsonFp)(pContext); - } - - httpProcessMultiSql(pContext); -} - -void httpProcessSingleSqlRetrieveCallBack(void *param, TAOS_RES *result, int32_t numOfRows); - -void httpProcessSingleSqlRetrieveCallBackImp(void *param, TAOS_RES *result, int32_t code, int32_t numOfRows) { - HttpContext *pContext = (HttpContext *)param; - if (pContext == NULL) return; - - HttpEncodeMethod *encode = pContext->encodeMethod; - - bool isContinue = false; - - if (code == TSDB_CODE_SUCCESS && numOfRows > 0) { - if (encode->buildQueryJsonFp) { - isContinue = (encode->buildQueryJsonFp)(pContext, &pContext->singleCmd, result, numOfRows); - } - } - - if (isContinue) { - // retrieve next batch of rows - httpDebug("context:%p, fd:%d, user:%s, continue retrieve, numOfRows:%d", pContext, pContext->fd, pContext->user, - numOfRows); - taos_fetch_rows_a(result, httpProcessSingleSqlRetrieveCallBack, param); - } else { - httpDebug("context:%p, fd:%d, user:%s, stop retrieve, numOfRows:%d", pContext, pContext->fd, pContext->user, - numOfRows); - - if (code < 0) { - httpError("context:%p, fd:%d, user:%s, retrieve failed, code:%s", pContext, pContext->fd, pContext->user, - tstrerror(code)); - } - - taos_free_result(result); - - if (encode->stopJsonFp) { - (encode->stopJsonFp)(pContext, &pContext->singleCmd); - } - - httpCloseContextByApp(pContext); - } -} - -void httpProcessSingleSqlRetrieveCallBack(void *param, TAOS_RES *result, int32_t numOfRows) { - int32_t code = taos_errno(result); - httpDispatchToResultQueue(param, result, code, numOfRows, httpProcessSingleSqlRetrieveCallBackImp); -} - -void httpProcessSingleSqlCallBackImp(void *param, TAOS_RES *result, int32_t code, int32_t affectRowsInput) { - HttpContext *pContext = (HttpContext *)param; - if (pContext == NULL) return; - - HttpEncodeMethod *encode = pContext->encodeMethod; - - if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) { - httpError("context:%p, fd:%d, user:%s, query error, code:%s:inprogress, sqlObj:%p", pContext, pContext->fd, - pContext->user, tstrerror(code), result); - return; - } - - if (code != TSDB_CODE_SUCCESS) { - SSqlObj *pObj = (SSqlObj *)result; - if (code == TSDB_CODE_TSC_INVALID_OPERATION) { - terrno = code; - httpError("context:%p, fd:%d, user:%s, query error, code:%s, sqlObj:%p, error:%s", pContext, pContext->fd, - pContext->user, tstrerror(code), pObj, taos_errstr(pObj)); - httpSendTaosdInvalidSqlErrorResp(pContext, taos_errstr(pObj)); - } else { - httpError("context:%p, fd:%d, user:%s, query error, code:%s, sqlObj:%p", pContext, pContext->fd, pContext->user, - tstrerror(code), pObj); - httpSendErrorResp(pContext, code); - } - taos_free_result(result); - return; - } - - bool isUpdate = tscIsUpdateQuery(result); - if (isUpdate) { - // not select or show commands - int32_t affectRows = taos_affected_rows(result); - assert(affectRows == affectRowsInput); - - httpDebug("context:%p, fd:%d, user:%s, affect rows:%d, stop query, sqlObj:%p", pContext, pContext->fd, - pContext->user, affectRows, result); - - if (encode->startJsonFp) { - (encode->startJsonFp)(pContext, &pContext->singleCmd, result); - } - - if (encode->buildAffectRowJsonFp) { - (encode->buildAffectRowJsonFp)(pContext, &pContext->singleCmd, affectRows); - } - - if (encode->stopJsonFp) { - (encode->stopJsonFp)(pContext, &pContext->singleCmd); - } - - taos_free_result(result); - httpCloseContextByApp(pContext); - } else { - httpDebug("context:%p, fd:%d, user:%s, start retrieve", pContext, pContext->fd, pContext->user); - - if (encode->startJsonFp) { - (encode->startJsonFp)(pContext, &pContext->singleCmd, result); - } - - taos_fetch_rows_a(result, httpProcessSingleSqlRetrieveCallBack, pContext); - } -} - -void httpProcessSingleSqlCallBack(void *param, TAOS_RES *result, int32_t unUsedCode) { - int32_t code = taos_errno(result); - int32_t affectRows = taos_affected_rows(result); - httpDispatchToResultQueue(param, result, code, affectRows, httpProcessSingleSqlCallBackImp); -} - -void httpProcessSingleSqlCmd(HttpContext *pContext) { - HttpSqlCmd * cmd = &pContext->singleCmd; - char * sql = cmd->nativSql; - HttpSession *pSession = pContext->session; - - if (sql == NULL || sql[0] == 0) { - httpError("context:%p, fd:%d, user:%s, error:no sql input", pContext, pContext->fd, pContext->user); - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_SQL_INPUT); - return; - } - - httpTraceL("context:%p, fd:%d, user:%s, start query, sql:%s", pContext, pContext->fd, pContext->user, sql); - nPrintHttp("%s", sql); - taos_query_a(pSession->taos, sql, httpProcessSingleSqlCallBack, (void *)pContext); -} - -void httpProcessLoginCmd(HttpContext *pContext) { - char token[128] = {0}; - if (httpGenTaosdAuthToken(pContext, token, 128) != 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_GEN_TAOSD_TOKEN_ERR); - } else { - httpDebug("context:%p, fd:%d, user:%s, login via http, return token:%s", pContext, pContext->fd, pContext->user, - token); - httpSendSuccResp(pContext, token); - } -} - -void httpProcessHeartBeatCmd(HttpContext *pContext) { - HttpEncodeMethod *encode = pContext->encodeMethod; - if (encode->startJsonFp) { - (encode->startJsonFp)(pContext, &pContext->singleCmd, NULL); - } - if (encode->stopJsonFp) { - (encode->stopJsonFp)(pContext, &pContext->singleCmd); - } - httpCloseContextByApp(pContext); -} - -void httpExecCmd(HttpContext *pContext) { - switch (pContext->reqType) { - case HTTP_REQTYPE_LOGIN: - httpProcessLoginCmd(pContext); - break; - case HTTP_REQTYPE_SINGLE_SQL: - httpProcessSingleSqlCmd(pContext); - break; - case HTTP_REQTYPE_MULTI_SQL: - httpProcessMultiSqlCmd(pContext); - break; - case HTTP_REQTYPE_HEARTBEAT: - httpProcessHeartBeatCmd(pContext); - break; - case HTTP_REQTYPE_OTHERS: - httpCloseContextByApp(pContext); - break; - default: - httpCloseContextByApp(pContext); - break; - } - - memset(&pContext->singleCmd, 0, sizeof(HttpSqlCmd)); -} - -void httpProcessRequestCb(void *param, TAOS_RES *result, int32_t code) { - HttpContext *pContext = param; - taos_free_result(result); - - if (pContext == NULL) return; - - if (code < 0) { - httpError("context:%p, fd:%d, user:%s, login error, code:%s", pContext, pContext->fd, pContext->user, - tstrerror(code)); - httpSendErrorResp(pContext, code); - return; - } - - httpDebug("context:%p, fd:%d, user:%s, connect tdengine success, taos:%p", pContext, pContext->fd, pContext->user, - pContext->taos); - if (pContext->taos == NULL) { - httpError("context:%p, fd:%d, user:%s, login error, taos is empty", pContext, pContext->fd, pContext->user); - httpSendErrorResp(pContext, TSDB_CODE_HTTP_LOGIN_FAILED); - return; - } - - httpCreateSession(pContext, pContext->taos); - - if (pContext->session == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_SESSION_FULL); - } else { - httpExecCmd(pContext); - } -} - -void httpProcessRequest(HttpContext *pContext) { - httpGetSession(pContext); - - if (pContext->session == NULL || pContext->reqType == HTTP_REQTYPE_LOGIN) { - taos_connect_a(NULL, pContext->user, pContext->pass, "", 0, httpProcessRequestCb, (void *)pContext, - &(pContext->taos)); - httpDebug("context:%p, fd:%d, user:%s, try connect tdengine, taos:%p", pContext, pContext->fd, pContext->user, - pContext->taos); - - if (pContext->taos != NULL) { - STscObj *pObj = pContext->taos; - pObj->from = TAOS_REQ_FROM_HTTP; - } - } else { - httpExecCmd(pContext); - } -} - -int32_t httpCheckAllocEscapeSql(char *oldSql, char **newSql) -{ - char *pos; - - if (oldSql == NULL || newSql == NULL) { - return TSDB_CODE_SUCCESS; - } - - /* bad sql clause */ - pos = strstr(oldSql, "%%"); - if (pos) { - httpError("bad sql:%s", oldSql); - return TSDB_CODE_HTTP_REQUEST_JSON_ERROR; - } - - pos = strchr(oldSql, '%'); - if (pos == NULL) { - httpDebug("sql:%s", oldSql); - *newSql = oldSql; - return TSDB_CODE_SUCCESS; - } - - *newSql = (char *) calloc(1, (strlen(oldSql) << 1) + 1); - if (newSql == NULL) { - httpError("failed to allocate for new sql, old sql:%s", oldSql); - return TSDB_CODE_HTTP_NO_ENOUGH_MEMORY; - } - - char *src = oldSql; - char *dst = *newSql; - size_t sqlLen = strlen(src); - - while (1) { - memcpy(dst, src, pos - src + 1); - dst += pos - src + 1; - *dst++ = '%'; - - if (pos + 1 >= oldSql + sqlLen) { - break; - } - - src = ++pos; - pos = strchr(pos, '%'); - if (pos == NULL) { - memcpy(dst, src, strlen(src)); - break; - } - } - - return TSDB_CODE_SUCCESS; -} - -void httpCheckFreeEscapedSql(char *oldSql, char *newSql) -{ - if (oldSql && newSql) { - if (oldSql != newSql) { - free(newSql); - } - } -} - diff --git a/src/plugins/http/src/httpSystem.c b/src/plugins/http/src/httpSystem.c deleted file mode 100644 index 878a3251c0..0000000000 --- a/src/plugins/http/src/httpSystem.c +++ /dev/null @@ -1,123 +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 "../../../../include/client/taos.h" -#include "httpContext.h" -#include "httpGcHandle.h" -#include "httpHandle.h" -#include "httpInt.h" -#include "httpMetricsHandle.h" -#include "httpQueue.h" -#include "httpResp.h" -#include "httpRestHandle.h" -#include "httpServer.h" -#include "httpSession.h" -#include "httpTgHandle.h" -#include "os.h" -#include "tadmin.h" -#include "tglobal.h" -#include "tsocket.h" -#include "ttimer.h" - -#ifndef _ADMIN -void adminInitHandle(HttpServer* pServer) {} -void opInitHandle(HttpServer* pServer) {} -#endif - -HttpServer tsHttpServer; - -int32_t httpInitSystem() { - strcpy(tsHttpServer.label, "rest"); - tsHttpServer.serverIp = 0; - tsHttpServer.serverPort = tsHttpPort; - tsHttpServer.numOfThreads = tsHttpMaxThreads; - tsHttpServer.processData = httpProcessData; - - pthread_mutex_init(&tsHttpServer.serverMutex, NULL); - - restInitHandle(&tsHttpServer); - adminInitHandle(&tsHttpServer); - gcInitHandle(&tsHttpServer); - tgInitHandle(&tsHttpServer); - opInitHandle(&tsHttpServer); - metricsInitHandle(&tsHttpServer); - return 0; -} - -int32_t httpStartSystem() { - httpInfo("start http server ..."); - - if (tsHttpServer.status != HTTP_SERVER_INIT) { - httpError("http server is already started"); - return -1; - } - - if (!httpInitResultQueue()) { - httpError("http init result queue failed"); - return -1; - } - - if (!httpInitContexts()) { - httpError("http init contexts failed"); - return -1; - } - - if (!httpInitSessions()) { - httpError("http init session failed"); - return -1; - } - - if (!httpInitConnect()) { - httpError("http init server failed"); - return -1; - } - - return 0; -} - -void httpStopSystem() { - tsHttpServer.status = HTTP_SERVER_CLOSING; - tsHttpServer.stop = 1; -#ifdef WINDOWS - closesocket(tsHttpServer.fd); -#elif __APPLE__ - if (tsHttpServer.fd!=-1) { - close(tsHttpServer.fd); - tsHttpServer.fd = -1; - } -#else - shutdown(tsHttpServer.fd, SHUT_RD); -#endif - tgCleanupHandle(); -} - -void httpCleanUpSystem() { - httpInfo("http server cleanup"); - httpStopSystem(); - - httpCleanUpConnect(); - httpCleanupContexts(); - httpCleanUpSessions(); - httpCleanupResultQueue(); - - pthread_mutex_destroy(&tsHttpServer.serverMutex); - tfree(tsHttpServer.pThreads); - tsHttpServer.pThreads = NULL; - - tsHttpServer.status = HTTP_SERVER_CLOSED; -} - -int32_t httpGetReqCount() { return atomic_exchange_32(&tsHttpServer.requestNum, 0); } diff --git a/src/plugins/http/src/httpTgHandle.c b/src/plugins/http/src/httpTgHandle.c deleted file mode 100644 index 32516b9fd1..0000000000 --- a/src/plugins/http/src/httpTgHandle.c +++ /dev/null @@ -1,917 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "tglobal.h" -#include "taosdef.h" -#include "taosmsg.h" -#include "httpInt.h" -#include "httpTgHandle.h" -#include "httpTgJson.h" -#include "cJSON.h" - -/* - * taos.telegraf.cfg formats like - { - "metrics": [ - { - "name" : "system", - "tbname" : "system_uptime", - "fields": [ - "uptime" - ] - }, - { - "name": "system", - "tbname" : "system_uptime_format", - "fields": [ - "uptime_format" - ] - }, - { - "name": "swap", - "tbname" : "swap_in", - "fields": [ - "in" - ] - }, - { - "name": "cpu", - "tbname" : "cpu_usage", - "fields": [ - "usage_active", - "usage_guest" - ] - } - ] - } - */ - -#define TG_MAX_SORT_TAG_SIZE 20 - -static HttpDecodeMethod tgDecodeMethod = {"telegraf", tgProcessRquest}; -static HttpEncodeMethod tgQueryMethod = { - .startJsonFp = tgStartQueryJson, - .stopJsonFp = tgStopQueryJson, - .buildQueryJsonFp = NULL, - .buildAffectRowJsonFp = tgBuildSqlAffectRowsJson, - .initJsonFp = tgInitQueryJson, - .cleanJsonFp = tgCleanQueryJson, - .checkFinishedFp = tgCheckFinished, - .setNextCmdFp = tgSetNextCmd -}; - -static const char DEFAULT_TELEGRAF_CFG[] = - "{\"metrics\":[" - "{\"name\":\"system\",\"tbname\":\"system_uptime\",\"fields\":[\"uptime\"]}," - "{\"name\":\"system\",\"tbname\":\"system_uptime_format\",\"fields\":[\"uptime_format\"]}," - "{\"name\":\"swap\",\"tbname\":\"swap_in\",\"fields\":[\"in\"]}," - "{\"name\":\"cpu\",\"tbname\":\"cpu_usage\",\"fields\":[\"usage_guest\"]}" - "]}"; - -typedef struct { - char * name; - char * tbName; - char ** fields; - int32_t fieldNum; -} STgSchema; - -typedef struct { - STgSchema *schemas; - int32_t size; - int32_t pos; -} STgSchemas; - -static STgSchemas tgSchemas = {0}; - -void tgFreeSchema(STgSchema *schema) { - if (schema->name != NULL) { - free(schema->name); - schema->name = NULL; - } - if (schema->tbName != NULL) { - free(schema->tbName); - schema->tbName = NULL; - } - if (schema->fields != NULL) { - for (int32_t f = 0; f < schema->fieldNum; ++f) { - if (schema->fields[f] != NULL) { - free(schema->fields[f]); - schema->fields[f] = NULL; - } - } - free(schema->fields); - schema->fields = NULL; - schema->fieldNum = 0; - } -} - -void tgFreeSchemas() { - if (tgSchemas.schemas != NULL) { - for (int32_t s = 0; s < tgSchemas.size; ++s) { - tgFreeSchema(&tgSchemas.schemas[s]); - } - free(tgSchemas.schemas); - tgSchemas.size = 0; - tgSchemas.schemas = NULL; - } -} - -void tgInitSchemas(int32_t size) { - tgFreeSchemas(); - tgSchemas.schemas = calloc(sizeof(STgSchema), size); - tgSchemas.size = 0; -} - -void tgParseSchemaMetric(cJSON *metric) { - STgSchema schema = {0}; - bool parsedOk = true; - - // name - cJSON *name = cJSON_GetObjectItem(metric, "name"); - if (name == NULL) { - parsedOk = false; - goto ParseEnd; - } - if (name->type != cJSON_String) { - parsedOk = false; - goto ParseEnd; - } - if (name->valuestring == NULL) { - parsedOk = false; - goto ParseEnd; - } - int32_t nameLen = (int32_t)strlen(name->valuestring); - if (nameLen == 0) { - parsedOk = false; - goto ParseEnd; - } - - schema.name = calloc(nameLen + 1, 1); - strcpy(schema.name, name->valuestring); - - // tbname - cJSON *tbname = cJSON_GetObjectItem(metric, "tbname"); - if (tbname == NULL) { - parsedOk = false; - goto ParseEnd; - } - if (tbname->type != cJSON_String) { - parsedOk = false; - goto ParseEnd; - } - if (tbname->valuestring == NULL) { - parsedOk = false; - goto ParseEnd; - } - int32_t tbnameLen = (int32_t)strlen(tbname->valuestring); - if (tbnameLen == 0) { - parsedOk = false; - goto ParseEnd; - } - - schema.tbName = calloc(tbnameLen + 1, 1); - strcpy(schema.tbName, tbname->valuestring); - - // fields - cJSON *fields = cJSON_GetObjectItem(metric, "fields"); - if (fields == NULL) { - goto ParseEnd; - } - int32_t fieldSize = cJSON_GetArraySize(fields); - if (fieldSize <= 0 || fieldSize > TSDB_MAX_COLUMNS) { - goto ParseEnd; - } - - if (fieldSize > 0) { - schema.fields = calloc(sizeof(STgSchema), (size_t)fieldSize); - schema.fieldNum = fieldSize; - for (int32_t i = 0; i < fieldSize; i++) { - cJSON *field = cJSON_GetArrayItem(fields, i); - if (field == NULL) { - parsedOk = false; - goto ParseEnd; - } - if (field->valuestring == NULL) { - parsedOk = false; - goto ParseEnd; - } - nameLen = (int32_t)strlen(field->valuestring); - if (nameLen == 0 || nameLen >= TSDB_TABLE_NAME_LEN) { - parsedOk = false; - goto ParseEnd; - } - schema.fields[i] = calloc(nameLen + 1, 1); - strcpy(schema.fields[i], field->valuestring); - } - } - -ParseEnd: - if (parsedOk) { - tgSchemas.schemas[tgSchemas.size++] = schema; - } else { - tgFreeSchema(&schema); - } -} - -int32_t tgParseSchema(const char *content, char *fileName) { - cJSON *root = cJSON_Parse(content); - if (root == NULL) { - httpError("failed to parse telegraf schema file:%s, invalid json format, content:%s", fileName, content); - return -1; - } - int32_t size = 0; - cJSON * metrics = cJSON_GetObjectItem(root, "metrics"); - if (metrics != NULL) { - size = cJSON_GetArraySize(metrics); - if (size <= 0) { - httpError("failed to parse telegraf schema file:%s, metrics size is 0", fileName); - cJSON_Delete(root); - return -1; - } - - tgInitSchemas(size); - for (int32_t i = 0; i < size; i++) { - cJSON *metric = cJSON_GetArrayItem(metrics, i); - if (metric != NULL) { - tgParseSchemaMetric(metric); - } - } - } else { - size = 1; - tgInitSchemas(size); - tgParseSchemaMetric(root); - } - - cJSON_Delete(root); - return size; -} - -int32_t tgReadSchema(char *fileName) { - FILE *fp = fopen(fileName, "r"); - if (fp == NULL) { - return -1; - } - - httpInfo("open telegraf schema file:%s success", fileName); - fseek(fp, 0, SEEK_END); - int32_t contentSize = (int32_t)ftell(fp); - if (contentSize <= 0) { - fclose(fp); - return 0; - } - - rewind(fp); - char * content = (char *)calloc(contentSize + 1, 1); - int32_t result = (int32_t)fread(content, 1, contentSize, fp); - - if (result != contentSize) { - httpError("failed to read telegraf schema file:%s", fileName); - fclose(fp); - free(content); - return 0; - } - - content[contentSize] = 0; - int32_t schemaNum = tgParseSchema(content, fileName); - - free(content); - fclose(fp); - httpInfo("parse telegraf schema file:%s, schema size:%d", fileName, schemaNum); - - return schemaNum; -} - -void tgInitHandle(HttpServer *pServer) { - char fileName[TSDB_FILENAME_LEN * 2] = {0}; - sprintf(fileName, "%s/taos.telegraf.cfg", configDir); - if (tgReadSchema(fileName) <= 0) { - tgFreeSchemas(); - if (tgParseSchema(DEFAULT_TELEGRAF_CFG, "default") <= 0) { - tgFreeSchemas(); - } - } - - httpAddMethod(pServer, &tgDecodeMethod); -} - -void tgCleanupHandle() { tgFreeSchemas(); } - -bool tgGetUserFromUrl(HttpContext *pContext) { - HttpParser *pParser = pContext->parser; - if (pParser->path[TG_USER_URL_POS].pos >= TSDB_USER_LEN || pParser->path[TG_USER_URL_POS].pos <= 0) { - return false; - } - - tstrncpy(pContext->user, pParser->path[TG_USER_URL_POS].str, sizeof(pContext->user)); - return true; -} - -bool tgGetPassFromUrl(HttpContext *pContext) { - HttpParser *pParser = pContext->parser; - if (pParser->path[TG_PASS_URL_POS].pos >= HTTP_PASSWORD_LEN || pParser->path[TG_PASS_URL_POS].pos <= 0) { - return false; - } - - tstrncpy(pContext->pass, pParser->path[TG_PASS_URL_POS].str, sizeof(pContext->pass)); - return true; -} - -char *tgGetDbFromUrl(HttpContext *pContext) { - HttpParser *pParser = pContext->parser; - if (pParser->path[TG_DB_URL_POS].pos <= 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_DB_NOT_INPUT); - return NULL; - } - - if (pParser->path[TG_DB_URL_POS].pos >= TSDB_DB_NAME_LEN) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_DB_TOO_LONG); - return NULL; - } - - return pParser->path[TG_DB_URL_POS].str; -} - -char *tgGetStableName(char *stname, cJSON *fields, int32_t fieldsSize) { - for (int32_t s = 0; s < tgSchemas.size; ++s) { - STgSchema *schema = &tgSchemas.schemas[s]; - if (strcasecmp(schema->name, stname) != 0) { - continue; - } - - bool schemaMatched = true; - for (int32_t f = 0; f < schema->fieldNum; ++f) { - char *fieldName = schema->fields[f]; - bool fieldMatched = false; - - for (int32_t i = 0; i < fieldsSize; i++) { - cJSON *field = cJSON_GetArrayItem(fields, i); - if (strcasecmp(field->string, fieldName) == 0) { - fieldMatched = true; - break; - } - } - - if (!fieldMatched) { - schemaMatched = false; - break; - } - } - - if (schemaMatched) { - return schema->tbName; - } - } - - return stname; -} - -/* - * parse single metric - { - "fields": { - "field_1": 30, - "field_2": 4, - "field_N": 59, - "n_images": 660 - }, - "name": "docker", - "tags": { - "host": "raynor" - }, - "timestamp": 1458229140 - } - */ -bool tgProcessSingleMetric(HttpContext *pContext, cJSON *metric, char *db) { - // metric name - cJSON *name = cJSON_GetObjectItem(metric, "name"); - if (name == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRIC_NULL); - return false; - } - if (name->type != cJSON_String) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRIC_TYPE); - return false; - } - if (name->valuestring == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRIC_NAME_NULL); - return false; - } - int32_t nameLen = (int32_t)strlen(name->valuestring); - if (nameLen == 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRIC_NAME_NULL); - return false; - } - if (nameLen >= TSDB_TABLE_NAME_LEN - 8) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRIC_NAME_LONG); - return false; - } - - // timestamp - cJSON *timestamp = cJSON_GetObjectItem(metric, "timestamp"); - if (timestamp == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TIMESTAMP_NULL); - return false; - } - if (timestamp->type != cJSON_Number) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TIMESTAMP_TYPE); - return false; - } - if (timestamp->valueint <= 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TIMESTAMP_VAL_NULL); - return false; - } - - // tags - cJSON *tags = cJSON_GetObjectItem(metric, "tags"); - if (tags == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAGS_NULL); - return false; - } - - int32_t tagsSize = cJSON_GetArraySize(tags); - if (tagsSize <= 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAGS_SIZE_0); - return false; - } - - if (tagsSize > TG_MAX_SORT_TAG_SIZE) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAGS_SIZE_LONG); - return false; - } - - cJSON *host = NULL; - - for (int32_t i = 0; i < tagsSize; i++) { - cJSON *tag = cJSON_GetArrayItem(tags, i); - if (tag == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAG_NULL); - return false; - } - if (tag->string == NULL || strlen(tag->string) == 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAG_NAME_NULL); - return false; - } - - /* - * tag size may be larget than TSDB_COL_NAME_LEN - * we keep the first TSDB_COL_NAME_LEN bytes - */ - if (0) { - if (strlen(tag->string) >= TSDB_COL_NAME_LEN) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAG_NAME_SIZE); - return false; - } - } - - if (tag->type != cJSON_Number && tag->type != cJSON_String) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAG_VALUE_TYPE); - return false; - } - - if (tag->type == cJSON_String) { - if (tag->valuestring == NULL || strlen(tag->valuestring) == 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TAG_VALUE_NULL); - return false; - } - } - - if (strcasecmp(tag->string, "host") == 0) { - host = tag; - } - } - - if (host == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TABLE_NULL); - return false; - } - - if (host->type != cJSON_String) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_HOST_NOT_STRING); - return false; - } - - if (strlen(host->valuestring) >= TSDB_TABLE_NAME_LEN - 1) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_TABLE_SIZE); - return false; - } - - // fields - cJSON *fields = cJSON_GetObjectItem(metric, "fields"); - if (fields == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELDS_NULL); - return false; - } - - int32_t fieldsSize = cJSON_GetArraySize(fields); - if (fieldsSize <= 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELDS_SIZE_0); - return false; - } - - if (fieldsSize > (TSDB_MAX_COLUMNS - TSDB_MAX_TAGS - 1)) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELDS_SIZE_LONG); - return false; - } - - for (int32_t i = 0; i < fieldsSize; i++) { - cJSON *field = cJSON_GetArrayItem(fields, i); - if (field == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELD_NULL); - return false; - } - if (field->string == NULL || strlen(field->string) == 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELD_NAME_NULL); - return false; - } - /* - * tag size may be larget than TSDB_COL_NAME_LEN - * we keep the first TSDB_COL_NAME_LEN bytes - */ - if (0) { - if (strlen(field->string) >= TSDB_COL_NAME_LEN) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELD_NAME_SIZE); - return false; - } - } - if (field->type != cJSON_Number && field->type != cJSON_String) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELD_VALUE_TYPE); - return false; - } - if (field->type == cJSON_String) { - if (field->valuestring == NULL || strlen(field->valuestring) == 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_FIELD_VALUE_NULL); - return false; - } - } - } - - // assembling cmds - HttpSqlCmd *stable_cmd = httpNewSqlCmd(pContext); - if (stable_cmd == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); - return false; - } - stable_cmd->cmdType = HTTP_CMD_TYPE_CREATE_STBALE; - stable_cmd->cmdReturnType = HTTP_CMD_RETURN_TYPE_NO_RETURN; - - HttpSqlCmd *table_cmd = httpNewSqlCmd(pContext); - if (table_cmd == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); - return false; - } - table_cmd->cmdType = HTTP_CMD_TYPE_INSERT; - - // order by tag name - cJSON * orderedTags[TG_MAX_SORT_TAG_SIZE] = {0}; - int32_t orderTagsLen = 0; - for (int32_t i = 0; i < tagsSize; ++i) { - cJSON *tag = cJSON_GetArrayItem(tags, i); - orderedTags[orderTagsLen++] = tag; - for (int32_t j = orderTagsLen - 1; j >= 1; --j) { - cJSON *tag1 = orderedTags[j]; - cJSON *tag2 = orderedTags[j - 1]; - if (strcasecmp(tag1->string, "host") == 0 || strcmp(tag1->string, tag2->string) < 0) { - orderedTags[j] = tag2; - orderedTags[j - 1] = tag1; - } - } - } - orderTagsLen = orderTagsLen < TSDB_MAX_TAGS ? orderTagsLen : TSDB_MAX_TAGS; - - table_cmd->tagNum = stable_cmd->tagNum = (int8_t)orderTagsLen; - table_cmd->timestamp = stable_cmd->timestamp = httpAddToSqlCmdBuffer(pContext, "%" PRId64, timestamp->valueint); - - // stable name - char *stname = tgGetStableName(name->valuestring, fields, fieldsSize); - table_cmd->metric = stable_cmd->metric = httpAddToSqlCmdBuffer(pContext, "%s", stname); - if (tsTelegrafUseFieldNum == 0) { - table_cmd->stable = stable_cmd->stable = httpAddToSqlCmdBuffer(pContext, "%s", stname); - } else { - table_cmd->stable = stable_cmd->stable = - httpAddToSqlCmdBuffer(pContext, "%s_%d_%d", stname, fieldsSize, orderTagsLen); - } - table_cmd->stable = stable_cmd->stable = - httpShrinkTableName(pContext, table_cmd->stable, httpGetCmdsString(pContext, table_cmd->stable)); - - // stable tag for detail - for (int32_t i = 0; i < orderTagsLen; ++i) { - cJSON *tag = orderedTags[i]; - - char *tagStr = NULL; - int32_t retCode = httpCheckAllocEscapeSql(tag->string, &tagStr); - if (retCode != TSDB_CODE_SUCCESS) { - httpSendErrorResp(pContext, retCode); - - return false; - } - - stable_cmd->tagNames[i] = table_cmd->tagNames[i] = httpAddToSqlCmdBuffer(pContext, tagStr); - - httpCheckFreeEscapedSql(tag->string, tagStr); - - if (tag->type == cJSON_String) - stable_cmd->tagValues[i] = table_cmd->tagValues[i] = httpAddToSqlCmdBuffer(pContext, "'%s'", tag->valuestring); - else if (tag->type == cJSON_Number) - stable_cmd->tagValues[i] = table_cmd->tagValues[i] = httpAddToSqlCmdBuffer(pContext, "%" PRId64, tag->valueint); - else if (tag->type == cJSON_True) - stable_cmd->tagValues[i] = table_cmd->tagValues[i] = httpAddToSqlCmdBuffer(pContext, "1"); - else if (tag->type == cJSON_False) - stable_cmd->tagValues[i] = table_cmd->tagValues[i] = httpAddToSqlCmdBuffer(pContext, "0"); - else - stable_cmd->tagValues[i] = table_cmd->tagValues[i] = httpAddToSqlCmdBuffer(pContext, "NULL"); - } - - // table name - if (tsTelegrafUseFieldNum == 0) { - table_cmd->table = stable_cmd->table = - httpAddToSqlCmdBufferNoTerminal(pContext, "%s_%s", stname, host->valuestring); - } else { - table_cmd->table = stable_cmd->table = - httpAddToSqlCmdBufferNoTerminal(pContext, "%s_%d_%d_%s", stname, fieldsSize, orderTagsLen, host->valuestring); - } - for (int32_t i = 0; i < orderTagsLen; ++i) { - cJSON *tag = orderedTags[i]; - if (tag == host) continue; - if (tag->type == cJSON_String) - httpAddToSqlCmdBufferNoTerminal(pContext, "_%s", tag->valuestring); - else if (tag->type == cJSON_Number) - httpAddToSqlCmdBufferNoTerminal(pContext, "_%" PRId64, tag->valueint); - else if (tag->type == cJSON_False) - httpAddToSqlCmdBufferNoTerminal(pContext, "_0"); - else if (tag->type == cJSON_True) - httpAddToSqlCmdBufferNoTerminal(pContext, "_1"); - else - httpAddToSqlCmdBufferNoTerminal(pContext, "_n"); - } - httpAddToSqlCmdBuffer(pContext, ""); - - table_cmd->table = stable_cmd->table = - httpShrinkTableName(pContext, table_cmd->table, httpGetCmdsString(pContext, table_cmd->table)); - - // assembling create stable sql - stable_cmd->sql = httpAddToSqlCmdBufferNoTerminal(pContext, "create table if not exists %s.%s(ts timestamp", db, - httpGetCmdsString(pContext, table_cmd->stable)); - for (int32_t i = 0; i < fieldsSize; ++i) { - cJSON *field = cJSON_GetArrayItem(fields, i); - char * field_type = "double"; - if (field->type == cJSON_String) - field_type = "binary(32)"; - else if (field->type == cJSON_False || field->type == cJSON_True) - field_type = "tinyint"; - else { - } - - char *field_name = field->string; - httpAddToSqlCmdBufferNoTerminal(pContext, ",f_%s %s", field_name, field_type); - } - httpAddToSqlCmdBufferNoTerminal(pContext, ") tags("); - - for (int32_t i = 0; i < orderTagsLen; ++i) { - cJSON *tag = orderedTags[i]; - char * tag_type = "bigint"; - if (tag->type == cJSON_String) - tag_type = "binary(32)"; - else if (tag->type == cJSON_False || tag->type == cJSON_True) - tag_type = "tinyint"; - else { - } - - char *tag_name = tag->string; - if (i != orderTagsLen - 1) - httpAddToSqlCmdBufferNoTerminal(pContext, "t_%s %s,", tag_name, tag_type); - else - httpAddToSqlCmdBuffer(pContext, "t_%s %s)", tag_name, tag_type); - } - - // assembling insert sql - table_cmd->sql = httpAddToSqlCmdBufferNoTerminal(pContext, "import into %s.%s using %s.%s tags(", db, - httpGetCmdsString(pContext, table_cmd->table), db, - httpGetCmdsString(pContext, table_cmd->stable)); - for (int32_t i = 0; i < orderTagsLen; ++i) { - cJSON *tag = orderedTags[i]; - if (i != orderTagsLen - 1) { - if (tag->type == cJSON_Number) - httpAddToSqlCmdBufferNoTerminal(pContext, "%" PRId64 ",", tag->valueint); - else if (tag->type == cJSON_String) - httpAddToSqlCmdBufferNoTerminal(pContext, "'%s',", tag->valuestring); - else if (tag->type == cJSON_False) - httpAddToSqlCmdBufferNoTerminal(pContext, "0,"); - else if (tag->type == cJSON_True) - httpAddToSqlCmdBufferNoTerminal(pContext, "1,"); - else { - httpAddToSqlCmdBufferNoTerminal(pContext, "NULL,"); - } - } else { - if (tag->type == cJSON_Number) - httpAddToSqlCmdBufferNoTerminal(pContext, "%" PRId64 ")", tag->valueint); - else if (tag->type == cJSON_String) - httpAddToSqlCmdBufferNoTerminal(pContext, "'%s')", tag->valuestring); - else if (tag->type == cJSON_False) - httpAddToSqlCmdBufferNoTerminal(pContext, "0)"); - else if (tag->type == cJSON_True) - httpAddToSqlCmdBufferNoTerminal(pContext, "1)"); - else { - httpAddToSqlCmdBufferNoTerminal(pContext, "NULL)"); - } - } - } - - httpAddToSqlCmdBufferNoTerminal(pContext, " values(%" PRId64 ",", timestamp->valueint); - for (int32_t i = 0; i < fieldsSize; ++i) { - cJSON *field = cJSON_GetArrayItem(fields, i); - if (i != fieldsSize - 1) { - if (field->type == cJSON_Number) - httpAddToSqlCmdBufferNoTerminal(pContext, "%lf,", field->valuedouble); - else if (field->type == cJSON_String) - httpAddToSqlCmdBufferNoTerminal(pContext, "'%s',", field->valuestring); - else if (field->type == cJSON_False) - httpAddToSqlCmdBufferNoTerminal(pContext, "0,"); - else if (field->type == cJSON_True) - httpAddToSqlCmdBufferNoTerminal(pContext, "1,"); - else { - httpAddToSqlCmdBufferNoTerminal(pContext, "NULL,"); - } - } else { - if (field->type == cJSON_Number) - httpAddToSqlCmdBuffer(pContext, "%lf)", field->valuedouble); - else if (field->type == cJSON_String) - httpAddToSqlCmdBuffer(pContext, "'%s')", field->valuestring); - else if (field->type == cJSON_False) - httpAddToSqlCmdBuffer(pContext, "0)"); - else if (field->type == cJSON_True) - httpAddToSqlCmdBuffer(pContext, "1)"); - else { - httpAddToSqlCmdBuffer(pContext, "NULL)"); - } - } - } - - return true; -} - -/** - * request from telegraf 1.7.0 - * single request: - { - "fields": { - "field_1": 30, - "field_2": 4, - "field_N": 59, - "n_images": 660 - }, - "name": "docker", - "tags": { - "host": "raynor" - }, - "timestamp": 1458229140 - } - * multiple request: - { - "metrics": [ - { - "fields": { - "field_1": 30, - "field_2": 4, - "field_N": 59, - "n_images": 660 - }, - "name": "docker", - "tags": { - "host": "raynor" - }, - "timestamp": 1458229140 - }, - { - "fields": { - "field_1": 30, - "field_2": 4, - "field_N": 59, - "n_images": 660 - }, - "name": "docker", - "tags": { - "host": "raynor" - },orderTagsLen - "timestamp": 1458229140 - } - ] - } - */ -bool tgProcessQueryRequest(HttpContext *pContext, char *db) { - httpDebug("context:%p, fd:%d, process telegraf query msg", pContext, pContext->fd); - - char *filter = pContext->parser->body.str; - if (filter == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_MSG_INPUT); - return false; - } - - cJSON *root = cJSON_Parse(filter); - if (root == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_INVALID_JSON); - return false; - } - - cJSON *metrics = cJSON_GetObjectItem(root, "metrics"); - if (metrics != NULL) { - int32_t size = cJSON_GetArraySize(metrics); - httpDebug("context:%p, fd:%d, multiple metrics:%d at one time", pContext, pContext->fd, size); - if (size <= 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRICS_NULL); - cJSON_Delete(root); - return false; - } - - int32_t cmdSize = size * 2 + 1; - if (cmdSize > HTTP_MAX_CMD_SIZE) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_TG_METRICS_SIZE); - cJSON_Delete(root); - return false; - } - - if (!httpMallocMultiCmds(pContext, cmdSize, HTTP_BUFFER_SIZE)) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); - cJSON_Delete(root); - return false; - } - - HttpSqlCmd *cmd = httpNewSqlCmd(pContext); - if (cmd == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); - cJSON_Delete(root); - return false; - } - cmd->cmdType = HTTP_CMD_TYPE_CREATE_DB; - cmd->cmdReturnType = HTTP_CMD_RETURN_TYPE_NO_RETURN; - cmd->sql = httpAddToSqlCmdBuffer(pContext, "create database if not exists %s", db); - - for (int32_t i = 0; i < size; i++) { - cJSON *metric = cJSON_GetArrayItem(metrics, i); - if (metric != NULL) { - if (!tgProcessSingleMetric(pContext, metric, db)) { - cJSON_Delete(root); - return false; - } - } - } - } else { - httpDebug("context:%p, fd:%d, single metric", pContext, pContext->fd); - - if (!httpMallocMultiCmds(pContext, 3, HTTP_BUFFER_SIZE)) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); - cJSON_Delete(root); - return false; - } - - HttpSqlCmd *cmd = httpNewSqlCmd(pContext); - if (cmd == NULL) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_ENOUGH_MEMORY); - cJSON_Delete(root); - return false; - } - cmd->cmdType = HTTP_CMD_TYPE_CREATE_DB; - cmd->cmdReturnType = HTTP_CMD_RETURN_TYPE_NO_RETURN; - cmd->sql = httpAddToSqlCmdBuffer(pContext, "create database if not exists %s", db); - - if (!tgProcessSingleMetric(pContext, root, db)) { - cJSON_Delete(root); - return false; - } - } - - cJSON_Delete(root); - - pContext->reqType = HTTP_REQTYPE_MULTI_SQL; - pContext->encodeMethod = &tgQueryMethod; - pContext->multiCmds->pos = 2; - - return true; -} - -bool tgProcessRquest(struct HttpContext *pContext) { - if (strlen(pContext->user) == 0 || strlen(pContext->pass) == 0) { - httpSendErrorResp(pContext, TSDB_CODE_HTTP_NO_AUTH_INFO); - return false; - } - - char *db = tgGetDbFromUrl(pContext); - if (db == NULL) { - return false; - } - - return tgProcessQueryRequest(pContext, db); -} diff --git a/src/plugins/http/src/httpTgJson.c b/src/plugins/http/src/httpTgJson.c deleted file mode 100644 index 603092f09d..0000000000 --- a/src/plugins/http/src/httpTgJson.c +++ /dev/null @@ -1,151 +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 "taosmsg.h" -#include "httpLog.h" -#include "httpJson.h" -#include "httpResp.h" -#include "httpTgHandle.h" -#include "httpTgJson.h" - -void tgInitQueryJson(HttpContext *pContext) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - httpInitJsonBuf(jsonBuf, pContext); - httpWriteJsonBufHead(jsonBuf); - - // array begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonObjStt); - - httpJsonPairHead(jsonBuf, "metrics", 7); - - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonArrStt); -} - -void tgCleanQueryJson(HttpContext *pContext) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - // array end - httpJsonToken(jsonBuf, JsonArrEnd); - httpJsonToken(jsonBuf, JsonObjEnd); - - httpWriteJsonBufEnd(jsonBuf); -} - -void tgStartQueryJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - // object begin - httpJsonItemToken(jsonBuf); - httpJsonToken(jsonBuf, JsonObjStt); - - // data - httpJsonItemToken(jsonBuf); - httpJsonPair(jsonBuf, "metric", 6, httpGetCmdsString(pContext, cmd->stable), - (int32_t)strlen(httpGetCmdsString(pContext, cmd->metric))); - - httpJsonItemToken(jsonBuf); - httpJsonPair(jsonBuf, "stable", 6, httpGetCmdsString(pContext, cmd->stable), - (int32_t)strlen(httpGetCmdsString(pContext, cmd->stable))); - - httpJsonItemToken(jsonBuf); - httpJsonPair(jsonBuf, "table", 5, httpGetCmdsString(pContext, cmd->table), - (int32_t)strlen(httpGetCmdsString(pContext, cmd->table))); - - httpJsonItemToken(jsonBuf); - httpJsonPair(jsonBuf, "timestamp", 9, httpGetCmdsString(pContext, cmd->timestamp), - (int32_t)strlen(httpGetCmdsString(pContext, cmd->timestamp))); // hack way -} - -void tgStopQueryJson(HttpContext *pContext, HttpSqlCmd *cmd) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - // data - httpJsonItemToken(jsonBuf); - httpJsonPairStatus(jsonBuf, cmd->code); - - // object end - httpJsonToken(jsonBuf, JsonObjEnd); -} - -void tgBuildSqlAffectRowsJson(HttpContext *pContext, HttpSqlCmd *cmd, int32_t affect_rows) { - JsonBuf *jsonBuf = httpMallocJsonBuf(pContext); - if (jsonBuf == NULL) return; - - // data - httpJsonPairIntVal(jsonBuf, "affected_rows", 13, affect_rows); -} - -bool tgCheckFinished(struct HttpContext *pContext, HttpSqlCmd *cmd, int32_t code) { - HttpSqlCmds *multiCmds = pContext->multiCmds; - httpDebug("context:%p, fd:%d, check telegraf command, code:%s, state:%d, type:%d, rettype:%d, tags:%d", pContext, - pContext->fd, tstrerror(code), cmd->cmdState, cmd->cmdType, cmd->cmdReturnType, cmd->tagNum); - - if (cmd->cmdType == HTTP_CMD_TYPE_INSERT) { - if (cmd->cmdState == HTTP_CMD_STATE_NOT_RUN_YET) { - if (code == TSDB_CODE_MND_DB_NOT_SELECTED || code == TSDB_CODE_MND_INVALID_DB) { - cmd->cmdState = HTTP_CMD_STATE_RUN_FINISHED; - if (multiCmds->cmds[0].cmdState == HTTP_CMD_STATE_NOT_RUN_YET) { - multiCmds->pos = (int16_t)-1; - httpDebug("context:%p, fd:%d, import failed, try create database", pContext, pContext->fd); - return false; - } - } else if (code == TSDB_CODE_MND_INVALID_TABLE_NAME) { - cmd->cmdState = HTTP_CMD_STATE_RUN_FINISHED; - if (multiCmds->cmds[multiCmds->pos - 1].cmdState == HTTP_CMD_STATE_NOT_RUN_YET) { - multiCmds->pos = (int16_t)(multiCmds->pos - 2); - httpDebug("context:%p, fd:%d, import failed, try create stable", pContext, pContext->fd); - return false; - } - } else { - } - } else { - } - } else if (cmd->cmdType == HTTP_CMD_TYPE_CREATE_DB) { - cmd->cmdState = HTTP_CMD_STATE_RUN_FINISHED; - httpDebug("context:%p, fd:%d, code:%s, create database failed", pContext, pContext->fd, tstrerror(code)); - } else if (cmd->cmdType == HTTP_CMD_TYPE_CREATE_STBALE) { - cmd->cmdState = HTTP_CMD_STATE_RUN_FINISHED; - httpDebug("context:%p, fd:%d, code:%s, create stable failed", pContext, pContext->fd, tstrerror(code)); - } else { - } - - return true; -} - -void tgSetNextCmd(struct HttpContext *pContext, HttpSqlCmd *cmd, int32_t code) { - HttpSqlCmds *multiCmds = pContext->multiCmds; - httpDebug("context:%p, fd:%d, get telegraf next command, pos:%d, code:%s, state:%d, type:%d, rettype:%d, tags:%d", - pContext, pContext->fd, multiCmds->pos, tstrerror(code), cmd->cmdState, cmd->cmdType, cmd->cmdReturnType, - cmd->tagNum); - - if (cmd->cmdType == HTTP_CMD_TYPE_INSERT) { - multiCmds->pos = (int16_t)(multiCmds->pos + 2); - } else if (cmd->cmdType == HTTP_CMD_TYPE_CREATE_DB) { - multiCmds->pos++; - } else if (cmd->cmdType == HTTP_CMD_TYPE_CREATE_STBALE) { - multiCmds->pos++; - } else { - multiCmds->pos++; - } -} diff --git a/src/plugins/http/src/httpUtil.c b/src/plugins/http/src/httpUtil.c deleted file mode 100644 index 12f3a6efd8..0000000000 --- a/src/plugins/http/src/httpUtil.c +++ /dev/null @@ -1,497 +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 "httpUtil.h" -#include "../../../../include/client/taos.h" -#include "httpInt.h" -#include "httpResp.h" -#include "httpSql.h" -#include "os.h" -#include "tmd5.h" -#include "ttoken.h" - -bool httpCheckUsedbSql(char *sql) { - if (strstr(sql, "use ") != NULL) { - return true; - } - return false; -} - -bool httpCheckAlterSql(char *sql) { - int32_t index = 0; - - do { - SStrToken t0 = tStrGetToken(sql, &index, false); - if (t0.type != TK_LP) { - return t0.type == TK_ALTER; - } - } while (1); -} - -void httpTimeToString(int32_t t, char *buf, int32_t buflen) { - memset(buf, 0, (size_t)buflen); - char ts[32] = {0}; - - struct tm *ptm; - time_t tt = t / 1000; - ptm = localtime(&tt); - strftime(ts, 31, "%Y-%m-%d %H:%M:%S", ptm); - sprintf(buf, "%s.%03d", ts, t % 1000); -} - -int32_t httpAddToSqlCmdBuffer(HttpContext *pContext, const char *const format, ...) { - HttpSqlCmds *cmd = pContext->multiCmds; - if (cmd->buffer == NULL) return -1; - - int32_t remainLength = cmd->bufferSize - cmd->bufferPos; - if (remainLength < 4096) { - if (!httpReMallocMultiCmdsBuffer(pContext, cmd->bufferSize * 2)) return -1; - } - - char * buffer = cmd->buffer + cmd->bufferPos; - int32_t len = 0; - - va_list argpointer; - va_start(argpointer, format); - len += vsnprintf(buffer, (size_t)remainLength, format, argpointer); - va_end(argpointer); - - if (cmd->bufferPos + len + 1 >= cmd->bufferSize) { - return -1; - } - - cmd->buffer[cmd->bufferPos + len] = 0; - cmd->bufferPos = cmd->bufferPos + len + 1; - - remainLength = cmd->bufferSize - cmd->bufferPos; - if (remainLength < 4096) { - if (!httpReMallocMultiCmdsBuffer(pContext, cmd->bufferSize * 2)) return -1; - } - - return (int32_t)(buffer - cmd->buffer); -} - -int32_t httpAddToSqlCmdBufferNoTerminal(HttpContext *pContext, const char *const format, ...) { - HttpSqlCmds *cmd = pContext->multiCmds; - if (cmd->buffer == NULL) return -1; - - int32_t remainLength = cmd->bufferSize - cmd->bufferPos; - if (remainLength < 4096) { - if (!httpReMallocMultiCmdsBuffer(pContext, cmd->bufferSize * 2)) return -1; - } - - char * buffer = cmd->buffer + cmd->bufferPos; - int32_t len = 0; - - va_list argpointer; - va_start(argpointer, format); - len += vsnprintf(buffer, (size_t)remainLength, format, argpointer); - va_end(argpointer); - - if (cmd->bufferPos + len + 1 >= cmd->bufferSize) { - return -1; - } - - cmd->bufferPos = cmd->bufferPos + len; - - remainLength = cmd->bufferSize - cmd->bufferPos; - if (remainLength < 4096) { - if (!httpReMallocMultiCmdsBuffer(pContext, cmd->bufferSize * 2)) return -1; - } - - return (int32_t)(buffer - cmd->buffer); -} - -int32_t httpAddToSqlCmdBufferTerminal(HttpContext *pContext) { - HttpSqlCmds *cmd = pContext->multiCmds; - if (cmd->buffer == NULL) return -1; - - int32_t remainLength = cmd->bufferSize - cmd->bufferPos; - if (remainLength < 4096) { - if (!httpReMallocMultiCmdsBuffer(pContext, cmd->bufferSize * 2)) return -1; - } - - char *buffer = cmd->buffer + cmd->bufferPos; - *buffer = 0; - cmd->bufferPos = cmd->bufferPos + 1; - - remainLength = cmd->bufferSize - cmd->bufferPos; - if (remainLength < 4096) { - if (!httpReMallocMultiCmdsBuffer(pContext, cmd->bufferSize * 2)) return -1; - } - - return (int32_t)(buffer - cmd->buffer); -} - -int32_t httpAddToSqlCmdBufferWithSize(HttpContext *pContext, int32_t mallocSize) { - HttpSqlCmds *cmd = pContext->multiCmds; - if (cmd->buffer == NULL) return -1; - - if (cmd->bufferPos + mallocSize >= cmd->bufferSize) { - if (!httpReMallocMultiCmdsBuffer(pContext, cmd->bufferSize * 2)) return -1; - } - - char *buffer = cmd->buffer + cmd->bufferPos; - memset(cmd->buffer + cmd->bufferPos, 0, (size_t)mallocSize); - cmd->bufferPos = cmd->bufferPos + mallocSize; - - return (int32_t)(buffer - cmd->buffer); -} - -bool httpMallocMultiCmds(HttpContext *pContext, int32_t cmdSize, int32_t bufferSize) { - if (cmdSize > HTTP_MAX_CMD_SIZE) { - httpError("context:%p, fd:%d, user:%s, mulitcmd size:%d large then %d", pContext, pContext->fd, pContext->user, - cmdSize, HTTP_MAX_CMD_SIZE); - return false; - } - - if (pContext->multiCmds == NULL) { - pContext->multiCmds = (HttpSqlCmds *)malloc(sizeof(HttpSqlCmds)); - if (pContext->multiCmds == NULL) { - httpError("context:%p, fd:%d, user:%s, malloc multiCmds error", pContext, pContext->fd, pContext->user); - return false; - } - memset(pContext->multiCmds, 0, sizeof(HttpSqlCmds)); - } - - HttpSqlCmds *multiCmds = pContext->multiCmds; - if (multiCmds->cmds == NULL || cmdSize > multiCmds->maxSize) { - free(multiCmds->cmds); - multiCmds->cmds = (HttpSqlCmd *)malloc((size_t)cmdSize * sizeof(HttpSqlCmd)); - if (multiCmds->cmds == NULL) { - httpError("context:%p, fd:%d, user:%s, malloc cmds:%d error", pContext, pContext->fd, pContext->user, cmdSize); - return false; - } - multiCmds->maxSize = (int16_t)cmdSize; - } - - if (multiCmds->buffer == NULL || bufferSize > multiCmds->bufferSize) { - free(multiCmds->buffer); - multiCmds->buffer = (char *)malloc((size_t)bufferSize); - if (multiCmds->buffer == NULL) { - httpError("context:%p, fd:%d, user:%s, malloc buffer:%d error", pContext, pContext->fd, pContext->user, - bufferSize); - return false; - } - multiCmds->bufferSize = bufferSize; - } - - multiCmds->pos = 0; - multiCmds->size = 0; - multiCmds->bufferPos = 0; - memset(multiCmds->cmds, 0, (size_t)multiCmds->maxSize * sizeof(HttpSqlCmd)); - - return true; -} - -bool httpReMallocMultiCmdsSize(HttpContext *pContext, int32_t cmdSize) { - HttpSqlCmds *multiCmds = pContext->multiCmds; - - if (cmdSize <= 0 || cmdSize > HTTP_MAX_CMD_SIZE) { - httpError("context:%p, fd:%d, user:%s, mulitcmd size:%d large then %d", pContext, pContext->fd, pContext->user, - cmdSize, HTTP_MAX_CMD_SIZE); - return false; - } - - HttpSqlCmd *new_cmds = (HttpSqlCmd *)realloc(multiCmds->cmds, (size_t)cmdSize * sizeof(HttpSqlCmd)); - if (new_cmds == NULL && multiCmds->cmds) { - free(multiCmds->cmds); - } - multiCmds->cmds = new_cmds; - if (multiCmds->cmds == NULL) { - httpError("context:%p, fd:%d, user:%s, malloc cmds:%d error", pContext, pContext->fd, pContext->user, cmdSize); - return false; - } - memset(multiCmds->cmds + multiCmds->maxSize, 0, (size_t)(cmdSize - multiCmds->maxSize) * sizeof(HttpSqlCmd)); - multiCmds->maxSize = (int16_t)cmdSize; - - return true; -} - -bool httpReMallocMultiCmdsBuffer(HttpContext *pContext, int32_t bufferSize) { - HttpSqlCmds *multiCmds = pContext->multiCmds; - - if (bufferSize <= 0 || bufferSize > HTTP_MAX_BUFFER_SIZE) { - httpError("context:%p, fd:%d, user:%s, mulitcmd buffer size:%d large then %d", pContext, pContext->fd, - pContext->user, bufferSize, HTTP_MAX_BUFFER_SIZE); - return false; - } - - char *new_buffer = (char *)realloc(multiCmds->buffer, (size_t)bufferSize); - if (new_buffer == NULL && multiCmds->buffer) { - free(multiCmds->buffer); - } - multiCmds->buffer = new_buffer; - if (multiCmds->buffer == NULL) { - httpError("context:%p, fd:%d, user:%s, malloc buffer:%d error", pContext, pContext->fd, pContext->user, bufferSize); - return false; - } - memset(multiCmds->buffer + multiCmds->bufferSize, 0, (size_t)(bufferSize - multiCmds->bufferSize)); - multiCmds->bufferSize = bufferSize; - - return true; -} - -void httpFreeMultiCmds(HttpContext *pContext) { - if (pContext->multiCmds != NULL) { - if (pContext->multiCmds->buffer != NULL) free(pContext->multiCmds->buffer); - if (pContext->multiCmds->cmds != NULL) free(pContext->multiCmds->cmds); - free(pContext->multiCmds); - pContext->multiCmds = NULL; - } -} - -JsonBuf *httpMallocJsonBuf(HttpContext *pContext) { - if (pContext->jsonBuf == NULL) { - pContext->jsonBuf = (JsonBuf *)malloc(sizeof(JsonBuf)); - if (pContext->jsonBuf == NULL) { - return NULL; - } - - memset(pContext->jsonBuf, 0, sizeof(JsonBuf)); - } - - if (!pContext->jsonBuf->pContext) { - pContext->jsonBuf->pContext = pContext; - } - - return pContext->jsonBuf; -} - -void httpFreeJsonBuf(HttpContext *pContext) { - if (pContext->jsonBuf != NULL) { - free(pContext->jsonBuf); - pContext->jsonBuf = 0; - } -} - -bool httpCompareMethod(HttpDecodeMethod *pSrc, HttpDecodeMethod *pCmp) { - if (strcmp(pSrc->module, pCmp->module) != 0) { - return false; - } - return true; -} - -void httpAddMethod(HttpServer *pServer, HttpDecodeMethod *pMethod) { - int32_t pos = 0; - for (pos = 0; pos < pServer->methodScannerLen; ++pos) { - if (httpCompareMethod(pServer->methodScanner[pos], pMethod)) { - break; - } - } - - if (pos == pServer->methodScannerLen && pServer->methodScannerLen < HTTP_METHOD_SCANNER_SIZE) { - pServer->methodScanner[pos] = pMethod; - pServer->methodScannerLen++; - } -} - -HttpSqlCmd *httpNewSqlCmd(HttpContext *pContext) { - HttpSqlCmds *multiCmds = pContext->multiCmds; - if (multiCmds->size >= multiCmds->maxSize) { - if (!httpReMallocMultiCmdsSize(pContext, 2 * multiCmds->maxSize)) return NULL; - } - - HttpSqlCmd *cmd = multiCmds->cmds + multiCmds->size++; - cmd->cmdType = HTTP_CMD_TYPE_UN_SPECIFIED; - cmd->cmdReturnType = HTTP_CMD_RETURN_TYPE_WITH_RETURN; - cmd->cmdState = HTTP_CMD_STATE_NOT_RUN_YET; - - return cmd; -} - -HttpSqlCmd *httpCurrSqlCmd(HttpContext *pContext) { - HttpSqlCmds *multiCmds = pContext->multiCmds; - if (multiCmds->size == 0) return NULL; - if (multiCmds->size > multiCmds->maxSize) return NULL; - - return multiCmds->cmds + multiCmds->size - 1; -} - -int32_t httpNextSqlCmdPos(HttpContext *pContext) { - HttpSqlCmds *multiCmds = pContext->multiCmds; - return multiCmds->size; -} - -void httpTrimTableName(char *name) { - for (int32_t i = 0; name[i] != 0; i++) { - if (name[i] == ' ' || name[i] == ':' || name[i] == '.' || name[i] == '-' || name[i] == '/' || name[i] == '\'') - name[i] = '_'; - if (i == TSDB_TABLE_NAME_LEN) { - name[i] = 0; - break; - } - } -} - -int32_t httpShrinkTableName(HttpContext *pContext, int32_t pos, char *name) { - int32_t len = 0; - for (int32_t i = 0; name[i] != 0; i++) { - if (name[i] == ' ' || name[i] == ':' || name[i] == '.' || name[i] == '-' || name[i] == '/' || name[i] == '\'' || - name[i] == '\"') - name[i] = '_'; - len++; - } - - if (len < TSDB_TABLE_NAME_LEN - 1) { - return pos; - } - - MD5_CTX context; - MD5Init(&context); - MD5Update(&context, (uint8_t *)name, (uint32_t)len); - MD5Final(&context); - - int32_t table_name = httpAddToSqlCmdBuffer( - pContext, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", context.digest[0], - context.digest[1], context.digest[2], context.digest[3], context.digest[4], context.digest[5], context.digest[6], - context.digest[7], context.digest[8], context.digest[9], context.digest[10], context.digest[11], - context.digest[12], context.digest[13], context.digest[14], context.digest[15]); - - if (table_name != -1) { - httpGetCmdsString(pContext, table_name)[0] = 't'; - } - - return table_name; -} - -char *httpGetCmdsString(HttpContext *pContext, int32_t pos) { - HttpSqlCmds *multiCmds = pContext->multiCmds; - if (pos < 0 || pos >= multiCmds->bufferSize) { - return ""; - } - - return multiCmds->buffer + pos; -} - -int32_t httpGzipDeCompress(char *srcData, int32_t nSrcData, char *destData, int32_t *nDestData) { - int32_t err = 0; - z_stream gzipStream = {0}; - - static char dummyHead[2] = { - 0x8 + 0x7 * 0x10, - (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF, - }; - - gzipStream.zalloc = (alloc_func)0; - gzipStream.zfree = (free_func)0; - gzipStream.opaque = (voidpf)0; - gzipStream.next_in = (Bytef *)srcData; - gzipStream.avail_in = 0; - gzipStream.next_out = (Bytef *)destData; - if (inflateInit2(&gzipStream, 47) != Z_OK) { - return -1; - } - - while (gzipStream.total_out < *nDestData && gzipStream.total_in < nSrcData) { - gzipStream.avail_in = gzipStream.avail_out = nSrcData; // 1 - if ((err = inflate(&gzipStream, Z_NO_FLUSH)) == Z_STREAM_END) { - break; - } - - if (err != Z_OK) { - if (err == Z_DATA_ERROR) { - gzipStream.next_in = (Bytef *)dummyHead; - gzipStream.avail_in = sizeof(dummyHead); - if ((err = inflate(&gzipStream, Z_NO_FLUSH)) != Z_OK) { - return -2; - } - } else { - return -3; - } - } - } - - if (inflateEnd(&gzipStream) != Z_OK) { - return -4; - } - *nDestData = (int32_t)gzipStream.total_out; - - return 0; -} - -int32_t httpGzipCompressInit(HttpContext *pContext) { - pContext->gzipStream.zalloc = (alloc_func)0; - pContext->gzipStream.zfree = (free_func)0; - pContext->gzipStream.opaque = (voidpf)0; - if (deflateInit2(&pContext->gzipStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY) != - Z_OK) { - return -1; - } - - return 0; -} - -int32_t httpGzipCompress(HttpContext *pContext, char *srcData, int32_t nSrcData, char *destData, int32_t *nDestData, - bool isTheLast) { - int32_t err = 0; - int32_t lastTotalLen = (int32_t)(pContext->gzipStream.total_out); - pContext->gzipStream.next_in = (Bytef *)srcData; - pContext->gzipStream.avail_in = (uLong)nSrcData; - pContext->gzipStream.next_out = (Bytef *)destData; - pContext->gzipStream.avail_out = (uLong)(*nDestData); - - while (pContext->gzipStream.avail_in != 0) { - if (deflate(&pContext->gzipStream, Z_FULL_FLUSH) != Z_OK) { - return -1; - } - - int32_t cacheLen = (int32_t)(pContext->gzipStream.total_out - lastTotalLen); - if (cacheLen >= *nDestData) { - return -2; - } - } - - if (pContext->gzipStream.avail_in != 0) { - return pContext->gzipStream.avail_in; - } - - if (isTheLast) { - for (;;) { - if ((err = deflate(&pContext->gzipStream, Z_FINISH)) == Z_STREAM_END) { - break; - } - if (err != Z_OK) { - return -3; - } - } - - if (deflateEnd(&pContext->gzipStream) != Z_OK) { - return -4; - } - } - - *nDestData = (int32_t)(pContext->gzipStream.total_out) - lastTotalLen; - return 0; -} - -bool httpUrlMatch(HttpContext *pContext, int32_t pos, char *cmp) { - HttpParser *pParser = pContext->parser; - - if (pos < 0 || pos >= HTTP_MAX_URL) { - return false; - } - - if (pParser->path[pos].pos <= 0) { - return false; - } - - if (strcmp(pParser->path[pos].str, cmp) != 0) { - return false; - } - - return true; -} diff --git a/src/plugins/mqtt/CMakeLists.txt b/src/plugins/mqtt/CMakeLists.txt deleted file mode 100644 index 0815121385..0000000000 --- a/src/plugins/mqtt/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -INCLUDE_DIRECTORIES(inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/client/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/src/query/inc) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/MQTT-C/include) -INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/MQTT-C/examples/templates) -AUX_SOURCE_DIRECTORY(src SRC) - -IF (TD_LINUX) - ADD_LIBRARY(mqtt ${SRC}) - TARGET_LINK_LIBRARIES(mqtt cJson mqttc) - - IF (TD_SOMODE_STATIC) - TARGET_LINK_LIBRARIES(mqtt taos_static) - ELSE () - TARGET_LINK_LIBRARIES(mqtt taos) - ENDIF () -ENDIF () - -IF (TD_DARWIN) - ADD_LIBRARY(mqtt ${SRC}) - TARGET_LINK_LIBRARIES(mqtt cJson mqttc) - - IF (TD_SOMODE_STATIC) - TARGET_LINK_LIBRARIES(mqtt taos_static) - ELSE () - TARGET_LINK_LIBRARIES(mqtt taos) - ENDIF () -ENDIF () - diff --git a/src/plugins/mqtt/inc/mqttInit.h b/src/plugins/mqtt/inc/mqttInit.h deleted file mode 100644 index 81a9a39a2c..0000000000 --- a/src/plugins/mqtt/inc/mqttInit.h +++ /dev/null @@ -1,77 +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_MQTT_INIT_H -#define TDENGINE_MQTT_INIT_H -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @file - * A simple subscriber program that performs automatic reconnections. - */ -#include "mqtt.h" - -#define QOS 1 -#define TIMEOUT 10000L -#define MQTT_SEND_BUF_SIZE 102400 -#define MQTT_RECV_BUF_SIZE 102400 - -/** - * @brief A structure that I will use to keep track of some data needed - * to setup the connection to the broker. - * - * An instance of this struct will be created in my \c main(). Then, whenever - * \ref mqttReconnectClient is called, this instance will be passed. - */ -typedef struct SMqttReconnectState { - uint8_t* sendbuf; - size_t sendbufsz; - uint8_t* recvbuf; - size_t recvbufsz; -} SMqttReconnectState; - -/** - * @brief My reconnect callback. It will reestablish the connection whenever - * an error occurs. - */ -void mqttReconnectClient(struct mqtt_client* client, void** reconnect_state_vptr); - -/** - * @brief The function will be called whenever a PUBLISH message is received. - */ -void mqttPublishCallback(void** unused, struct mqtt_response_publish* published); - -/** - * @brief The client's refresher. This function triggers back-end routines to - * handle ingress/egress traffic to the broker. - * - * @note All this function needs to do is call \ref __mqtt_recv and - * \ref __mqtt_send every so often. I've picked 100 ms meaning that - * client ingress/egress traffic will be handled every 100 ms. - */ -void* mqttClientRefresher(void* client); - -/** - * @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit. - */ -void mqttCleanupRes(int status, int sockfd, pthread_t* client_daemon); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/plugins/mqtt/inc/mqttLog.h b/src/plugins/mqtt/inc/mqttLog.h deleted file mode 100644 index e186b81112..0000000000 --- a/src/plugins/mqtt/inc/mqttLog.h +++ /dev/null @@ -1,30 +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_MQTT_LOG_H -#define TDENGINE_MQTT_LOG_H - -#include "tlog.h" - -extern int32_t mqttDebugFlag; - -#define mqttFatal(...) { if (mqttDebugFlag & DEBUG_FATAL) { taosPrintLog("MQT FATAL ", 255, __VA_ARGS__); }} -#define mqttError(...) { if (mqttDebugFlag & DEBUG_ERROR) { taosPrintLog("MQT ERROR ", 255, __VA_ARGS__); }} -#define mqttWarn(...) { if (mqttDebugFlag & DEBUG_WARN) { taosPrintLog("MQT WARN ", 255, __VA_ARGS__); }} -#define mqttInfo(...) { if (mqttDebugFlag & DEBUG_INFO) { taosPrintLog("MQT ", 255, __VA_ARGS__); }} -#define mqttDebug(...) { if (mqttDebugFlag & DEBUG_DEBUG) { taosPrintLog("MQT ", mqttDebugFlag, __VA_ARGS__); }} -#define mqttTrace(...) { if (mqttDebugFlag & DEBUG_TRACE) { taosPrintLog("MQT ", mqttDebugFlag, __VA_ARGS__); }} - -#endif diff --git a/src/plugins/mqtt/inc/mqttPayload.h b/src/plugins/mqtt/inc/mqttPayload.h deleted file mode 100644 index 12a714afac..0000000000 --- a/src/plugins/mqtt/inc/mqttPayload.h +++ /dev/null @@ -1,29 +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_MQTT_PLYLOAD_H -#define TDENGINE_MQTT_PLYLOAD_H - -#ifdef __cplusplus -extern "C" { -#endif - -char* mqttConverJsonToSql(char* json, int maxSize); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/plugins/mqtt/src/mqttPayload.c b/src/plugins/mqtt/src/mqttPayload.c deleted file mode 100644 index 1af8b02fad..0000000000 --- a/src/plugins/mqtt/src/mqttPayload.c +++ /dev/null @@ -1,159 +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 "cJSON.h" -#include "mqttLog.h" -#include "mqttPayload.h" - -// subscribe message like this - -/* -/test { - "timestamp": 1599121290, - "gateway": { - "name": "AcuLink 810 Gateway", - "model": "AcuLink810-868", - "serial": "S8P20200207" - }, - "device": { - "name": "Acuvim L V3 .221", - "model": "Acuvim-L-V3", - "serial": "221", - "online": true, - "readings": [ - { - "param": "Freq_Hz", - "value": "59.977539", - "unit": "Hz" - }, - { - "param": "Va_V", - "value": "122.002907", - "unit": "V" - }, - { - "param": "DI4", - "value": "5.000000", - "unit": "" - } - ] - } -} -*/ - -// send msg cmd -// mosquitto_pub -h test.mosquitto.org -t "/test" -m '{"timestamp": 1599121290,"gateway": {"name": "AcuLink 810 Gateway","model": "AcuLink810-868","serial": "S8P20200207"},"device": {"name": "Acuvim L V3 .221","model": "Acuvim-L-V3","serial": "221","online": true,"readings": [{"param": "Freq_Hz","value": "59.977539","unit": "Hz"},{"param": "Va_V","value": "122.002907","unit": "V"},{"param": "DI4","value": "5.000000","unit": ""}]}}' - -/* - * This is an example, this function needs to be implemented in order to parse the json file into a sql statement - * Note that you need to create a super table and database before writing data - * In this case: - * create database mqttdb; - * create table mqttdb.devices(ts timestamp, value bigint) tags(name binary(32), model binary(32), serial binary(16), param binary(16), unit binary(16)); - */ - -char* mqttConverJsonToSql(char* json, int maxSize) { - // const int32_t maxSize = 10240; - maxSize *= 5; - char* sql = malloc(maxSize); - - cJSON* root = cJSON_Parse(json); - if (root == NULL) { - mqttError("failed to parse msg, invalid json format"); - goto MQTT_PARSE_OVER; - } - - cJSON* timestamp = cJSON_GetObjectItem(root, "timestamp"); - if (!timestamp || timestamp->type != cJSON_Number) { - mqttError("failed to parse msg, timestamp not found"); - goto MQTT_PARSE_OVER; - } - - cJSON* device = cJSON_GetObjectItem(root, "device"); - if (!device) { - mqttError("failed to parse msg, device not found"); - goto MQTT_PARSE_OVER; - } - - cJSON* name = cJSON_GetObjectItem(device, "name"); - if (!name || name->type != cJSON_String) { - mqttError("failed to parse msg, name not found"); - goto MQTT_PARSE_OVER; - } - - cJSON* model = cJSON_GetObjectItem(device, "model"); - if (!model || model->type != cJSON_String) { - mqttError("failed to parse msg, model not found"); - goto MQTT_PARSE_OVER; - } - - cJSON* serial = cJSON_GetObjectItem(device, "serial"); - if (!serial || serial->type != cJSON_String) { - mqttError("failed to parse msg, serial not found"); - goto MQTT_PARSE_OVER; - } - - cJSON* readings = cJSON_GetObjectItem(device, "readings"); - if (!readings || readings->type != cJSON_Array) { - mqttError("failed to parse msg, readings not found"); - goto MQTT_PARSE_OVER; - } - - int count = cJSON_GetArraySize(readings); - if (count <= 0) { - mqttError("failed to parse msg, readings size smaller than 0"); - goto MQTT_PARSE_OVER; - } - - int len = snprintf(sql, maxSize, "insert into"); - - for (int i = 0; i < count; ++i) { - cJSON* reading = cJSON_GetArrayItem(readings, i); - if (reading == NULL) continue; - - cJSON* param = cJSON_GetObjectItem(reading, "param"); - if (!param || param->type != cJSON_String) { - mqttError("failed to parse msg, param not found"); - goto MQTT_PARSE_OVER; - } - - cJSON* value = cJSON_GetObjectItem(reading, "value"); - if (!value || value->type != cJSON_String) { - mqttError("failed to parse msg, value not found"); - goto MQTT_PARSE_OVER; - } - - cJSON* unit = cJSON_GetObjectItem(reading, "unit"); - if (!unit || unit->type != cJSON_String) { - mqttError("failed to parse msg, unit not found"); - goto MQTT_PARSE_OVER; - } - - len += snprintf(sql + len, maxSize - len, - " mqttdb.serial_%s_%s using mqttdb.devices tags('%s', '%s', '%s', '%s', '%s') values(%" PRId64 ", %s)", - serial->valuestring, param->valuestring, name->valuestring, model->valuestring, serial->valuestring, - param->valuestring, unit->valuestring, timestamp->valueint * 1000, value->valuestring); - } - - cJSON_free(root); - return sql; - -MQTT_PARSE_OVER: - cJSON_free(root); - free(sql); - return NULL; -} \ No newline at end of file diff --git a/src/plugins/mqtt/src/mqttSystem.c b/src/plugins/mqtt/src/mqttSystem.c deleted file mode 100644 index d0c33d43a8..0000000000 --- a/src/plugins/mqtt/src/mqttSystem.c +++ /dev/null @@ -1,146 +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 "../../../../include/client/taos.h" -#include "mqtt.h" -#include "mqttInit.h" -#include "mqttLog.h" -#include "mqttPayload.h" -#include "os.h" -#include "posix_sockets.h" -#include "taoserror.h" -#include "tglobal.h" -#include "tmqtt.h" - -struct SMqttReconnectState tsMqttStatus = {0}; -struct mqtt_client tsMqttClient = {0}; -static pthread_t tsMqttClientDaemonThread = {0}; -static void* tsMqttConnect = NULL; -static bool tsMqttIsRuning = false; - -int32_t mqttInitSystem() { return 0; } - -int32_t mqttStartSystem() { - tsMqttStatus.sendbufsz = MQTT_SEND_BUF_SIZE; - tsMqttStatus.recvbufsz = MQTT_RECV_BUF_SIZE; - tsMqttStatus.sendbuf = malloc(MQTT_SEND_BUF_SIZE); - tsMqttStatus.recvbuf = malloc(MQTT_RECV_BUF_SIZE); - tsMqttIsRuning = true; - - mqtt_init_reconnect(&tsMqttClient, mqttReconnectClient, &tsMqttStatus, mqttPublishCallback); - if (pthread_create(&tsMqttClientDaemonThread, NULL, mqttClientRefresher, &tsMqttClient)) { - mqttError("mqtt failed to start daemon."); - mqttCleanupRes(EXIT_FAILURE, -1, NULL); - return -1; - } - - mqttInfo("mqtt listening for topic:%s messages", tsMqttTopic); - return 0; -} - -void mqttStopSystem() { - if (tsMqttIsRuning) { - tsMqttIsRuning = false; - tsMqttClient.error = MQTT_ERROR_SOCKET_ERROR; - - taosMsleep(300); - mqttCleanupRes(EXIT_SUCCESS, tsMqttClient.socketfd, &tsMqttClientDaemonThread); - - mqttInfo("mqtt is stopped"); - } -} - -void mqttCleanUpSystem() { - mqttStopSystem(); - mqttInfo("mqtt is cleaned up"); -} - -void mqttPublishCallback(void** unused, struct mqtt_response_publish* published) { - const char* content = published->application_message; - mqttDebug("receive mqtt message, size:%d", (int)published->application_message_size); - - if (tsMqttConnect == NULL) { - tsMqttConnect = taos_connect(NULL, "_root", tsInternalPass, "", 0); - if (tsMqttConnect == NULL) { - mqttError("failed to connect to tdengine, reason:%s", tstrerror(terrno)); - return; - } else { - mqttInfo("successfully connected to the tdengine"); - } - } - - mqttTrace("receive mqtt message, content:%s", content); - - char* sql = mqttConverJsonToSql((char*)content, (int)published->application_message_size); - if (sql != NULL) { - void* res = taos_query(tsMqttConnect, sql); - int code = taos_errno(res); - if (code != 0) { - mqttError("failed to exec sql, reason:%s sql:%s", tstrerror(code), sql); - } else { - mqttTrace("successfully to exec sql:%s", sql); - } - taos_free_result(res); - } else { - mqttError("failed to parse mqtt message"); - } -} - -void* mqttClientRefresher(void* client) { - setThreadName("mqttCliRefresh"); - - while (tsMqttIsRuning) { - mqtt_sync((struct mqtt_client*)client); - taosMsleep(100); - } - - mqttDebug("mqtt quit refresher"); - return NULL; -} - -void mqttCleanupRes(int status, int sockfd, pthread_t* client_daemon) { - mqttInfo("clean up mqtt module"); - if (sockfd != -1) { - close(sockfd); - } - - if (client_daemon != NULL) { - pthread_cancel(*client_daemon); - } -} - -void mqttReconnectClient(struct mqtt_client* client, void** unused) { - mqttInfo("mqtt tries to connect to the mqtt server"); - - if (client->error != MQTT_ERROR_INITIAL_RECONNECT) { - close(client->socketfd); - } - - if (client->error != MQTT_ERROR_INITIAL_RECONNECT) { - mqttError("mqtt client was in error state %s", mqtt_error_str(client->error)); - } - - int sockfd = open_nb_socket(tsMqttHostName, tsMqttPort); - if (sockfd < 0) { - mqttError("mqtt client failed to open socket %s:%s", tsMqttHostName, tsMqttPort); - //mqttCleanupRes(EXIT_FAILURE, sockfd, NULL); - return; - } - - mqtt_reinit(client, sockfd, tsMqttStatus.sendbuf, tsMqttStatus.sendbufsz, tsMqttStatus.recvbuf, tsMqttStatus.recvbufsz); - mqtt_connect(client, tsMqttClientId, NULL, NULL, 0, tsMqttUser, tsMqttPass, MQTT_CONNECT_CLEAN_SESSION, 400); - mqtt_subscribe(client, tsMqttTopic, 0); -} diff --git a/src/sync/CMakeLists.txt b/src/sync/CMakeLists.txt deleted file mode 100644 index 2cd84c7c3f..0000000000 --- a/src/sync/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -INCLUDE_DIRECTORIES(inc) -AUX_SOURCE_DIRECTORY(src SRC) - -LIST(REMOVE_ITEM SRC src/syncArbitrator.c) -ADD_LIBRARY(sync ${SRC}) -TARGET_LINK_LIBRARIES(sync tutil pthread common) - -LIST(APPEND BIN_SRC src/syncArbitrator.c) -LIST(APPEND BIN_SRC src/syncTcp.c) -ADD_EXECUTABLE(tarbitrator ${BIN_SRC}) -TARGET_LINK_LIBRARIES(tarbitrator sync common os tutil) - -#ADD_SUBDIRECTORY(test) diff --git a/src/sync/inc/syncInt.h b/src/sync/inc/syncInt.h deleted file mode 100644 index 411e706fb1..0000000000 --- a/src/sync/inc/syncInt.h +++ /dev/null @@ -1,143 +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_SYNC_INT_H -#define TDENGINE_SYNC_INT_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "syncMsg.h" -#include "twal.h" - -#define sFatal(...) { if (sDebugFlag & DEBUG_FATAL) { taosPrintLog("SYN FATAL ", sDebugFlag, __VA_ARGS__); }} -#define sError(...) { if (sDebugFlag & DEBUG_ERROR) { taosPrintLog("SYN ERROR ", sDebugFlag, __VA_ARGS__); }} -#define sWarn(...) { if (sDebugFlag & DEBUG_WARN) { taosPrintLog("SYN WARN ", sDebugFlag, __VA_ARGS__); }} -#define sInfo(...) { if (sDebugFlag & DEBUG_INFO) { taosPrintLog("SYN ", sDebugFlag, __VA_ARGS__); }} -#define sDebug(...) { if (sDebugFlag & DEBUG_DEBUG) { taosPrintLog("SYN ", sDebugFlag, __VA_ARGS__); }} -#define sTrace(...) { if (sDebugFlag & DEBUG_TRACE) { taosPrintLog("SYN ", sDebugFlag, __VA_ARGS__); }} - -#define SYNC_TCP_THREADS 2 -#define SYNC_MAX_NUM 2 - -#define SYNC_MAX_SIZE (TSDB_MAX_WAL_SIZE + sizeof(SWalHead) + sizeof(SSyncHead) + 16) -#define SYNC_RECV_BUFFER_SIZE (5*1024*1024) - -#define SYNC_MAX_FWDS 4096 -#define SYNC_FWD_TIMER 300 -#define SYNC_ROLE_TIMER 15000 // ms -#define SYNC_CHECK_INTERVAL 1000 // ms -#define SYNC_WAIT_AFTER_CHOOSE_MASTER 10 // ms - -#define nodeRole pNode->peerInfo[pNode->selfIndex]->role -#define nodeVersion pNode->peerInfo[pNode->selfIndex]->version -#define nodeSStatus pNode->peerInfo[pNode->selfIndex]->sstatus - -typedef struct { - char * buffer; - int32_t bufferSize; - char * offset; - int32_t forwards; - int32_t code; -} SRecvBuffer; - -typedef struct { - uint64_t version; - void *mhandle; - int8_t acks; - int8_t nacks; - int8_t confirmed; - int32_t code; - int64_t time; -} SFwdInfo; - -typedef struct { - int32_t first; - int32_t last; - int32_t fwds; // number of forwards - SFwdInfo fwdInfo[]; -} SSyncFwds; - -typedef struct SsyncPeer { - int32_t nodeId; - uint32_t ip; - uint16_t port; - int8_t role; - int8_t sstatus; // sync status - char fqdn[TSDB_FQDN_LEN]; // peer ip string - char id[TSDB_EP_LEN + 32]; // peer vgId + end point - uint64_t version; - uint64_t sversion; // track the peer version in retrieve process - uint64_t lastFileVer; // track the file version while retrieve - uint64_t lastWalVer; // track the wal version while retrieve - SOCKET syncFd; - SOCKET peerFd; // forward FD - int32_t numOfRetrieves; // number of retrieves tried - int32_t fileChanged; // a flag to indicate file is changed during retrieving process - int32_t refCount; - int8_t isArb; - int64_t rid; - void * timer; - void * pConn; - struct SSyncNode *pSyncNode; -} SSyncPeer; - -typedef struct SSyncNode { - char path[TSDB_FILENAME_LEN]; - int8_t replica; - int8_t quorum; - int8_t selfIndex; - uint32_t vgId; - int32_t refCount; - int64_t rid; - SSyncPeer * peerInfo[TAOS_SYNC_MAX_REPLICA + 1]; // extra one for arbitrator - SSyncPeer * pMaster; - SRecvBuffer *pRecv; - SSyncFwds * pSyncFwds; // saved forward info if quorum >1 - void * pFwdTimer; - void * pRoleTimer; - void * pTsdb; - FGetWalInfo getWalInfoFp; - FWriteToCache writeToCacheFp; - FConfirmForward confirmForward; - FNotifyRole notifyRoleFp; - FNotifyFlowCtrl notifyFlowCtrlFp; - FStartSyncFile startSyncFileFp; - FStopSyncFile stopSyncFileFp; - FGetVersion getVersionFp; - FSendFile sendFileFp; - FRecvFile recvFileFp; - pthread_mutex_t mutex; -} SSyncNode; - -// sync module global -extern int32_t tsSyncNum; -extern char tsNodeFqdn[TSDB_FQDN_LEN]; -extern char * syncStatus[]; - -void * syncRetrieveData(void *param); -void * syncRestoreData(void *param); -int32_t syncSaveIntoBuffer(SSyncPeer *pPeer, SWalHead *pHead); -void syncRestartConnection(SSyncPeer *pPeer); -void syncBroadcastStatus(SSyncNode *pNode); -uint32_t syncResolvePeerFqdn(SSyncPeer *pPeer); -SSyncPeer *syncAcquirePeer(int64_t rid); -void syncReleasePeer(SSyncPeer *pPeer); - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_VNODEPEER_H diff --git a/src/sync/inc/syncMain.h b/src/sync/inc/syncMain.h deleted file mode 100644 index d4ddb12733..0000000000 --- a/src/sync/inc/syncMain.h +++ /dev/null @@ -1,33 +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_PLUGINS_SYNC_H -#define TDENGINE_PLUGINS_SYNC_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -int32_t syncTest1(); -int32_t syncTest2(); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/sync/inc/syncMsg.h b/src/sync/inc/syncMsg.h deleted file mode 100644 index 85ac9c78af..0000000000 --- a/src/sync/inc/syncMsg.h +++ /dev/null @@ -1,141 +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_SYNC_MSG_H -#define TDENGINE_SYNC_MSG_H - -#ifdef __cplusplus -extern "C" { -#endif -#include "tsync.h" - -typedef enum { - TAOS_SMSG_START = 0, - TAOS_SMSG_SYNC_DATA = 1, - TAOS_SMSG_SYNC_DATA_RSP = 2, - TAOS_SMSG_SYNC_FWD = 3, - TAOS_SMSG_SYNC_FWD_RSP = 4, - TAOS_SMSG_SYNC_REQ = 5, - TAOS_SMSG_SYNC_REQ_RSP = 6, - TAOS_SMSG_SYNC_MUST = 7, - TAOS_SMSG_SYNC_MUST_RSP = 8, - TAOS_SMSG_STATUS = 9, - TAOS_SMSG_STATUS_RSP = 10, - TAOS_SMSG_SETUP = 11, - TAOS_SMSG_SETUP_RSP = 12, - TAOS_SMSG_SYNC_FILE = 13, - TAOS_SMSG_SYNC_FILE_RSP = 14, - TAOS_SMSG_TEST = 15, - TAOS_SMSG_END = 16 -} ESyncMsgType; - -typedef enum { - SYNC_STATUS_BROADCAST, - SYNC_STATUS_BROADCAST_RSP, - SYNC_STATUS_SETUP_CONN, - SYNC_STATUS_SETUP_CONN_RSP, - SYNC_STATUS_EXCHANGE_DATA, - SYNC_STATUS_EXCHANGE_DATA_RSP, - SYNC_STATUS_CHECK_ROLE, - SYNC_STATUS_CHECK_ROLE_RSP -} ESyncStatusType; - -#pragma pack(push, 1) - -typedef struct { - int8_t type; // msg type - int8_t protocol; // protocol version - uint16_t signature; // fixed value - int32_t code; // - int32_t cId; // cluster Id - int32_t vgId; // vg ID - int32_t len; // content length, does not include head - uint32_t cksum; -} SSyncHead; - -typedef struct { - SSyncHead head; - uint16_t port; - uint16_t tranId; - int32_t sourceId; // only for arbitrator - char fqdn[TSDB_FQDN_LEN]; -} SSyncMsg; - -typedef struct { - SSyncHead head; - int8_t sync; - int8_t reserved; - uint16_t tranId; - int8_t reserverd[4]; -} SSyncRsp; - -typedef struct { - int8_t role; - uint64_t version; -} SPeerStatus; - -typedef struct { - SSyncHead head; - int8_t role; - int8_t ack; - int8_t type; - int8_t reserved[3]; - uint16_t tranId; - uint64_t version; - SPeerStatus peersStatus[TAOS_SYNC_MAX_REPLICA]; -} SPeersStatus; - -typedef struct { - SSyncHead head; - uint64_t fversion; -} SFileVersion; - -typedef struct { - SSyncHead head; - int8_t ack; -} SFileAck; - -typedef struct { - SSyncHead head; - uint64_t version; - int32_t code; -} SFwdRsp; - -#pragma pack(pop) - -#define SYNC_PROTOCOL_VERSION 1 -#define SYNC_SIGNATURE ((uint16_t)(0xCDEF)) - -extern char *statusType[]; - -uint16_t syncGenTranId(); -int32_t syncCheckHead(SSyncHead *pHead); - -void syncBuildSyncFwdMsg(SSyncHead *pHead, int32_t vgId, int32_t len); -void syncBuildSyncFwdRsp(SFwdRsp *pMsg, int32_t vgId, uint64_t version, int32_t code); -void syncBuildSyncReqMsg(SSyncMsg *pMsg, int32_t vgId); -void syncBuildSyncDataMsg(SSyncMsg *pMsg, int32_t vgId); -void syncBuildSyncSetupMsg(SSyncMsg *pMsg, int32_t vgId); -void syncBuildPeersStatus(SPeersStatus *pMsg, int32_t vgId); -void syncBuildSyncTestMsg(SSyncMsg *pMsg, int32_t vgId); - -void syncBuildFileAck(SFileAck *pMsg, int32_t vgId); -void syncBuildFileVersion(SFileVersion *pMsg, int32_t vgId); - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_VNODEPEER_H diff --git a/src/sync/inc/syncTcp.h b/src/sync/inc/syncTcp.h deleted file mode 100644 index e2e5234d39..0000000000 --- a/src/sync/inc/syncTcp.h +++ /dev/null @@ -1,43 +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_SYNC_TCP_POOL_H -#define TDENGINE_SYNC_TCP_POOL_H - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - int32_t numOfThreads; - uint32_t serverIp; - int16_t port; - int32_t bufferSize; - void (*processBrokenLink)(int64_t handleId, int32_t closedByApp); - int32_t (*processIncomingMsg)(int64_t handleId, void *buffer); - void (*processIncomingConn)(SOCKET fd, uint32_t ip); -} SPoolInfo; - -void *syncOpenTcpThreadPool(SPoolInfo *pInfo); -void syncCloseTcpThreadPool(void *); -void *syncAllocateTcpConn(void *, int64_t rid, SOCKET connFd); -void syncFreeTcpConn(void *); - -#ifdef __cplusplus -} -#endif - -#endif // TDENGINE_TCP_POOL_H - diff --git a/src/sync/src/syncArbitrator.c b/src/sync/src/syncArbitrator.c deleted file mode 100644 index 08ebe7ae3e..0000000000 --- a/src/sync/src/syncArbitrator.c +++ /dev/null @@ -1,189 +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 "hash.h" -#include "tlog.h" -#include "tutil.h" -#include "ttimer.h" -#include "tsocket.h" -#include "tglobal.h" -#include "taoserror.h" -#include "twal.h" -#include "tsync.h" -#include "syncInt.h" -#include "syncTcp.h" - -extern void syncProcessTestMsg(SSyncMsg *pMsg, SOCKET connFd); -static void arbSignalHandler(int32_t signum, void *sigInfo, void *context); -static void arbProcessIncommingConnection(SOCKET connFd, uint32_t sourceIp); -static void arbProcessBrokenLink(int64_t rid, int32_t closedByApp); -static int32_t arbProcessPeerMsg(int64_t rid, void *buffer); -static tsem_t tsArbSem; -static void * tsArbTcpPool; - -typedef struct { - char id[TSDB_EP_LEN + 24]; - SOCKET nodeFd; - void * pConn; -} SNodeConn; - -int32_t main(int32_t argc, char *argv[]) { - char arbLogPath[TSDB_FILENAME_LEN + 16] = {0}; - - for (int32_t i = 1; i < argc; ++i) { - if (strcmp(argv[i], "-p") == 0 && i < argc - 1) { - tsArbitratorPort = atoi(argv[++i]); - } else if (strcmp(argv[i], "-d") == 0 && i < argc - 1) { - debugFlag = atoi(argv[++i]); - } else if (strcmp(argv[i], "-g") == 0 && i < argc - 1) { - if (strlen(argv[++i]) > TSDB_FILENAME_LEN) continue; - tstrncpy(arbLogPath, argv[i], sizeof(arbLogPath)); - } else { - printf("\nusage: %s [options] \n", argv[0]); - printf(" [-p port]: arbitrator server port number, default is:%d\n", tsServerPort + TSDB_PORT_ARBITRATOR); - printf(" [-d debugFlag]: debug flag, option 131 | 135 | 143, default:0\n"); - printf(" [-g logFilePath]: log file pathe, default:/arbitrator.log\n"); - printf(" [-h help]: print out this help\n\n"); - exit(0); - } - } - - sDebugFlag = debugFlag; - - if (tsem_init(&tsArbSem, 0, 0) != 0) { - printf("failed to create exit semphore\n"); - exit(EXIT_FAILURE); - } - - /* Set termination handler. */ - taosSetSignal(SIGTERM, arbSignalHandler); - taosSetSignal(SIGINT, arbSignalHandler); - taosSetSignal(SIGHUP, arbSignalHandler); - taosSetSignal(SIGABRT, arbSignalHandler); - - tsAsyncLog = 0; - strcat(arbLogPath, "/arbitrator.log"); - taosInitLog(arbLogPath, 1000000, 10); - - taosGetFqdn(tsNodeFqdn); - - SPoolInfo info; - info.numOfThreads = 1; - info.serverIp = 0; - info.port = tsArbitratorPort; - info.bufferSize = SYNC_MAX_SIZE; - info.processBrokenLink = arbProcessBrokenLink; - info.processIncomingMsg = arbProcessPeerMsg; - info.processIncomingConn = arbProcessIncommingConnection; - tsArbTcpPool = syncOpenTcpThreadPool(&info); - - if (tsArbTcpPool == NULL) { - sDebug("failed to open TCP thread pool, exit..."); - return -1; - } - - sInfo("TAOS arbitrator: %s:%d is running", tsNodeFqdn, tsArbitratorPort); - - tsem_wait(&tsArbSem); - - syncCloseTcpThreadPool(tsArbTcpPool); - sInfo("TAOS arbitrator is shut down"); - - closelog(); - return 0; -} - -static void arbProcessIncommingConnection(SOCKET connFd, uint32_t sourceIp) { - char ipstr[24]; - tinet_ntoa(ipstr, sourceIp); - sDebug("peer TCP connection from ip:%s", ipstr); - - SSyncMsg msg; - if (taosReadMsg(connFd, &msg, sizeof(SSyncMsg)) != sizeof(SSyncMsg)) { - sError("failed to read peer sync msg from ip:%s since %s", ipstr, strerror(errno)); - taosCloseSocket(connFd); - return; - } - - if (msg.head.type == TAOS_SMSG_TEST) { - syncProcessTestMsg(&msg, connFd); - return; - } - - SNodeConn *pNode = calloc(sizeof(SNodeConn), 1); - if (pNode == NULL) { - sError("failed to allocate memory since %s", strerror(errno)); - taosCloseSocket(connFd); - return; - } - - msg.fqdn[TSDB_FQDN_LEN - 1] = 0; - snprintf(pNode->id, sizeof(pNode->id), "vgId:%d, peer:%s:%d", msg.sourceId, msg.fqdn, msg.port); - if (msg.head.vgId) { - sDebug("%s, vgId in head is not zero, close the connection", pNode->id); - tfree(pNode); - taosCloseSocket(connFd); - return; - } - - sDebug("%s, arbitrator request is accepted", pNode->id); - pNode->nodeFd = connFd; - pNode->pConn = syncAllocateTcpConn(tsArbTcpPool, (int64_t)pNode, connFd); - - return; -} - -static void arbProcessBrokenLink(int64_t rid, int32_t closedByApp) { - SNodeConn *pNode = (SNodeConn *)rid; - - sDebug("%s, TCP link is broken since %s, closedByApp:%d", pNode->id, strerror(errno), closedByApp); - tfree(pNode); -} - -static int32_t arbProcessPeerMsg(int64_t rid, void *buffer) { - SNodeConn *pNode = (SNodeConn *)rid; - SSyncHead head; - int32_t bytes = 0; - char * cont = (char *)buffer; - - int32_t hlen = taosReadMsg(pNode->nodeFd, &head, sizeof(SSyncHead)); - if (hlen != sizeof(SSyncHead)) { - sDebug("%s, failed to read msg, hlen:%d", pNode->id, hlen); - return -1; - } - - bytes = taosReadMsg(pNode->nodeFd, cont, head.len); - if (bytes != head.len) { - sDebug("%s, failed to read, bytes:%d len:%d", pNode->id, bytes, head.len); - return -1; - } - - sDebug("%s, msg is received, len:%d", pNode->id, head.len); - return 0; -} - -static void arbSignalHandler(int32_t signum, void *sigInfo, void *context) { - taosIgnSignal(SIGTERM); - taosIgnSignal(SIGINT); - taosIgnSignal(SIGABRT); - taosIgnSignal(SIGHUP); - - sInfo("shut down signal is %d", signum); - - // inform main thread to exit - tsem_post(&tsArbSem); -} diff --git a/src/sync/src/syncMain.c b/src/sync/src/syncMain.c deleted file mode 100644 index 68bafb09ca..0000000000 --- a/src/sync/src/syncMain.c +++ /dev/null @@ -1,1519 +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 "hash.h" -#include "tlog.h" -#include "tutil.h" -#include "ttimer.h" -#include "tref.h" -#include "tsocket.h" -#include "tglobal.h" -#include "taoserror.h" -#include "tqueue.h" -#include "twal.h" -#include "tsync.h" -#include "syncTcp.h" -#include "syncInt.h" - -int32_t tsSyncNum = 0; // number of sync in process in whole system -char tsNodeFqdn[TSDB_FQDN_LEN] = {0}; - -static void * tsTcpPool = NULL; -static void * tsSyncTmrCtrl = NULL; -static void * tsVgIdHash = NULL; -static int32_t tsNodeRefId = -1; -static int32_t tsPeerRefId = -1; - -// local functions -static void syncProcessSyncRequest(char *pMsg, SSyncPeer *pPeer); -static void syncRecoverFromMaster(SSyncPeer *pPeer); -static void syncCheckPeerConnection(void *param, void *tmrId); -static int32_t syncSendPeersStatusMsgToPeer(SSyncPeer *pPeer, char ack, int8_t type, uint16_t tranId); -static void syncProcessBrokenLink(int64_t rid, int32_t closedByApp); -static int32_t syncProcessPeerMsg(int64_t rid, void *buffer); -static void syncProcessIncommingConnection(SOCKET connFd, uint32_t sourceIp); -static void syncRemovePeer(SSyncPeer *pPeer); -static void syncAddArbitrator(SSyncNode *pNode); -static void syncFreeNode(void *); -static void syncFreePeer(void *); -static void syncRemoveConfirmedFwdInfo(SSyncNode *pNode); -static void syncMonitorFwdInfos(void *param, void *tmrId); -static void syncMonitorNodeRole(void *param, void *tmrId); -static void syncProcessFwdAck(SSyncNode *pNode, SFwdInfo *pFwdInfo, int32_t code); -static int32_t syncSaveFwdInfo(SSyncNode *pNode, uint64_t version, void *mhandle); -static void syncRestartPeer(SSyncPeer *pPeer); -static int32_t syncForwardToPeerImpl(SSyncNode *pNode, void *data, void *mhandle, int32_t qtype, bool force); - -static SSyncPeer *syncAddPeer(SSyncNode *pNode, const SNodeInfo *pInfo); -static void syncStartCheckPeerConn(SSyncPeer *pPeer); -static void syncStopCheckPeerConn(SSyncPeer *pPeer); -static SSyncNode *syncAcquireNode(int64_t rid); -static void syncReleaseNode(SSyncNode *pNode); - -char* syncRole[] = { - "offline", - "unsynced", - "syncing", - "slave", - "master" -}; - -char *syncStatus[] = { - "init", - "start", - "file", - "cache", - "invalid" -}; - -int32_t syncInit() { - SPoolInfo info = {0}; - - info.numOfThreads = SYNC_TCP_THREADS; - info.serverIp = 0; - info.port = tsSyncPort; - info.bufferSize = SYNC_MAX_SIZE; - info.processBrokenLink = syncProcessBrokenLink; - info.processIncomingMsg = syncProcessPeerMsg; - info.processIncomingConn = syncProcessIncommingConnection; - - tsTcpPool = syncOpenTcpThreadPool(&info); - if (tsTcpPool == NULL) { - sError("failed to init tcpPool"); - syncCleanUp(); - return -1; - } - - tsSyncTmrCtrl = taosTmrInit(1000, 50, 10000, "SYNC"); - if (tsSyncTmrCtrl == NULL) { - sError("failed to init tmrCtrl"); - syncCleanUp(); - return -1; - } - - tsVgIdHash = taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); - if (tsVgIdHash == NULL) { - sError("failed to init vgIdHash"); - syncCleanUp(); - return -1; - } - - tsNodeRefId = taosOpenRef(200, syncFreeNode); - if (tsNodeRefId < 0) { - sError("failed to init node ref"); - syncCleanUp(); - return -1; - } - - tsPeerRefId = taosOpenRef(1000, syncFreePeer); - if (tsPeerRefId < 0) { - sError("failed to init peer ref"); - syncCleanUp(); - return -1; - } - - tstrncpy(tsNodeFqdn, tsLocalFqdn, sizeof(tsNodeFqdn)); - sInfo("sync module initialized successfully"); - - return 0; -} - -void syncCleanUp() { - if (tsTcpPool != NULL) { - syncCloseTcpThreadPool(tsTcpPool); - tsTcpPool = NULL; - } - - if (tsSyncTmrCtrl != NULL) { - taosTmrCleanUp(tsSyncTmrCtrl); - tsSyncTmrCtrl = NULL; - } - - if (tsVgIdHash) { - taosHashCleanup(tsVgIdHash); - tsVgIdHash = NULL; - } - - if (tsNodeRefId != -1) { - taosCloseRef(tsNodeRefId); - tsNodeRefId = -1; - } - - if (tsPeerRefId != -1) { - taosCloseRef(tsPeerRefId); - tsPeerRefId = -1; - } - - sInfo("sync module is cleaned up"); -} - -int64_t syncStart(const SSyncInfo *pInfo) { - const SSyncCfg *pCfg = &pInfo->syncCfg; - - SSyncNode *pNode = calloc(sizeof(SSyncNode), 1); - if (pNode == NULL) { - sError("no memory to allocate syncNode"); - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; - } - - tstrncpy(pNode->path, pInfo->path, sizeof(pNode->path)); - pthread_mutex_init(&pNode->mutex, NULL); - - pNode->getWalInfoFp = pInfo->getWalInfoFp; - pNode->writeToCacheFp = pInfo->writeToCacheFp; - pNode->notifyRoleFp = pInfo->notifyRoleFp; - pNode->confirmForward = pInfo->confirmForward; - pNode->notifyFlowCtrlFp = pInfo->notifyFlowCtrlFp; - pNode->startSyncFileFp = pInfo->startSyncFileFp; - pNode->stopSyncFileFp = pInfo->stopSyncFileFp; - pNode->getVersionFp = pInfo->getVersionFp; - pNode->sendFileFp = pInfo->sendFileFp; - pNode->recvFileFp = pInfo->recvFileFp; - - pNode->selfIndex = -1; - pNode->vgId = pInfo->vgId; - pNode->replica = pCfg->replica; - pNode->quorum = pCfg->quorum; - pNode->pTsdb = pInfo->pTsdb; - if (pNode->quorum > pNode->replica) pNode->quorum = pNode->replica; - - pNode->refCount = 1; - pNode->rid = taosAddRef(tsNodeRefId, pNode); - if (pNode->rid < 0) { - syncFreeNode(pNode); - return -1; - } - - for (int32_t index = 0; index < pCfg->replica; ++index) { - const SNodeInfo *pNodeInfo = pCfg->nodeInfo + index; - pNode->peerInfo[index] = syncAddPeer(pNode, pNodeInfo); - if (pNode->peerInfo[index] == NULL) { - sError("vgId:%d, node:%d fqdn:%s port:%u is not configured, stop taosd", pNode->vgId, pNodeInfo->nodeId, - pNodeInfo->nodeFqdn, pNodeInfo->nodePort); - syncStop(pNode->rid); - exit(1); - } - - if ((strcmp(pNodeInfo->nodeFqdn, tsNodeFqdn) == 0) && (pNodeInfo->nodePort == tsSyncPort)) { - pNode->selfIndex = index; - } - } - - if (pNode->selfIndex < 0) { - sError("vgId:%d, this node is not configured", pNode->vgId); - terrno = TSDB_CODE_SYN_INVALID_CONFIG; - syncStop(pNode->rid); - return -1; - } - - nodeVersion = pInfo->version; // set the initial version - nodeRole = (pNode->replica > 1) ? TAOS_SYNC_ROLE_UNSYNCED : TAOS_SYNC_ROLE_MASTER; - sInfo("vgId:%d, %d replicas are configured, quorum:%d role:%s", pNode->vgId, pNode->replica, pNode->quorum, - syncRole[nodeRole]); - - pNode->pSyncFwds = calloc(sizeof(SSyncFwds) + SYNC_MAX_FWDS * sizeof(SFwdInfo), 1); - if (pNode->pSyncFwds == NULL) { - sError("vgId:%d, no memory to allocate syncFwds", pNode->vgId); - terrno = TAOS_SYSTEM_ERROR(errno); - syncStop(pNode->rid); - return -1; - } - - pNode->pFwdTimer = taosTmrStart(syncMonitorFwdInfos, SYNC_FWD_TIMER, (void *)pNode->rid, tsSyncTmrCtrl); - if (pNode->pFwdTimer == NULL) { - sError("vgId:%d, failed to allocate fwd timer", pNode->vgId); - syncStop(pNode->rid); - return -1; - } - - pNode->pRoleTimer = taosTmrStart(syncMonitorNodeRole, SYNC_ROLE_TIMER, (void *)pNode->rid, tsSyncTmrCtrl); - if (pNode->pRoleTimer == NULL) { - sError("vgId:%d, failed to allocate role timer", pNode->vgId); - syncStop(pNode->rid); - return -1; - } - - syncAddArbitrator(pNode); - taosHashPut(tsVgIdHash, &pNode->vgId, sizeof(int32_t), &pNode, sizeof(SSyncNode *)); - - if (pNode->notifyRoleFp) { - (*pNode->notifyRoleFp)(pNode->vgId, nodeRole); - } - - syncStartCheckPeerConn(pNode->peerInfo[TAOS_SYNC_MAX_REPLICA]); // arb - for (int32_t index = 0; index < pNode->replica; ++index) { - syncStartCheckPeerConn(pNode->peerInfo[index]); - } - - return pNode->rid; -} - -void syncStop(int64_t rid) { - SSyncPeer *pPeer; - - SSyncNode *pNode = syncAcquireNode(rid); - if (pNode == NULL) return; - - sInfo("vgId:%d, cleanup sync", pNode->vgId); - - pthread_mutex_lock(&pNode->mutex); - - if (tsVgIdHash) taosHashRemove(tsVgIdHash, &pNode->vgId, sizeof(int32_t)); - if (pNode->pFwdTimer) taosTmrStop(pNode->pFwdTimer); - if (pNode->pRoleTimer) taosTmrStop(pNode->pRoleTimer); - - for (int32_t index = 0; index < pNode->replica; ++index) { - pPeer = pNode->peerInfo[index]; - if (pPeer) syncRemovePeer(pPeer); - } - - pPeer = pNode->peerInfo[TAOS_SYNC_MAX_REPLICA]; - if (pPeer) syncRemovePeer(pPeer); - - pthread_mutex_unlock(&pNode->mutex); - - syncReleaseNode(pNode); - taosRemoveRef(tsNodeRefId, rid); -} - -int32_t syncReconfig(int64_t rid, const SSyncCfg *pNewCfg) { - int32_t i, j; - - SSyncNode *pNode = syncAcquireNode(rid); - if (pNode == NULL) return TSDB_CODE_SYN_INVALID_CONFIG; - - sInfo("vgId:%d, reconfig, role:%s replica:%d old:%d", pNode->vgId, syncRole[nodeRole], pNewCfg->replica, - pNode->replica); - - pthread_mutex_lock(&pNode->mutex); - - syncStopCheckPeerConn(pNode->peerInfo[TAOS_SYNC_MAX_REPLICA]); // arb - for (int32_t index = 0; index < pNode->replica; ++index) { - syncStopCheckPeerConn(pNode->peerInfo[index]); - } - - for (i = 0; i < pNode->replica; ++i) { - for (j = 0; j < pNewCfg->replica; ++j) { - if ((strcmp(pNode->peerInfo[i]->fqdn, pNewCfg->nodeInfo[j].nodeFqdn) == 0) && - (pNode->peerInfo[i]->port == pNewCfg->nodeInfo[j].nodePort)) - break; - } - - if (j >= pNewCfg->replica) { - syncRemovePeer(pNode->peerInfo[i]); - pNode->peerInfo[i] = NULL; - } - } - - SSyncPeer *newPeers[TAOS_SYNC_MAX_REPLICA]; - for (i = 0; i < pNewCfg->replica; ++i) { - const SNodeInfo *pNewNode = &pNewCfg->nodeInfo[i]; - - for (j = 0; j < pNode->replica; ++j) { - if (pNode->peerInfo[j] && (strcmp(pNode->peerInfo[j]->fqdn, pNewNode->nodeFqdn) == 0) && - (pNode->peerInfo[j]->port == pNewNode->nodePort)) - break; - } - - if (j >= pNode->replica) { - newPeers[i] = syncAddPeer(pNode, pNewNode); - } else { - newPeers[i] = pNode->peerInfo[j]; - } - - if (newPeers[i] == NULL) { - sError("vgId:%d, failed to reconfig", pNode->vgId); - return TSDB_CODE_SYN_INVALID_CONFIG; - } - - if ((strcmp(pNewNode->nodeFqdn, tsNodeFqdn) == 0) && (pNewNode->nodePort == tsSyncPort)) { - pNode->selfIndex = i; - } - } - - pNode->replica = pNewCfg->replica; - pNode->quorum = pNewCfg->quorum; - if (pNode->quorum > pNode->replica) pNode->quorum = pNode->replica; - memcpy(pNode->peerInfo, newPeers, sizeof(SSyncPeer *) * pNewCfg->replica); - - for (i = pNewCfg->replica; i < TAOS_SYNC_MAX_REPLICA; ++i) { - pNode->peerInfo[i] = NULL; - } - - syncAddArbitrator(pNode); - - if (pNewCfg->replica <= 1) { - sInfo("vgId:%d, no peers are configured, work as master!", pNode->vgId); - nodeRole = TAOS_SYNC_ROLE_MASTER; - (*pNode->notifyRoleFp)(pNode->vgId, nodeRole); - } - - syncStartCheckPeerConn(pNode->peerInfo[TAOS_SYNC_MAX_REPLICA]); // arb - for (int32_t index = 0; index < pNode->replica; ++index) { - syncStartCheckPeerConn(pNode->peerInfo[index]); - } - - pthread_mutex_unlock(&pNode->mutex); - - sInfo("vgId:%d, %d replicas are configured, quorum:%d", pNode->vgId, pNode->replica, pNode->quorum); - syncBroadcastStatus(pNode); - - syncReleaseNode(pNode); - return 0; -} - -int32_t syncForwardToPeer(int64_t rid, void *data, void *mhandle, int32_t qtype, bool force) { - if (rid <= 0) return 0; - - SSyncNode *pNode = syncAcquireNode(rid); - if (pNode == NULL) return 0; - - int32_t code = syncForwardToPeerImpl(pNode, data, mhandle, qtype, force); - - syncReleaseNode(pNode); - return code; -} - -void syncConfirmForward(int64_t rid, uint64_t _version, int32_t code, bool force) { - SSyncNode *pNode = syncAcquireNode(rid); - if (pNode == NULL) return; - - SSyncPeer *pPeer = pNode->pMaster; - if (pPeer && (pNode->quorum > 1 || force)) { - SFwdRsp rsp; - syncBuildSyncFwdRsp(&rsp, pNode->vgId, _version, code); - - if (taosWriteMsg(pPeer->peerFd, &rsp, sizeof(SFwdRsp)) == sizeof(SFwdRsp)) { - sTrace("%s, forward-rsp is sent, code:0x%x hver:%" PRIu64, pPeer->id, code, _version); - } else { - sDebug("%s, failed to send forward-rsp, restart", pPeer->id); - syncRestartConnection(pPeer); - } - } - - syncReleaseNode(pNode); -} - -void syncRecover(int64_t rid) { - SSyncPeer *pPeer; - - SSyncNode *pNode = syncAcquireNode(rid); - if (pNode == NULL) return; - - nodeRole = TAOS_SYNC_ROLE_UNSYNCED; - (*pNode->notifyRoleFp)(pNode->vgId, nodeRole); - - pthread_mutex_lock(&pNode->mutex); - - nodeVersion = 0; - - for (int32_t i = 0; i < pNode->replica; ++i) { - if (i == pNode->selfIndex) continue; - - pPeer = pNode->peerInfo[i]; - if (pPeer->peerFd >= 0) { - syncRestartConnection(pPeer); - } - } - - pthread_mutex_unlock(&pNode->mutex); - - syncReleaseNode(pNode); -} - -int32_t syncGetNodesRole(int64_t rid, SNodesRole *pNodesRole) { - SSyncNode *pNode = syncAcquireNode(rid); - if (pNode == NULL) return -1; - - pNodesRole->selfIndex = pNode->selfIndex; - for (int32_t i = 0; i < pNode->replica; ++i) { - pNodesRole->nodeId[i] = pNode->peerInfo[i]->nodeId; - pNodesRole->role[i] = pNode->peerInfo[i]->role; - } - - syncReleaseNode(pNode); - return 0; -} - -static void syncAddArbitrator(SSyncNode *pNode) { - SSyncPeer *pPeer = pNode->peerInfo[TAOS_SYNC_MAX_REPLICA]; - - // if not configured, return right away - if (tsArbitrator[0] == 0) { - if (pPeer) syncRemovePeer(pPeer); - pNode->peerInfo[TAOS_SYNC_MAX_REPLICA] = NULL; - return; - } - - SNodeInfo nodeInfo; - nodeInfo.nodeId = 0; - int32_t ret = taosGetFqdnPortFromEp(tsArbitrator, nodeInfo.nodeFqdn, &nodeInfo.nodePort); - if (-1 == ret) { - nodeInfo.nodePort = tsArbitratorPort; - } - - if (pPeer) { - if ((strcmp(nodeInfo.nodeFqdn, pPeer->fqdn) == 0) && (nodeInfo.nodePort == pPeer->port)) { - return; - } else { - syncRemovePeer(pPeer); - pNode->peerInfo[TAOS_SYNC_MAX_REPLICA] = NULL; - } - } - - pPeer = syncAddPeer(pNode, &nodeInfo); - if (pPeer != NULL) { - pPeer->isArb = 1; - sInfo("%s, is added as arbitrator", pPeer->id); - } - - pNode->peerInfo[TAOS_SYNC_MAX_REPLICA] = pPeer; -} - -static void syncFreeNode(void *param) { - SSyncNode *pNode = param; - sDebug("vgId:%d, node is freed, refCount:%d", pNode->vgId, pNode->refCount); - - pthread_mutex_destroy(&pNode->mutex); - tfree(pNode->pRecv); - tfree(pNode->pSyncFwds); - tfree(pNode); -} - -SSyncNode *syncAcquireNode(int64_t rid) { - SSyncNode *pNode = taosAcquireRef(tsNodeRefId, rid); - if (pNode == NULL) { - sDebug("failed to acquire node from refId:%" PRId64, rid); - } else { - int32_t refCount = atomic_add_fetch_32(&pNode->refCount, 1); - sTrace("vgId:%d, acquire node refId:%" PRId64 ", refCount:%d", pNode->vgId, rid, refCount); - } - - return pNode; -} - -void syncReleaseNode(SSyncNode *pNode) { - int32_t refCount = atomic_sub_fetch_32(&pNode->refCount, 1); - sTrace("vgId:%d, release node refId:%" PRId64 ", refCount:%d", pNode->vgId, pNode->rid, refCount); - - taosReleaseRef(tsNodeRefId, pNode->rid); -} - -static void syncFreePeer(void *param) { - SSyncPeer *pPeer = param; - sDebug("%s, peer is freed, refCount:%d", pPeer->id, pPeer->refCount); - - syncReleaseNode(pPeer->pSyncNode); - tfree(pPeer); -} - -SSyncPeer *syncAcquirePeer(int64_t rid) { - SSyncPeer *pPeer = taosAcquireRef(tsPeerRefId, rid); - if (pPeer == NULL) { - sDebug("failed to acquire peer from refId:%" PRId64, rid); - } else { - int32_t refCount = atomic_add_fetch_32(&pPeer->refCount, 1); - sTrace("%s, acquire peer refId:%" PRId64 ", refCount:%d", pPeer->id, rid, refCount); - } - - return pPeer; -} - -void syncReleasePeer(SSyncPeer *pPeer) { - int32_t refCount = atomic_sub_fetch_32(&pPeer->refCount, 1); - sTrace("%s, release peer refId:%" PRId64 ", refCount:%d", pPeer->id, pPeer->rid, refCount); - - taosReleaseRef(tsPeerRefId, pPeer->rid); -} - -static void syncClosePeerConn(SSyncPeer *pPeer) { - sDebug("%s, pfd:%d sfd:%d will be closed", pPeer->id, pPeer->peerFd, pPeer->syncFd); - - taosTmrStopA(&pPeer->timer); - taosCloseSocket(pPeer->syncFd); - if (pPeer->peerFd >= 0) { - pPeer->peerFd = -1; - void *pConn = pPeer->pConn; - if (pConn != NULL) { - syncFreeTcpConn(pPeer->pConn); - pPeer->pConn = NULL; - } - } -} - -static void syncRemovePeer(SSyncPeer *pPeer) { - sInfo("%s, it is removed", pPeer->id); - - //pPeer->ip = 0; - pPeer->fqdn[0] = '\0'; - syncClosePeerConn(pPeer); - //taosRemoveRef(tsPeerRefId, pPeer->rid); - syncReleasePeer(pPeer); -} - -static void syncStartCheckPeerConn(SSyncPeer *pPeer) { - if (pPeer == NULL) return; - SSyncNode *pNode = pPeer->pSyncNode; - - int32_t ret = strcmp(pPeer->fqdn, tsNodeFqdn); - if (pPeer->nodeId == 0 || (ret > 0) || (ret == 0 && pPeer->port > tsSyncPort)) { - int32_t checkMs = 100 + (pNode->vgId * 10) % 100; - - sDebug("%s, check peer connection after %d ms", pPeer->id, checkMs); - taosTmrReset(syncCheckPeerConnection, checkMs, (void *)pPeer->rid, tsSyncTmrCtrl, &pPeer->timer); - } -} - -static void syncStopCheckPeerConn(SSyncPeer *pPeer) { - if (pPeer == NULL) return; - - taosTmrStopA(&pPeer->timer); - sDebug("%s, stop check peer connection", pPeer->id); -} - -uint32_t syncResolvePeerFqdn(SSyncPeer *pPeer) { - uint32_t ip = taosGetIpv4FromFqdn(pPeer->fqdn); - if (ip == 0xFFFFFFFF) { - sError("failed to resolve peer fqdn:%s since %s", pPeer->fqdn, strerror(errno)); - terrno = TSDB_CODE_RPC_FQDN_ERROR; - return 0; - } - - return ip; -} - -static SSyncPeer *syncAddPeer(SSyncNode *pNode, const SNodeInfo *pInfo) { - /*uint32_t ip = taosGetIpv4FromFqdn(pInfo->nodeFqdn); - if (ip == 0xFFFFFFFF) { - sError("failed to add peer, can resolve fqdn:%s since %s", pInfo->nodeFqdn, strerror(errno)); - terrno = TSDB_CODE_RPC_FQDN_ERROR; - return NULL; - } - */ - SSyncPeer *pPeer = calloc(1, sizeof(SSyncPeer)); - if (pPeer == NULL) return NULL; - - pPeer->nodeId = pInfo->nodeId; - tstrncpy(pPeer->fqdn, pInfo->nodeFqdn, sizeof(pPeer->fqdn)); - //pPeer->ip = ip; - pPeer->port = pInfo->nodePort; - pPeer->fqdn[sizeof(pPeer->fqdn) - 1] = 0; - snprintf(pPeer->id, sizeof(pPeer->id), "vgId:%d, nodeId:%d", pNode->vgId, pPeer->nodeId); - - pPeer->peerFd = -1; - pPeer->syncFd = -1; - pPeer->role = TAOS_SYNC_ROLE_OFFLINE; - pPeer->pSyncNode = pNode; - pPeer->refCount = 1; - pPeer->rid = taosAddRef(tsPeerRefId, pPeer); - - sInfo("%s, %p it is configured, ep:%s:%u rid:%" PRId64, pPeer->id, pPeer, pPeer->fqdn, pPeer->port, pPeer->rid); - - (void)syncAcquireNode(pNode->rid); - return pPeer; -} - -void syncBroadcastStatus(SSyncNode *pNode) { - for (int32_t index = 0; index < pNode->replica; ++index) { - if (index == pNode->selfIndex) continue; - SSyncPeer *pPeer = pNode->peerInfo[index]; - syncSendPeersStatusMsgToPeer(pPeer, 1, SYNC_STATUS_BROADCAST, syncGenTranId()); - } -} - -static void syncResetFlowCtrl(SSyncNode *pNode) { - for (int32_t index = 0; index < pNode->replica; ++index) { - pNode->peerInfo[index]->numOfRetrieves = 0; - } - - if (pNode->notifyFlowCtrlFp) { - (*pNode->notifyFlowCtrlFp)(pNode->vgId, 0); - } -} - -static void syncChooseMaster(SSyncNode *pNode) { - SSyncPeer *pPeer; - int32_t onlineNum = 0; - int32_t index = -1; - int32_t replica = pNode->replica; - - for (int32_t i = 0; i < pNode->replica; ++i) { - if (pNode->peerInfo[i]->role != TAOS_SYNC_ROLE_OFFLINE) { - onlineNum++; - } - } - - if (onlineNum == pNode->replica) { - // if all peers are online, peer with highest version shall be master - index = 0; - for (int32_t i = 1; i < pNode->replica; ++i) { - if (pNode->peerInfo[i]->version > pNode->peerInfo[index]->version) { - index = i; - } - } - sDebug("vgId:%d, master:%s may be choosed, index:%d", pNode->vgId, pNode->peerInfo[index]->id, index); - } else { - sDebug("vgId:%d, no master election since onlineNum:%d replica:%d", pNode->vgId, onlineNum, pNode->replica); - } - - // add arbitrator connection - SSyncPeer *pArb = pNode->peerInfo[TAOS_SYNC_MAX_REPLICA]; - if (pArb) { - if (pArb->role != TAOS_SYNC_ROLE_OFFLINE) { - onlineNum++; - replica = pNode->replica + 1; - sDebug("vgId:%d, arb:%s is used while choose master", pNode->vgId, pArb->id); - } else { - sError("vgId:%d, arb:%s is not used while choose master for its offline", pNode->vgId, pArb->id); - } - } - - if (index < 0 && onlineNum > replica / 2.0) { - // over half of nodes are online - for (int32_t i = 0; i < pNode->replica; ++i) { - // slave with highest version shall be master - pPeer = pNode->peerInfo[i]; - if (pPeer->role == TAOS_SYNC_ROLE_SLAVE || pPeer->role == TAOS_SYNC_ROLE_MASTER) { - if (index < 0 || pPeer->version > pNode->peerInfo[index]->version) { - index = i; - } - } - } - - if (index >= 0) { - sDebug("vgId:%d, master:%s may be choosed, index:%d onlineNum(arb):%d replica:%d", pNode->vgId, - pNode->peerInfo[index]->id, index, onlineNum, replica); - } - } - - if (index >= 0) { - if (index == pNode->selfIndex) { - sInfo("vgId:%d, start to work as master", pNode->vgId); - nodeRole = TAOS_SYNC_ROLE_MASTER; - - // Wait for other nodes to receive status to avoid version inconsistency - taosMsleep(SYNC_WAIT_AFTER_CHOOSE_MASTER); - - syncResetFlowCtrl(pNode); - (*pNode->notifyRoleFp)(pNode->vgId, nodeRole); - } else { - pPeer = pNode->peerInfo[index]; - sInfo("%s, it shall work as master", pPeer->id); - } - } else { - sDebug("vgId:%d, failed to choose master", pNode->vgId); - } -} - -static SSyncPeer *syncCheckMaster(SSyncNode *pNode) { - int32_t onlineNum = 0, arbOnlineNum = 0; - int32_t masterIndex = -1; - int32_t replica = pNode->replica; - - for (int32_t index = 0; index < pNode->replica; ++index) { - if (pNode->peerInfo[index]->role != TAOS_SYNC_ROLE_OFFLINE) { - onlineNum++; - } - } - - // add arbitrator connection - SSyncPeer *pArb = pNode->peerInfo[TAOS_SYNC_MAX_REPLICA]; - if (pArb && pArb->role != TAOS_SYNC_ROLE_OFFLINE) { - onlineNum++; - ++arbOnlineNum; - replica = pNode->replica + 1; - } - - if (onlineNum <= replica * 0.5) { - if (nodeRole != TAOS_SYNC_ROLE_UNSYNCED) { - if (nodeRole == TAOS_SYNC_ROLE_MASTER && onlineNum == replica * 0.5 && ((replica > 2 && onlineNum - arbOnlineNum > 1) || pNode->replica < 3)) { - sInfo("vgId:%d, self keep work as master, online:%d replica:%d", pNode->vgId, onlineNum, replica); - masterIndex = pNode->selfIndex; - } else { - nodeRole = TAOS_SYNC_ROLE_UNSYNCED; - sInfo("vgId:%d, self change to unsynced state, online:%d replica:%d", pNode->vgId, onlineNum, replica); - } - (*pNode->notifyRoleFp)(pNode->vgId, nodeRole); - } - } else { - for (int32_t index = 0; index < pNode->replica; ++index) { - SSyncPeer *pTemp = pNode->peerInfo[index]; - if (pTemp->role != TAOS_SYNC_ROLE_MASTER) continue; - if (masterIndex < 0) { - masterIndex = index; - sDebug("vgId:%d, peer:%s is master, index:%d", pNode->vgId, pTemp->id, index); - } else { // multiple masters, it shall not happen - if (masterIndex == pNode->selfIndex) { - sError("%s, peer is master, work as slave instead", pTemp->id); - nodeRole = TAOS_SYNC_ROLE_SLAVE; - (*pNode->notifyRoleFp)(pNode->vgId, nodeRole); - } else { - sError("vgId:%d, peer:%s is master too, masterIndex:%d index:%d", pNode->vgId, pTemp->id, masterIndex, index); - } - } - } - } - - SSyncPeer *pMaster = (masterIndex >= 0) ? pNode->peerInfo[masterIndex] : NULL; - return pMaster; -} - -static int32_t syncValidateMaster(SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - int32_t code = 0; - - if (nodeRole == TAOS_SYNC_ROLE_MASTER && nodeVersion < pPeer->version) { - sDebug("%s, peer has higher sver:%" PRIu64 ", restart all peer connections", pPeer->id, pPeer->version); - nodeRole = TAOS_SYNC_ROLE_UNSYNCED; - (*pNode->notifyRoleFp)(pNode->vgId, nodeRole); - code = -1; - - for (int32_t index = 0; index < pNode->replica; ++index) { - if (index == pNode->selfIndex) continue; - syncRestartPeer(pNode->peerInfo[index]); - } - } - - return code; -} - -static void syncCheckRole(SSyncPeer *pPeer, SPeerStatus* peersStatus, int8_t newPeerRole) { - SSyncNode *pNode = pPeer->pSyncNode; - int8_t oldPeerRole = pPeer->role; - int8_t oldSelfRole = nodeRole; - int8_t syncRequired = 0; - - pPeer->role = newPeerRole; - sDebug("%s, peer role:%s change to %s", pPeer->id, syncRole[oldPeerRole], syncRole[newPeerRole]); - - SSyncPeer *pMaster = syncCheckMaster(pNode); - - if (pMaster) { - // master is there - pNode->pMaster = pMaster; - sDebug("%s, it is the master, replica:%d sver:%" PRIu64, pMaster->id, pNode->replica, pMaster->version); - - if (syncValidateMaster(pPeer) < 0) return; - - if (nodeRole == TAOS_SYNC_ROLE_UNSYNCED) { - if (nodeVersion < pMaster->version) { - sDebug("%s, is master, sync required, self sver:%" PRIu64, pMaster->id, nodeVersion); - syncRequired = 1; - } else { - sInfo("%s, is master, work as slave, self sver:%" PRIu64, pMaster->id, nodeVersion); - nodeRole = TAOS_SYNC_ROLE_SLAVE; - (*pNode->notifyRoleFp)(pNode->vgId, nodeRole); - } - } else if (nodeRole == TAOS_SYNC_ROLE_SLAVE && pMaster == pPeer) { - sDebug("%s, is master, continue work as slave, self sver:%" PRIu64, pMaster->id, nodeVersion); - } - } else { - // master not there, if all peer's state and version are consistent, choose the master - int32_t consistent = 0; - int32_t index = 0; - if (peersStatus != NULL) { - for (index = 0; index < pNode->replica; ++index) { - SSyncPeer *pTemp = pNode->peerInfo[index]; - if (pTemp->role != peersStatus[index].role) break; - if ((pTemp->role != TAOS_SYNC_ROLE_OFFLINE) && (pTemp->version != peersStatus[index].version)) break; - } - - if (index >= pNode->replica) consistent = 1; - } else { - if (pNode->replica == 2) consistent = 1; - } - - if (consistent) { - sDebug("vgId:%d, choose master, replica:%d", pNode->vgId, pNode->replica); - syncChooseMaster(pNode); - } else { - sDebug("vgId:%d, cannot choose master since roles inequality, replica:%d", pNode->vgId, pNode->replica); - } - } - - if (syncRequired) { - syncRecoverFromMaster(pMaster); - } - - if (oldPeerRole != newPeerRole || nodeRole != oldSelfRole) { - sDebug("vgId:%d, roles changed, broadcast status, replica:%d", pNode->vgId, pNode->replica); - syncBroadcastStatus(pNode); - } - - if (nodeRole != TAOS_SYNC_ROLE_MASTER) { - syncResetFlowCtrl(pNode); - } -} - -static void syncRestartPeer(SSyncPeer *pPeer) { - sDebug("%s, restart peer connection, last sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]); - - syncClosePeerConn(pPeer); - - pPeer->sstatus = TAOS_SYNC_STATUS_INIT; - sDebug("%s, peer conn is restart and set sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]); - - int32_t ret = strcmp(pPeer->fqdn, tsNodeFqdn); - if (pPeer->nodeId == 0 || ret > 0 || (ret == 0 && pPeer->port > tsSyncPort)) { - sDebug("%s, check peer connection in 1000 ms", pPeer->id); - taosTmrReset(syncCheckPeerConnection, SYNC_CHECK_INTERVAL, (void *)pPeer->rid, tsSyncTmrCtrl, &pPeer->timer); - } -} - -void syncRestartConnection(SSyncPeer *pPeer) { - if (pPeer->fqdn[0] == '\0') return; - - if (syncAcquirePeer(pPeer->rid) == NULL) return; - - syncRestartPeer(pPeer); - syncCheckRole(pPeer, NULL, TAOS_SYNC_ROLE_OFFLINE); - - syncReleasePeer(pPeer); -} - -static void syncProcessSyncRequest(char *msg, SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - sInfo("%s, sync-req is received", pPeer->id); - - if (pPeer->fqdn[0] == '\0') return; - - if (nodeRole != TAOS_SYNC_ROLE_MASTER) { - sError("%s, I am not master anymore", pPeer->id); - taosCloseSocket(pPeer->syncFd); - return; - } - - if (pPeer->sstatus != TAOS_SYNC_STATUS_INIT) { - sDebug("%s, sync is already started for sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]); - return; // already started - } - - // start a new thread to retrieve the data - (void)syncAcquirePeer(pPeer->rid); - - pthread_attr_t thattr; - pthread_t thread; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_DETACHED); - int32_t ret = pthread_create(&thread, &thattr, syncRetrieveData, (void *)pPeer->rid); - pthread_attr_destroy(&thattr); - - if (ret < 0) { - sError("%s, failed to create sync retrieve thread since %s", pPeer->id, strerror(errno)); - syncReleasePeer(pPeer); - } else { - pPeer->sstatus = TAOS_SYNC_STATUS_START; - sDebug("%s, sync retrieve thread:0x%08" PRIx64 " create successfully, rid:%" PRId64 ", set sstatus:%s", pPeer->id, - taosGetPthreadId(thread), pPeer->rid, syncStatus[pPeer->sstatus]); - } -} - -static void syncNotStarted(void *param, void *tmrId) { - int64_t rid = (int64_t)param; - SSyncPeer *pPeer = syncAcquirePeer(rid); - if (pPeer == NULL) return; - - SSyncNode *pNode = pPeer->pSyncNode; - - pthread_mutex_lock(&pNode->mutex); - pPeer->timer = NULL; - pPeer->sstatus = TAOS_SYNC_STATUS_INIT; - sInfo("%s, sync conn is still not up, restart and set sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]); - syncRestartConnection(pPeer); - pthread_mutex_unlock(&pNode->mutex); - - syncReleasePeer(pPeer); -} - -static void syncTryRecoverFromMaster(void *param, void *tmrId) { - int64_t rid = (int64_t)param; - SSyncPeer *pPeer = syncAcquirePeer(rid); - if (pPeer == NULL) return; - - SSyncNode *pNode = pPeer->pSyncNode; - - pthread_mutex_lock(&pNode->mutex); - syncRecoverFromMaster(pPeer); - pthread_mutex_unlock(&pNode->mutex); - - syncReleasePeer(pPeer); -} - -static void syncRecoverFromMaster(SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - - if (nodeSStatus != TAOS_SYNC_STATUS_INIT) { - sDebug("%s, sync is already started since sstatus:%s", pPeer->id, syncStatus[nodeSStatus]); - return; - } - - taosTmrStopA(&pPeer->timer); - - // Ensure the sync of mnode not interrupted - if (pNode->vgId != 1 && tsSyncNum >= SYNC_MAX_NUM) { - sInfo("%s, %d syncs are in process, try later", pPeer->id, tsSyncNum); - taosTmrReset(syncTryRecoverFromMaster, 500 + (pNode->vgId * 10) % 200, (void *)pPeer->rid, tsSyncTmrCtrl, &pPeer->timer); - return; - } - - sDebug("%s, try to sync", pPeer->id); - - SSyncMsg msg; - syncBuildSyncReqMsg(&msg, pNode->vgId); - - taosTmrReset(syncNotStarted, SYNC_CHECK_INTERVAL, (void *)pPeer->rid, tsSyncTmrCtrl, &pPeer->timer); - - if (taosWriteMsg(pPeer->peerFd, &msg, sizeof(SSyncMsg)) != sizeof(SSyncMsg)) { - sError("%s, failed to send sync-req to peer", pPeer->id); - } else { - sInfo("%s, sync-req is sent to peer, tranId:%u, sstatus:%s", pPeer->id, msg.tranId, syncStatus[nodeSStatus]); - } -} - -static void syncProcessFwdResponse(SFwdRsp *pFwdRsp, SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - SSyncFwds *pSyncFwds = pNode->pSyncFwds; - SFwdInfo * pFwdInfo; - - sTrace("%s, forward-rsp is received, code:%x hver:%" PRIu64, pPeer->id, pFwdRsp->code, pFwdRsp->version); - SFwdInfo *pFirst = pSyncFwds->fwdInfo + pSyncFwds->first; - - if (pFirst->version <= pFwdRsp->version && pSyncFwds->fwds > 0) { - // find the forwardInfo from first - for (int32_t i = 0; i < pSyncFwds->fwds; ++i) { - pFwdInfo = pSyncFwds->fwdInfo + (i + pSyncFwds->first) % SYNC_MAX_FWDS; - if (pFwdRsp->version == pFwdInfo->version) { - syncProcessFwdAck(pNode, pFwdInfo, pFwdRsp->code); - syncRemoveConfirmedFwdInfo(pNode); - return; - } - } - } -} - -static void syncProcessForwardFromPeer(char *cont, SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - SWalHead * pHead = (SWalHead *)(cont + sizeof(SSyncHead)); - - sTrace("%s, forward is received, hver:%" PRIu64 ", len:%d", pPeer->id, pHead->version, pHead->len); - - int32_t code = 0; - if (nodeRole == TAOS_SYNC_ROLE_SLAVE) { - // nodeVersion = pHead->version; - code = (*pNode->writeToCacheFp)(pNode->vgId, pHead, TAOS_QTYPE_FWD, NULL); - syncConfirmForward(pNode->rid, pHead->version, code, false); - } else { - if (nodeSStatus != TAOS_SYNC_STATUS_INIT) { - code = syncSaveIntoBuffer(pPeer, pHead); - } else { - sError("%s, forward discarded since sstatus:%s, hver:%" PRIu64, pPeer->id, syncStatus[nodeSStatus], - pHead->version); - code = -1; - } - } - - if (code != 0) { - sError("%s, failed to process fwd msg, hver:%" PRIu64 ", len:%d", pPeer->id, pHead->version, pHead->len); - syncRestartConnection(pPeer); - } -} - -static void syncProcessPeersStatusMsg(SPeersStatus *pPeersStatus, SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - - sDebug("%s, status is received, self:%s:%s:%" PRIu64 ", peer:%s:%" PRIu64 ", ack:%d tranId:%u type:%s pfd:%d", - pPeer->id, syncRole[nodeRole], syncStatus[nodeSStatus], nodeVersion, syncRole[pPeersStatus->role], - pPeersStatus->version, pPeersStatus->ack, pPeersStatus->tranId, statusType[pPeersStatus->type], pPeer->peerFd); - - pPeer->version = pPeersStatus->version; - syncCheckRole(pPeer, pPeersStatus->peersStatus, pPeersStatus->role); - - if (pPeersStatus->ack) { - syncSendPeersStatusMsgToPeer(pPeer, 0, pPeersStatus->type + 1, pPeersStatus->tranId); - } -} - -static int32_t syncReadPeerMsg(SSyncPeer *pPeer, SSyncHead *pHead) { - if (pPeer->peerFd < 0) return -1; - - int32_t hlen = taosReadMsg(pPeer->peerFd, pHead, sizeof(SSyncHead)); - if (hlen != sizeof(SSyncHead)) { - sDebug("%s, failed to read msg since %s, hlen:%d", pPeer->id, tstrerror(errno), hlen); - return -1; - } - - int32_t code = syncCheckHead(pHead); - if (code != 0) { - sError("%s, failed to check msg head since %s, type:%d", pPeer->id, tstrerror(code), pHead->type); - return -1; - } - - int32_t bytes = taosReadMsg(pPeer->peerFd, (char *)pHead + sizeof(SSyncHead), pHead->len); - if (bytes != pHead->len) { - sError("%s, failed to read, bytes:%d len:%d", pPeer->id, bytes, pHead->len); - return -1; - } - - return 0; -} - -static int32_t syncProcessPeerMsg(int64_t rid, void *buffer) { - SSyncPeer *pPeer = syncAcquirePeer(rid); - if (pPeer == NULL) return -1; - - SSyncHead *pHead = buffer; - SSyncNode *pNode = pPeer->pSyncNode; - - pthread_mutex_lock(&pNode->mutex); - - int32_t code = syncReadPeerMsg(pPeer, pHead); - - if (code == 0) { - if (pHead->type == TAOS_SMSG_SYNC_FWD) { - syncProcessForwardFromPeer(buffer, pPeer); - } else if (pHead->type == TAOS_SMSG_SYNC_FWD_RSP) { - syncProcessFwdResponse(buffer, pPeer); - } else if (pHead->type == TAOS_SMSG_SYNC_REQ) { - syncProcessSyncRequest(buffer, pPeer); - } else if (pHead->type == TAOS_SMSG_STATUS) { - syncProcessPeersStatusMsg(buffer, pPeer); - } - } - - pthread_mutex_unlock(&pNode->mutex); - syncReleasePeer(pPeer); - - return code; -} - -static int32_t syncSendPeersStatusMsgToPeer(SSyncPeer *pPeer, char ack, int8_t type, uint16_t tranId) { - if (pPeer->peerFd < 0 || pPeer->fqdn[0] == '\0') { - sDebug("%s, failed to send status msg, restart fd:%d", pPeer->id, pPeer->peerFd); - syncRestartConnection(pPeer); - return -1; - } - - SSyncNode * pNode = pPeer->pSyncNode; - SPeersStatus msg; - - memset(&msg, 0, sizeof(SPeersStatus)); - syncBuildPeersStatus(&msg, pNode->vgId); - - msg.role = nodeRole; - msg.ack = ack; - msg.type = type; - msg.tranId = tranId; - msg.version = nodeVersion; - - for (int32_t i = 0; i < pNode->replica; ++i) { - msg.peersStatus[i].role = pNode->peerInfo[i]->role; - msg.peersStatus[i].version = pNode->peerInfo[i]->version; - } - - if (taosWriteMsg(pPeer->peerFd, &msg, sizeof(SPeersStatus)) == sizeof(SPeersStatus)) { - sDebug("%s, status is sent, self:%s:%s:%" PRIu64 ", peer:%s:%s:%" PRIu64 ", ack:%d tranId:%u type:%s pfd:%d", - pPeer->id, syncRole[nodeRole], syncStatus[nodeSStatus], nodeVersion, syncRole[pPeer->role], - syncStatus[pPeer->sstatus], pPeer->version, ack, tranId, statusType[type], pPeer->peerFd); - return 0; - } else { - sDebug("%s, failed to send status msg, restart", pPeer->id); - syncRestartConnection(pPeer); - return -1; - } -} - -static void syncSetupPeerConnection(SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - - taosTmrStopA(&pPeer->timer); - if (pPeer->peerFd >= 0) { - sDebug("%s, send role version to peer", pPeer->id); - syncSendPeersStatusMsgToPeer(pPeer, 1, SYNC_STATUS_SETUP_CONN, syncGenTranId()); - return; - } - - uint32_t ip = syncResolvePeerFqdn(pPeer); - if (!ip) { - taosTmrReset(syncCheckPeerConnection, SYNC_CHECK_INTERVAL, (void *)pPeer->rid, tsSyncTmrCtrl, &pPeer->timer); - return; - } - - SOCKET connFd = taosOpenTcpClientSocket(ip, pPeer->port, 0); - if (connFd <= 0) { - sDebug("%s, failed to open tcp socket since %s", pPeer->id, strerror(errno)); - taosTmrReset(syncCheckPeerConnection, SYNC_CHECK_INTERVAL, (void *)pPeer->rid, tsSyncTmrCtrl, &pPeer->timer); - return; - } - - SSyncMsg msg; - syncBuildSyncSetupMsg(&msg, pPeer->nodeId ? pNode->vgId : 0); - - if (taosWriteMsg(connFd, &msg, sizeof(SSyncMsg)) == sizeof(SSyncMsg)) { - sDebug("%s, connection to peer server is setup, pfd:%d sfd:%d tranId:%u", pPeer->id, connFd, pPeer->syncFd, msg.tranId); - pPeer->peerFd = connFd; - pPeer->role = TAOS_SYNC_ROLE_UNSYNCED; - pPeer->pConn = syncAllocateTcpConn(tsTcpPool, pPeer->rid, connFd); - if (pPeer->isArb) { - tsArbOnline = 1; - if (tsArbOnlineTimestamp == TSDB_ARB_DUMMY_TIME) { - tsArbOnlineTimestamp = taosGetTimestampMs(); - } - } - } else { - sDebug("%s, failed to setup peer connection to server since %s, try later", pPeer->id, strerror(errno)); - taosCloseSocket(connFd); - taosTmrReset(syncCheckPeerConnection, SYNC_CHECK_INTERVAL, (void *)pPeer->rid, tsSyncTmrCtrl, &pPeer->timer); - } -} - -static void syncCheckPeerConnection(void *param, void *tmrId) { - int64_t rid = (int64_t)param; - SSyncPeer *pPeer = syncAcquirePeer(rid); - if (pPeer == NULL) return; - - SSyncNode *pNode = pPeer->pSyncNode; - - pthread_mutex_lock(&pNode->mutex); - - sDebug("%s, check peer connection", pPeer->id); - syncSetupPeerConnection(pPeer); - - pthread_mutex_unlock(&pNode->mutex); - - syncReleasePeer(pPeer); -} - -static void syncCreateRestoreDataThread(SSyncPeer *pPeer) { - taosTmrStopA(&pPeer->timer); - - pthread_attr_t thattr; - pthread_t thread; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_DETACHED); - - (void)syncAcquirePeer(pPeer->rid); - - int32_t ret = pthread_create(&thread, &thattr, (void *)syncRestoreData, (void *)pPeer->rid); - pthread_attr_destroy(&thattr); - - if (ret < 0) { - SSyncNode *pNode = pPeer->pSyncNode; - nodeSStatus = TAOS_SYNC_STATUS_INIT; - sError("%s, failed to create sync restore thread, set sstatus:%s", pPeer->id, syncStatus[nodeSStatus]); - taosCloseSocket(pPeer->syncFd); - syncReleasePeer(pPeer); - } else { - sInfo("%s, sync restore thread:0x%08" PRIx64 " create successfully, rid:%" PRId64, pPeer->id, - taosGetPthreadId(thread), pPeer->rid); - } -} - -void syncProcessTestMsg(SSyncMsg *pMsg, SOCKET connFd) { - sInfo("recv sync test msg"); - - SSyncMsg rsp; - syncBuildSyncTestMsg(&rsp, -1); - if (taosWriteMsg(connFd, &rsp, sizeof(SSyncMsg)) != sizeof(SSyncMsg)) { - sInfo("failed to send sync test rsp since %s", strerror(errno)); - } - - sInfo("send sync test rsp"); - taosMsleep(1000); - taosCloseSocket(connFd); -} - -static void syncProcessIncommingConnection(SOCKET connFd, uint32_t sourceIp) { - char ipstr[24]; - int32_t i; - - tinet_ntoa(ipstr, sourceIp); - sDebug("peer TCP connection from ip:%s", ipstr); - - SSyncMsg msg; - if (taosReadMsg(connFd, &msg, sizeof(SSyncMsg)) != sizeof(SSyncMsg)) { - sError("failed to read peer sync msg from ip:%s since %s", ipstr, strerror(errno)); - taosCloseSocket(connFd); - return; - } - - int32_t code = syncCheckHead((SSyncHead *)(&msg)); - if (code != 0) { - sError("failed to check peer sync msg from ip:%s since %s", ipstr, strerror(code)); - taosCloseSocket(connFd); - return; - } - - if (msg.head.type == TAOS_SMSG_TEST) { - syncProcessTestMsg(&msg, connFd); - return; - } - - int32_t vgId = msg.head.vgId; - SSyncNode **ppNode = taosHashGet(tsVgIdHash, &vgId, sizeof(int32_t)); - if (ppNode == NULL || *ppNode == NULL) { - sError("vgId:%d, vgId could not be found", vgId); - taosCloseSocket(connFd); - return; - } - - sDebug("vgId:%d, sync connection is incoming, tranId:%u", vgId, msg.tranId); - - SSyncNode *pNode = *ppNode; - pthread_mutex_lock(&pNode->mutex); - - SSyncPeer *pPeer; - for (i = 0; i < pNode->replica; ++i) { - pPeer = pNode->peerInfo[i]; - if (pPeer && (strcmp(pPeer->fqdn, msg.fqdn) == 0) && (pPeer->port == msg.port)) break; - } - - pPeer = (i < pNode->replica) ? pNode->peerInfo[i] : NULL; - if (pPeer == NULL) { - sError("vgId:%d, peer:%s:%u not configured", pNode->vgId, msg.fqdn, msg.port); - taosCloseSocket(connFd); - // syncSendVpeerCfgMsg(sync); - } else { - // first packet tells what kind of link - if (msg.head.type == TAOS_SMSG_SYNC_DATA) { - pPeer->syncFd = connFd; - nodeSStatus = TAOS_SYNC_STATUS_START; - sInfo("%s, sync-data msg from master is received, tranId:%u, set sstatus:%s", pPeer->id, msg.tranId, - syncStatus[nodeSStatus]); - syncCreateRestoreDataThread(pPeer); - } else { - sDebug("%s, TCP connection is up, pfd:%d sfd:%d, old pfd:%d", pPeer->id, connFd, pPeer->syncFd, pPeer->peerFd); - syncClosePeerConn(pPeer); - pPeer->peerFd = connFd; - pPeer->pConn = syncAllocateTcpConn(tsTcpPool, pPeer->rid, connFd); - sDebug("%s, ready to exchange data", pPeer->id); - syncSendPeersStatusMsgToPeer(pPeer, 1, SYNC_STATUS_EXCHANGE_DATA, syncGenTranId()); - } - } - - pthread_mutex_unlock(&pNode->mutex); -} - -static void syncProcessBrokenLink(int64_t rid, int32_t closedByApp) { - SSyncPeer *pPeer = syncAcquirePeer(rid); - if (pPeer == NULL) return; - - SSyncNode *pNode = pPeer->pSyncNode; - - pthread_mutex_lock(&pNode->mutex); - - sDebug("%s, TCP link is broken since %s, pfd:%d sfd:%d closedByApp:%d", - pPeer->id, strerror(errno), pPeer->peerFd, pPeer->syncFd, closedByApp); - pPeer->peerFd = -1; - if (!closedByApp && pPeer->isArb) { - tsArbOnline = 0; - } - - syncRestartConnection(pPeer); - pthread_mutex_unlock(&pNode->mutex); - - syncReleasePeer(pPeer); -} - -static int32_t syncSaveFwdInfo(SSyncNode *pNode, uint64_t _version, void *mhandle) { - SSyncFwds *pSyncFwds = pNode->pSyncFwds; - int64_t time = taosGetTimestampMs(); - - if (pSyncFwds->fwds >= SYNC_MAX_FWDS) { - // pSyncFwds->first = (pSyncFwds->first + 1) % SYNC_MAX_FWDS; - // pSyncFwds->fwds--; - sError("vgId:%d, failed to save fwd info, hver:%" PRIu64 " fwds:%d", pNode->vgId, _version, pSyncFwds->fwds); - return TSDB_CODE_SYN_TOO_MANY_FWDINFO; - } - - if (pSyncFwds->fwds > 0) { - pSyncFwds->last = (pSyncFwds->last + 1) % SYNC_MAX_FWDS; - } - - SFwdInfo *pFwdInfo = pSyncFwds->fwdInfo + pSyncFwds->last; - memset(pFwdInfo, 0, sizeof(SFwdInfo)); - pFwdInfo->version = _version; - pFwdInfo->mhandle = mhandle; - pFwdInfo->time = time; - - pSyncFwds->fwds++; - sTrace("vgId:%d, fwd info is saved, hver:%" PRIu64 " fwds:%d ", pNode->vgId, _version, pSyncFwds->fwds); - - return 0; -} - -static void syncRemoveConfirmedFwdInfo(SSyncNode *pNode) { - SSyncFwds *pSyncFwds = pNode->pSyncFwds; - - int32_t fwds = pSyncFwds->fwds; - for (int32_t i = 0; i < fwds; ++i) { - SFwdInfo *pFwdInfo = pSyncFwds->fwdInfo + pSyncFwds->first; - if (pFwdInfo->confirmed == 0) break; - - pSyncFwds->first = (pSyncFwds->first + 1) % SYNC_MAX_FWDS; - pSyncFwds->fwds--; - if (pSyncFwds->fwds == 0) pSyncFwds->first = pSyncFwds->last; - sTrace("vgId:%d, fwd info is removed, hver:%" PRIu64 " fwds:%d", pNode->vgId, pFwdInfo->version, pSyncFwds->fwds); - memset(pFwdInfo, 0, sizeof(SFwdInfo)); - } -} - -static void syncProcessFwdAck(SSyncNode *pNode, SFwdInfo *pFwdInfo, int32_t code) { - int32_t confirm = 0; - if (pFwdInfo->code == 0) pFwdInfo->code = code; - - if (code == 0) { - pFwdInfo->acks++; - if (pFwdInfo->acks >= pNode->quorum - 1) { - confirm = 1; - } - } else { - pFwdInfo->nacks++; - if (pFwdInfo->nacks > pNode->replica - pNode->quorum) { - confirm = 1; - } - } - - if (confirm && pFwdInfo->confirmed == 0) { - sTrace("vgId:%d, forward is confirmed, hver:%" PRIu64 " code:0x%x", pNode->vgId, pFwdInfo->version, pFwdInfo->code); - (*pNode->confirmForward)(pNode->vgId, pFwdInfo->mhandle, pFwdInfo->code); - pFwdInfo->confirmed = 1; - } -} - -static void syncMonitorNodeRole(void *param, void *tmrId) { - int64_t rid = (int64_t)param; - SSyncNode *pNode = syncAcquireNode(rid); - if (pNode == NULL) return; - - for (int32_t index = 0; index < pNode->replica; index++) { - if (index == pNode->selfIndex) continue; - - SSyncPeer *pPeer = pNode->peerInfo[index]; - if (/*pPeer->role > TAOS_SYNC_ROLE_UNSYNCED && */ nodeRole > TAOS_SYNC_ROLE_UNSYNCED) continue; - if (/*pPeer->sstatus > TAOS_SYNC_STATUS_INIT || */ nodeSStatus > TAOS_SYNC_STATUS_INIT) continue; - - sDebug("%s, check roles since peer:%s sstatus:%s, self:%s sstatus:%s", pPeer->id, syncRole[pPeer->role], - syncStatus[pPeer->sstatus], syncRole[nodeRole], syncStatus[nodeSStatus]); - syncSendPeersStatusMsgToPeer(pPeer, 1, SYNC_STATUS_CHECK_ROLE, syncGenTranId()); - break; - } - - pNode->pRoleTimer = taosTmrStart(syncMonitorNodeRole, SYNC_ROLE_TIMER, (void *)pNode->rid, tsSyncTmrCtrl); - syncReleaseNode(pNode); -} - -static void syncMonitorFwdInfos(void *param, void *tmrId) { - int64_t rid = (int64_t)param; - SSyncNode *pNode = syncAcquireNode(rid); - if (pNode == NULL) return; - - SSyncFwds *pSyncFwds = pNode->pSyncFwds; - - if (pSyncFwds) { - int64_t time = taosGetTimestampMs(); - - if (pSyncFwds->fwds > 0) { - pthread_mutex_lock(&pNode->mutex); - for (int32_t i = 0; i < pSyncFwds->fwds; ++i) { - SFwdInfo *pFwdInfo = pSyncFwds->fwdInfo + (pSyncFwds->first + i) % SYNC_MAX_FWDS; - if (ABS(time - pFwdInfo->time) < 10000) break; - - sDebug("vgId:%d, forward info expired, hver:%" PRIu64 " curtime:%" PRIu64 " savetime:%" PRIu64, pNode->vgId, - pFwdInfo->version, time, pFwdInfo->time); - syncProcessFwdAck(pNode, pFwdInfo, TSDB_CODE_SYN_CONFIRM_EXPIRED); - } - - syncRemoveConfirmedFwdInfo(pNode); - pthread_mutex_unlock(&pNode->mutex); - } - - pNode->pFwdTimer = taosTmrStart(syncMonitorFwdInfos, SYNC_FWD_TIMER, (void *)pNode->rid, tsSyncTmrCtrl); - } - - syncReleaseNode(pNode); -} - -static int32_t syncForwardToPeerImpl(SSyncNode *pNode, void *data, void *mhandle, int32_t qtype, bool force) { - SSyncPeer *pPeer; - SSyncHead *pSyncHead; - SWalHead * pWalHead = data; - int32_t fwdLen; - int32_t code = 0; - - if (pWalHead->version > nodeVersion + 1) { - sError("vgId:%d, hver:%" PRIu64 ", inconsistent with sver:%" PRIu64, pNode->vgId, pWalHead->version, nodeVersion); - if (nodeRole == TAOS_SYNC_ROLE_SLAVE) { - sInfo("vgId:%d, restart connection", pNode->vgId); - for (int32_t i = 0; i < pNode->replica; ++i) { - pPeer = pNode->peerInfo[i]; - syncRestartConnection(pPeer); - } - } - - if (pNode->replica != 1) { - return TSDB_CODE_SYN_INVALID_VERSION; - } - } - - // always update version - sTrace("vgId:%d, update version, replica:%d role:%s qtype:%s hver:%" PRIu64, pNode->vgId, pNode->replica, - syncRole[nodeRole], qtypeStr[qtype], pWalHead->version); - nodeVersion = pWalHead->version; - - if (pNode->replica == 1 || nodeRole != TAOS_SYNC_ROLE_MASTER) return 0; - - // only msg from RPC or CQ can be forwarded - if (qtype != TAOS_QTYPE_RPC && qtype != TAOS_QTYPE_CQ) return 0; - - // a hacker way to improve the performance - pSyncHead = (SSyncHead *)(((char *)pWalHead) - sizeof(SSyncHead)); - syncBuildSyncFwdMsg(pSyncHead, pNode->vgId, sizeof(SWalHead) + pWalHead->len); - fwdLen = pSyncHead->len + sizeof(SSyncHead); // include the WAL and SYNC head - - pthread_mutex_lock(&pNode->mutex); - - for (int32_t i = 0; i < pNode->replica; ++i) { - pPeer = pNode->peerInfo[i]; - if (pPeer == NULL || pPeer->peerFd < 0) continue; - if (pPeer->role != TAOS_SYNC_ROLE_SLAVE && pPeer->sstatus != TAOS_SYNC_STATUS_CACHE) continue; - - if ((pNode->quorum > 1 || force) && code == 0) { - code = syncSaveFwdInfo(pNode, pWalHead->version, mhandle); - if (code >= 0) { - code = 1; - } else { - pthread_mutex_unlock(&pNode->mutex); - return code; - } - } - - int32_t retLen = taosWriteMsg(pPeer->peerFd, pSyncHead, fwdLen); - if (retLen == fwdLen) { - sTrace("%s, forward is sent, role:%s sstatus:%s hver:%" PRIu64 " contLen:%d", pPeer->id, syncRole[pPeer->role], - syncStatus[pPeer->sstatus], pWalHead->version, pWalHead->len); - } else { - sError("%s, failed to forward, role:%s sstatus:%s hver:%" PRIu64 " retLen:%d", pPeer->id, syncRole[pPeer->role], - syncStatus[pPeer->sstatus], pWalHead->version, retLen); - syncRestartConnection(pPeer); - } - } - - pthread_mutex_unlock(&pNode->mutex); - - return code; -} diff --git a/src/sync/src/syncMsg.c b/src/sync/src/syncMsg.c deleted file mode 100644 index 64d4e72fac..0000000000 --- a/src/sync/src/syncMsg.c +++ /dev/null @@ -1,110 +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 "tglobal.h" -#include "tchecksum.h" -#include "syncInt.h" - -char *statusType[] = { - "broadcast", - "broadcast-rsp", - "setup-conn", - "setup-conn-rsp", - "exchange-data", - "exchange-data-rsp", - "check-role", - "check-role-rsp" -}; - -uint16_t syncGenTranId() { - return taosRand() & 0XFFFF; -} - -static void syncBuildHead(SSyncHead *pHead) { - pHead->protocol = SYNC_PROTOCOL_VERSION; - pHead->signature = SYNC_SIGNATURE; - pHead->code = 0; - pHead->cId = 0; - taosCalcChecksumAppend(0, (uint8_t *)pHead, sizeof(SSyncHead)); -} - -int32_t syncCheckHead(SSyncHead *pHead) { - if (pHead->protocol != SYNC_PROTOCOL_VERSION) return TSDB_CODE_SYN_MISMATCHED_PROTOCOL; - if (pHead->signature != SYNC_SIGNATURE) return TSDB_CODE_SYN_MISMATCHED_SIGNATURE; - if (pHead->cId != 0) return TSDB_CODE_SYN_MISMATCHED_CLUSTERID; - if (pHead->len <= 0 || pHead->len > TSDB_MAX_WAL_SIZE) return TSDB_CODE_SYN_INVALID_MSGLEN; - if (pHead->type <= TAOS_SMSG_START || pHead->type >= TAOS_SMSG_END) return TSDB_CODE_SYN_INVALID_MSGTYPE; - if (!taosCheckChecksumWhole((uint8_t *)pHead, sizeof(SSyncHead))) return TSDB_CODE_SYN_INVALID_CHECKSUM; - - return TSDB_CODE_SUCCESS; -} - -void syncBuildSyncFwdMsg(SSyncHead *pHead, int32_t vgId, int32_t len) { - pHead->type = TAOS_SMSG_SYNC_FWD; - pHead->vgId = vgId; - pHead->len = len; - syncBuildHead(pHead); -} - -void syncBuildSyncFwdRsp(SFwdRsp *pMsg, int32_t vgId, uint64_t _version, int32_t code) { - pMsg->head.type = TAOS_SMSG_SYNC_FWD_RSP; - pMsg->head.vgId = vgId; - pMsg->head.len = sizeof(SFwdRsp) - sizeof(SSyncHead); - syncBuildHead(&pMsg->head); - - pMsg->version = _version; - pMsg->code = code; -} - -static void syncBuildMsg(SSyncMsg *pMsg, int32_t vgId, ESyncMsgType type) { - pMsg->head.type = type; - pMsg->head.vgId = vgId; - pMsg->head.len = sizeof(SSyncMsg) - sizeof(SSyncHead); - syncBuildHead(&pMsg->head); - - pMsg->port = tsSyncPort; - pMsg->tranId = syncGenTranId(); - pMsg->sourceId = vgId; - tstrncpy(pMsg->fqdn, tsNodeFqdn, TSDB_FQDN_LEN); -} - -void syncBuildSyncReqMsg(SSyncMsg *pMsg, int32_t vgId) { syncBuildMsg(pMsg, vgId, TAOS_SMSG_SYNC_REQ); } -void syncBuildSyncDataMsg(SSyncMsg *pMsg, int32_t vgId) { syncBuildMsg(pMsg, vgId, TAOS_SMSG_SYNC_DATA); } -void syncBuildSyncSetupMsg(SSyncMsg *pMsg, int32_t vgId) { syncBuildMsg(pMsg, vgId, TAOS_SMSG_SETUP); } -void syncBuildSyncTestMsg(SSyncMsg *pMsg, int32_t vgId) { syncBuildMsg(pMsg, vgId, TAOS_SMSG_TEST); } - -void syncBuildPeersStatus(SPeersStatus *pMsg, int32_t vgId) { - pMsg->head.type = TAOS_SMSG_STATUS; - pMsg->head.vgId = vgId; - pMsg->head.len = sizeof(SPeersStatus) - sizeof(SSyncHead); - syncBuildHead(&pMsg->head); -} - -void syncBuildFileAck(SFileAck *pMsg, int32_t vgId) { - pMsg->head.type = TAOS_SMSG_SYNC_FILE_RSP; - pMsg->head.vgId = vgId; - pMsg->head.len = sizeof(SFileAck) - sizeof(SSyncHead); - syncBuildHead(&pMsg->head); -} - -void syncBuildFileVersion(SFileVersion *pMsg, int32_t vgId) { - pMsg->head.type = TAOS_SMSG_SYNC_FILE; - pMsg->head.vgId = vgId; - pMsg->head.len = sizeof(SFileVersion) - sizeof(SSyncHead); - syncBuildHead(&pMsg->head); -} \ No newline at end of file diff --git a/src/sync/src/syncRestore.c b/src/sync/src/syncRestore.c deleted file mode 100644 index bf9d5201a0..0000000000 --- a/src/sync/src/syncRestore.c +++ /dev/null @@ -1,312 +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 "tlog.h" -#include "tutil.h" -#include "ttimer.h" -#include "tsocket.h" -#include "tqueue.h" -#include "twal.h" -#include "tsync.h" -#include "syncInt.h" - -static int32_t syncRecvFileVersion(SSyncPeer *pPeer, uint64_t *fversion) { - SSyncNode *pNode = pPeer->pSyncNode; - - SFileVersion fileVersion; - memset(&fileVersion, 0, sizeof(SFileVersion)); - int32_t ret = taosReadMsg(pPeer->syncFd, &fileVersion, sizeof(SFileVersion)); - if (ret != sizeof(SFileVersion)) { - sError("%s, failed to read fver since %s", pPeer->id, strerror(errno)); - return -1; - } - - SFileAck fileVersionAck; - memset(&fileVersionAck, 0, sizeof(SFileAck)); - syncBuildFileAck(&fileVersionAck, pNode->vgId); - ret = taosWriteMsg(pPeer->syncFd, &fileVersionAck, sizeof(SFileAck)); - if (ret != sizeof(SFileAck)) { - sError("%s, failed to write fver ack since %s", pPeer->id, strerror(errno)); - return -1; - } - - *fversion = htobe64(fileVersion.fversion); - return 0; -} - -static int32_t syncRestoreFile(SSyncPeer *pPeer, uint64_t *fversion) { - SSyncNode *pNode = pPeer->pSyncNode; - - if (pNode->recvFileFp && (*pNode->recvFileFp)(pNode->pTsdb, pPeer->syncFd) != 0) { - sError("%s, failed to restore file", pPeer->id); - return -1; - } - - if (syncRecvFileVersion(pPeer, fversion) < 0) { - return -1; - } - - sInfo("%s, all files are restored, fver:%" PRIu64, pPeer->id, *fversion); - return 0; -} - -static int32_t syncRestoreWal(SSyncPeer *pPeer, uint64_t *wver) { - SSyncNode *pNode = pPeer->pSyncNode; - int32_t ret, code = -1; - uint64_t lastVer = 0; - - SWalHead *pHead = calloc(SYNC_MAX_SIZE, 1); // size for one record - if (pHead == NULL) return -1; - - while (1) { - ret = taosReadMsg(pPeer->syncFd, pHead, sizeof(SWalHead)); - if (ret != sizeof(SWalHead)) { - sError("%s, failed to read walhead while restore wal since %s", pPeer->id, strerror(errno)); - break; - } - - if (pHead->len == 0) { - sDebug("%s, wal is synced over, last wver:%" PRIu64, pPeer->id, lastVer); - code = 0; - break; - } // wal sync over - - ret = taosReadMsg(pPeer->syncFd, pHead->cont, pHead->len); - if (ret != pHead->len) { - sError("%s, failed to read walcont, len:%d while restore wal since %s", pPeer->id, pHead->len, strerror(errno)); - break; - } - - sTrace("%s, restore a record, qtype:wal len:%d hver:%" PRIu64, pPeer->id, pHead->len, pHead->version); - - if (lastVer == pHead->version) { - sError("%s, failed to restore record, same hver:%" PRIu64 ", wal sync failed" PRIu64, pPeer->id, lastVer); - break; - } - lastVer = pHead->version; - - ret = (*pNode->writeToCacheFp)(pNode->vgId, pHead, TAOS_QTYPE_WAL, NULL); - if (ret != 0) { - sError("%s, failed to restore record since %s, hver:%" PRIu64, pPeer->id, tstrerror(ret), pHead->version); - break; - } - } - - if (code < 0) { - sError("%s, failed to restore wal from syncFd:%d since %s", pPeer->id, pPeer->syncFd, strerror(errno)); - } - - free(pHead); - *wver = lastVer; - return code; -} - -static char *syncProcessOneBufferedFwd(SSyncPeer *pPeer, char *offset) { - SSyncNode *pNode = pPeer->pSyncNode; - SWalHead * pHead = (SWalHead *)offset; - - (*pNode->writeToCacheFp)(pNode->vgId, pHead, TAOS_QTYPE_FWD, NULL); - offset += pHead->len + sizeof(SWalHead); - - return offset; -} - -static int32_t syncProcessBufferedFwd(SSyncPeer *pPeer) { - SSyncNode * pNode = pPeer->pSyncNode; - SRecvBuffer *pRecv = pNode->pRecv; - int32_t forwards = 0; - - if (pRecv == NULL) { - sError("%s, recv buffer is null, restart connect", pPeer->id); - return -1; - } - - sDebug("%s, number of buffered forwards:%d", pPeer->id, pRecv->forwards); - - char *offset = pRecv->buffer; - while (forwards < pRecv->forwards) { - offset = syncProcessOneBufferedFwd(pPeer, offset); - forwards++; - } - - pthread_mutex_lock(&pNode->mutex); - - while (forwards < pRecv->forwards && pRecv->code == 0) { - offset = syncProcessOneBufferedFwd(pPeer, offset); - forwards++; - } - - nodeRole = TAOS_SYNC_ROLE_SLAVE; - sDebug("%s, finish processing buffered fwds:%d", pPeer->id, forwards); - - pthread_mutex_unlock(&pNode->mutex); - - return pRecv->code; -} - -int32_t syncSaveIntoBuffer(SSyncPeer *pPeer, SWalHead *pHead) { - SSyncNode * pNode = pPeer->pSyncNode; - SRecvBuffer *pRecv = pNode->pRecv; - int32_t len = pHead->len + sizeof(SWalHead); - - if (pRecv == NULL) { - sError("%s, recv buffer is not create yet", pPeer->id); - return -1; - } - - if (pRecv->bufferSize - (pRecv->offset - pRecv->buffer) >= len) { - memcpy(pRecv->offset, pHead, len); - pRecv->offset += len; - pRecv->forwards++; - sTrace("%s, fwd is saved into queue, hver:%" PRIu64 " fwds:%d", pPeer->id, pHead->version, pRecv->forwards); - } else { - sError("%s, buffer size:%d is too small", pPeer->id, pRecv->bufferSize); - pRecv->code = -1; // set error code - } - - return pRecv->code; -} - -static void syncCloseRecvBuffer(SSyncNode *pNode) { - if (pNode->pRecv) { - sDebug("vgId:%d, recv buffer:%p is freed", pNode->vgId, pNode->pRecv); - tfree(pNode->pRecv->buffer); - } - - tfree(pNode->pRecv); -} - -static int32_t syncOpenRecvBuffer(SSyncNode *pNode) { - syncCloseRecvBuffer(pNode); - - SRecvBuffer *pRecv = calloc(sizeof(SRecvBuffer), 1); - if (pRecv == NULL) return -1; - - pRecv->bufferSize = SYNC_RECV_BUFFER_SIZE; - pRecv->buffer = malloc(pRecv->bufferSize); - if (pRecv->buffer == NULL) { - free(pRecv); - return -1; - } - - pRecv->offset = pRecv->buffer; - pRecv->forwards = 0; - - pNode->pRecv = pRecv; - - sDebug("vgId:%d, recv buffer:%p is created", pNode->vgId, pNode->pRecv); - return 0; -} - -static int32_t syncRestoreDataStepByStep(SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - nodeSStatus = TAOS_SYNC_STATUS_FILE; - uint64_t fversion = 0; - - sInfo("%s, start to restore, sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]); - SSyncRsp rsp = {.sync = 1, .tranId = syncGenTranId()}; - if (taosWriteMsg(pPeer->syncFd, &rsp, sizeof(SSyncRsp)) != sizeof(SSyncRsp)) { - sError("%s, failed to send sync rsp since %s", pPeer->id, strerror(errno)); - return -1; - } - sDebug("%s, send sync rsp to peer, tranId:%u", pPeer->id, rsp.tranId); - - sInfo("%s, start to restore file, set sstatus:%s", pPeer->id, syncStatus[nodeSStatus]); - (*pNode->startSyncFileFp)(pNode->vgId); - - int32_t code = syncRestoreFile(pPeer, &fversion); - if (code < 0) { - (*pNode->stopSyncFileFp)(pNode->vgId, fversion); - sError("%s, failed to restore files", pPeer->id); - return -1; - } - - (*pNode->stopSyncFileFp)(pNode->vgId, fversion); - nodeVersion = fversion; - - sInfo("%s, start to restore wal, fver:%" PRIu64, pPeer->id, nodeVersion); - uint64_t wver = 0; - code = syncRestoreWal(pPeer, &wver); // lastwar - if (code < 0) { - sError("%s, failed to restore wal, code:%d", pPeer->id, code); - return -1; - } - - if (wver != 0) { - nodeVersion = wver; - sDebug("%s, restore wal finished, set sver:%" PRIu64, pPeer->id, nodeVersion); - } - - nodeSStatus = TAOS_SYNC_STATUS_CACHE; - sInfo("%s, start to insert buffered points, set sstatus:%s", pPeer->id, syncStatus[nodeSStatus]); - if (syncProcessBufferedFwd(pPeer) < 0) { - sError("%s, failed to insert buffered points", pPeer->id); - return -1; - } - - return 0; -} - -void *syncRestoreData(void *param) { - setThreadName("syncRestoreData"); - int64_t rid = (int64_t)param; - SSyncPeer *pPeer = syncAcquirePeer(rid); - if (pPeer == NULL) { - sError("failed to restore data, invalid peer rid:%" PRId64, rid); - return NULL; - } - - SSyncNode *pNode = pPeer->pSyncNode; - - taosBlockSIGPIPE(); - atomic_add_fetch_32(&tsSyncNum, 1); - sInfo("%s, start to restore data, sstatus:%s", pPeer->id, syncStatus[nodeSStatus]); - - nodeRole = TAOS_SYNC_ROLE_SYNCING; - (*pNode->notifyRoleFp)(pNode->vgId, nodeRole); - - if (syncOpenRecvBuffer(pNode) < 0) { - sError("%s, failed to allocate recv buffer, restart connection", pPeer->id); - syncRestartConnection(pPeer); - } else { - if (syncRestoreDataStepByStep(pPeer) == 0) { - sInfo("%s, it is synced successfully", pPeer->id); - nodeRole = TAOS_SYNC_ROLE_SLAVE; - syncBroadcastStatus(pNode); - } else { - sError("%s, failed to restore data, restart connection", pPeer->id); - nodeRole = TAOS_SYNC_ROLE_UNSYNCED; - syncRestartConnection(pPeer); - } - } - - (*pNode->notifyRoleFp)(pNode->vgId, nodeRole); - - nodeSStatus = TAOS_SYNC_STATUS_INIT; - sInfo("%s, restore data over, set sstatus:%s", pPeer->id, syncStatus[nodeSStatus]); - - taosCloseSocket(pPeer->syncFd); - syncCloseRecvBuffer(pNode); - atomic_sub_fetch_32(&tsSyncNum, 1); - - // The ref is obtained in both the create thread and the current thread, so it is released twice - syncReleasePeer(pPeer); - syncReleasePeer(pPeer); - - return NULL; -} diff --git a/src/sync/src/syncRetrieve.c b/src/sync/src/syncRetrieve.c deleted file mode 100644 index c86ab85499..0000000000 --- a/src/sync/src/syncRetrieve.c +++ /dev/null @@ -1,472 +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 "tlog.h" -#include "tutil.h" -#include "tglobal.h" -#include "ttimer.h" -#include "tsocket.h" -#include "twal.h" -#include "tsync.h" -#include "syncInt.h" - -static int32_t syncGetWalVersion(SSyncNode *pNode, SSyncPeer *pPeer) { - uint64_t fver, wver; - int32_t code = (*pNode->getVersionFp)(pNode->vgId, &fver, &wver); - if (code != 0) { - sInfo("%s, vnode is commiting while retrieve, last wver:%" PRIu64, pPeer->id, pPeer->lastWalVer); - return -1; - } - - pPeer->lastWalVer = wver; - return code; -} - -static bool syncIsWalModified(SSyncNode *pNode, SSyncPeer *pPeer) { - uint64_t fver, wver; - int32_t code = (*pNode->getVersionFp)(pNode->vgId, &fver, &wver); - if (code != 0) { - sInfo("%s, vnode is commiting while retrieve, last wver:%" PRIu64, pPeer->id, pPeer->lastWalVer); - return true; - } - - if (wver != pPeer->lastWalVer) { - sInfo("%s, wal is modified while retrieve, wver:%" PRIu64 ", last:%" PRIu64, pPeer->id, wver, pPeer->lastWalVer); - return true; - } - - return false; -} - -static int32_t syncGetFileVersion(SSyncNode *pNode, SSyncPeer *pPeer) { - uint64_t fver, wver; - int32_t code = (*pNode->getVersionFp)(pNode->vgId, &fver, &wver); - if (code != 0) { - sInfo("%s, vnode is commiting while get fver for retrieve, last fver:%" PRIu64, pPeer->id, pPeer->lastFileVer); - return -1; - } - - pPeer->lastFileVer = fver; - return code; -} - -static bool syncAreFilesModified(SSyncNode *pNode, SSyncPeer *pPeer) { - uint64_t fver, wver; - int32_t code = (*pNode->getVersionFp)(pNode->vgId, &fver, &wver); - if (code != 0) { - sInfo("%s, vnode is commiting while retrieve, last fver:%" PRIu64, pPeer->id, pPeer->lastFileVer); - pPeer->fileChanged = 1; - return true; - } - - if (fver != pPeer->lastFileVer) { - sInfo("%s, files are modified while retrieve, fver:%" PRIu64 ", last:%" PRIu64, pPeer->id, fver, pPeer->lastFileVer); - pPeer->fileChanged = 1; - return true; - } - - pPeer->fileChanged = 0; - return false; -} - -static int32_t syncSendFileVersion(SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - - SFileVersion fileVersion; - memset(&fileVersion, 0, sizeof(SFileVersion)); - syncBuildFileVersion(&fileVersion, pNode->vgId); - - uint64_t fver = pPeer->lastFileVer; - fileVersion.fversion = htobe64(fver); - int32_t ret = taosWriteMsg(pPeer->syncFd, &fileVersion, sizeof(SFileVersion)); - if (ret != sizeof(SFileVersion)) { - sError("%s, failed to write fver:%" PRIu64 " since %s", pPeer->id, fver, strerror(errno)); - return -1; - } - - SFileAck fileAck; - memset(&fileAck, 0, sizeof(SFileAck)); - ret = taosReadMsg(pPeer->syncFd, &fileAck, sizeof(SFileAck)); - if (ret != sizeof(SFileAck)) { - sError("%s, failed to read fver ack since %s", pPeer->id, strerror(errno)); - return -1; - } - - // set the peer sync version - pPeer->sversion = fver; - - return 0; -} - -static int32_t syncRetrieveFile(SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - - if (syncGetFileVersion(pNode, pPeer) < 0) { - pPeer->fileChanged = 1; - return -1; - } - - if (pNode->sendFileFp && (*pNode->sendFileFp)(pNode->pTsdb, pPeer->syncFd) != 0) { - sError("%s, failed to retrieve file", pPeer->id); - return -1; - } - - if (syncSendFileVersion(pPeer) < 0) { - return -1; - } - - sInfo("%s, all files are retrieved", pPeer->id); - return 0; -} - -// if only a partial record is read out, upper layer will reload the file to get a complete record -static int32_t syncReadOneWalRecord(int32_t sfd, SWalHead *pHead) { - int32_t ret = read(sfd, pHead, sizeof(SWalHead)); - if (ret < 0) { - sError("sfd:%d, failed to read wal head since %s, ret:%d", sfd, strerror(errno), ret); - return -1; - } - - if (ret == 0) { - sInfo("sfd:%d, read to the end of file, ret:%d", sfd, ret); - return 0; - } - - if (ret != sizeof(SWalHead)) { - // file is not at end yet, it shall be reloaded - sInfo("sfd:%d, a partial wal head is read out, ret:%d", sfd, ret); - return 0; - } - - assert(pHead->len <= TSDB_MAX_WAL_SIZE); - - ret = read(sfd, pHead->cont, pHead->len); - if (ret < 0) { - sError("sfd:%d, failed to read wal content since %s, ret:%d", sfd, strerror(errno), ret); - return -1; - } - - if (ret != pHead->len) { - // file is not at end yet, it shall be reloaded - sInfo("sfd:%d, a partial wal conetnt is read out, ret:%d", sfd, ret); - return 0; - } - - return sizeof(SWalHead) + pHead->len; -} - -static int64_t syncRetrieveLastWal(SSyncPeer *pPeer, char *name, uint64_t fversion, int64_t offset) { - int32_t sfd = open(name, O_RDONLY | O_BINARY); - if (sfd < 0) { - sError("%s, failed to open wal:%s for retrieve since:%s", pPeer->id, name, tstrerror(errno)); - return -1; - } - - int64_t code = taosLSeek(sfd, offset, SEEK_SET); - if (code < 0) { - sError("%s, failed to seek %" PRId64 " in wal:%s for retrieve since:%s", pPeer->id, offset, name, tstrerror(errno)); - close(sfd); - return -1; - } - - sInfo("%s, retrieve last wal:%s, offset:%" PRId64 " fver:%" PRIu64, pPeer->id, name, offset, fversion); - - SWalHead *pHead = malloc(SYNC_MAX_SIZE); - int64_t bytes = 0; - - while (1) { - code = syncReadOneWalRecord(sfd, pHead); - if (code < 0) { - sError("%s, failed to read one record from wal:%s", pPeer->id, name); - break; - } - - if (code == 0) { - code = bytes; - sInfo("%s, read to the end of wal, bytes:%" PRId64, pPeer->id, bytes); - break; - } - - sTrace("%s, last wal is forwarded, hver:%" PRIu64, pPeer->id, pHead->version); - - int32_t wsize = (int32_t)code; - int32_t ret = taosWriteMsg(pPeer->syncFd, pHead, wsize); - if (ret != wsize) { - code = -1; - sError("%s, failed to forward wal since %s, hver:%" PRIu64, pPeer->id, strerror(errno), pHead->version); - break; - } - - pPeer->sversion = pHead->version; - bytes += wsize; - - if (pHead->version >= fversion && fversion > 0) { - code = 0; - sInfo("%s, retrieve wal finished, hver:%" PRIu64 " fver:%" PRIu64, pPeer->id, pHead->version, fversion); - break; - } - } - - free(pHead); - close(sfd); - - return code; -} - -static int64_t syncProcessLastWal(SSyncPeer *pPeer, char *wname, int64_t index) { - SSyncNode *pNode = pPeer->pSyncNode; - int32_t once = 0; // last WAL has once ever been processed - int64_t offset = 0; - uint64_t fversion = 0; - char fname[TSDB_FILENAME_LEN * 2] = {0}; // full path to wal file - - // get full path to wal file - snprintf(fname, sizeof(fname), "%s/%s", pNode->path, wname); - sInfo("%s, start to retrieve last wal:%s", pPeer->id, fname); - - while (1) { - if (syncAreFilesModified(pNode, pPeer)) return -1; - if (syncGetWalVersion(pNode, pPeer) < 0) return -1; - - int64_t bytes = syncRetrieveLastWal(pPeer, fname, fversion, offset); - if (bytes < 0) { - sInfo("%s, failed to retrieve last wal, bytes:%" PRId64, pPeer->id, bytes); - return bytes; - } - - // check file changes - bool walModified = syncIsWalModified(pNode, pPeer); - - // if file is not updated or updated once, set the fversion and sstatus - if (!walModified || once) { - if (fversion == 0) { - pPeer->sstatus = TAOS_SYNC_STATUS_CACHE; // start to forward pkt - fversion = nodeVersion; // must read data to fversion - sInfo("%s, set sstatus:%s and fver:%" PRIu64, pPeer->id, syncStatus[pPeer->sstatus], fversion); - } - } - - // if all data up to fversion is read out, it is over - if (pPeer->sversion >= fversion && fversion > 0) { - sInfo("%s, data up to fver:%" PRIu64 " has been read out, bytes:%" PRId64 " sver:%" PRIu64, pPeer->id, fversion, bytes, - pPeer->sversion); - return 0; - } - - // if all data are read out, and no update - if (bytes == 0 && !walModified) { - // wal not closed, it means some data not flushed to disk, wait for a while - taosMsleep(10); - } - - // if bytes > 0, file is updated, or fversion is not reached but file still open, read again - once = 1; - offset += bytes; - sInfo("%s, continue retrieve last wal, bytes:%" PRId64 " offset:%" PRId64 " sver:%" PRIu64 " fver:%" PRIu64, pPeer->id, - bytes, offset, pPeer->sversion, fversion); - } - - return -1; -} - -static int64_t syncRetrieveWal(SSyncPeer *pPeer) { - SSyncNode * pNode = pPeer->pSyncNode; - char fname[TSDB_FILENAME_LEN * 3]; - char wname[TSDB_FILENAME_LEN * 2]; - int32_t size; - int64_t code = -1; - int64_t index = 0; - - while (1) { - // retrieve wal info - wname[0] = 0; - code = (*pNode->getWalInfoFp)(pNode->vgId, wname, &index); - if (code < 0) { - sError("%s, failed to get wal info since:%s, code:0x%" PRIx64, pPeer->id, strerror(errno), code); - break; - } - - if (wname[0] == 0) { // no wal file - code = 0; - sInfo("%s, no wal file anymore", pPeer->id); - break; - } - - if (code == 0) { // last wal - code = syncProcessLastWal(pPeer, wname, index); - sInfo("%s, last wal processed, code:%" PRId64, pPeer->id, code); - break; - } - - // get the full path to wal file - snprintf(fname, sizeof(fname), "%s/%s", pNode->path, wname); - - // send wal file, old wal file won't be modified, even remove is ok - struct stat fstat; - if (stat(fname, &fstat) < 0) { - code = -1; - sInfo("%s, failed to stat wal:%s for retrieve since %s, code:0x%" PRIx64, pPeer->id, fname, strerror(errno), code); - break; - } - - size = fstat.st_size; - sInfo("%s, retrieve wal:%s size:%d", pPeer->id, fname, size); - - int32_t sfd = open(fname, O_RDONLY | O_BINARY); - if (sfd < 0) { - code = -1; - sError("%s, failed to open wal:%s for retrieve since %s, code:0x%" PRIx64, pPeer->id, fname, strerror(errno), code); - break; - } - - code = taosSendFile(pPeer->syncFd, sfd, NULL, size); - close(sfd); - if (code < 0) { - sError("%s, failed to send wal:%s for retrieve since %s, code:0x%" PRIx64, pPeer->id, fname, strerror(errno), code); - break; - } - - if (syncAreFilesModified(pNode, pPeer)) { - code = -1; - break; - } - } - - if (code == 0) { - SWalHead walHead; - memset(&walHead, 0, sizeof(walHead)); - if (taosWriteMsg(pPeer->syncFd, &walHead, sizeof(walHead)) == sizeof(walHead)) { - pPeer->sstatus = TAOS_SYNC_STATUS_CACHE; - sInfo("%s, wal retrieve is finished, set sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]); - } else { - sError("%s, failed to send last wal record since %s", pPeer->id, strerror(errno)); - code = -1; - } - } else { - sError("%s, failed to send wal since %s, code:0x%" PRIx64, pPeer->id, strerror(errno), code); - } - - return code; -} - -static int32_t syncRetrieveFirstPkt(SSyncPeer *pPeer) { - SSyncNode *pNode = pPeer->pSyncNode; - - SSyncMsg msg; - syncBuildSyncDataMsg(&msg, pNode->vgId); - - if (taosWriteMsg(pPeer->syncFd, &msg, sizeof(SSyncMsg)) != sizeof(SSyncMsg)) { - sError("%s, failed to send sync-data msg since %s, tranId:%u", pPeer->id, strerror(errno), msg.tranId); - return -1; - } - sInfo("%s, send sync-data msg to peer, tranId:%u", pPeer->id, msg.tranId); - - SSyncRsp rsp; - if (taosReadMsg(pPeer->syncFd, &rsp, sizeof(SSyncRsp)) != sizeof(SSyncRsp)) { - sError("%s, failed to read sync-data rsp since %s, tranId:%u", pPeer->id, strerror(errno), msg.tranId); - return -1; - } - - sInfo("%s, recv sync-data rsp from peer, tranId:%u rsp-tranId:%u", pPeer->id, msg.tranId, rsp.tranId); - return 0; -} - -static int32_t syncRetrieveDataStepByStep(SSyncPeer *pPeer) { - sInfo("%s, start to retrieve, sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]); - if (syncRetrieveFirstPkt(pPeer) < 0) { - sError("%s, failed to start retrieve", pPeer->id); - return -1; - } - - pPeer->sversion = 0; - pPeer->sstatus = TAOS_SYNC_STATUS_FILE; - sInfo("%s, start to retrieve files, set sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]); - if (syncRetrieveFile(pPeer) != 0) { - sError("%s, failed to retrieve files", pPeer->id); - return -1; - } - - // if no files are synced, there must be wal to sync, sversion must be larger than one - if (pPeer->sversion == 0) pPeer->sversion = 1; - - sInfo("%s, start to retrieve wals", pPeer->id); - int64_t code = syncRetrieveWal(pPeer); - if (code < 0) { - sError("%s, failed to retrieve wals, code:0x%" PRIx64, pPeer->id, code); - return -1; - } - - return 0; -} - -void *syncRetrieveData(void *param) { - int64_t rid = (int64_t)param; - SSyncPeer *pPeer = syncAcquirePeer(rid); - if (pPeer == NULL) { - sError("failed to retrieve data, invalid peer rid:%" PRId64, rid); - return NULL; - } - - uint32_t ip = syncResolvePeerFqdn(pPeer); - if (!ip) { - syncReleasePeer(pPeer); - return NULL; - } - - SSyncNode *pNode = pPeer->pSyncNode; - - taosBlockSIGPIPE(); - sInfo("%s, start to retrieve data, sstatus:%s, numOfRetrieves:%d", pPeer->id, syncStatus[pPeer->sstatus], - pPeer->numOfRetrieves); - - if (pNode->notifyFlowCtrlFp) (*pNode->notifyFlowCtrlFp)(pNode->vgId, pPeer->numOfRetrieves); - - pPeer->syncFd = taosOpenTcpClientSocket(ip, pPeer->port, 0); - if (pPeer->syncFd < 0) { - sError("%s, failed to open socket to sync", pPeer->id); - } else { - sInfo("%s, sync tcp is setup", pPeer->id); - - if (syncRetrieveDataStepByStep(pPeer) == 0) { - sInfo("%s, sync retrieve process is successful", pPeer->id); - } else { - sError("%s, failed to retrieve data, restart connection", pPeer->id); - syncRestartConnection(pPeer); - } - } - - if (pPeer->fileChanged) { - pPeer->numOfRetrieves++; - } else { - pPeer->numOfRetrieves = 0; - // if (pNode->notifyFlowCtrlFp) (*pNode->notifyFlowCtrlFp)(pNode->vgId, 0); - } - - if (pNode->notifyFlowCtrlFp) (*pNode->notifyFlowCtrlFp)(pNode->vgId, 0); - - pPeer->fileChanged = 0; - taosCloseSocket(pPeer->syncFd); - - // The ref is obtained in both the create thread and the current thread, so it is released twice - sInfo("%s, sync retrieve data over, sstatus:%s", pPeer->id, syncStatus[pPeer->sstatus]); - - syncReleasePeer(pPeer); - syncReleasePeer(pPeer); - - return NULL; -} diff --git a/src/sync/src/syncTcp.c b/src/sync/src/syncTcp.c deleted file mode 100644 index ccb0a67e5c..0000000000 --- a/src/sync/src/syncTcp.c +++ /dev/null @@ -1,338 +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 "tulog.h" -#include "tutil.h" -#include "tsocket.h" -#include "taoserror.h" -#include "twal.h" -#include "tsync.h" -#include "syncInt.h" -#include "syncTcp.h" - -typedef struct SThreadObj { - pthread_t thread; - bool stop; - SOCKET pollFd; - int32_t numOfFds; - struct SPoolObj *pPool; -} SThreadObj; - -typedef struct SPoolObj { - SPoolInfo info; - SThreadObj **pThread; - pthread_t thread; - int32_t nextId; - SOCKET acceptFd; // FD for accept new connection - int8_t stop; -} SPoolObj; - -typedef struct { - SThreadObj *pThread; - int64_t handleId; - SOCKET fd; - int32_t closedByApp; -} SConnObj; - -static void *syncAcceptPeerTcpConnection(void *argv); -static void *syncProcessTcpData(void *param); -static void syncStopPoolThread(SThreadObj *pThread); -static SThreadObj *syncGetTcpThread(SPoolObj *pPool); - -void *syncOpenTcpThreadPool(SPoolInfo *pInfo) { - pthread_attr_t thattr; - - SPoolObj *pPool = calloc(sizeof(SPoolObj), 1); - if (pPool == NULL) { - sError("failed to alloc pool for TCP server since no enough memory"); - return NULL; - } - - pPool->info = *pInfo; - - pPool->pThread = calloc(sizeof(SThreadObj *), pInfo->numOfThreads); - if (pPool->pThread == NULL) { - sError("failed to alloc pool thread for TCP server since no enough memory"); - tfree(pPool); - return NULL; - } - - pPool->acceptFd = taosOpenTcpServerSocket(pInfo->serverIp, pInfo->port); - if (pPool->acceptFd < 0) { - tfree(pPool->pThread); - tfree(pPool); - sError("failed to create TCP server socket, port:%d (%s)", pInfo->port, strerror(errno)); - return NULL; - } - - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&(pPool->thread), &thattr, (void *)syncAcceptPeerTcpConnection, pPool) != 0) { - sError("failed to create accept thread for TCP server since %s", strerror(errno)); - taosCloseSocket(pPool->acceptFd); - tfree(pPool->pThread); - tfree(pPool); - return NULL; - } - - pthread_attr_destroy(&thattr); - - sDebug("%p TCP pool is created", pPool); - return pPool; -} - -void syncCloseTcpThreadPool(void *param) { - SPoolObj * pPool = param; - SThreadObj *pThread; - - pPool->stop = 1; - -#ifdef WINDOWS - closesocket(pPool->acceptFd); -#elif defined(__APPLE__) - if (pPool->acceptFd!=-1) { - close(pPool->acceptFd); - pPool->acceptFd = -1; - } -#else - shutdown(pPool->acceptFd, SHUT_RD); -#endif - - pthread_join(pPool->thread, NULL); - - for (int32_t i = 0; i < pPool->info.numOfThreads; ++i) { - pThread = pPool->pThread[i]; - if (pThread) syncStopPoolThread(pThread); - } - - sDebug("%p TCP pool is closed", pPool); - - tfree(pPool->pThread); - tfree(pPool); -} - -void *syncAllocateTcpConn(void *param, int64_t rid, SOCKET connFd) { - struct epoll_event event; - SPoolObj *pPool = param; - - SConnObj *pConn = calloc(sizeof(SConnObj), 1); - if (pConn == NULL) { - terrno = TAOS_SYSTEM_ERROR(errno); - return NULL; - } - - SThreadObj *pThread = syncGetTcpThread(pPool); - if (pThread == NULL) { - tfree(pConn); - return NULL; - } - - pConn->fd = connFd; - pConn->pThread = pThread; - pConn->handleId = rid; - pConn->closedByApp = 0; - - event.events = EPOLLIN | EPOLLRDHUP; - event.data.ptr = pConn; - - if (epoll_ctl(pThread->pollFd, EPOLL_CTL_ADD, connFd, &event) < 0) { - sError("failed to add fd:%d since %s", connFd, strerror(errno)); - terrno = TAOS_SYSTEM_ERROR(errno); - tfree(pConn); - pConn = NULL; - } else { - pThread->numOfFds++; - sDebug("%p fd:%d is added to epoll thread, num:%d", pThread, connFd, pThread->numOfFds); - } - - return pConn; -} - -void syncFreeTcpConn(void *param) { - SConnObj * pConn = param; - SThreadObj *pThread = pConn->pThread; - - sDebug("%p TCP connection will be closed, fd:%d", pThread, pConn->fd); - pConn->closedByApp = 1; - shutdown(pConn->fd, SHUT_WR); -} - -static void taosProcessBrokenLink(SConnObj *pConn) { - SThreadObj *pThread = pConn->pThread; - SPoolObj * pPool = pThread->pPool; - SPoolInfo * pInfo = &pPool->info; - - if (pConn->closedByApp == 0) shutdown(pConn->fd, SHUT_WR); - (*pInfo->processBrokenLink)(pConn->handleId, pConn->closedByApp); - - pThread->numOfFds--; - epoll_ctl(pThread->pollFd, EPOLL_CTL_DEL, pConn->fd, NULL); - sDebug("%p fd:%d is removed from epoll thread, num:%d", pThread, pConn->fd, pThread->numOfFds); - taosCloseSocket(pConn->fd); - tfree(pConn); -} - -#define maxEvents 10 - -static void *syncProcessTcpData(void *param) { - SThreadObj *pThread = (SThreadObj *)param; - SPoolObj * pPool = pThread->pPool; - SPoolInfo * pInfo = &pPool->info; - SConnObj * pConn = NULL; - struct epoll_event events[maxEvents]; - - setThreadName("syncTcpData"); - - void *buffer = malloc(pInfo->bufferSize); - taosBlockSIGPIPE(); - - while (1) { - if (pThread->stop) break; - int32_t fdNum = epoll_wait(pThread->pollFd, events, maxEvents, TAOS_EPOLL_WAIT_TIME); - if (pThread->stop) { - sDebug("%p TCP epoll thread is exiting...", pThread); - break; - } - - if (fdNum < 0) { - sError("epoll_wait failed since %s", strerror(errno)); - continue; - } - - for (int32_t i = 0; i < fdNum; ++i) { - pConn = events[i].data.ptr; - assert(pConn); - - if (events[i].events & EPOLLERR) { - sDebug("conn is broken since EPOLLERR"); - taosProcessBrokenLink(pConn); - continue; - } - - if (events[i].events & EPOLLHUP) { - sDebug("conn is broken since EPOLLHUP"); - taosProcessBrokenLink(pConn); - continue; - } - - if (events[i].events & EPOLLRDHUP) { - sDebug("conn is broken since EPOLLRDHUP"); - taosProcessBrokenLink(pConn); - continue; - } - - if (pConn->closedByApp == 0) { - if ((*pInfo->processIncomingMsg)(pConn->handleId, buffer) < 0) { - syncFreeTcpConn(pConn); - continue; - } - } - } - - if (pThread->stop) break; - } - - sDebug("%p TCP epoll thread exits", pThread); - - EpollClose(pThread->pollFd); - tfree(pThread); - tfree(buffer); - return NULL; -} - -static void *syncAcceptPeerTcpConnection(void *argv) { - SPoolObj * pPool = (SPoolObj *)argv; - SPoolInfo *pInfo = &pPool->info; - - taosBlockSIGPIPE(); - setThreadName("acceptTcpConn"); - - while (1) { - struct sockaddr_in clientAddr; - socklen_t addrlen = sizeof(clientAddr); - SOCKET connFd = accept(pPool->acceptFd, (struct sockaddr *)&clientAddr, &addrlen); - if (pPool->stop) { - sDebug("%p TCP server accept is stopped", pPool); - break; - } - - if (connFd < 0) { - if (errno == EINVAL) { - sDebug("%p TCP server accept is exiting...", pPool); - break; - } else { - sError("TCP accept failure since %s", strerror(errno)); - continue; - } - } - - // sDebug("TCP connection from: 0x%x:%d", clientAddr.sin_addr.s_addr, clientAddr.sin_port); - taosKeepTcpAlive(connFd); - (*pInfo->processIncomingConn)(connFd, clientAddr.sin_addr.s_addr); - } - - taosCloseSocket(pPool->acceptFd); - return NULL; -} - -static SThreadObj *syncGetTcpThread(SPoolObj *pPool) { - SThreadObj *pThread = pPool->pThread[pPool->nextId]; - - if (pThread) return pThread; - - pThread = (SThreadObj *)calloc(1, sizeof(SThreadObj)); - if (pThread == NULL) return NULL; - - pThread->pPool = pPool; - pThread->pollFd = (EpollFd)epoll_create(10); // size does not matter - if (pThread->pollFd < 0) { - tfree(pThread); - return NULL; - } - - pthread_attr_t thattr; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); - int32_t ret = pthread_create(&(pThread->thread), &thattr, (void *)syncProcessTcpData, pThread); - pthread_attr_destroy(&thattr); - - if (ret != 0) { - EpollClose(pThread->pollFd); - tfree(pThread); - return NULL; - } - - sDebug("%p TCP epoll thread is created", pThread); - pPool->pThread[pPool->nextId] = pThread; - pPool->nextId++; - pPool->nextId = pPool->nextId % pPool->info.numOfThreads; - - return pThread; -} - -static void syncStopPoolThread(SThreadObj *pThread) { - pthread_t thread = pThread->thread; - if (!taosCheckPthreadValid(thread)) { - return; - } - pThread->stop = true; - if (taosComparePthread(thread, pthread_self())) { - pthread_detach(pthread_self()); - return; - } - pthread_join(thread, NULL); -} diff --git a/src/sync/test/CMakeLists.txt b/src/sync/test/CMakeLists.txt deleted file mode 100644 index a5ab819137..0000000000 --- a/src/sync/test/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.8...3.20) -PROJECT(TDengine) - -IF (TD_LINUX) - INCLUDE_DIRECTORIES(../inc) - - LIST(APPEND CLIENT_SRC ./syncClient.c) - ADD_EXECUTABLE(syncClient ${CLIENT_SRC}) - TARGET_LINK_LIBRARIES(syncClient sync trpc common) - - LIST(APPEND SERVER_SRC ./syncServer.c) - ADD_EXECUTABLE(syncServer ${SERVER_SRC}) - TARGET_LINK_LIBRARIES(syncServer sync trpc common) -ENDIF () - diff --git a/src/sync/test/syncClient.c b/src/sync/test/syncClient.c deleted file mode 100644 index 23ea54ee0c..0000000000 --- a/src/sync/test/syncClient.c +++ /dev/null @@ -1,194 +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 . - */ - -#include "os.h" -#include "tglobal.h" -#include "tulog.h" -#include "trpc.h" -#include "taoserror.h" - -typedef struct { - int index; - SRpcEpSet epSet; - int num; - int numOfReqs; - int msgSize; - tsem_t rspSem; - tsem_t * pOverSem; - pthread_t thread; - void * pRpc; -} SInfo; - -void processResponse(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - SInfo *pInfo = (SInfo *)pMsg->ahandle; - uDebug("thread:%d, response is received, type:%d contLen:%d code:0x%x", pInfo->index, pMsg->msgType, pMsg->contLen, - pMsg->code); - - if (pEpSet) pInfo->epSet = *pEpSet; - rpcFreeCont(pMsg->pCont); - - tsem_post(&pInfo->rspSem); -} - -int tcount = 0; - -void *sendRequest(void *param) { - SInfo * pInfo = (SInfo *)param; - SRpcMsg rpcMsg = {0}; - - uDebug("thread:%d, start to send request", pInfo->index); - - while (pInfo->numOfReqs == 0 || pInfo->num < pInfo->numOfReqs) { - pInfo->num++; - rpcMsg.pCont = rpcMallocCont(pInfo->msgSize); - rpcMsg.contLen = pInfo->msgSize; - rpcMsg.ahandle = pInfo; - rpcMsg.msgType = 1; - uDebug("thread:%d, send request, contLen:%d num:%d", pInfo->index, pInfo->msgSize, pInfo->num); - rpcSendRequest(pInfo->pRpc, &pInfo->epSet, &rpcMsg, NULL); - if (pInfo->num % 20000 == 0) { - uInfo("thread:%d, %d requests have been sent", pInfo->index, pInfo->num); - } - tsem_wait(&pInfo->rspSem); - } - - uDebug("thread:%d, it is over", pInfo->index); - tcount++; - - return NULL; -} - -int main(int argc, char *argv[]) { - SRpcInit rpcInit; - SRpcEpSet epSet; - char secret[TSDB_KEY_LEN] = "mypassword"; - int msgSize = 128; - int numOfReqs = 0; - int appThreads = 1; - char serverIp[40] = "127.0.0.1"; - struct timeval systemTime; - int64_t startTime, endTime; - pthread_attr_t thattr; - - // server info - epSet.numOfEps = 1; - epSet.inUse = 0; - epSet.port[0] = 7000; - epSet.port[1] = 7000; - strcpy(epSet.fqdn[0], serverIp); - strcpy(epSet.fqdn[1], "192.168.0.1"); - - // client info - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = 0; - rpcInit.label = "APP"; - rpcInit.numOfThreads = 1; - rpcInit.cfp = processResponse; - rpcInit.sessions = 100; - rpcInit.idleTime = tsShellActivityTimer*1000; - rpcInit.user = "michael"; - rpcInit.secret = secret; - rpcInit.ckey = "key"; - rpcInit.spi = 1; - rpcInit.connType = TAOS_CONN_CLIENT; - - for (int i = 1; i < argc; ++i) { - if (strcmp(argv[i], "-p") == 0 && i < argc - 1) { - epSet.port[0] = atoi(argv[++i]); - } else if (strcmp(argv[i], "-i") == 0 && i < argc - 1) { - tstrncpy(epSet.fqdn[0], argv[++i], TSDB_FQDN_LEN); - } else if (strcmp(argv[i], "-t") == 0 && i < argc - 1) { - rpcInit.numOfThreads = atoi(argv[++i]); - } else if (strcmp(argv[i], "-m") == 0 && i < argc - 1) { - msgSize = atoi(argv[++i]); - } else if (strcmp(argv[i], "-s") == 0 && i < argc - 1) { - rpcInit.sessions = atoi(argv[++i]); - } else if (strcmp(argv[i], "-n") == 0 && i < argc - 1) { - numOfReqs = atoi(argv[++i]); - } else if (strcmp(argv[i], "-a") == 0 && i < argc - 1) { - appThreads = atoi(argv[++i]); - } else if (strcmp(argv[i], "-o") == 0 && i < argc - 1) { - tsCompressMsgSize = atoi(argv[++i]); - } else if (strcmp(argv[i], "-u") == 0 && i < argc - 1) { - rpcInit.user = argv[++i]; - } else if (strcmp(argv[i], "-k") == 0 && i < argc - 1) { - rpcInit.secret = argv[++i]; - } else if (strcmp(argv[i], "-spi") == 0 && i < argc - 1) { - rpcInit.spi = atoi(argv[++i]); - } else if (strcmp(argv[i], "-d") == 0 && i < argc - 1) { - rpcDebugFlag = atoi(argv[++i]); - } else { - printf("\nusage: %s [options] \n", argv[0]); - printf(" [-i ip]: first server IP address, default is:%s\n", serverIp); - printf(" [-p port]: server port number, default is:%d\n", epSet.port[0]); - printf(" [-t threads]: number of rpc threads, default is:%d\n", rpcInit.numOfThreads); - printf(" [-s sessions]: number of rpc sessions, default is:%d\n", rpcInit.sessions); - printf(" [-m msgSize]: message body size, default is:%d\n", msgSize); - printf(" [-a threads]: number of app threads, default is:%d\n", appThreads); - printf(" [-n requests]: number of requests per thread, default is:%d\n", numOfReqs); - printf(" [-o compSize]: compression message size, default is:%d\n", tsCompressMsgSize); - printf(" [-u user]: user name for the connection, default is:%s\n", rpcInit.user); - printf(" [-k secret]: password for the connection, default is:%s\n", rpcInit.secret); - printf(" [-spi SPI]: security parameter index, default is:%d\n", rpcInit.spi); - printf(" [-d debugFlag]: debug flag, default:%d\n", rpcDebugFlag); - printf(" [-h help]: print out this help\n\n"); - exit(0); - } - } - - taosInitLog("client.log", 100000, 10); - - void *pRpc = rpcOpen(&rpcInit); - if (pRpc == NULL) { - uError("failed to initialize RPC"); - return -1; - } - - uInfo("client is initialized"); - - gettimeofday(&systemTime, NULL); - startTime = systemTime.tv_sec * 1000000 + systemTime.tv_usec; - - SInfo *pInfo = (SInfo *)calloc(1, sizeof(SInfo) * appThreads); - - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); - - for (int i = 0; i < appThreads; ++i) { - pInfo->index = i; - pInfo->epSet = epSet; - pInfo->numOfReqs = numOfReqs; - pInfo->msgSize = msgSize; - tsem_init(&pInfo->rspSem, 0, 0); - pInfo->pRpc = pRpc; - pthread_create(&pInfo->thread, &thattr, sendRequest, pInfo); - pInfo++; - } - - do { - usleep(1); - } while (tcount < appThreads); - - gettimeofday(&systemTime, NULL); - endTime = systemTime.tv_sec * 1000000 + systemTime.tv_usec; - float usedTime = (endTime - startTime) / 1000.0; // mseconds - - uInfo("it takes %.3f mseconds to send %d requests to server", usedTime, numOfReqs * appThreads); - uInfo("Performance: %.3f requests per second, msgSize:%d bytes", 1000.0 * numOfReqs * appThreads / usedTime, msgSize); - - taosCloseLog(); - - return 0; -} diff --git a/src/sync/test/syncServer.c b/src/sync/test/syncServer.c deleted file mode 100644 index 4598e16a9d..0000000000 --- a/src/sync/test/syncServer.c +++ /dev/null @@ -1,472 +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 -#include "os.h" -#include "tulog.h" -#include "tglobal.h" -#include "tsocket.h" -#include "trpc.h" -#include "tqueue.h" -#include "twal.h" -#include "tsync.h" - -int msgSize = 128; -int commit = 0; -int dataFd = -1; -void * qhandle = NULL; -int walNum = 0; -uint64_t tversion = 0; -int64_t syncHandle; -int role; -int nodeId; -char path[256]; -int numOfWrites; -SSyncInfo syncInfo; -SSyncCfg *pCfg; - -int writeIntoWal(SWalHead *pHead) { - if (dataFd < 0) { - char walName[280]; - snprintf(walName, sizeof(walName), "%s/wal/wal.%d", path, walNum); - (void)remove(walName); - dataFd = open(walName, O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO); - if (dataFd < 0) { - uInfo("failed to open wal file:%s(%s)", walName, strerror(errno)); - return -1; - } else { - walNum++; - uInfo("file:%s is opened to write, walNum:%d", walName, walNum); - } - } - - if (write(dataFd, pHead, sizeof(SWalHead) + pHead->len) < 0) { - uError("ver:%" PRIu64 ", failed to write wal file(%s)", pHead->version, strerror(errno)); - } else { - uDebug("ver:%" PRIu64 ", written to wal", pHead->version); - } - - numOfWrites++; - if (numOfWrites >= 10000) { - uInfo("%d request have been written into disk", numOfWrites); - close(dataFd); - dataFd = -1; - numOfWrites = 0; - } - - return 0; -} - -void confirmForward(int32_t vgId, void *mhandle, int32_t code) { - SRpcMsg * pMsg = (SRpcMsg *)mhandle; - SWalHead *pHead = (SWalHead *)(((char *)pMsg->pCont) - sizeof(SWalHead)); - - uDebug("ver:%" PRIu64 ", confirm is received", pHead->version); - - rpcFreeCont(pMsg->pCont); - - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = rpcMallocCont(msgSize); - rpcMsg.contLen = msgSize; - rpcMsg.handle = pMsg->handle; - rpcMsg.code = code; - rpcSendResponse(&rpcMsg); - - taosFreeQitem(mhandle); -} - -int processRpcMsg(void *item) { - SRpcMsg * pMsg = (SRpcMsg *)item; - SWalHead *pHead = (SWalHead *)(((char *)pMsg->pCont) - sizeof(SWalHead)); - int code = -1; - - if (role != TAOS_SYNC_ROLE_MASTER) { - uError("not master, write failed, role:%s", syncRole[role]); - } else { - pHead->version = ++tversion; - pHead->msgType = pMsg->msgType; - pHead->len = pMsg->contLen; - - uDebug("ver:%" PRIu64 ", rsp from client processed", pHead->version); - writeIntoWal(pHead); - syncForwardToPeer(syncHandle, pHead, item, TAOS_QTYPE_RPC); - - code = 0; - } - - if (pCfg->quorum <= 1) { - rpcFreeCont(pMsg->pCont); - - SRpcMsg rpcMsg = {0}; - rpcMsg.pCont = rpcMallocCont(msgSize); - rpcMsg.contLen = msgSize; - rpcMsg.handle = pMsg->handle; - rpcMsg.code = code; - rpcSendResponse(&rpcMsg); - taosFreeQitem(item); - } - - return code; -} - -int processFwdMsg(void *item) { - SWalHead *pHead = (SWalHead *)item; - - if (pHead->version <= tversion) { - uError("ver:%" PRIu64 ", forward is even lower than local:%" PRIu64, pHead->version, tversion); - return -1; - } - - uDebug("ver:%" PRIu64 ", forward from peer is received", pHead->version); - writeIntoWal(pHead); - tversion = pHead->version; - - if (pCfg->quorum > 1) syncConfirmForward(syncHandle, pHead->version, 0); - - // write into cache - - /* - if (pHead->handle) { - syncSendFwdAck(syncHandle, pHead->handle, 0); - } - */ - - taosFreeQitem(item); - - return 0; -} - -int processWalMsg(void *item) { - SWalHead *pHead = (SWalHead *)item; - - if (pHead->version <= tversion) { - uError("ver:%" PRIu64 ", wal is even lower than local:%" PRIu64, pHead->version, tversion); - return -1; - }; - - uDebug("ver:%" PRIu64 ", wal from peer is received", pHead->version); - writeIntoWal(pHead); - tversion = pHead->version; - - // write into cache - - /* - if (pHead->handle) { - syncSendFwdAck(syncHandle, pHead->handle, 0); - } - */ - - taosFreeQitem(item); - - return 0; -} - -void *processWriteQueue(void *param) { - int type; - void *item; - - setThreadName("syncWrite"); - - while (1) { - int ret = taosReadQitem(qhandle, &type, &item); - if (ret <= 0) { - usleep(1000); - continue; - } - - if (type == TAOS_QTYPE_RPC) { - processRpcMsg(item); - } else if (type == TAOS_QTYPE_WAL) { - processWalMsg(item); - } else if (type == TAOS_QTYPE_FWD) { - processFwdMsg(item); - } - } - - return NULL; -} - -int retrieveAuthInfo(char *meterId, char *spi, char *encrypt, char *secret, char *ckey) { - // app shall retrieve the auth info based on meterID from DB or a data file - // demo code here only for simple demo - int ret = 0; - - if (strcmp(meterId, "michael") == 0) { - *spi = 1; - *encrypt = 0; - strcpy(secret, "mypassword"); - strcpy(ckey, "key"); - } else if (strcmp(meterId, "jeff") == 0) { - *spi = 0; - *encrypt = 0; - } else { - ret = -1; // user not there - } - - return ret; -} - -void processRequestMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet) { - SRpcMsg *pTemp; - - pTemp = taosAllocateQitem(sizeof(SRpcMsg)); - memcpy(pTemp, pMsg, sizeof(SRpcMsg)); - - uDebug("request is received, type:%d, len:%d", pMsg->msgType, pMsg->contLen); - taosWriteQitem(qhandle, TAOS_QTYPE_RPC, pTemp); -} - -uint32_t getFileInfo(int32_t vgId, char *name, uint32_t *index, uint32_t eindex, int64_t *size, uint64_t *fversion) { - uint32_t magic; - struct stat fstat; - char aname[280]; - - if (*index == 2) { - uInfo("wait for a while ....."); - sleep(3); - } - - if (name[0] == 0) { - // find the file - snprintf(aname, sizeof(aname), "%s/data/data.%d", path, *index); - sprintf(name, "data/data.%d", *index); - } else { - snprintf(aname, sizeof(aname), "%s/%s", path, name); - } - - uInfo("get file info:%s", aname); - if (stat(aname, &fstat) < 0) return 0; - - *size = fstat.st_size; - magic = fstat.st_size; - - return magic; -} - -int getWalInfo(int32_t vgId, char *name, int64_t *index) { - struct stat fstat; - char aname[280]; - - name[0] = 0; - if (*index + 1 > walNum) return 0; - - snprintf(aname, sizeof(aname), "%s/wal/wal.%d", path, *index); - sprintf(name, "wal/wal.%d", *index); - uInfo("get wal info:%s", aname); - - if (stat(aname, &fstat) < 0) return -1; - - if (*index >= walNum - 1) return 0; // no more - - return 1; -} - -int writeToCache(int32_t vgId, void *data, int type) { - SWalHead *pHead = data; - - uDebug("rsp from peer is received, ver:%" PRIu64 " len:%d type:%d", pHead->version, pHead->len, type); - - int msgSize = pHead->len + sizeof(SWalHead); - void *pMsg = taosAllocateQitem(msgSize); - memcpy(pMsg, pHead, msgSize); - taosWriteQitem(qhandle, type, pMsg); - - return 0; -} - -void confirmFwd(int32_t vgId, int64_t version) { return; } - -void notifyRole(int32_t vgId, int8_t r) { - role = r; - printf("current role:%s\n", syncRole[role]); -} - -void initSync() { - pCfg->replica = 1; - pCfg->quorum = 1; - syncInfo.vgId = 1; - syncInfo.getWalInfoFp = getWalInfo; - syncInfo.writeToCacheFp = writeToCache; - syncInfo.confirmForward = confirmForward; - syncInfo.notifyRoleFp = notifyRole; - - pCfg->nodeInfo[0].nodeId = 1; - pCfg->nodeInfo[0].nodePort = 7010; - taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); - - pCfg->nodeInfo[1].nodeId = 2; - pCfg->nodeInfo[1].nodePort = 7110; - taosGetFqdn(pCfg->nodeInfo[1].nodeFqdn); - - pCfg->nodeInfo[2].nodeId = 3; - pCfg->nodeInfo[2].nodePort = 7210; - taosGetFqdn(pCfg->nodeInfo[2].nodeFqdn); - - pCfg->nodeInfo[3].nodeId = 4; - pCfg->nodeInfo[3].nodePort = 7310; - taosGetFqdn(pCfg->nodeInfo[3].nodeFqdn); - - pCfg->nodeInfo[4].nodeId = 5; - pCfg->nodeInfo[4].nodePort = 7410; - taosGetFqdn(pCfg->nodeInfo[4].nodeFqdn); -} - -void doSync() { - for (int i = 0; i < 5; ++i) { - if (tsSyncPort == pCfg->nodeInfo[i].nodePort) nodeId = pCfg->nodeInfo[i].nodeId; - } - - snprintf(path, sizeof(path), "/root/test/d%d", nodeId); - tstrncpy(syncInfo.path, path, sizeof(syncInfo.path)); - - if (syncHandle == NULL) { - syncHandle = syncStart(&syncInfo); - } else { - if (syncReconfig(syncHandle, pCfg) < 0) syncHandle = NULL; - } - - uInfo("nodeId:%d path:%s syncPort:%d", nodeId, path, tsSyncPort); -} - -int main(int argc, char *argv[]) { - SRpcInit rpcInit; - char dataName[20] = "server.data"; - pCfg = &syncInfo.syncCfg; - - initSync(); - - memset(&rpcInit, 0, sizeof(rpcInit)); - rpcInit.localPort = 7000; - rpcInit.label = "SER"; - rpcInit.numOfThreads = 1; - rpcInit.cfp = processRequestMsg; - rpcInit.sessions = 1000; - rpcInit.idleTime = tsShellActivityTimer * 1500; - rpcInit.afp = retrieveAuthInfo; - - for (int i = 1; i < argc; ++i) { - if (strcmp(argv[i], "-p") == 0 && i < argc - 1) { - rpcInit.localPort = atoi(argv[++i]); - } else if (strcmp(argv[i], "-t") == 0 && i < argc - 1) { - rpcInit.numOfThreads = atoi(argv[++i]); - } else if (strcmp(argv[i], "-m") == 0 && i < argc - 1) { - msgSize = atoi(argv[++i]); - } else if (strcmp(argv[i], "-s") == 0 && i < argc - 1) { - rpcInit.sessions = atoi(argv[++i]); - } else if (strcmp(argv[i], "-o") == 0 && i < argc - 1) { - tsCompressMsgSize = atoi(argv[++i]); - } else if (strcmp(argv[i], "-w") == 0 && i < argc - 1) { - commit = atoi(argv[++i]); - } else if (strcmp(argv[i], "-v") == 0 && i < argc - 1) { - syncInfo.version = atoi(argv[++i]); - } else if (strcmp(argv[i], "-r") == 0 && i < argc - 1) { - pCfg->replica = atoi(argv[++i]); - } else if (strcmp(argv[i], "-q") == 0 && i < argc - 1) { - pCfg->quorum = atoi(argv[++i]); - } else if (strcmp(argv[i], "-d") == 0 && i < argc - 1) { - rpcDebugFlag = atoi(argv[++i]); - } else { - printf("\nusage: %s [options] \n", argv[0]); - printf(" [-p port]: server port number, default is:%d\n", rpcInit.localPort); - printf(" [-t threads]: number of rpc threads, default is:%d\n", rpcInit.numOfThreads); - printf(" [-s sessions]: number of sessions, default is:%d\n", rpcInit.sessions); - printf(" [-m msgSize]: message body size, default is:%d\n", msgSize); - printf(" [-o compSize]: compression message size, default is:%d\n", tsCompressMsgSize); - printf(" [-w write]: write received data to file(0, 1, 2), default is:%d\n", commit); - printf(" [-v version]: initial node version, default is:%" PRId64 "\n", syncInfo.version); - printf(" [-r replica]: replicacation number, default is:%d\n", pCfg->replica); - printf(" [-q quorum]: quorum, default is:%d\n", pCfg->quorum); - printf(" [-d debugFlag]: debug flag, default:%d\n", rpcDebugFlag); - printf(" [-h help]: print out this help\n\n"); - exit(0); - } - } - - uDebugFlag = rpcDebugFlag; - dDebugFlag = rpcDebugFlag; - // tmrDebugFlag = rpcDebugFlag; - tsAsyncLog = 0; - taosInitLog("server.log", 1000000, 10); - - rpcInit.connType = TAOS_CONN_SERVER; - void *pRpc = rpcOpen(&rpcInit); - if (pRpc == NULL) { - uError("failed to start RPC server"); - return -1; - } - - tsSyncPort = rpcInit.localPort + 10; - qhandle = taosOpenQueue(); - - doSync(); - - pthread_attr_t thattr; - pthread_t thread; - pthread_attr_init(&thattr); - pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(&thread, &thattr, processWriteQueue, NULL) != 0) { - uError("failed to create thread, reason:%s", strerror(errno)); - return -1; - } - - printf("server is running, localPort:%d\n", rpcInit.localPort); - SNodesRole nroles; - - while (1) { - int c = getchar(); - - switch (c) { - case '1': - pCfg->replica = 1; - doSync(); - break; - case '2': - pCfg->replica = 2; - doSync(); - break; - case '3': - pCfg->replica = 3; - doSync(); - break; - case '4': - pCfg->replica = 4; - doSync(); - break; - case '5': - pCfg->replica = 5; - doSync(); - break; - case 's': - syncGetNodesRole(syncHandle, &nroles); - for (int i = 0; i < pCfg->replica; ++i) - printf("=== nodeId:%d role:%s\n", nroles.nodeId[i], syncRole[nroles.role[i]]); - break; - default: - break; - } - - if (c == 'q') break; - } - - syncStop(syncHandle); - - if (dataFd >= 0) { - close(dataFd); - remove(dataName); - } - - return 0; -} From 12c6ea282b6837ddb5cc25dfb79e25f723aecc98 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 3 Nov 2021 11:13:17 +0800 Subject: [PATCH 26/33] refact --- .../server/vnode/tsdb/impl/tsdbImpl.h | 15 +++-- include/server/vnode/tsdb/tsdb.h | 63 +++---------------- source/dnode/vnode/impl/src/vnodeCommit.c | 2 + .../vnode/tsdb/{src/tsdb.c => inc/tsdbDef.h} | 26 +++++--- source/dnode/vnode/tsdb/inc/tsdbMemTable.h | 46 -------------- .../tsdb/inc/{tsdbInt.h => tsdbOptions.h} | 6 +- source/dnode/vnode/tsdb/src/tsdbMain.c | 16 +++++ source/dnode/vnode/tsdb/src/tsdbMemTable.c | 49 --------------- .../tsdb/src/{tsdbSMA.c => tsdbOptions.c} | 2 +- 9 files changed, 55 insertions(+), 170 deletions(-) rename source/dnode/vnode/tsdb/inc/tsdbWriteBatch.h => include/server/vnode/tsdb/impl/tsdbImpl.h (74%) rename source/dnode/vnode/tsdb/{src/tsdb.c => inc/tsdbDef.h} (66%) delete mode 100644 source/dnode/vnode/tsdb/inc/tsdbMemTable.h rename source/dnode/vnode/tsdb/inc/{tsdbInt.h => tsdbOptions.h} (89%) create mode 100644 source/dnode/vnode/tsdb/src/tsdbMain.c delete mode 100644 source/dnode/vnode/tsdb/src/tsdbMemTable.c rename source/dnode/vnode/tsdb/src/{tsdbSMA.c => tsdbOptions.c} (99%) diff --git a/source/dnode/vnode/tsdb/inc/tsdbWriteBatch.h b/include/server/vnode/tsdb/impl/tsdbImpl.h similarity index 74% rename from source/dnode/vnode/tsdb/inc/tsdbWriteBatch.h rename to include/server/vnode/tsdb/impl/tsdbImpl.h index f6b761f6a5..15f611c703 100644 --- a/source/dnode/vnode/tsdb/inc/tsdbWriteBatch.h +++ b/include/server/vnode/tsdb/impl/tsdbImpl.h @@ -13,22 +13,21 @@ * along with this program. If not, see . */ -#ifndef _TD_TSDB_WRITE_BATCH_H_ -#define _TD_TSDB_WRITE_BATCH_H_ +#ifndef _TD_TSDB_IMPL_H_ +#define _TD_TSDB_IMPL_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -typedef struct STsdbWriteBatch STsdbWriteBatch; - -/* ------------------------- ------------------------- */ -struct STsdbWriteBatch { - // TODO +struct STsdbOptions { + /* TODO */ }; #ifdef __cplusplus } #endif -#endif /*_TD_TSDB_WRITE_BATCH_H_*/ \ No newline at end of file +#endif /*_TD_TSDB_IMPL_H_*/ \ No newline at end of file diff --git a/include/server/vnode/tsdb/tsdb.h b/include/server/vnode/tsdb/tsdb.h index 09cebc7bb6..949ac679ae 100644 --- a/include/server/vnode/tsdb/tsdb.h +++ b/include/server/vnode/tsdb/tsdb.h @@ -16,67 +16,24 @@ #ifndef _TD_TSDB_H_ #define _TD_TSDB_H_ -#include "os.h" -#include "taosmsg.h" +#include "impl/tsdbImpl.h" #ifdef __cplusplus extern "C" { #endif -// Types exported -typedef struct STsdb STsdb; -typedef struct STsdbOptions STsdbOptions; -typedef struct STsdbSMAOptions STsdbSMAOptions; // SMA stands for Small Materialized Aggregation -typedef struct STsdbReadOptions STsdbReadOptions; -typedef struct STsdbSnapshot STsdbSnapshot; -typedef struct STsdbQueryHandle STsdbQueryHandle; +// TYPES EXPOSED +typedef struct STsdb STsdb; +typedef struct STsdbOptions STsdbOptions; -// DB operations -int tsdbCreate(const char *path); -int tsdbDestroy(const char *path); -STsdb *tsdbOpen(const STsdbOptions *options); +// STsdb +STsdb *tsdbOpen(const char *path, const STsdbOptions *); void tsdbClose(STsdb *); -int tsdbReset(STsdb *, const STsdbOptions *); -int tsdbInsert(STsdb *, SSubmitReq *, SSubmitRsp *); -int tsdbCommit(STsdb *); -int tsdbCompact(STsdb *); +void tsdbRemove(const char *path); -// Options -STsdbOptions *tsdbOptionsCreate(); -void tsdbOptionsDestroy(STsdbOptions *); -void tsdbOptionsSetId(STsdbOptions *, int id); -void tsdbOptionsSetHoursPerFile(STsdbOptions *, int hours); -void tsdbOptionsSetRetention(STsdbOptions *, int keep, int keep1, int keep2); -void tsdbOptionsSetMinAndMaxRows(STsdbOptions *, int minRows, int maxRows); -void tsdbOptionsSetPrecision(STsdbOptions *, int); -void tsdbOptionsSetCache(STsdbOptions *, int); -typedef enum { TSDB_NO_UPDATE = 0, TSDB_WHOLE_ROW_UPDATE = 1, TSDB_PARTIAL_ROW_UPDATE = 2 } ETsdbUpdateType; -void tsdbOptionsSetUpdate(STsdbOptions *, ETsdbUpdateType); -void tsdbOptionsSetSMA(STsdbOptions *, STsdbSMAOptions *); - -// STsdbSMAOptions -STsdbSMAOptions *tsdbSMAOptionsCreate(); -void tsdbSMAOptionsDestroy(STsdbSMAOptions *); -// void tsdbSMAOptionsSetFuncs(STsdbSMAOptions *, SArray * /*Array of function to perform on each block*/); -// void tsdbSMAOptionsSetIntervals(STsdbSMAOptions *, SArray *); -// void tsdbSMAOptionsSetColTypes(STsdbSMAOptions *, SArray *); - -// STsdbQueryHandle -STsdbQueryHandle *tsdbQueryHandleCreate(STsdb *, STsdbReadOptions *); -void tsdbQueryHandleDestroy(STsdbQueryHandle *); -void tsdbResetQueryHandle(STsdbQueryHandle *, STsdbReadOptions *); -bool tsdbNextDataBlock(STsdbQueryHandle *); -// void tsdbGetDataBlockInfo(STsdbQueryHandle *, SDataBlockInfo *); -// void tsdbGetDataBlockStatisInfo(STsdbQueryHandle *, SDataStatis **); - -// STsdbReadOptions -STsdbReadOptions *tsdbReadOptionsCreate(); -void tsdbReadOptionsDestroy(STsdbReadOptions *); -void tsdbReadOptionsSetSnapshot(STsdbReadOptions *, STsdbSnapshot *); - -// STsdbSnapshot -STsdbSnapshot *tsdbSnapshotCreate(STsdb *); -void tsdbSnapshotDestroy(STsdbSnapshot *); +// STsdbOptions +int tsdbOptionsInit(STsdbOptions *); +void tsdbOptionsClear(STsdbOptions *); #ifdef __cplusplus } diff --git a/source/dnode/vnode/impl/src/vnodeCommit.c b/source/dnode/vnode/impl/src/vnodeCommit.c index 3200411f4d..826589e8c9 100644 --- a/source/dnode/vnode/impl/src/vnodeCommit.c +++ b/source/dnode/vnode/impl/src/vnodeCommit.c @@ -19,6 +19,7 @@ static int vnodeStartCommit(SVnode *pVnode); static int vnodeEndCommit(SVnode *pVnode); int vnodeAsyncCommit(SVnode *pVnode) { + #if 0 if (vnodeStartCommit(pVnode) < 0) { // TODO } @@ -39,6 +40,7 @@ int vnodeAsyncCommit(SVnode *pVnode) { // TODO } +#endif return 0; } diff --git a/source/dnode/vnode/tsdb/src/tsdb.c b/source/dnode/vnode/tsdb/inc/tsdbDef.h similarity index 66% rename from source/dnode/vnode/tsdb/src/tsdb.c rename to source/dnode/vnode/tsdb/inc/tsdbDef.h index 520c342a63..ebe0d6b4b0 100644 --- a/source/dnode/vnode/tsdb/src/tsdb.c +++ b/source/dnode/vnode/tsdb/inc/tsdbDef.h @@ -13,17 +13,23 @@ * along with this program. If not, see . */ -#include "tsdb.h" -#include "tkv.h" -#include "tsdbMemTable.h" +#ifndef _TD_TSDB_DEF_H_ +#define _TD_TSDB_DEF_H_ + +#include "tsdb.h" +#include "tsdbOptions.h" + +#ifdef __cplusplus +extern "C" { +#endif -/* -------------- -------------- */ struct STsdb { - STkvDb *tsdb; // original time-series data - STkvDb *lrowdb; // last row cache - STkvDb *lastdb; // last cache - STkvDb *fivemindb; + char * path; + STsdbOptions options; }; -int tsdbInsert(STsdb *tsdb, SSubmitReq *pReq, SSubmitRsp *pRsp) { return 0; } -int tsdbCommit(STsdb *pTsdb) { return 0; } \ No newline at end of file +#ifdef __cplusplus +} +#endif + +#endif /*_TD_TSDB_DEF_H_*/ \ No newline at end of file diff --git a/source/dnode/vnode/tsdb/inc/tsdbMemTable.h b/source/dnode/vnode/tsdb/inc/tsdbMemTable.h deleted file mode 100644 index 5d1dcfcac7..0000000000 --- a/source/dnode/vnode/tsdb/inc/tsdbMemTable.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_TSDB_MEMTABLE_H_ -#define _TD_TSDB_MEMTABLE_H_ - -#include "tdef.h" -#include "thash.h" -#include "amalloc.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct STsdbMemTable STsdbMemTable; - -STsdbMemTable *tsdbMemTableCreate(SMemAllocator *); -void tsdbMemTableDestroy(STsdbMemTable *); -int tsdbMemTableWriteBatch(STsdbMemTable *pTsdbMemTable, void *batch); - -/* --------------------- For compile and test only --------------------- */ -struct STsdbMemTable { - TSKEY minKey; - TSKEY maxKey; - SHashObj * tData; // uid --> SSkipList - SMemAllocator *ma; - T_REF_DECLARE() -}; - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_TSDB_MEMTABLE_H_*/ \ No newline at end of file diff --git a/source/dnode/vnode/tsdb/inc/tsdbInt.h b/source/dnode/vnode/tsdb/inc/tsdbOptions.h similarity index 89% rename from source/dnode/vnode/tsdb/inc/tsdbInt.h rename to source/dnode/vnode/tsdb/inc/tsdbOptions.h index b9ee525a9d..4d6a250424 100644 --- a/source/dnode/vnode/tsdb/inc/tsdbInt.h +++ b/source/dnode/vnode/tsdb/inc/tsdbOptions.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef _TD_TSDB_INT_H_ -#define _TD_TSDB_INT_H_ +#ifndef _TD_TSDB_OPTIONS_H_ +#define _TD_TSDB_OPTIONS_H_ #ifdef __cplusplus extern "C" { @@ -24,4 +24,4 @@ extern "C" { } #endif -#endif /*_TD_TSDB_INT_H_*/ \ No newline at end of file +#endif /*_TD_TSDB_OPTIONS_H_*/ \ No newline at end of file diff --git a/source/dnode/vnode/tsdb/src/tsdbMain.c b/source/dnode/vnode/tsdb/src/tsdbMain.c new file mode 100644 index 0000000000..61e887dd45 --- /dev/null +++ b/source/dnode/vnode/tsdb/src/tsdbMain.c @@ -0,0 +1,16 @@ +/* + * 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 . + */ + +#include "tsdbDef.h" \ No newline at end of file diff --git a/source/dnode/vnode/tsdb/src/tsdbMemTable.c b/source/dnode/vnode/tsdb/src/tsdbMemTable.c deleted file mode 100644 index 9fd815155f..0000000000 --- a/source/dnode/vnode/tsdb/src/tsdbMemTable.c +++ /dev/null @@ -1,49 +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 . - */ - -#include "tsdbMemTable.h" - -STsdbMemTable *tsdbMemTableCreate(SMemAllocator *ma) { - STsdbMemTable *pTsdbMemTable = NULL; - - pTsdbMemTable = (STsdbMemTable *)malloc(sizeof(*pTsdbMemTable)); - if (pTsdbMemTable == NULL) { - return NULL; - } - - // TODO - pTsdbMemTable->minKey = TSKEY_INITIAL_VAL; - pTsdbMemTable->maxKey = TSKEY_INITIAL_VAL; - pTsdbMemTable->ma = ma; - pTsdbMemTable->tData = taosHashInit(1024, taosIntHash_64, true /* TODO */, HASH_NO_LOCK); - if (pTsdbMemTable->tData == NULL) { - // TODO - } - - return pTsdbMemTable; -} - -void tsdbMemTableDestroy(STsdbMemTable *pTsdbMemTable) { - if (pTsdbMemTable) { - // TODO - free(pTsdbMemTable); - } -} - -int tsdbMemTableWriteBatch(STsdbMemTable *pTsdbMemTable, void *batch) { - // TODO - - return 0; -} \ No newline at end of file diff --git a/source/dnode/vnode/tsdb/src/tsdbSMA.c b/source/dnode/vnode/tsdb/src/tsdbOptions.c similarity index 99% rename from source/dnode/vnode/tsdb/src/tsdbSMA.c rename to source/dnode/vnode/tsdb/src/tsdbOptions.c index f2f48bbc8a..6dea4a4e57 100644 --- a/source/dnode/vnode/tsdb/src/tsdbSMA.c +++ b/source/dnode/vnode/tsdb/src/tsdbOptions.c @@ -11,4 +11,4 @@ * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . - */ + */ \ No newline at end of file From ab4b640fffec28abebebac9bca8fc192b1ad8a34 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 4 Nov 2021 09:28:55 +0800 Subject: [PATCH 27/33] dnode-vnodes --- include/common/taosmsg.h | 98 ++-- include/server/vnode/vnode.h | 17 +- source/dnode/mgmt/inc/dnodeInt.h | 5 +- source/dnode/mgmt/inc/dnodeVnodes.h | 7 +- source/dnode/mgmt/src/dnodeInt.c | 4 +- source/dnode/mgmt/src/dnodeTransport.c | 62 +-- source/dnode/mgmt/src/dnodeVnodes.c | 619 ++++++++++++++++++++++++- source/dnode/vnode/impl/src/vnodeInt.c | 4 +- 8 files changed, 709 insertions(+), 107 deletions(-) diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index e798a0c42a..49e8892c7d 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -36,7 +36,7 @@ enum { TSDB_MESSAGE_NULL = 0, #endif -// message from client to dnode +// message from client to vnode TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SUBMIT, "submit" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_QUERY, "query" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_FETCH, "fetch" ) @@ -46,25 +46,12 @@ TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_TABLE, "alter-table" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_UPDATE_TAG_VAL, "update-tag-val" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_TABLE_META, "table-meta" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_TABLES_META, "tables-meta" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_STABLE_VGROUP, "stable-vgroup" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_CONSUME, "mq-consume" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_QUERY, "mq-query" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_CONNECT, "mq-connect" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_DISCONNECT, "mq-disconnect" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_ACK, "mq-ack" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_MQ_RESET, "mq-reset" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_NETWORK_TEST, "nettest" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY0, "dummy0" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY1, "dummy1" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY2, "dummy2" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY3, "dummy3" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY4, "dummy4" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY5, "dummy5" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY6, "dummy6" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY7, "dummy7" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY8, "dummy8" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY9, "dummy9" ) - // message from client to mnode TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CONNECT, "connect" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_ACCT, "create-acct" ) @@ -88,6 +75,7 @@ TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_FUNCTION, "create-function" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_FUNCTION, "alter-function" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_FUNCTION, "drop-function" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_STABLE, "create-stable" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_STABLE_VGROUP, "stable-vgroup" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_STABLE, "drop-stable" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_STABLE, "alter-stable" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_KILL_QUERY, "kill-query" ) @@ -97,54 +85,55 @@ TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SHOW, "show" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SHOW_RETRIEVE, "retrieve" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SHOW_RETRIEVE_FUNC, "retrieve-func" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_COMPACT_VNODE, "compact-vnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY10, "dummy10" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY11, "dummy11" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY12, "dummy12" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY13, "dummy13" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY14, "dummy14" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY15, "dummy15" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY16, "dummy16" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY17, "dummy17" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY18, "dummy18" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY19, "dummy19" ) +// message from client to qnode +// message from client to dnode +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_NETWORK_TEST, "nettest" ) -// message from mnode to dnode +// message from vnode to vnode +// message from vnode to mnode +// message from vnode to qnode +// message from vnode to dnode + +// message from mnode to vnode TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_STABLE_IN, "create-stable" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_STABLE_IN, "alter-stable" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_STABLE_IN, "drop-stable" ) +// message from mnode to mnode +// message from mnode to qnode +// message from mnode to dnode TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_VNODE_IN, "create-vnode" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_ALTER_VNODE_IN, "alter-vnode" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_VNODE_IN, "drop-vnode" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_AUTH_VNODE_IN, "auth-vnode" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_SYNC_VNODE_IN, "sync-vnode" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_COMPACT_VNODE_IN, "compact-vnode" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CREATE_MNODE_IN, "create-mnode" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DROP_MNODE_IN, "drop-mnode" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_CONFIG_DNODE_IN, "config-dnode" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY20, "dummy20" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY21, "dummy21" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY22, "dummy22" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY23, "dummy23" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY24, "dummy24" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY25, "dummy25" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY26, "dummy26" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY27, "dummy27" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY28, "dummy28" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY29, "dummy29" ) +// message from qnode to vnode +// message from qnode to mnode +// message from qnode to qnode +// message from qnode to dnode + +// message from dnode to vnode // message from dnode to mnode TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_STATUS, "status" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_GRANT, "grant" ) TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_AUTH, "auth" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY30, "dummy30" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY31, "dummy31" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY32, "dummy32" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY33, "dummy33" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY34, "dummy34" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY35, "dummy35" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY36, "dummy36" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY37, "dummy37" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY38, "dummy38" ) -TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY39, "dummy39" ) +// message from dnode to qnode +// message from dnode to dnode + +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY0, "dummy0" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY1, "dummy1" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY2, "dummy2" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY3, "dummy3" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY4, "dummy4" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY5, "dummy5" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY6, "dummy6" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY7, "dummy7" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY8, "dummy8" ) +TAOS_DEFINE_MESSAGE_TYPE( TSDB_MSG_TYPE_DUMMY9, "dummy9" ) #ifndef TAOS_MESSAGE_C TSDB_MSG_TYPE_MAX // 147 @@ -428,10 +417,6 @@ typedef struct { char tableFname[TSDB_TABLE_FNAME_LEN]; } SDropSTableMsg; -typedef struct { - int32_t vgId; -} SDropVnodeMsg, SSyncVnodeMsg, SCompactVnodeMsg; - typedef struct SColIndex { int16_t colId; // column id int16_t colIndex; // column index in colList if it is a normal column or index in tagColList if a tag @@ -661,9 +646,8 @@ typedef struct { typedef struct { int32_t vgId; - int8_t status; int8_t role; - int8_t reserved[2]; + int8_t reserved[3]; int64_t totalStorage; int64_t compStorage; int64_t pointsWritten; @@ -737,9 +721,18 @@ typedef struct { int8_t replica; int8_t quorum; int8_t selfIndex; - SVnodeDesc nodes[TSDB_MAX_REPLICA]; + SVnodeDesc replicas[TSDB_MAX_REPLICA]; } SCreateVnodeMsg, SAlterVnodeMsg; +typedef struct { + int32_t vgId; +} SDropVnodeMsg, SSyncVnodeMsg, SCompactVnodeMsg; + +typedef struct { + int32_t vgId; + int8_t accessState; +} SAuthVnodeMsg; + typedef struct { char tableFname[TSDB_TABLE_FNAME_LEN]; int16_t createFlag; @@ -1002,6 +995,7 @@ typedef struct { /* data */ } SUpdateTagValRsp; + #pragma pack(pop) #ifdef __cplusplus diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index c4f8df79c1..a20a7fd410 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -27,7 +27,7 @@ extern "C" { typedef struct SVnode SVnode; typedef struct { - char dbName[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; int32_t cacheBlockSize; // MB int32_t totalBlocks; int32_t daysPerFile; @@ -47,17 +47,6 @@ typedef struct { SVnodeDesc replicas[TSDB_MAX_REPLICA]; } SVnodeCfg; -typedef struct { - int64_t totalStorage; - int64_t compStorage; - int64_t pointsWritten; - int64_t tablesNum; -} SVnodeStatisic; - -typedef struct { - int8_t syncRole; -} SVnodeStatus; - typedef struct SVnodeMsg { int32_t msgType; int32_t code; @@ -69,9 +58,6 @@ typedef struct SVnodeMsg { int32_t vnodeInit(); void vnodeCleanup(); -int32_t vnodeGetStatistics(SVnode *pVnode, SVnodeStatisic *pStat); -int32_t vnodeGetStatus(SVnode *pVnode, SVnodeStatus *pStatus); - SVnode *vnodeOpen(int32_t vgId, const char *path); void vnodeClose(SVnode *pVnode); int32_t vnodeAlter(SVnode *pVnode, const SVnodeCfg *pCfg); @@ -80,6 +66,7 @@ int32_t vnodeDrop(SVnode *pVnode); int32_t vnodeCompact(SVnode *pVnode); int32_t vnodeSync(SVnode *pVnode); +void vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad); void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg); #ifdef __cplusplus diff --git a/source/dnode/mgmt/inc/dnodeInt.h b/source/dnode/mgmt/inc/dnodeInt.h index 472c467ad6..906455dce4 100644 --- a/source/dnode/mgmt/inc/dnodeInt.h +++ b/source/dnode/mgmt/inc/dnodeInt.h @@ -42,7 +42,10 @@ void dnodeCleanup(); EDnStat dnodeGetRunStat(); void dnodeSetRunStat(); -void dnodeGetStartup(SStartupMsg *); + +void dnodeReportStartup(char *name, char *desc); +void dnodeReportStartupFinished(char *name, char *desc); +void dnodeGetStartup(SStartupMsg *); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/inc/dnodeVnodes.h b/source/dnode/mgmt/inc/dnodeVnodes.h index 6e9bce9ae5..2b72ba5d59 100644 --- a/source/dnode/mgmt/inc/dnodeVnodes.h +++ b/source/dnode/mgmt/inc/dnodeVnodes.h @@ -23,9 +23,14 @@ extern "C" { int32_t dnodeInitVnodes(); void dnodeCleanupVnodes(); -void dnodeProcessVnodesMsg(SRpcMsg *pMsg, SEpSet *pEpSet); void dnodeGetVnodes(SVnodeLoads *pVloads); +void dnodeProcessVnodeMgmtMsg(SRpcMsg *pMsg, SEpSet *pEpSet); +void dnodeProcessVnodeWriteMsg(SRpcMsg *pMsg, SEpSet *pEpSet); +void dnodeProcessVnodeSyncMsg(SRpcMsg *pMsg, SEpSet *pEpSet); +void dnodeProcessVnodeQueryMsg(SRpcMsg *pMsg, SEpSet *pEpSet); +void dnodeProcessVnodeFetchMsg(SRpcMsg *pMsg, SEpSet *pEpSet); + #ifdef __cplusplus } #endif diff --git a/source/dnode/mgmt/src/dnodeInt.c b/source/dnode/mgmt/src/dnodeInt.c index e853ba7f14..e7018f4265 100644 --- a/source/dnode/mgmt/src/dnodeInt.c +++ b/source/dnode/mgmt/src/dnodeInt.c @@ -35,14 +35,14 @@ EDnStat dnodeGetRunStat() { return tsInt.runStat; } void dnodeSetRunStat(EDnStat stat) { tsInt.runStat = stat; } -static void dnodeReportStartup(char *name, char *desc) { +void dnodeReportStartup(char *name, char *desc) { SStartupMsg *pStartup = &tsInt.startup; tstrncpy(pStartup->name, name, strlen(pStartup->name)); tstrncpy(pStartup->desc, desc, strlen(pStartup->desc)); pStartup->finished = 0; } -static void dnodeReportStartupFinished(char *name, char *desc) { +void dnodeReportStartupFinished(char *name, char *desc) { SStartupMsg *pStartup = &tsInt.startup; tstrncpy(pStartup->name, name, strlen(pStartup->name)); tstrncpy(pStartup->desc, desc, strlen(pStartup->desc)); diff --git a/source/dnode/mgmt/src/dnodeTransport.c b/source/dnode/mgmt/src/dnodeTransport.c index 18e97e2b9f..265efe3070 100644 --- a/source/dnode/mgmt/src/dnodeTransport.c +++ b/source/dnode/mgmt/src/dnodeTransport.c @@ -34,24 +34,22 @@ static struct { static void dnodeInitMsgFp() { // msg from client to dnode - tsTrans.msgFp[TSDB_MSG_TYPE_SUBMIT] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_QUERY] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_FETCH] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TABLE] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TABLE] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TABLE] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_TABLE_META] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_TABLES_META] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = dnodeProcessMnodeMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_QUERY] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONNECT] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_ACK] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_MQ_RESET] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_NETWORK_TEST] = dnodeProcessDnodeMsg; - + tsTrans.msgFp[TSDB_MSG_TYPE_SUBMIT] = dnodeProcessVnodeWriteMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_QUERY] = dnodeProcessVnodeQueryMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_FETCH] = dnodeProcessVnodeFetchMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_TABLE] = dnodeProcessVnodeWriteMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_TABLE] = dnodeProcessVnodeWriteMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_TABLE] = dnodeProcessVnodeWriteMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_UPDATE_TAG_VAL] = dnodeProcessVnodeWriteMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_TABLE_META] = dnodeProcessVnodeQueryMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_TABLES_META] = dnodeProcessVnodeQueryMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_QUERY] = dnodeProcessVnodeQueryMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONSUME] = dnodeProcessVnodeQueryMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_CONNECT] = dnodeProcessVnodeWriteMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_DISCONNECT] = dnodeProcessVnodeWriteMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_ACK] = dnodeProcessVnodeWriteMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_MQ_RESET] = dnodeProcessVnodeWriteMsg; + // msg from client to mnode tsTrans.msgFp[TSDB_MSG_TYPE_CONNECT] = dnodeProcessMnodeMsg; tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_ACCT] = dnodeProcessMnodeMsg; @@ -77,6 +75,7 @@ static void dnodeInitMsgFp() { tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE] = dnodeProcessMnodeMsg; tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE] = dnodeProcessMnodeMsg; tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_STABLE_VGROUP] = dnodeProcessMnodeMsg; tsTrans.msgFp[TSDB_MSG_TYPE_KILL_QUERY] = dnodeProcessMnodeMsg; tsTrans.msgFp[TSDB_MSG_TYPE_KILL_CONN] = dnodeProcessMnodeMsg; tsTrans.msgFp[TSDB_MSG_TYPE_HEARTBEAT] = dnodeProcessMnodeMsg; @@ -85,22 +84,29 @@ static void dnodeInitMsgFp() { tsTrans.msgFp[TSDB_MSG_TYPE_SHOW_RETRIEVE_FUNC] = dnodeProcessMnodeMsg; tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE] = dnodeProcessMnodeMsg; - // message from mnode to dnode - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = dnodeProcessVnodesMsg; + // message from client to dnode + tsTrans.msgFp[TSDB_MSG_TYPE_NETWORK_TEST] = dnodeProcessDnodeMsg; + + // message from mnode to vnode + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = dnodeProcessVnodeWriteMsg; tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN_RSP] = dnodeProcessMnodeMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE_IN] = dnodeProcessVnodeWriteMsg; tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_STABLE_IN_RSP] = dnodeProcessMnodeMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_STABLE_IN] = dnodeProcessVnodesMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE_IN_RSP] = dnodeProcessMnodeMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE_IN] = dnodeProcessVnodeWriteMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_STABLE_IN] = dnodeProcessMnodeMsg; + + // message from mnode to dnode + tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN] = dnodeProcessVnodeMgmtMsg; tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_VNODE_IN_RSP] = dnodeProcessMnodeMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN] = dnodeProcessVnodeMgmtMsg; tsTrans.msgFp[TSDB_MSG_TYPE_ALTER_VNODE_IN_RSP] = dnodeProcessMnodeMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN] = dnodeProcessVnodeMgmtMsg; tsTrans.msgFp[TSDB_MSG_TYPE_DROP_VNODE_IN_RSP] = dnodeProcessMnodeMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN] = dnodeProcessVnodeMgmtMsg; tsTrans.msgFp[TSDB_MSG_TYPE_SYNC_VNODE_IN_RSP] = dnodeProcessMnodeMsg; - tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN] = dnodeProcessVnodesMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_AUTH_VNODE_IN] = dnodeProcessVnodeMgmtMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_AUTH_VNODE_IN_RSP] = dnodeProcessMnodeMsg; + tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN] = dnodeProcessVnodeMgmtMsg; tsTrans.msgFp[TSDB_MSG_TYPE_COMPACT_VNODE_IN_RSP] = dnodeProcessMnodeMsg; tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN] = dnodeProcessMnodeMsg; tsTrans.msgFp[TSDB_MSG_TYPE_CREATE_MNODE_IN_RSP] = dnodeProcessMnodeMsg; diff --git a/source/dnode/mgmt/src/dnodeVnodes.c b/source/dnode/mgmt/src/dnodeVnodes.c index 59f5cf0fd0..c2e143da80 100644 --- a/source/dnode/mgmt/src/dnodeVnodes.c +++ b/source/dnode/mgmt/src/dnodeVnodes.c @@ -14,13 +14,622 @@ */ #define _DEFAULT_SOURCE -#include "dnodeDnode.h" +#include "dnodeVnodes.h" +#include "thash.h" +#include "tqueue.h" +#include "tstep.h" +#include "tthread.h" +#include "tworker.h" #include "vnode.h" -int32_t dnodeInitVnodes() { return vnodeInit(); } +typedef struct { + int32_t vgId; + int32_t refCount; + int8_t dropped; + int8_t accessState; + SVnode *pImpl; + taos_queue pWriteQ; + taos_queue pSyncQ; + taos_queue pApplyQ; + taos_queue pQueryQ; + taos_queue pFetchQ; +} SVnodeObj; -void dnodeCleanupVnodes() { vnodeCleanup(); } +typedef struct { + pthread_t *threadId; + int32_t threadIndex; + int32_t failed; + int32_t opened; + int32_t vnodeNum; + SVnodeObj *pVnodes; +} SVThread; -void dnodeProcessVnodesMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { vnodeProcessMsg(NULL, NULL); } +static struct { + SHashObj *hash; + SWorkerPool mgmtPool; + taos_queue pMgmtQ; + SSteps *pSteps; + int32_t openVnodes; + int32_t totalVnodes; + char file[PATH_MAX + 20]; +} tsVnodes; -void dnodeGetVnodes(SVnodeLoads *pVloads) {} \ No newline at end of file +static int32_t dnodeCreateVnodeWrapper(int32_t vgId, SVnode *pImpl) { + SVnodeObj *pVnode = calloc(1, sizeof(SVnodeObj)); + pVnode->vgId = vgId; + pVnode->refCount = 0; + pVnode->dropped = 0; + pVnode->accessState = TSDB_VN_ALL_ACCCESS; + pVnode->pImpl = pImpl; + pVnode->pWriteQ = NULL; + pVnode->pSyncQ = NULL; + pVnode->pApplyQ = NULL; + pVnode->pQueryQ = NULL; + pVnode->pFetchQ = NULL; + + return taosHashPut(tsVnodes.hash, &vgId, sizeof(int32_t), &pVnode, sizeof(SVnodeObj *)); +} + +static void dnodeDropVnodeWrapper(SVnodeObj *pVnode) { + taosHashRemove(tsVnodes.hash, &pVnode->vgId, sizeof(int32_t)); + + //todo wait all queue empty + pVnode->pWriteQ = NULL; + pVnode->pSyncQ = NULL; + pVnode->pApplyQ = NULL; + pVnode->pQueryQ = NULL; + pVnode->pFetchQ = NULL; +} + +static int32_t dnodeGetVnodesFromHash(SVnodeObj *pVnodes[], int32_t *numOfVnodes) { + void *pIter = taosHashIterate(tsVnodes.hash, NULL); + while (pIter) { + SVnodeObj **ppVnode = pIter; + if (*ppVnode) { + (*numOfVnodes)++; + if (*numOfVnodes >= TSDB_MAX_VNODES) { + dError("vgId:%d, too many open vnodes, exist:%d max:%d", (*ppVnode)->vgId, *numOfVnodes, TSDB_MAX_VNODES); + continue; + } else { + pVnodes[*numOfVnodes - 1] = (*ppVnode); + } + } + + pIter = taosHashIterate(tsVnodes.hash, pIter); + } + + return TSDB_CODE_SUCCESS; +} + +static int32_t dnodeGetVnodesFromFile(SVnodeObj *pVnodes, int32_t *numOfVnodes) { + pVnodes[0].vgId = 2; + pVnodes[0].dropped = 0; + pVnodes[0].vgId = 3; + pVnodes[0].dropped = 0; + return 0; +} + +static int32_t dnodeWriteVnodesToFile() { return 0; } + +static int32_t dnodeCreateVnode(int32_t vgId, SVnodeCfg *pCfg) { + int32_t code = 0; + + char path[PATH_MAX + 20] = {0}; + snprintf(path, sizeof(path),"%s/vnode%d", tsVnodeDir, vgId); + SVnode *pImpl = vnodeCreate(vgId, path, pCfg); + + if (pImpl == NULL) { + code = terrno; + return code; + } + + code = dnodeCreateVnodeWrapper(vgId, pImpl); + if (code != 0) { + vnodeDrop(pImpl); + return code; + } + + code = dnodeWriteVnodesToFile(); + if (code != 0) { + vnodeDrop(pImpl); + return code; + } + + return code; +} + +static int32_t dnodeDropVnode(SVnodeObj *pVnode) { + pVnode->dropped = 1; + + int32_t code = dnodeWriteVnodesToFile(); + if (code != 0) { + pVnode->dropped = 0; + return code; + } + + dnodeDropVnodeWrapper(pVnode); + vnodeDrop(pVnode->pImpl); + dnodeWriteVnodesToFile(); + return 0; +} + +static SVnodeObj *dnodeAcquireVnode(int32_t vgId) { + SVnodeObj *pVnode = NULL; + + taosHashGetClone(tsVnodes.hash, &vgId, sizeof(int32_t), (void *)&pVnode); + if (pVnode == NULL) { + terrno = TSDB_CODE_VND_INVALID_VGROUP_ID; + } + + int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1); + dTrace("vgId:%d, accquire vnode, refCount:%d", pVnode->vgId, refCount); + return pVnode; +} + +static void dnodeReleaseVnode(SVnodeObj *pVnode) { + int32_t refCount = atomic_sub_fetch_32(&pVnode->refCount, 1); + dTrace("vgId:%d, release vnode, refCount:%d", pVnode->vgId, refCount); +} + +static void *dnodeOpenVnodeFunc(void *param) { + SVThread *pThread = param; + + dDebug("thread:%d, start to open %d vnodes", pThread->threadIndex, pThread->vnodeNum); + setThreadName("open-vnodes"); + + for (int32_t v = 0; v < pThread->vnodeNum; ++v) { + SVnodeObj *pVnode = &pThread->pVnodes[v]; + + char stepDesc[TSDB_STEP_DESC_LEN] = {0}; + snprintf(stepDesc, TSDB_STEP_DESC_LEN, "vgId:%d, start to restore, %d of %d have been opened", pVnode->vgId, + tsVnodes.openVnodes, tsVnodes.totalVnodes); + dnodeReportStartup("open-vnodes", stepDesc); + + char path[PATH_MAX + 20] = {0}; + snprintf(path, sizeof(path),"%s/vnode%d", tsVnodeDir, pVnode->vgId); + SVnode *pImpl = vnodeOpen(pVnode->vgId, path); + if (pImpl == NULL) { + dError("vgId:%d, failed to open vnode by thread:%d", pVnode->vgId, pThread->threadIndex); + pThread->failed++; + } else { + dnodeCreateVnodeWrapper(pVnode->vgId, pImpl); + dDebug("vgId:%d, is opened by thread:%d", pVnode->vgId, pThread->threadIndex); + pThread->opened++; + } + + atomic_add_fetch_32(&tsVnodes.openVnodes, 1); + } + + dDebug("thread:%d, total vnodes:%d, opened:%d failed:%d", pThread->threadIndex, pThread->vnodeNum, pThread->opened, + pThread->failed); + return NULL; +} + +static int32_t dnodeOpenVnodes() { + tsVnodes.hash = taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); + if (tsVnodes.hash == NULL) { + dError("failed to init vnode hash"); + return TSDB_CODE_VND_OUT_OF_MEMORY; + } + + SVnodeObj pVnodes[TSDB_MAX_VNODES] = {0}; + int32_t numOfVnodes = 0; + int32_t code = dnodeGetVnodesFromFile(pVnodes, &numOfVnodes); + if (code != TSDB_CODE_SUCCESS) { + dInfo("failed to get vnode list from disk since %s", tstrerror(code)); + return code; + } + + tsVnodes.totalVnodes = numOfVnodes; + + int32_t threadNum = tsNumOfCores; + int32_t vnodesPerThread = numOfVnodes / threadNum + 1; + + SVThread *threads = calloc(threadNum, sizeof(SVThread)); + for (int32_t t = 0; t < threadNum; ++t) { + threads[t].threadIndex = t; + threads[t].pVnodes = calloc(vnodesPerThread, sizeof(SVnodeObj)); + } + + for (int32_t v = 0; v < numOfVnodes; ++v) { + int32_t t = v % threadNum; + SVThread *pThread = &threads[t]; + pThread->pVnodes[pThread->vnodeNum++] = pVnodes[v]; + } + + dInfo("start %d threads to open %d vnodes", threadNum, numOfVnodes); + + for (int32_t t = 0; t < threadNum; ++t) { + SVThread *pThread = &threads[t]; + if (pThread->vnodeNum == 0) continue; + + pThread->threadId = taosCreateThread(dnodeOpenVnodeFunc, pThread); + if (pThread->threadId == NULL) { + dError("thread:%d, failed to create thread to open vnode, reason:%s", pThread->threadIndex, strerror(errno)); + } + } + + for (int32_t t = 0; t < threadNum; ++t) { + SVThread *pThread = &threads[t]; + taosDestoryThread(pThread->threadId); + pThread->threadId = NULL; + free(pThread->pVnodes); + } + free(threads); + + if (tsVnodes.openVnodes != tsVnodes.totalVnodes) { + dError("there are total vnodes:%d, opened:%d", tsVnodes.totalVnodes, tsVnodes.openVnodes); + return -1; + } else { + dInfo("total vnodes:%d open successfully", tsVnodes.totalVnodes); + } + + return TSDB_CODE_SUCCESS; +} + +static void dnodeCloseVnodes() { + SVnodeObj *pVnodes[TSDB_MAX_VNODES] = {0}; + int32_t numOfVnodes = 0; + + int32_t code = dnodeGetVnodesFromHash(pVnodes, &numOfVnodes); + if (code != TSDB_CODE_SUCCESS) { + dInfo("failed to get dnode list since code %d", code); + return; + } + + for (int32_t i = 0; i < numOfVnodes; ++i) { + vnodeClose(pVnodes[i]->pImpl); + } + + if (tsVnodes.hash != NULL) { + taosHashCleanup(tsVnodes.hash); + tsVnodes.hash = NULL; + } + + dInfo("total vnodes:%d are all closed", numOfVnodes); +} + +static int32_t dnodeParseCreateVnodeReq(SRpcMsg *rpcMsg, int32_t *vgId, SVnodeCfg *pCfg) { + SCreateVnodeMsg *pCreate = rpcMsg->pCont; + *vgId = htonl(pCreate->vgId); + + tstrncpy(pCfg->db, pCreate->db, TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN); + pCfg->cacheBlockSize = htonl(pCreate->cacheBlockSize); + pCfg->totalBlocks = htonl(pCreate->totalBlocks); + pCfg->daysPerFile = htonl(pCreate->daysPerFile); + pCfg->daysToKeep0 = htonl(pCreate->daysToKeep0); + pCfg->daysToKeep1 = htonl(pCreate->daysToKeep1); + pCfg->daysToKeep2 = htonl(pCreate->daysToKeep2); + pCfg->minRowsPerFileBlock = htonl(pCreate->minRowsPerFileBlock); + pCfg->maxRowsPerFileBlock = htonl(pCreate->maxRowsPerFileBlock); + pCfg->precision = pCreate->precision; + pCfg->compression = pCreate->compression; + pCfg->cacheLastRow = pCreate->cacheLastRow; + pCfg->update = pCreate->update; + pCfg->quorum = pCreate->quorum; + pCfg->replica = pCreate->replica; + pCfg->walLevel = pCreate->walLevel; + pCfg->fsyncPeriod = htonl(pCreate->fsyncPeriod); + + for (int32_t i = 0; i < pCfg->replica; ++i) { + pCfg->replicas[i].port = htons(pCreate->replicas[i].port); + tstrncpy(pCfg->replicas[i].fqdn, pCreate->replicas[i].fqdn, TSDB_FQDN_LEN); + } + + return 0; +} + +static SDropVnodeMsg *vnodeParseDropVnodeReq(SRpcMsg *rpcMsg) { + SDropVnodeMsg *pDrop = rpcMsg->pCont; + pDrop->vgId = htonl(pDrop->vgId); + return pDrop; +} + +static SAuthVnodeMsg *vnodeParseAuthVnodeReq(SRpcMsg *rpcMsg) { + SAuthVnodeMsg *pAuth = rpcMsg->pCont; + pAuth->vgId = htonl(pAuth->vgId); + return pAuth; +} + +static int32_t vnodeProcessCreateVnodeReq(SRpcMsg *rpcMsg) { + SVnodeCfg vnodeCfg = {0}; + int32_t vgId = 0; + + dnodeParseCreateVnodeReq(rpcMsg, &vgId, &vnodeCfg); + dDebug("vgId:%d, create vnode req is received", vgId); + + SVnodeObj *pVnode = dnodeAcquireVnode(vgId); + if (pVnode != NULL) { + dDebug("vgId:%d, already exist, return success", vgId); + dnodeReleaseVnode(pVnode); + return 0; + } + + int32_t code = dnodeCreateVnode(vgId, &vnodeCfg); + if (code != 0) { + dError("vgId:%d, failed to create vnode since %s", vgId, tstrerror(code)); + } + + return code; +} + +static int32_t vnodeProcessAlterVnodeReq(SRpcMsg *rpcMsg) { + SVnodeCfg vnodeCfg = {0}; + int32_t vgId = 0; + int32_t code = 0; + + dnodeParseCreateVnodeReq(rpcMsg, &vgId, &vnodeCfg); + dDebug("vgId:%d, alter vnode req is received", vgId); + + SVnodeObj *pVnode = dnodeAcquireVnode(vgId); + if (pVnode == NULL) { + code = terrno; + dDebug("vgId:%d, failed to alter vnode since %s", vgId, tstrerror(code)); + return code; + } + + code = vnodeAlter(pVnode->pImpl, &vnodeCfg); + if (code != 0) { + dError("vgId:%d, failed to alter vnode since %s", vgId, tstrerror(code)); + } + + dnodeReleaseVnode(pVnode); + return code; +} + +static int32_t vnodeProcessDropVnodeReq(SRpcMsg *rpcMsg) { + SDropVnodeMsg *pDrop = vnodeParseDropVnodeReq(rpcMsg); + + int32_t code = 0; + int32_t vgId = pDrop->vgId; + dDebug("vgId:%d, drop vnode req is received", vgId); + + SVnodeObj *pVnode = dnodeAcquireVnode(vgId); + if (pVnode == NULL) { + code = terrno; + dDebug("vgId:%d, failed to drop since %s", vgId, tstrerror(code)); + return code; + } + + code = vnodeDrop(pVnode->pImpl); + if (code != 0) { + dError("vgId:%d, failed to drop vnode since %s", vgId, tstrerror(code)); + } + + dnodeReleaseVnode(pVnode); + return code; +} + +static int32_t vnodeProcessAuthVnodeReq(SRpcMsg *rpcMsg) { + SAuthVnodeMsg *pAuth = (SAuthVnodeMsg *)vnodeParseAuthVnodeReq(rpcMsg); + + int32_t code = 0; + int32_t vgId = pAuth->vgId; + dDebug("vgId:%d, auth vnode req is received", vgId); + + SVnodeObj *pVnode = dnodeAcquireVnode(vgId); + if (pVnode == NULL) { + code = terrno; + dDebug("vgId:%d, failed to auth since %s", vgId, tstrerror(code)); + return code; + } + + pVnode->accessState = pAuth->accessState; + dnodeReleaseVnode(pVnode); + return code; +} + +static int32_t vnodeProcessSyncVnodeReq(SRpcMsg *rpcMsg) { + SAuthVnodeMsg *pAuth = (SAuthVnodeMsg *)vnodeParseAuthVnodeReq(rpcMsg); + + int32_t code = 0; + int32_t vgId = pAuth->vgId; + dDebug("vgId:%d, auth vnode req is received", vgId); + + SVnodeObj *pVnode = dnodeAcquireVnode(vgId); + if (pVnode == NULL) { + code = terrno; + dDebug("vgId:%d, failed to auth since %s", vgId, tstrerror(code)); + return code; + } + + code = vnodeSync(pVnode->pImpl); + if (code != 0) { + dError("vgId:%d, failed to auth vnode since %s", vgId, tstrerror(code)); + } + + dnodeReleaseVnode(pVnode); + return code; +} + +static int32_t vnodeProcessCompactVnodeReq(SRpcMsg *rpcMsg) { + SCompactVnodeMsg *pCompact = (SCompactVnodeMsg *)vnodeParseDropVnodeReq(rpcMsg); + + int32_t code = 0; + int32_t vgId = pCompact->vgId; + dDebug("vgId:%d, compact vnode req is received", vgId); + + SVnodeObj *pVnode = dnodeAcquireVnode(vgId); + if (pVnode == NULL) { + code = terrno; + dDebug("vgId:%d, failed to compact since %s", vgId, tstrerror(code)); + return code; + } + + code = vnodeCompact(pVnode->pImpl); + if (code != 0) { + dError("vgId:%d, failed to compact vnode since %s", vgId, tstrerror(code)); + } + + dnodeReleaseVnode(pVnode); + return code; +} + +static void dnodeProcessVnodeMgmtReq(SRpcMsg *pMsg, void *unused) { + int32_t code = 0; + + switch (pMsg->msgType) { + case TSDB_MSG_TYPE_CREATE_VNODE_IN: + code = vnodeProcessCreateVnodeReq(pMsg); + break; + case TSDB_MSG_TYPE_ALTER_VNODE_IN: + code = vnodeProcessAlterVnodeReq(pMsg); + break; + case TSDB_MSG_TYPE_DROP_VNODE_IN: + code = vnodeProcessDropVnodeReq(pMsg); + break; + case TSDB_MSG_TYPE_AUTH_VNODE_IN: + code = vnodeProcessAuthVnodeReq(pMsg); + break; + case TSDB_MSG_TYPE_SYNC_VNODE_IN: + code = vnodeProcessSyncVnodeReq(pMsg); + break; + case TSDB_MSG_TYPE_COMPACT_VNODE_IN: + code = vnodeProcessCompactVnodeReq(pMsg); + break; + default: + code = TSDB_CODE_DND_MSG_NOT_PROCESSED; + break; + } + + SRpcMsg rsp = {.code = code, .handle = pMsg->handle}; + rpcSendResponse(&rsp); + rpcFreeCont(pMsg->pCont); + taosFreeQitem(pMsg); +} + +static int32_t dnodeWriteToVnodeQueue(taos_queue pQueue, SRpcMsg *pRpcMsg) { + int32_t code = 0; + + if (pQueue == NULL) { + code = TSDB_CODE_DND_MSG_NOT_PROCESSED; + } else { + SRpcMsg *pMsg = taosAllocateQitem(sizeof(SRpcMsg)); + if (pMsg == NULL) { + code = TSDB_CODE_DND_OUT_OF_MEMORY; + } else { + *pMsg = *pRpcMsg; + code = taosWriteQitem(pQueue, pMsg); + } + } + + if (code != TSDB_CODE_SUCCESS) { + SRpcMsg rsp = {.handle = pRpcMsg->handle, .code = code}; + rpcSendResponse(&rsp); + rpcFreeCont(pRpcMsg->pCont); + } +} + +static SVnodeObj *dnodeAcquireVnodeFromMsg(SRpcMsg *pMsg) { + SMsgHead *pHead = (SMsgHead *)pMsg->pCont; + pHead->vgId = htonl(pHead->vgId); + + SVnodeObj *pVnode = dnodeAcquireVnode(pHead->vgId); + if (pVnode == NULL) { + SRpcMsg rsp = {.handle = pMsg->handle, .code = terrno}; + rpcSendResponse(&rsp); + rpcFreeCont(pMsg->pCont); + } + + return pVnode; +} + +void dnodeProcessVnodeMgmtMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { dnodeWriteToVnodeQueue(tsVnodes.pMgmtQ, pMsg); } + +void dnodeProcessVnodeWriteMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { + SVnodeObj *pVnode = dnodeAcquireVnodeFromMsg(pMsg); + if (pVnode != NULL) { + dnodeWriteToVnodeQueue(pVnode->pWriteQ, pMsg); + dnodeReleaseVnode(pVnode); + } +} + +void dnodeProcessVnodeSyncMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { + SVnodeObj *pVnode = dnodeAcquireVnodeFromMsg(pMsg); + if (pVnode != NULL) { + dnodeWriteToVnodeQueue(pVnode->pSyncQ, pMsg); + dnodeReleaseVnode(pVnode); + } +} + +void dnodeProcessVnodeQueryMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { + SVnodeObj *pVnode = dnodeAcquireVnodeFromMsg(pMsg); + if (pVnode != NULL) { + dnodeWriteToVnodeQueue(pVnode->pQueryQ, pMsg); + dnodeReleaseVnode(pVnode); + } +} + +void dnodeProcessVnodeFetchMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { + SVnodeObj *pVnode = dnodeAcquireVnodeFromMsg(pMsg); + if (pVnode != NULL) { + dnodeWriteToVnodeQueue(pVnode->pFetchQ, pMsg); + dnodeReleaseVnode(pVnode); + } +} + +static int32_t dnodeInitVnodeMgmtWorker() { + SWorkerPool *pPool = &tsVnodes.mgmtPool; + pPool->name = "vnode-mgmt"; + pPool->min = 1; + pPool->max = 1; + if (tWorkerInit(pPool) != 0) { + return TSDB_CODE_VND_OUT_OF_MEMORY; + } + + tsVnodes.pMgmtQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)dnodeProcessVnodeMgmtReq); + if (tsVnodes.pMgmtQ == NULL) { + return TSDB_CODE_VND_OUT_OF_MEMORY; + } + + return 0; +} + +static void dnodeCleanupVnodeMgmtWorker() { + tWorkerFreeQueue(&tsVnodes.mgmtPool, tsVnodes.pMgmtQ); + tWorkerCleanup(&tsVnodes.mgmtPool); + tsVnodes.pMgmtQ = NULL; +} + +int32_t dnodeInitVnodes() { + dInfo("dnode-vnodes start to init"); + + SSteps *pSteps = taosStepInit(3, dnodeReportStartup); + taosStepAdd(pSteps, "dnode-vnode-env", vnodeInit, vnodeCleanup); + taosStepAdd(pSteps, "dnode-vnode-mgmt", dnodeInitVnodeMgmtWorker, dnodeCleanupVnodeMgmtWorker); + taosStepAdd(pSteps, "dnode-vnodes", dnodeOpenVnodes, dnodeCleanupVnodes); + + tsVnodes.pSteps = pSteps; + return taosStepExec(pSteps); +} + +void dnodeCleanupVnodes() { + if (tsVnodes.pSteps != NULL) { + dInfo("dnode-vnodes start to clean up"); + taosStepCleanup(tsVnodes.pSteps); + tsVnodes.pSteps = NULL; + dInfo("dnode-vnodes is cleaned up"); + } +} + +void dnodeGetVnodes(SVnodeLoads *pLoads) { + pLoads->vnodeNum = taosHashGetSize(tsVnodes.hash); + + int32_t v = 0; + void *pIter = taosHashIterate(tsVnodes.hash, NULL); + while (pIter) { + SVnodeObj **ppVnode = pIter; + if (ppVnode == NULL) continue; + SVnodeObj *pVnode = *ppVnode; + if (pVnode) { + SVnodeLoad *pLoad = &pLoads->vnodeLoads[v++]; + vnodeGetLoad(pVnode->pImpl, pLoad); + pLoad->vgId = htonl(pLoad->vgId); + pLoad->totalStorage = htobe64(pLoad->totalStorage); + pLoad->compStorage = htobe64(pLoad->compStorage); + pLoad->pointsWritten = htobe64(pLoad->pointsWritten); + pLoad->tablesNum = htobe64(pLoad->tablesNum); + } + pIter = taosHashIterate(tsVnodes.hash, pIter); + } +} \ No newline at end of file diff --git a/source/dnode/vnode/impl/src/vnodeInt.c b/source/dnode/vnode/impl/src/vnodeInt.c index 6f83542cef..1df3d37749 100644 --- a/source/dnode/vnode/impl/src/vnodeInt.c +++ b/source/dnode/vnode/impl/src/vnodeInt.c @@ -19,9 +19,6 @@ int32_t vnodeInit() { return 0; } void vnodeCleanup() {} -int32_t vnodeGetStatistics(SVnode *pVnode, SVnodeStatisic *pStat) { return 0; } -int32_t vnodeGetStatus(SVnode *pVnode, SVnodeStatus *pStatus) { return 0; } - SVnode *vnodeOpen(int32_t vgId, const char *path) { return NULL; } void vnodeClose(SVnode *pVnode) {} int32_t vnodeAlter(SVnode *pVnode, const SVnodeCfg *pCfg) { return 0; } @@ -31,3 +28,4 @@ int32_t vnodeCompact(SVnode *pVnode) { return 0; } int32_t vnodeSync(SVnode *pVnode) { return 0; } void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg) {} +void vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {} From 2f09d49aa4c32f01398448fed3d06e0030099223 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 4 Nov 2021 14:06:29 +0800 Subject: [PATCH 28/33] refact dnode - vnodes --- include/common/taosmsg.h | 4 +- include/server/vnode/vnode.h | 22 +- include/util/tqueue.h | 4 +- source/dnode/mgmt/inc/dnodeVnodes.h | 2 +- source/dnode/mgmt/src/dnodeDnode.c | 4 +- source/dnode/mgmt/src/dnodeVnodes.c | 299 ++++++++++++++++++++++--- source/dnode/vnode/impl/src/vnodeInt.c | 43 +++- source/util/src/tworker.c | 4 +- 8 files changed, 329 insertions(+), 53 deletions(-) diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index 49e8892c7d..822f850018 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -655,8 +655,8 @@ typedef struct { } SVnodeLoad; typedef struct { - int32_t vnodeNum; - SVnodeLoad vnodeLoads[]; + int32_t num; + SVnodeLoad data[]; } SVnodeLoads; typedef struct SStatusMsg { diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index a20a7fd410..ca0706f0b9 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -47,12 +47,18 @@ typedef struct { SVnodeDesc replicas[TSDB_MAX_REPLICA]; } SVnodeCfg; +typedef enum { + VN_MSG_TYPE_WRITE = 1, + VN_MSG_TYPE_APPLY, + VN_MSG_TYPE_SYNC, + VN_MSG_TYPE_QUERY, + VN_MSG_TYPE_FETCH +} EVMType; + typedef struct SVnodeMsg { - int32_t msgType; - int32_t code; - SRpcMsg rpcMsg; // original message from rpc - int32_t contLen; - char pCont[]; + int32_t curNum; + int32_t allocNum; + SRpcMsg rpcMsg[]; } SVnodeMsg; int32_t vnodeInit(); @@ -67,7 +73,11 @@ int32_t vnodeCompact(SVnode *pVnode); int32_t vnodeSync(SVnode *pVnode); void vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad); -void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg); + +SVnodeMsg *vnodeInitMsg(int32_t msgNum); +int32_t vnodeAppendMsg(SVnodeMsg *pMsg, SRpcMsg *pRpcMsg); +void vnodeCleanupMsg(SVnodeMsg *pMsg); +void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg, EVMType msgType); #ifdef __cplusplus } diff --git a/include/util/tqueue.h b/include/util/tqueue.h index faac1afe70..24c56ea6a3 100644 --- a/include/util/tqueue.h +++ b/include/util/tqueue.h @@ -40,8 +40,8 @@ shall be used to set up the protection. typedef void *taos_queue; typedef void *taos_qset; typedef void *taos_qall; -typedef void *(*FProcessItem)(void *pItem, void *ahandle); -typedef void *(*FProcessItems)(taos_qall qall, int numOfItems, void *ahandle); +typedef void (*FProcessItem)(void *ahandle, void *pItem); +typedef void (*FProcessItems)(void *ahandle, taos_qall qall, int numOfItems); taos_queue taosOpenQueue(); void taosCloseQueue(taos_queue); diff --git a/source/dnode/mgmt/inc/dnodeVnodes.h b/source/dnode/mgmt/inc/dnodeVnodes.h index 2b72ba5d59..31eae049ab 100644 --- a/source/dnode/mgmt/inc/dnodeVnodes.h +++ b/source/dnode/mgmt/inc/dnodeVnodes.h @@ -23,7 +23,7 @@ extern "C" { int32_t dnodeInitVnodes(); void dnodeCleanupVnodes(); -void dnodeGetVnodes(SVnodeLoads *pVloads); +void dnodeGetVnodeLoads(SVnodeLoads *pVloads); void dnodeProcessVnodeMgmtMsg(SRpcMsg *pMsg, SEpSet *pEpSet); void dnodeProcessVnodeWriteMsg(SRpcMsg *pMsg, SEpSet *pEpSet); diff --git a/source/dnode/mgmt/src/dnodeDnode.c b/source/dnode/mgmt/src/dnodeDnode.c index 5bf5b1d56a..63de2b940d 100644 --- a/source/dnode/mgmt/src/dnodeDnode.c +++ b/source/dnode/mgmt/src/dnodeDnode.c @@ -372,8 +372,8 @@ static void dnodeSendStatusMsg() { char timestr[32] = "1970-01-01 00:00:00.00"; (void)taosParseTime(timestr, &pStatus->clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0); - dnodeGetVnodes(&pStatus->vnodeLoads); - contLen = sizeof(SStatusMsg) + pStatus->vnodeLoads.vnodeNum * sizeof(SVnodeLoad); + dnodeGetVnodeLoads(&pStatus->vnodeLoads); + contLen = sizeof(SStatusMsg) + pStatus->vnodeLoads.num * sizeof(SVnodeLoad); SRpcMsg rpcMsg = {.pCont = pStatus, .contLen = contLen, .msgType = TSDB_MSG_TYPE_STATUS}; dnodeSendMsgToMnode(&rpcMsg); diff --git a/source/dnode/mgmt/src/dnodeVnodes.c b/source/dnode/mgmt/src/dnodeVnodes.c index c2e143da80..72048a15a5 100644 --- a/source/dnode/mgmt/src/dnodeVnodes.c +++ b/source/dnode/mgmt/src/dnodeVnodes.c @@ -45,27 +45,66 @@ typedef struct { } SVThread; static struct { - SHashObj *hash; - SWorkerPool mgmtPool; - taos_queue pMgmtQ; - SSteps *pSteps; - int32_t openVnodes; - int32_t totalVnodes; - char file[PATH_MAX + 20]; + SHashObj *hash; + SWorkerPool mgmtPool; + SWorkerPool queryPool; + SWorkerPool fetchPool; + SMWorkerPool syncPool; + SMWorkerPool writePool; + taos_queue pMgmtQ; + SSteps *pSteps; + int32_t openVnodes; + int32_t totalVnodes; + char file[PATH_MAX + 20]; } tsVnodes; +static int32_t dnodeAllocVnodeQueryQueue(SVnodeObj *pVnode); +static void dnodeFreeVnodeQueryQueue(SVnodeObj *pVnode); +static int32_t dnodeAllocVnodeFetchQueue(SVnodeObj *pVnode); +static void dnodeFreeVnodeFetchQueue(SVnodeObj *pVnode); +static int32_t dnodeAllocVnodeWriteQueue(SVnodeObj *pVnode); +static void dnodeFreeVnodeWriteQueue(SVnodeObj *pVnode); +static int32_t dnodeAllocVnodeApplyQueue(SVnodeObj *pVnode); +static void dnodeFreeVnodeApplyQueue(SVnodeObj *pVnode); +static int32_t dnodeAllocVnodeSyncQueue(SVnodeObj *pVnode); +static void dnodeFreeVnodeSyncQueue(SVnodeObj *pVnode); + static int32_t dnodeCreateVnodeWrapper(int32_t vgId, SVnode *pImpl) { SVnodeObj *pVnode = calloc(1, sizeof(SVnodeObj)); + if (pVnode == NULL) { + return TSDB_CODE_DND_OUT_OF_MEMORY; + } + pVnode->vgId = vgId; pVnode->refCount = 0; pVnode->dropped = 0; pVnode->accessState = TSDB_VN_ALL_ACCCESS; pVnode->pImpl = pImpl; - pVnode->pWriteQ = NULL; - pVnode->pSyncQ = NULL; - pVnode->pApplyQ = NULL; - pVnode->pQueryQ = NULL; - pVnode->pFetchQ = NULL; + + int32_t code = dnodeAllocVnodeQueryQueue(pVnode); + if (code != 0) { + return code; + } + + code = dnodeAllocVnodeFetchQueue(pVnode); + if (code != 0) { + return code; + } + + code = dnodeAllocVnodeWriteQueue(pVnode); + if (code != 0) { + return code; + } + + code = dnodeAllocVnodeApplyQueue(pVnode); + if (code != 0) { + return code; + } + + code = dnodeAllocVnodeSyncQueue(pVnode); + if (code != 0) { + return code; + } return taosHashPut(tsVnodes.hash, &vgId, sizeof(int32_t), &pVnode, sizeof(SVnodeObj *)); } @@ -74,11 +113,11 @@ static void dnodeDropVnodeWrapper(SVnodeObj *pVnode) { taosHashRemove(tsVnodes.hash, &pVnode->vgId, sizeof(int32_t)); //todo wait all queue empty - pVnode->pWriteQ = NULL; - pVnode->pSyncQ = NULL; - pVnode->pApplyQ = NULL; - pVnode->pQueryQ = NULL; - pVnode->pFetchQ = NULL; + dnodeFreeVnodeQueryQueue(pVnode); + dnodeFreeVnodeFetchQueue(pVnode); + dnodeFreeVnodeWriteQueue(pVnode); + dnodeFreeVnodeApplyQueue(pVnode); + dnodeFreeVnodeSyncQueue(pVnode); } static int32_t dnodeGetVnodesFromHash(SVnodeObj *pVnodes[], int32_t *numOfVnodes) { @@ -465,7 +504,7 @@ static int32_t vnodeProcessCompactVnodeReq(SRpcMsg *rpcMsg) { return code; } -static void dnodeProcessVnodeMgmtReq(SRpcMsg *pMsg, void *unused) { +static void dnodeProcessVnodeMgmtQueue(void *unused, SRpcMsg *pMsg) { int32_t code = 0; switch (pMsg->msgType) { @@ -498,7 +537,44 @@ static void dnodeProcessVnodeMgmtReq(SRpcMsg *pMsg, void *unused) { taosFreeQitem(pMsg); } -static int32_t dnodeWriteToVnodeQueue(taos_queue pQueue, SRpcMsg *pRpcMsg) { +static void dnodeProcessVnodeQueryQueue(SVnodeObj *pVnode, SVnodeMsg *pMsg) { + vnodeProcessMsg(pVnode->pImpl, pMsg, VN_MSG_TYPE_QUERY); +} + +static void dnodeProcessVnodeFetchQueue(SVnodeObj *pVnode, SVnodeMsg *pMsg) { + vnodeProcessMsg(pVnode->pImpl, pMsg, VN_MSG_TYPE_FETCH); +} + +static void dnodeProcessVnodeWriteQueue(SVnodeObj *pVnode, taos_qall qall, int32_t numOfMsgs) { + SVnodeMsg *pMsg = vnodeInitMsg(numOfMsgs); + SRpcMsg *pRpcMsg = NULL; + + for (int32_t i = 0; i < numOfMsgs; ++i) { + taosGetQitem(qall, (void **)&pRpcMsg); + vnodeAppendMsg(pMsg, pRpcMsg); + taosFreeQitem(pRpcMsg); + } + + vnodeProcessMsg(pVnode->pImpl, pMsg, VN_MSG_TYPE_WRITE); +} + +static void dnodeProcessVnodeApplyQueue(SVnodeObj *pVnode, taos_qall qall, int32_t numOfMsgs) { + SVnodeMsg *pMsg = NULL; + for (int32_t i = 0; i < numOfMsgs; ++i) { + taosGetQitem(qall, (void **)&pMsg); + vnodeProcessMsg(pVnode->pImpl, pMsg, VN_MSG_TYPE_APPLY); + } +} + +static void dnodeProcessVnodeSyncQueue(SVnodeObj *pVnode, taos_qall qall, int32_t numOfMsgs) { + SVnodeMsg *pMsg = NULL; + for (int32_t i = 0; i < numOfMsgs; ++i) { + taosGetQitem(qall, (void **)&pMsg); + vnodeProcessMsg(pVnode->pImpl, pMsg, VN_MSG_TYPE_SYNC); + } +} + +static int32_t dnodeWriteRpcMsgToVnodeQueue(taos_queue pQueue, SRpcMsg *pRpcMsg) { int32_t code = 0; if (pQueue == NULL) { @@ -520,6 +596,28 @@ static int32_t dnodeWriteToVnodeQueue(taos_queue pQueue, SRpcMsg *pRpcMsg) { } } +static int32_t dnodeWriteVnodeMsgToVnodeQueue(taos_queue pQueue, SRpcMsg *pRpcMsg) { + int32_t code = 0; + + if (pQueue == NULL) { + code = TSDB_CODE_DND_MSG_NOT_PROCESSED; + } else { + SVnodeMsg *pMsg = vnodeInitMsg(1); + if (pMsg == NULL) { + code = TSDB_CODE_DND_OUT_OF_MEMORY; + } else { + vnodeAppendMsg(pMsg, pRpcMsg); + code = taosWriteQitem(pQueue, pMsg); + } + } + + if (code != TSDB_CODE_SUCCESS) { + SRpcMsg rsp = {.handle = pRpcMsg->handle, .code = code}; + rpcSendResponse(&rsp); + rpcFreeCont(pRpcMsg->pCont); + } +} + static SVnodeObj *dnodeAcquireVnodeFromMsg(SRpcMsg *pMsg) { SMsgHead *pHead = (SMsgHead *)pMsg->pCont; pHead->vgId = htonl(pHead->vgId); @@ -534,12 +632,12 @@ static SVnodeObj *dnodeAcquireVnodeFromMsg(SRpcMsg *pMsg) { return pVnode; } -void dnodeProcessVnodeMgmtMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { dnodeWriteToVnodeQueue(tsVnodes.pMgmtQ, pMsg); } +void dnodeProcessVnodeMgmtMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { dnodeWriteRpcMsgToVnodeQueue(tsVnodes.pMgmtQ, pMsg); } void dnodeProcessVnodeWriteMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { SVnodeObj *pVnode = dnodeAcquireVnodeFromMsg(pMsg); if (pVnode != NULL) { - dnodeWriteToVnodeQueue(pVnode->pWriteQ, pMsg); + dnodeWriteRpcMsgToVnodeQueue(pVnode->pWriteQ, pMsg); dnodeReleaseVnode(pVnode); } } @@ -547,7 +645,7 @@ void dnodeProcessVnodeWriteMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { void dnodeProcessVnodeSyncMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { SVnodeObj *pVnode = dnodeAcquireVnodeFromMsg(pMsg); if (pVnode != NULL) { - dnodeWriteToVnodeQueue(pVnode->pSyncQ, pMsg); + dnodeWriteVnodeMsgToVnodeQueue(pVnode->pSyncQ, pMsg); dnodeReleaseVnode(pVnode); } } @@ -555,7 +653,7 @@ void dnodeProcessVnodeSyncMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { void dnodeProcessVnodeQueryMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { SVnodeObj *pVnode = dnodeAcquireVnodeFromMsg(pMsg); if (pVnode != NULL) { - dnodeWriteToVnodeQueue(pVnode->pQueryQ, pMsg); + dnodeWriteVnodeMsgToVnodeQueue(pVnode->pQueryQ, pMsg); dnodeReleaseVnode(pVnode); } } @@ -563,7 +661,7 @@ void dnodeProcessVnodeQueryMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { void dnodeProcessVnodeFetchMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { SVnodeObj *pVnode = dnodeAcquireVnodeFromMsg(pMsg); if (pVnode != NULL) { - dnodeWriteToVnodeQueue(pVnode->pFetchQ, pMsg); + dnodeWriteVnodeMsgToVnodeQueue(pVnode->pFetchQ, pMsg); dnodeReleaseVnode(pVnode); } } @@ -577,7 +675,7 @@ static int32_t dnodeInitVnodeMgmtWorker() { return TSDB_CODE_VND_OUT_OF_MEMORY; } - tsVnodes.pMgmtQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)dnodeProcessVnodeMgmtReq); + tsVnodes.pMgmtQ = tWorkerAllocQueue(pPool, NULL, (FProcessItem)dnodeProcessVnodeMgmtQueue); if (tsVnodes.pMgmtQ == NULL) { return TSDB_CODE_VND_OUT_OF_MEMORY; } @@ -591,12 +689,137 @@ static void dnodeCleanupVnodeMgmtWorker() { tsVnodes.pMgmtQ = NULL; } +static int32_t dnodeAllocVnodeQueryQueue(SVnodeObj *pVnode) { + pVnode->pQueryQ = tWorkerAllocQueue(&tsVnodes.queryPool, pVnode, (FProcessItem)dnodeProcessVnodeQueryQueue); + if (pVnode->pQueryQ == NULL) { + return TSDB_CODE_DND_OUT_OF_MEMORY; + } + return 0; +} + +static void dnodeFreeVnodeQueryQueue(SVnodeObj *pVnode) { + tWorkerFreeQueue(&tsVnodes.queryPool, pVnode->pQueryQ); + pVnode->pQueryQ = NULL; +} + +static int32_t dnodeAllocVnodeFetchQueue(SVnodeObj *pVnode) { + pVnode->pFetchQ = tWorkerAllocQueue(&tsVnodes.fetchPool, pVnode, (FProcessItem)dnodeProcessVnodeFetchQueue); + if (pVnode->pFetchQ == NULL) { + return TSDB_CODE_DND_OUT_OF_MEMORY; + } + return 0; +} + +static void dnodeFreeVnodeFetchQueue(SVnodeObj *pVnode) { + tWorkerFreeQueue(&tsVnodes.fetchPool, pVnode->pFetchQ); + pVnode->pFetchQ = NULL; +} + +static int32_t dnodeInitVnodeReadWorker() { + int32_t maxFetchThreads = 4; + float threadsForQuery = MAX(tsNumOfCores * tsRatioOfQueryCores, 1); + + SWorkerPool *pPool = &tsVnodes.queryPool; + pPool->name = "vnode-query"; + pPool->min = (int32_t)threadsForQuery; + pPool->max = pPool->min; + if (tWorkerInit(pPool) != 0) { + return TSDB_CODE_VND_OUT_OF_MEMORY; + } + + pPool = &tsVnodes.fetchPool; + pPool->name = "vnode-fetch"; + pPool->min = MIN(maxFetchThreads, tsNumOfCores); + pPool->max = pPool->min; + if (tWorkerInit(pPool) != 0) { + TSDB_CODE_VND_OUT_OF_MEMORY; + } + + return 0; +} + +static void dnodeCleanupVnodeReadWorker() { + tWorkerCleanup(&tsVnodes.fetchPool); + tWorkerCleanup(&tsVnodes.queryPool); +} + +static int32_t dnodeAllocVnodeWriteQueue(SVnodeObj *pVnode) { + pVnode->pWriteQ = tMWorkerAllocQueue(&tsVnodes.writePool, pVnode, (FProcessItems)dnodeProcessVnodeWriteQueue); + if (pVnode->pWriteQ == NULL) { + return TSDB_CODE_DND_OUT_OF_MEMORY; + } + return 0; +} + +static void dnodeFreeVnodeWriteQueue(SVnodeObj *pVnode) { + tMWorkerFreeQueue(&tsVnodes.writePool, pVnode->pWriteQ); + pVnode->pWriteQ = NULL; +} + +static int32_t dnodeAllocVnodeApplyQueue(SVnodeObj *pVnode) { + pVnode->pApplyQ = tMWorkerAllocQueue(&tsVnodes.writePool, pVnode, (FProcessItems)dnodeProcessVnodeApplyQueue); + if (pVnode->pApplyQ == NULL) { + return TSDB_CODE_DND_OUT_OF_MEMORY; + } + return 0; +} + +static void dnodeFreeVnodeApplyQueue(SVnodeObj *pVnode) { + tMWorkerFreeQueue(&tsVnodes.writePool, pVnode->pApplyQ); + pVnode->pApplyQ = NULL; +} + +static int32_t dnodeInitVnodeWriteWorker() { + SMWorkerPool *pPool = &tsVnodes.writePool; + pPool->name = "vnode-write"; + pPool->max = tsNumOfCores; + if (tMWorkerInit(pPool) != 0) { + return TSDB_CODE_VND_OUT_OF_MEMORY; + } + + return 0; +} + +static void dnodeCleanupVnodeWriteWorker() { tMWorkerCleanup(&tsVnodes.writePool); } + +static int32_t dnodeAllocVnodeSyncQueue(SVnodeObj *pVnode) { + pVnode->pSyncQ = tMWorkerAllocQueue(&tsVnodes.writePool, pVnode, (FProcessItems)dnodeProcessVnodeSyncQueue); + if (pVnode->pSyncQ == NULL) { + return TSDB_CODE_DND_OUT_OF_MEMORY; + } + return 0; +} + +static void dnodeFreeVnodeSyncQueue(SVnodeObj *pVnode) { + tMWorkerFreeQueue(&tsVnodes.writePool, pVnode->pSyncQ); + pVnode->pSyncQ = NULL; +} + +static int32_t dnodeInitVnodeSyncWorker() { + int32_t maxThreads = tsNumOfCores / 2; + if (maxThreads < 1) maxThreads = 1; + + SMWorkerPool *pPool = &tsVnodes.writePool; + pPool->name = "vnode-sync"; + pPool->max = maxThreads; + if (tMWorkerInit(pPool) != 0) { + return TSDB_CODE_VND_OUT_OF_MEMORY; + } + + return 0; +} + +static void dnodeCleanupVnodeSyncWorker() { tMWorkerCleanup(&tsVnodes.syncPool); } + int32_t dnodeInitVnodes() { dInfo("dnode-vnodes start to init"); SSteps *pSteps = taosStepInit(3, dnodeReportStartup); taosStepAdd(pSteps, "dnode-vnode-env", vnodeInit, vnodeCleanup); taosStepAdd(pSteps, "dnode-vnode-mgmt", dnodeInitVnodeMgmtWorker, dnodeCleanupVnodeMgmtWorker); + taosStepAdd(pSteps, "dnode-vnode-read", dnodeInitVnodeReadWorker, dnodeCleanupVnodeReadWorker); + taosStepAdd(pSteps, "dnode-vnode-write", dnodeInitVnodeWriteWorker, dnodeCleanupVnodeWriteWorker); + taosStepAdd(pSteps, "dnode-vnode-sync", dnodeInitVnodeSyncWorker, dnodeCleanupVnodeSyncWorker); taosStepAdd(pSteps, "dnode-vnodes", dnodeOpenVnodes, dnodeCleanupVnodes); tsVnodes.pSteps = pSteps; @@ -612,24 +835,26 @@ void dnodeCleanupVnodes() { } } -void dnodeGetVnodes(SVnodeLoads *pLoads) { - pLoads->vnodeNum = taosHashGetSize(tsVnodes.hash); +void dnodeGetVnodeLoads(SVnodeLoads *pLoads) { + pLoads->num = taosHashGetSize(tsVnodes.hash); int32_t v = 0; void *pIter = taosHashIterate(tsVnodes.hash, NULL); while (pIter) { SVnodeObj **ppVnode = pIter; if (ppVnode == NULL) continue; + SVnodeObj *pVnode = *ppVnode; - if (pVnode) { - SVnodeLoad *pLoad = &pLoads->vnodeLoads[v++]; - vnodeGetLoad(pVnode->pImpl, pLoad); - pLoad->vgId = htonl(pLoad->vgId); - pLoad->totalStorage = htobe64(pLoad->totalStorage); - pLoad->compStorage = htobe64(pLoad->compStorage); - pLoad->pointsWritten = htobe64(pLoad->pointsWritten); - pLoad->tablesNum = htobe64(pLoad->tablesNum); - } + if (pVnode == NULL) continue; + + SVnodeLoad *pLoad = &pLoads->data[v++]; + vnodeGetLoad(pVnode->pImpl, pLoad); + pLoad->vgId = htonl(pLoad->vgId); + pLoad->totalStorage = htobe64(pLoad->totalStorage); + pLoad->compStorage = htobe64(pLoad->compStorage); + pLoad->pointsWritten = htobe64(pLoad->pointsWritten); + pLoad->tablesNum = htobe64(pLoad->tablesNum); + pIter = taosHashIterate(tsVnodes.hash, pIter); } -} \ No newline at end of file +} diff --git a/source/dnode/vnode/impl/src/vnodeInt.c b/source/dnode/vnode/impl/src/vnodeInt.c index 1df3d37749..c345f2e1b9 100644 --- a/source/dnode/vnode/impl/src/vnodeInt.c +++ b/source/dnode/vnode/impl/src/vnodeInt.c @@ -15,6 +15,7 @@ #define _DEFAULT_SOURCE #include "vnodeInt.h" +#include "tqueue.h" int32_t vnodeInit() { return 0; } void vnodeCleanup() {} @@ -27,5 +28,45 @@ int32_t vnodeDrop(SVnode *pVnode) { return 0; } int32_t vnodeCompact(SVnode *pVnode) { return 0; } int32_t vnodeSync(SVnode *pVnode) { return 0; } -void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg) {} void vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {} + +SVnodeMsg *vnodeInitMsg(int32_t msgNum) { + SVnodeMsg *pMsg = taosAllocateQitem(msgNum * sizeof(SRpcMsg *) + sizeof(SVnodeMsg)); + if (pMsg == NULL) { + terrno = TSDB_CODE_VND_OUT_OF_MEMORY; + return NULL; + } else { + pMsg->allocNum = msgNum; + return pMsg; + } +} + +int32_t vnodeAppendMsg(SVnodeMsg *pMsg, SRpcMsg *pRpcMsg) { + if (pMsg->curNum >= pMsg->allocNum) { + return TSDB_CODE_VND_OUT_OF_MEMORY; + } + + pMsg->rpcMsg[pMsg->curNum++] = *pRpcMsg; +} + +void vnodeCleanupMsg(SVnodeMsg *pMsg) { + for (int32_t i = 0; i < pMsg->curNum; ++i) { + rpcFreeCont(pMsg->rpcMsg[i].pCont); + } + taosFreeQitem(pMsg); +} + +void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg, EVMType msgType) { + switch (msgType) { + case VN_MSG_TYPE_WRITE: + break; + case VN_MSG_TYPE_APPLY: + break; + case VN_MSG_TYPE_SYNC: + break; + case VN_MSG_TYPE_QUERY: + break; + case VN_MSG_TYPE_FETCH: + break; + } +} diff --git a/source/util/src/tworker.c b/source/util/src/tworker.c index 7df12089b7..136bc40482 100644 --- a/source/util/src/tworker.c +++ b/source/util/src/tworker.c @@ -76,7 +76,7 @@ static void *tWorkerThreadFp(SWorker *worker) { } if (fp) { - (*fp)(msg, ahandle); + (*fp)(ahandle, msg); } } @@ -186,7 +186,7 @@ static void *tWriteWorkerThreadFp(SMWorker *worker) { } if (fp) { - (*fp)(worker->qall, numOfMsgs, ahandle); + (*fp)(ahandle, worker->qall, numOfMsgs); } } From 318c3b7cf78915363a2c4c9610eb7fb8cc4ccb90 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 4 Nov 2021 14:18:03 +0800 Subject: [PATCH 29/33] add apply callback --- include/server/vnode/vnode.h | 10 ++++++++-- source/dnode/mgmt/src/dnodeVnodes.c | 23 ++++++++++++++++++++++- source/dnode/vnode/impl/src/vnodeInt.c | 2 +- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index ca0706f0b9..6db0eb86f6 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -55,13 +55,19 @@ typedef enum { VN_MSG_TYPE_FETCH } EVMType; -typedef struct SVnodeMsg { +typedef struct { int32_t curNum; int32_t allocNum; SRpcMsg rpcMsg[]; } SVnodeMsg; -int32_t vnodeInit(); +typedef struct { + void (*SendMsgToDnode)(SEpSet *pEpSet, SRpcMsg *pMsg); + void (*SendMsgToMnode)(SRpcMsg *pMsg); + int32_t (*PutMsgIntoApplyQueue)(int32_t vgId, SVnodeMsg *pMsg); +} SVnodePara; + +int32_t vnodeInit(SVnodePara); void vnodeCleanup(); SVnode *vnodeOpen(int32_t vgId, const char *path); diff --git a/source/dnode/mgmt/src/dnodeVnodes.c b/source/dnode/mgmt/src/dnodeVnodes.c index 72048a15a5..46b79c84a0 100644 --- a/source/dnode/mgmt/src/dnodeVnodes.c +++ b/source/dnode/mgmt/src/dnodeVnodes.c @@ -15,6 +15,7 @@ #define _DEFAULT_SOURCE #include "dnodeVnodes.h" +#include "dnodeTransport.h" #include "thash.h" #include "tqueue.h" #include "tstep.h" @@ -666,6 +667,17 @@ void dnodeProcessVnodeFetchMsg(SRpcMsg *pMsg, SEpSet *pEpSet) { } } +static int32_t dnodePutMsgIntoVnodeApplyQueue(int32_t vgId, SVnodeMsg *pMsg) { + SVnodeObj *pVnode = dnodeAcquireVnode(vgId); + if (pVnode == NULL) { + return terrno; + } + + int32_t code = taosWriteQitem(pVnode->pApplyQ, pMsg); + dnodeReleaseVnode(pVnode); + return code; +} + static int32_t dnodeInitVnodeMgmtWorker() { SWorkerPool *pPool = &tsVnodes.mgmtPool; pPool->name = "vnode-mgmt"; @@ -811,11 +823,20 @@ static int32_t dnodeInitVnodeSyncWorker() { static void dnodeCleanupVnodeSyncWorker() { tMWorkerCleanup(&tsVnodes.syncPool); } +static int32_t dnodeInitVnodeModule() { + SVnodePara para; + para.SendMsgToDnode = dnodeSendMsgToDnode; + para.SendMsgToMnode = dnodeSendMsgToMnode; + para.PutMsgIntoApplyQueue = dnodePutMsgIntoVnodeApplyQueue; + + return vnodeInit(para); +} + int32_t dnodeInitVnodes() { dInfo("dnode-vnodes start to init"); SSteps *pSteps = taosStepInit(3, dnodeReportStartup); - taosStepAdd(pSteps, "dnode-vnode-env", vnodeInit, vnodeCleanup); + taosStepAdd(pSteps, "dnode-vnode-env", dnodeInitVnodeModule, vnodeCleanup); taosStepAdd(pSteps, "dnode-vnode-mgmt", dnodeInitVnodeMgmtWorker, dnodeCleanupVnodeMgmtWorker); taosStepAdd(pSteps, "dnode-vnode-read", dnodeInitVnodeReadWorker, dnodeCleanupVnodeReadWorker); taosStepAdd(pSteps, "dnode-vnode-write", dnodeInitVnodeWriteWorker, dnodeCleanupVnodeWriteWorker); diff --git a/source/dnode/vnode/impl/src/vnodeInt.c b/source/dnode/vnode/impl/src/vnodeInt.c index c345f2e1b9..e08cc47aa1 100644 --- a/source/dnode/vnode/impl/src/vnodeInt.c +++ b/source/dnode/vnode/impl/src/vnodeInt.c @@ -17,7 +17,7 @@ #include "vnodeInt.h" #include "tqueue.h" -int32_t vnodeInit() { return 0; } +int32_t vnodeInit(SVnodePara para) { return 0; } void vnodeCleanup() {} SVnode *vnodeOpen(int32_t vgId, const char *path) { return NULL; } From 97f16ecb65aaf32dbdb2121a000f9b5fb9540a87 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 4 Nov 2021 14:45:48 +0800 Subject: [PATCH 30/33] minor changes --- include/common/taosmsg.h | 10 +++++----- include/server/vnode/vnode.h | 2 +- include/util/tdef.h | 3 ++- source/common/src/tname.c | 2 +- source/dnode/mgmt/src/dnodeVnodes.c | 2 +- source/dnode/mnode/inc/mnodeDef.h | 6 +++--- source/libs/parser/src/parserUtil.c | 2 +- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index 822f850018..2ccab41f8f 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -325,7 +325,7 @@ typedef struct { typedef struct { char tableFname[TSDB_TABLE_FNAME_LEN]; - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + char db[TSDB_FULL_DB_NAME_LEN]; int16_t type; /* operation type */ int16_t numOfCols; /* number of schema */ int32_t tagValLen; @@ -570,7 +570,7 @@ typedef struct SRetrieveTableRsp { } SRetrieveTableRsp; typedef struct { - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + char db[TSDB_FULL_DB_NAME_LEN]; int32_t cacheBlockSize; //MB int32_t totalBlocks; int32_t maxTables; @@ -701,7 +701,7 @@ typedef struct { } SVnodeDesc; typedef struct { - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + char db[TSDB_FULL_DB_NAME_LEN]; uint32_t vgId; int32_t cacheBlockSize; int32_t totalBlocks; @@ -804,13 +804,13 @@ typedef struct { */ typedef struct { int8_t type; - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + char db[TSDB_FULL_DB_NAME_LEN]; uint16_t payloadLen; char payload[]; } SShowMsg; typedef struct { - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + char db[TSDB_FULL_DB_NAME_LEN]; int32_t numOfVgroup; int32_t vgid[]; } SCompactMsg; diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index 6db0eb86f6..16699b855a 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -27,7 +27,7 @@ extern "C" { typedef struct SVnode SVnode; typedef struct { - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + char db[TSDB_FULL_DB_NAME_LEN]; int32_t cacheBlockSize; // MB int32_t totalBlocks; int32_t daysPerFile; diff --git a/include/util/tdef.h b/include/util/tdef.h index 80cd3cf8b8..66e5f28bde 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -153,11 +153,12 @@ do { \ #define TSDB_NODE_NAME_LEN 64 #define TSDB_TABLE_NAME_LEN 193 // it is a null-terminated string #define TSDB_DB_NAME_LEN 33 +#define TSDB_FULL_DB_NAME_LEN (TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN) #define TSDB_FUNC_NAME_LEN 65 #define TSDB_FUNC_CODE_LEN (65535 - 512) #define TSDB_FUNC_BUF_SIZE 512 #define TSDB_TYPE_STR_MAX_LEN 32 -#define TSDB_TABLE_FNAME_LEN (TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN + TSDB_TABLE_NAME_LEN) +#define TSDB_TABLE_FNAME_LEN (TSDB_FULL_DB_NAME_LEN + TSDB_TABLE_NAME_LEN) #define TSDB_COL_NAME_LEN 65 #define TSDB_MAX_SAVED_SQL_LEN TSDB_MAX_COLUMNS * 64 #define TSDB_MAX_SQL_LEN TSDB_PAYLOAD_SIZE diff --git a/source/common/src/tname.c b/source/common/src/tname.c index c290a04ebc..28f920a6a9 100644 --- a/source/common/src/tname.c +++ b/source/common/src/tname.c @@ -201,7 +201,7 @@ int32_t tNameExtractFullName(const SName* name, char* dst) { return -1; } - int32_t len = snprintf(dst, TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN, "%s.%s", name->acctId, name->dbname); + int32_t len = snprintf(dst, TSDB_FULL_DB_NAME_LEN, "%s.%s", name->acctId, name->dbname); size_t tnameLen = strlen(name->tname); if (tnameLen > 0) { diff --git a/source/dnode/mgmt/src/dnodeVnodes.c b/source/dnode/mgmt/src/dnodeVnodes.c index 46b79c84a0..c7682539ef 100644 --- a/source/dnode/mgmt/src/dnodeVnodes.c +++ b/source/dnode/mgmt/src/dnodeVnodes.c @@ -333,7 +333,7 @@ static int32_t dnodeParseCreateVnodeReq(SRpcMsg *rpcMsg, int32_t *vgId, SVnodeCf SCreateVnodeMsg *pCreate = rpcMsg->pCont; *vgId = htonl(pCreate->vgId); - tstrncpy(pCfg->db, pCreate->db, TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN); + tstrncpy(pCfg->db, pCreate->db, TSDB_FULL_DB_NAME_LEN); pCfg->cacheBlockSize = htonl(pCreate->cacheBlockSize); pCfg->totalBlocks = htonl(pCreate->totalBlocks); pCfg->daysPerFile = htonl(pCreate->daysPerFile); diff --git a/source/dnode/mnode/inc/mnodeDef.h b/source/dnode/mnode/inc/mnodeDef.h index 33606e8ee2..0825815bc7 100644 --- a/source/dnode/mnode/inc/mnodeDef.h +++ b/source/dnode/mnode/inc/mnodeDef.h @@ -208,7 +208,7 @@ typedef struct { typedef struct SDbObj { SdbHead head; - char name[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + char name[TSDB_FULL_DB_NAME_LEN]; char acct[TSDB_USER_LEN]; int64_t createdTime; int64_t updateTime; @@ -236,7 +236,7 @@ typedef struct SVgObj { int64_t updateTime; int32_t lbDnodeId; int32_t lbTime; - char dbName[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + char dbName[TSDB_FULL_DB_NAME_LEN]; int8_t inUse; int8_t accessState; int8_t status; @@ -288,7 +288,7 @@ typedef struct { void *pIter; void *pVgIter; void **ppShow; - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN]; + char db[TSDB_FULL_DB_NAME_LEN]; int16_t offset[TSDB_MAX_COLUMNS]; int32_t bytes[TSDB_MAX_COLUMNS]; char payload[]; diff --git a/source/libs/parser/src/parserUtil.c b/source/libs/parser/src/parserUtil.c index 0bef796026..5aab21bdae 100644 --- a/source/libs/parser/src/parserUtil.c +++ b/source/libs/parser/src/parserUtil.c @@ -1934,7 +1934,7 @@ char* cloneCurrentDBName(SSqlObj* pSql) { case TAOS_REQ_FROM_HTTP: pCtx = pSql->param; if (pCtx && pCtx->db[0] != '\0') { - char db[TSDB_ACCT_ID_LEN + TSDB_DB_NAME_LEN] = {0}; + char db[TSDB_FULL_DB_NAME_LEN] = {0}; int32_t len = sprintf(db, "%s%s%s", pTscObj->acctId, TS_PATH_DELIMITER, pCtx->db); assert(len <= sizeof(db)); From a12a4f38c50549e29979c1e8a12cf6610eeeea51 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 4 Nov 2021 17:03:24 +0800 Subject: [PATCH 31/33] add vnode file --- include/util/taoserror.h | 1 + include/util/tdef.h | 2 +- include/util/tqueue.h | 1 + source/dnode/mgmt/src/dnodeVnodes.c | 252 ++++++++++++++++++++++------ source/util/src/terror.c | 1 + source/util/src/tqueue.c | 14 ++ 6 files changed, 218 insertions(+), 53 deletions(-) diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 0579ae46bc..e2f9a58023 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -223,6 +223,7 @@ int32_t* taosGetErrno(); #define TSDB_CODE_DND_ACTION_IN_PROGRESS TAOS_DEF_ERROR_CODE(0, 0x0406) //"Action in progress") #define TSDB_CODE_DND_TOO_MANY_VNODES TAOS_DEF_ERROR_CODE(0, 0x0407) //"Too many vnode directories") #define TSDB_CODE_DND_EXITING TAOS_DEF_ERROR_CODE(0, 0x0408) //"Dnode is exiting" +#define TSDB_CODE_DND_PARSE_VNODE_FILE_ERROR TAOS_DEF_ERROR_CODE(0, 0x0409) //"Parse vnodes.json error") // vnode #define TSDB_CODE_VND_ACTION_IN_PROGRESS TAOS_DEF_ERROR_CODE(0, 0x0500) //"Action in progress") diff --git a/include/util/tdef.h b/include/util/tdef.h index 66e5f28bde..0ad0f68f3f 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -212,7 +212,7 @@ do { \ #define TSDB_EXTRA_PAYLOAD_SIZE 128 // extra bytes for auth #define TSDB_CQ_SQL_SIZE 1024 #define TSDB_MIN_VNODES 64 -#define TSDB_MAX_VNODES 2048 +#define TSDB_MAX_VNODES 512 #define TSDB_MIN_VNODES_PER_DB 2 #define TSDB_MAX_VNODES_PER_DB 64 diff --git a/include/util/tqueue.h b/include/util/tqueue.h index 24c56ea6a3..bcb9aea856 100644 --- a/include/util/tqueue.h +++ b/include/util/tqueue.h @@ -50,6 +50,7 @@ void *taosAllocateQitem(int size); void taosFreeQitem(void *pItem); int taosWriteQitem(taos_queue, void *pItem); int taosReadQitem(taos_queue, void **pItem); +bool taosQueueEmpty(taos_queue); taos_qall taosAllocateQall(); void taosFreeQall(taos_qall); diff --git a/source/dnode/mgmt/src/dnodeVnodes.c b/source/dnode/mgmt/src/dnodeVnodes.c index c7682539ef..8bf80ccff8 100644 --- a/source/dnode/mgmt/src/dnodeVnodes.c +++ b/source/dnode/mgmt/src/dnodeVnodes.c @@ -16,7 +16,9 @@ #define _DEFAULT_SOURCE #include "dnodeVnodes.h" #include "dnodeTransport.h" +#include "cJSON.h" #include "thash.h" +#include "tlockfree.h" #include "tqueue.h" #include "tstep.h" #include "tthread.h" @@ -56,7 +58,7 @@ static struct { SSteps *pSteps; int32_t openVnodes; int32_t totalVnodes; - char file[PATH_MAX + 20]; + SRWLatch latch; } tsVnodes; static int32_t dnodeAllocVnodeQueryQueue(SVnodeObj *pVnode); @@ -70,6 +72,28 @@ static void dnodeFreeVnodeApplyQueue(SVnodeObj *pVnode); static int32_t dnodeAllocVnodeSyncQueue(SVnodeObj *pVnode); static void dnodeFreeVnodeSyncQueue(SVnodeObj *pVnode); +static SVnodeObj *dnodeAcquireVnode(int32_t vgId) { + SVnodeObj *pVnode = NULL; + int32_t refCount = 0; + + taosRLockLatch(&tsVnodes.latch); + taosHashGetClone(tsVnodes.hash, &vgId, sizeof(int32_t), (void *)&pVnode); + if (pVnode == NULL) { + terrno = TSDB_CODE_VND_INVALID_VGROUP_ID; + } else { + refCount = atomic_add_fetch_32(&pVnode->refCount, 1); + } + taosRUnLockLatch(&tsVnodes.latch); + + dTrace("vgId:%d, accquire vnode, refCount:%d", pVnode->vgId, refCount); + return pVnode; +} + +static void dnodeReleaseVnode(SVnodeObj *pVnode) { + int32_t refCount = atomic_sub_fetch_32(&pVnode->refCount, 1); + dTrace("vgId:%d, release vnode, refCount:%d", pVnode->vgId, refCount); +} + static int32_t dnodeCreateVnodeWrapper(int32_t vgId, SVnode *pImpl) { SVnodeObj *pVnode = calloc(1, sizeof(SVnodeObj)); if (pVnode == NULL) { @@ -107,13 +131,27 @@ static int32_t dnodeCreateVnodeWrapper(int32_t vgId, SVnode *pImpl) { return code; } - return taosHashPut(tsVnodes.hash, &vgId, sizeof(int32_t), &pVnode, sizeof(SVnodeObj *)); + taosWLockLatch(&tsVnodes.latch); + code = taosHashPut(tsVnodes.hash, &vgId, sizeof(int32_t), &pVnode, sizeof(SVnodeObj *)); + taosWUnLockLatch(&tsVnodes.latch); + + return code; } static void dnodeDropVnodeWrapper(SVnodeObj *pVnode) { + taosWLockLatch(&tsVnodes.latch); taosHashRemove(tsVnodes.hash, &pVnode->vgId, sizeof(int32_t)); + taosWUnLockLatch(&tsVnodes.latch); + + // wait all queue empty + dnodeReleaseVnode(pVnode); + while (pVnode->refCount > 0) taosMsleep(10); + while (!taosQueueEmpty(pVnode->pWriteQ)) taosMsleep(10); + while (!taosQueueEmpty(pVnode->pSyncQ)) taosMsleep(10); + while (!taosQueueEmpty(pVnode->pApplyQ)) taosMsleep(10); + while (!taosQueueEmpty(pVnode->pQueryQ)) taosMsleep(10); + while (!taosQueueEmpty(pVnode->pFetchQ)) taosMsleep(10); - //todo wait all queue empty dnodeFreeVnodeQueryQueue(pVnode); dnodeFreeVnodeFetchQueue(pVnode); dnodeFreeVnodeWriteQueue(pVnode); @@ -121,35 +159,164 @@ static void dnodeDropVnodeWrapper(SVnodeObj *pVnode) { dnodeFreeVnodeSyncQueue(pVnode); } -static int32_t dnodeGetVnodesFromHash(SVnodeObj *pVnodes[], int32_t *numOfVnodes) { +static SVnodeObj **dnodeGetVnodesFromHash(int32_t *numOfVnodes) { + taosRLockLatch(&tsVnodes.latch); + + int32_t num = 0; + int32_t size = taosHashGetSize(tsVnodes.hash); + SVnodeObj **pVnodes = calloc(size, sizeof(SVnodeObj *)); + void *pIter = taosHashIterate(tsVnodes.hash, NULL); while (pIter) { SVnodeObj **ppVnode = pIter; - if (*ppVnode) { - (*numOfVnodes)++; - if (*numOfVnodes >= TSDB_MAX_VNODES) { - dError("vgId:%d, too many open vnodes, exist:%d max:%d", (*ppVnode)->vgId, *numOfVnodes, TSDB_MAX_VNODES); - continue; - } else { - pVnodes[*numOfVnodes - 1] = (*ppVnode); + SVnodeObj *pVnode = *ppVnode; + if (pVnode) { + num++; + if (num < size) { + int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1); + dTrace("vgId:%d, accquire vnode, refCount:%d", pVnode->vgId, refCount); + pVnodes[num] = (*ppVnode); } } - pIter = taosHashIterate(tsVnodes.hash, pIter); } - return TSDB_CODE_SUCCESS; + taosRUnLockLatch(&tsVnodes.latch); + *numOfVnodes = num; + + return pVnodes; } -static int32_t dnodeGetVnodesFromFile(SVnodeObj *pVnodes, int32_t *numOfVnodes) { - pVnodes[0].vgId = 2; - pVnodes[0].dropped = 0; - pVnodes[0].vgId = 3; - pVnodes[0].dropped = 0; - return 0; +static int32_t dnodeGetVnodesFromFile(SVnodeObj **ppVnodes, int32_t *numOfVnodes) { + int32_t code = TSDB_CODE_DND_PARSE_VNODE_FILE_ERROR; + int32_t len = 0; + int32_t maxLen = 30000; + char *content = calloc(1, maxLen + 1); + cJSON *root = NULL; + FILE *fp = NULL; + char file[PATH_MAX + 20] = {0}; + SVnodeObj *pVnodes = NULL; + + snprintf(file, PATH_MAX + 20, "%s/vnodes.json", tsVnodeDir); + + fp = fopen(file, "r"); + if (!fp) { + dDebug("file %s not exist", file); + code = 0; + goto PRASE_VNODE_OVER; + } + + len = (int32_t)fread(content, 1, maxLen, fp); + if (len <= 0) { + dError("failed to read %s since content is null", file); + goto PRASE_VNODE_OVER; + } + + content[len] = 0; + root = cJSON_Parse(content); + if (root == NULL) { + dError("failed to read %s since invalid json format", file); + goto PRASE_VNODE_OVER; + } + + cJSON *vnodes = cJSON_GetObjectItem(root, "vnodes"); + if (!vnodes || vnodes->type != cJSON_Array) { + dError("failed to read %s since vnodes not found", file); + goto PRASE_VNODE_OVER; + } + + int32_t vnodesNum = cJSON_GetArraySize(vnodes); + if (vnodesNum <= 0) { + dError("failed to read %s since vnodes size:%d invalid", file, vnodesNum); + goto PRASE_VNODE_OVER; + } + + pVnodes = calloc(vnodesNum, sizeof(SVnodeObj)); + if (pVnodes == NULL) { + dError("failed to read %s since out of memory", file); + goto PRASE_VNODE_OVER; + } + + for (int32_t i = 0; i < vnodesNum; ++i) { + cJSON *vnode = cJSON_GetArrayItem(vnodes, i); + SVnodeObj *pVnode = &pVnodes[i]; + + cJSON *vgId = cJSON_GetObjectItem(vnode, "vgId"); + if (!vgId || vgId->type != cJSON_String) { + dError("failed to read %s since vgId not found", file); + goto PRASE_VNODE_OVER; + } + pVnode->vgId = atoi(vgId->valuestring); + + cJSON *dropped = cJSON_GetObjectItem(vnode, "dropped"); + if (!dropped || dropped->type != cJSON_String) { + dError("failed to read %s since dropped not found", file); + goto PRASE_VNODE_OVER; + } + pVnode->dropped = atoi(vnode->valuestring); + } + + code = 0; + dInfo("succcessed to read file %s", file); + +PRASE_VNODE_OVER: + if (content != NULL) free(content); + if (root != NULL) cJSON_Delete(root); + if (fp != NULL) fclose(fp); + + return code; } -static int32_t dnodeWriteVnodesToFile() { return 0; } +static int32_t dnodeWriteVnodesToFile() { + char file[PATH_MAX + 20] = {0}; + char realfile[PATH_MAX + 20] = {0}; + snprintf(file, PATH_MAX + 20, "%s/vnodes.json.bak", tsVnodeDir); + snprintf(realfile, PATH_MAX + 20, "%s/vnodes.json", tsVnodeDir); + + FILE *fp = fopen(file, "w"); + if (!fp) { + dError("failed to write %s since %s", file, strerror(errno)); + return -1; + } + + int32_t len = 0; + int32_t maxLen = 30000; + char *content = calloc(1, maxLen + 1); + int32_t numOfVnodes = 0; + SVnodeObj **pVnodes = dnodeGetVnodesFromHash(&numOfVnodes); + + len += snprintf(content + len, maxLen - len, "{\n"); + len += snprintf(content + len, maxLen - len, " \"vnodes\": [{\n"); + for (int32_t i = 0; i < numOfVnodes; ++i) { + SVnodeObj *pVnode = pVnodes[i]; + len += snprintf(content + len, maxLen - len, " \"vgId\": \"%d\",\n", pVnode->vgId); + len += snprintf(content + len, maxLen - len, " \"dropped\": \"%d\"\n", pVnode->dropped); + if (i < numOfVnodes - 1) { + len += snprintf(content + len, maxLen - len, " },{\n"); + } else { + len += snprintf(content + len, maxLen - len, " }]\n"); + } + } + len += snprintf(content + len, maxLen - len, "}\n"); + + fwrite(content, 1, len, fp); + taosFsyncFile(fileno(fp)); + fclose(fp); + free(content); + terrno = 0; + + for (int32_t i = 0; i < numOfVnodes; ++i) { + SVnodeObj *pVnode = pVnodes[i]; + dnodeReleaseVnode(pVnode); + } + + if (pVnodes != NULL) { + free(pVnodes); + } + + dInfo("successed to write %s", file); + return taosRenameFile(file, realfile); +} static int32_t dnodeCreateVnode(int32_t vgId, SVnodeCfg *pCfg) { int32_t code = 0; @@ -193,24 +360,6 @@ static int32_t dnodeDropVnode(SVnodeObj *pVnode) { return 0; } -static SVnodeObj *dnodeAcquireVnode(int32_t vgId) { - SVnodeObj *pVnode = NULL; - - taosHashGetClone(tsVnodes.hash, &vgId, sizeof(int32_t), (void *)&pVnode); - if (pVnode == NULL) { - terrno = TSDB_CODE_VND_INVALID_VGROUP_ID; - } - - int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1); - dTrace("vgId:%d, accquire vnode, refCount:%d", pVnode->vgId, refCount); - return pVnode; -} - -static void dnodeReleaseVnode(SVnodeObj *pVnode) { - int32_t refCount = atomic_sub_fetch_32(&pVnode->refCount, 1); - dTrace("vgId:%d, release vnode, refCount:%d", pVnode->vgId, refCount); -} - static void *dnodeOpenVnodeFunc(void *param) { SVThread *pThread = param; @@ -246,15 +395,17 @@ static void *dnodeOpenVnodeFunc(void *param) { } static int32_t dnodeOpenVnodes() { + taosInitRWLatch(&tsVnodes.latch); + tsVnodes.hash = taosHashInit(TSDB_MIN_VNODES, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); if (tsVnodes.hash == NULL) { dError("failed to init vnode hash"); return TSDB_CODE_VND_OUT_OF_MEMORY; } - SVnodeObj pVnodes[TSDB_MAX_VNODES] = {0}; - int32_t numOfVnodes = 0; - int32_t code = dnodeGetVnodesFromFile(pVnodes, &numOfVnodes); + SVnodeObj *pVnodes = NULL; + int32_t numOfVnodes = 0; + int32_t code = dnodeGetVnodesFromFile(&pVnodes, &numOfVnodes); if (code != TSDB_CODE_SUCCESS) { dInfo("failed to get vnode list from disk since %s", tstrerror(code)); return code; @@ -308,17 +459,14 @@ static int32_t dnodeOpenVnodes() { } static void dnodeCloseVnodes() { - SVnodeObj *pVnodes[TSDB_MAX_VNODES] = {0}; - int32_t numOfVnodes = 0; - - int32_t code = dnodeGetVnodesFromHash(pVnodes, &numOfVnodes); - if (code != TSDB_CODE_SUCCESS) { - dInfo("failed to get dnode list since code %d", code); - return; - } + int32_t numOfVnodes = 0; + SVnodeObj **pVnodes = dnodeGetVnodesFromHash(&numOfVnodes); for (int32_t i = 0; i < numOfVnodes; ++i) { - vnodeClose(pVnodes[i]->pImpl); + dnodeDropVnodeWrapper(pVnodes[i]); + } + if (pVnodes != NULL) { + free(pVnodes); } if (tsVnodes.hash != NULL) { @@ -431,12 +579,12 @@ static int32_t vnodeProcessDropVnodeReq(SRpcMsg *rpcMsg) { return code; } - code = vnodeDrop(pVnode->pImpl); + code = dnodeDropVnode(pVnode); if (code != 0) { + dnodeReleaseVnode(pVnode); dError("vgId:%d, failed to drop vnode since %s", vgId, tstrerror(code)); } - dnodeReleaseVnode(pVnode); return code; } diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 22fbeb1883..6838bab403 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -235,6 +235,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_DND_INVALID_MSG_LEN, "Invalid message lengt TAOS_DEFINE_ERROR(TSDB_CODE_DND_ACTION_IN_PROGRESS, "Action in progress") TAOS_DEFINE_ERROR(TSDB_CODE_DND_TOO_MANY_VNODES, "Too many vnode directories") TAOS_DEFINE_ERROR(TSDB_CODE_DND_EXITING, "Dnode is exiting") +TAOS_DEFINE_ERROR(TSDB_CODE_DND_PARSE_VNODE_FILE_ERROR, "Parse vnodes.json error") // vnode TAOS_DEFINE_ERROR(TSDB_CODE_VND_ACTION_IN_PROGRESS, "Action in progress") diff --git a/source/util/src/tqueue.c b/source/util/src/tqueue.c index 2813a55fea..5d6a507172 100644 --- a/source/util/src/tqueue.c +++ b/source/util/src/tqueue.c @@ -98,6 +98,20 @@ void taosCloseQueue(taos_queue param) { uTrace("queue:%p is closed", queue); } +bool taosQueueEmpty(taos_queue param) { + if (param == NULL) return true; + STaosQueue *queue = (STaosQueue *)param; + + bool empty = false; + pthread_mutex_lock(&queue->mutex); + if (queue->head == NULL && queue->tail == NULL) { + empty = true; + } + pthread_mutex_destroy(&queue->mutex); + + return empty; +} + void *taosAllocateQitem(int size) { STaosQnode *pNode = (STaosQnode *)calloc(sizeof(STaosQnode) + size, 1); From dd3281c9d2f163530809f8dc1f0568ace824ced8 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 4 Nov 2021 18:38:07 +0800 Subject: [PATCH 32/33] minor changes --- include/server/vnode/vnode.h | 4 ++-- source/dnode/vnode/impl/src/vnodeInt.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index 16699b855a..9bd6888479 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -74,11 +74,11 @@ SVnode *vnodeOpen(int32_t vgId, const char *path); void vnodeClose(SVnode *pVnode); int32_t vnodeAlter(SVnode *pVnode, const SVnodeCfg *pCfg); SVnode *vnodeCreate(int32_t vgId, const char *path, const SVnodeCfg *pCfg); -int32_t vnodeDrop(SVnode *pVnode); +void vnodeDrop(SVnode *pVnode); int32_t vnodeCompact(SVnode *pVnode); int32_t vnodeSync(SVnode *pVnode); -void vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad); +int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad); SVnodeMsg *vnodeInitMsg(int32_t msgNum); int32_t vnodeAppendMsg(SVnodeMsg *pMsg, SRpcMsg *pRpcMsg); diff --git a/source/dnode/vnode/impl/src/vnodeInt.c b/source/dnode/vnode/impl/src/vnodeInt.c index e08cc47aa1..20a94c0786 100644 --- a/source/dnode/vnode/impl/src/vnodeInt.c +++ b/source/dnode/vnode/impl/src/vnodeInt.c @@ -24,11 +24,11 @@ SVnode *vnodeOpen(int32_t vgId, const char *path) { return NULL; } void vnodeClose(SVnode *pVnode) {} int32_t vnodeAlter(SVnode *pVnode, const SVnodeCfg *pCfg) { return 0; } SVnode *vnodeCreate(int32_t vgId, const char *path, const SVnodeCfg *pCfg) { return NULL; } -int32_t vnodeDrop(SVnode *pVnode) { return 0; } +void vnodeDrop(SVnode *pVnode) {} int32_t vnodeCompact(SVnode *pVnode) { return 0; } int32_t vnodeSync(SVnode *pVnode) { return 0; } -void vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) {} +int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad) { return 0; } SVnodeMsg *vnodeInitMsg(int32_t msgNum) { SVnodeMsg *pMsg = taosAllocateQitem(msgNum * sizeof(SRpcMsg *) + sizeof(SVnodeMsg)); From da1bf2a2a8fb4f6a15d68ab046630c3e3c1cff56 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 4 Nov 2021 18:39:12 +0800 Subject: [PATCH 33/33] minor changes --- include/server/vnode/vnode.h | 4 ++-- source/dnode/vnode/impl/src/vnodeInt.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/server/vnode/vnode.h b/include/server/vnode/vnode.h index 9bd6888479..e570cf4261 100644 --- a/include/server/vnode/vnode.h +++ b/include/server/vnode/vnode.h @@ -53,7 +53,7 @@ typedef enum { VN_MSG_TYPE_SYNC, VN_MSG_TYPE_QUERY, VN_MSG_TYPE_FETCH -} EVMType; +} EVnMsgType; typedef struct { int32_t curNum; @@ -83,7 +83,7 @@ int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad); SVnodeMsg *vnodeInitMsg(int32_t msgNum); int32_t vnodeAppendMsg(SVnodeMsg *pMsg, SRpcMsg *pRpcMsg); void vnodeCleanupMsg(SVnodeMsg *pMsg); -void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg, EVMType msgType); +void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg, EVnMsgType msgType); #ifdef __cplusplus } diff --git a/source/dnode/vnode/impl/src/vnodeInt.c b/source/dnode/vnode/impl/src/vnodeInt.c index 20a94c0786..427a6dae4d 100644 --- a/source/dnode/vnode/impl/src/vnodeInt.c +++ b/source/dnode/vnode/impl/src/vnodeInt.c @@ -56,7 +56,7 @@ void vnodeCleanupMsg(SVnodeMsg *pMsg) { taosFreeQitem(pMsg); } -void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg, EVMType msgType) { +void vnodeProcessMsg(SVnode *pVnode, SVnodeMsg *pMsg, EVnMsgType msgType) { switch (msgType) { case VN_MSG_TYPE_WRITE: break;