diff --git a/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/redo/NamingGrpcRedoService.java b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/redo/NamingGrpcRedoService.java index de9af3242..1b2caf9a9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/redo/NamingGrpcRedoService.java +++ b/client/src/main/java/com/alibaba/nacos/client/naming/remote/gprc/redo/NamingGrpcRedoService.java @@ -177,8 +177,12 @@ public class NamingGrpcRedoService implements ConnectionEventListener { * @param groupName group name */ public void removeInstanceForRedo(String serviceName, String groupName) { + String key = NamingUtils.getGroupedName(serviceName, groupName); synchronized (registeredInstances) { - registeredInstances.remove(NamingUtils.getGroupedName(serviceName, groupName)); + InstanceRedoData redoData = registeredInstances.get(key); + if (null != redoData && !redoData.isExpectedRegistered()) { + registeredInstances.remove(key); + } } } @@ -244,6 +248,7 @@ public class NamingGrpcRedoService implements ConnectionEventListener { SubscriberRedoData redoData = subscribes.get(key); if (null != redoData) { redoData.setUnregistering(true); + redoData.setExpectedRegistered(false); } } } @@ -272,8 +277,12 @@ public class NamingGrpcRedoService implements ConnectionEventListener { * @param cluster cluster */ public void removeSubscriberForRedo(String serviceName, String groupName, String cluster) { + String key = ServiceInfo.getKey(NamingUtils.getGroupedName(serviceName, groupName), cluster); synchronized (subscribes) { - subscribes.remove(ServiceInfo.getKey(NamingUtils.getGroupedName(serviceName, groupName), cluster)); + SubscriberRedoData redoData = subscribes.get(key); + if (null != redoData && !redoData.isExpectedRegistered()) { + subscribes.remove(key); + } } } diff --git a/client/src/test/java/com/alibaba/nacos/client/naming/remote/gprc/redo/NamingGrpcRedoServiceTest.java b/client/src/test/java/com/alibaba/nacos/client/naming/remote/gprc/redo/NamingGrpcRedoServiceTest.java index 3d2030f99..32a8b1f5b 100644 --- a/client/src/test/java/com/alibaba/nacos/client/naming/remote/gprc/redo/NamingGrpcRedoServiceTest.java +++ b/client/src/test/java/com/alibaba/nacos/client/naming/remote/gprc/redo/NamingGrpcRedoServiceTest.java @@ -156,6 +156,7 @@ public class NamingGrpcRedoServiceTest { assertTrue(registeredInstances.isEmpty()); redoService.cacheInstanceForRedo(SERVICE, GROUP, new Instance()); assertFalse(registeredInstances.isEmpty()); + redoService.instanceDeregister(SERVICE, GROUP); redoService.removeInstanceForRedo(SERVICE, GROUP); assertTrue(registeredInstances.isEmpty()); } @@ -221,6 +222,7 @@ public class NamingGrpcRedoServiceTest { assertTrue(subscribes.isEmpty()); redoService.cacheSubscriberForRedo(SERVICE, GROUP, CLUSTER); assertFalse(subscribes.isEmpty()); + redoService.subscriberDeregister(SERVICE, GROUP, CLUSTER); redoService.removeSubscriberForRedo(SERVICE, GROUP, CLUSTER); assertTrue(subscribes.isEmpty()); }