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 3444fa06b1 fix(admin): null-guard getMasterUrl when the master row is 
not persisted yet (#7334)
3444fa06b1 is described below

commit 3444fa06b135af67958064a2339dbd0d1cab3689
Author: Sean-Walker0 <[email protected]>
AuthorDate: Wed Oct 14 09:35:00 2026 +0800

    fix(admin): null-guard getMasterUrl when the master row is not persisted 
yet (#7334)
    
    * fix(admin): null-guard getMasterUrl when the master row is not persisted 
yet
    
    getMasterUrl dereferenced clusterMasterMapper.selectById(MASTER_ID)
    without a null check, but the row is only inserted by the first
    successful selectMaster, so a slave node that serves a RUNNING_MODE
    websocket request before any master has persisted its row crashes with
    NullPointerException inside the response assembly (WebsocketCollector
    calls getMasterUrl on every such request). The sibling method directly
    above, getMaster(), guards exactly this lookup and returns an empty DTO
    - proving the missing row is an anticipated state. Mirror that guard
    and return an empty string, which the only caller already tolerates
    (its replace/concat chain requires a non-null value).
    
    The new test fails on current master with the exact NPE and passes
    with this change.
    
    * ci: retrigger checks (e2e family hit the known httpbin/cluster flakes)
    
    * ci: retrigger the flaky e2e jobs (websocket reconnect window / teardown 
ordering)
    
    ---------
    
    Co-authored-by: Sean-Walker0 
<[email protected]>
    Co-authored-by: aias00 <[email protected]>
---
 .../cluster/impl/jdbc/ClusterSelectMasterServiceJdbcImpl.java  |  4 ++++
 .../admin/service/ClusterSelectMasterServiceJdbcImplTest.java  | 10 ++++++++++
 2 files changed, 14 insertions(+)

diff --git 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/jdbc/ClusterSelectMasterServiceJdbcImpl.java
 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/jdbc/ClusterSelectMasterServiceJdbcImpl.java
index 67ccece4c3..38b8aa34a5 100644
--- 
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/jdbc/ClusterSelectMasterServiceJdbcImpl.java
+++ 
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mode/cluster/impl/jdbc/ClusterSelectMasterServiceJdbcImpl.java
@@ -131,6 +131,10 @@ public class ClusterSelectMasterServiceJdbcImpl implements 
ClusterSelectMasterSe
     @Override
     public String getMasterUrl() {
         ClusterMasterDO master = clusterMasterMapper.selectById(MASTER_ID);
+        if (Objects.isNull(master)) {
+            // same anticipated state getMaster() guards: no row until the 
first selectMaster persists it
+            return StringUtils.EMPTY;
+        }
         String contextPath = master.getContextPath();
         if (StringUtils.isEmpty(contextPath)) {
             return clusterProperties.getSchema() + "://" + 
master.getMasterHost() + ":" + master.getMasterPort();
diff --git 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ClusterSelectMasterServiceJdbcImplTest.java
 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ClusterSelectMasterServiceJdbcImplTest.java
index 104e122bc8..49c96907f8 100644
--- 
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ClusterSelectMasterServiceJdbcImplTest.java
+++ 
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ClusterSelectMasterServiceJdbcImplTest.java
@@ -33,6 +33,8 @@ import org.mockito.quality.Strictness;
 import org.springframework.integration.jdbc.lock.JdbcLockRegistry;
 
 import java.lang.reflect.Field;
+import org.apache.commons.lang3.StringUtils;
+
 import java.util.concurrent.locks.Lock;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -103,6 +105,14 @@ public final class ClusterSelectMasterServiceJdbcImplTest {
         
     }
     
+    @Test
+    void testGetMasterUrlShouldNotThrowWhenMasterRowIsAbsent() {
+        // before the first successful selectMaster persists row 1, a slave 
node reads no master row
+        given(clusterMasterMapper.selectById(any())).willReturn(null);
+
+        assertEquals(StringUtils.EMPTY, 
clusterSelectMasterServiceJdbc.getMasterUrl());
+    }
+
     private ClusterMasterDO buildClusterMasterDO() {
         ClusterMasterDO clusterMasterDO = new ClusterMasterDO();
         clusterMasterDO.setId("1");

Reply via email to