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

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


The following commit(s) were added to refs/heads/master by this push:
     new df30bbada9a Refactor ClusterStateContext (#31249)
df30bbada9a is described below

commit df30bbada9a046fa05591f4dfc5f8e4adaf5ed7f
Author: Liang Zhang <[email protected]>
AuthorDate: Thu May 16 19:36:16 2024 +0800

    Refactor ClusterStateContext (#31249)
    
    * Refactor ClusterStateContext
    
    * Refactor ClusterStateContext
---
 .../infra/state/cluster/ClusterStateContext.java   | 27 +++++++++++-----------
 .../infra/state/ClusterStateContextTest.java       | 22 ++----------------
 2 files changed, 16 insertions(+), 33 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/cluster/ClusterStateContext.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/cluster/ClusterStateContext.java
index 2ef2f2d3d7c..06c9e736e87 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/state/cluster/ClusterStateContext.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/state/cluster/ClusterStateContext.java
@@ -17,29 +17,30 @@
 
 package org.apache.shardingsphere.infra.state.cluster;
 
-import lombok.Getter;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * Cluster state context.
  */
-@Getter
 public final class ClusterStateContext {
     
-    private ClusterState currentState = ClusterState.OK;
+    private final AtomicReference<ClusterState> currentState = new 
AtomicReference<>(ClusterState.OK);
     
     /**
-     * Switch state.
+     * Get current cluster state.
      * 
-     * @param state state
-     * @throws IllegalStateException illegal state exception
+     * @return current cluster state
+     */
+    public ClusterState getCurrentState() {
+        return currentState.get();
+    }
+    
+    /**
+     * Switch current cluster state.
+     * 
+     * @param state to be switched cluster state
      */
     public void switchState(final ClusterState state) {
-        if (currentState == state) {
-            return;
-        }
-        if (ClusterState.OK != currentState && ClusterState.OK != state) {
-            throw new IllegalStateException("Cluster is locked");
-        }
-        currentState = state;
+        currentState.set(state);
     }
 }
diff --git 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/state/ClusterStateContextTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/state/ClusterStateContextTest.java
index e23f807c541..e8a947afc78 100644
--- 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/state/ClusterStateContextTest.java
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/state/ClusterStateContextTest.java
@@ -23,33 +23,15 @@ import org.junit.jupiter.api.Test;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertThrows;
 
 class ClusterStateContextTest {
     
     private final ClusterStateContext clusterStateContext = new 
ClusterStateContext();
     
     @Test
-    void assertSwitchStateWithUnavailable() {
+    void assertSwitchState() {
+        assertThat(clusterStateContext.getCurrentState(), is(ClusterState.OK));
         clusterStateContext.switchState(ClusterState.UNAVAILABLE);
         assertThat(clusterStateContext.getCurrentState(), 
is(ClusterState.UNAVAILABLE));
-        clusterStateContext.switchState(ClusterState.OK);
-    }
-    
-    @Test
-    void assertSwitchStateWithReadOnly() {
-        clusterStateContext.switchState(ClusterState.READ_ONLY);
-        assertThat(clusterStateContext.getCurrentState(), 
is(ClusterState.READ_ONLY));
-        clusterStateContext.switchState(ClusterState.OK);
-    }
-    
-    @Test
-    void assertSwitchStateWithMultiStateChange() {
-        clusterStateContext.switchState(ClusterState.UNAVAILABLE);
-        assertThrows(IllegalStateException.class, () -> 
clusterStateContext.switchState(ClusterState.READ_ONLY));
-        clusterStateContext.switchState(ClusterState.OK);
-        clusterStateContext.switchState(ClusterState.READ_ONLY);
-        assertThat(clusterStateContext.getCurrentState(), 
is(ClusterState.READ_ONLY));
-        clusterStateContext.switchState(ClusterState.OK);
     }
 }

Reply via email to