From 201de554f8f7e992c203135d5f3f95d5e6904718 Mon Sep 17 00:00:00 2001 From: Lei Zhiyuan Date: Wed, 6 Nov 2019 12:07:40 +0800 Subject: [PATCH] chore:remove fetch group name. (#792) * chore:remove fetch group name --- .../rpc/client/SingleGroupAddressHolder.java | 57 ++----------------- .../client/SingleGroupAddressHolderTest.java | 32 ----------- .../alipay/sofa/rpc/client/AddressHolder.java | 8 +-- .../sofa/rpc/client/TestAddressHolder.java | 5 -- 4 files changed, 6 insertions(+), 96 deletions(-) diff --git a/core-impl/client/src/main/java/com/alipay/sofa/rpc/client/SingleGroupAddressHolder.java b/core-impl/client/src/main/java/com/alipay/sofa/rpc/client/SingleGroupAddressHolder.java index 55c39b978..e61f22ad4 100644 --- a/core-impl/client/src/main/java/com/alipay/sofa/rpc/client/SingleGroupAddressHolder.java +++ b/core-impl/client/src/main/java/com/alipay/sofa/rpc/client/SingleGroupAddressHolder.java @@ -39,25 +39,20 @@ public class SingleGroupAddressHolder extends AddressHolder { /** * 配置的直连地址列表 */ - protected ProviderGroup directUrlGroup; + protected ProviderGroup directUrlGroup; /** * 注册中心来的地址列表 */ - protected ProviderGroup registryGroup; - - /** - * 存储关系 - */ - protected Map reversedRelationMap = new ConcurrentHashMap(); + protected ProviderGroup registryGroup; /** * 地址变化的锁 */ - private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); // 读锁,允许并发读 - private Lock rLock = lock.readLock(); + private Lock rLock = lock.readLock(); // 写锁,写的时候不允许读 - private Lock wLock = lock.writeLock(); + private Lock wLock = lock.writeLock(); /** * 构造函数 @@ -115,22 +110,6 @@ public int getAllProviderSize() { } } - @Override - public String fetchGroupName(ProviderInfo providerInfo) { - rLock.lock(); - - try { - final ProviderGroup providerGroup = reversedRelationMap.get(providerInfo); - if (providerGroup != null) { - return providerGroup.getName(); - } else { - return ""; - } - } finally { - rLock.unlock(); - } - } - @Override public void addProvider(ProviderGroup providerGroup) { if (ProviderHelper.isEmpty(providerGroup)) { @@ -139,7 +118,6 @@ public void addProvider(ProviderGroup providerGroup) { wLock.lock(); try { getProviderGroup(providerGroup.getName()).addAll(providerGroup.getProviderInfos()); - addOrUpdateReversedRelation(this.reversedRelationMap, providerGroup); } finally { wLock.unlock(); } @@ -153,7 +131,6 @@ public void removeProvider(ProviderGroup providerGroup) { wLock.lock(); try { getProviderGroup(providerGroup.getName()).removeAll(providerGroup.getProviderInfos()); - removeReversedRelation(this.reversedRelationMap, providerGroup); } finally { wLock.unlock(); } @@ -165,37 +142,15 @@ public void updateProviders(ProviderGroup providerGroup) { try { getProviderGroup(providerGroup.getName()) .setProviderInfos(new ArrayList(providerGroup.getProviderInfos())); - addOrUpdateReversedRelation(this.reversedRelationMap, providerGroup); } finally { wLock.unlock(); } } - public void addOrUpdateReversedRelation(Map tmpReversedRelationMap, - ProviderGroup providerGroup) { - final List providerInfos = providerGroup.getProviderInfos(); - if (providerInfos != null) { - for (ProviderInfo providerInfo : providerInfos) { - tmpReversedRelationMap.put(providerInfo, providerGroup); - } - } - } - - public void removeReversedRelation(Map tmpReversedRelationMap, - ProviderGroup providerGroup) { - final List providerInfos = providerGroup.getProviderInfos(); - if (providerInfos != null) { - for (ProviderInfo providerInfo : providerInfos) { - reversedRelationMap.remove(providerInfo); - } - } - } - @Override public void updateAllProviders(List providerGroups) { ConcurrentHashSet tmpDirectUrl = new ConcurrentHashSet(); ConcurrentHashSet tmpRegistry = new ConcurrentHashSet(); - Map tmpReversedRelationMap = new ConcurrentHashMap<>(); for (ProviderGroup providerGroup : providerGroups) { if (!ProviderHelper.isEmpty(providerGroup)) { if (RpcConstants.ADDRESS_DIRECT_GROUP.equals(providerGroup.getName())) { @@ -203,14 +158,12 @@ public void updateAllProviders(List providerGroups) { } else { tmpRegistry.addAll(providerGroup.getProviderInfos()); } - addOrUpdateReversedRelation(tmpReversedRelationMap, providerGroup); } } wLock.lock(); try { this.directUrlGroup.setProviderInfos(new ArrayList(tmpDirectUrl)); this.registryGroup.setProviderInfos(new ArrayList(tmpRegistry)); - this.reversedRelationMap = tmpReversedRelationMap; } finally { wLock.unlock(); } diff --git a/core-impl/client/src/test/java/com/alipay/sofa/rpc/client/SingleGroupAddressHolderTest.java b/core-impl/client/src/test/java/com/alipay/sofa/rpc/client/SingleGroupAddressHolderTest.java index af08fd7b8..5390c0b45 100644 --- a/core-impl/client/src/test/java/com/alipay/sofa/rpc/client/SingleGroupAddressHolderTest.java +++ b/core-impl/client/src/test/java/com/alipay/sofa/rpc/client/SingleGroupAddressHolderTest.java @@ -286,36 +286,4 @@ public void run() { } Assert.assertFalse(error.get()); } - - @Test - public void testReversedRelation() throws Exception { - SingleGroupAddressHolder addressHolder = new SingleGroupAddressHolder(null); - Assert.assertTrue(ProviderHelper.isEmpty(addressHolder.getProviderGroup(null))); - Assert.assertTrue(ProviderHelper.isEmpty(addressHolder.getProviderGroup(StringUtils.EMPTY))); - Assert.assertTrue(ProviderHelper.isEmpty(addressHolder.getProviderGroup(ADDRESS_DEFAULT_GROUP))); - - final ProviderInfo providerInfo = ProviderHelper.toProviderInfo("127.0.0.1:12200"); - ProviderGroup providerGroup = new ProviderGroup("xxx"); - providerGroup.add(providerInfo); - addressHolder.addProvider(providerGroup); - - - String groupName = addressHolder.fetchGroupName(providerInfo); - - Assert.assertEquals("xxx", groupName); - - addressHolder.removeProvider(providerGroup); - - groupName = addressHolder.fetchGroupName(providerInfo); - - Assert.assertEquals("", groupName); - - - final ArrayList providerGroups = new ArrayList<>(); - providerGroups.add(providerGroup); - addressHolder.updateAllProviders(providerGroups); - groupName = addressHolder.fetchGroupName(providerInfo); - Assert.assertEquals("xxx", groupName); - - } } \ No newline at end of file diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/client/AddressHolder.java b/core/api/src/main/java/com/alipay/sofa/rpc/client/AddressHolder.java index 0b024673d..1c9621ac1 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/client/AddressHolder.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/client/AddressHolder.java @@ -24,7 +24,7 @@ /** * 地址管理器 - * + * * @author GengZhang */ @Extensible(singleton = false) @@ -73,10 +73,4 @@ protected AddressHolder(ConsumerBootstrap consumerBootstrap) { * @return 全部服务列表 */ public abstract int getAllProviderSize(); - - /** - * @param providerInfo - * @return - */ - public abstract String fetchGroupName(ProviderInfo providerInfo); } diff --git a/core/api/src/test/java/com/alipay/sofa/rpc/client/TestAddressHolder.java b/core/api/src/test/java/com/alipay/sofa/rpc/client/TestAddressHolder.java index 752f3be77..ddefb026e 100644 --- a/core/api/src/test/java/com/alipay/sofa/rpc/client/TestAddressHolder.java +++ b/core/api/src/test/java/com/alipay/sofa/rpc/client/TestAddressHolder.java @@ -57,11 +57,6 @@ public int getAllProviderSize() { return 0; } - @Override - public String fetchGroupName(ProviderInfo providerInfo) { - return null; - } - @Override public void addProvider(ProviderGroup providerGroup) {