diff --git a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-config/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-config/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java index a0878eb35e38898860f81f522eaede34b1be4bc7..3b7b6ffde5a3a63f85389d72d499fdd6a3f03804 100644 --- a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-config/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java +++ b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-config/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java @@ -21,16 +21,18 @@ import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.base.Strings; +import com.google.common.eventbus.Subscribe; import org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncryptRuleConfiguration; import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration; import org.apache.shardingsphere.infra.auth.Authentication; import org.apache.shardingsphere.infra.auth.yaml.config.YamlAuthenticationConfiguration; import org.apache.shardingsphere.infra.auth.yaml.swapper.AuthenticationYamlSwapper; -import org.apache.shardingsphere.infra.callback.governance.DataSourceCallback; -import org.apache.shardingsphere.infra.callback.governance.RuleCallback; -import org.apache.shardingsphere.infra.callback.governance.SchemaNameCallback; import org.apache.shardingsphere.infra.config.DataSourceConfiguration; import org.apache.shardingsphere.infra.config.RuleConfiguration; +import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus; +import org.apache.shardingsphere.infra.eventbus.event.DataSourceEvent; +import org.apache.shardingsphere.infra.eventbus.event.RuleEvent; +import org.apache.shardingsphere.infra.eventbus.event.SchemaNameEvent; import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations; import org.apache.shardingsphere.infra.yaml.engine.YamlEngine; import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine; @@ -66,9 +68,7 @@ public final class ConfigCenter { public ConfigCenter(final ConfigurationRepository repository) { node = new ConfigCenterNode(); this.repository = repository; - DataSourceCallback.getInstance().register(this::persistDataSourceConfiguration); - RuleCallback.getInstance().register(this::persistRuleConfigurations); - SchemaNameCallback.getInstance().register(this::persistSchema); + ShardingSphereEventBus.getInstance().register(this); } /** @@ -81,7 +81,7 @@ public final class ConfigCenter { */ public void persistConfigurations(final String schemaName, final Map dataSourceConfigs, final Collection ruleConfigurations, final boolean isOverwrite) { - persistDataSourceConfiguration(schemaName, dataSourceConfigs, isOverwrite); + persistDataSourceConfigurations(schemaName, dataSourceConfigs, isOverwrite); persistRuleConfigurations(schemaName, ruleConfigurations, isOverwrite); // TODO Consider removing the following one. persistSchemaName(schemaName, isOverwrite); @@ -99,14 +99,43 @@ public final class ConfigCenter { persistProperties(props, isOverwrite); } - private void persistDataSourceConfiguration(final String schemaName, final Map dataSourceConfigurations, final boolean isOverwrite) { + /** + * persist data source configurations. + * @param event Data source event. + */ + @Subscribe + public synchronized void renew(final DataSourceEvent event) { + persistDataSourceConfigurations(event.getSchemaName(), event.getDataSourceConfigurations()); + } + + /** + * Persist rule configurations. + * + * @param event Rule event. + */ + @Subscribe + public synchronized void renew(final RuleEvent event) { + persistRuleConfigurations(event.getSchemaName(), event.getRuleConfigurations()); + } + + /** + * Persist schema name. + * + * @param event Schema name event. + */ + @Subscribe + public synchronized void renew(final SchemaNameEvent event) { + persistSchemaName(event.getSchemaName(), event.isOverwrite()); + } + + private void persistDataSourceConfigurations(final String schemaName, final Map dataSourceConfigurations, final boolean isOverwrite) { if (dataSourceConfigurations.isEmpty() || !isOverwrite) { return; } - persistDataSourceConfiguration(schemaName, dataSourceConfigurations); + persistDataSourceConfigurations(schemaName, dataSourceConfigurations); } - private void persistDataSourceConfiguration(final String schemaName, final Map dataSourceConfigurations) { + private void persistDataSourceConfigurations(final String schemaName, final Map dataSourceConfigurations) { Preconditions.checkState(null != dataSourceConfigurations && !dataSourceConfigurations.isEmpty(), "No available data source in `%s` for governance.", schemaName); Map yamlDataSourceConfigurations = dataSourceConfigurations.entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> new DataSourceConfigurationYamlSwapper().swapToYamlConfiguration(entry.getValue()))); diff --git a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-metadata/src/main/java/org/apache/shardingsphere/governance/core/metadata/MetaDataCenter.java b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-metadata/src/main/java/org/apache/shardingsphere/governance/core/metadata/MetaDataCenter.java index 39b758607af5898d683d2c251ba549d281a6eb5f..edf3c001b8526f758503128e62a6188f53d4d134 100644 --- a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-metadata/src/main/java/org/apache/shardingsphere/governance/core/metadata/MetaDataCenter.java +++ b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-metadata/src/main/java/org/apache/shardingsphere/governance/core/metadata/MetaDataCenter.java @@ -18,10 +18,12 @@ package org.apache.shardingsphere.governance.core.metadata; import com.google.common.base.Strings; -import org.apache.shardingsphere.infra.callback.governance.MetaDataCallback; +import com.google.common.eventbus.Subscribe; import org.apache.shardingsphere.governance.repository.api.GovernanceRepository; import org.apache.shardingsphere.governance.core.metadata.yaml.RuleSchemaMetaDataYamlSwapper; import org.apache.shardingsphere.governance.core.metadata.yaml.YamlRuleSchemaMetaData; +import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus; +import org.apache.shardingsphere.infra.eventbus.event.MetaDataEvent; import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData; import org.apache.shardingsphere.infra.yaml.engine.YamlEngine; @@ -39,7 +41,7 @@ public final class MetaDataCenter { public MetaDataCenter(final GovernanceRepository governanceRepository) { node = new MetaDataCenterNode(); repository = governanceRepository; - MetaDataCallback.getInstance().register(this::persistMetaDataCenterNode); + ShardingSphereEventBus.getInstance().register(this); } /** @@ -65,4 +67,14 @@ public final class MetaDataCenter { } return Optional.of(new RuleSchemaMetaDataYamlSwapper().swapToObject(YamlEngine.unmarshal(path, YamlRuleSchemaMetaData.class))); } + + /** + * Persist meta data. + * + * @param event Meta data event. + */ + @Subscribe + public synchronized void renew(final MetaDataEvent event) { + persistMetaDataCenterNode(event.getSchemaName(), event.getMetaData()); + } } diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/Callback.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/Callback.java deleted file mode 100644 index 09dcec557d779a8add929f8e3848734706133bf2..0000000000000000000000000000000000000000 --- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/Callback.java +++ /dev/null @@ -1,52 +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.infra.callback; - -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.function.BiConsumer; - -/** - * Callback. - */ -// TODO rename schema based Callback -public abstract class Callback { - - private final List> consumers = new CopyOnWriteArrayList<>(); - - /** - * Register consumer. - * - * @param consumer consumer - */ - public void register(final BiConsumer consumer) { - consumers.add(consumer); - } - - /** - * Run consumer. - * - * @param schemaName schema name - * @param arg another argument - */ - public void run(final String schemaName, final T arg) { - for (BiConsumer each : consumers) { - each.accept(schemaName, arg); - } - } -} diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/RuleCallback.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/ShardingSphereEventBus.java similarity index 64% rename from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/RuleCallback.java rename to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/ShardingSphereEventBus.java index 86e0ff89d482c93fbaa623a54553e982159f1a4c..9e131ebacffc6db47c300ec57ea4cb6397811de1 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/RuleCallback.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/ShardingSphereEventBus.java @@ -1,43 +1,42 @@ -/* - * 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.infra.callback.governance; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.apache.shardingsphere.infra.callback.Callback; -import org.apache.shardingsphere.infra.config.RuleConfiguration; - -import java.util.Collection; - -/** - * Rule callback. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class RuleCallback extends Callback> { - - private static final RuleCallback INSTANCE = new RuleCallback(); - - /** - * Get instance. - * - * @return rule callback - */ - public static RuleCallback getInstance() { - return INSTANCE; - } -} +/* + * 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.infra.eventbus; + +import com.google.common.eventbus.EventBus; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * ShardingSphere event bus. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ShardingSphereEventBus { + + /** + * Get instance of ShardingSphere event bus. + * + * @return instance of ShardingSphere event bus + */ + public static EventBus getInstance() { + return ShardingSphereEventBusHolder.INSTANCE; + } + + private static final class ShardingSphereEventBusHolder { + private static final EventBus INSTANCE = new EventBus(); + } +} diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/DataSourceCallback.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/DataSourceEvent.java similarity index 60% rename from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/DataSourceCallback.java rename to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/DataSourceEvent.java index 830f366495399b5680af7b1a11b3ab7205ebddaa..9c38d3318bd0a4d9fc5ec75143e1a543f3df8f82 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/DataSourceCallback.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/DataSourceEvent.java @@ -1,43 +1,36 @@ -/* - * 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.infra.callback.governance; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.apache.shardingsphere.infra.callback.Callback; -import org.apache.shardingsphere.infra.config.DataSourceConfiguration; - -import java.util.Map; - -/** - * Data source callback. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class DataSourceCallback extends Callback> { - - private static final DataSourceCallback INSTANCE = new DataSourceCallback(); - - /** - * Get instance. - * - * @return data source callback - */ - public static DataSourceCallback getInstance() { - return INSTANCE; - } -} +/* + * 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.infra.eventbus.event; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.infra.config.DataSourceConfiguration; + +import java.util.Map; + +/** + * Data source event. + */ +@RequiredArgsConstructor +@Getter +public class DataSourceEvent { + + private final String schemaName; + + private final Map dataSourceConfigurations; +} diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/MetaDataCallback.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/MetaDataEvent.java similarity index 61% rename from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/MetaDataCallback.java rename to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/MetaDataEvent.java index ed70269503ef1a30c9125562b6f899ad2a90ed41..c7078b8cad13cbfb383966b89d96ee021bd9ab7c 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/MetaDataCallback.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/MetaDataEvent.java @@ -1,41 +1,34 @@ -/* - * 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.infra.callback.governance; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.apache.shardingsphere.infra.callback.Callback; -import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData; - -/** - * Meta data call back. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class MetaDataCallback extends Callback { - - private static final MetaDataCallback INSTANCE = new MetaDataCallback(); - - /** - * Get instance. - * - * @return meta data call back - */ - public static MetaDataCallback getInstance() { - return INSTANCE; - } -} +/* + * 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.infra.eventbus.event; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData; + +/** + * Meta data event. + */ +@RequiredArgsConstructor +@Getter +public class MetaDataEvent { + + private final String schemaName; + + private final RuleSchemaMetaData metaData; +} diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/SchemaNameCallback.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/RuleEvent.java similarity index 59% rename from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/SchemaNameCallback.java rename to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/RuleEvent.java index dfcdb026c880ff453c6f9bcef97c911838052cf7..11c178d6f264cb1765dc4bed66f292e77cb69906 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/callback/governance/SchemaNameCallback.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/RuleEvent.java @@ -1,40 +1,36 @@ -/* - * 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.infra.callback.governance; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.apache.shardingsphere.infra.callback.Callback; - -/** - * Schema name callback. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class SchemaNameCallback extends Callback { - - private static final SchemaNameCallback INSTANCE = new SchemaNameCallback(); - - /** - * Get instance. - * - * @return data source callback - */ - public static SchemaNameCallback getInstance() { - return INSTANCE; - } -} +/* + * 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.infra.eventbus.event; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.infra.config.RuleConfiguration; + +import java.util.Collection; + +/** + * Rule event. + */ +@RequiredArgsConstructor +@Getter +public class RuleEvent { + + private final String schemaName; + + private final Collection ruleConfigurations; +} diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/SchemaNameEvent.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/SchemaNameEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..11164e3191afe3909b41dc6ab452df23f7872558 --- /dev/null +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/eventbus/event/SchemaNameEvent.java @@ -0,0 +1,33 @@ +/* + * 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.infra.eventbus.event; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +/** + * Schema name event. + */ +@RequiredArgsConstructor +@Getter +public class SchemaNameEvent { + + private final String schemaName; + + private final boolean overwrite; +} diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/callback/CallbackTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/callback/CallbackTest.java deleted file mode 100644 index 834a117d75afea590dcf20f209fe4ef565ff675c..0000000000000000000000000000000000000000 --- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/callback/CallbackTest.java +++ /dev/null @@ -1,74 +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.infra.callback; - -import org.apache.shardingsphere.infra.callback.governance.DataSourceCallback; -import org.apache.shardingsphere.infra.callback.governance.MetaDataCallback; -import org.apache.shardingsphere.infra.callback.governance.RuleCallback; -import org.apache.shardingsphere.infra.config.DataSourceConfiguration; -import org.apache.shardingsphere.infra.config.RuleConfiguration; -import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; - -public final class CallbackTest { - - private static final String TEST = "test"; - - @Test - public void assertRun() { - MetaDataCallback.getInstance().run(TEST, new RuleSchemaMetaData(null, null)); - MetaDataCallback.getInstance().register((schemaName, ruleSchemaMetaData) -> { - assertThat(schemaName, is(TEST)); - assertThat(ruleSchemaMetaData.getClass().getName(), is(RuleSchemaMetaData.class.getName())); - }); - MetaDataCallback.getInstance().run(TEST, new RuleSchemaMetaData(null, null)); - - DataSourceCallback.getInstance().run(TEST, new HashMap<>()); - DataSourceCallback.getInstance().register((schemaName, map) -> { - assertThat(schemaName, is(TEST)); - assertThat(map.size(), is(1)); - map.forEach((k, v) -> assertThat(v.getClass().getName(), is(DataSourceConfiguration.class.getName()))); - }); - Map maps = new HashMap<>(); - DataSourceConfiguration configuration = new DataSourceConfiguration("test"); - maps.put(TEST, configuration); - DataSourceCallback.getInstance().run(TEST, maps); - - RuleCallback.getInstance().run(TEST, new ArrayList<>()); - RuleCallback.getInstance().register((schemaName, ruleConfigurations) -> { - assertThat(schemaName, is(TEST)); - assertFalse(ruleConfigurations.isEmpty()); - ruleConfigurations.forEach(each -> assertThat(each.getClass().getName(), is(TestRuleConfiguration.class.getName()))); - }); - Collection ruleConfigurations = new ArrayList<>(); - ruleConfigurations.add(new TestRuleConfiguration()); - RuleCallback.getInstance().run(TEST, ruleConfigurations); - } - - static class TestRuleConfiguration implements RuleConfiguration { - } -} diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/eventbus/ShardingSphereEventBusTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/eventbus/ShardingSphereEventBusTest.java new file mode 100644 index 0000000000000000000000000000000000000000..ed488251e2b79929fef92b9157a9ace5ce25b13d --- /dev/null +++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/eventbus/ShardingSphereEventBusTest.java @@ -0,0 +1,31 @@ +/* + * 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.infra.eventbus; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class ShardingSphereEventBusTest { + + @Test + public void assertInstance() { + assertThat(ShardingSphereEventBus.getInstance(), is(ShardingSphereEventBus.getInstance())); + } +} diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/PreparedStatementExecutor.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/PreparedStatementExecutor.java index 9e013d3d17456cea44edfb27f7c37a9707eaab22..1c984065888f1418ff18a610fd82eab91d28c632 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/PreparedStatementExecutor.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/PreparedStatementExecutor.java @@ -18,10 +18,11 @@ package org.apache.shardingsphere.driver.executor; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.callback.governance.MetaDataCallback; import org.apache.shardingsphere.infra.context.SchemaContext; import org.apache.shardingsphere.infra.context.SchemaContexts; import org.apache.shardingsphere.infra.database.DefaultSchema; +import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus; +import org.apache.shardingsphere.infra.eventbus.event.MetaDataEvent; import org.apache.shardingsphere.infra.executor.kernel.InputGroup; import org.apache.shardingsphere.infra.executor.sql.ConnectionMode; import org.apache.shardingsphere.infra.executor.sql.QueryResult; @@ -171,7 +172,7 @@ public final class PreparedStatementExecutor { refreshStrategy.get().refreshMetaData(schemaContext.getSchema().getMetaData(), schemaContexts.getDatabaseType(), dataSourceMap, sqlStatementContext, tableName -> metaDataLoader.load(schemaContexts.getDatabaseType(), dataSourceMap, tableName, schemaContexts.getProps())); - MetaDataCallback.getInstance().run(DefaultSchema.LOGIC_NAME, schemaContext.getSchema().getMetaData().getSchema()); + ShardingSphereEventBus.getInstance().post(new MetaDataEvent(DefaultSchema.LOGIC_NAME, schemaContext.getSchema().getMetaData().getSchema())); } } } diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/StatementExecutor.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/StatementExecutor.java index 4a6b1c97986d12eff4a582f5b58ac79205bfa472..9144a564560e5460195b135595c2c65cc60466a8 100644 --- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/StatementExecutor.java +++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/StatementExecutor.java @@ -18,10 +18,11 @@ package org.apache.shardingsphere.driver.executor; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.callback.governance.MetaDataCallback; import org.apache.shardingsphere.infra.context.SchemaContext; import org.apache.shardingsphere.infra.context.SchemaContexts; import org.apache.shardingsphere.infra.database.DefaultSchema; +import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus; +import org.apache.shardingsphere.infra.eventbus.event.MetaDataEvent; import org.apache.shardingsphere.infra.executor.kernel.InputGroup; import org.apache.shardingsphere.infra.executor.sql.ConnectionMode; import org.apache.shardingsphere.infra.executor.sql.QueryResult; @@ -246,7 +247,7 @@ public final class StatementExecutor { RuleSchemaMetaDataLoader metaDataLoader = new RuleSchemaMetaDataLoader(schemaContext.getSchema().getRules()); refreshStrategy.get().refreshMetaData(schemaContext.getSchema().getMetaData(), schemaContexts.getDatabaseType(), dataSourceMap, sqlStatementContext, tableName -> metaDataLoader.load(schemaContexts.getDatabaseType(), dataSourceMap, tableName, schemaContexts.getProps())); - MetaDataCallback.getInstance().run(DefaultSchema.LOGIC_NAME, schemaContext.getSchema().getMetaData().getSchema()); + ShardingSphereEventBus.getInstance().post(new MetaDataEvent(DefaultSchema.LOGIC_NAME, schemaContext.getSchema().getMetaData().getSchema())); } } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java index 863b8d3a18da2e2f84453d433e8798f1851c51a2..dcf3421ea36d07947e64ca98518a1b3e1a810f8e 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java @@ -18,10 +18,11 @@ package org.apache.shardingsphere.proxy.backend.communication.jdbc; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.callback.governance.MetaDataCallback; import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException; import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.context.SchemaContext; +import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus; +import org.apache.shardingsphere.infra.eventbus.event.MetaDataEvent; import org.apache.shardingsphere.infra.executor.sql.QueryResult; import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext; import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger; @@ -132,7 +133,7 @@ public final class JDBCDatabaseCommunicationEngine implements DatabaseCommunicat if (refreshStrategy.isPresent()) { refreshStrategy.get().refreshMetaData(schema.getSchema().getMetaData(), ProxySchemaContexts.getInstance().getSchemaContexts().getDatabaseType(), schema.getSchema().getDataSources(), sqlStatementContext, this::loadTableMetaData); - MetaDataCallback.getInstance().run(schema.getName(), schema.getSchema().getMetaData().getSchema()); + ShardingSphereEventBus.getInstance().post(new MetaDataEvent(schema.getName(), schema.getSchema().getMetaData().getSchema())); } } diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/RDLBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/RDLBackendHandler.java index 1982678dd01554c07d05056f5951f4ddf437bdc5..77f344830229879f9ba56e6ea4ef16cc0b4e8dae 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/RDLBackendHandler.java +++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/RDLBackendHandler.java @@ -18,13 +18,14 @@ package org.apache.shardingsphere.proxy.backend.text.admin; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.callback.governance.DataSourceCallback; -import org.apache.shardingsphere.infra.callback.governance.RuleCallback; -import org.apache.shardingsphere.infra.callback.governance.SchemaNameCallback; import org.apache.shardingsphere.infra.config.DataSourceConfiguration; import org.apache.shardingsphere.infra.config.RuleConfiguration; import org.apache.shardingsphere.infra.context.impl.StandardSchemaContexts; import org.apache.shardingsphere.infra.database.type.DatabaseType; +import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus; +import org.apache.shardingsphere.infra.eventbus.event.DataSourceEvent; +import org.apache.shardingsphere.infra.eventbus.event.RuleEvent; +import org.apache.shardingsphere.infra.eventbus.event.SchemaNameEvent; import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine; import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection; import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException; @@ -78,8 +79,8 @@ public final class RDLBackendHandler implements TextProtocolBackendHandler { if (ProxySchemaContexts.getInstance().getSchemaNames().contains(context.getSqlStatement().getDatabaseName())) { return new ErrorResponse(new DBCreateExistsException(context.getSqlStatement().getDatabaseName())); } - SchemaNameCallback.getInstance().run(context.getSqlStatement().getDatabaseName(), false); // TODO Need to get the executed feedback from registry center for returning. + ShardingSphereEventBus.getInstance().post(new SchemaNameEvent(context.getSqlStatement().getDatabaseName(), true)); UpdateResponse result = new UpdateResponse(); result.setType("CREATE"); return result; @@ -89,8 +90,8 @@ public final class RDLBackendHandler implements TextProtocolBackendHandler { if (!ProxySchemaContexts.getInstance().getSchemaNames().contains(context.getSqlStatement().getDatabaseName())) { return new ErrorResponse(new DBCreateExistsException(context.getSqlStatement().getDatabaseName())); } - SchemaNameCallback.getInstance().run(context.getSqlStatement().getDatabaseName(), true); // TODO Need to get the executed feedback from registry center for returning. + ShardingSphereEventBus.getInstance().post(new SchemaNameEvent(context.getSqlStatement().getDatabaseName(), true)); UpdateResponse result = new UpdateResponse(); result.setType("DROP"); return result; @@ -100,7 +101,7 @@ public final class RDLBackendHandler implements TextProtocolBackendHandler { Map parameters = new CreateDataSourcesStatementContextConverter().convert(context); Map dataSources = DataSourceConverter.getDataSourceConfigurationMap(DataSourceConverter.getDataSourceParameterMap2(parameters)); // TODO Need to get the executed feedback from registry center for returning. - DataSourceCallback.getInstance().run(backendConnection.getSchema(), dataSources); + ShardingSphereEventBus.getInstance().post(new DataSourceEvent(backendConnection.getSchema(), dataSources)); UpdateResponse result = new UpdateResponse(); result.setType("CREATE"); return result; @@ -110,7 +111,7 @@ public final class RDLBackendHandler implements TextProtocolBackendHandler { YamlShardingRuleConfiguration configurations = new CreateShardingRuleStatementContextConverter().convert(context); Collection rules = new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(configurations)); // TODO Need to get the executed feedback from registry center for returning. - RuleCallback.getInstance().run(backendConnection.getSchema(), rules); + ShardingSphereEventBus.getInstance().post(new RuleEvent(backendConnection.getSchema(), rules)); UpdateResponse result = new UpdateResponse(); result.setType("CREATE"); return result;