This is an automated email from the ASF dual-hosted git repository.
Aias00 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 2916a2fb41 fix(admin): guard getMasterUrl in the zookeeper cluster
implementation (#7385)
2916a2fb41 is described below
commit 2916a2fb41fa11de96e17bb9bd5e653e76b0a9fc
Author: Sean-Walker0 <[email protected]>
AuthorDate: Thu Oct 1 07:06:01 2026 +0800
fix(admin): guard getMasterUrl in the zookeeper cluster implementation
(#7385)
---
.../ClusterSelectMasterServiceZookeeperImpl.java | 17 ++-
...lusterSelectMasterServiceZookeeperImplTest.java | 114 +++++++++++++++++++++
2 files changed, 129 insertions(+), 2 deletions(-)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/zookeeper/ClusterSelectMasterServiceZookeeperImpl.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/zookeeper/ClusterSelectMasterServiceZookeeperImpl.java
index 14f158c224..f65f706a81 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/zookeeper/ClusterSelectMasterServiceZookeeperImpl.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/zookeeper/ClusterSelectMasterServiceZookeeperImpl.java
@@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.integration.zookeeper.lock.ZookeeperLockRegistry;
import java.util.Map;
+import java.util.Objects;
import java.util.concurrent.locks.Lock;
public class ClusterSelectMasterServiceZookeeperImpl implements
ClusterSelectMasterService {
@@ -104,11 +105,23 @@ public class ClusterSelectMasterServiceZookeeperImpl
implements ClusterSelectMas
@Override
public String getMasterUrl() {
+ if (!clusterZookeeperClient.isExist(MASTER_INFO)) {
+ // the info znode is only written by the first successful
selectMaster - same anticipated state the JDBC impl guards
+ return StringUtils.EMPTY;
+ }
String masterInfoJson =
clusterZookeeperClient.getDirectly(MASTER_INFO);
ClusterMasterDTO master = JsonUtils.jsonToObject(masterInfoJson,
ClusterMasterDTO.class);
- if (StringUtils.isEmpty(master.getContextPath())) {
+ if (Objects.isNull(master)) {
+ // the znode exists but carries no readable master info yet (empty
or unparsable content)
+ return StringUtils.EMPTY;
+ }
+ String contextPath = master.getContextPath();
+ if (StringUtils.isEmpty(contextPath)) {
return clusterProperties.getSchema() + "://" +
master.getMasterHost() + ":" + master.getMasterPort();
}
- return clusterProperties.getSchema() + "://" + master.getMasterHost()
+ ":" + master.getMasterPort() + "/" + master.getContextPath();
+ if (contextPath.startsWith("/")) {
+ return clusterProperties.getSchema() + "://" +
master.getMasterHost() + ":" + master.getMasterPort() + contextPath;
+ }
+ return clusterProperties.getSchema() + "://" + master.getMasterHost()
+ ":" + master.getMasterPort() + "/" + contextPath;
}
}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ClusterSelectMasterServiceZookeeperImplTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ClusterSelectMasterServiceZookeeperImplTest.java
new file mode 100644
index 0000000000..e549cd9363
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ClusterSelectMasterServiceZookeeperImplTest.java
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.service;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.admin.config.properties.ClusterProperties;
+import
org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterSelectMasterServiceZookeeperImpl;
+import
org.apache.shenyu.admin.mode.cluster.impl.zookeeper.ClusterZookeeperClient;
+import org.apache.shenyu.common.exception.ShenyuException;
+import org.apache.zookeeper.KeeperException;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+import org.springframework.integration.zookeeper.lock.ZookeeperLockRegistry;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test cases for ClusterSelectMasterServiceZookeeperImpl.
+ */
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
+public final class ClusterSelectMasterServiceZookeeperImplTest {
+
+ private static final String MASTER_INFO =
"/shenyu_cluster_lock/master/info";
+
+ private static final String HOST = "127.0.0.1";
+
+ private static final String PORT = "8080";
+
+ private ClusterSelectMasterServiceZookeeperImpl
zookeeperSelectMasterService;
+
+ @Mock
+ private ClusterProperties clusterProperties;
+
+ @Mock
+ private ZookeeperLockRegistry zookeeperLockRegistry;
+
+ @Mock
+ private ClusterZookeeperClient clusterZookeeperClient;
+
+ @BeforeEach
+ public void setUp() {
+ zookeeperSelectMasterService = new
ClusterSelectMasterServiceZookeeperImpl(clusterProperties,
zookeeperLockRegistry, clusterZookeeperClient);
+ given(clusterProperties.getSchema()).willReturn("http");
+ }
+
+ @Test
+ public void testGetMasterUrlShouldNotThrowWhenMasterInfoNodeIsAbsent() {
+ // a fresh cluster has no master info znode until the first
selectMaster writes it
+ given(clusterZookeeperClient.isExist(MASTER_INFO)).willReturn(false);
+ when(clusterZookeeperClient.getDirectly(MASTER_INFO)).thenThrow(new
ShenyuException(new KeeperException.NoNodeException()));
+
+ assertEquals(StringUtils.EMPTY,
zookeeperSelectMasterService.getMasterUrl());
+ }
+
+ @Test
+ public void testGetMasterUrlShouldNotThrowWhenMasterInfoContentIsEmpty() {
+ // the znode exists but carries no readable master info yet
+ given(clusterZookeeperClient.isExist(MASTER_INFO)).willReturn(true);
+
given(clusterZookeeperClient.getDirectly(MASTER_INFO)).willReturn(StringUtils.EMPTY);
+
+ assertEquals(StringUtils.EMPTY,
zookeeperSelectMasterService.getMasterUrl());
+ }
+
+ @Test
+ public void
testGetMasterUrlShouldNotDoubleTheSlashWhenContextPathStartsWithOne() {
+ given(clusterZookeeperClient.isExist(MASTER_INFO)).willReturn(true);
+
given(clusterZookeeperClient.getDirectly(MASTER_INFO)).willReturn(masterInfoJson("/admin"));
+
+ assertEquals("http://" + HOST + ":" + PORT + "/admin",
zookeeperSelectMasterService.getMasterUrl());
+ }
+
+ @Test
+ public void testGetMasterUrlWithBareContextPath() {
+ given(clusterZookeeperClient.isExist(MASTER_INFO)).willReturn(true);
+
given(clusterZookeeperClient.getDirectly(MASTER_INFO)).willReturn(masterInfoJson("admin"));
+
+ assertEquals("http://" + HOST + ":" + PORT + "/admin",
zookeeperSelectMasterService.getMasterUrl());
+ }
+
+ @Test
+ public void testGetMasterUrlWithoutContextPath() {
+ given(clusterZookeeperClient.isExist(MASTER_INFO)).willReturn(true);
+
given(clusterZookeeperClient.getDirectly(MASTER_INFO)).willReturn(masterInfoJson(StringUtils.EMPTY));
+
+ assertEquals("http://" + HOST + ":" + PORT,
zookeeperSelectMasterService.getMasterUrl());
+ }
+
+ private String masterInfoJson(final String contextPath) {
+ return "{\"masterHost\":\"" + HOST + "\",\"masterPort\":\"" + PORT +
"\",\"contextPath\":\"" + contextPath + "\"}";
+ }
+}