Skip to content

Commit

Permalink
Use DataChangedEventHandler to instead of DispatchEventBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Dec 31, 2024
1 parent 5e79134 commit 79ecc24
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 159 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<RuleConfiguration> ruleConfig = contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load(ruleName.get());
Preconditions.checkArgument(ruleConfig.isPresent(), "Can not find rule configuration with name: %s", ruleName.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AlterPropertiesEvent> {
public final class PropertiesChangedHandler implements DataChangedEventHandler {

@Override
public String getSubscribedKey() {
Expand All @@ -44,7 +44,13 @@ public Collection<Type> getSubscribedTypes() {
}

@Override
public Optional<AlterPropertiesEvent> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.GlobalRuleChangedHandler
org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.PropertiesChangedHandler

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 79ecc24

Please sign in to comment.