From 79ecc24fc034844c0166b1016ea6ede823cfa0ba Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 31 Dec 2024 12:58:02 +0800 Subject: [PATCH] Use DataChangedEventHandler to instead of DispatchEventBuilder --- .../event/config/AlterPropertiesEvent.java | 34 ------------ .../type/GlobalRuleChangedHandler.java | 2 +- .../type/PropertiesChangedHandler.java} | 22 +++++--- ...lusterDispatchEventSubscriberRegistry.java | 4 +- .../type/PropertiesEventSubscriber.java | 47 ---------------- ...vent.dispatch.builder.DispatchEventBuilder | 1 - ...t.dispatch.handler.DataChangedEventHandler | 3 +- .../PropertiesDispatchEventBuilderTest.java | 54 ------------------- .../type/PropertiesChangedHandlerTest.java} | 33 ++++++++---- 9 files changed, 41 insertions(+), 159 deletions(-) delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterPropertiesEvent.java rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{builder/type/PropertiesDispatchEventBuilder.java => handler/type/PropertiesChangedHandler.java} (60%) delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriber.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilderTest.java rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{subscriber/type/PropertiesEventSubscriberTest.java => handler/type/PropertiesChangedHandlerTest.java} (59%) diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterPropertiesEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterPropertiesEvent.java deleted file mode 100644 index e4d6ca9942502..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterPropertiesEvent.java +++ /dev/null @@ -1,34 +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.event.config; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Alter properties event. - */ -@RequiredArgsConstructor -@Getter -public final class AlterPropertiesEvent implements DispatchEvent { - - private final String activeVersionKey; - - private final String activeVersion; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandler.java index ff058e65f9128..f950991266d24 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandler.java @@ -58,7 +58,7 @@ public void handle(final ContextManager contextManager, final DataChangedEvent e return; } Preconditions.checkArgument(event.getValue().equals( - contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getKey())), + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getKey())), "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); Optional ruleConfig = contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load(ruleName.get()); Preconditions.checkArgument(ruleConfig.isPresent(), "Can not find rule configuration with name: %s", ruleName.get()); diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandler.java similarity index 60% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilder.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandler.java index 1f10eae527e25..bc147b22e327e 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilder.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandler.java @@ -15,23 +15,23 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +import com.google.common.base.Preconditions; import org.apache.shardingsphere.metadata.persist.node.GlobalNode; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.config.AlterPropertiesEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler; import org.apache.shardingsphere.mode.path.GlobalNodePath; import java.util.Arrays; import java.util.Collection; -import java.util.Optional; /** - * Properties dispatch event builder. + * Properties changed handler. */ -public final class PropertiesDispatchEventBuilder implements DispatchEventBuilder { +public final class PropertiesChangedHandler implements DataChangedEventHandler { @Override public String getSubscribedKey() { @@ -44,7 +44,13 @@ public Collection getSubscribedTypes() { } @Override - public Optional build(final DataChangedEvent event) { - return GlobalNodePath.isPropsActiveVersionPath(event.getKey()) ? Optional.of(new AlterPropertiesEvent(event.getKey(), event.getValue())) : Optional.empty(); + public void handle(final ContextManager contextManager, final DataChangedEvent event) { + if (!GlobalNodePath.isPropsActiveVersionPath(event.getKey())) { + return; + } + Preconditions.checkArgument(event.getValue().equals( + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getKey())), + "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterProperties(contextManager.getPersistServiceFacade().getMetaDataPersistService().getPropsService().load()); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java index 139d6f8e2fa4d..05fa39a2dad0b 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java @@ -26,7 +26,6 @@ import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.ListenerAssistedSubscriber; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.MetaDataChangedSubscriber; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.ProcessListChangedSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.PropertiesEventSubscriber; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.QualifiedDataSourceSubscriber; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.RuleItemChangedSubscriber; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.StorageUnitEventSubscriber; @@ -51,7 +50,6 @@ public ClusterDispatchEventSubscriberRegistry(final ContextManager contextManage new CacheEvictedSubscriber(), new ComputeNodeStateSubscriber(contextManager), new QualifiedDataSourceSubscriber(contextManager), - new StorageUnitEventSubscriber(contextManager), - new PropertiesEventSubscriber(contextManager)); + new StorageUnitEventSubscriber(contextManager)); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriber.java deleted file mode 100644 index ba3d0df017f42..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriber.java +++ /dev/null @@ -1,47 +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.subscriber.type; - -import com.google.common.base.Preconditions; -import com.google.common.eventbus.Subscribe; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.config.AlterPropertiesEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; - -/** - * Properties event subscriber. - */ -@RequiredArgsConstructor -public final class PropertiesEventSubscriber implements DispatchEventSubscriber { - - private final ContextManager contextManager; - - /** - * Renew for global properties. - * - * @param event global properties alter event - */ - @Subscribe - public synchronized void renew(final AlterPropertiesEvent event) { - Preconditions.checkArgument(event.getActiveVersion().equals( - contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())), - "Invalid active version: %s of key: %s", event.getActiveVersion(), event.getActiveVersionKey()); - contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterProperties(contextManager.getPersistServiceFacade().getMetaDataPersistService().getPropsService().load()); - } -} 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 index 5b3cbed7e0f56..2cbadec1333f6 100644 --- 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 @@ -18,7 +18,6 @@ org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type.QualifiedDataSourceDispatchEventBuilder org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type.ComputeNodeStateDispatchEventBuilder org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type.ShardingSphereDataDispatchEventBuilder -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type.PropertiesDispatchEventBuilder org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type.ListenerAssistedDispatchEventBuilder org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type.ComputeNodeOnlineDispatchEventBuilder org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type.ShowProcessListDispatchEventBuilder diff --git a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler index 50a4652948d03..b4496356ae141 100644 --- a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler +++ b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler @@ -16,4 +16,5 @@ # org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ClusterStateChangedHandler -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.GlobalRuleChangedHandler \ No newline at end of file +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.GlobalRuleChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.PropertiesChangedHandler diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilderTest.java deleted file mode 100644 index ac2b04b0b25b2..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilderTest.java +++ /dev/null @@ -1,54 +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.type; - -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.config.AlterPropertiesEvent; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class PropertiesDispatchEventBuilderTest { - - private final PropertiesDispatchEventBuilder builder = new PropertiesDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is("/props")); - } - - @Test - void assertBuildAlterPropertiesEvent() { - Optional actual = builder.build(new DataChangedEvent("/props/active_version", "key=value", Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat((actual.get()).getActiveVersionKey(), is("/props/active_version")); - assertThat((actual.get()).getActiveVersion(), is("key=value")); - } - - @Test - void assertBuildWithInvalidEventKey() { - Optional actual = builder.build(new DataChangedEvent("/props/xxx", "key=value", Type.ADDED)); - assertFalse(actual.isPresent()); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandlerTest.java similarity index 59% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriberTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandlerTest.java index e6aea84fb9936..d251a203780be 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandlerTest.java @@ -15,10 +15,13 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.config.AlterPropertiesEvent; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; +import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -28,27 +31,37 @@ import java.util.Properties; +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 PropertiesEventSubscriberTest { +class PropertiesChangedHandlerTest { - private PropertiesEventSubscriber subscriber; + private DataChangedEventHandler handler; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ContextManager contextManager; @BeforeEach void setUp() { - subscriber = new PropertiesEventSubscriber(contextManager); + handler = ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).stream() + .filter(each -> each.getSubscribedKey().equals("/props")).findFirst().orElse(null); } @Test - void assertRenew() { - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getPropsService().load()).thenReturn(new Properties()); - subscriber.renew(new AlterPropertiesEvent("key", "value")); - verify(contextManager.getMetaDataContextManager().getGlobalConfigurationManager()).alterProperties(new Properties()); + void assertHandleWithInvalidEventKey() { + handler.handle(contextManager, new DataChangedEvent("/props/xxx", "key=value", Type.ADDED)); + verify(contextManager, times(0)).getPersistServiceFacade(); + } + + @Test + void assertHandle() { + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("/props/active_version")).thenReturn("key=value"); + Properties props = mock(Properties.class); + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getPropsService().load()).thenReturn(props); + handler.handle(contextManager, new DataChangedEvent("/props/active_version", "key=value", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getGlobalConfigurationManager()).alterProperties(props); } }