fix metadata batch operation may delete instance problem.

This commit is contained in:
horizonzy 2021-01-14 16:32:15 +08:00
parent d22e5909d4
commit 6cdead245e

View File

@ -21,6 +21,7 @@ import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.common.utils.IPUtil; import com.alibaba.nacos.common.utils.IPUtil;
import com.alibaba.nacos.common.utils.JacksonUtils; 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.Member;
import com.alibaba.nacos.core.cluster.ServerMemberManager; import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.naming.consistency.ConsistencyService; import com.alibaba.nacos.naming.consistency.ConsistencyService;
@ -580,7 +581,7 @@ public class ServiceManager implements RecordListener<Service> {
} }
/** /**
* locate consistency's datum by all or instances provided. * Locate consistency's datum by all or instances provided.
* *
* @param namespaceId namespace * @param namespaceId namespace
* @param serviceName serviceName * @param serviceName serviceName
@ -618,16 +619,22 @@ public class ServiceManager implements RecordListener<Service> {
return locatedInstance; return locatedInstance;
} }
private Instance locateInstance(List<Instance> instances, Instance instance) { /**
int target = 0; * Locate instance.
while (target >= 0) { *
target = instances.indexOf(instance); * @param sources the instanceList in DistroConsistencyServiceImpl.dataStore
if (target >= 0) { * @param target the instance need to locate
Instance result = instances.get(target); * @return the located instance
if (result.getClusterName().equals(instance.getClusterName())) { */
return result; private Instance locateInstance(List<Instance> sources, Instance target) {
if (CollectionUtils.isEmpty(sources)) {
return null;
} }
instances.remove(target);
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; return null;
@ -963,7 +970,8 @@ public class ServiceManager implements RecordListener<Service> {
List<Instance> instances = service.allIPs(); List<Instance> instances = service.allIPs();
for (Instance instance : instances) { for (Instance instance : instances) {
if (IPUtil.containsPort(containedInstance)) { 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; contained = true;
break; break;
} }