Skip to content
Permalink
Browse files Browse the repository at this point in the history
remove redundant code (#9089)
* Modifying duplicate code,remove unnecessary if

* remove [instance.getIp() + ":" + instance.getPort()] and use the original Instance.toIpAddr() method,Because their implementation is the same
lphx committed Sep 7, 2022
1 parent a9af1bf commit 0cf9c24
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions naming/src/main/java/com/alibaba/nacos/naming/core/Cluster.java
}
}
private List<Instance> updatedIps(Collection<Instance> newInstance, Collection<Instance> oldInstance) {
List<Instance> intersects = (List<Instance>) CollectionUtils.intersection(newInstance, oldInstance);
Map<String, Instance> stringIpAddressMap = new ConcurrentHashMap<>(intersects.size());

for (Instance instance : intersects) {
stringIpAddressMap.put(instance.getIp() + ":" + instance.getPort(), instance);
stringIpAddressMap.put(instance.toIpAddr(), instance);
}

Map<String, Integer> intersectMap = new ConcurrentHashMap<>(newInstance.size() + oldInstance.size());
Map<String, Instance> updatedInstancesMap = new ConcurrentHashMap<>(newInstance.size());
Map<String, Instance> newInstancesMap = new ConcurrentHashMap<>(newInstance.size());

for (Instance instance : oldInstance) {
if (stringIpAddressMap.containsKey(instance.getIp() + ":" + instance.getPort())) {
if (stringIpAddressMap.containsKey(instance.toIpAddr())) {
intersectMap.put(instance.toString(), 1);
}
}

for (Instance instance : newInstance) {
if (stringIpAddressMap.containsKey(instance.getIp() + ":" + instance.getPort())) {

if (intersectMap.containsKey(instance.toString())) {
intersectMap.put(instance.toString(), 2);
} else {
intersectMap.put(instance.toString(), 1);
}
if (stringIpAddressMap.containsKey(instance.toIpAddr())) {
intersectMap.put(instance.toString(), intersectMap.containsKey(instance.toString()) ? 2 : 1);
}

newInstancesMap.put(instance.toString(), instance);

COLLECTOR-SAHAB / differentiating test: ClusterTest

instance.lastBeat=1684783870272 only occurs in the original version.

COLLECTOR-SAHAB / differentiating test: ClusterTest

instance.lastBeat=1684783912761 only occurs in the patched version.

}
for (Map.Entry<String, Integer> entry : intersectMap.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();

if (value == 1) {
if (newInstancesMap.containsKey(key)) {
updatedInstancesMap.put(key, newInstancesMap.get(key));
}

if (value == 1 && newInstancesMap.containsKey(key)) {
updatedInstancesMap.put(key, newInstancesMap.get(key));
}
}

return new ArrayList<>(updatedInstancesMap.values());
}
private List<Instance> subtract(Collection<Instance> oldIp, Collection<Instance> ips) {

0 comments on commit 0cf9c24

Please sign in to comment.