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 59c0a7709f8 Refactor usage of ContextManagerLifecycleListener.onInitialized (#35014) 59c0a7709f8 is described below commit 59c0a7709f891452874809bca83a4bc6ec4ada79 Author: Liang Zhang <zhangli...@apache.org> AuthorDate: Sun Mar 16 23:19:15 2025 +0800 Refactor usage of ContextManagerLifecycleListener.onInitialized (#35014) * Refactor usage of ContextManagerLifecycleListener.onInitialized * Refactor usage of ContextManagerLifecycleListener.onInitialized * Refactor usage of ContextManagerLifecycleListener.onInitialized * Refactor usage of ContextManagerLifecycleListener.onInitialized --- .../ReadwriteSplittingContextManagerLifecycleListener.java | 14 ++++---------- .../listener/PipelineContextManagerLifecycleListener.java | 8 +------- .../StatisticsCollectContextManagerLifecycleListener.java | 3 +-- ...atisticsCollectContextManagerLifecycleListenerTest.java | 11 ++--------- .../apache/shardingsphere/mode/manager/ContextManager.java | 4 +++- 5 files changed, 11 insertions(+), 29 deletions(-) diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/listener/ReadwriteSplittingContextManagerLifecycleListener.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/listener/ReadwriteSplittingContextManagerLifecycleListener.java index 841464a6e5a..ce7e240718a 100644 --- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/listener/ReadwriteSplittingContextManagerLifecycleListener.java +++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/listener/ReadwriteSplittingContextManagerLifecycleListener.java @@ -33,16 +33,6 @@ public class ReadwriteSplittingContextManagerLifecycleListener implements Contex @Override public void onInitialized(final ContextManager contextManager) { - if (contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()) { - updateQualifiedDataSourceState(contextManager); - } - } - - @Override - public void onDestroyed(final ContextManager contextManager) { - } - - private void updateQualifiedDataSourceState(final ContextManager contextManager) { Map<String, QualifiedDataSourceState> qualifiedDataSourceStateMap = contextManager.getPersistServiceFacade().getQualifiedDataSourceStateService().load(); qualifiedDataSourceStateMap.forEach((key, value) -> updateQualifiedDataSourceState(contextManager.getMetaDataContexts().getMetaData(), new QualifiedDataSource(key), value)); } @@ -54,4 +44,8 @@ public class ReadwriteSplittingContextManagerLifecycleListener implements Contex } }); } + + @Override + public void onDestroyed(final ContextManager contextManager) { + } } diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/PipelineContextManagerLifecycleListener.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/PipelineContextManagerLifecycleListener.java index 6a2ac6cc2a2..09c5fb2dff3 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/PipelineContextManagerLifecycleListener.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/PipelineContextManagerLifecycleListener.java @@ -32,7 +32,6 @@ import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO; import org.apache.shardingsphere.elasticjob.infra.spi.ElasticJobServiceLoader; import org.apache.shardingsphere.elasticjob.lite.lifecycle.api.JobConfigurationAPI; import org.apache.shardingsphere.elasticjob.lite.lifecycle.domain.JobBriefInfo; -import org.apache.shardingsphere.infra.config.mode.ModeConfiguration; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener; @@ -48,17 +47,12 @@ public final class PipelineContextManagerLifecycleListener implements ContextMan @Override public void onInitialized(final ContextManager contextManager) { - ModeConfiguration modeConfig = contextManager.getComputeNodeInstanceContext().getModeConfiguration(); - if (!contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()) { - log.info("mode type is not Cluster, mode type='{}', ignore", modeConfig.getType()); - return; - } String preSelectedDatabaseName = contextManager.getPreSelectedDatabaseName(); if (DefaultDatabase.LOGIC_NAME.equals(preSelectedDatabaseName)) { return; } PipelineContextKey contextKey = new PipelineContextKey(preSelectedDatabaseName, contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()); - PipelineContextManager.putContext(contextKey, new PipelineContext(modeConfig, contextManager)); + PipelineContextManager.putContext(contextKey, new PipelineContext(contextManager.getComputeNodeInstanceContext().getModeConfiguration(), contextManager)); PipelineMetaDataNodeWatcher.getInstance(contextKey); ElasticJobServiceLoader.registerTypedService(ElasticJobListener.class); try { diff --git a/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListener.java b/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListener.java index 694bcde7873..361be27bf49 100644 --- a/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListener.java +++ b/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListener.java @@ -28,8 +28,7 @@ public final class StatisticsCollectContextManagerLifecycleListener implements C @Override public void onInitialized(final ContextManager contextManager) { - if (contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster() - && InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) { + if (InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) { new StatisticsCollectJobWorker(contextManager).initialize(); } } diff --git a/kernel/schedule/core/src/test/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListenerTest.java b/kernel/schedule/core/src/test/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListenerTest.java index 789a0b49c8d..9fb256a4efd 100644 --- a/kernel/schedule/core/src/test/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListenerTest.java +++ b/kernel/schedule/core/src/test/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListenerTest.java @@ -30,19 +30,12 @@ import static org.mockito.Mockito.when; class StatisticsCollectContextManagerLifecycleListenerTest { - @Test - void assertOnInitializedWithNotClusterMode() { - ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); - new StatisticsCollectContextManagerLifecycleListener().onInitialized(contextManager); - verify(contextManager.getComputeNodeInstanceContext()).getModeConfiguration(); - } - @Test void assertOnInitializedWithNotProxy() { ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()).thenReturn(true); new StatisticsCollectContextManagerLifecycleListener().onInitialized(contextManager); - verify(contextManager.getComputeNodeInstanceContext(), times(2)).getModeConfiguration(); + verify(contextManager.getComputeNodeInstanceContext()).getModeConfiguration(); } @Test @@ -51,7 +44,7 @@ class StatisticsCollectContextManagerLifecycleListenerTest { when(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()).thenReturn(true); when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()).thenReturn(InstanceType.PROXY); new StatisticsCollectContextManagerLifecycleListener().onInitialized(contextManager); - verify(contextManager.getComputeNodeInstanceContext(), times(3)).getModeConfiguration(); + verify(contextManager.getComputeNodeInstanceContext(), times(2)).getModeConfiguration(); } @Test diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java index b5c3344eafc..fbaa7a8edce 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java @@ -81,7 +81,9 @@ public final class ContextManager implements AutoCloseable { stateContext = new StateContext(persistServiceFacade.getStateService().load()); executorEngine = ExecutorEngine.createExecutorEngineWithSize(metaDataContexts.getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE)); for (ContextManagerLifecycleListener each : ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class)) { - each.onInitialized(this); + if (computeNodeInstanceContext.getModeConfiguration().isCluster()) { + each.onInitialized(this); + } } }