Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ClusterEventSubscriberRegistry to mode-core module #34064

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public class RuleItemManager {

private final AtomicReference<MetaDataContexts> metaDataContexts;

private final DatabaseRuleConfigurationManager ruleConfigurationManager;
private final DatabaseRuleConfigurationManager ruleConfigManager;

private final MetaDataPersistService metaDataPersistService;

public RuleItemManager(final AtomicReference<MetaDataContexts> metaDataContexts, final PersistRepository repository, final DatabaseRuleConfigurationManager ruleConfigurationManager) {
public RuleItemManager(final AtomicReference<MetaDataContexts> metaDataContexts, final PersistRepository repository, final DatabaseRuleConfigurationManager ruleConfigManager) {
this.metaDataContexts = metaDataContexts;
this.ruleConfigurationManager = ruleConfigurationManager;
this.ruleConfigManager = ruleConfigManager;
metaDataPersistService = new MetaDataPersistService(repository);
}

Expand All @@ -53,7 +53,7 @@ public RuleItemManager(final AtomicReference<MetaDataContexts> 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());
Expand All @@ -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);
}
}

Expand All @@ -74,15 +74,15 @@ 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);
RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class, event.getType());
RuleConfiguration currentRuleConfig = processor.findRuleConfiguration(metaDataContexts.get().getMetaData().getDatabase(databaseName));
synchronized (this) {
processor.dropRuleItemConfiguration(event, currentRuleConfig);
ruleConfigurationManager.dropRuleConfiguration(databaseName, currentRuleConfig);
ruleConfigManager.dropRuleConfiguration(databaseName, currentRuleConfig);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

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

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

/**
Expand All @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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();
}

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