From 8171c90f8297d68830f4320dd6cdc3c0a2fe19a4 Mon Sep 17 00:00:00 2001 From: KomachiSion Date: Mon, 2 Sep 2024 11:39:14 +0800 Subject: [PATCH] For #12526, judge whether cp protocol has init to avoid dead lock to get cp protocol. --- .../alibaba/nacos/core/distributed/ProtocolManager.java | 8 ++++++++ .../alibaba/nacos/naming/cluster/ServerStatusManager.java | 2 +- .../nacos/naming/cluster/ServerStatusManagerTest.java | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java b/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java index af518582dd0..3559cb1fdca 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java @@ -99,6 +99,14 @@ public APProtocol getApProtocol() { return apProtocol; } + public boolean isCpInit() { + return cpInit; + } + + public boolean isApInit() { + return apInit; + } + @PreDestroy @Override public void destroy() { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerStatusManager.java b/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerStatusManager.java index 7494b000fde..012b0383259 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerStatusManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/cluster/ServerStatusManager.java @@ -77,7 +77,7 @@ private boolean isReady() { if (!globalConfig.isDataWarmup()) { return true; } - if (protocolManager.getCpProtocol() == null) { + if (!protocolManager.isCpInit() || protocolManager.getCpProtocol() == null) { return false; } return protocolManager.getCpProtocol().isReady() && distroProtocol.isInitialized(); diff --git a/naming/src/test/java/com/alibaba/nacos/naming/cluster/ServerStatusManagerTest.java b/naming/src/test/java/com/alibaba/nacos/naming/cluster/ServerStatusManagerTest.java index 947c3ee2ac3..c2c5e96bb45 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/cluster/ServerStatusManagerTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/cluster/ServerStatusManagerTest.java @@ -85,6 +85,7 @@ void testGetServerStatus() { @Test void testGetErrorMsgForDistroProtocol() { + when(protocolManager.isCpInit()).thenReturn(true); when(globalConfig.isDataWarmup()).thenReturn(true); when(protocolManager.getCpProtocol()).thenReturn(cpProtocol); when(distroProtocol.isInitialized()).thenReturn(false); @@ -95,6 +96,7 @@ void testGetErrorMsgForDistroProtocol() { @Test void testGetErrorMsgForRaft() { + when(protocolManager.isCpInit()).thenReturn(true); when(globalConfig.isDataWarmup()).thenReturn(true); when(protocolManager.getCpProtocol()).thenReturn(cpProtocol); when(cpProtocol.isReady()).thenReturn(false); @@ -137,6 +139,7 @@ void testUpdaterStatusBySwitch() { @Test void testUpdaterStatusForDistroFailed() { + when(protocolManager.isCpInit()).thenReturn(true); when(globalConfig.isDataWarmup()).thenReturn(true); when(protocolManager.getCpProtocol()).thenReturn(cpProtocol); ServerStatusManager.ServerStatusUpdater updater = serverStatusManager.new ServerStatusUpdater(); @@ -146,6 +149,7 @@ void testUpdaterStatusForDistroFailed() { @Test void testUpdaterStatusForRaftFailed() { + when(protocolManager.isCpInit()).thenReturn(true); when(globalConfig.isDataWarmup()).thenReturn(true); ServerStatusManager.ServerStatusUpdater updater = serverStatusManager.new ServerStatusUpdater(); updater.run(); @@ -158,6 +162,7 @@ void testUpdaterStatusForRaftFailed() { @Test void testUpdaterStatus() { + when(protocolManager.isCpInit()).thenReturn(true); when(globalConfig.isDataWarmup()).thenReturn(true); when(protocolManager.getCpProtocol()).thenReturn(cpProtocol); when(cpProtocol.isReady()).thenReturn(true);