diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/RuleItemManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/RuleItemManager.java index 194d24bb026ee..4af0ebe9bc295 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/RuleItemManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/RuleItemManager.java @@ -37,13 +37,13 @@ public class RuleItemManager { private final AtomicReference metaDataContexts; - private final DatabaseRuleConfigurationManager ruleConfigurationManager; + private final DatabaseRuleConfigurationManager ruleConfigManager; private final MetaDataPersistService metaDataPersistService; - public RuleItemManager(final AtomicReference metaDataContexts, final PersistRepository repository, final DatabaseRuleConfigurationManager ruleConfigurationManager) { + public RuleItemManager(final AtomicReference metaDataContexts, final PersistRepository repository, final DatabaseRuleConfigurationManager ruleConfigManager) { this.metaDataContexts = metaDataContexts; - this.ruleConfigurationManager = ruleConfigurationManager; + this.ruleConfigManager = ruleConfigManager; metaDataPersistService = new MetaDataPersistService(repository); } @@ -53,7 +53,7 @@ public RuleItemManager(final AtomicReference metaDataContexts, * @param event alter rule item event * @throws SQLException SQL Exception */ - @SuppressWarnings({"rawtypes", "unchecked", "unused"}) + @SuppressWarnings({"rawtypes", "unchecked"}) public void alterRuleItem(final AlterRuleItemEvent event) throws SQLException { Preconditions.checkArgument(event.getActiveVersion().equals(metaDataPersistService.getMetaDataVersionPersistService() .getActiveVersionByFullPath(event.getActiveVersionKey())), "Invalid active version: {} of key: {}", event.getActiveVersion(), event.getActiveVersionKey()); @@ -64,7 +64,7 @@ public void alterRuleItem(final AlterRuleItemEvent event) throws SQLException { RuleConfiguration currentRuleConfig = processor.findRuleConfiguration(metaDataContexts.get().getMetaData().getDatabase(databaseName)); synchronized (this) { processor.changeRuleItemConfiguration(event, currentRuleConfig, processor.swapRuleItemConfiguration(event, yamlContent)); - ruleConfigurationManager.alterRuleConfiguration(databaseName, currentRuleConfig); + ruleConfigManager.alterRuleConfiguration(databaseName, currentRuleConfig); } } @@ -74,7 +74,7 @@ public void alterRuleItem(final AlterRuleItemEvent event) throws SQLException { * @param event drop rule item event * @throws SQLException SQL Exception */ - @SuppressWarnings({"rawtypes", "unchecked", "unused"}) + @SuppressWarnings({"rawtypes", "unchecked"}) public void dropRuleItem(final DropRuleItemEvent event) throws SQLException { String databaseName = event.getDatabaseName(); Preconditions.checkState(metaDataContexts.get().getMetaData().containsDatabase(databaseName), "No database '%s' exists.", databaseName); @@ -82,7 +82,7 @@ public void dropRuleItem(final DropRuleItemEvent event) throws SQLException { RuleConfiguration currentRuleConfig = processor.findRuleConfiguration(metaDataContexts.get().getMetaData().getDatabase(databaseName)); synchronized (this) { processor.dropRuleItemConfiguration(event, currentRuleConfig); - ruleConfigurationManager.dropRuleConfiguration(databaseName, currentRuleConfig); + ruleConfigManager.dropRuleConfiguration(databaseName, currentRuleConfig); } } } 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 c942b31c0fca5..ebe7b89f7a9db 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 @@ -27,7 +27,7 @@ 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.manager.cluster.event.ClusterEventSubscriberRegistry; import org.apache.shardingsphere.mode.lock.GlobalLockContext; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.ContextManagerBuilder; @@ -79,7 +79,7 @@ private void registerOnline(final ComputeNodeInstanceContext computeNodeInstance contextManager.getPersistServiceFacade().getComputeNodePersistService().registerOnline(computeNodeInstanceContext.getInstance()); contextManager.getComputeNodeInstanceContext().getAllClusterInstances().addAll(contextManager.getPersistServiceFacade().getComputeNodePersistService().loadAllComputeNodeInstances()); new DataChangedEventListenerRegistry(contextManager, getDatabaseNames(param, contextManager.getPersistServiceFacade().getMetaDataPersistService())).register(); - EventSubscriberRegistry eventSubscriberRegistry = new EventSubscriberRegistry(contextManager.getComputeNodeInstanceContext().getEventBusContext()); + ClusterEventSubscriberRegistry eventSubscriberRegistry = new ClusterEventSubscriberRegistry(contextManager.getComputeNodeInstanceContext().getEventBusContext()); eventSubscriberRegistry.register(ShardingSphereServiceLoader.getServiceInstances(DeliverEventSubscriberFactory.class).stream() .map(each -> each.create(repository, contextManager.getComputeNodeInstanceContext().getEventBusContext())).collect(Collectors.toList())); eventSubscriberRegistry.register(new ClusterDispatchEventSubscriberRegistry(contextManager).getSubscribers()); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/event/subsciber/EventSubscriberRegistry.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/ClusterEventSubscriberRegistry.java similarity index 89% rename from mode/core/src/main/java/org/apache/shardingsphere/mode/event/subsciber/EventSubscriberRegistry.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/ClusterEventSubscriberRegistry.java index 137896a6810c8..5fdff6c4de8c4 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/event/subsciber/EventSubscriberRegistry.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/ClusterEventSubscriberRegistry.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.event.subsciber; +package org.apache.shardingsphere.mode.manager.cluster.event; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; @@ -24,10 +24,10 @@ import java.util.Collection; /** - * Event subscriber registry. + * Cluster event subscriber registry. */ @RequiredArgsConstructor -public final class EventSubscriberRegistry { +public final class ClusterEventSubscriberRegistry { private final EventBusContext eventBusContext; 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 b500cad5b03e0..cb09c7d43785e 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 @@ -56,7 +56,7 @@ public synchronized void renew(final CreateDatabaseListenerAssistedEvent event) repository.watch(DatabaseMetaDataNode.getDatabaseNamePath(event.getDatabaseName()), new DatabaseMetaDataChangedListener(contextManager.getComputeNodeInstanceContext().getEventBusContext())); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().addDatabase(event.getDatabaseName()); contextManager.getPersistServiceFacade().getListenerAssistedPersistService().deleteDatabaseNameListenerAssisted(event.getDatabaseName()); - refreshShardingSphereStatisticsData(); + refreshStatisticsData(); } /** @@ -69,10 +69,10 @@ public synchronized void renew(final DropDatabaseListenerAssistedEvent event) { repository.removeDataListener(DatabaseMetaDataNode.getDatabaseNamePath(event.getDatabaseName())); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().dropDatabase(event.getDatabaseName()); contextManager.getPersistServiceFacade().getListenerAssistedPersistService().deleteDatabaseNameListenerAssisted(event.getDatabaseName()); - refreshShardingSphereStatisticsData(); + refreshStatisticsData(); } - private void refreshShardingSphereStatisticsData() { + private void refreshStatisticsData() { if (contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster() && InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) { new ShardingSphereStatisticsRefreshEngine(contextManager, new GlobalLockContext(new GlobalLockPersistService(repository))).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 b27967830c529..d096dbd732536 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 @@ -57,7 +57,7 @@ public MetaDataChangedSubscriber(final ContextManager contextManager) { @Subscribe public synchronized void renew(final SchemaAddedEvent event) { contextManager.getMetaDataContextManager().getSchemaMetaDataManager().addSchema(event.getDatabaseName(), event.getSchemaName()); - refreshShardingSphereStatisticsData(); + refreshStatisticsData(); } /** @@ -68,7 +68,7 @@ public synchronized void renew(final SchemaAddedEvent event) { @Subscribe public synchronized void renew(final SchemaDeletedEvent event) { contextManager.getMetaDataContextManager().getSchemaMetaDataManager().dropSchema(event.getDatabaseName(), event.getSchemaName()); - refreshShardingSphereStatisticsData(); + refreshStatisticsData(); } /** @@ -84,7 +84,7 @@ public synchronized void renew(final TableCreatedOrAlteredEvent event) { ShardingSphereTable table = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getTable() .load(event.getDatabaseName(), event.getSchemaName(), event.getTableName()); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), table, null); - refreshShardingSphereStatisticsData(); + refreshStatisticsData(); } /** @@ -95,7 +95,7 @@ public synchronized void renew(final TableCreatedOrAlteredEvent event) { @Subscribe public synchronized void renew(final TableDroppedEvent event) { contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), event.getTableName(), null); - refreshShardingSphereStatisticsData(); + refreshStatisticsData(); } /** @@ -111,7 +111,7 @@ public synchronized void renew(final ViewCreatedOrAlteredEvent event) { ShardingSphereView view = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getView() .load(event.getDatabaseName(), event.getSchemaName(), event.getViewName()); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, view); - refreshShardingSphereStatisticsData(); + refreshStatisticsData(); } /** @@ -122,10 +122,10 @@ public synchronized void renew(final ViewCreatedOrAlteredEvent event) { @Subscribe public synchronized void renew(final ViewDroppedEvent event) { contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, event.getViewName()); - refreshShardingSphereStatisticsData(); + refreshStatisticsData(); } - private void refreshShardingSphereStatisticsData() { + private void refreshStatisticsData() { if (contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster() && InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) { new ShardingSphereStatisticsRefreshEngine(contextManager, new GlobalLockContext(new GlobalLockPersistService(repository))).asyncRefresh(); diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/ClusterEventSubscriberRegistryTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/ClusterEventSubscriberRegistryTest.java new file mode 100644 index 0000000000000..17ff210230c5f --- /dev/null +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/ClusterEventSubscriberRegistryTest.java @@ -0,0 +1,39 @@ +/* + * 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; + +import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; +import org.apache.shardingsphere.infra.util.eventbus.EventSubscriber; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +class ClusterEventSubscriberRegistryTest { + + @Test + void assertRegister() { + EventBusContext eventBusContext = mock(EventBusContext.class); + ClusterEventSubscriberRegistry registry = new ClusterEventSubscriberRegistry(eventBusContext); + EventSubscriber subscriber = mock(EventSubscriber.class); + registry.register(Collections.singleton(subscriber)); + verify(eventBusContext).register(subscriber); + } +} diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java index 1518a07cc8486..f5badd706065e 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/persist/service/StandaloneMetaDataManagerPersistService.java @@ -64,15 +64,16 @@ */ public final class StandaloneMetaDataManagerPersistService implements MetaDataManagerPersistService { - private final RuleConfigurationChangedEventBuilder ruleConfigChangedEventBuilder = new RuleConfigurationChangedEventBuilder(); - private final MetaDataPersistService metaDataPersistService; private final MetaDataContextManager metaDataContextManager; + private final RuleConfigurationChangedEventBuilder ruleConfigChangedEventBuilder; + public StandaloneMetaDataManagerPersistService(final PersistRepository repository, final MetaDataContextManager metaDataContextManager) { metaDataPersistService = new MetaDataPersistService(repository); this.metaDataContextManager = metaDataContextManager; + ruleConfigChangedEventBuilder = new RuleConfigurationChangedEventBuilder(); } @Override