From 04538f9049db41241b79be0d9b3627d1768540db Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Mon, 22 Jan 2024 08:59:40 +0000 Subject: [PATCH] add test case --- include/common/tmisce.h | 9 +++++---- source/common/src/tmisce.c | 33 ++++++++++++++++++++++++++++----- source/libs/sync/src/syncMain.c | 4 +++- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/include/common/tmisce.h b/include/common/tmisce.h index 3d1afcd21f..afb33c733a 100644 --- a/include/common/tmisce.h +++ b/include/common/tmisce.h @@ -47,10 +47,11 @@ typedef struct SCorEpSet { int32_t taosGetFqdnPortFromEp(const char* ep, SEp* pEp); void addEpIntoEpSet(SEpSet* pEpSet, const char* fqdn, uint16_t port); -bool isEpsetEqual(const SEpSet* s1, const SEpSet* s2); -void epsetAssign(SEpSet* dst, const SEpSet* pSrc); -void updateEpSet_s(SCorEpSet* pEpSet, SEpSet* pNewEpSet); -SEpSet getEpSet_s(SCorEpSet* pEpSet); +bool isEpsetEqual(const SEpSet* s1, const SEpSet* s2); +void epsetAssign(SEpSet* dst, const SEpSet* pSrc); +void updateEpSet_s(SCorEpSet* pEpSet, SEpSet* pNewEpSet); +SEpSet getEpSet_s(SCorEpSet* pEpSet); +void epsetSort(SEpSet* pEpSet); #ifdef __cplusplus } diff --git a/source/common/src/tmisce.c b/source/common/src/tmisce.c index 95a5c27cf1..c3e2846d9a 100644 --- a/source/common/src/tmisce.c +++ b/source/common/src/tmisce.c @@ -15,11 +15,8 @@ #define _DEFAULT_SOURCE #include "tmisce.h" -#include "tjson.h" #include "tglobal.h" -#include "tlog.h" -#include "tname.h" - +#include "tjson.h" int32_t taosGetFqdnPortFromEp(const char* ep, SEp* pEp) { pEp->port = 0; memset(pEp->fqdn, 0, TSDB_FQDN_LEN); @@ -63,7 +60,7 @@ bool isEpsetEqual(const SEpSet* s1, const SEpSet* s2) { void epsetAssign(SEpSet* pDst, const SEpSet* pSrc) { if (pSrc == NULL || pDst == NULL) { - return; + return; } pDst->inUse = pSrc->inUse; @@ -73,6 +70,32 @@ void epsetAssign(SEpSet* pDst, const SEpSet* pSrc) { tstrncpy(pDst->eps[i].fqdn, pSrc->eps[i].fqdn, tListLen(pSrc->eps[i].fqdn)); } } +void epAssign(SEp* pDst, SEp* pSrc) { + if (pSrc == NULL || pDst == NULL) { + return; + } + memset(pDst->fqdn, 0, tListLen(pSrc->fqdn)); + tstrncpy(pDst->fqdn, pSrc->fqdn, tListLen(pSrc->fqdn)); + pDst->port = pSrc->port; +} +void epsetSort(SEpSet* pDst) { + if (pDst->numOfEps <= 1) { + return; + } + for (int i = 0; i < pDst->numOfEps - 1; i++) { + for (int j = 0; j < pDst->numOfEps - 1 - i; j++) { + SEp* f = &pDst->eps[j]; + SEp* s = &pDst->eps[j + 1]; + int cmp = strncmp(f->fqdn, s->fqdn, sizeof(f->fqdn)); + if (cmp > 0 || (cmp == 0 && f->port > s->port)) { + SEp ep = {0}; + epAssign(&ep, f); + epAssign(f, s); + epAssign(s, &ep); + } + } + } +} void updateEpSet_s(SCorEpSet* pEpSet, SEpSet* pNewEpSet) { taosCorBeginWrite(&pEpSet->version); diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 89a41806cd..b60b3c96ca 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -579,13 +579,15 @@ void syncGetRetryEpSet(int64_t rid, SEpSet* pEpSet) { SSyncNode* pSyncNode = syncNodeAcquire(rid); if (pSyncNode == NULL) return; + int j = 0; for (int32_t i = 0; i < pSyncNode->raftCfg.cfg.totalReplicaNum; ++i) { if (pSyncNode->raftCfg.cfg.nodeInfo[i].nodeRole == TAOS_SYNC_ROLE_LEARNER) continue; - SEp* pEp = &pEpSet->eps[i]; + SEp* pEp = &pEpSet->eps[j]; tstrncpy(pEp->fqdn, pSyncNode->raftCfg.cfg.nodeInfo[i].nodeFqdn, TSDB_FQDN_LEN); pEp->port = (pSyncNode->raftCfg.cfg.nodeInfo)[i].nodePort; pEpSet->numOfEps++; sDebug("vgId:%d, sync get retry epset, index:%d %s:%d", pSyncNode->vgId, i, pEp->fqdn, pEp->port); + j++; } if (pEpSet->numOfEps > 0) { pEpSet->inUse = (pSyncNode->raftCfg.cfg.myIndex + 1) % pEpSet->numOfEps;