You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/12/01 10:22:51 UTC

[shardingsphere] branch master updated: Perfect proxy state transition (#8447)

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

zhangliang 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 2a6eea4  Perfect proxy state transition (#8447)
2a6eea4 is described below

commit 2a6eea49f6371134d330bd6ef54c1321e843b15b
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Tue Dec 1 18:22:29 2020 +0800

    Perfect proxy state transition (#8447)
    
    * Perfect proxy state transition
    
    * Add unit tests
---
 .../registry/listener/TerminalStateChangedListener.java     |  7 ++++++-
 .../registry/listener/TerminalStateChangedListenerTest.java | 13 +++++++++++--
 .../org/apache/shardingsphere/infra/state/StateContext.java |  5 ++++-
 .../apache/shardingsphere/infra/state/StateContextTest.java |  6 ++++++
 4 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/listener/TerminalStateChangedListener.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/listener/TerminalStateChangedListener.java
index 0fe29f5..c59277d 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/listener/TerminalStateChangedListener.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/listener/TerminalStateChangedListener.java
@@ -40,6 +40,11 @@ public final class TerminalStateChangedListener extends PostGovernanceRepository
     
     @Override
     protected Optional<StateEvent> createEvent(final DataChangedEvent event) {
-        return Optional.of(new StateEvent(StateType.CIRCUIT_BREAK, RegistryCenterNodeStatus.DISABLED.toString().equalsIgnoreCase(event.getValue())));
+        if (RegistryCenterNodeStatus.DISABLED.toString().equalsIgnoreCase(event.getValue())) {
+            return Optional.of(new StateEvent(StateType.CIRCUIT_BREAK, true));
+        } else if (RegistryCenterNodeStatus.LOCKED.toString().equalsIgnoreCase(event.getValue())) {
+            return Optional.of(new StateEvent(StateType.LOCK, true));
+        }
+        return Optional.of(new StateEvent(StateType.OK, true));
     }
 }
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/listener/TerminalStateChangedListenerTest.java b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/listener/TerminalStateChangedListenerTest.java
index befa733..6be7274 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/listener/TerminalStateChangedListenerTest.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/listener/TerminalStateChangedListenerTest.java
@@ -22,6 +22,7 @@ import org.apache.shardingsphere.governance.repository.api.RegistryRepository;
 import org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent;
 import org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent.Type;
 import org.apache.shardingsphere.infra.state.StateEvent;
+import org.apache.shardingsphere.infra.state.StateType;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -30,7 +31,8 @@ import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Optional;
 
-import static org.junit.Assert.assertFalse;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -50,7 +52,7 @@ public final class TerminalStateChangedListenerTest {
     public void assertCreateEventWhenEnabled() {
         Optional<StateEvent> actual = terminalStateChangedListener.createEvent(new DataChangedEvent("/test_ds", "", Type.UPDATED));
         assertTrue(actual.isPresent());
-        assertFalse(actual.get().isOn());
+        assertTrue(actual.get().isOn());
     }
     
     @Test
@@ -59,4 +61,11 @@ public final class TerminalStateChangedListenerTest {
         assertTrue(actual.isPresent());
         assertTrue(actual.get().isOn());
     }
+    
+    @Test
+    public void assertCreateEventWhenLocked() {
+        Optional<StateEvent> actual = terminalStateChangedListener.createEvent(new DataChangedEvent("/test_ds", RegistryCenterNodeStatus.LOCKED.name(), Type.UPDATED));
+        assertTrue(actual.isPresent());
+        assertThat(actual.get().getType(), is(StateType.LOCK));
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java
index 72a8cbb..4948aef 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/state/StateContext.java
@@ -40,7 +40,10 @@ public final class StateContext {
             CURRENT_STATE.set(StateType.CIRCUIT_BREAK);
             return;
         }
-        // TODO check lock state
+        if (StateType.LOCK == event.getType()) {
+            CURRENT_STATE.set(StateType.LOCK);
+            return;
+        }
         CURRENT_STATE.set(StateType.OK);
     }
     
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/state/StateContextTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/state/StateContextTest.java
index db018e7..9a1de39 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/state/StateContextTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/state/StateContextTest.java
@@ -37,6 +37,12 @@ public final class StateContextTest {
         assertThat(StateContext.getCurrentState(), is(StateType.OK));
     }
     
+    @Test
+    public void assertSwitchStateWithLocked() {
+        StateContext.switchState(new StateEvent(StateType.LOCK, false));
+        assertThat(StateContext.getCurrentState(), is(StateType.LOCK));
+    }
+    
     @After
     public void reset() {
         StateContext.switchState(new StateEvent(StateType.OK, true));