This is an automated email from the ASF dual-hosted git repository.

hengyuss pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 1cadb4b09d fix: update ZooKeeper instance cache when the last child is 
removed (#7372)
1cadb4b09d is described below

commit 1cadb4b09d7cdff47f233ff72d7ff29da1cbef75
Author: lymerin <[email protected]>
AuthorDate: Wed Sep 30 01:07:30 2026 +0800

    fix: update ZooKeeper instance cache when the last child is removed (#7372)
    
    Co-authored-by: hengyuss <[email protected]>
---
 .../zookeeper/ZookeeperInstanceRegisterRepository.java    |  4 +---
 .../ZookeeperInstanceRegisterRepositoryTest.java          | 15 ++++++++++++---
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git 
a/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepository.java
 
b/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepository.java
index 86edd43333..4c87896cb4 100644
--- 
a/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepository.java
+++ 
b/shenyu-registry/shenyu-registry-zookeeper/src/main/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepository.java
@@ -156,9 +156,7 @@ public class ZookeeperInstanceRegisterRepository implements 
ShenyuInstanceRegist
                         String path = Objects.isNull(event.getPath()) ? 
selectKey : event.getPath();
                         List<String> childrenList = 
StringUtils.isNotBlank(path) ? client.subscribeChildrenChanges(path, this)
                                 : Collections.emptyList();
-                        if (!childrenList.isEmpty()) {
-                            watcherInstanceRegisterMap.put(selectKey, 
getInstanceRegisterFun.apply(childrenList));
-                        }
+                        watcherInstanceRegisterMap.put(selectKey, 
getInstanceRegisterFun.apply(childrenList));
                     } catch (Exception e) {
                         watcherInstanceRegisterMap.remove(selectKey);
                         LOGGER.error("zookeeper registry client 
subscribeChildrenChanges watch interrupt error:", e);
diff --git 
a/shenyu-registry/shenyu-registry-zookeeper/src/test/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepositoryTest.java
 
b/shenyu-registry/shenyu-registry-zookeeper/src/test/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepositoryTest.java
index 9fda0407f5..4740c779b0 100644
--- 
a/shenyu-registry/shenyu-registry-zookeeper/src/test/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepositoryTest.java
+++ 
b/shenyu-registry/shenyu-registry-zookeeper/src/test/java/org/apache/shenyu/registry/zookeeper/ZookeeperInstanceRegisterRepositoryTest.java
@@ -39,6 +39,8 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Properties;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.doAnswer;
@@ -82,6 +84,7 @@ public final class ZookeeperInstanceRegisterRepositoryTest {
                 .build();
         final Listenable listenable = mock(Listenable.class);
         final CuratorWatcher[] watcherArr = new CuratorWatcher[1];
+        final boolean[] hasInstance = {true};
 
         try (MockedConstruction<ZookeeperClient> construction = 
mockConstruction(ZookeeperClient.class, (mock, context) -> {
             final CuratorFramework curatorFramework = 
mock(CuratorFramework.class);
@@ -89,7 +92,7 @@ public final class ZookeeperInstanceRegisterRepositoryTest {
             when(mock.subscribeChildrenChanges(anyString(), 
any(CuratorWatcher.class))).thenAnswer(invocation -> {
                 Object[] args = invocation.getArguments();
                 watcherArr[0] = (CuratorWatcher) args[1];
-                return Collections.singletonList("shenyu-test");
+                return hasInstance[0] ? 
Collections.singletonList("shenyu-test") : Collections.emptyList();
             });
             
when(mock.get(anyString())).thenReturn(GsonUtils.getInstance().toJson(data));
             
when(curatorFramework.getConnectionStateListenable()).thenReturn(listenable);
@@ -100,10 +103,16 @@ public final class 
ZookeeperInstanceRegisterRepositoryTest {
             final Properties configProps = config.getProps();
             configProps.setProperty("digest", "digest");
             repository.init(config);
-            
repository.selectInstances(InstancePathConstants.buildInstanceParentPath());
+            String selectKey = InstancePathConstants.buildInstanceParentPath();
+            assertEquals(1, repository.selectInstances(selectKey).size());
             WatchedEvent mockEvent = mock(WatchedEvent.class);
-            
when(mockEvent.getPath()).thenReturn(InstancePathConstants.buildInstanceParentPath());
+            when(mockEvent.getPath()).thenReturn(selectKey);
+            hasInstance[0] = false;
             watcherArr[0].process(mockEvent);
+            assertTrue(repository.selectInstances(selectKey).isEmpty());
+            hasInstance[0] = true;
+            watcherArr[0].process(mockEvent);
+            assertEquals(1, repository.selectInstances(selectKey).size());
             repository.close();
         }
     }

Reply via email to