From 4df9a83b78de1bd550c52d889cb0dbaa01c73257 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Wed, 1 Jan 2025 14:49:55 +0800 Subject: [PATCH 1/2] Remove DispatchEventBuilder --- .../builder/DispatchEventBuilder.java | 56 --------------- .../DataChangedEventListenerRegistry.java | 7 -- .../type/GlobalMetaDataChangedListener.java | 42 ----------- .../GlobalMetaDataChangedListenerTest.java | 70 ------------------- 4 files changed, 175 deletions(-) delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/DispatchEventBuilder.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListenerTest.java diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/DispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/DispatchEventBuilder.java deleted file mode 100644 index 1ea4ab224ae46..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/DispatchEventBuilder.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder; - -import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; - -import java.util.Collection; -import java.util.Optional; - -/** - * Dispatch event builder. - * - * @param type of event - */ -@SingletonSPI -public interface DispatchEventBuilder { - - /** - * Get subscribed key. - * - * @return subscribed key - */ - String getSubscribedKey(); - - /** - * Get subscribed types. - * - * @return subscribed types - */ - Collection getSubscribedTypes(); - - /** - * Create dispatch event. - * - * @param event registry center data changed event - * @return dispatch event - */ - Optional build(DataChangedEvent event); -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java index 9122a5006c93f..052a89e143473 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java @@ -21,11 +21,9 @@ import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.DatabaseMetaDataChangedListener; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.GlobalMetaDataChangedHandler; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.GlobalMetaDataChangedListener; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import java.util.Collection; @@ -55,7 +53,6 @@ public DataChangedEventListenerRegistry(final ContextManager contextManager, fin */ public void register() { databaseNames.forEach(this::registerDatabaseListeners); - ShardingSphereServiceLoader.getServiceInstances(DispatchEventBuilder.class).forEach(this::registerGlobalListeners); ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).forEach(this::registerGlobalHandlers); } @@ -63,10 +60,6 @@ private void registerDatabaseListeners(final String databaseName) { repository.watch(DatabaseMetaDataNode.getDatabaseNamePath(databaseName), new DatabaseMetaDataChangedListener(eventBusContext)); } - private void registerGlobalListeners(final DispatchEventBuilder builder) { - repository.watch(builder.getSubscribedKey(), new GlobalMetaDataChangedListener(eventBusContext, builder)); - } - private void registerGlobalHandlers(final DataChangedEventHandler handler) { repository.watch(handler.getSubscribedKey(), new GlobalMetaDataChangedHandler(contextManager, handler)); } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java deleted file mode 100644 index cbdc96d6a0db7..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type; - -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; -import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEventListener; - -/** - * Global meta data changed listener. - */ -@RequiredArgsConstructor -public final class GlobalMetaDataChangedListener implements DataChangedEventListener { - - private final EventBusContext eventBusContext; - - private final DispatchEventBuilder eventBuilder; - - @Override - public void onChange(final DataChangedEvent event) { - if (eventBuilder.getSubscribedTypes().contains(event.getType())) { - eventBuilder.build(event).ifPresent(eventBusContext::post); - } - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListenerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListenerTest.java deleted file mode 100644 index 14ea417fa5771..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListenerTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type; - -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.Collections; -import java.util.Optional; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class GlobalMetaDataChangedListenerTest { - - private GlobalMetaDataChangedListener listener; - - @Mock - private EventBusContext eventBusContext; - - @Mock - private DispatchEventBuilder builder; - - @BeforeEach - void setUp() { - listener = new GlobalMetaDataChangedListener(eventBusContext, builder); - when(builder.getSubscribedTypes()).thenReturn(Collections.singleton(Type.ADDED)); - } - - @Test - void assertOnChangeWithUnsupportedType() { - DataChangedEvent event = new DataChangedEvent("key", "value", Type.DELETED); - listener.onChange(event); - verify(builder, times(0)).build(event); - } - - @Test - void assertOnChangeWithSupportedType() { - DataChangedEvent event = new DataChangedEvent("key", "value", Type.ADDED); - Object builtEvent = mock(Object.class); - when(builder.build(event)).thenReturn(Optional.of(builtEvent)); - listener.onChange(event); - verify(eventBusContext).post(builtEvent); - } -} From f4d3d6bfff1ecc7094035b3cd3a9df3390a6e71e Mon Sep 17 00:00:00 2001 From: zhangliang Date: Wed, 1 Jan 2025 14:50:12 +0800 Subject: [PATCH 2/2] Remove DispatchEventBuilder --- ...r.event.dispatch.builder.DispatchEventBuilder | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder diff --git a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder deleted file mode 100644 index b1312a0905c3c..0000000000000 --- a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder +++ /dev/null @@ -1,16 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#