Skip to content

Commit

Permalink
Rename ContextServiceFacade to PersistServiceFacade and move StatePer…
Browse files Browse the repository at this point in the history
…sistService to PersistServiceFacade (#31343)
  • Loading branch information
menghaoranss authored May 22, 2024
1 parent 8ff6b2a commit 1a5c2cf
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.state.StateContext;
import org.apache.shardingsphere.mode.state.StateService;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -68,7 +67,7 @@ void setUp() {
mock(MetaDataPersistService.class), new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties())));
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
when(contextManager.getComputeNodeInstanceContext().getInstance().getState()).thenReturn(new InstanceStateContext());
when(contextManager.getStateContext()).thenReturn(new StateContext(mock(StateService.class)));
when(contextManager.getStateContext()).thenReturn(new StateContext());
}

private Map<String, ShardingSphereDatabase> mockDatabases() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@
import org.apache.shardingsphere.mode.manager.switcher.ResourceSwitchManager;
import org.apache.shardingsphere.mode.manager.switcher.SwitchingResource;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.service.ContextServiceFacade;
import org.apache.shardingsphere.mode.service.PersistServiceFacade;
import org.apache.shardingsphere.mode.state.StateContext;
import org.apache.shardingsphere.mode.state.StateService;

import java.sql.SQLException;
import java.util.Collection;
Expand Down Expand Up @@ -75,7 +74,7 @@ public final class ContextManager implements AutoCloseable {

private final StateContext stateContext;

private final ContextServiceFacade contextServiceFacade;
private final PersistServiceFacade persistServiceFacade;

public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNodeInstanceContext computeNodeInstanceContext) {
this.metaDataContexts = new AtomicReference<>(metaDataContexts);
Expand All @@ -84,8 +83,8 @@ public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNode
configurationContextManager = new ConfigurationContextManager(this.metaDataContexts, computeNodeInstanceContext);
resourceMetaDataContextManager = new ResourceMetaDataContextManager(this.metaDataContexts);
executorEngine = ExecutorEngine.createExecutorEngineWithSize(metaDataContexts.getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE));
stateContext = new StateContext(new StateService(metaDataContexts.getPersistService().getRepository()));
contextServiceFacade = new ContextServiceFacade(metaDataContexts.getPersistService().getRepository());
stateContext = new StateContext();
persistServiceFacade = new PersistServiceFacade(metaDataContexts.getPersistService().getRepository());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
import java.util.Optional;

/**
* Compute node status service.
* Compute node persist service.
*/
@RequiredArgsConstructor
@Slf4j
public final class ComputeNodeService {
public final class ComputeNodePersistService {

private final PersistRepository repository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@

import lombok.Getter;
import org.apache.shardingsphere.mode.spi.PersistRepository;
import org.apache.shardingsphere.mode.state.StatePersistService;

/**
* Context service facade.
* Persist service facade.
*/
@Getter
public final class ContextServiceFacade {
public final class PersistServiceFacade {

private final ComputeNodeService computeNodeService;
private final ComputeNodePersistService computeNodePersistService;

public ContextServiceFacade(final PersistRepository repository) {
computeNodeService = new ComputeNodeService(repository);
private final StatePersistService statePersistService;

public PersistServiceFacade(final PersistRepository repository) {
computeNodePersistService = new ComputeNodePersistService(repository);
statePersistService = new StatePersistService(repository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@

package org.apache.shardingsphere.mode.state;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import java.util.concurrent.atomic.AtomicReference;

/**
* State context.
*/
@RequiredArgsConstructor
public final class StateContext {

private final AtomicReference<ClusterState> currentClusterState = new AtomicReference<>(ClusterState.OK);

@Getter
private final StateService stateService;

/**
* Get current cluster state.
*
Expand All @@ -50,13 +44,4 @@ public ClusterState getCurrentClusterState() {
public void switchCurrentClusterState(final ClusterState state) {
currentClusterState.set(state);
}

/**
* Update cluster state.
*
* @param state cluster state
*/
public void updateClusterState(final ClusterState state) {
stateService.persist(state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
import java.util.Optional;

/**
* State service.
* State persist service.
*/
@RequiredArgsConstructor
public final class StateService {
public final class StatePersistService {

private final PersistRepository repository;

/**
* Persist cluster state.
* Update cluster state.
*
* @param state cluster state
*/
public void persist(final ClusterState state) {
public void updateClusterState(final ClusterState state) {
repository.persist(ComputeNode.getClusterStateNodePath(), state.name());
}

Expand All @@ -47,7 +47,7 @@ public void persist(final ClusterState state) {
*
* @return cluster state
*/
public Optional<ClusterState> load() {
public Optional<ClusterState> loadClusterState() {
String value = repository.getDirectly(ComputeNode.getClusterStateNodePath());
return Strings.isNullOrEmpty(value) ? Optional.empty() : Optional.of(ClusterState.valueOf(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class ComputeNodeServiceTest {
class ComputeNodePersistServiceTest {

@Mock
private PersistRepository repository;
Expand All @@ -56,52 +56,52 @@ class ComputeNodeServiceTest {
void assertRegisterOnline() {
ComputeNodeInstance computeNodeInstance = new ComputeNodeInstance(new ProxyInstanceMetaData("foo_instance_id", 3307));
computeNodeInstance.getLabels().add("test");
new ComputeNodeService(repository).registerOnline(computeNodeInstance);
new ComputeNodePersistService(repository).registerOnline(computeNodeInstance);
verify(repository).persistEphemeral(eq("/nodes/compute_nodes/online/proxy/" + computeNodeInstance.getMetaData().getId()), anyString());
verify(repository).persistEphemeral(ComputeNode.getComputeNodeStateNodePath(computeNodeInstance.getMetaData().getId()), InstanceState.OK.name());
verify(repository).persistEphemeral(ComputeNode.getInstanceLabelsNodePath(computeNodeInstance.getMetaData().getId()), YamlEngine.marshal(Collections.singletonList("test")));
}

@Test
void assertPersistInstanceLabels() {
ComputeNodeService computeNodeService = new ComputeNodeService(repository);
ComputeNodePersistService computeNodePersistService = new ComputeNodePersistService(repository);
InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3307);
final String instanceId = instanceMetaData.getId();
computeNodeService.persistInstanceLabels(instanceId, Collections.singletonList("test"));
computeNodePersistService.persistInstanceLabels(instanceId, Collections.singletonList("test"));
verify(repository).persistEphemeral(ComputeNode.getInstanceLabelsNodePath(instanceId), YamlEngine.marshal(Collections.singletonList("test")));
computeNodeService.persistInstanceLabels(instanceId, Collections.emptyList());
computeNodePersistService.persistInstanceLabels(instanceId, Collections.emptyList());
verify(repository).persistEphemeral(ComputeNode.getInstanceLabelsNodePath(instanceId), YamlEngine.marshal(Collections.emptyList()));
}

@Test
void assertPersistInstanceWorkerId() {
InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3307);
final String instanceId = instanceMetaData.getId();
new ComputeNodeService(repository).persistInstanceWorkerId(instanceId, 100);
new ComputeNodePersistService(repository).persistInstanceWorkerId(instanceId, 100);
verify(repository).persistEphemeral(ComputeNode.getInstanceWorkerIdNodePath(instanceId), String.valueOf(100));
}

@Test
void assertLoadInstanceLabels() {
InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3307);
final String instanceId = instanceMetaData.getId();
new ComputeNodeService(repository).loadInstanceLabels(instanceId);
new ComputeNodePersistService(repository).loadInstanceLabels(instanceId);
verify(repository).getDirectly(ComputeNode.getInstanceLabelsNodePath(instanceId));
}

@Test
void assertLoadComputeNodeState() {
InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3307);
final String instanceId = instanceMetaData.getId();
new ComputeNodeService(repository).loadComputeNodeState(instanceId);
new ComputeNodePersistService(repository).loadComputeNodeState(instanceId);
verify(repository).getDirectly(ComputeNode.getComputeNodeStateNodePath(instanceId));
}

@Test
void assertLoadInstanceWorkerId() {
InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3307);
final String instanceId = instanceMetaData.getId();
new ComputeNodeService(repository).loadInstanceWorkerId(instanceId);
new ComputeNodePersistService(repository).loadInstanceWorkerId(instanceId);
verify(repository).getDirectly(ComputeNode.getInstanceWorkerIdNodePath(instanceId));
}

Expand All @@ -117,7 +117,7 @@ void assertLoadAllComputeNodeInstances() {
yamlComputeNodeData1.setAttribute("127.0.0.1@3308");
yamlComputeNodeData1.setVersion("foo_version");
when(repository.getDirectly("/nodes/compute_nodes/online/proxy/foo_instance_3308")).thenReturn(YamlEngine.marshal(yamlComputeNodeData1));
List<ComputeNodeInstance> actual = new ArrayList<>(new ComputeNodeService(repository).loadAllComputeNodeInstances());
List<ComputeNodeInstance> actual = new ArrayList<>(new ComputeNodePersistService(repository).loadAllComputeNodeInstances());
assertThat(actual.size(), is(2));
assertThat(actual.get(0).getMetaData().getId(), is("foo_instance_3307"));
assertThat(actual.get(0).getMetaData().getIp(), is(IpUtils.getIp()));
Expand All @@ -130,19 +130,19 @@ void assertLoadAllComputeNodeInstances() {
@Test
void assertLoadComputeNodeInstance() {
InstanceMetaData instanceMetaData = new ProxyInstanceMetaData("foo_instance_id", 3307);
ComputeNodeInstance actual = new ComputeNodeService(repository).loadComputeNodeInstance(instanceMetaData);
ComputeNodeInstance actual = new ComputeNodePersistService(repository).loadComputeNodeInstance(instanceMetaData);
assertThat(actual.getMetaData(), is(instanceMetaData));
}

@Test
void assertGetUsedWorkerIds() {
new ComputeNodeService(repository).getAssignedWorkerIds();
new ComputeNodePersistService(repository).getAssignedWorkerIds();
verify(repository).getChildrenKeys(ComputeNode.getInstanceWorkerIdRootNodePath());
}

@Test
void assertUpdateComputeNodeState() {
new ComputeNodeService(repository).updateComputeNodeState("foo_instance_id", InstanceState.OK);
new ComputeNodePersistService(repository).updateComputeNodeState("foo_instance_id", InstanceState.OK);
verify(repository).persistEphemeral(ComputeNode.getComputeNodeStateNodePath("foo_instance_id"), InstanceState.OK.name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

class StateContextTest {

private final StateContext stateContext = new StateContext(mock(StateService.class));
private final StateContext stateContext = new StateContext();

@Test
void assertGetCurrentClusterState() {
Expand All @@ -40,10 +38,4 @@ void assertSwitchCurrentClusterState() {
stateContext.switchCurrentClusterState(ClusterState.UNAVAILABLE);
assertThat(stateContext.getCurrentClusterState(), is(ClusterState.UNAVAILABLE));
}

@Test
void assertUpdateClusterState() {
stateContext.updateClusterState(ClusterState.OK);
verify(stateContext.getStateService()).persist(ClusterState.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
class StateServiceTest {
class StatePersistServiceTest {

@Mock
private PersistRepository repository;

@Test
void assertPersistClusterStateWithoutPath() {
StateService stateService = new StateService(repository);
stateService.persist(ClusterState.OK);
void assertUpdateClusterStateClusterStateWithoutPath() {
StatePersistService statePersistService = new StatePersistService(repository);
statePersistService.updateClusterState(ClusterState.OK);
verify(repository).persist(ComputeNode.getClusterStateNodePath(), ClusterState.OK.name());
}

@Test
void assertLoadClusterState() {
new StateService(repository).load();
void assertLoadClusterStateClusterState() {
new StatePersistService(repository).loadClusterState();
verify(repository).getDirectly(ComputeNode.getClusterStateNodePath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
import org.apache.shardingsphere.mode.repository.cluster.lock.holder.DistributedLockHolder;
import org.apache.shardingsphere.mode.repository.cluster.lock.impl.props.DefaultLockTypedProperties;
import org.apache.shardingsphere.mode.state.StateService;
import org.apache.shardingsphere.mode.state.StatePersistService;
import org.apache.shardingsphere.mode.storage.service.QualifiedDataSourceStatusService;

import java.sql.SQLException;
Expand Down Expand Up @@ -109,23 +109,23 @@ private void createSubscribers(final EventBusContext eventBusContext, final Clus

private void registerOnline(final EventBusContext eventBusContext, final ComputeNodeInstanceContext computeNodeInstanceContext,
final ClusterPersistRepository repository, final ContextManagerBuilderParameter param, final ContextManager contextManager) {
contextManager.getContextServiceFacade().getComputeNodeService().registerOnline(computeNodeInstanceContext.getInstance());
contextManager.getPersistServiceFacade().getComputeNodePersistService().registerOnline(computeNodeInstanceContext.getInstance());
new GovernanceWatcherFactory(repository,
eventBusContext, param.getInstanceMetaData() instanceof JDBCInstanceMetaData ? param.getDatabaseConfigs().keySet() : Collections.emptyList()).watchListeners();
if (null != param.getLabels()) {
contextManager.getComputeNodeInstanceContext().getInstance().getLabels().addAll(param.getLabels());
}
contextManager.getComputeNodeInstanceContext().getAllClusterInstances().addAll(contextManager.getContextServiceFacade().getComputeNodeService().loadAllComputeNodeInstances());
contextManager.getComputeNodeInstanceContext().getAllClusterInstances().addAll(contextManager.getPersistServiceFacade().getComputeNodePersistService().loadAllComputeNodeInstances());
new ClusterEventSubscriberRegistry(contextManager, repository).register();
}

private void setClusterState(final ContextManager contextManager) {
StateService stateService = contextManager.getStateContext().getStateService();
Optional<ClusterState> clusterState = stateService.load();
StatePersistService statePersistService = contextManager.getPersistServiceFacade().getStatePersistService();
Optional<ClusterState> clusterState = statePersistService.loadClusterState();
if (clusterState.isPresent()) {
contextManager.getStateContext().switchCurrentClusterState(clusterState.get());
} else {
stateService.persist(ClusterState.OK);
statePersistService.updateClusterState(ClusterState.OK);
}
}

Expand Down
Loading

0 comments on commit 1a5c2cf

Please sign in to comment.