From 380780331da64917d7e9754214ca485cd8343c9f Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 19 May 2022 15:19:19 +0800 Subject: [PATCH] refactor: change lockfree to rwlock --- source/dnode/mgmt/mgmt_dnode/src/dmHandle.c | 8 +++---- source/dnode/mgmt/mgmt_snode/inc/smInt.h | 1 - source/dnode/mgmt/node_mgmt/inc/dmMgmt.h | 2 +- source/dnode/mgmt/node_mgmt/src/dmMgmt.c | 22 +++++++++--------- source/dnode/mgmt/node_mgmt/src/dmNodes.c | 4 ++-- source/dnode/mgmt/node_util/inc/dmUtil.h | 24 ++++++++++---------- source/dnode/mgmt/node_util/src/dmEps.c | 25 ++++++++++----------- 7 files changed, 43 insertions(+), 43 deletions(-) diff --git a/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c b/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c index 7c1162ec10..f7337f482f 100644 --- a/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c +++ b/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c @@ -19,11 +19,11 @@ static void dmUpdateDnodeCfg(SDnodeMgmt *pMgmt, SDnodeCfg *pCfg) { if (pMgmt->pData->dnodeId == 0 || pMgmt->pData->clusterId == 0) { dInfo("set dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId); - taosWLockLatch(&pMgmt->pData->latch); + taosThreadRwlockWrlock(&pMgmt->pData->lock); pMgmt->pData->dnodeId = pCfg->dnodeId; pMgmt->pData->clusterId = pCfg->clusterId; dmWriteEps(pMgmt->pData); - taosWUnLockLatch(&pMgmt->pData->latch); + taosThreadRwlockUnlock(&pMgmt->pData->lock); } } @@ -50,7 +50,7 @@ static void dmProcessStatusRsp(SDnodeMgmt *pMgmt, SRpcMsg *pRsp) { void dmSendStatusReq(SDnodeMgmt *pMgmt) { SStatusReq req = {0}; - taosRLockLatch(&pMgmt->pData->latch); + taosThreadRwlockRdlock(&pMgmt->pData->lock); req.sver = tsVersion; req.dnodeVer = pMgmt->pData->dnodeVer; req.dnodeId = pMgmt->pData->dnodeId; @@ -69,7 +69,7 @@ void dmSendStatusReq(SDnodeMgmt *pMgmt) { memcpy(req.clusterCfg.timezone, tsTimezoneStr, TD_TIMEZONE_LEN); memcpy(req.clusterCfg.locale, tsLocale, TD_LOCALE_LEN); memcpy(req.clusterCfg.charset, tsCharset, TD_LOCALE_LEN); - taosRUnLockLatch(&pMgmt->pData->latch); + taosThreadRwlockUnlock(&pMgmt->pData->lock); SMonVloadInfo vinfo = {0}; (*pMgmt->getVnodeLoadsFp)(&vinfo); diff --git a/source/dnode/mgmt/mgmt_snode/inc/smInt.h b/source/dnode/mgmt/mgmt_snode/inc/smInt.h index 6d0bea9590..68b6ef659e 100644 --- a/source/dnode/mgmt/mgmt_snode/inc/smInt.h +++ b/source/dnode/mgmt/mgmt_snode/inc/smInt.h @@ -30,7 +30,6 @@ typedef struct SSnodeMgmt { SMsgCb msgCb; const char *path; const char *name; - SRWLatch latch; int8_t uniqueWorkerInUse; SArray *uniqueWorkers; // SArray SSingleWorker sharedWorker; diff --git a/source/dnode/mgmt/node_mgmt/inc/dmMgmt.h b/source/dnode/mgmt/node_mgmt/inc/dmMgmt.h index 9d092a93bc..5818b58801 100644 --- a/source/dnode/mgmt/node_mgmt/inc/dmMgmt.h +++ b/source/dnode/mgmt/node_mgmt/inc/dmMgmt.h @@ -70,7 +70,7 @@ typedef struct SMgmtWrapper { const char *name; char *path; int32_t refCount; - SRWLatch latch; + TdThreadRwlock lock; EDndNodeType ntype; bool deployed; bool required; diff --git a/source/dnode/mgmt/node_mgmt/src/dmMgmt.c b/source/dnode/mgmt/node_mgmt/src/dmMgmt.c index 3cbb9ff046..96285bbe1c 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmMgmt.c +++ b/source/dnode/mgmt/node_mgmt/src/dmMgmt.c @@ -91,7 +91,7 @@ static int32_t dmInitVars(SDnode *pDnode, EDndNodeType rtype) { return -1; } - taosInitRWLatch(&pData->latch); + taosThreadRwlockInit(&pData->lock, NULL); taosThreadMutexInit(&pDnode->mutex, NULL); return 0; } @@ -100,6 +100,7 @@ static void dmClearVars(SDnode *pDnode) { for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) { SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; taosMemoryFreeClear(pWrapper->path); + taosThreadRwlockDestroy(&pWrapper->lock); } if (pDnode->lockfile != NULL) { taosUnLockFile(pDnode->lockfile); @@ -108,7 +109,7 @@ static void dmClearVars(SDnode *pDnode) { } SDnodeData *pData = &pDnode->data; - taosWLockLatch(&pData->latch); + taosThreadRwlockWrlock(&pData->lock); if (pData->dnodeEps != NULL) { taosArrayDestroy(pData->dnodeEps); pData->dnodeEps = NULL; @@ -117,8 +118,9 @@ static void dmClearVars(SDnode *pDnode) { taosHashCleanup(pData->dnodeHash); pData->dnodeHash = NULL; } - taosWUnLockLatch(&pData->latch); + taosThreadRwlockUnlock(&pData->lock); + taosThreadRwlockDestroy(&pData->lock); taosThreadMutexDestroy(&pDnode->mutex); memset(&pDnode->mutex, 0, sizeof(pDnode->mutex)); } @@ -151,7 +153,7 @@ int32_t dmInitDnode(SDnode *pDnode, EDndNodeType rtype) { if (ntype == DNODE) { pWrapper->proc.ptype = DND_PROC_SINGLE; } - taosInitRWLatch(&pWrapper->latch); + taosThreadRwlockInit(&pWrapper->lock, NULL); snprintf(path, sizeof(path), "%s%s%s", tsDataDir, TD_DIRSEP, pWrapper->name); pWrapper->path = strdup(path); @@ -223,7 +225,7 @@ SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType ntype) { SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; SMgmtWrapper *pRetWrapper = pWrapper; - taosRLockLatch(&pWrapper->latch); + taosThreadRwlockRdlock(&pWrapper->lock); if (pWrapper->deployed) { int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1); dTrace("node:%s, is acquired, ref:%d", pWrapper->name, refCount); @@ -231,7 +233,7 @@ SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType ntype) { terrno = TSDB_CODE_NODE_NOT_DEPLOYED; pRetWrapper = NULL; } - taosRUnLockLatch(&pWrapper->latch); + taosThreadRwlockUnlock(&pWrapper->lock); return pRetWrapper; } @@ -239,7 +241,7 @@ SMgmtWrapper *dmAcquireWrapper(SDnode *pDnode, EDndNodeType ntype) { int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) { int32_t code = 0; - taosRLockLatch(&pWrapper->latch); + taosThreadRwlockRdlock(&pWrapper->lock); if (pWrapper->deployed || (InParentProc(pWrapper) && pWrapper->required)) { int32_t refCount = atomic_add_fetch_32(&pWrapper->refCount, 1); dTrace("node:%s, is marked, ref:%d", pWrapper->name, refCount); @@ -247,7 +249,7 @@ int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) { terrno = TSDB_CODE_NODE_NOT_DEPLOYED; code = -1; } - taosRUnLockLatch(&pWrapper->latch); + taosThreadRwlockUnlock(&pWrapper->lock); return code; } @@ -255,9 +257,9 @@ int32_t dmMarkWrapper(SMgmtWrapper *pWrapper) { void dmReleaseWrapper(SMgmtWrapper *pWrapper) { if (pWrapper == NULL) return; - taosRLockLatch(&pWrapper->latch); + taosThreadRwlockRdlock(&pWrapper->lock); int32_t refCount = atomic_sub_fetch_32(&pWrapper->refCount, 1); - taosRUnLockLatch(&pWrapper->latch); + taosThreadRwlockUnlock(&pWrapper->lock); dTrace("node:%s, is released, ref:%d", pWrapper->name, refCount); } diff --git a/source/dnode/mgmt/node_mgmt/src/dmNodes.c b/source/dnode/mgmt/node_mgmt/src/dmNodes.c index ecfa37725a..ab9d3f67e7 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmNodes.c +++ b/source/dnode/mgmt/node_mgmt/src/dmNodes.c @@ -186,12 +186,12 @@ void dmCloseNode(SMgmtWrapper *pWrapper) { } } - taosWLockLatch(&pWrapper->latch); + taosThreadRwlockWrlock(&pWrapper->lock); if (pWrapper->pMgmt != NULL) { (*pWrapper->func.closeFp)(pWrapper->pMgmt); pWrapper->pMgmt = NULL; } - taosWUnLockLatch(&pWrapper->latch); + taosThreadRwlockUnlock(&pWrapper->lock); if (!OnlyInSingleProc(pWrapper)) { dmCleanupProc(pWrapper); diff --git a/source/dnode/mgmt/node_util/inc/dmUtil.h b/source/dnode/mgmt/node_util/inc/dmUtil.h index b0e764bf8e..e7256a3a87 100644 --- a/source/dnode/mgmt/node_util/inc/dmUtil.h +++ b/source/dnode/mgmt/node_util/inc/dmUtil.h @@ -94,18 +94,18 @@ typedef void (*GetVnodeLoadsFp)(); typedef void (*GetMnodeLoadsFp)(); typedef struct { - int32_t dnodeId; - int64_t clusterId; - int64_t dnodeVer; - int64_t updateTime; - int64_t rebootTime; - bool dropped; - bool stopped; - SEpSet mnodeEps; - SArray *dnodeEps; - SHashObj *dnodeHash; - SRWLatch latch; - SMsgCb msgCb; + int32_t dnodeId; + int64_t clusterId; + int64_t dnodeVer; + int64_t updateTime; + int64_t rebootTime; + bool dropped; + bool stopped; + SEpSet mnodeEps; + SArray *dnodeEps; + SHashObj *dnodeHash; + TdThreadRwlock lock; + SMsgCb msgCb; } SDnodeData; typedef struct { diff --git a/source/dnode/mgmt/node_util/src/dmEps.c b/source/dnode/mgmt/node_util/src/dmEps.c index a6c9fda64d..94fa569557 100644 --- a/source/dnode/mgmt/node_util/src/dmEps.c +++ b/source/dnode/mgmt/node_util/src/dmEps.c @@ -21,7 +21,7 @@ static bool dmIsEpChanged(SDnodeData *pData, int32_t dnodeId, const char *ep); static void dmResetEps(SDnodeData *pData, SArray *dnodeEps); static void dmGetDnodeEp(SDnodeData *pData, int32_t dnodeId, char *pEp, char *pFqdn, uint16_t *pPort) { - taosRLockLatch(&pData->latch); + taosThreadRwlockRdlock(&pData->lock); SDnodeEp *pDnodeEp = taosHashGet(pData->dnodeHash, &dnodeId, sizeof(int32_t)); if (pDnodeEp != NULL) { @@ -36,7 +36,7 @@ static void dmGetDnodeEp(SDnodeData *pData, int32_t dnodeId, char *pEp, char *pF } } - taosRUnLockLatch(&pData->latch); + taosThreadRwlockUnlock(&pData->lock); } int32_t dmReadEps(SDnodeData *pData) { @@ -232,7 +232,7 @@ void dmUpdateEps(SDnodeData *pData, SArray *eps) { int32_t numOfEps = taosArrayGetSize(eps); if (numOfEps <= 0) return; - taosWLockLatch(&pData->latch); + taosThreadRwlockWrlock(&pData->lock); int32_t numOfEpsOld = (int32_t)taosArrayGetSize(pData->dnodeEps); if (numOfEps != numOfEpsOld) { @@ -246,7 +246,7 @@ void dmUpdateEps(SDnodeData *pData, SArray *eps) { } } - taosWUnLockLatch(&pData->latch); + taosThreadRwlockUnlock(&pData->lock); } static void dmResetEps(SDnodeData *pData, SArray *dnodeEps) { @@ -292,7 +292,7 @@ static void dmPrintEps(SDnodeData *pData) { static bool dmIsEpChanged(SDnodeData *pData, int32_t dnodeId, const char *ep) { bool changed = false; if (dnodeId == 0) return changed; - taosRLockLatch(&pData->latch); + taosThreadRwlockRdlock(&pData->lock); SDnodeEp *pDnodeEp = taosHashGet(pData->dnodeHash, &dnodeId, sizeof(int32_t)); if (pDnodeEp != NULL) { @@ -304,24 +304,23 @@ static bool dmIsEpChanged(SDnodeData *pData, int32_t dnodeId, const char *ep) { } } - taosRUnLockLatch(&pData->latch); + taosThreadRwlockUnlock(&pData->lock); return changed; } void dmGetMnodeEpSet(SDnodeData *pData, SEpSet *pEpSet) { - taosRLockLatch(&pData->latch); + taosThreadRwlockRdlock(&pData->lock); *pEpSet = pData->mnodeEps; - taosRUnLockLatch(&pData->latch); + taosThreadRwlockUnlock(&pData->lock); } void dmSetMnodeEpSet(SDnodeData *pData, SEpSet *pEpSet) { - dInfo("mnode is changed, num:%d use:%d", pEpSet->numOfEps, pEpSet->inUse); - - taosWLockLatch(&pData->latch); + taosThreadRwlockWrlock(&pData->lock); pData->mnodeEps = *pEpSet; + taosThreadRwlockUnlock(&pData->lock); + + dInfo("mnode is changed, num:%d use:%d", pEpSet->numOfEps, pEpSet->inUse); for (int32_t i = 0; i < pEpSet->numOfEps; ++i) { dInfo("mnode index:%d %s:%u", i, pEpSet->eps[i].fqdn, pEpSet->eps[i].port); } - - taosWUnLockLatch(&pData->latch); } -- GitLab