From f61784c0a8e6a9b70ccd30870a82ce9fe005c6ce Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 10 Jan 2025 19:42:03 +0800 Subject: [PATCH 1/3] Add ActiveVersionChecker --- .../checker/ActiveVersionChecker.java | 36 +++++++++++++++++++ .../metadata/StorageUnitChangedHandler.java | 7 ++-- .../metadata/TableChangedHandler.java | 5 ++- .../database/metadata/ViewChangedHandler.java | 5 ++- .../global/GlobalRuleChangedHandler.java | 4 +-- .../global/PropertiesChangedHandler.java | 5 ++- 6 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java new file mode 100644 index 0000000000000..ff6a29dba7178 --- /dev/null +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java @@ -0,0 +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.mode.manager.cluster.dispatch.checker; + +import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.manager.ContextManager; + +/** + * Active version checker. + */ +public final class ActiveVersionChecker { + + /** + * Check active version. + */ + public static void checkActiveVersion(final ContextManager contextManager, final DataChangedEvent event) { + ShardingSpherePreconditions.checkState(event.getValue().equals(contextManager.getPersistServiceFacade().getRepository().query(event.getKey())), + () -> new IllegalStateException(String.format("Invalid active version: %s of key: %s", event.getValue(), event.getKey()))); + } +} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/StorageUnitChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/StorageUnitChangedHandler.java index b9a3100ce8d47..cc1ebcf92411f 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/StorageUnitChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/StorageUnitChangedHandler.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.manager.cluster.dispatch.checker.ActiveVersionChecker; import java.util.Collections; @@ -41,8 +42,7 @@ public final class StorageUnitChangedHandler { * @param event data changed event */ public void handleRegistered(final String databaseName, final String dataSourceUnitName, final DataChangedEvent event) { - Preconditions.checkArgument(event.getValue().equals(contextManager.getPersistServiceFacade().getRepository().query(event.getKey())), - "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + ActiveVersionChecker.checkActiveVersion(contextManager, event); DataSourcePoolProperties dataSourcePoolProps = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load(databaseName, dataSourceUnitName); contextManager.getMetaDataContextManager().getStorageUnitManager().registerStorageUnit(databaseName, Collections.singletonMap(dataSourceUnitName, dataSourcePoolProps)); } @@ -55,8 +55,7 @@ public void handleRegistered(final String databaseName, final String dataSourceU * @param event data changed event */ public void handleAltered(final String databaseName, final String dataSourceUnitName, final DataChangedEvent event) { - Preconditions.checkArgument(event.getValue().equals(contextManager.getPersistServiceFacade().getRepository().query(event.getKey())), - "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + ActiveVersionChecker.checkActiveVersion(contextManager, event); DataSourcePoolProperties dataSourcePoolProps = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load(databaseName, dataSourceUnitName); contextManager.getMetaDataContextManager().getStorageUnitManager().alterStorageUnit(databaseName, Collections.singletonMap(dataSourceUnitName, dataSourcePoolProps)); } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/TableChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/TableChangedHandler.java index e77e047dcce4d..5c957759e1727 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/TableChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/TableChangedHandler.java @@ -17,11 +17,11 @@ package org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.database.metadata; -import com.google.common.base.Preconditions; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.metadata.persist.node.metadata.TableMetaDataNodePath; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.manager.cluster.dispatch.checker.ActiveVersionChecker; import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; /** @@ -47,8 +47,7 @@ public TableChangedHandler(final ContextManager contextManager) { */ public void handleCreatedOrAltered(final String databaseName, final String schemaName, final DataChangedEvent event) { String tableName = TableMetaDataNodePath.getTableNameByActiveVersionPath(event.getKey()).orElseThrow(() -> new IllegalStateException("Table name not found.")); - Preconditions.checkArgument(event.getValue().equals(contextManager.getPersistServiceFacade().getRepository().query(event.getKey())), - "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + ActiveVersionChecker.checkActiveVersion(contextManager, event); ShardingSphereTable table = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getTable().load(databaseName, schemaName, tableName); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(databaseName, schemaName, table, null); statisticsRefreshEngine.asyncRefresh(); diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/ViewChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/ViewChangedHandler.java index 7a2a70d89f0ab..b8c65bfbb91f8 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/ViewChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/database/metadata/ViewChangedHandler.java @@ -17,11 +17,11 @@ package org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.database.metadata; -import com.google.common.base.Preconditions; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; import org.apache.shardingsphere.metadata.persist.node.metadata.ViewMetaDataNodePath; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.manager.cluster.dispatch.checker.ActiveVersionChecker; import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; /** @@ -47,8 +47,7 @@ public ViewChangedHandler(final ContextManager contextManager) { */ public void handleCreatedOrAltered(final String databaseName, final String schemaName, final DataChangedEvent event) { String viewName = ViewMetaDataNodePath.getViewNameByActiveVersionPath(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found.")); - Preconditions.checkArgument(event.getValue().equals(contextManager.getPersistServiceFacade().getRepository().query(event.getKey())), - "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + ActiveVersionChecker.checkActiveVersion(contextManager, event); ShardingSphereView view = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load(databaseName, schemaName, viewName); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(databaseName, schemaName, null, view); statisticsRefreshEngine.asyncRefresh(); diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/GlobalRuleChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/GlobalRuleChangedHandler.java index d60e2b3328834..37456d371e165 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/GlobalRuleChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/GlobalRuleChangedHandler.java @@ -24,6 +24,7 @@ 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.dispatch.checker.ActiveVersionChecker; import org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.DataChangedEventHandler; import org.apache.shardingsphere.mode.spi.RuleConfigurationPersistDecorator; @@ -56,8 +57,7 @@ public void handle(final ContextManager contextManager, final DataChangedEvent e if (!ruleName.isPresent()) { return; } - Preconditions.checkArgument(event.getValue().equals(contextManager.getPersistServiceFacade().getRepository().query(event.getKey())), - "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + ActiveVersionChecker.checkActiveVersion(contextManager, event); Optional ruleConfig = contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load(ruleName.get()); Preconditions.checkArgument(ruleConfig.isPresent(), "Can not find rule configuration with name: %s", ruleName.get()); contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterGlobalRuleConfiguration( diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/PropertiesChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/PropertiesChangedHandler.java index feac32b96e0a8..a3d4468ce377c 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/PropertiesChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/PropertiesChangedHandler.java @@ -17,11 +17,11 @@ package org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.global; -import com.google.common.base.Preconditions; import org.apache.shardingsphere.metadata.persist.node.GlobalNodePath; 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.dispatch.checker.ActiveVersionChecker; import org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.DataChangedEventHandler; import java.util.Arrays; @@ -47,8 +47,7 @@ public void handle(final ContextManager contextManager, final DataChangedEvent e if (!org.apache.shardingsphere.mode.path.GlobalNodePath.isPropsActiveVersionPath(event.getKey())) { return; } - Preconditions.checkArgument(event.getValue().equals(contextManager.getPersistServiceFacade().getRepository().query(event.getKey())), - "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + ActiveVersionChecker.checkActiveVersion(contextManager, event); contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterProperties(contextManager.getPersistServiceFacade().getMetaDataPersistService().getPropsService().load()); } } From 4b74c9fb2708a96e17190a35852b2abd58f56878 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 10 Jan 2025 19:47:02 +0800 Subject: [PATCH 2/3] Add ActiveVersionChecker --- .../cluster/dispatch/checker/ActiveVersionChecker.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java index ff6a29dba7178..c10a93a87b1e3 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java @@ -17,6 +17,8 @@ package org.apache.shardingsphere.mode.manager.cluster.dispatch.checker; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -24,10 +26,14 @@ /** * Active version checker. */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) public final class ActiveVersionChecker { /** * Check active version. + * + * @param contextManager context manager + * @param event data changed event */ public static void checkActiveVersion(final ContextManager contextManager, final DataChangedEvent event) { ShardingSpherePreconditions.checkState(event.getValue().equals(contextManager.getPersistServiceFacade().getRepository().query(event.getKey())), From 2c580eefba98e2c2cd5f512fffcde9ee4cb5fb7a Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 10 Jan 2025 19:48:41 +0800 Subject: [PATCH 3/3] Add ActiveVersionChecker --- .../manager/cluster/dispatch/checker/ActiveVersionChecker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java index c10a93a87b1e3..8df8f227773f6 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/checker/ActiveVersionChecker.java @@ -31,7 +31,7 @@ public final class ActiveVersionChecker { /** * Check active version. - * + * * @param contextManager context manager * @param event data changed event */