Skip to content

Commit

Permalink
Move ClusterState to mode-core module
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Dec 28, 2024
1 parent 6d7d680 commit c8d87b3
Show file tree
Hide file tree
Showing 21 changed files with 44 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.infra.state.instance.InstanceState;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
Expand Down Expand Up @@ -56,7 +56,7 @@ void assertNewConstructorWithModeConfigurationOnly() throws Exception {
try (ShardingSphereDataSource actual = new ShardingSphereDataSource("foo_db", null)) {
ContextManager contextManager = getContextManager(actual);
assertNotNull(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db"));
assertThat(contextManager.getStateContext().getClusterState(), is(ClusterState.OK));
assertThat(contextManager.getStateContext().getState(), is(ClusterState.OK));
assertThat(contextManager.getComputeNodeInstanceContext().getInstance().getState().getCurrentState(), is(InstanceState.OK));
assertTrue(contextManager.getStorageUnits("foo_db").isEmpty());
}
Expand All @@ -69,7 +69,7 @@ void assertNewConstructorWithAllArguments() throws Exception {
try (ShardingSphereDataSource actual = createShardingSphereDataSource(new MockedDataSource(connection))) {
ContextManager contextManager = getContextManager(actual);
assertNotNull(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db"));
assertThat(contextManager.getStateContext().getClusterState(), is(ClusterState.OK));
assertThat(contextManager.getStateContext().getState(), is(ClusterState.OK));
assertThat(contextManager.getComputeNodeInstanceContext().getInstance().getState().getCurrentState(), is(InstanceState.OK));
assertThat(contextManager.getStorageUnits("foo_db").size(), is(1));
assertThat(contextManager.getStorageUnits("foo_db").get("ds").getDataSource().getConnection().getMetaData().getURL(), is("jdbc:mock://127.0.0.1/foo_ds"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.persist.PersistServiceFacade;
import org.apache.shardingsphere.mode.spi.PersistRepository;
import org.apache.shardingsphere.mode.state.StateContext;
import org.apache.shardingsphere.mode.state.ClusterStateContext;

import java.sql.SQLException;
import java.util.Collections;
Expand All @@ -58,7 +58,7 @@ public final class ContextManager implements AutoCloseable {

private final ExecutorEngine executorEngine;

private final StateContext stateContext;
private final ClusterStateContext stateContext;

private final PersistServiceFacade persistServiceFacade;

Expand All @@ -69,7 +69,7 @@ public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNode
this.computeNodeInstanceContext = computeNodeInstanceContext;
metaDataContextManager = new MetaDataContextManager(this.metaDataContexts, computeNodeInstanceContext, repository);
persistServiceFacade = new PersistServiceFacade(repository, computeNodeInstanceContext.getModeConfiguration(), metaDataContextManager);
stateContext = new StateContext(persistServiceFacade.getStatePersistService().load());
stateContext = new ClusterStateContext(persistServiceFacade.getStatePersistService().load());
executorEngine = ExecutorEngine.createExecutorEngineWithSize(metaDataContexts.getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE));
for (ContextManagerLifecycleListener each : ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class)) {
each.onInitialized(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.google.common.base.Strings;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.metadata.persist.node.StatesNode;
import org.apache.shardingsphere.mode.spi.PersistRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.infra.state.cluster;
package org.apache.shardingsphere.mode.state;

/**
* Cluster state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@

package org.apache.shardingsphere.mode.state;

import org.apache.shardingsphere.infra.state.cluster.ClusterState;

import java.util.concurrent.atomic.AtomicReference;

/**
* State context.
* Cluster state context.
*/
public final class StateContext {
public final class ClusterStateContext {

private final AtomicReference<ClusterState> clusterState;

public StateContext(final ClusterState clusterState) {
public ClusterStateContext(final ClusterState clusterState) {
this.clusterState = new AtomicReference<>(clusterState);
}

Expand All @@ -37,7 +35,7 @@ public StateContext(final ClusterState clusterState) {
*
* @return cluster state
*/
public ClusterState getClusterState() {
public ClusterState getState() {
return clusterState.get();
}

Expand All @@ -46,7 +44,7 @@ public ClusterState getClusterState() {
*
* @param state to be switched cluster state
*/
public void switchClusterState(final ClusterState state) {
public void switchState(final ClusterState state) {
clusterState.set(state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.shardingsphere.mode.persist.service;

import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.mode.spi.PersistRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,24 @@

package org.apache.shardingsphere.mode.state;

import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class StateContextTest {

private final StateContext stateContext = new StateContext(ClusterState.OK);
private final ClusterStateContext stateContext = new ClusterStateContext(ClusterState.OK);

@Test
void assertGetClusterState() {
assertThat(stateContext.getClusterState(), is(ClusterState.OK));
assertThat(stateContext.getState(), is(ClusterState.OK));
}

@Test
void assertSwitchClusterState() {
assertThat(stateContext.getClusterState(), is(ClusterState.OK));
stateContext.switchClusterState(ClusterState.UNAVAILABLE);
assertThat(stateContext.getClusterState(), is(ClusterState.UNAVAILABLE));
assertThat(stateContext.getState(), is(ClusterState.OK));
stateContext.switchState(ClusterState.UNAVAILABLE);
assertThat(stateContext.getState(), is(ClusterState.UNAVAILABLE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type;

import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.metadata.persist.node.StatesNode;
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;

/**
* Cluster state event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public final class StateChangedSubscriber implements DispatchEventSubscriber {
*/
@Subscribe
public synchronized void renew(final ClusterStateEvent event) {
contextManager.getStateContext().switchClusterState(event.getClusterState());
contextManager.getStateContext().switchState(event.getClusterState());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type;

import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type;

import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.cluster.ClusterStateEvent;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -45,6 +45,6 @@ void setUp() {
@Test
void assertRenew() {
subscriber.renew(new ClusterStateEvent(ClusterState.READ_ONLY));
verify(contextManager.getStateContext()).switchClusterState(ClusterState.READ_ONLY);
verify(contextManager.getStateContext()).switchState(ClusterState.READ_ONLY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.distsql.DistSQLStatementContext;
import org.apache.shardingsphere.proxy.backend.handler.admin.DatabaseAdminBackendHandlerFactory;
Expand Down Expand Up @@ -188,7 +188,7 @@ private static void checkUnsupportedSQLStatement(final SQLStatement sqlStatement
}

private static void checkClusterState(final SQLStatement sqlStatement) {
ClusterState clusterCurrentState = ProxyContext.getInstance().getContextManager().getStateContext().getClusterState();
ClusterState clusterCurrentState = ProxyContext.getInstance().getContextManager().getStateContext().getState();
if (ClusterState.OK != clusterCurrentState) {
TypedSPILoader.getService(ProxyClusterState.class, clusterCurrentState.name()).check(sqlStatement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.shardingsphere.infra.exception.core.external.sql.identifier.SQLExceptionIdentifier;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.mode.lock.exception.LockedClusterException;
import org.apache.shardingsphere.mode.lock.global.GlobalLockDefinition;
import org.apache.shardingsphere.mode.manager.ContextManager;
Expand Down Expand Up @@ -56,7 +56,7 @@ public void executeUpdate(final LockClusterStatement sqlStatement, final Context
}

private void checkState(final ContextManager contextManager) {
ShardingSpherePreconditions.checkState(ClusterState.OK == contextManager.getStateContext().getClusterState(), LockedClusterException::new);
ShardingSpherePreconditions.checkState(ClusterState.OK == contextManager.getStateContext().getState(), LockedClusterException::new);
}

private void checkAlgorithm(final LockClusterStatement sqlStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.shardingsphere.distsql.statement.ral.updatable.UnlockClusterStatement;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.mode.lock.exception.NotLockedClusterException;
import org.apache.shardingsphere.mode.lock.global.GlobalLockDefinition;
import org.apache.shardingsphere.mode.manager.ContextManager;
Expand Down Expand Up @@ -52,7 +52,7 @@ public void executeUpdate(final UnlockClusterStatement sqlStatement, final Conte
}

private void checkState(final ContextManager contextManager) {
ShardingSpherePreconditions.checkState(ClusterState.OK != contextManager.getStateContext().getClusterState(), NotLockedClusterException::new);
ShardingSpherePreconditions.checkState(ClusterState.OK != contextManager.getStateContext().getState(), NotLockedClusterException::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.shardingsphere.proxy.backend.lock.impl;

import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.lock.spi.ClusterLockStrategy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.shardingsphere.proxy.backend.lock.impl;

import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.lock.spi.ClusterLockStrategy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
Expand Down Expand Up @@ -73,7 +73,7 @@ void assertInit() {
MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData());
ProxyContext.init(new ContextManager(metaDataContexts, mock(ComputeNodeInstanceContext.class, RETURNS_DEEP_STUBS), mock(PersistRepository.class)));
assertThat(ProxyContext.getInstance().getContextManager().getStateContext(), is(ProxyContext.getInstance().getContextManager().getStateContext()));
assertThat(ProxyContext.getInstance().getContextManager().getStateContext().getClusterState(), is(ClusterState.OK));
assertThat(ProxyContext.getInstance().getContextManager().getStateContext().getState(), is(ClusterState.OK));
assertThat(ProxyContext.getInstance().getContextManager().getMetaDataContexts(), is(ProxyContext.getInstance().getContextManager().getMetaDataContexts()));
assertTrue(ProxyContext.getInstance().getInstanceStateContext().isPresent());
assertThat(ProxyContext.getInstance().getInstanceStateContext(), is(ProxyContext.getInstance().getInstanceStateContext()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
Expand Down Expand Up @@ -100,7 +100,7 @@ void setUp() {
when(databaseConnectionManager.getConnectionSession()).thenReturn(connectionSession);
when(connectionSession.getDatabaseConnectionManager()).thenReturn(databaseConnectionManager);
ContextManager contextManager = mockContextManager();
when(contextManager.getStateContext().getClusterState()).thenReturn(ClusterState.OK);
when(contextManager.getStateContext().getState()).thenReturn(ClusterState.OK);
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;
import org.apache.shardingsphere.distsql.statement.ral.updatable.LockClusterStatement;
import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.mode.state.ClusterState;
import org.apache.shardingsphere.mode.lock.exception.LockedClusterException;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
Expand All @@ -46,21 +46,21 @@ class LockClusterExecutorTest {
@Test
void assertExecuteUpdateWithLockedCluster() {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(contextManager.getStateContext().getClusterState()).thenReturn(ClusterState.UNAVAILABLE);
when(contextManager.getStateContext().getState()).thenReturn(ClusterState.UNAVAILABLE);
assertThrows(LockedClusterException.class, () -> executor.executeUpdate(new LockClusterStatement(new AlgorithmSegment("FOO", new Properties()), null), contextManager));
}

@Test
void assertExecuteUpdateWithWrongAlgorithm() {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(contextManager.getStateContext().getClusterState()).thenReturn(ClusterState.OK);
when(contextManager.getStateContext().getState()).thenReturn(ClusterState.OK);
assertThrows(ServiceProviderNotFoundException.class, () -> executor.executeUpdate(new LockClusterStatement(new AlgorithmSegment("FOO", new Properties()), null), contextManager));
}

@Test
void assertExecuteUpdateWithUsingTimeout() {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(contextManager.getStateContext().getClusterState()).thenReturn(ClusterState.OK);
when(contextManager.getStateContext().getState()).thenReturn(ClusterState.OK);
assertDoesNotThrow(() -> executor.executeUpdate(new LockClusterStatement(new AlgorithmSegment("WRITE", new Properties()), 2000L), contextManager));
}
}
Loading

0 comments on commit c8d87b3

Please sign in to comment.