From 30954dc612209e0f77ec68c2061d7154fff60c5a Mon Sep 17 00:00:00 2001 From: MajorHe1 <53824889+MajorHe1@users.noreply.github.com> Date: Wed, 8 Feb 2023 09:38:37 +0800 Subject: [PATCH] [ISSUE #9816] check expected final state before remove redo data. (#9907) * [ISSUE #9816] check expected final state before remove redo data. * check expected final state before remove subscriber redo data. * update NamingGrpcRedoServiceTest when test remove * update NamingGrpcRedoService, set expect register status to false when subscribers deregister --- .../remote/gprc/redo/NamingGrpcRedoService.java | 13 +++++++++++-- .../remote/gprc/redo/NamingGrpcRedoServiceTest.java | 2 ++ 2 files changed, 13 insertions(+), 2 deletions(-) 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()); }