Skip to content

Commit

Permalink
chore:remove fetch group name. (sofastack#792)
Browse files Browse the repository at this point in the history
* chore:remove fetch group name
  • Loading branch information
leizhiyuan authored Nov 6, 2019
1 parent 340ebc3 commit 201de55
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,20 @@ public class SingleGroupAddressHolder extends AddressHolder {
/**
* 配置的直连地址列表
*/
protected ProviderGroup directUrlGroup;
protected ProviderGroup directUrlGroup;
/**
* 注册中心来的地址列表
*/
protected ProviderGroup registryGroup;

/**
* 存储关系
*/
protected Map<ProviderInfo, ProviderGroup> reversedRelationMap = new ConcurrentHashMap<ProviderInfo, ProviderGroup>();
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();

/**
* 构造函数
Expand Down Expand Up @@ -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)) {
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -165,52 +142,28 @@ public void updateProviders(ProviderGroup providerGroup) {
try {
getProviderGroup(providerGroup.getName())
.setProviderInfos(new ArrayList<ProviderInfo>(providerGroup.getProviderInfos()));
addOrUpdateReversedRelation(this.reversedRelationMap, providerGroup);
} finally {
wLock.unlock();
}
}

public void addOrUpdateReversedRelation(Map<ProviderInfo, ProviderGroup> tmpReversedRelationMap,
ProviderGroup providerGroup) {
final List<ProviderInfo> providerInfos = providerGroup.getProviderInfos();
if (providerInfos != null) {
for (ProviderInfo providerInfo : providerInfos) {
tmpReversedRelationMap.put(providerInfo, providerGroup);
}
}
}

public void removeReversedRelation(Map<ProviderInfo, ProviderGroup> tmpReversedRelationMap,
ProviderGroup providerGroup) {
final List<ProviderInfo> providerInfos = providerGroup.getProviderInfos();
if (providerInfos != null) {
for (ProviderInfo providerInfo : providerInfos) {
reversedRelationMap.remove(providerInfo);
}
}
}

@Override
public void updateAllProviders(List<ProviderGroup> providerGroups) {
ConcurrentHashSet<ProviderInfo> tmpDirectUrl = new ConcurrentHashSet<ProviderInfo>();
ConcurrentHashSet<ProviderInfo> tmpRegistry = new ConcurrentHashSet<ProviderInfo>();
Map<ProviderInfo, ProviderGroup> tmpReversedRelationMap = new ConcurrentHashMap<>();
for (ProviderGroup providerGroup : providerGroups) {
if (!ProviderHelper.isEmpty(providerGroup)) {
if (RpcConstants.ADDRESS_DIRECT_GROUP.equals(providerGroup.getName())) {
tmpDirectUrl.addAll(providerGroup.getProviderInfos());
} else {
tmpRegistry.addAll(providerGroup.getProviderInfos());
}
addOrUpdateReversedRelation(tmpReversedRelationMap, providerGroup);
}
}
wLock.lock();
try {
this.directUrlGroup.setProviderInfos(new ArrayList<ProviderInfo>(tmpDirectUrl));
this.registryGroup.setProviderInfos(new ArrayList<ProviderInfo>(tmpRegistry));
this.reversedRelationMap = tmpReversedRelationMap;
} finally {
wLock.unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProviderGroup> providerGroups = new ArrayList<>();
providerGroups.add(providerGroup);
addressHolder.updateAllProviders(providerGroups);
groupName = addressHolder.fetchGroupName(providerInfo);
Assert.assertEquals("xxx", groupName);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

/**
* 地址管理器
*
*
* @author <a href="mailto:[email protected]">GengZhang</a>
*/
@Extensible(singleton = false)
Expand Down Expand Up @@ -73,10 +73,4 @@ protected AddressHolder(ConsumerBootstrap consumerBootstrap) {
* @return 全部服务列表
*/
public abstract int getAllProviderSize();

/**
* @param providerInfo
* @return
*/
public abstract String fetchGroupName(ProviderInfo providerInfo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ public int getAllProviderSize() {
return 0;
}

@Override
public String fetchGroupName(ProviderInfo providerInfo) {
return null;
}

@Override
public void addProvider(ProviderGroup providerGroup) {

Expand Down

0 comments on commit 201de55

Please sign in to comment.