This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 f064398823c Refactor EventSubscriberRegistry (#32674)
f064398823c is described below
commit f064398823cdbc498cc992b00520b14e3306aa3e
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Aug 25 21:52:12 2024 +0800
Refactor EventSubscriberRegistry (#32674)
* Refactor EventSubscriberRegistry
* Refactor EventSubscriberRegistry
---
.../mode/event/subsciber/EventSubscriberRegistry.java | 17 +++++++++++++++--
.../manager/cluster/ClusterContextManagerBuilder.java | 10 ++++++----
.../registry/ClusterDeliverEventSubscriberRegistry.java | 16 +++-------------
.../ClusterDispatchEventSubscriberRegistry.java | 14 +++-----------
4 files changed, 27 insertions(+), 30 deletions(-)
diff --git
a/mode/core/src/main/java/org/apache/shardingsphere/mode/event/subsciber/EventSubscriberRegistry.java
b/mode/core/src/main/java/org/apache/shardingsphere/mode/event/subsciber/EventSubscriberRegistry.java
index c7c5152e948..137896a6810 100644
---
a/mode/core/src/main/java/org/apache/shardingsphere/mode/event/subsciber/EventSubscriberRegistry.java
+++
b/mode/core/src/main/java/org/apache/shardingsphere/mode/event/subsciber/EventSubscriberRegistry.java
@@ -17,13 +17,26 @@
package org.apache.shardingsphere.mode.event.subsciber;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
+import org.apache.shardingsphere.infra.util.eventbus.EventSubscriber;
+
+import java.util.Collection;
+
/**
* Event subscriber registry.
*/
-public interface EventSubscriberRegistry {
+@RequiredArgsConstructor
+public final class EventSubscriberRegistry {
+
+ private final EventBusContext eventBusContext;
/**
* Register subscribers.
+ *
+ * @param subscribers to be registered subscribers
*/
- void register();
+ public void register(final Collection<EventSubscriber> subscribers) {
+ subscribers.forEach(eventBusContext::register);
+ }
}
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 b03d6c28e26..ca4e610ea48 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
@@ -26,16 +26,17 @@ import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
+import org.apache.shardingsphere.mode.event.subsciber.EventSubscriberRegistry;
import org.apache.shardingsphere.mode.lock.GlobalLockContext;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilder;
import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
-import
org.apache.shardingsphere.mode.manager.cluster.listener.DataChangedEventListenerRegistry;
import
org.apache.shardingsphere.mode.manager.cluster.event.subscriber.registry.ClusterDeliverEventSubscriberRegistry;
import
org.apache.shardingsphere.mode.manager.cluster.event.subscriber.registry.ClusterDispatchEventSubscriberRegistry;
import
org.apache.shardingsphere.mode.manager.cluster.exception.MissingRequiredClusterRepositoryConfigurationException;
-import
org.apache.shardingsphere.mode.manager.cluster.workerid.ClusterWorkerIdGenerator;
+import
org.apache.shardingsphere.mode.manager.cluster.listener.DataChangedEventListenerRegistry;
import
org.apache.shardingsphere.mode.manager.cluster.lock.GlobalLockPersistService;
+import
org.apache.shardingsphere.mode.manager.cluster.workerid.ClusterWorkerIdGenerator;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
@@ -75,8 +76,9 @@ public final class ClusterContextManagerBuilder implements
ContextManagerBuilder
contextManager.getPersistServiceFacade().getComputeNodePersistService().registerOnline(computeNodeInstanceContext.getInstance());
contextManager.getComputeNodeInstanceContext().getAllClusterInstances().addAll(contextManager.getPersistServiceFacade().getComputeNodePersistService().loadAllComputeNodeInstances());
new DataChangedEventListenerRegistry(contextManager,
getDatabaseNames(param,
contextManager.getPersistServiceFacade().getMetaDataPersistService())).register();
- new ClusterDeliverEventSubscriberRegistry(contextManager).register();
- new ClusterDispatchEventSubscriberRegistry(contextManager).register();
+ EventSubscriberRegistry eventSubscriberRegistry = new
EventSubscriberRegistry(contextManager.getComputeNodeInstanceContext().getEventBusContext());
+ eventSubscriberRegistry.register(new
ClusterDeliverEventSubscriberRegistry(contextManager).getSubscribers());
+ eventSubscriberRegistry.register(new
ClusterDispatchEventSubscriberRegistry(contextManager).getSubscribers());
}
private Collection<String> getDatabaseNames(final
ContextManagerBuilderParameter param, final MetaDataPersistService
metaDataPersistService) {
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/registry/ClusterDeliverEventSubscriberRegistry.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/registry/ClusterDeliverEventSubscriberRegistry.java
index 67c29c40e8f..da02b05f300 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/registry/ClusterDeliverEventSubscriberRegistry.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/registry/ClusterDeliverEventSubscriberRegistry.java
@@ -17,9 +17,8 @@
package
org.apache.shardingsphere.mode.manager.cluster.event.subscriber.registry;
-import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
+import lombok.Getter;
import org.apache.shardingsphere.infra.util.eventbus.EventSubscriber;
-import org.apache.shardingsphere.mode.event.subsciber.EventSubscriberRegistry;
import org.apache.shardingsphere.mode.manager.ContextManager;
import
org.apache.shardingsphere.mode.manager.cluster.event.subscriber.deliver.DeliverQualifiedDataSourceSubscriber;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
@@ -30,21 +29,12 @@ import java.util.Collections;
/**
* Cluster deliver event subscriber registry.
*/
-public final class ClusterDeliverEventSubscriberRegistry implements
EventSubscriberRegistry {
-
- private final EventBusContext eventBusContext;
+@Getter
+public final class ClusterDeliverEventSubscriberRegistry {
private final Collection<EventSubscriber> subscribers;
public ClusterDeliverEventSubscriberRegistry(final ContextManager
contextManager) {
- eventBusContext =
contextManager.getComputeNodeInstanceContext().getEventBusContext();
subscribers = Collections.singleton(new
DeliverQualifiedDataSourceSubscriber((ClusterPersistRepository)
contextManager.getPersistServiceFacade().getRepository()));
}
-
- /**
- * Register subscribers.
- */
- public void register() {
- subscribers.forEach(eventBusContext::register);
- }
}
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/registry/ClusterDispatchEventSubscriberRegistry.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/registry/ClusterDispatchEventSubscriberRegistry.java
index b767575a715..cee730a8d87 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/registry/ClusterDispatchEventSubscriberRegistry.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/registry/ClusterDispatchEventSubscriberRegistry.java
@@ -17,9 +17,8 @@
package
org.apache.shardingsphere.mode.manager.cluster.event.subscriber.registry;
-import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
+import lombok.Getter;
import org.apache.shardingsphere.infra.util.eventbus.EventSubscriber;
-import org.apache.shardingsphere.mode.event.subsciber.EventSubscriberRegistry;
import org.apache.shardingsphere.mode.manager.ContextManager;
import
org.apache.shardingsphere.mode.manager.cluster.event.subscriber.dispatch.CacheEvictedSubscriber;
import
org.apache.shardingsphere.mode.manager.cluster.event.subscriber.dispatch.ComputeNodeOnlineSubscriber;
@@ -40,14 +39,12 @@ import java.util.Collection;
/**
* Cluster dispatch event subscriber registry.
*/
-public final class ClusterDispatchEventSubscriberRegistry implements
EventSubscriberRegistry {
-
- private final EventBusContext eventBusContext;
+@Getter
+public final class ClusterDispatchEventSubscriberRegistry {
private final Collection<EventSubscriber> subscribers;
public ClusterDispatchEventSubscriberRegistry(final ContextManager
contextManager) {
- eventBusContext =
contextManager.getComputeNodeInstanceContext().getEventBusContext();
subscribers = Arrays.asList(new
RuleItemChangedSubscriber(contextManager),
new ResourceMetaDataChangedSubscriber(contextManager),
new ListenerAssistedSubscriber(contextManager),
@@ -61,9 +58,4 @@ public final class ClusterDispatchEventSubscriberRegistry
implements EventSubscr
new PropertiesEventSubscriber(contextManager),
new GlobalRuleConfigurationEventSubscriber(contextManager));
}
-
- @Override
- public void register() {
- subscribers.forEach(eventBusContext::register);
- }
}