From b0ef66fd92a8aeb796031434411a16d4abc04645 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sun, 29 Dec 2024 11:39:56 +0800 Subject: [PATCH] Refactor ShardingSphereStatisticsRefreshEngine --- .../core/job/statistics/collect/StatisticsCollectJob.java | 4 +--- .../refresher/ShardingSphereStatisticsRefreshEngine.java | 7 +++++-- .../ShardingSphereStatisticsRefreshEngineTest.java | 6 ++---- .../subscriber/type/ListenerAssistedSubscriber.java | 6 ++---- .../subscriber/type/MetaDataChangedSubscriber.java | 6 ++---- 5 files changed, 12 insertions(+), 17 deletions(-) diff --git a/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectJob.java b/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectJob.java index bd5537db7201f..c49160cdde8b5 100644 --- a/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectJob.java +++ b/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectJob.java @@ -21,8 +21,6 @@ import org.apache.shardingsphere.elasticjob.api.ShardingContext; import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.lock.ClusterLockContext; -import org.apache.shardingsphere.mode.manager.cluster.persist.service.GlobalLockPersistService; import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import org.apache.shardingsphere.mode.spi.PersistRepository; @@ -39,7 +37,7 @@ public final class StatisticsCollectJob implements SimpleJob { public void execute(final ShardingContext shardingContext) { PersistRepository repository = contextManager.getPersistServiceFacade().getRepository(); if (repository instanceof ClusterPersistRepository) { - new ShardingSphereStatisticsRefreshEngine(contextManager, new ClusterLockContext(new GlobalLockPersistService((ClusterPersistRepository) repository))).refresh(); + new ShardingSphereStatisticsRefreshEngine(contextManager).refresh(); } } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java index 61fe8413cb779..073be7e5a019b 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.mode.metadata.refresher; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey; import org.apache.shardingsphere.infra.executor.kernel.thread.ExecutorThreadFactoryBuilder; @@ -51,7 +50,6 @@ /** * ShardingSphere statistics refresh engine. */ -@RequiredArgsConstructor @Slf4j public final class ShardingSphereStatisticsRefreshEngine { @@ -61,6 +59,11 @@ public final class ShardingSphereStatisticsRefreshEngine { private final LockContext lockContext; + public ShardingSphereStatisticsRefreshEngine(final ContextManager contextManager) { + this.contextManager = contextManager; + lockContext = contextManager.getComputeNodeInstanceContext().getLockContext(); + } + /** * Async refresh. */ diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngineTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngineTest.java index ffc7a4e17d082..02b8937a081d1 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngineTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngineTest.java @@ -20,7 +20,6 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationProperties; import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey; -import org.apache.shardingsphere.infra.lock.LockContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; @@ -59,9 +58,8 @@ void assertRefresh() { when(contextManager.getMetaDataContexts().getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties())); when(contextManager.getMetaDataContexts().getMetaData().getTemporaryProps()).thenReturn(new TemporaryConfigurationProperties( PropertiesBuilder.build(new Property(TemporaryConfigurationPropertyKey.PROXY_META_DATA_COLLECTOR_ENABLED.getKey(), Boolean.TRUE.toString())))); - LockContext lockContext = mock(LockContext.class); - when(lockContext.tryLock(any(), anyLong())).thenReturn(true); - new ShardingSphereStatisticsRefreshEngine(contextManager, lockContext).refresh(); + when(contextManager.getComputeNodeInstanceContext().getLockContext().tryLock(any(), anyLong())).thenReturn(true); + new ShardingSphereStatisticsRefreshEngine(contextManager).refresh(); verify(contextManager.getPersistServiceFacade().getMetaDataPersistService().getShardingSphereDataPersistService()).update(any()); } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ListenerAssistedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ListenerAssistedSubscriber.java index 831e4fe3e6b3d..c8b3c2cda351b 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ListenerAssistedSubscriber.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ListenerAssistedSubscriber.java @@ -21,13 +21,11 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.instance.metadata.InstanceType; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; +import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.CreateDatabaseListenerAssistedEvent; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.DropDatabaseListenerAssistedEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.DatabaseMetaDataChangedListener; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.lock.ClusterLockContext; -import org.apache.shardingsphere.mode.manager.cluster.persist.service.GlobalLockPersistService; import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; @@ -75,7 +73,7 @@ public synchronized void renew(final DropDatabaseListenerAssistedEvent event) { private void refreshStatisticsData() { if (contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster() && InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) { - new ShardingSphereStatisticsRefreshEngine(contextManager, new ClusterLockContext(new GlobalLockPersistService(repository))).asyncRefresh(); + new ShardingSphereStatisticsRefreshEngine(contextManager).asyncRefresh(); } } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/MetaDataChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/MetaDataChangedSubscriber.java index 52d6aeae4db0d..ed75eb960b90f 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/MetaDataChangedSubscriber.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/MetaDataChangedSubscriber.java @@ -22,16 +22,14 @@ import org.apache.shardingsphere.infra.instance.metadata.InstanceType; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; +import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.SchemaAddedEvent; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.SchemaDeletedEvent; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.table.TableCreatedOrAlteredEvent; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.table.TableDroppedEvent; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.view.ViewCreatedOrAlteredEvent; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.view.ViewDroppedEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.lock.ClusterLockContext; -import org.apache.shardingsphere.mode.manager.cluster.persist.service.GlobalLockPersistService; import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; @@ -128,7 +126,7 @@ public synchronized void renew(final ViewDroppedEvent event) { private void refreshStatisticsData() { if (contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster() && InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) { - new ShardingSphereStatisticsRefreshEngine(contextManager, new ClusterLockContext(new GlobalLockPersistService(repository))).asyncRefresh(); + new ShardingSphereStatisticsRefreshEngine(contextManager).asyncRefresh(); } } }