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 56db030312f Remove RegistryCenter.persistClusterState() (#31246)
56db030312f is described below
commit 56db030312ff5b1f54317a783e45c03c33f549ea
Author: Liang Zhang <[email protected]>
AuthorDate: Thu May 16 17:58:07 2024 +0800
Remove RegistryCenter.persistClusterState() (#31246)
---
.../infra/state/cluster/ClusterStateContext.java | 2 +-
.../manager/cluster/ClusterContextManagerBuilder.java | 2 +-
.../mode/manager/cluster/coordinator/RegistryCenter.java | 14 --------------
.../status/cluster/service/ClusterStatusService.java | 11 +++++++----
.../status/cluster/service/ClusterStatusServiceTest.java | 15 ++++++++++++---
5 files changed, 21 insertions(+), 23 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 f5699729264..2ef2f2d3d7c 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
@@ -22,9 +22,9 @@ import lombok.Getter;
/**
* Cluster state context.
*/
+@Getter
public final class ClusterStateContext {
- @Getter
private ClusterState currentState = ClusterState.OK;
/**
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index ae6446377ea..f046d7bbcf4 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -100,7 +100,7 @@ public final class ClusterContextManagerBuilder implements
ContextManagerBuilder
}
private void loadClusterStatus(final RegistryCenter registryCenter, final
ContextManager contextManager) {
- registryCenter.persistClusterState(contextManager);
+
registryCenter.getClusterStatusService().persistClusterState(contextManager.getClusterStateContext().getCurrentState());
contextManager.updateClusterState(registryCenter.getClusterStatusService().loadClusterStatus());
}
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java
index ccec273fff3..b7c05e0f5ce 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/RegistryCenter.java
@@ -17,15 +17,12 @@
package org.apache.shardingsphere.mode.manager.cluster.coordinator;
-import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import
org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
-import org.apache.shardingsphere.metadata.persist.node.ComputeNode;
-import org.apache.shardingsphere.mode.manager.ContextManager;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.lock.GlobalLockPersistService;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.GovernanceWatcherFactory;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.service.ClusterStatusService;
@@ -96,15 +93,4 @@ public final class RegistryCenter {
computeNodeStatusService.persistInstanceState(computeNodeInstance.getCurrentInstanceId(),
computeNodeInstance.getState());
listenerFactory.watchListeners();
}
-
- /**
- * Persist cluster state.
- *
- * @param contextManager context manager
- */
- public void persistClusterState(final ContextManager contextManager) {
- if
(Strings.isNullOrEmpty(repository.getDirectly(ComputeNode.getClusterStatusNodePath())))
{
-
clusterStatusService.persistClusterState(contextManager.getClusterStateContext());
- }
- }
}
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/service/ClusterStatusService.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/service/ClusterStatusService.java
index 03045139976..07ede1342aa 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/service/ClusterStatusService.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/service/ClusterStatusService.java
@@ -17,8 +17,9 @@
package
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.service;
+import com.google.common.base.Strings;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.state.cluster.ClusterStateContext;
+import org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.metadata.persist.node.ComputeNode;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
@@ -33,10 +34,12 @@ public final class ClusterStatusService {
/**
* Persist cluster state.
*
- * @param state cluster state context
+ * @param state cluster state
*/
- public void persistClusterState(final ClusterStateContext state) {
- repository.persist(ComputeNode.getClusterStatusNodePath(),
state.getCurrentState().name());
+ public void persistClusterState(final ClusterState state) {
+ if
(Strings.isNullOrEmpty(repository.getDirectly(ComputeNode.getClusterStatusNodePath())))
{
+ repository.persist(ComputeNode.getClusterStatusNodePath(),
state.name());
+ }
}
/**
diff --git
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/service/ClusterStatusServiceTest.java
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/service/ClusterStatusServiceTest.java
index 1a8890b7fc1..15b1de83b95 100644
---
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/service/ClusterStatusServiceTest.java
+++
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/status/cluster/service/ClusterStatusServiceTest.java
@@ -18,7 +18,6 @@
package
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.service;
import org.apache.shardingsphere.infra.state.cluster.ClusterState;
-import org.apache.shardingsphere.infra.state.cluster.ClusterStateContext;
import org.apache.shardingsphere.metadata.persist.node.ComputeNode;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import org.junit.jupiter.api.Test;
@@ -26,7 +25,9 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class ClusterStatusServiceTest {
@@ -35,12 +36,20 @@ class ClusterStatusServiceTest {
private ClusterPersistRepository repository;
@Test
- void assertPersistClusterState() {
+ void assertPersistClusterStateWithoutPath() {
ClusterStatusService clusterStatusService = new
ClusterStatusService(repository);
- clusterStatusService.persistClusterState(new ClusterStateContext());
+ clusterStatusService.persistClusterState(ClusterState.OK);
verify(repository).persist(ComputeNode.getClusterStatusNodePath(),
ClusterState.OK.name());
}
+ @Test
+ void assertPersistClusterStateWithPath() {
+ ClusterStatusService clusterStatusService = new
ClusterStatusService(repository);
+
when(repository.getDirectly("/nodes/compute_nodes/status")).thenReturn(ClusterState.OK.name());
+ clusterStatusService.persistClusterState(ClusterState.OK);
+ verify(repository,
times(0)).persist(ComputeNode.getClusterStatusNodePath(),
ClusterState.OK.name());
+ }
+
@Test
void assertLoadClusterStatus() {
new ClusterStatusService(repository).loadClusterStatus();