This is an automated email from the ASF dual-hosted git repository.
xiaoyu 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 0e631d2 Refactor global lock (#8655)
0e631d2 is described below
commit 0e631d2304886c611fdfb89fa60ff0de9abf506a
Author: Haoran Meng <[email protected]>
AuthorDate: Wed Dec 16 19:52:21 2020 +0800
Refactor global lock (#8655)
---
.../metadata/GovernanceMetaDataContexts.java | 7 ++-
.../metadata/GovernanceMetaDataContextsTest.java | 7 +--
.../governance/core/lock/LockCenter.java | 27 ---------
.../core/registry/RegistryCenterNodeStatus.java | 7 +--
.../listener/TerminalStateChangedListener.java | 7 +--
.../governance/core/state/GovernedState.java | 66 ----------------------
.../core/state/GovernedStateContext.java | 25 +++++++-
.../governance/core/lock/LockCenterTest.java | 21 -------
.../listener/TerminalStateChangedListenerTest.java | 13 +----
.../governance/core/state/GovernedStateTest.java | 44 ---------------
.../infra/lock/StandardLockStrategy.java | 2 +-
.../shardingsphere/infra/state/StateContext.java | 21 ++++---
.../infra/state/StateContextTest.java | 14 ++---
13 files changed, 56 insertions(+), 205 deletions(-)
diff --git
a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContexts.java
b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContexts.java
index c648d27..3c831e1 100644
---
a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContexts.java
+++
b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContexts.java
@@ -30,6 +30,7 @@ import
org.apache.shardingsphere.governance.core.event.model.schema.SchemaChange
import org.apache.shardingsphere.governance.core.facade.GovernanceFacade;
import
org.apache.shardingsphere.governance.core.registry.event.DisabledStateChangedEvent;
import
org.apache.shardingsphere.governance.core.registry.schema.GovernanceSchema;
+import org.apache.shardingsphere.governance.core.state.GovernedStateContext;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
@@ -45,6 +46,8 @@ import
org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import
org.apache.shardingsphere.infra.rule.event.impl.DataSourceNameDisabledEvent;
import org.apache.shardingsphere.infra.rule.type.StatusContainedRule;
+import org.apache.shardingsphere.infra.state.StateContext;
+import org.apache.shardingsphere.infra.state.StateType;
import javax.sql.DataSource;
import java.sql.SQLException;
@@ -195,7 +198,9 @@ public final class GovernanceMetaDataContexts implements
MetaDataContexts {
}
metaDataContexts = new StandardMetaDataContexts(newMetaDataMap,
metaDataContexts.getExecutorEngine(), metaDataContexts.getAuthentication(),
metaDataContexts.getProps());
} finally {
- governanceFacade.getLockCenter().unlock();
+ if (StateContext.getCurrentState() == StateType.LOCK) {
+ GovernedStateContext.unlock();
+ }
}
}
diff --git
a/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContextsTest.java
b/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContextsTest.java
index f2f1670..49cd8ed 100644
---
a/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContextsTest.java
+++
b/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContextsTest.java
@@ -26,10 +26,10 @@ import
org.apache.shardingsphere.governance.core.event.model.props.PropertiesCha
import
org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsChangedEvent;
import
org.apache.shardingsphere.governance.core.event.model.schema.SchemaChangedEvent;
import org.apache.shardingsphere.governance.core.facade.GovernanceFacade;
-import org.apache.shardingsphere.governance.core.lock.LockCenter;
import org.apache.shardingsphere.governance.core.registry.RegistryCenter;
import
org.apache.shardingsphere.governance.core.registry.event.DisabledStateChangedEvent;
import
org.apache.shardingsphere.governance.core.registry.schema.GovernanceSchema;
+import org.apache.shardingsphere.governance.core.state.GovernedStateContext;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
@@ -90,9 +90,6 @@ public final class GovernanceMetaDataContextsTest {
@Mock
private ConfigCenter configCenter;
- @Mock
- private LockCenter lockCenter;
-
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ShardingSphereMetaData metaData;
@@ -105,9 +102,9 @@ public final class GovernanceMetaDataContextsTest {
public void setUp() {
when(governanceFacade.getRegistryCenter()).thenReturn(registryCenter);
when(governanceFacade.getConfigCenter()).thenReturn(configCenter);
- when(governanceFacade.getLockCenter()).thenReturn(lockCenter);
when(registryCenter.loadDisabledDataSources("schema")).thenReturn(Collections.singletonList("schema.ds_1"));
governanceMetaDataContexts = new GovernanceMetaDataContexts(new
StandardMetaDataContexts(createMetaDataMap(), mock(ExecutorEngine.class),
authentication, props), governanceFacade);
+ GovernedStateContext.startUp();
}
private Map<String, ShardingSphereMetaData> createMetaDataMap() {
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java
index 2189643..8cb84dd 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/lock/LockCenter.java
@@ -17,19 +17,15 @@
package org.apache.shardingsphere.governance.core.lock;
-import com.google.common.eventbus.Subscribe;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import
org.apache.shardingsphere.governance.core.event.model.lock.GlobalLockAddedEvent;
import org.apache.shardingsphere.governance.core.lock.node.LockNode;
import org.apache.shardingsphere.governance.core.registry.RegistryCenter;
import
org.apache.shardingsphere.governance.core.registry.RegistryCenterNodeStatus;
-import org.apache.shardingsphere.governance.core.state.GovernedState;
import org.apache.shardingsphere.governance.repository.api.RegistryRepository;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import java.util.Collection;
-import java.util.Optional;
import java.util.concurrent.TimeUnit;
/**
@@ -50,8 +46,6 @@ public final class LockCenter {
private final LockNode lockNode = new LockNode();
- private final GovernedState governedState = new GovernedState();
-
/**
* Get lock center instance.
*
@@ -75,27 +69,6 @@ public final class LockCenter {
}
/**
- * Lock instance after global lock added.
- *
- * @param event global lock added event
- */
- @Subscribe
- public synchronized void lock(final GlobalLockAddedEvent event) {
- if (Optional.of(event).isPresent()) {
-
registryCenter.persistInstanceData(governedState.addState(RegistryCenterNodeStatus.LOCKED).toString());
- }
- }
-
- /**
- * Unlock instance.
- */
- public void unlock() {
- if
(governedState.getState().toString().equalsIgnoreCase(RegistryCenterNodeStatus.LOCKED.toString()))
{
-
registryCenter.persistInstanceData(governedState.recoverState().toString());
- }
- }
-
- /**
* Try to get global lock.
*
* @param timeout the maximum time in milliseconds to acquire lock
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNodeStatus.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNodeStatus.java
index 83c5501..4f35613 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNodeStatus.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/registry/RegistryCenterNodeStatus.java
@@ -30,10 +30,5 @@ public enum RegistryCenterNodeStatus {
/**
* Locked state.
*/
- LOCKED,
-
- /**
- * Ok state.
- */
- OK
+ LOCKED
}
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 c59277d..0fe29f5 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,11 +40,6 @@ public final class TerminalStateChangedListener extends
PostGovernanceRepository
@Override
protected Optional<StateEvent> createEvent(final DataChangedEvent event) {
- 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));
+ return Optional.of(new StateEvent(StateType.CIRCUIT_BREAK,
RegistryCenterNodeStatus.DISABLED.toString().equalsIgnoreCase(event.getValue())));
}
}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedState.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedState.java
deleted file mode 100644
index 8778b14..0000000
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedState.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * 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.shardingsphere.governance.core.state;
-
-import
org.apache.shardingsphere.governance.core.registry.RegistryCenterNodeStatus;
-
-import java.util.Deque;
-import java.util.Optional;
-import java.util.concurrent.ConcurrentLinkedDeque;
-
-/**
- * Governed state.
- */
-public final class GovernedState {
-
- private final Deque<RegistryCenterNodeStatus> states = new
ConcurrentLinkedDeque<>();
-
- public GovernedState() {
- states.push(RegistryCenterNodeStatus.OK);
- }
-
- /**
- * Add state to stack and return.
- *
- * @param state state
- * @return current state of instance
- */
- public RegistryCenterNodeStatus addState(final RegistryCenterNodeStatus
state) {
- states.push(state);
- return getState();
- }
-
- /**
- * Get the current state of instance.
- *
- * @return state
- */
- public RegistryCenterNodeStatus getState() {
- return Optional.of(states.peek()).orElse(RegistryCenterNodeStatus.OK);
- }
-
- /**
- * Recover state to the previous and return the current state of instance.
- *
- * @return current state of instance
- */
- public RegistryCenterNodeStatus recoverState() {
- states.pop();
- return getState();
- }
-}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedStateContext.java
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedStateContext.java
index 635373c..d174d03 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedStateContext.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/state/GovernedStateContext.java
@@ -18,9 +18,13 @@
package org.apache.shardingsphere.governance.core.state;
import com.google.common.eventbus.Subscribe;
+import
org.apache.shardingsphere.governance.core.event.model.lock.GlobalLockAddedEvent;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
-import org.apache.shardingsphere.infra.state.StateEvent;
import org.apache.shardingsphere.infra.state.StateContext;
+import org.apache.shardingsphere.infra.state.StateEvent;
+import org.apache.shardingsphere.infra.state.StateType;
+
+import java.util.Optional;
/**
* Governed state machine.
@@ -43,4 +47,23 @@ public final class GovernedStateContext {
public void switchState(final StateEvent event) {
StateContext.switchState(event);
}
+
+ /**
+ * Lock instance after global lock added.
+ *
+ * @param event global lock added event
+ */
+ @Subscribe
+ public void lock(final GlobalLockAddedEvent event) {
+ if (Optional.of(event).isPresent()) {
+ StateContext.switchState(new StateEvent(StateType.LOCK, true));
+ }
+ }
+
+ /**
+ * Unlock instance.
+ */
+ public static void unlock() {
+ StateContext.switchState(new StateEvent(StateType.LOCK, false));
+ }
}
diff --git
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java
index 94b8e40..89d2268 100644
---
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java
+++
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/lock/LockCenterTest.java
@@ -17,12 +17,9 @@
package org.apache.shardingsphere.governance.core.lock;
-import
org.apache.shardingsphere.governance.core.event.model.lock.GlobalLockAddedEvent;
import org.apache.shardingsphere.governance.core.lock.node.LockNode;
import org.apache.shardingsphere.governance.core.registry.RegistryCenter;
-import
org.apache.shardingsphere.governance.core.registry.RegistryCenterNodeStatus;
import org.apache.shardingsphere.governance.repository.api.RegistryRepository;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -51,19 +48,6 @@ public final class LockCenterTest {
}
@Test
- public void assertLock() {
- lockCenter.lock(new GlobalLockAddedEvent());
-
verify(registryCenter).persistInstanceData(RegistryCenterNodeStatus.LOCKED.toString());
- }
-
- @Test
- public void assertUnlock() {
- lockCenter.lock(new GlobalLockAddedEvent());
- lockCenter.unlock();
-
verify(registryCenter).persistInstanceData(RegistryCenterNodeStatus.OK.toString());
- }
-
- @Test
public void assertTryGlobalLock() {
lockCenter.tryGlobalLock(50L);
verify(registryRepository).tryLock(eq(50L), eq(TimeUnit.MILLISECONDS));
@@ -75,9 +59,4 @@ public final class LockCenterTest {
verify(registryRepository).releaseLock();
verify(registryRepository).delete(eq(new
LockNode().getGlobalLockNodePath()));
}
-
- @After
- public void tearDown() {
- lockCenter.unlock();
- }
}
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 6be7274..befa733 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,7 +22,6 @@ 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;
@@ -31,8 +30,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.Optional;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(MockitoJUnitRunner.class)
@@ -52,7 +50,7 @@ public final class TerminalStateChangedListenerTest {
public void assertCreateEventWhenEnabled() {
Optional<StateEvent> actual =
terminalStateChangedListener.createEvent(new DataChangedEvent("/test_ds", "",
Type.UPDATED));
assertTrue(actual.isPresent());
- assertTrue(actual.get().isOn());
+ assertFalse(actual.get().isOn());
}
@Test
@@ -61,11 +59,4 @@ 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-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/state/GovernedStateTest.java
b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/state/GovernedStateTest.java
deleted file mode 100644
index 8950190..0000000
---
a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/state/GovernedStateTest.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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.shardingsphere.governance.core.state;
-
-import
org.apache.shardingsphere.governance.core.registry.RegistryCenterNodeStatus;
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public final class GovernedStateTest {
-
- private final GovernedState governedState = new GovernedState();
-
- @Test
- public void addState() {
- assertThat(governedState.getState(), is(RegistryCenterNodeStatus.OK));
- RegistryCenterNodeStatus state =
governedState.addState(RegistryCenterNodeStatus.LOCKED);
- assertThat(state, is(RegistryCenterNodeStatus.LOCKED));
- }
-
- @Test
- public void recoverState() {
- governedState.addState(RegistryCenterNodeStatus.LOCKED);
- assertThat(governedState.getState(),
is(RegistryCenterNodeStatus.LOCKED));
- assertThat(governedState.recoverState(),
is(RegistryCenterNodeStatus.OK));
- assertThat(governedState.getState(), is(RegistryCenterNodeStatus.OK));
- }
-}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/StandardLockStrategy.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/StandardLockStrategy.java
index 5982361..d962058 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/StandardLockStrategy.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/StandardLockStrategy.java
@@ -49,7 +49,7 @@ public final class StandardLockStrategy implements
LockStrategy {
@Override
public void releaseLock() {
lock.unlock();
- StateContext.switchState(new StateEvent(StateType.OK, true));
+ StateContext.switchState(new StateEvent(StateType.LOCK, false));
}
@Override
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 6d7ecd1..27e6683 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
@@ -21,7 +21,10 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.lock.LockContext;
-import java.util.concurrent.atomic.AtomicReference;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentLinkedDeque;
/**
* State context.
@@ -29,7 +32,7 @@ import java.util.concurrent.atomic.AtomicReference;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class StateContext {
- private static final AtomicReference<StateType> CURRENT_STATE = new
AtomicReference<>(StateType.OK);
+ private static final Deque<StateType> CURRENT_STATE = new
ConcurrentLinkedDeque<>(Collections.singleton(StateType.OK));
/**
* Switch state.
@@ -37,12 +40,10 @@ public final class StateContext {
* @param event state event
*/
public static void switchState(final StateEvent event) {
- if (StateType.CIRCUIT_BREAK == event.getType() && event.isOn()) {
- CURRENT_STATE.set(StateType.CIRCUIT_BREAK);
- } else if (StateType.LOCK == event.getType()) {
- CURRENT_STATE.set(StateType.LOCK);
+ if (event.isOn()) {
+ CURRENT_STATE.push(event.getType());
} else {
- CURRENT_STATE.set(StateType.OK);
+ recoverState();
}
signalAll();
}
@@ -60,6 +61,10 @@ public final class StateContext {
* @return current state
*/
public static StateType getCurrentState() {
- return CURRENT_STATE.get();
+ return Optional.ofNullable(CURRENT_STATE.peek()).orElse(StateType.OK);
+ }
+
+ private static void recoverState() {
+ CURRENT_STATE.pop();
}
}
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 9a1de39..1fc2633 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
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.infra.state;
-import org.junit.After;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
@@ -29,6 +28,7 @@ public final class StateContextTest {
public void assertSwitchStateWithCircuitBreakOn() {
StateContext.switchState(new StateEvent(StateType.CIRCUIT_BREAK,
true));
assertThat(StateContext.getCurrentState(),
is(StateType.CIRCUIT_BREAK));
+ StateContext.switchState(new StateEvent(StateType.CIRCUIT_BREAK,
false));
}
@Test
@@ -38,13 +38,11 @@ public final class StateContextTest {
}
@Test
- public void assertSwitchStateWithLocked() {
- StateContext.switchState(new StateEvent(StateType.LOCK, false));
+ public void assertSwitchStateWithMultiState() {
+ StateContext.switchState(new StateEvent(StateType.CIRCUIT_BREAK,
true));
+ StateContext.switchState(new StateEvent(StateType.LOCK, true));
assertThat(StateContext.getCurrentState(), is(StateType.LOCK));
- }
-
- @After
- public void reset() {
- StateContext.switchState(new StateEvent(StateType.OK, true));
+ StateContext.switchState(new StateEvent(StateType.LOCK, false));
+ assertThat(StateContext.getCurrentState(),
is(StateType.CIRCUIT_BREAK));
}
}