diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index fda5b5d6b..9058884d5 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -21,6 +21,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.common.utils.IPUtil; import com.alibaba.nacos.common.utils.JacksonUtils; +import com.alibaba.nacos.common.utils.Objects; import com.alibaba.nacos.core.cluster.Member; import com.alibaba.nacos.core.cluster.ServerMemberManager; import com.alibaba.nacos.naming.consistency.ConsistencyService; @@ -580,7 +581,7 @@ public class ServiceManager implements RecordListener { } /** - * locate consistency's datum by all or instances provided. + * Locate consistency's datum by all or instances provided. * * @param namespaceId namespace * @param serviceName serviceName @@ -618,16 +619,22 @@ public class ServiceManager implements RecordListener { return locatedInstance; } - private Instance locateInstance(List instances, Instance instance) { - int target = 0; - while (target >= 0) { - target = instances.indexOf(instance); - if (target >= 0) { - Instance result = instances.get(target); - if (result.getClusterName().equals(instance.getClusterName())) { - return result; - } - instances.remove(target); + /** + * Locate instance. + * + * @param sources the instanceList in DistroConsistencyServiceImpl.dataStore + * @param target the instance need to locate + * @return the located instance + */ + private Instance locateInstance(List sources, Instance target) { + if (CollectionUtils.isEmpty(sources)) { + return null; + } + + for (Instance element : sources) { + //also need clusterName equals, the same instance maybe exist in two cluster. + if (Objects.equals(element, target) && Objects.equals(element.getClusterName(), target.getClusterName())) { + return element; } } return null; @@ -963,7 +970,8 @@ public class ServiceManager implements RecordListener { List instances = service.allIPs(); for (Instance instance : instances) { if (IPUtil.containsPort(containedInstance)) { - if (StringUtils.equals(instance.getIp() + IPUtil.IP_PORT_SPLITER + instance.getPort(), containedInstance)) { + if (StringUtils.equals(instance.getIp() + IPUtil.IP_PORT_SPLITER + instance.getPort(), + containedInstance)) { contained = true; break; }