Skip to content

Commit

Permalink
fix(alibaba#10831): When using the deregisterInstance method to remov…
Browse files Browse the repository at this point in the history
…e one of multiple instances registered by batchRegisterInstance, all instances registered by batchRegisterInstance will be removed.
  • Loading branch information
Bo-Qiu committed Jul 26, 2023
1 parent 8adca68 commit c7603bb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
import com.alibaba.nacos.common.remote.ConnectionType;
import com.alibaba.nacos.common.remote.client.RpcClient;
import com.alibaba.nacos.common.remote.client.RpcClientFactory;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.remote.client.RpcClientTlsConfig;
import com.alibaba.nacos.common.remote.client.ServerListFactory;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;

Expand Down Expand Up @@ -139,8 +139,10 @@ public void batchRegisterService(String serviceName, String groupName, List<Inst
@Override
public void batchDeregisterService(String serviceName, String groupName, List<Instance> instances)
throws NacosException {
List<Instance> retainInstance = getRetainInstance(serviceName, groupName, instances);
batchRegisterService(serviceName, groupName, retainInstance);
synchronized (redoService.getRegisteredInstances()) {
List<Instance> retainInstance = getRetainInstance(serviceName, groupName, instances);
batchRegisterService(serviceName, groupName, retainInstance);
}
}

/**
Expand Down Expand Up @@ -218,11 +220,20 @@ public void doRegisterService(String serviceName, String groupName, Instance ins

@Override
public void deregisterService(String serviceName, String groupName, Instance instance) throws NacosException {
NAMING_LOGGER
.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}", namespaceId, serviceName,
instance);
redoService.instanceDeregister(serviceName, groupName);
doDeregisterService(serviceName, groupName, instance);
NAMING_LOGGER.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}", namespaceId,
serviceName, instance);
String key = NamingUtils.getGroupedName(serviceName, groupName);
InstanceRedoData instanceRedoData = redoService.getRegisteredInstancesByKey(key);
if (instanceRedoData instanceof BatchInstanceRedoData) {
List<Instance> instances = new ArrayList<>();
if (null != instance) {
instances.add(instance);
}
batchDeregisterService(serviceName, groupName, instances);
} else {
redoService.instanceDeregister(serviceName, groupName);
doDeregisterService(serviceName, groupName, instance);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public NamingGrpcRedoService(NamingGrpcClientProxy clientProxy) {
DEFAULT_REDO_DELAY, TimeUnit.MILLISECONDS);
}

public ConcurrentMap<String, InstanceRedoData> getRegisteredInstances() {
return registeredInstances;
}

public boolean isConnected() {
return connected;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,33 @@ public void testDeregisterService() throws NacosException {
}));
}

@Test
public void testDeregisterServiceForBatchRegistered() throws NacosException {
try {
List<Instance> instanceList = new ArrayList<>();
instance.setHealthy(true);
instanceList.add(instance);
instanceList.add(new Instance());
client.batchRegisterService(SERVICE_NAME, GROUP_NAME, instanceList);
} catch (Exception ignored) {
}
response = new BatchInstanceResponse();
when(this.rpcClient.request(any())).thenReturn(response);
List<Instance> instanceList = new ArrayList<>();
instance.setHealthy(true);
instanceList.add(instance);
client.deregisterService(SERVICE_NAME, GROUP_NAME, instance);
verify(this.rpcClient, times(1)).request(argThat(request -> {
if (request instanceof BatchInstanceRequest) {
BatchInstanceRequest request1 = (BatchInstanceRequest) request;
request1.setRequestId("1");
return request1.getInstances().size() == 1 && request1.getType()
.equals(NamingRemoteConstants.BATCH_REGISTER_INSTANCE);
}
return false;
}));
}

@Test
public void testBatchRegisterService() throws NacosException {
List<Instance> instanceList = new ArrayList<>();
Expand Down

0 comments on commit c7603bb

Please sign in to comment.