From 38c0669d053a583b56e5dae717f808e416743cf0 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Tue, 31 Dec 2024 23:41:07 +0800 Subject: [PATCH 01/10] Use DataChangedEventHandler to instead of DispatchEventBuilder (#34212) * Use DataChangedEventHandler to instead of DispatchEventBuilder --- .../type/GlobalRuleDispatchEventBuilder.java | 54 ------- .../ListenerAssistedDispatchEventBuilder.java | 58 ------- ...hardingSphereDataDispatchEventBuilder.java | 133 --------------- .../CreateDatabaseListenerAssistedEvent.java | 32 ---- .../DropDatabaseListenerAssistedEvent.java | 32 ---- .../AlterGlobalRuleConfigurationEvent.java | 36 ----- .../event/config/AlterPropertiesEvent.java | 34 ---- .../metadata/data/DatabaseDataAddedEvent.java | 32 ---- .../data/DatabaseDataDeletedEvent.java | 32 ---- .../metadata/data/SchemaDataAddedEvent.java | 34 ---- .../metadata/data/SchemaDataDeletedEvent.java | 34 ---- .../ShardingSphereRowDataChangedEvent.java | 39 ----- .../ShardingSphereRowDataDeletedEvent.java | 38 ----- .../metadata/data/TableDataChangedEvent.java | 38 ----- .../state/cluster/ClusterStateEvent.java | 33 ---- .../ComputeNodeInstanceStateChangedEvent.java | 34 ---- .../KillLocalProcessCompletedEvent.java | 32 ---- .../state/compute/KillLocalProcessEvent.java | 34 ---- .../ReportLocalProcessesCompletedEvent.java | 32 ---- .../compute/ReportLocalProcessesEvent.java | 34 ---- .../event/state/compute/WorkerIdEvent.java | 34 ---- .../instance/InstanceOfflineEvent.java | 33 ---- .../compute/instance/InstanceOnlineEvent.java | 33 ---- .../QualifiedDataSourceStateEvent.java | 36 ----- .../DataChangedEventHandler.java} | 38 +++-- .../type/ClusterStateChangedHandler.java} | 18 +-- .../type/ComputeNodeOnlineHandler.java} | 31 ++-- .../type/ComputeNodeStateChangedHandler.java} | 45 ++---- .../type/GlobalRuleChangedHandler.java} | 54 ++++--- .../type/KillProcessHandler.java} | 43 ++--- .../type/ListenerAssistedChangedHandler.java | 69 ++++++++ .../type/PropertiesChangedHandler.java} | 22 ++- .../QualifiedDataSourceChangedHandler.java} | 32 ++-- .../ShardingSphereDataChangedHandler.java | 132 +++++++++++++++ .../type/ShowProcessListHandler.java} | 34 ++-- .../DataChangedEventListenerRegistry.java | 10 ++ .../type/GlobalMetaDataChangedHandler.java} | 27 ++-- ...lusterDispatchEventSubscriberRegistry.java | 18 +-- .../type/ComputeNodeStateSubscriber.java | 96 ----------- .../type/DatabaseDataChangedSubscriber.java | 117 -------------- .../type/ListenerAssistedSubscriber.java | 79 --------- .../type/ProcessListChangedSubscriber.java | 93 ----------- .../type/PropertiesEventSubscriber.java | 47 ------ .../type/QualifiedDataSourceSubscriber.java | 53 ------ ...vent.dispatch.builder.DispatchEventBuilder | 11 -- ...t.dispatch.handler.DataChangedEventHandler | 27 ++++ .../ClusterStateDispatchEventBuilderTest.java | 55 ------- ...uteNodeOnlineDispatchEventBuilderTest.java | 77 --------- ...puteNodeStateDispatchEventBuilderTest.java | 105 ------------ .../GlobalRuleDispatchEventBuilderTest.java | 56 ------- .../KillProcessDispatchEventBuilderTest.java | 75 --------- ...tenerAssistedDispatchEventBuilderTest.java | 62 ------- .../PropertiesDispatchEventBuilderTest.java | 54 ------- ...iedDataSourceDispatchEventBuilderTest.java | 64 -------- ...ingSphereDataDispatchEventBuilderTest.java | 152 ------------------ ...owProcessListDispatchEventBuilderTest.java | 75 --------- .../type/ClusterStateChangedHandlerTest.java} | 26 ++- .../type/ComputeNodeOnlineHandlerTest.java} | 57 +++---- .../ComputeNodeStateChangedHandlerTest.java | 74 +++++++++ .../type/GlobalRuleChangedHandlerTest.java} | 43 +++-- .../handler/type/KillProcessHandlerTest.java | 83 ++++++++++ .../ListenerAssistedChangedHandlerTest.java} | 28 ++-- .../type/PropertiesChangedHandlerTest.java} | 36 +++-- ...ualifiedDataSourceChangedHandlerTest.java} | 37 +++-- .../ShardingSphereDataChangedHandlerTest.java | 126 +++++++++++++++ .../type/ShowProcessListHandlerTest.java} | 57 +++---- .../DataChangedEventListenerRegistryTest.java | 4 +- .../type/CacheEvictedSubscriberTest.java | 7 +- .../DatabaseDataChangedSubscriberTest.java | 94 ----------- 69 files changed, 869 insertions(+), 2635 deletions(-) delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/GlobalRuleDispatchEventBuilder.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ListenerAssistedDispatchEventBuilder.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShardingSphereDataDispatchEventBuilder.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/assisted/CreateDatabaseListenerAssistedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/assisted/DropDatabaseListenerAssistedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterGlobalRuleConfigurationEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterPropertiesEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/DatabaseDataAddedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/DatabaseDataDeletedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/SchemaDataAddedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/SchemaDataDeletedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/ShardingSphereRowDataChangedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/ShardingSphereRowDataDeletedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/TableDataChangedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/cluster/ClusterStateEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ComputeNodeInstanceStateChangedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/KillLocalProcessCompletedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/KillLocalProcessEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ReportLocalProcessesCompletedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ReportLocalProcessesEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/WorkerIdEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/instance/InstanceOfflineEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/instance/InstanceOnlineEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/storage/QualifiedDataSourceStateEvent.java rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{event/state/compute/LabelsEvent.java => handler/DataChangedEventHandler.java} (52%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{builder/type/ClusterStateDispatchEventBuilder.java => handler/type/ClusterStateChangedHandler.java} (73%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{builder/type/ComputeNodeOnlineDispatchEventBuilder.java => handler/type/ComputeNodeOnlineHandler.java} (73%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{builder/type/ComputeNodeStateDispatchEventBuilder.java => handler/type/ComputeNodeStateChangedHandler.java} (50%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{subscriber/type/GlobalRuleConfigurationEventSubscriber.java => handler/type/GlobalRuleChangedHandler.java} (60%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{builder/type/KillProcessDispatchEventBuilder.java => handler/type/KillProcessHandler.java} (59%) create mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandler.java rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{builder/type/PropertiesDispatchEventBuilder.java => handler/type/PropertiesChangedHandler.java} (60%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{builder/type/QualifiedDataSourceDispatchEventBuilder.java => handler/type/QualifiedDataSourceChangedHandler.java} (61%) create mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandler.java rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{builder/type/ShowProcessListDispatchEventBuilder.java => handler/type/ShowProcessListHandler.java} (63%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{subscriber/type/StateChangedSubscriber.java => listener/type/GlobalMetaDataChangedHandler.java} (65%) delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ComputeNodeStateSubscriber.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/DatabaseDataChangedSubscriber.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ListenerAssistedSubscriber.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ProcessListChangedSubscriber.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriber.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/QualifiedDataSourceSubscriber.java create mode 100644 mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ClusterStateDispatchEventBuilderTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeOnlineDispatchEventBuilderTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeStateDispatchEventBuilderTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/GlobalRuleDispatchEventBuilderTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/KillProcessDispatchEventBuilderTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ListenerAssistedDispatchEventBuilderTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilderTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/QualifiedDataSourceDispatchEventBuilderTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShardingSphereDataDispatchEventBuilderTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShowProcessListDispatchEventBuilderTest.java rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{subscriber/type/StateChangedSubscriberTest.java => handler/type/ClusterStateChangedHandlerTest.java} (61%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{subscriber/type/ComputeNodeStateSubscriberTest.java => handler/type/ComputeNodeOnlineHandlerTest.java} (51%) create mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandlerTest.java rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{subscriber/type/PropertiesEventSubscriberTest.java => handler/type/GlobalRuleChangedHandlerTest.java} (50%) create mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{subscriber/type/ListenerAssistedSubscriberTest.java => handler/type/ListenerAssistedChangedHandlerTest.java} (72%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{subscriber/type/GlobalRuleConfigurationEventSubscriberTest.java => handler/type/PropertiesChangedHandlerTest.java} (59%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{subscriber/type/QualifiedDataSourceSubscriberTest.java => handler/type/QualifiedDataSourceChangedHandlerTest.java} (59%) create mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandlerTest.java rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/{subscriber/type/ProcessListChangedSubscriberTest.java => handler/type/ShowProcessListHandlerTest.java} (54%) delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/DatabaseDataChangedSubscriberTest.java diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/GlobalRuleDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/GlobalRuleDispatchEventBuilder.java deleted file mode 100644 index 0c54632e5dfdc..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/GlobalRuleDispatchEventBuilder.java +++ /dev/null @@ -1,54 +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.mode.manager.cluster.event.dispatch.builder.type; - -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.config.AlterGlobalRuleConfigurationEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; -import org.apache.shardingsphere.mode.path.GlobalNodePath; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Optional; - -/** - * Global rule dispatch event builder. - */ -public final class GlobalRuleDispatchEventBuilder implements DispatchEventBuilder { - - @Override - public String getSubscribedKey() { - return GlobalNode.getGlobalRuleRootNode(); - } - - @Override - public Collection getSubscribedTypes() { - return Arrays.asList(Type.ADDED, Type.UPDATED); - } - - @Override - public Optional build(final DataChangedEvent event) { - if (GlobalNodePath.isRuleActiveVersionPath(event.getKey())) { - return GlobalNodePath.getRuleName(event.getKey()).map(optional -> new AlterGlobalRuleConfigurationEvent(optional, event.getKey(), event.getValue())); - } - return Optional.empty(); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ListenerAssistedDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ListenerAssistedDispatchEventBuilder.java deleted file mode 100644 index 14bc93274cc02..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ListenerAssistedDispatchEventBuilder.java +++ /dev/null @@ -1,58 +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.mode.manager.cluster.event.dispatch.builder.type; - -import org.apache.shardingsphere.metadata.persist.node.StatesNode; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.CreateDatabaseListenerAssistedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.DropDatabaseListenerAssistedEvent; -import org.apache.shardingsphere.mode.persist.service.unified.ListenerAssistedType; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Optional; - -/** - * Listener assisted dispatch event builder. - */ -public final class ListenerAssistedDispatchEventBuilder implements DispatchEventBuilder { - - @Override - public String getSubscribedKey() { - return StatesNode.getListenerAssistedNodePath(); - } - - @Override - public Collection getSubscribedTypes() { - return Arrays.asList(Type.ADDED, Type.UPDATED); - } - - @Override - public Optional build(final DataChangedEvent event) { - return StatesNode.getDatabaseNameByListenerAssistedNodePath(event.getKey()).map(optional -> createDatabaseListenerAssistedEvent(optional, event)); - } - - private DispatchEvent createDatabaseListenerAssistedEvent(final String databaseName, final DataChangedEvent event) { - return ListenerAssistedType.CREATE_DATABASE == ListenerAssistedType.valueOf(event.getValue()) - ? new CreateDatabaseListenerAssistedEvent(databaseName) - : new DropDatabaseListenerAssistedEvent(databaseName); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShardingSphereDataDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShardingSphereDataDispatchEventBuilder.java deleted file mode 100644 index be8c172ecb920..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShardingSphereDataDispatchEventBuilder.java +++ /dev/null @@ -1,133 +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.mode.manager.cluster.event.dispatch.builder.type; - -import com.google.common.base.Strings; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; -import org.apache.shardingsphere.metadata.persist.node.ShardingSphereDataNode; -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.DatabaseDataAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.DatabaseDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.SchemaDataAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.SchemaDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.ShardingSphereRowDataChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.ShardingSphereRowDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.TableDataChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Optional; - -/** - * ShardingSphere data dispatch event builder. - */ -public final class ShardingSphereDataDispatchEventBuilder implements DispatchEventBuilder { - - @Override - public String getSubscribedKey() { - return ShardingSphereDataNode.getShardingSphereDataNodePath(); - } - - @Override - public Collection getSubscribedTypes() { - return Arrays.asList(Type.ADDED, Type.UPDATED, Type.DELETED); - } - - @Override - public Optional build(final DataChangedEvent event) { - Optional databaseName = ShardingSphereDataNode.getDatabaseName(event.getKey()); - if (databaseName.isPresent()) { - return createDatabaseChangedEvent(event, databaseName.get()); - } - databaseName = ShardingSphereDataNode.getDatabaseNameByDatabasePath(event.getKey()); - if (!databaseName.isPresent()) { - return Optional.empty(); - } - Optional schemaName = ShardingSphereDataNode.getSchemaName(event.getKey()); - if (schemaName.isPresent()) { - return createSchemaChangedEvent(event, databaseName.get(), schemaName.get()); - } - schemaName = ShardingSphereDataNode.getSchemaNameBySchemaPath(event.getKey()); - if (!schemaName.isPresent()) { - return Optional.empty(); - } - Optional tableName = ShardingSphereDataNode.getTableName(event.getKey()); - if (tableName.isPresent()) { - return createTableChangedEvent(event, databaseName.get(), schemaName.get(), tableName.get()); - } - tableName = ShardingSphereDataNode.getTableNameByRowPath(event.getKey()); - if (!tableName.isPresent()) { - return Optional.empty(); - } - Optional rowPath = ShardingSphereDataNode.getRowUniqueKey(event.getKey()); - if (rowPath.isPresent()) { - return createRowDataChangedEvent(event, databaseName.get(), schemaName.get(), tableName.get(), rowPath.get()); - } - return Optional.empty(); - } - - private Optional createDatabaseChangedEvent(final DataChangedEvent event, final String databaseName) { - switch (event.getType()) { - case ADDED: - case UPDATED: - return Optional.of(new DatabaseDataAddedEvent(databaseName)); - case DELETED: - return Optional.of(new DatabaseDataDeletedEvent(databaseName)); - default: - return Optional.empty(); - } - } - - private Optional createSchemaChangedEvent(final DataChangedEvent event, final String databaseName, final String schemaName) { - switch (event.getType()) { - case ADDED: - case UPDATED: - return Optional.of(new SchemaDataAddedEvent(databaseName, schemaName)); - case DELETED: - return Optional.of(new SchemaDataDeletedEvent(databaseName, schemaName)); - default: - return Optional.empty(); - } - } - - private Optional createTableChangedEvent(final DataChangedEvent event, final String databaseName, final String schemaName, final String tableName) { - switch (event.getType()) { - case ADDED: - case UPDATED: - return Optional.of(new TableDataChangedEvent(databaseName, schemaName, tableName, null)); - case DELETED: - return Optional.of(new TableDataChangedEvent(databaseName, schemaName, null, tableName)); - default: - return Optional.empty(); - } - } - - private Optional createRowDataChangedEvent(final DataChangedEvent event, final String databaseName, final String schemaName, final String tableName, final String rowPath) { - if ((Type.ADDED == event.getType() || Type.UPDATED == event.getType()) && !Strings.isNullOrEmpty(event.getValue())) { - return Optional.of(new ShardingSphereRowDataChangedEvent(databaseName, schemaName, tableName, YamlEngine.unmarshal(event.getValue(), YamlShardingSphereRowData.class))); - } - if (Type.DELETED == event.getType()) { - return Optional.of(new ShardingSphereRowDataDeletedEvent(databaseName, schemaName, tableName, rowPath)); - } - return Optional.empty(); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/assisted/CreateDatabaseListenerAssistedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/assisted/CreateDatabaseListenerAssistedEvent.java deleted file mode 100644 index 40607549bd672..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/assisted/CreateDatabaseListenerAssistedEvent.java +++ /dev/null @@ -1,32 +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.mode.manager.cluster.event.dispatch.event.assisted; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Create database listener assisted event. - */ -@RequiredArgsConstructor -@Getter -public final class CreateDatabaseListenerAssistedEvent implements DispatchEvent { - - private final String databaseName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/assisted/DropDatabaseListenerAssistedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/assisted/DropDatabaseListenerAssistedEvent.java deleted file mode 100644 index 4c43fc03e2e43..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/assisted/DropDatabaseListenerAssistedEvent.java +++ /dev/null @@ -1,32 +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.mode.manager.cluster.event.dispatch.event.assisted; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Drop database listener assisted event. - */ -@RequiredArgsConstructor -@Getter -public final class DropDatabaseListenerAssistedEvent implements DispatchEvent { - - private final String databaseName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterGlobalRuleConfigurationEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterGlobalRuleConfigurationEvent.java deleted file mode 100644 index 2ca8a6559ee3b..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterGlobalRuleConfigurationEvent.java +++ /dev/null @@ -1,36 +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.mode.manager.cluster.event.dispatch.event.config; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Alter global rule configuration event. - */ -@RequiredArgsConstructor -@Getter -public final class AlterGlobalRuleConfigurationEvent implements DispatchEvent { - - private final String ruleSimpleName; - - private final String activeVersionKey; - - private final String activeVersion; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterPropertiesEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterPropertiesEvent.java deleted file mode 100644 index e4d6ca9942502..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/config/AlterPropertiesEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.config; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Alter properties event. - */ -@RequiredArgsConstructor -@Getter -public final class AlterPropertiesEvent implements DispatchEvent { - - private final String activeVersionKey; - - private final String activeVersion; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/DatabaseDataAddedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/DatabaseDataAddedEvent.java deleted file mode 100644 index 02ba24a693b87..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/DatabaseDataAddedEvent.java +++ /dev/null @@ -1,32 +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.mode.manager.cluster.event.dispatch.event.metadata.data; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Database data added event. - */ -@RequiredArgsConstructor -@Getter -public final class DatabaseDataAddedEvent implements DispatchEvent { - - private final String databaseName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/DatabaseDataDeletedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/DatabaseDataDeletedEvent.java deleted file mode 100644 index 0c2ad5ebd75f9..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/DatabaseDataDeletedEvent.java +++ /dev/null @@ -1,32 +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.mode.manager.cluster.event.dispatch.event.metadata.data; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Database deleted event. - */ -@RequiredArgsConstructor -@Getter -public final class DatabaseDataDeletedEvent implements DispatchEvent { - - private final String databaseName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/SchemaDataAddedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/SchemaDataAddedEvent.java deleted file mode 100644 index 08ce0de9e127e..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/SchemaDataAddedEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.metadata.data; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Schema added event. - */ -@RequiredArgsConstructor -@Getter -public final class SchemaDataAddedEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/SchemaDataDeletedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/SchemaDataDeletedEvent.java deleted file mode 100644 index f837cd25c3e13..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/SchemaDataDeletedEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.metadata.data; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Schema deleted event. - */ -@RequiredArgsConstructor -@Getter -public final class SchemaDataDeletedEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/ShardingSphereRowDataChangedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/ShardingSphereRowDataChangedEvent.java deleted file mode 100644 index f54dc3aefbc2f..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/ShardingSphereRowDataChangedEvent.java +++ /dev/null @@ -1,39 +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.mode.manager.cluster.event.dispatch.event.metadata.data; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Row data changed event. - */ -@RequiredArgsConstructor -@Getter -public final class ShardingSphereRowDataChangedEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; - - private final String tableName; - - private final YamlShardingSphereRowData yamlRowData; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/ShardingSphereRowDataDeletedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/ShardingSphereRowDataDeletedEvent.java deleted file mode 100644 index a7171206fe788..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/ShardingSphereRowDataDeletedEvent.java +++ /dev/null @@ -1,38 +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.mode.manager.cluster.event.dispatch.event.metadata.data; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Row data deleted event. - */ -@RequiredArgsConstructor -@Getter -public final class ShardingSphereRowDataDeletedEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; - - private final String tableName; - - private final String uniqueKey; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/TableDataChangedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/TableDataChangedEvent.java deleted file mode 100644 index 507a13bef1fa0..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/data/TableDataChangedEvent.java +++ /dev/null @@ -1,38 +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.mode.manager.cluster.event.dispatch.event.metadata.data; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Table data changed event. - */ -@RequiredArgsConstructor -@Getter -public final class TableDataChangedEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; - - private final String addedTable; - - private final String deletedTable; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/cluster/ClusterStateEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/cluster/ClusterStateEvent.java deleted file mode 100644 index 52614460681ea..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/cluster/ClusterStateEvent.java +++ /dev/null @@ -1,33 +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.mode.manager.cluster.event.dispatch.event.state.cluster; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; -import org.apache.shardingsphere.mode.state.ClusterState; - -/** - * Cluster state event. - */ -@RequiredArgsConstructor -@Getter -public final class ClusterStateEvent implements DispatchEvent { - - private final ClusterState clusterState; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ComputeNodeInstanceStateChangedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ComputeNodeInstanceStateChangedEvent.java deleted file mode 100644 index 430a9ccfcaf57..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ComputeNodeInstanceStateChangedEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.state.compute; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Compute node instance state changed event. - */ -@RequiredArgsConstructor -@Getter -public final class ComputeNodeInstanceStateChangedEvent implements DispatchEvent { - - private final String instanceId; - - private final String status; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/KillLocalProcessCompletedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/KillLocalProcessCompletedEvent.java deleted file mode 100644 index 45f6bc4a2489e..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/KillLocalProcessCompletedEvent.java +++ /dev/null @@ -1,32 +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.mode.manager.cluster.event.dispatch.event.state.compute; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Kill local process completed event. - */ -@RequiredArgsConstructor -@Getter -public final class KillLocalProcessCompletedEvent implements DispatchEvent { - - private final String processId; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/KillLocalProcessEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/KillLocalProcessEvent.java deleted file mode 100644 index bfe759c6ce68f..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/KillLocalProcessEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.state.compute; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Kill local process event. - */ -@RequiredArgsConstructor -@Getter -public final class KillLocalProcessEvent implements DispatchEvent { - - private final String instanceId; - - private final String processId; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ReportLocalProcessesCompletedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ReportLocalProcessesCompletedEvent.java deleted file mode 100644 index 64ccd05402fcf..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ReportLocalProcessesCompletedEvent.java +++ /dev/null @@ -1,32 +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.mode.manager.cluster.event.dispatch.event.state.compute; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Report local processes completed event. - */ -@RequiredArgsConstructor -@Getter -public final class ReportLocalProcessesCompletedEvent implements DispatchEvent { - - private final String taskId; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ReportLocalProcessesEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ReportLocalProcessesEvent.java deleted file mode 100644 index 6cd9916b3d888..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/ReportLocalProcessesEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.state.compute; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Report local processes event. - */ -@RequiredArgsConstructor -@Getter -public final class ReportLocalProcessesEvent implements DispatchEvent { - - private final String instanceId; - - private final String taskId; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/WorkerIdEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/WorkerIdEvent.java deleted file mode 100644 index 8bd6d9cd89074..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/WorkerIdEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.state.compute; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Worker id event. - */ -@RequiredArgsConstructor -@Getter -public final class WorkerIdEvent implements DispatchEvent { - - private final String instanceId; - - private final Integer workerId; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/instance/InstanceOfflineEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/instance/InstanceOfflineEvent.java deleted file mode 100644 index bdc19a2a5e35d..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/instance/InstanceOfflineEvent.java +++ /dev/null @@ -1,33 +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.mode.manager.cluster.event.dispatch.event.state.compute.instance; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Compute node instance offline event. - */ -@RequiredArgsConstructor -@Getter -public final class InstanceOfflineEvent implements DispatchEvent { - - private final InstanceMetaData instanceMetaData; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/instance/InstanceOnlineEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/instance/InstanceOnlineEvent.java deleted file mode 100644 index 95942775f0868..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/instance/InstanceOnlineEvent.java +++ /dev/null @@ -1,33 +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.mode.manager.cluster.event.dispatch.event.state.compute.instance; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Compute node instance online event. - */ -@RequiredArgsConstructor -@Getter -public final class InstanceOnlineEvent implements DispatchEvent { - - private final InstanceMetaData instanceMetaData; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/storage/QualifiedDataSourceStateEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/storage/QualifiedDataSourceStateEvent.java deleted file mode 100644 index 6a71ef77a286a..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/storage/QualifiedDataSourceStateEvent.java +++ /dev/null @@ -1,36 +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.mode.manager.cluster.event.dispatch.event.state.storage; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; -import org.apache.shardingsphere.infra.state.datasource.qualified.QualifiedDataSourceState; - -/** - * Storage node changed event. - */ -@RequiredArgsConstructor -@Getter -public final class QualifiedDataSourceStateEvent implements DispatchEvent { - - private final QualifiedDataSource qualifiedDataSource; - - private final QualifiedDataSourceState status; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/LabelsEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/DataChangedEventHandler.java similarity index 52% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/LabelsEvent.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/DataChangedEventHandler.java index 636d645beb11b..38ff0eae5b9f6 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/state/compute/LabelsEvent.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/DataChangedEventHandler.java @@ -15,22 +15,40 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; +import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; +import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; +import org.apache.shardingsphere.mode.manager.ContextManager; import java.util.Collection; /** - * Labels changed event. + * Data changed event handler. */ -@RequiredArgsConstructor -@Getter -public final class LabelsEvent implements DispatchEvent { +@SingletonSPI +public interface DataChangedEventHandler { - private final String instanceId; + /** + * Get subscribed key. + * + * @return subscribed key + */ + String getSubscribedKey(); - private final Collection labels; + /** + * Get subscribed types. + * + * @return subscribed types + */ + Collection getSubscribedTypes(); + + /** + * Handle data changed event. + * + * @param contextManager context manager + * @param event data changed event + */ + void handle(ContextManager contextManager, DataChangedEvent event); } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ClusterStateDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandler.java similarity index 73% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ClusterStateDispatchEventBuilder.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandler.java index fdc7db6c7a369..5117361c659c7 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ClusterStateDispatchEventBuilder.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandler.java @@ -15,24 +15,22 @@ * 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 org.apache.shardingsphere.mode.state.ClusterState; import org.apache.shardingsphere.metadata.persist.node.StatesNode; 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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.cluster.ClusterStateEvent; -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.state.ClusterState; import java.util.Arrays; import java.util.Collection; -import java.util.Optional; /** - * Cluster state dispatch event builder. + * Cluster state changed handler. */ -public final class ClusterStateDispatchEventBuilder implements DispatchEventBuilder { +public final class ClusterStateChangedHandler implements DataChangedEventHandler { @Override public String getSubscribedKey() { @@ -45,8 +43,8 @@ public Collection getSubscribedTypes() { } @Override - public Optional build(final DataChangedEvent event) { - return event.getKey().equals(StatesNode.getClusterStateNodePath()) ? Optional.of(new ClusterStateEvent(getClusterState(event))) : Optional.empty(); + public void handle(final ContextManager contextManager, final DataChangedEvent event) { + contextManager.getStateContext().switchState(getClusterState(event)); } private ClusterState getClusterState(final DataChangedEvent event) { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeOnlineDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandler.java similarity index 73% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeOnlineDispatchEventBuilder.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandler.java index ad5ffdc9b4bf6..e60cc13c2959a 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeOnlineDispatchEventBuilder.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandler.java @@ -15,9 +15,10 @@ * 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 org.apache.shardingsphere.infra.instance.ComputeNodeData; +import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaDataFactory; import org.apache.shardingsphere.infra.instance.metadata.InstanceType; @@ -27,21 +28,18 @@ import org.apache.shardingsphere.metadata.persist.node.ComputeNode; 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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.instance.InstanceOfflineEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.instance.InstanceOnlineEvent; -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 java.util.Arrays; import java.util.Collection; -import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * Compute node online dispatch event builder. + * Compute node online handler. */ -public final class ComputeNodeOnlineDispatchEventBuilder implements DispatchEventBuilder { +public final class ComputeNodeOnlineHandler implements DataChangedEventHandler { @Override public String getSubscribedKey() { @@ -54,25 +52,20 @@ public Collection getSubscribedTypes() { } @Override - public Optional build(final DataChangedEvent event) { - return createInstanceEvent(event); - } - - private Optional createInstanceEvent(final DataChangedEvent event) { + public void handle(final ContextManager contextManager, final DataChangedEvent event) { Matcher matcher = getInstanceOnlinePathMatcher(event.getKey()); if (!matcher.find()) { - return Optional.empty(); + return; } ComputeNodeData computeNodeData = new YamlComputeNodeDataSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlComputeNodeData.class)); InstanceMetaData instanceMetaData = InstanceMetaDataFactory.create( matcher.group(2), InstanceType.valueOf(matcher.group(1).toUpperCase()), computeNodeData.getAttribute(), computeNodeData.getVersion(), computeNodeData.getDatabaseName()); if (Type.ADDED == event.getType()) { - return Optional.of(new InstanceOnlineEvent(instanceMetaData)); - } - if (Type.DELETED == event.getType()) { - return Optional.of(new InstanceOfflineEvent(instanceMetaData)); + contextManager.getComputeNodeInstanceContext().getClusterInstanceRegistry() + .add(contextManager.getPersistServiceFacade().getComputeNodePersistService().loadComputeNodeInstance(instanceMetaData)); + } else if (Type.DELETED == event.getType()) { + contextManager.getComputeNodeInstanceContext().getClusterInstanceRegistry().delete(new ComputeNodeInstance(instanceMetaData)); } - return Optional.empty(); } private Matcher getInstanceOnlinePathMatcher(final String onlineInstancePath) { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeStateDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandler.java similarity index 50% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeStateDispatchEventBuilder.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandler.java index 27dc92033138b..7df07f90e1c0e 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeStateDispatchEventBuilder.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandler.java @@ -15,28 +15,25 @@ * 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.Strings; +import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.metadata.persist.node.ComputeNode; 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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ComputeNodeInstanceStateChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.LabelsEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.WorkerIdEvent; -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 java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Optional; /** - * Compute node state dispatch event builder. + * Compute node state changed handler. */ -public final class ComputeNodeStateDispatchEventBuilder implements DispatchEventBuilder { +public final class ComputeNodeStateChangedHandler implements DataChangedEventHandler { @Override public String getSubscribedKey() { @@ -48,29 +45,21 @@ public Collection getSubscribedTypes() { return Arrays.asList(Type.ADDED, Type.UPDATED, Type.DELETED); } + @SuppressWarnings("unchecked") @Override - public Optional build(final DataChangedEvent event) { + public void handle(final ContextManager contextManager, final DataChangedEvent event) { String instanceId = ComputeNode.getInstanceIdByComputeNode(event.getKey()); - if (!Strings.isNullOrEmpty(instanceId)) { - Optional result = createInstanceDispatchEvent(event, instanceId); - if (result.isPresent()) { - return result; - } + if (Strings.isNullOrEmpty(instanceId)) { + return; } - return Optional.empty(); - } - - @SuppressWarnings("unchecked") - private Optional createInstanceDispatchEvent(final DataChangedEvent event, final String instanceId) { + ComputeNodeInstanceContext computeNodeInstanceContext = contextManager.getComputeNodeInstanceContext(); if (event.getKey().equals(ComputeNode.getComputeNodeStateNodePath(instanceId)) && Type.DELETED != event.getType()) { - return Optional.of(new ComputeNodeInstanceStateChangedEvent(instanceId, event.getValue())); - } - if (event.getKey().equals(ComputeNode.getInstanceLabelsNodePath(instanceId)) && Type.DELETED != event.getType()) { - return Optional.of(new LabelsEvent(instanceId, Strings.isNullOrEmpty(event.getValue()) ? new ArrayList<>() : YamlEngine.unmarshal(event.getValue(), Collection.class))); - } - if (event.getKey().equals(ComputeNode.getInstanceWorkerIdNodePath(instanceId))) { - return Optional.of(new WorkerIdEvent(instanceId, Strings.isNullOrEmpty(event.getValue()) ? null : Integer.valueOf(event.getValue()))); + computeNodeInstanceContext.updateStatus(instanceId, event.getValue()); + } else if (event.getKey().equals(ComputeNode.getInstanceLabelsNodePath(instanceId)) && Type.DELETED != event.getType()) { + // TODO labels may be empty + computeNodeInstanceContext.updateLabels(instanceId, Strings.isNullOrEmpty(event.getValue()) ? new ArrayList<>() : YamlEngine.unmarshal(event.getValue(), Collection.class)); + } else if (event.getKey().equals(ComputeNode.getInstanceWorkerIdNodePath(instanceId))) { + computeNodeInstanceContext.updateWorkerId(instanceId, Strings.isNullOrEmpty(event.getValue()) ? null : Integer.valueOf(event.getValue())); } - return Optional.empty(); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/GlobalRuleConfigurationEventSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandler.java similarity index 60% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/GlobalRuleConfigurationEventSubscriber.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandler.java index d5cab08d6443b..f950991266d24 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/GlobalRuleConfigurationEventSubscriber.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandler.java @@ -15,41 +15,53 @@ * 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 com.google.common.base.Preconditions; -import com.google.common.eventbus.Subscribe; -import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.config.AlterGlobalRuleConfigurationEvent; +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.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; +import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler; +import org.apache.shardingsphere.mode.path.GlobalNodePath; import org.apache.shardingsphere.mode.spi.RuleConfigurationPersistDecorator; +import java.util.Arrays; +import java.util.Collection; import java.util.Optional; /** - * Global rule configuration event subscriber. + * Global rule changed handler. */ -@RequiredArgsConstructor -public final class GlobalRuleConfigurationEventSubscriber implements DispatchEventSubscriber { +public final class GlobalRuleChangedHandler implements DataChangedEventHandler { - private final ContextManager contextManager; + @Override + public String getSubscribedKey() { + return GlobalNode.getGlobalRuleRootNode(); + } + + @Override + public Collection getSubscribedTypes() { + return Arrays.asList(Type.ADDED, Type.UPDATED); + } - /** - * Renew for global rule configuration. - * - * @param event global rule alter event - */ @SuppressWarnings("unchecked") - @Subscribe - public synchronized void renew(final AlterGlobalRuleConfigurationEvent event) { - Preconditions.checkArgument(event.getActiveVersion().equals( - contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())), - "Invalid active version: %s of key: %s", event.getActiveVersion(), event.getActiveVersionKey()); - Optional ruleConfig = contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load(event.getRuleSimpleName()); - Preconditions.checkArgument(ruleConfig.isPresent(), "Can not find rule configuration with name: %s", event.getRuleSimpleName()); + @Override + public void handle(final ContextManager contextManager, final DataChangedEvent event) { + if (!GlobalNodePath.isRuleActiveVersionPath(event.getKey())) { + return; + } + Optional ruleName = GlobalNodePath.getRuleName(event.getKey()); + if (!ruleName.isPresent()) { + return; + } + Preconditions.checkArgument(event.getValue().equals( + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getKey())), + "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + 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( TypedSPILoader.findService(RuleConfigurationPersistDecorator.class, ruleConfig.getClass()).map(optional -> optional.restore(ruleConfig.get())).orElse(ruleConfig.get())); } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/KillProcessDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandler.java similarity index 59% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/KillProcessDispatchEventBuilder.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandler.java index dd6718dc9a83e..08e1ecb0ecd91 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/KillProcessDispatchEventBuilder.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandler.java @@ -15,26 +15,27 @@ * 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 org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException; +import org.apache.shardingsphere.infra.executor.sql.process.ProcessRegistry; +import org.apache.shardingsphere.infra.executor.sql.process.lock.ProcessOperationLockRegistry; import org.apache.shardingsphere.metadata.persist.node.ComputeNode; 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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.KillLocalProcessCompletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.KillLocalProcessEvent; -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 java.sql.SQLException; import java.util.Arrays; import java.util.Collection; -import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * Kill process dispatch event builder. + * Kill process handler. */ -public final class KillProcessDispatchEventBuilder implements DispatchEventBuilder { +public final class KillProcessHandler implements DataChangedEventHandler { @Override public String getSubscribedKey() { @@ -47,22 +48,26 @@ public Collection getSubscribedTypes() { } @Override - public Optional build(final DataChangedEvent event) { - return createKillLocalProcessEvent(event); - } - - private Optional createKillLocalProcessEvent(final DataChangedEvent event) { + public void handle(final ContextManager contextManager, final DataChangedEvent event) { Matcher matcher = getKillProcessTriggerMatcher(event); if (!matcher.find()) { - return Optional.empty(); + return; } + String instanceId = matcher.group(1); + String processId = matcher.group(2); if (Type.ADDED == event.getType()) { - return Optional.of(new KillLocalProcessEvent(matcher.group(1), matcher.group(2))); - } - if (Type.DELETED == event.getType()) { - return Optional.of(new KillLocalProcessCompletedEvent(matcher.group(2))); + if (!instanceId.equals(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getId())) { + return; + } + try { + ProcessRegistry.getInstance().kill(processId); + } catch (final SQLException ex) { + throw new SQLWrapperException(ex); + } + contextManager.getPersistCoordinatorFacade().getProcessPersistCoordinator().cleanProcess(instanceId, processId); + } else if (Type.DELETED == event.getType()) { + ProcessOperationLockRegistry.getInstance().notify(processId); } - return Optional.empty(); } private Matcher getKillProcessTriggerMatcher(final DataChangedEvent event) { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandler.java new file mode 100644 index 0000000000000..19fdf3f7f9aee --- /dev/null +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandler.java @@ -0,0 +1,69 @@ +/* + * 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.dispatch.handler.type; + +import org.apache.shardingsphere.infra.instance.metadata.InstanceType; +import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; +import org.apache.shardingsphere.metadata.persist.node.StatesNode; +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.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.DatabaseMetaDataChangedListener; +import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; +import org.apache.shardingsphere.mode.persist.service.unified.ListenerAssistedType; +import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; + +import java.util.Arrays; +import java.util.Collection; + +/** + * Listener assisted changed handler. + */ +public final class ListenerAssistedChangedHandler implements DataChangedEventHandler { + + @Override + public String getSubscribedKey() { + return StatesNode.getListenerAssistedNodePath(); + } + + @Override + public Collection getSubscribedTypes() { + return Arrays.asList(Type.ADDED, Type.UPDATED); + } + + @Override + public void handle(final ContextManager contextManager, final DataChangedEvent event) { + StatesNode.getDatabaseNameByListenerAssistedNodePath(event.getKey()).ifPresent(optional -> handle(contextManager, optional, ListenerAssistedType.valueOf(event.getValue()))); + } + + private static void handle(final ContextManager contextManager, final String databaseName, final ListenerAssistedType listenerAssistedType) { + ClusterPersistRepository repository = (ClusterPersistRepository) contextManager.getPersistServiceFacade().getRepository(); + if (ListenerAssistedType.CREATE_DATABASE == listenerAssistedType) { + repository.watch(DatabaseMetaDataNode.getDatabaseNamePath(databaseName), new DatabaseMetaDataChangedListener(contextManager.getComputeNodeInstanceContext().getEventBusContext())); + contextManager.getMetaDataContextManager().getSchemaMetaDataManager().addDatabase(databaseName); + } else if (ListenerAssistedType.DROP_DATABASE == listenerAssistedType) { + repository.removeDataListener(DatabaseMetaDataNode.getDatabaseNamePath(databaseName)); + contextManager.getMetaDataContextManager().getSchemaMetaDataManager().dropDatabase(databaseName); + } + contextManager.getPersistServiceFacade().getListenerAssistedPersistService().deleteDatabaseNameListenerAssisted(databaseName); + if (InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) { + new ShardingSphereStatisticsRefreshEngine(contextManager).asyncRefresh(); + } + } +} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandler.java similarity index 60% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilder.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandler.java index 1f10eae527e25..bc147b22e327e 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilder.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandler.java @@ -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 { +public final class PropertiesChangedHandler implements DataChangedEventHandler { @Override public String getSubscribedKey() { @@ -44,7 +44,13 @@ public Collection getSubscribedTypes() { } @Override - public Optional 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()); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/QualifiedDataSourceDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandler.java similarity index 61% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/QualifiedDataSourceDispatchEventBuilder.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandler.java index 4196556bf172e..30b7a1af79868 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/QualifiedDataSourceDispatchEventBuilder.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandler.java @@ -15,10 +15,13 @@ * 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 com.google.common.base.Strings; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource; +import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute; import org.apache.shardingsphere.infra.state.datasource.qualified.QualifiedDataSourceState; import org.apache.shardingsphere.infra.state.datasource.qualified.yaml.YamlQualifiedDataSourceState; import org.apache.shardingsphere.infra.state.datasource.qualified.yaml.YamlQualifiedDataSourceStateSwapper; @@ -26,18 +29,16 @@ import org.apache.shardingsphere.metadata.persist.node.QualifiedDataSourceNode; 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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.storage.QualifiedDataSourceStateEvent; -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 java.util.Arrays; import java.util.Collection; -import java.util.Optional; /** - * Qualified data source dispatch event builder. + * Qualified data source changed handler. */ -public final class QualifiedDataSourceDispatchEventBuilder implements DispatchEventBuilder { +public final class QualifiedDataSourceChangedHandler implements DataChangedEventHandler { @Override public String getSubscribedKey() { @@ -50,15 +51,16 @@ public Collection getSubscribedTypes() { } @Override - public Optional build(final DataChangedEvent event) { - if (Strings.isNullOrEmpty(event.getValue())) { - return Optional.empty(); - } - Optional qualifiedDataSource = QualifiedDataSourceNode.extractQualifiedDataSource(event.getKey()); - if (qualifiedDataSource.isPresent()) { + public void handle(final ContextManager contextManager, final DataChangedEvent event) { + if (!Strings.isNullOrEmpty(event.getValue())) { QualifiedDataSourceState state = new YamlQualifiedDataSourceStateSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlQualifiedDataSourceState.class)); - return Optional.of(new QualifiedDataSourceStateEvent(qualifiedDataSource.get(), state)); + QualifiedDataSourceNode.extractQualifiedDataSource(event.getKey()).ifPresent(optional -> handle(contextManager.getMetaDataContexts().getMetaData(), optional, state)); } - return Optional.empty(); + } + + private void handle(final ShardingSphereMetaData metaData, final QualifiedDataSource qualifiedDataSource, final QualifiedDataSourceState state) { + Preconditions.checkState(metaData.containsDatabase(qualifiedDataSource.getDatabaseName()), "No database '%s' exists.", qualifiedDataSource.getDatabaseName()); + metaData.getDatabase(qualifiedDataSource.getDatabaseName()).getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class) + .forEach(each -> each.updateStatus(qualifiedDataSource, state.getState())); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandler.java new file mode 100644 index 0000000000000..a6f42a53f37cc --- /dev/null +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandler.java @@ -0,0 +1,132 @@ +/* + * 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.dispatch.handler.type; + +import com.google.common.base.Strings; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; +import org.apache.shardingsphere.metadata.persist.node.ShardingSphereDataNode; +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.apache.shardingsphere.mode.metadata.manager.ShardingSphereDatabaseDataManager; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Optional; + +/** + * ShardingSphere data changed handler. + */ +public final class ShardingSphereDataChangedHandler implements DataChangedEventHandler { + + @Override + public String getSubscribedKey() { + return ShardingSphereDataNode.getShardingSphereDataNodePath(); + } + + @Override + public Collection getSubscribedTypes() { + return Arrays.asList(Type.ADDED, Type.UPDATED, Type.DELETED); + } + + @Override + public void handle(final ContextManager contextManager, final DataChangedEvent event) { + ShardingSphereDatabaseDataManager databaseManager = contextManager.getMetaDataContextManager().getDatabaseManager(); + Optional databaseName = ShardingSphereDataNode.getDatabaseName(event.getKey()); + if (databaseName.isPresent()) { + handleDatabaseChanged(databaseManager, event.getType(), databaseName.get()); + return; + } + databaseName = ShardingSphereDataNode.getDatabaseNameByDatabasePath(event.getKey()); + if (!databaseName.isPresent()) { + return; + } + Optional schemaName = ShardingSphereDataNode.getSchemaName(event.getKey()); + if (schemaName.isPresent()) { + handleSchemaChanged(databaseManager, event.getType(), databaseName.get(), schemaName.get()); + return; + } + schemaName = ShardingSphereDataNode.getSchemaNameBySchemaPath(event.getKey()); + if (!schemaName.isPresent()) { + return; + } + Optional tableName = ShardingSphereDataNode.getTableName(event.getKey()); + if (tableName.isPresent()) { + handleTableChanged(databaseManager, event.getType(), databaseName.get(), schemaName.get(), tableName.get()); + return; + } + tableName = ShardingSphereDataNode.getTableNameByRowPath(event.getKey()); + if (!tableName.isPresent()) { + return; + } + Optional rowPath = ShardingSphereDataNode.getRowUniqueKey(event.getKey()); + if (rowPath.isPresent()) { + handleRowDataChanged(databaseManager, event.getType(), event.getValue(), databaseName.get(), schemaName.get(), tableName.get(), rowPath.get()); + } + } + + private void handleDatabaseChanged(final ShardingSphereDatabaseDataManager databaseManager, final Type type, final String databaseName) { + switch (type) { + case ADDED: + case UPDATED: + databaseManager.addShardingSphereDatabaseData(databaseName); + return; + case DELETED: + databaseManager.dropShardingSphereDatabaseData(databaseName); + return; + default: + } + } + + private void handleSchemaChanged(final ShardingSphereDatabaseDataManager databaseManager, final Type type, final String databaseName, final String schemaName) { + switch (type) { + case ADDED: + case UPDATED: + databaseManager.addShardingSphereSchemaData(databaseName, schemaName); + return; + case DELETED: + databaseManager.dropShardingSphereSchemaData(databaseName, schemaName); + return; + default: + } + } + + private void handleTableChanged(final ShardingSphereDatabaseDataManager databaseManager, final Type type, final String databaseName, final String schemaName, final String tableName) { + switch (type) { + case ADDED: + case UPDATED: + databaseManager.addShardingSphereTableData(databaseName, schemaName, tableName); + return; + case DELETED: + databaseManager.dropShardingSphereTableData(databaseName, schemaName, tableName); + return; + default: + } + } + + private void handleRowDataChanged(final ShardingSphereDatabaseDataManager databaseManager, final Type type, final String eventValue, + final String databaseName, final String schemaName, final String tableName, final String rowPath) { + if ((Type.ADDED == type || Type.UPDATED == type) && !Strings.isNullOrEmpty(eventValue)) { + databaseManager.alterShardingSphereRowData(databaseName, schemaName, tableName, YamlEngine.unmarshal(eventValue, YamlShardingSphereRowData.class)); + } else if (Type.DELETED == type) { + databaseManager.deleteShardingSphereRowData(databaseName, schemaName, tableName, rowPath); + } + } +} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShowProcessListDispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandler.java similarity index 63% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShowProcessListDispatchEventBuilder.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandler.java index f7a8a40e74e7e..f9d98414be2e2 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShowProcessListDispatchEventBuilder.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandler.java @@ -15,26 +15,24 @@ * 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 org.apache.shardingsphere.infra.executor.sql.process.lock.ProcessOperationLockRegistry; import org.apache.shardingsphere.metadata.persist.node.ComputeNode; 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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ReportLocalProcessesCompletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ReportLocalProcessesEvent; -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 java.util.Arrays; import java.util.Collection; -import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * Show process list dispatch event builder. + * Show process list handler. */ -public final class ShowProcessListDispatchEventBuilder implements DispatchEventBuilder { +public final class ShowProcessListHandler implements DataChangedEventHandler { @Override public String getSubscribedKey() { @@ -47,22 +45,20 @@ public Collection getSubscribedTypes() { } @Override - public Optional build(final DataChangedEvent event) { - return createReportLocalProcessesEvent(event); - } - - private Optional createReportLocalProcessesEvent(final DataChangedEvent event) { + public void handle(final ContextManager contextManager, final DataChangedEvent event) { Matcher matcher = getShowProcessListTriggerMatcher(event); if (!matcher.find()) { - return Optional.empty(); + return; } + String instanceId = matcher.group(1); + String taskId = matcher.group(2); if (Type.ADDED == event.getType()) { - return Optional.of(new ReportLocalProcessesEvent(matcher.group(1), matcher.group(2))); - } - if (Type.DELETED == event.getType()) { - return Optional.of(new ReportLocalProcessesCompletedEvent(matcher.group(2))); + if (instanceId.equals(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getId())) { + contextManager.getPersistCoordinatorFacade().getProcessPersistCoordinator().reportLocalProcesses(instanceId, taskId); + } + } else if (Type.DELETED == event.getType()) { + ProcessOperationLockRegistry.getInstance().notify(taskId); } - return Optional.empty(); } private Matcher getShowProcessListTriggerMatcher(final DataChangedEvent event) { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java index 7daeb327d3471..9122a5006c93f 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java @@ -22,7 +22,9 @@ import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; +import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.DatabaseMetaDataChangedListener; +import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.GlobalMetaDataChangedHandler; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.GlobalMetaDataChangedListener; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; @@ -33,6 +35,8 @@ */ public final class DataChangedEventListenerRegistry { + private final ContextManager contextManager; + private final ClusterPersistRepository repository; private final EventBusContext eventBusContext; @@ -40,6 +44,7 @@ public final class DataChangedEventListenerRegistry { private final Collection databaseNames; public DataChangedEventListenerRegistry(final ContextManager contextManager, final Collection databaseNames) { + this.contextManager = contextManager; repository = (ClusterPersistRepository) contextManager.getPersistServiceFacade().getRepository(); eventBusContext = contextManager.getComputeNodeInstanceContext().getEventBusContext(); this.databaseNames = databaseNames; @@ -51,6 +56,7 @@ public DataChangedEventListenerRegistry(final ContextManager contextManager, fin public void register() { databaseNames.forEach(this::registerDatabaseListeners); ShardingSphereServiceLoader.getServiceInstances(DispatchEventBuilder.class).forEach(this::registerGlobalListeners); + ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).forEach(this::registerGlobalHandlers); } private void registerDatabaseListeners(final String databaseName) { @@ -60,4 +66,8 @@ private void registerDatabaseListeners(final String databaseName) { private void registerGlobalListeners(final DispatchEventBuilder builder) { repository.watch(builder.getSubscribedKey(), new GlobalMetaDataChangedListener(eventBusContext, builder)); } + + private void registerGlobalHandlers(final DataChangedEventHandler handler) { + repository.watch(handler.getSubscribedKey(), new GlobalMetaDataChangedHandler(contextManager, handler)); + } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StateChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedHandler.java similarity index 65% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StateChangedSubscriber.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedHandler.java index 40728cb3315c2..d68a42ce5e6cd 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StateChangedSubscriber.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedHandler.java @@ -15,29 +15,28 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type; -import com.google.common.eventbus.Subscribe; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.cluster.ClusterStateEvent; +import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; +import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler; +import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEventListener; /** - * State changed subscriber. + * Global meta data changed handler. */ @RequiredArgsConstructor -public final class StateChangedSubscriber implements DispatchEventSubscriber { +public final class GlobalMetaDataChangedHandler implements DataChangedEventListener { private final ContextManager contextManager; - /** - * Renew cluster state. - * - * @param event cluster state event - */ - @Subscribe - public synchronized void renew(final ClusterStateEvent event) { - contextManager.getStateContext().switchState(event.getClusterState()); + private final DataChangedEventHandler handler; + + @Override + public void onChange(final DataChangedEvent event) { + if (handler.getSubscribedTypes().contains(event.getType())) { + handler.handle(contextManager, event); + } } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java index efe9292425a27..969b6117ebb49 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java @@ -21,16 +21,8 @@ import org.apache.shardingsphere.infra.util.eventbus.EventSubscriber; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.CacheEvictedSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.ComputeNodeStateSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.DatabaseDataChangedSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.GlobalRuleConfigurationEventSubscriber; -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.StateChangedSubscriber; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.StorageUnitEventSubscriber; import java.util.Arrays; @@ -47,15 +39,7 @@ public final class ClusterDispatchEventSubscriberRegistry { public ClusterDispatchEventSubscriberRegistry(final ContextManager contextManager) { subscribers = Arrays.asList(new RuleItemChangedSubscriber(contextManager), new MetaDataChangedSubscriber(contextManager), - new ListenerAssistedSubscriber(contextManager), - new StateChangedSubscriber(contextManager), - new DatabaseDataChangedSubscriber(contextManager), - new ProcessListChangedSubscriber(contextManager), new CacheEvictedSubscriber(), - new ComputeNodeStateSubscriber(contextManager), - new QualifiedDataSourceSubscriber(contextManager), - new StorageUnitEventSubscriber(contextManager), - new PropertiesEventSubscriber(contextManager), - new GlobalRuleConfigurationEventSubscriber(contextManager)); + new StorageUnitEventSubscriber(contextManager)); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ComputeNodeStateSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ComputeNodeStateSubscriber.java deleted file mode 100644 index 069734a88f408..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ComputeNodeStateSubscriber.java +++ /dev/null @@ -1,96 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import com.google.common.eventbus.Subscribe; -import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; -import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ComputeNodeInstanceStateChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.LabelsEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.WorkerIdEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.instance.InstanceOfflineEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.instance.InstanceOnlineEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; - -/** - * Compute node state subscriber. - */ -public final class ComputeNodeStateSubscriber implements DispatchEventSubscriber { - - private final ContextManager contextManager; - - private final ComputeNodeInstanceContext computeNodeInstanceContext; - - public ComputeNodeStateSubscriber(final ContextManager contextManager) { - this.contextManager = contextManager; - computeNodeInstanceContext = contextManager.getComputeNodeInstanceContext(); - } - - /** - * Renew instance list. - * - * @param event compute node online event - */ - @Subscribe - public synchronized void renew(final InstanceOnlineEvent event) { - computeNodeInstanceContext.getClusterInstanceRegistry() - .add(contextManager.getPersistServiceFacade().getComputeNodePersistService().loadComputeNodeInstance(event.getInstanceMetaData())); - } - - /** - * Renew instance list. - * - * @param event compute node offline event - */ - @Subscribe - public synchronized void renew(final InstanceOfflineEvent event) { - computeNodeInstanceContext.getClusterInstanceRegistry().delete(new ComputeNodeInstance(event.getInstanceMetaData())); - } - - /** - * Renew compute node instance state. - * - * @param event compute node instance state changed event - */ - @Subscribe - public synchronized void renew(final ComputeNodeInstanceStateChangedEvent event) { - computeNodeInstanceContext.updateStatus(event.getInstanceId(), event.getStatus()); - } - - /** - * Renew instance worker id. - * - * @param event worker id event - */ - @Subscribe - public synchronized void renew(final WorkerIdEvent event) { - computeNodeInstanceContext.updateWorkerId(event.getInstanceId(), event.getWorkerId()); - } - - /** - * Renew instance labels. - * - * @param event label event - */ - @Subscribe - public synchronized void renew(final LabelsEvent event) { - // TODO labels may be empty - computeNodeInstanceContext.updateLabels(event.getInstanceId(), event.getLabels()); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/DatabaseDataChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/DatabaseDataChangedSubscriber.java deleted file mode 100644 index 9a1cc2aa6f7fb..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/DatabaseDataChangedSubscriber.java +++ /dev/null @@ -1,117 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import com.google.common.eventbus.Subscribe; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.DatabaseDataAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.DatabaseDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.SchemaDataAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.SchemaDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.ShardingSphereRowDataChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.ShardingSphereRowDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.TableDataChangedEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; -import org.apache.shardingsphere.mode.metadata.manager.ShardingSphereDatabaseDataManager; - -/** - * Database data changed subscriber. - */ -public final class DatabaseDataChangedSubscriber implements DispatchEventSubscriber { - - private final ShardingSphereDatabaseDataManager databaseManager; - - public DatabaseDataChangedSubscriber(final ContextManager contextManager) { - databaseManager = contextManager.getMetaDataContextManager().getDatabaseManager(); - } - - /** - * Renew to persist ShardingSphere database data. - * - * @param event database data added event - */ - @Subscribe - public synchronized void renew(final DatabaseDataAddedEvent event) { - databaseManager.addShardingSphereDatabaseData(event.getDatabaseName()); - } - - /** - * Renew to delete ShardingSphere data database. - * - * @param event database delete event - */ - @Subscribe - public synchronized void renew(final DatabaseDataDeletedEvent event) { - databaseManager.dropShardingSphereDatabaseData(event.getDatabaseName()); - } - - /** - * Renew to added ShardingSphere data schema. - * - * @param event schema added event - */ - @Subscribe - public synchronized void renew(final SchemaDataAddedEvent event) { - databaseManager.addShardingSphereSchemaData(event.getDatabaseName(), event.getSchemaName()); - } - - /** - * Renew to delete ShardingSphere data schema. - * - * @param event schema delete event - */ - @Subscribe - public synchronized void renew(final SchemaDataDeletedEvent event) { - databaseManager.dropShardingSphereSchemaData(event.getDatabaseName(), event.getSchemaName()); - } - - /** - * Renew ShardingSphere data of the table. - * - * @param event table data changed event - */ - @Subscribe - public synchronized void renew(final TableDataChangedEvent event) { - if (null != event.getAddedTable()) { - databaseManager.addShardingSphereTableData(event.getDatabaseName(), event.getSchemaName(), event.getAddedTable()); - } - if (null != event.getDeletedTable()) { - databaseManager.dropShardingSphereTableData(event.getDatabaseName(), event.getSchemaName(), event.getDeletedTable()); - } - } - - /** - * Renew ShardingSphere data of row. - * - * @param event ShardingSphere row data added event - */ - @Subscribe - public synchronized void renew(final ShardingSphereRowDataChangedEvent event) { - databaseManager.alterShardingSphereRowData(event.getDatabaseName(), event.getSchemaName(), event.getTableName(), event.getYamlRowData()); - } - - /** - * Renew ShardingSphere data of row. - * - * @param event ShardingSphere row data deleted event - */ - @Subscribe - public synchronized void renew(final ShardingSphereRowDataDeletedEvent event) { - databaseManager.deleteShardingSphereRowData(event.getDatabaseName(), event.getSchemaName(), event.getTableName(), event.getUniqueKey()); - } -} 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 deleted file mode 100644 index c8b3c2cda351b..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ListenerAssistedSubscriber.java +++ /dev/null @@ -1,79 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import com.google.common.eventbus.Subscribe; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.instance.metadata.InstanceType; -import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.CreateDatabaseListenerAssistedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.DropDatabaseListenerAssistedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.DatabaseMetaDataChangedListener; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; -import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; - -/** - * Listener assisted subscriber. - */ -@RequiredArgsConstructor -public final class ListenerAssistedSubscriber implements DispatchEventSubscriber { - - private final ContextManager contextManager; - - private final ClusterPersistRepository repository; - - public ListenerAssistedSubscriber(final ContextManager contextManager) { - this.contextManager = contextManager; - repository = (ClusterPersistRepository) contextManager.getPersistServiceFacade().getRepository(); - } - - /** - * Renew to persist meta data. - * - * @param event database added event - */ - @Subscribe - 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()); - refreshStatisticsData(); - } - - /** - * Renew to delete database. - * - * @param event database delete event - */ - @Subscribe - 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()); - refreshStatisticsData(); - } - - private void refreshStatisticsData() { - if (contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster() - && InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) { - new ShardingSphereStatisticsRefreshEngine(contextManager).asyncRefresh(); - } - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ProcessListChangedSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ProcessListChangedSubscriber.java deleted file mode 100644 index 4216bc6a19a1c..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ProcessListChangedSubscriber.java +++ /dev/null @@ -1,93 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import com.google.common.eventbus.Subscribe; -import org.apache.shardingsphere.infra.executor.sql.process.ProcessRegistry; -import org.apache.shardingsphere.infra.executor.sql.process.lock.ProcessOperationLockRegistry; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.KillLocalProcessCompletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.KillLocalProcessEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ReportLocalProcessesCompletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ReportLocalProcessesEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; -import org.apache.shardingsphere.mode.persist.coordinator.ProcessPersistCoordinator; - -import java.sql.SQLException; - -/** - * Process list changed subscriber. - */ -public final class ProcessListChangedSubscriber implements DispatchEventSubscriber { - - private final String instanceId; - - private final ProcessPersistCoordinator processPersistService; - - public ProcessListChangedSubscriber(final ContextManager contextManager) { - instanceId = contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getId(); - processPersistService = contextManager.getPersistCoordinatorFacade().getProcessPersistCoordinator(); - } - - /** - * Report local processes. - * - * @param event report local processes event - */ - @Subscribe - public void reportLocalProcesses(final ReportLocalProcessesEvent event) { - if (event.getInstanceId().equals(instanceId)) { - processPersistService.reportLocalProcesses(instanceId, event.getTaskId()); - } - } - - /** - * Complete to report local processes. - * - * @param event report local processes completed event - */ - @Subscribe - public synchronized void completeToReportLocalProcesses(final ReportLocalProcessesCompletedEvent event) { - ProcessOperationLockRegistry.getInstance().notify(event.getTaskId()); - } - - /** - * Kill local process. - * - * @param event kill local process event - * @throws SQLException SQL exception - */ - @Subscribe - public synchronized void killLocalProcess(final KillLocalProcessEvent event) throws SQLException { - if (!event.getInstanceId().equals(instanceId)) { - return; - } - ProcessRegistry.getInstance().kill(event.getProcessId()); - processPersistService.cleanProcess(instanceId, event.getProcessId()); - } - - /** - * Complete to kill local process. - * - * @param event kill local process completed event - */ - @Subscribe - public synchronized void completeToKillLocalProcess(final KillLocalProcessCompletedEvent event) { - ProcessOperationLockRegistry.getInstance().notify(event.getProcessId()); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriber.java deleted file mode 100644 index ba3d0df017f42..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriber.java +++ /dev/null @@ -1,47 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import com.google.common.base.Preconditions; -import com.google.common.eventbus.Subscribe; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.config.AlterPropertiesEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; - -/** - * Properties event subscriber. - */ -@RequiredArgsConstructor -public final class PropertiesEventSubscriber implements DispatchEventSubscriber { - - private final ContextManager contextManager; - - /** - * Renew for global properties. - * - * @param event global properties alter event - */ - @Subscribe - public synchronized void renew(final AlterPropertiesEvent event) { - Preconditions.checkArgument(event.getActiveVersion().equals( - contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())), - "Invalid active version: %s of key: %s", event.getActiveVersion(), event.getActiveVersionKey()); - contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterProperties(contextManager.getPersistServiceFacade().getMetaDataPersistService().getPropsService().load()); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/QualifiedDataSourceSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/QualifiedDataSourceSubscriber.java deleted file mode 100644 index 13c4e4babecfd..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/QualifiedDataSourceSubscriber.java +++ /dev/null @@ -1,53 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import com.google.common.base.Preconditions; -import com.google.common.eventbus.Subscribe; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource; -import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.storage.QualifiedDataSourceStateEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; - -/** - * Qualified data source subscriber. - */ -@RequiredArgsConstructor -public class QualifiedDataSourceSubscriber implements DispatchEventSubscriber { - - private final ContextManager contextManager; - - /** - * Renew disabled data source names. - * - * @param event qualified data source state event - */ - @Subscribe - public synchronized void renew(final QualifiedDataSourceStateEvent event) { - QualifiedDataSource qualifiedDataSource = event.getQualifiedDataSource(); - Preconditions.checkState(contextManager.getMetaDataContexts().getMetaData().containsDatabase(qualifiedDataSource.getDatabaseName()), - "No database '%s' exists.", qualifiedDataSource.getDatabaseName()); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(qualifiedDataSource.getDatabaseName()); - for (StaticDataSourceRuleAttribute each : database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class)) { - each.updateStatus(qualifiedDataSource, event.getStatus().getState()); - } - } -} diff --git a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder index 8b04a9c552eec..b1312a0905c3c 100644 --- a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder +++ b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder @@ -14,14 +14,3 @@ # See the License for the specific language governing permissions and # limitations under the License. # - -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.ClusterStateDispatchEventBuilder -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type.ShardingSphereDataDispatchEventBuilder -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type.GlobalRuleDispatchEventBuilder -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 -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.type.KillProcessDispatchEventBuilder diff --git a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler new file mode 100644 index 0000000000000..e21cc73a2ad4b --- /dev/null +++ b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler @@ -0,0 +1,27 @@ +# +# 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. +# + +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ClusterStateChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.QualifiedDataSourceChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ComputeNodeOnlineHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ComputeNodeStateChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ShardingSphereDataChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.GlobalRuleChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.PropertiesChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ListenerAssistedChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ShowProcessListHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.KillProcessHandler diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ClusterStateDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ClusterStateDispatchEventBuilderTest.java deleted file mode 100644 index c23bffcea4458..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ClusterStateDispatchEventBuilderTest.java +++ /dev/null @@ -1,55 +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.mode.manager.cluster.event.dispatch.builder.type; - -import org.apache.shardingsphere.mode.state.ClusterState; -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.cluster.ClusterStateEvent; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class ClusterStateDispatchEventBuilderTest { - - private final ClusterStateDispatchEventBuilder builder = new ClusterStateDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is("/states/cluster_state")); - } - - @Test - void assertBuildEventWithValidClusterState() { - Optional actual = builder.build(new DataChangedEvent("/states/cluster_state", ClusterState.READ_ONLY.name(), Type.UPDATED)); - assertTrue(actual.isPresent()); - assertThat(((ClusterStateEvent) actual.get()).getClusterState(), is(ClusterState.READ_ONLY)); - } - - @Test - void assertBuildEventWithInvalidClusterState() { - Optional actual = builder.build(new DataChangedEvent("/states/cluster_state", "INVALID", Type.UPDATED)); - assertTrue(actual.isPresent()); - assertThat(((ClusterStateEvent) actual.get()).getClusterState(), is(ClusterState.OK)); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeOnlineDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeOnlineDispatchEventBuilderTest.java deleted file mode 100644 index 7f01064280cde..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeOnlineDispatchEventBuilderTest.java +++ /dev/null @@ -1,77 +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.mode.manager.cluster.event.dispatch.builder.type; - -import org.apache.shardingsphere.infra.instance.metadata.InstanceType; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; -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.state.compute.instance.InstanceOfflineEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.instance.InstanceOnlineEvent; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class ComputeNodeOnlineDispatchEventBuilderTest { - - private final ComputeNodeOnlineDispatchEventBuilder builder = new ComputeNodeOnlineDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is("/nodes/compute_nodes/online")); - } - - @Test - void assertBuildWithInvalidInstanceOnlinePath() { - assertFalse(builder.build(new DataChangedEvent("/nodes/compute_nodes/online/foo", "{attribute: 127.0.0.1@3307,version: 1}", Type.ADDED)).isPresent()); - } - - @Test - void assertBuildWithInvalidOperationType() { - assertFalse(builder.build(new DataChangedEvent("/nodes/compute_nodes/online/proxy/foo_instance_id", "{attribute: 127.0.0.1@3307,version: 1}", Type.UPDATED)).isPresent()); - } - - @Test - void assertBuildComputeNodeOnlineEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/online/proxy/foo_instance_id", "{attribute: 127.0.0.1@3307,version: 1}", Type.ADDED)); - assertTrue(actual.isPresent()); - InstanceOnlineEvent event = (InstanceOnlineEvent) actual.get(); - assertThat(event.getInstanceMetaData().getId(), is("foo_instance_id")); - assertThat(event.getInstanceMetaData().getIp(), is("127.0.0.1")); - assertThat(event.getInstanceMetaData().getType(), is(InstanceType.PROXY)); - assertThat(event.getInstanceMetaData().getVersion(), is("1")); - assertThat(event.getInstanceMetaData().getAttributes(), is("127.0.0.1@3307")); - } - - @Test - void assertBuildComputeNodeOfflineEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/online/proxy/foo_instance_id", "{attribute: 127.0.0.1@3307,version: 1}", Type.DELETED)); - assertTrue(actual.isPresent()); - InstanceOfflineEvent event = (InstanceOfflineEvent) actual.get(); - assertThat(event.getInstanceMetaData().getId(), is("foo_instance_id")); - assertThat(event.getInstanceMetaData().getIp(), is("127.0.0.1")); - assertThat(event.getInstanceMetaData().getType(), is(InstanceType.PROXY)); - assertThat(event.getInstanceMetaData().getVersion(), is("1")); - assertThat(event.getInstanceMetaData().getAttributes(), is("127.0.0.1@3307")); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeStateDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeStateDispatchEventBuilderTest.java deleted file mode 100644 index da793b58ffe4e..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ComputeNodeStateDispatchEventBuilderTest.java +++ /dev/null @@ -1,105 +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.mode.manager.cluster.event.dispatch.builder.type; - -import org.apache.shardingsphere.infra.state.instance.InstanceState; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ComputeNodeInstanceStateChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.LabelsEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.WorkerIdEvent; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class ComputeNodeStateDispatchEventBuilderTest { - - private final ComputeNodeStateDispatchEventBuilder builder = new ComputeNodeStateDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is("/nodes/compute_nodes")); - } - - @Test - void assertBuildComputeNodeInstanceStateChangedEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/status/foo_instance_id", InstanceState.CIRCUIT_BREAK.name(), Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat(((ComputeNodeInstanceStateChangedEvent) actual.get()).getInstanceId(), is("foo_instance_id")); - assertThat(((ComputeNodeInstanceStateChangedEvent) actual.get()).getStatus(), is(InstanceState.CIRCUIT_BREAK.name())); - } - - @Test - void assertBuildWithEmptyInstanceId() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/status", "", Type.ADDED)); - assertFalse(actual.isPresent()); - } - - @Test - void assertBuildWithDeleteInstanceIdEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/status/foo_instance_id", "", Type.DELETED)); - assertFalse(actual.isPresent()); - } - - @Test - void assertBuildLabelsEventWithLabels() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/labels/foo_instance_id", YamlEngine.marshal(Arrays.asList("label_1", "label_2")), Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat(((LabelsEvent) actual.get()).getInstanceId(), is("foo_instance_id")); - assertThat(((LabelsEvent) actual.get()).getLabels(), is(Arrays.asList("label_1", "label_2"))); - } - - @Test - void assertBuildLabelsEventWithoutLabels() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/labels/foo_instance_id", "", Type.UPDATED)); - assertTrue(actual.isPresent()); - assertThat(((LabelsEvent) actual.get()).getInstanceId(), is("foo_instance_id")); - assertTrue(((LabelsEvent) actual.get()).getLabels().isEmpty()); - } - - @Test - void assertBuildWithDeleteLabelsEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/labels/foo_instance_id", "", Type.DELETED)); - assertFalse(actual.isPresent()); - } - - @Test - void assertBuildWorkerIdEventWithWorkerId() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/worker_id/foo_instance_id", "1", Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat(((WorkerIdEvent) actual.get()).getInstanceId(), is("foo_instance_id")); - assertThat(((WorkerIdEvent) actual.get()).getWorkerId(), is(1)); - } - - @Test - void assertBuildWorkerIdEventWithoutWorkerId() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/worker_id/foo_instance_id", "", Type.DELETED)); - assertTrue(actual.isPresent()); - assertThat(((WorkerIdEvent) actual.get()).getInstanceId(), is("foo_instance_id")); - assertNull(((WorkerIdEvent) actual.get()).getWorkerId()); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/GlobalRuleDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/GlobalRuleDispatchEventBuilderTest.java deleted file mode 100644 index b112cdd27c536..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/GlobalRuleDispatchEventBuilderTest.java +++ /dev/null @@ -1,56 +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.mode.manager.cluster.event.dispatch.builder.type; - -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.config.AlterGlobalRuleConfigurationEvent; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class GlobalRuleDispatchEventBuilderTest { - - private final GlobalRuleDispatchEventBuilder builder = new GlobalRuleDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is("/rules")); - } - - @Test - void assertBuildAlterGlobalRuleConfigurationEvent() { - Optional actual = builder.build(new DataChangedEvent("/rules/foo_rule/active_version", "rule_value", Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat(((AlterGlobalRuleConfigurationEvent) actual.get()).getRuleSimpleName(), is("foo_rule")); - assertThat(((AlterGlobalRuleConfigurationEvent) actual.get()).getActiveVersionKey(), is("/rules/foo_rule/active_version")); - assertThat(((AlterGlobalRuleConfigurationEvent) actual.get()).getActiveVersion(), is("rule_value")); - } - - @Test - void assertBuildWithInvalidEventKey() { - Optional actual = builder.build(new DataChangedEvent("/rules/foo_rule/xxx", "rule_value", Type.ADDED)); - assertFalse(actual.isPresent()); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/KillProcessDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/KillProcessDispatchEventBuilderTest.java deleted file mode 100644 index dbd281442b894..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/KillProcessDispatchEventBuilderTest.java +++ /dev/null @@ -1,75 +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.mode.manager.cluster.event.dispatch.builder.type; - -import org.apache.shardingsphere.metadata.persist.node.ComputeNode; -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.KillLocalProcessCompletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.KillLocalProcessEvent; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class KillProcessDispatchEventBuilderTest { - - private final KillProcessDispatchEventBuilder builder = new KillProcessDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is(ComputeNode.getKillProcessTriggerNodePath())); - } - - @Test - void assertGetSubscribedTypes() { - assertThat(builder.getSubscribedTypes().size(), is(2)); - } - - @Test - void assertBuildKillLocalProcessEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/kill_process_trigger/foo_instance_id:foo_pid", "", Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat(((KillLocalProcessEvent) actual.get()).getInstanceId(), is("foo_instance_id")); - assertThat(((KillLocalProcessEvent) actual.get()).getProcessId(), is("foo_pid")); - } - - @Test - void assertBuildReportKillLocalProcessCompletedEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/kill_process_trigger/foo_instance_id:foo_pid", "", Type.DELETED)); - assertTrue(actual.isPresent()); - assertThat(((KillLocalProcessCompletedEvent) actual.get()).getProcessId(), is("foo_pid")); - } - - @Test - void assertBuildWithUpdateReportKillLocalProcessCompletedEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/kill_process_trigger/foo_instance_id:foo_pid", "", Type.UPDATED)); - assertFalse(actual.isPresent()); - } - - @Test - void assertBuildWithInvalidKillProcessListTriggerEventKey() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/kill_process_trigger/foo_instance_id", "", Type.ADDED)); - assertFalse(actual.isPresent()); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ListenerAssistedDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ListenerAssistedDispatchEventBuilderTest.java deleted file mode 100644 index 6d7fd4f8f5ea4..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ListenerAssistedDispatchEventBuilderTest.java +++ /dev/null @@ -1,62 +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.mode.manager.cluster.event.dispatch.builder.type; - -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.CreateDatabaseListenerAssistedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.DropDatabaseListenerAssistedEvent; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class ListenerAssistedDispatchEventBuilderTest { - - private final ListenerAssistedDispatchEventBuilder builder = new ListenerAssistedDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is("/states/listener_assisted")); - } - - @Test - void assertBuildCreateDatabaseListenerAssistedEvent() { - Optional actual = builder.build(new DataChangedEvent("/states/listener_assisted/foo_db", "CREATE_DATABASE", Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat(((CreateDatabaseListenerAssistedEvent) actual.get()).getDatabaseName(), is("foo_db")); - } - - @Test - void assertBuildDropDatabaseListenerAssistedEvent() { - Optional actual = builder.build(new DataChangedEvent("/states/listener_assisted/foo_db", "DROP_DATABASE", Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat(((DropDatabaseListenerAssistedEvent) actual.get()).getDatabaseName(), is("foo_db")); - } - - @Test - void assertBuildWithoutDatabase() { - Optional actual = builder.build(new DataChangedEvent("/states/listener_assisted", "", Type.ADDED)); - assertFalse(actual.isPresent()); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilderTest.java deleted file mode 100644 index ac2b04b0b25b2..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/PropertiesDispatchEventBuilderTest.java +++ /dev/null @@ -1,54 +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.mode.manager.cluster.event.dispatch.builder.type; - -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.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class PropertiesDispatchEventBuilderTest { - - private final PropertiesDispatchEventBuilder builder = new PropertiesDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is("/props")); - } - - @Test - void assertBuildAlterPropertiesEvent() { - Optional actual = builder.build(new DataChangedEvent("/props/active_version", "key=value", Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat((actual.get()).getActiveVersionKey(), is("/props/active_version")); - assertThat((actual.get()).getActiveVersion(), is("key=value")); - } - - @Test - void assertBuildWithInvalidEventKey() { - Optional actual = builder.build(new DataChangedEvent("/props/xxx", "key=value", Type.ADDED)); - assertFalse(actual.isPresent()); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/QualifiedDataSourceDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/QualifiedDataSourceDispatchEventBuilderTest.java deleted file mode 100644 index 07f8158be28e9..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/QualifiedDataSourceDispatchEventBuilderTest.java +++ /dev/null @@ -1,64 +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.mode.manager.cluster.event.dispatch.builder.type; - -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; -import org.apache.shardingsphere.infra.state.datasource.DataSourceState; -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.state.storage.QualifiedDataSourceStateEvent; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class QualifiedDataSourceDispatchEventBuilderTest { - - private final QualifiedDataSourceDispatchEventBuilder builder = new QualifiedDataSourceDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is("/nodes/qualified_data_sources")); - } - - @Test - void assertBuildQualifiedDataSourceStateEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/qualified_data_sources/foo_db.foo_group.foo_ds", "state: ENABLED", Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat(((QualifiedDataSourceStateEvent) actual.get()).getQualifiedDataSource().getDatabaseName(), is("foo_db")); - assertThat(((QualifiedDataSourceStateEvent) actual.get()).getQualifiedDataSource().getGroupName(), is("foo_group")); - assertThat(((QualifiedDataSourceStateEvent) actual.get()).getQualifiedDataSource().getDataSourceName(), is("foo_ds")); - assertThat(((QualifiedDataSourceStateEvent) actual.get()).getStatus().getState(), is(DataSourceState.ENABLED)); - } - - @Test - void assertBuildWithEmptyValue() { - Optional actual = builder.build(new DataChangedEvent("/nodes/qualified_data_sources/foo_db.foo_group.foo_ds", "", Type.ADDED)); - assertFalse(actual.isPresent()); - } - - @Test - void assertBuildWithoutQualifiedDataSource() { - Optional actual = builder.build(new DataChangedEvent("/nodes/qualified_data_sources", "state: DISABLED", Type.ADDED)); - assertFalse(actual.isPresent()); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShardingSphereDataDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShardingSphereDataDispatchEventBuilderTest.java deleted file mode 100644 index 2120e3df0260d..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShardingSphereDataDispatchEventBuilderTest.java +++ /dev/null @@ -1,152 +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.mode.manager.cluster.event.dispatch.builder.type; - -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.DatabaseDataAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.DatabaseDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.SchemaDataAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.SchemaDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.ShardingSphereRowDataChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.ShardingSphereRowDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.TableDataChangedEvent; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.ArgumentsProvider; -import org.junit.jupiter.params.provider.ArgumentsSource; -import java.util.Optional; -import java.util.function.Consumer; -import java.util.stream.Stream; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertNull; - -class ShardingSphereDataDispatchEventBuilderTest { - - private final ShardingSphereDataDispatchEventBuilder builder = new ShardingSphereDataDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is("/statistics/databases")); - } - - @ParameterizedTest(name = "{0}") - @ArgumentsSource(TestCaseArgumentsProvider.class) - void assertBuild(final String name, final String eventKey, final String eventValue, final DataChangedEvent.Type type, final boolean isEventPresent, final Consumer consumer) { - Optional actual = builder.build(new DataChangedEvent(eventKey, eventValue, type)); - assertThat(actual.isPresent(), is(isEventPresent)); - if (actual.isPresent() && consumer != null) { - consumer.accept(actual.get()); - } - } - - static void assertBuildDatabaseDataChangedEventWithAdd(final DispatchEvent actual) { - assertThat(((DatabaseDataAddedEvent) actual).getDatabaseName(), is("foo_db")); - } - - static void assertBuildDatabaseDataChangedEventWithUpdate(final DispatchEvent actual) { - assertThat(((DatabaseDataAddedEvent) actual).getDatabaseName(), is("foo_db")); - } - - static void assertBuildDatabaseDataChangedEventWithDelete(final DispatchEvent actual) { - assertThat(((DatabaseDataDeletedEvent) actual).getDatabaseName(), is("foo_db")); - } - - static void assertBuildSchemaDataADDEDEvent(final DispatchEvent actual) { - assertThat(((SchemaDataAddedEvent) actual).getDatabaseName(), is("foo_db")); - assertThat(((SchemaDataAddedEvent) actual).getSchemaName(), is("foo_schema")); - } - - static void assertBuildSchemaDataChangedEventWithDelete(final DispatchEvent actual) { - assertThat(((SchemaDataDeletedEvent) actual).getDatabaseName(), is("foo_db")); - assertThat(((SchemaDataDeletedEvent) actual).getSchemaName(), is("foo_schema")); - } - - static void assertBuildRowDataChangedEventWithDelete(final DispatchEvent actual) { - assertThat(((ShardingSphereRowDataDeletedEvent) actual).getDatabaseName(), is("foo_db")); - assertThat(((ShardingSphereRowDataDeletedEvent) actual).getSchemaName(), is("foo_schema")); - assertThat(((ShardingSphereRowDataDeletedEvent) actual).getTableName(), is("foo_tbl")); - assertThat(((ShardingSphereRowDataDeletedEvent) actual).getUniqueKey(), is("1")); - } - - static void assertBuildRowDataChangedEvent(final DispatchEvent actual) { - assertThat(((ShardingSphereRowDataChangedEvent) actual).getDatabaseName(), is("foo_db")); - assertThat(((ShardingSphereRowDataChangedEvent) actual).getSchemaName(), is("foo_schema")); - assertThat(((ShardingSphereRowDataChangedEvent) actual).getTableName(), is("foo_tbl")); - assertThat(((ShardingSphereRowDataChangedEvent) actual).getYamlRowData().getUniqueKey(), is("1")); - } - - static void assertBuildTableDataChangedEventWithDelete(final DispatchEvent actual) { - assertThat(((TableDataChangedEvent) actual).getDatabaseName(), is("foo_db")); - assertThat(((TableDataChangedEvent) actual).getSchemaName(), is("foo_schema")); - assertNull(((TableDataChangedEvent) actual).getAddedTable()); - assertThat(((TableDataChangedEvent) actual).getDeletedTable(), is("foo_tbl")); - } - - static void assertBuildTableDataChangedEvent(final DispatchEvent actual) { - assertThat(((TableDataChangedEvent) actual).getDatabaseName(), is("foo_db")); - assertThat(((TableDataChangedEvent) actual).getSchemaName(), is("foo_schema")); - assertThat(((TableDataChangedEvent) actual).getAddedTable(), is("foo_tbl")); - assertNull(((TableDataChangedEvent) actual).getDeletedTable()); - } - - private static class TestCaseArgumentsProvider implements ArgumentsProvider { - - @Override - public Stream provideArguments(final ExtensionContext extensionContext) { - return Stream.of( - Arguments.of("assertBuildDatabaseDataChangedEventWithAdd", "/statistics/databases/foo_db", "", Type.ADDED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildDatabaseDataChangedEventWithAdd), - Arguments.of("assertBuildDatabaseDataChangedEventWithUpdate", "/statistics/databases/foo_db", "", Type.UPDATED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildDatabaseDataChangedEventWithUpdate), - Arguments.of("assertBuildDatabaseDataChangedEventWithDelete", "/statistics/databases/foo_db", "", Type.DELETED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildDatabaseDataChangedEventWithDelete), - Arguments.of("assertBuildSchemaDataChangedEventWithAdd", "/statistics/databases/foo_db/schemas/foo_schema", "", Type.ADDED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildSchemaDataADDEDEvent), - Arguments.of("assertBuildSchemaDataChangedEventWithUpdate", "/statistics/databases/foo_db/schemas/foo_schema", "", Type.UPDATED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildSchemaDataADDEDEvent), - Arguments.of("assertBuildSchemaDataChangedEventWithDelete", "/statistics/databases/foo_db/schemas/foo_schema", "", Type.DELETED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildSchemaDataChangedEventWithDelete), - Arguments.of("assertBuildRowDataChangedEventWithAddNullValue", "/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/1", "", Type.ADDED, false, null), - Arguments.of("assertBuildWithMissedDatabaseNameEventKey", "/statistics/databases", "", Type.ADDED, false, null), - Arguments.of("assertBuildWithMissedSchemaNameEventKey", "/statistics/databases/foo_db/schemas", "", Type.ADDED, false, null), - Arguments.of("assertBuildWithMissedTableNameEventKey", "/statistics/databases/foo_db/schemas/foo_schema/tables", "", Type.ADDED, false, null), - Arguments.of("assertBuildDatabaseDataChangedEventWithIgnore", "/statistics/databases/foo_db", "", Type.IGNORED, false, null), - Arguments.of("assertBuildTableDataChangedEventWithIgnore", "/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl", "", Type.IGNORED, false, null), - Arguments.of("assertBuildSchemaDataChangedEventWithIgnore", "/statistics/databases/foo_db/schemas/foo_schema", "", Type.IGNORED, false, null), - Arguments.of("assertBuildWithMissedRowEventKey", "/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/", "", Type.UPDATED, false, null), - Arguments.of("assertBuildRowDataChangedEventWithDelete", "/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/1", "", Type.DELETED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildRowDataChangedEventWithDelete), - Arguments.of("assertBuildRowDataChangedEventWithUpdate", "/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/1", "{uniqueKey: 1}", Type.UPDATED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildRowDataChangedEvent), - Arguments.of("assertBuildRowDataChangedEventWithAdd", "/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/xxx", "{uniqueKey: 1}", Type.ADDED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildRowDataChangedEvent), - Arguments.of("assertBuildTableDataChangedEventWithDelete", "/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl", "", Type.DELETED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildTableDataChangedEventWithDelete), - Arguments.of("assertBuildTableDataChangedEventWithUpdate", "/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl", "", Type.UPDATED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildTableDataChangedEvent), - Arguments.of("assertBuildTableDataChangedEventWithAdd", "/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl", "", Type.ADDED, true, - (Consumer) ShardingSphereDataDispatchEventBuilderTest::assertBuildTableDataChangedEvent)); - } - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShowProcessListDispatchEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShowProcessListDispatchEventBuilderTest.java deleted file mode 100644 index 51eb09873ed50..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/type/ShowProcessListDispatchEventBuilderTest.java +++ /dev/null @@ -1,75 +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.mode.manager.cluster.event.dispatch.builder.type; - -import org.apache.shardingsphere.metadata.persist.node.ComputeNode; -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ReportLocalProcessesCompletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ReportLocalProcessesEvent; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class ShowProcessListDispatchEventBuilderTest { - - private final ShowProcessListDispatchEventBuilder builder = new ShowProcessListDispatchEventBuilder(); - - @Test - void assertGetSubscribedKey() { - assertThat(builder.getSubscribedKey(), is(ComputeNode.getShowProcessListTriggerNodePath())); - } - - @Test - void assertGetSubscribedTypes() { - assertThat(builder.getSubscribedTypes().size(), is(2)); - } - - @Test - void assertBuildReportLocalProcessesEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/show_process_list_trigger/foo_instance_id:foo_task_id", "", Type.ADDED)); - assertTrue(actual.isPresent()); - assertThat(((ReportLocalProcessesEvent) actual.get()).getInstanceId(), is("foo_instance_id")); - assertThat(((ReportLocalProcessesEvent) actual.get()).getTaskId(), is("foo_task_id")); - } - - @Test - void assertBuildReportLocalProcessesCompletedEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/show_process_list_trigger/foo_instance_id:foo_task_id", "", Type.DELETED)); - assertTrue(actual.isPresent()); - assertThat(((ReportLocalProcessesCompletedEvent) actual.get()).getTaskId(), is("foo_task_id")); - } - - @Test - void assertBuildWithUpdateReportLocalProcessesCompletedEvent() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/show_process_list_trigger/foo_instance_id:foo_task_id", "", Type.UPDATED)); - assertFalse(actual.isPresent()); - } - - @Test - void assertBuildWithInvalidShowProcessListTriggerEventKey() { - Optional actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/show_process_list_trigger/foo_instance_id", "", Type.ADDED)); - assertFalse(actual.isPresent()); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StateChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandlerTest.java similarity index 61% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StateChangedSubscriberTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandlerTest.java index 9d29ac80f9c68..b8fc6a033ad41 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StateChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandlerTest.java @@ -15,11 +15,14 @@ * 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.state.ClusterState; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.cluster.ClusterStateEvent; +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.apache.shardingsphere.mode.state.ClusterState; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -30,21 +33,28 @@ import static org.mockito.Mockito.verify; @ExtendWith(MockitoExtension.class) -class StateChangedSubscriberTest { +class ClusterStateChangedHandlerTest { - private StateChangedSubscriber subscriber; + private DataChangedEventHandler handler; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ContextManager contextManager; @BeforeEach void setUp() { - subscriber = new StateChangedSubscriber(contextManager); + handler = ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).stream() + .filter(each -> each.getSubscribedKey().equals("/states/cluster_state")).findFirst().orElse(null); } @Test - void assertRenew() { - subscriber.renew(new ClusterStateEvent(ClusterState.READ_ONLY)); + void assertHandleWithValidClusterState() { + handler.handle(contextManager, new DataChangedEvent("/states/cluster_state", ClusterState.READ_ONLY.name(), Type.UPDATED)); verify(contextManager.getStateContext()).switchState(ClusterState.READ_ONLY); } + + @Test + void assertHandleWithInvalidClusterState() { + handler.handle(contextManager, new DataChangedEvent("/states/cluster_state", "INVALID", Type.UPDATED)); + verify(contextManager.getStateContext()).switchState(ClusterState.OK); + } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ComputeNodeStateSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandlerTest.java similarity index 51% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ComputeNodeStateSubscriberTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandlerTest.java index c04be8ad8af1e..43f44ee288f64 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ComputeNodeStateSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandlerTest.java @@ -15,16 +15,14 @@ * 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.infra.instance.ComputeNodeInstance; -import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ComputeNodeInstanceStateChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.LabelsEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.WorkerIdEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.instance.InstanceOfflineEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.instance.InstanceOnlineEvent; +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; @@ -32,56 +30,43 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.Collections; - import static org.mockito.ArgumentMatchers.any; 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 ComputeNodeStateSubscriberTest { +class ComputeNodeOnlineHandlerTest { - private ComputeNodeStateSubscriber subscriber; + private DataChangedEventHandler handler; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ContextManager contextManager; @BeforeEach void setUp() { - subscriber = new ComputeNodeStateSubscriber(contextManager); + handler = ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).stream() + .filter(each -> each.getSubscribedKey().equals("/nodes/compute_nodes/online")).findFirst().orElse(null); } @Test - void assertRenewWithInstanceOnlineEvent() { - InstanceMetaData instanceMetaData = mock(InstanceMetaData.class); - ComputeNodeInstance computeNodeInstance = mock(ComputeNodeInstance.class); - when(contextManager.getPersistServiceFacade().getComputeNodePersistService().loadComputeNodeInstance(instanceMetaData)).thenReturn(computeNodeInstance); - subscriber.renew(new InstanceOnlineEvent(instanceMetaData)); - verify(contextManager.getComputeNodeInstanceContext().getClusterInstanceRegistry()).add(computeNodeInstance); - } - - @Test - void assertRenewWithInstanceOfflineEvent() { - subscriber.renew(new InstanceOfflineEvent(mock(InstanceMetaData.class))); - verify(contextManager.getComputeNodeInstanceContext().getClusterInstanceRegistry()).delete(any()); + void assertHandleWithInvalidInstanceOnlinePath() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/online/foo", "{attribute: 127.0.0.1@3307,version: 1}", Type.ADDED)); + verify(contextManager, times(0)).getComputeNodeInstanceContext(); } @Test - void assertRenewWithComputeNodeInstanceStateChangedEvent() { - subscriber.renew(new ComputeNodeInstanceStateChangedEvent("foo_instance_id", "OK")); - verify(contextManager.getComputeNodeInstanceContext()).updateStatus("foo_instance_id", "OK"); - } - - @Test - void assertRenewWithWorkerIdEvent() { - subscriber.renew(new WorkerIdEvent("foo_instance_id", 1)); - verify(contextManager.getComputeNodeInstanceContext()).updateWorkerId("foo_instance_id", 1); + void assertHandleWithInstanceOnlineEvent() { + ComputeNodeInstance computeNodeInstance = mock(ComputeNodeInstance.class); + when(contextManager.getPersistServiceFacade().getComputeNodePersistService().loadComputeNodeInstance(any())).thenReturn(computeNodeInstance); + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/online/proxy/foo_instance_id", "{attribute: 127.0.0.1@3307,version: 1}", Type.ADDED)); + verify(contextManager.getComputeNodeInstanceContext().getClusterInstanceRegistry()).add(computeNodeInstance); } @Test - void assertRenewWithLabelsEvent() { - subscriber.renew(new LabelsEvent("foo_instance_id", Collections.emptyList())); - verify(contextManager.getComputeNodeInstanceContext()).updateLabels("foo_instance_id", Collections.emptyList()); + void assertHandleWithInstanceOfflineEvent() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/online/proxy/foo_instance_id", "{attribute: 127.0.0.1@3307,version: 1}", Type.DELETED)); + verify(contextManager.getComputeNodeInstanceContext().getClusterInstanceRegistry()).delete(any()); } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandlerTest.java new file mode 100644 index 0000000000000..c9c66df4ef6e4 --- /dev/null +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandlerTest.java @@ -0,0 +1,74 @@ +/* + * 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.dispatch.handler.type; + +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; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Collections; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@ExtendWith(MockitoExtension.class) +class ComputeNodeStateChangedHandlerTest { + + private DataChangedEventHandler handler; + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private ContextManager contextManager; + + @BeforeEach + void setUp() { + handler = ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).stream() + .filter(each -> each.getSubscribedKey().equals("/nodes/compute_nodes")).findFirst().orElse(null); + } + + @Test + void assertHandleWithEmptyInstanceId() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/status", "", Type.ADDED)); + verify(contextManager, times(0)).getComputeNodeInstanceContext(); + } + + @Test + void assertHandleWithComputeNodeInstanceStateChangedEvent() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/status/foo_instance_id", "OK", Type.ADDED)); + verify(contextManager.getComputeNodeInstanceContext()).updateStatus("foo_instance_id", "OK"); + } + + @Test + void assertHandleWithLabelsEvent() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/labels/foo_instance_id", "", Type.ADDED)); + verify(contextManager.getComputeNodeInstanceContext()).updateLabels("foo_instance_id", Collections.emptyList()); + } + + @Test + void assertHandleWithWorkerIdEvent() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/worker_id/foo_instance_id", "1", Type.ADDED)); + verify(contextManager.getComputeNodeInstanceContext()).updateWorkerId("foo_instance_id", 1); + } +} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandlerTest.java similarity index 50% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriberTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandlerTest.java index e6aea84fb9936..83086a2e620fc 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/PropertiesEventSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandlerTest.java @@ -15,10 +15,14 @@ * 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.config.rule.RuleConfiguration; +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; @@ -26,29 +30,46 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.Properties; +import java.util.Optional; +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 GlobalRuleChangedHandlerTest { - 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("/rules")).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("/rules/foo_rule/xxx", "rule_value", Type.ADDED)); + verify(contextManager, times(0)).getPersistServiceFacade(); + } + + @Test + void assertHandleWithEmptyRuleName() { + handler.handle(contextManager, new DataChangedEvent("/rules/foo_rule/active_version/foo", "rule_value", Type.ADDED)); + verify(contextManager, times(0)).getPersistServiceFacade(); + } + + @Test + void assertHandle() { + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("/rules/foo_rule/active_version")) + .thenReturn("rule_value"); + RuleConfiguration ruleConfig = mock(RuleConfiguration.class); + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load("foo_rule")).thenReturn(Optional.of(ruleConfig)); + handler.handle(contextManager, new DataChangedEvent("/rules/foo_rule/active_version", "rule_value", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getGlobalConfigurationManager()).alterGlobalRuleConfiguration(ruleConfig); } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java new file mode 100644 index 0000000000000..990844db7916c --- /dev/null +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java @@ -0,0 +1,83 @@ +/* + * 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.dispatch.handler.type; + +import org.apache.shardingsphere.infra.executor.sql.process.lock.ProcessOperationLockRegistry; +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.apache.shardingsphere.test.mock.AutoMockExtension; +import org.apache.shardingsphere.test.mock.StaticMockSettings; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import java.sql.SQLException; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(AutoMockExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +@StaticMockSettings(ProcessOperationLockRegistry.class) +class KillProcessHandlerTest { + + private DataChangedEventHandler handler; + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private ContextManager contextManager; + + @BeforeEach + void setUp() { + when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getId()).thenReturn("foo_instance_id"); + handler = ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).stream() + .filter(each -> each.getSubscribedKey().equals("/nodes/compute_nodes/kill_process_trigger")).findFirst().orElse(null); + } + + @Test + void assertHandleWithInvalidKillProcessListTriggerEventKey() throws SQLException { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/kill_process_trigger/foo_instance_id", "", Type.DELETED)); + verify(ProcessOperationLockRegistry.getInstance(), times(0)).notify(any()); + } + + @Test + void assertHandleKillLocalProcessWithCurrentInstance() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/kill_process_trigger/foo_instance_id:foo_pid", "", Type.ADDED)); + verify(contextManager.getPersistCoordinatorFacade().getProcessPersistCoordinator()).cleanProcess("foo_instance_id", "foo_pid"); + } + + @Test + void assertHandleKillLocalProcessWithNotCurrentInstance() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/kill_process_trigger/bar_instance_id:foo_pid", "", Type.ADDED)); + verify(contextManager.getPersistCoordinatorFacade().getProcessPersistCoordinator(), times(0)).cleanProcess("bar_instance_id", "foo_pid"); + } + + @Test + void assertHandleCompleteToKillLocalProcess() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/kill_process_trigger/foo_instance_id:foo_pid", "", Type.DELETED)); + verify(ProcessOperationLockRegistry.getInstance()).notify("foo_pid"); + } +} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ListenerAssistedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandlerTest.java similarity index 72% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ListenerAssistedSubscriberTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandlerTest.java index 02b0094c87093..f227c6c841108 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ListenerAssistedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandlerTest.java @@ -15,12 +15,14 @@ * 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.infra.instance.metadata.InstanceType; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.CreateDatabaseListenerAssistedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.DropDatabaseListenerAssistedEvent; +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.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -31,13 +33,14 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) -class ListenerAssistedSubscriberTest { +class ListenerAssistedChangedHandlerTest { - private ListenerAssistedSubscriber subscriber; + private DataChangedEventHandler handler; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ContextManager contextManager; @@ -48,14 +51,20 @@ class ListenerAssistedSubscriberTest { @BeforeEach void setUp() { when(contextManager.getPersistServiceFacade().getRepository()).thenReturn(repository); - subscriber = new ListenerAssistedSubscriber(contextManager); + handler = ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).stream() + .filter(each -> each.getSubscribedKey().equals("/states/listener_assisted")).findFirst().orElse(null); + } + + @Test + void assertHandleWithoutDatabase() { + handler.handle(contextManager, new DataChangedEvent("/states/listener_assisted", "", Type.ADDED)); + verify(contextManager.getPersistServiceFacade(), times(0)).getListenerAssistedPersistService(); } @Test void assertRenewWithCreateDatabaseListenerAssistedEvent() { - when(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()).thenReturn(true); when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()).thenReturn(InstanceType.JDBC); - subscriber.renew(new CreateDatabaseListenerAssistedEvent("foo_db")); + handler.handle(contextManager, new DataChangedEvent("/states/listener_assisted/foo_db", "CREATE_DATABASE", Type.ADDED)); verify(repository).watch(eq("/metadata/foo_db"), any()); verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).addDatabase("foo_db"); verify(contextManager.getPersistServiceFacade().getListenerAssistedPersistService()).deleteDatabaseNameListenerAssisted("foo_db"); @@ -63,9 +72,8 @@ void assertRenewWithCreateDatabaseListenerAssistedEvent() { @Test void assertRenewWithDropDatabaseListenerAssistedEvent() { - when(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()).thenReturn(true); when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()).thenReturn(InstanceType.PROXY); - subscriber.renew(new DropDatabaseListenerAssistedEvent("foo_db")); + handler.handle(contextManager, new DataChangedEvent("/states/listener_assisted/foo_db", "DROP_DATABASE", Type.ADDED)); verify(repository).removeDataListener("/metadata/foo_db"); verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).dropDatabase("foo_db"); verify(contextManager.getPersistServiceFacade().getListenerAssistedPersistService()).deleteDatabaseNameListenerAssisted("foo_db"); diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/GlobalRuleConfigurationEventSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandlerTest.java similarity index 59% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/GlobalRuleConfigurationEventSubscriberTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandlerTest.java index 5b772b53bdb27..d251a203780be 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/GlobalRuleConfigurationEventSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandlerTest.java @@ -15,11 +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.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.config.AlterGlobalRuleConfigurationEvent; +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; @@ -27,31 +29,39 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.Optional; +import java.util.Properties; -import static org.mockito.ArgumentMatchers.any; 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 GlobalRuleConfigurationEventSubscriberTest { +class PropertiesChangedHandlerTest { - private GlobalRuleConfigurationEventSubscriber subscriber; + private DataChangedEventHandler handler; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ContextManager contextManager; @BeforeEach void setUp() { - subscriber = new GlobalRuleConfigurationEventSubscriber(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().getGlobalRuleService().load("foo")).thenReturn(Optional.of(mock(RuleConfiguration.class))); - subscriber.renew(new AlterGlobalRuleConfigurationEvent("foo", "key", "value")); - verify(contextManager.getMetaDataContextManager().getGlobalConfigurationManager()).alterGlobalRuleConfiguration(any()); + 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); } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/QualifiedDataSourceSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandlerTest.java similarity index 59% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/QualifiedDataSourceSubscriberTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandlerTest.java index 4342022497831..cedf77812c457 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/QualifiedDataSourceSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandlerTest.java @@ -15,14 +15,16 @@ * 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.infra.metadata.database.schema.QualifiedDataSource; import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute; +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.infra.state.datasource.DataSourceState; -import org.apache.shardingsphere.infra.state.datasource.qualified.QualifiedDataSourceState; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.storage.QualifiedDataSourceStateEvent; +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; @@ -32,31 +34,46 @@ import java.util.Collections; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.refEq; 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 QualifiedDataSourceSubscriberTest { +class QualifiedDataSourceChangedHandlerTest { - private QualifiedDataSourceSubscriber subscriber; + private DataChangedEventHandler handler; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ContextManager contextManager; @BeforeEach void setUp() { - subscriber = new QualifiedDataSourceSubscriber(contextManager); + handler = ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).stream() + .filter(each -> each.getSubscribedKey().equals("/nodes/qualified_data_sources")).findFirst().orElse(null); } @Test - void assertRenew() { + void assertHandleWithEmptyValue() { + handler.handle(contextManager, new DataChangedEvent("/nodes/qualified_data_sources/foo_db.foo_group.foo_ds", "", Type.ADDED)); + verify(contextManager.getMetaDataContexts(), times(0)).getMetaData(); + } + + @Test + void assertHandleWithoutQualifiedDataSource() { + handler.handle(contextManager, new DataChangedEvent("/nodes/qualified_data_sources", "state: DISABLED", Type.ADDED)); + verify(contextManager.getMetaDataContexts(), times(0)).getMetaData(); + } + + @Test + void assertHandle() { when(contextManager.getMetaDataContexts().getMetaData().containsDatabase("foo_db")).thenReturn(true); StaticDataSourceRuleAttribute staticDataSourceRuleAttribute = mock(StaticDataSourceRuleAttribute.class); when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class)) .thenReturn(Collections.singleton(staticDataSourceRuleAttribute)); - QualifiedDataSource qualifiedDataSource = new QualifiedDataSource("foo_db.foo_group.foo_ds"); - subscriber.renew(new QualifiedDataSourceStateEvent(qualifiedDataSource, new QualifiedDataSourceState(DataSourceState.DISABLED))); - verify(staticDataSourceRuleAttribute).updateStatus(qualifiedDataSource, DataSourceState.DISABLED); + handler.handle(contextManager, new DataChangedEvent("/nodes/qualified_data_sources/foo_db.foo_group.foo_ds", "state: DISABLED", Type.ADDED)); + verify(staticDataSourceRuleAttribute).updateStatus(refEq(new QualifiedDataSource("foo_db.foo_group.foo_ds")), eq(DataSourceState.DISABLED)); } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandlerTest.java new file mode 100644 index 0000000000000..909b60f876b20 --- /dev/null +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandlerTest.java @@ -0,0 +1,126 @@ +/* + * 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.dispatch.handler.type; + +import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; +import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; +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; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.refEq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@ExtendWith(MockitoExtension.class) +class ShardingSphereDataChangedHandlerTest { + + private DataChangedEventHandler handler; + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private ContextManager contextManager; + + @BeforeEach + void setUp() { + handler = ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).stream() + .filter(each -> each.getSubscribedKey().equals("/statistics/databases")).findFirst().orElse(null); + } + + @Test + void assertHandleWithDatabaseAdded() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db", "", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager()).addShardingSphereDatabaseData("foo_db"); + } + + @Test + void assertHandleWithDatabaseDeleted() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db", "", Type.DELETED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager()).dropShardingSphereDatabaseData("foo_db"); + } + + @Test + void assertHandleWithSchemaAdded() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db/schemas/foo_schema", "", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager()).addShardingSphereSchemaData("foo_db", "foo_schema"); + } + + @Test + void assertHandleWithSchemaDeleted() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db/schemas/foo_schema", "", Type.DELETED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager()).dropShardingSphereSchemaData("foo_db", "foo_schema"); + } + + @Test + void assertHandleWithTableAdded() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl", "", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager()).addShardingSphereTableData("foo_db", "foo_schema", "foo_tbl"); + } + + @Test + void assertHandleWithTableDeleted() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl", "", Type.DELETED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager()).dropShardingSphereTableData("foo_db", "foo_schema", "foo_tbl"); + } + + @Test + void assertHandleWithShardingSphereRowChanged() { + YamlShardingSphereRowData rowData = new YamlShardingSphereRowData(); + rowData.setUniqueKey("1"); + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/1", "{uniqueKey: 1}", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager()).alterShardingSphereRowData(eq("foo_db"), eq("foo_schema"), eq("foo_tbl"), refEq(rowData)); + } + + @Test + void assertHandleWithShardingSphereRowDeleted() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/1", "{uniqueKey: 1}", Type.DELETED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager()).deleteShardingSphereRowData("foo_db", "foo_schema", "foo_tbl", "1"); + } + + @Test + void assertHandleWithMissedDatabaseNameEventKey() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases", "=", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager(), times(0)).addShardingSphereDatabaseData(any()); + } + + @Test + void assertHandleWithMissedSchemaNameEventKey() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db/schemas", "=", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager(), times(0)).addShardingSphereSchemaData(any(), any()); + } + + @Test + void assertHandleWithMissedTableNameEventKey() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db/schemas/foo_schema/tables", "", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager(), times(0)).addShardingSphereTableData(any(), any(), any()); + } + + @Test + void assertHandleWithMissedRowEventKey() { + handler.handle(contextManager, new DataChangedEvent("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl/", "", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getDatabaseManager(), times(0)).alterShardingSphereRowData(any(), any(), any(), any()); + } +} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ProcessListChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandlerTest.java similarity index 54% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ProcessListChangedSubscriberTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandlerTest.java index c5198c25d3a26..44c7baeed7ec2 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/ProcessListChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandlerTest.java @@ -15,16 +15,15 @@ * 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.infra.executor.sql.process.ProcessRegistry; import org.apache.shardingsphere.infra.executor.sql.process.lock.ProcessOperationLockRegistry; +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.event.state.compute.KillLocalProcessCompletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.KillLocalProcessEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ReportLocalProcessesCompletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.state.compute.ReportLocalProcessesEvent; -import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; +import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; import org.junit.jupiter.api.BeforeEach; @@ -35,11 +34,9 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import java.sql.SQLException; import java.util.Collections; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -47,54 +44,42 @@ @ExtendWith(AutoMockExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) @StaticMockSettings({ProcessRegistry.class, ProcessOperationLockRegistry.class}) -class ProcessListChangedSubscriberTest { +class ShowProcessListHandlerTest { - private ProcessListChangedSubscriber subscriber; + private DataChangedEventHandler handler; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ContextManager contextManager; @BeforeEach void setUp() { - when(contextManager.getPersistServiceFacade().getRepository()).thenReturn(mock(ClusterPersistRepository.class)); when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getId()).thenReturn("foo_instance_id"); - subscriber = new ProcessListChangedSubscriber(contextManager); + handler = ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).stream() + .filter(each -> each.getSubscribedKey().equals("/nodes/compute_nodes/show_process_list_trigger")).findFirst().orElse(null); } @Test - void assertReportLocalProcessesWithNotCurrentInstance() { - subscriber.reportLocalProcesses(new ReportLocalProcessesEvent("bar_instance_id", "foo_task_id")); + void assertHandleWithInvalidShowProcessListTriggerEventKey() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/show_process_list_trigger/foo_instance_id", "", Type.DELETED)); + verify(ProcessOperationLockRegistry.getInstance(), times(0)).notify(any()); + } + + @Test + void assertHandleReportLocalProcessesWithNotCurrentInstance() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/show_process_list_trigger/bar_instance_id:foo_task_id", "", Type.ADDED)); verify(contextManager.getPersistServiceFacade().getRepository(), times(0)).delete(any()); } @Test - void assertReportLocalProcesses() { + void assertHandleReportLocalProcesses() { when(ProcessRegistry.getInstance().listAll()).thenReturn(Collections.emptyList()); - subscriber.reportLocalProcesses(new ReportLocalProcessesEvent("foo_instance_id", "foo_task_id")); + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/show_process_list_trigger/foo_instance_id:foo_task_id", "", Type.ADDED)); verify(contextManager.getPersistCoordinatorFacade().getProcessPersistCoordinator()).reportLocalProcesses("foo_instance_id", "foo_task_id"); } @Test - void assertCompleteToReportLocalProcesses() { - subscriber.completeToReportLocalProcesses(new ReportLocalProcessesCompletedEvent("foo_task_id")); + void assertHandleCompleteToReportLocalProcesses() { + handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/show_process_list_trigger/foo_instance_id:foo_task_id", "", Type.DELETED)); verify(ProcessOperationLockRegistry.getInstance()).notify("foo_task_id"); } - - @Test - void assertKillLocalProcessWithCurrentInstance() throws SQLException { - subscriber.killLocalProcess(new KillLocalProcessEvent("foo_instance_id", "foo_pid")); - verify(contextManager.getPersistCoordinatorFacade().getProcessPersistCoordinator()).cleanProcess("foo_instance_id", "foo_pid"); - } - - @Test - void assertKillLocalProcessWithNotCurrentInstance() throws SQLException { - subscriber.killLocalProcess(new KillLocalProcessEvent("bar_instance_id", "foo_pid")); - verify(contextManager.getPersistCoordinatorFacade().getProcessPersistCoordinator(), times(0)).cleanProcess("bar_instance_id", "foo_pid"); - } - - @Test - void assertCompleteToKillLocalProcess() { - subscriber.completeToKillLocalProcess(new KillLocalProcessCompletedEvent("foo_pid")); - verify(ProcessOperationLockRegistry.getInstance()).notify("foo_pid"); - } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistryTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistryTest.java index 7cf9672d944b8..bf682e541857c 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistryTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistryTest.java @@ -19,7 +19,7 @@ import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.DatabaseMetaDataChangedListener; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.GlobalMetaDataChangedListener; +import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.GlobalMetaDataChangedHandler; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import org.junit.jupiter.api.Test; @@ -44,6 +44,6 @@ void assertRegister() { DataChangedEventListenerRegistry registry = new DataChangedEventListenerRegistry(contextManager, Collections.singleton("foo_db")); registry.register(); verify(repository).watch(eq("/metadata/foo_db"), any(DatabaseMetaDataChangedListener.class)); - verify(repository, atLeastOnce()).watch(anyString(), any(GlobalMetaDataChangedListener.class)); + verify(repository, atLeastOnce()).watch(anyString(), any(GlobalMetaDataChangedHandler.class)); } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/CacheEvictedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/CacheEvictedSubscriberTest.java index ab788919b27e3..9a043c88824f9 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/CacheEvictedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/CacheEvictedSubscriberTest.java @@ -17,14 +17,15 @@ package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.infra.spi.type.ordered.cache.OrderedServicesCache; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.assisted.DropDatabaseListenerAssistedEvent; +import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; +import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; import org.junit.jupiter.api.Test; import java.util.Collections; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.mockito.Mockito.mock; class CacheEvictedSubscriberTest { @@ -33,7 +34,7 @@ void assertClearCache() { EventBusContext eventBusContext = new EventBusContext(); eventBusContext.register(new CacheEvictedSubscriber()); OrderedServicesCache.cacheServices(getClass(), Collections.emptyList(), Collections.emptyMap()); - eventBusContext.post(new DropDatabaseListenerAssistedEvent("db")); + eventBusContext.post(mock(DispatchEvent.class)); assertFalse(OrderedServicesCache.findCachedServices(getClass(), Collections.emptyList()).isPresent()); } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/DatabaseDataChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/DatabaseDataChangedSubscriberTest.java deleted file mode 100644 index b920fc57fee81..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/DatabaseDataChangedSubscriberTest.java +++ /dev/null @@ -1,94 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.DatabaseDataAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.DatabaseDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.SchemaDataAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.SchemaDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.ShardingSphereRowDataChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.ShardingSphereRowDataDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.data.TableDataChangedEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import static org.mockito.Mockito.verify; - -@ExtendWith(MockitoExtension.class) -class DatabaseDataChangedSubscriberTest { - - private DatabaseDataChangedSubscriber subscriber; - - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ContextManager contextManager; - - @BeforeEach - void setUp() { - subscriber = new DatabaseDataChangedSubscriber(contextManager); - } - - @Test - void assertRenewWithDatabaseDataAddedEvent() { - subscriber.renew(new DatabaseDataAddedEvent("foo_db")); - verify(contextManager.getMetaDataContextManager().getDatabaseManager()).addShardingSphereDatabaseData("foo_db"); - } - - @Test - void assertRenewWithDatabaseDataDeletedEvent() { - subscriber.renew(new DatabaseDataDeletedEvent("foo_db")); - verify(contextManager.getMetaDataContextManager().getDatabaseManager()).dropShardingSphereDatabaseData("foo_db"); - } - - @Test - void assertRenewWithSchemaDataAddedEvent() { - subscriber.renew(new SchemaDataAddedEvent("foo_db", "foo_schema")); - verify(contextManager.getMetaDataContextManager().getDatabaseManager()).addShardingSphereSchemaData("foo_db", "foo_schema"); - } - - @Test - void assertRenewWithSchemaDataDeletedEvent() { - subscriber.renew(new SchemaDataDeletedEvent("foo_db", "foo_schema")); - verify(contextManager.getMetaDataContextManager().getDatabaseManager()).dropShardingSphereSchemaData("foo_db", "foo_schema"); - } - - @Test - void assertRenewWithTableDataChangedEvent() { - subscriber.renew(new TableDataChangedEvent("foo_db", "foo_schema", "add_tbl", "del_tbl")); - verify(contextManager.getMetaDataContextManager().getDatabaseManager()).addShardingSphereTableData("foo_db", "foo_schema", "add_tbl"); - verify(contextManager.getMetaDataContextManager().getDatabaseManager()).dropShardingSphereTableData("foo_db", "foo_schema", "del_tbl"); - } - - @Test - void assertRenewWithShardingSphereRowDataChangedEvent() { - YamlShardingSphereRowData rowData = new YamlShardingSphereRowData(); - subscriber.renew(new ShardingSphereRowDataChangedEvent("foo_db", "foo_schema", "foo_tbl", rowData)); - verify(contextManager.getMetaDataContextManager().getDatabaseManager()).alterShardingSphereRowData("foo_db", "foo_schema", "foo_tbl", rowData); - } - - @Test - void assertRenewWithShardingSphereRowDataDeletedEvent() { - subscriber.renew(new ShardingSphereRowDataDeletedEvent("foo_db", "foo_schema", "foo_tbl", "id")); - verify(contextManager.getMetaDataContextManager().getDatabaseManager()).deleteShardingSphereRowData("foo_db", "foo_schema", "foo_tbl", "id"); - } -} From 115078e63d7ea997ecc448701ae74a0f17b1204d Mon Sep 17 00:00:00 2001 From: wangda <38549158+daziz@users.noreply.github.com> Date: Wed, 1 Jan 2025 01:16:59 +0800 Subject: [PATCH 02/10] Correcting spelling mistakes (#34214) --- ...ep_Dive_Into_Observability_Apache_ShardingSphere_Agent.en.md | 2 +- ...buted_Database_Performance_Record_with_10_Million_tpmC.en.md | 2 +- ...atabase_Plus_concept_and_what_challenges_can_it_solve.en.md" | 2 +- ..._2.0_Database_Governance_in_a_Cloud_Native_Environment.en.md | 2 +- ..._Apache_ShardingSphere_Shadow_Database_Feature_Upgrade.en.md | 2 +- .../content/material/Nov_23_1_Integrate_SCTL_into_RAL.en.md | 2 +- docs/document/content/overview/_index.en.md | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/blog/content/material/2022_03_25_A_Deep_Dive_Into_Observability_Apache_ShardingSphere_Agent.en.md b/docs/blog/content/material/2022_03_25_A_Deep_Dive_Into_Observability_Apache_ShardingSphere_Agent.en.md index 4bdf9595feebc..30ace29f17726 100644 --- a/docs/blog/content/material/2022_03_25_A_Deep_Dive_Into_Observability_Apache_ShardingSphere_Agent.en.md +++ b/docs/blog/content/material/2022_03_25_A_Deep_Dive_Into_Observability_Apache_ShardingSphere_Agent.en.md @@ -199,7 +199,7 @@ After performing the same access test as before, we can view dependencies throug ## Sampling Rate -The Observability plugin also enables users to set differnt sampling rate configured to suit different scenarios. Zipkin plugins support various sampling rate type configurations including const, counting, rate limiting, and boundary. +The Observability plugin also enables users to set different sampling rate configured to suit different scenarios. Zipkin plugins support various sampling rate type configurations including const, counting, rate limiting, and boundary. For scenarios with a high volume of requests, we suggest you to choose the boundary type and configure it with the appropriate sampling rate to reduce the collect volume of tracing data. diff --git a/docs/blog/content/material/2022_04_21_Apache_ShardingSphere_openGauss_Breaking_the_Distributed_Database_Performance_Record_with_10_Million_tpmC.en.md b/docs/blog/content/material/2022_04_21_Apache_ShardingSphere_openGauss_Breaking_the_Distributed_Database_Performance_Record_with_10_Million_tpmC.en.md index ff488479391f0..1705087cd9292 100644 --- a/docs/blog/content/material/2022_04_21_Apache_ShardingSphere_openGauss_Breaking_the_Distributed_Database_Performance_Record_with_10_Million_tpmC.en.md +++ b/docs/blog/content/material/2022_04_21_Apache_ShardingSphere_openGauss_Breaking_the_Distributed_Database_Performance_Record_with_10_Million_tpmC.en.md @@ -64,7 +64,7 @@ This means it can effectively solve problems caused by increasing data volume in ## Conclusion Apache ShardingSphere and openGauss can seek potential cooperation opportunities. -Considering the increasingly diversified applicaiton scenarios and increasing data volume, the requirements for database performance are at an all time high and will only continue to increase in the future. +Considering the increasingly diversified application scenarios and increasing data volume, the requirements for database performance are at an all time high and will only continue to increase in the future. The success of our two communities cooperation is just the beginning of ourtwo communities building a collaborative database ecosystem. diff --git "a/docs/blog/content/material/2022_05_10_What\342\200\231s_the_Database_Plus_concept_and_what_challenges_can_it_solve.en.md" "b/docs/blog/content/material/2022_05_10_What\342\200\231s_the_Database_Plus_concept_and_what_challenges_can_it_solve.en.md" index 206f8c0c5622f..f12bd198473bf 100644 --- "a/docs/blog/content/material/2022_05_10_What\342\200\231s_the_Database_Plus_concept_and_what_challenges_can_it_solve.en.md" +++ "b/docs/blog/content/material/2022_05_10_What\342\200\231s_the_Database_Plus_concept_and_what_challenges_can_it_solve.en.md" @@ -167,7 +167,7 @@ Apache ShardingSphere currently only supports federated queries between homogene ## Conclusion The Apache ShardingSphere community has been active in open source for 7 years. Through perseverance, the community has become mature we’d like to extend our sincere welcome to any devs or contributors who are enthusiastic about open source and coding to collaborate with us. -Among our recent achievements we’re particulary proud of, Apache ShardingSphere’s pluggable architecture and data sharding philosophy have been recognized by the academic community. [The paper, Apache ShardingSphere: A Holistic and Pluggable Platform for Data Sharding, has been published at this year’s ICDE, a top conference in the database field.](https://faun.pub/a-holistic-pluggable-platform-for-data-sharding-icde-2022-understanding-apache-shardingsphere-55779cfde16) +Among our recent achievements we’re particularly proud of, Apache ShardingSphere’s pluggable architecture and data sharding philosophy have been recognized by the academic community. [The paper, Apache ShardingSphere: A Holistic and Pluggable Platform for Data Sharding, has been published at this year’s ICDE, a top conference in the database field.](https://faun.pub/a-holistic-pluggable-platform-for-data-sharding-icde-2022-understanding-apache-shardingsphere-55779cfde16) ## Author diff --git a/docs/blog/content/material/2022_06_02_Database_Mesh_2.0_Database_Governance_in_a_Cloud_Native_Environment.en.md b/docs/blog/content/material/2022_06_02_Database_Mesh_2.0_Database_Governance_in_a_Cloud_Native_Environment.en.md index 81e66666ea83f..352a374a093d9 100644 --- a/docs/blog/content/material/2022_06_02_Database_Mesh_2.0_Database_Governance_in_a_Cloud_Native_Environment.en.md +++ b/docs/blog/content/material/2022_06_02_Database_Mesh_2.0_Database_Governance_in_a_Cloud_Native_Environment.en.md @@ -68,7 +68,7 @@ Database Mesh 2.0 focuses on how to achieve the following goals in a cloud nativ > **Developer experience** -As mentioned above, business developers are mainly concerned about business logic and implementation instead of infrastructure, operation and maintenance features. Developement experience will move towards [Serverless](https://www.redhat.com/en/topics/cloud-native-apps/what-is-serverless), which means it will become more and more transparent and intuitive when accessing databases. Developers only need to understand the type of data storage required by their business, and then use preset or dynamic ID credential information to access corresponding database services. +As mentioned above, business developers are mainly concerned about business logic and implementation instead of infrastructure, operation and maintenance features. Development experience will move towards [Serverless](https://www.redhat.com/en/topics/cloud-native-apps/what-is-serverless), which means it will become more and more transparent and intuitive when accessing databases. Developers only need to understand the type of data storage required by their business, and then use preset or dynamic ID credential information to access corresponding database services. > **Programmable** diff --git a/docs/blog/content/material/Jan_28_Full_Link_Online_Stress_Testing_for_Production_Database_Apache_ShardingSphere_Shadow_Database_Feature_Upgrade.en.md b/docs/blog/content/material/Jan_28_Full_Link_Online_Stress_Testing_for_Production_Database_Apache_ShardingSphere_Shadow_Database_Feature_Upgrade.en.md index d23c463396e5d..ec632464217ac 100644 --- a/docs/blog/content/material/Jan_28_Full_Link_Online_Stress_Testing_for_Production_Database_Apache_ShardingSphere_Shadow_Database_Feature_Upgrade.en.md +++ b/docs/blog/content/material/Jan_28_Full_Link_Online_Stress_Testing_for_Production_Database_Apache_ShardingSphere_Shadow_Database_Feature_Upgrade.en.md @@ -104,7 +104,7 @@ The data generated from test user order creation will be routed to shadow databa As mentioned in the introduction, full-link online stress testing is a complicated task that requires collaboration between microservices and middlewares to meet the needs of different traffic and stress testing tag transmissions. -Additonally, the testing service should be stateless and immediately available. [CyborgFlow](https://github.com/SphereEx/CyborgFlow), which is jointly maintained by Apache ShardingSphere, Apache APISIX and Apache SkyWalking provides out-of-the-box (OoTB) solution to run load test in your online system. +Additionally, the testing service should be stateless and immediately available. [CyborgFlow](https://github.com/SphereEx/CyborgFlow), which is jointly maintained by Apache ShardingSphere, Apache APISIX and Apache SkyWalking provides out-of-the-box (OoTB) solution to run load test in your online system. [Apache APISIX](https://apisix.apache.org/) is responsible for making tags on testing data at the gateway layer, while [Apache SkyWalking](https://skywalking.apache.org/) is responsible for transmission through the whole scheduling link, and finally, Apache ShardingSphere-Proxy will isolate data and route testing data to the shadow database. diff --git a/docs/blog/content/material/Nov_23_1_Integrate_SCTL_into_RAL.en.md b/docs/blog/content/material/Nov_23_1_Integrate_SCTL_into_RAL.en.md index 8553264ea08b4..a5a67f8fd22b1 100644 --- a/docs/blog/content/material/Nov_23_1_Integrate_SCTL_into_RAL.en.md +++ b/docs/blog/content/material/Nov_23_1_Integrate_SCTL_into_RAL.en.md @@ -189,7 +189,7 @@ Input command Output -a. If sucessful, show "Query OK, 0 rows affected"; +a. If successful, show "Query OK, 0 rows affected"; b. Re-execute `show readwrite_splitting hint status`; show the ource is changed into Write; c. Execute `preview select * from t_order`and see the queried SQL will go to the master database. diff --git a/docs/document/content/overview/_index.en.md b/docs/document/content/overview/_index.en.md index 54cdf33dc01dd..b9dedc131f3c5 100644 --- a/docs/document/content/overview/_index.en.md +++ b/docs/document/content/overview/_index.en.md @@ -65,7 +65,7 @@ ShardingSphere offers a flat learning curve to DBAs and is interaction-friendly It can provide enhancement capability based on mature databases while ensuring security and stability. -- Elastic Extention +- Elastic Extension It supports computing, storage, and smooth online expansion, which can meet diverse business needs. From af462985316617e295769694e98a99ccc11de71f Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 1 Jan 2025 01:17:37 +0800 Subject: [PATCH 03/10] Refactor KillProcessHandlerTest (#34215) --- .../event/dispatch/handler/type/KillProcessHandlerTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java index 990844db7916c..a9e7a7936c662 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java @@ -33,8 +33,6 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import java.sql.SQLException; - import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -58,7 +56,7 @@ void setUp() { } @Test - void assertHandleWithInvalidKillProcessListTriggerEventKey() throws SQLException { + void assertHandleWithInvalidKillProcessListTriggerEventKey() { handler.handle(contextManager, new DataChangedEvent("/nodes/compute_nodes/kill_process_trigger/foo_instance_id", "", Type.DELETED)); verify(ProcessOperationLockRegistry.getInstance(), times(0)).notify(any()); } From 9d5fc4d2a9df95caf6954ed6e73aab15a78a51bb Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 1 Jan 2025 01:56:18 +0800 Subject: [PATCH 04/10] Refactor ListenerAssistedChangedHandlerTest (#34216) --- .../handler/type/ListenerAssistedChangedHandlerTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandlerTest.java index f227c6c841108..a97ed45b7d667 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandlerTest.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationProperties; import org.apache.shardingsphere.infra.instance.metadata.InstanceType; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.mode.event.DataChangedEvent; @@ -31,6 +32,8 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Properties; + import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.times; @@ -51,6 +54,7 @@ class ListenerAssistedChangedHandlerTest { @BeforeEach void setUp() { when(contextManager.getPersistServiceFacade().getRepository()).thenReturn(repository); + when(contextManager.getMetaDataContexts().getMetaData().getTemporaryProps()).thenReturn(new TemporaryConfigurationProperties(new Properties())); handler = ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).stream() .filter(each -> each.getSubscribedKey().equals("/states/listener_assisted")).findFirst().orElse(null); } From 9938d48ea1df04d6e9953432b80637775d388659 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 1 Jan 2025 15:00:13 +0800 Subject: [PATCH 05/10] Remove DispatchEventBuilder (#34217) * Remove DispatchEventBuilder * Remove DispatchEventBuilder --- .../builder/DispatchEventBuilder.java | 56 --------------- .../DataChangedEventListenerRegistry.java | 7 -- .../type/GlobalMetaDataChangedListener.java | 42 ----------- ...vent.dispatch.builder.DispatchEventBuilder | 16 ----- .../GlobalMetaDataChangedListenerTest.java | 70 ------------------- 5 files changed, 191 deletions(-) delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/DispatchEventBuilder.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java delete mode 100644 mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListenerTest.java diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/DispatchEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/DispatchEventBuilder.java deleted file mode 100644 index 1ea4ab224ae46..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/DispatchEventBuilder.java +++ /dev/null @@ -1,56 +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.mode.manager.cluster.event.dispatch.builder; - -import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; - -import java.util.Collection; -import java.util.Optional; - -/** - * Dispatch event builder. - * - * @param type of event - */ -@SingletonSPI -public interface DispatchEventBuilder { - - /** - * Get subscribed key. - * - * @return subscribed key - */ - String getSubscribedKey(); - - /** - * Get subscribed types. - * - * @return subscribed types - */ - Collection getSubscribedTypes(); - - /** - * Create dispatch event. - * - * @param event registry center data changed event - * @return dispatch event - */ - Optional build(DataChangedEvent event); -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java index 9122a5006c93f..052a89e143473 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java @@ -21,11 +21,9 @@ import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.DatabaseMetaDataChangedListener; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.GlobalMetaDataChangedHandler; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type.GlobalMetaDataChangedListener; import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository; import java.util.Collection; @@ -55,7 +53,6 @@ public DataChangedEventListenerRegistry(final ContextManager contextManager, fin */ public void register() { databaseNames.forEach(this::registerDatabaseListeners); - ShardingSphereServiceLoader.getServiceInstances(DispatchEventBuilder.class).forEach(this::registerGlobalListeners); ShardingSphereServiceLoader.getServiceInstances(DataChangedEventHandler.class).forEach(this::registerGlobalHandlers); } @@ -63,10 +60,6 @@ private void registerDatabaseListeners(final String databaseName) { repository.watch(DatabaseMetaDataNode.getDatabaseNamePath(databaseName), new DatabaseMetaDataChangedListener(eventBusContext)); } - private void registerGlobalListeners(final DispatchEventBuilder builder) { - repository.watch(builder.getSubscribedKey(), new GlobalMetaDataChangedListener(eventBusContext, builder)); - } - private void registerGlobalHandlers(final DataChangedEventHandler handler) { repository.watch(handler.getSubscribedKey(), new GlobalMetaDataChangedHandler(contextManager, handler)); } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java deleted file mode 100644 index cbdc96d6a0db7..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListener.java +++ /dev/null @@ -1,42 +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.mode.manager.cluster.event.dispatch.listener.type; - -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; -import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEventListener; - -/** - * Global meta data changed listener. - */ -@RequiredArgsConstructor -public final class GlobalMetaDataChangedListener implements DataChangedEventListener { - - private final EventBusContext eventBusContext; - - private final DispatchEventBuilder eventBuilder; - - @Override - public void onChange(final DataChangedEvent event) { - if (eventBuilder.getSubscribedTypes().contains(event.getType())) { - eventBuilder.build(event).ifPresent(eventBusContext::post); - } - } -} diff --git a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder deleted file mode 100644 index b1312a0905c3c..0000000000000 --- a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder +++ /dev/null @@ -1,16 +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. -# diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListenerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListenerTest.java deleted file mode 100644 index 14ea417fa5771..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/GlobalMetaDataChangedListenerTest.java +++ /dev/null @@ -1,70 +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.mode.manager.cluster.event.dispatch.listener.type; - -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; -import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.DispatchEventBuilder; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.util.Collections; -import java.util.Optional; - -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 GlobalMetaDataChangedListenerTest { - - private GlobalMetaDataChangedListener listener; - - @Mock - private EventBusContext eventBusContext; - - @Mock - private DispatchEventBuilder builder; - - @BeforeEach - void setUp() { - listener = new GlobalMetaDataChangedListener(eventBusContext, builder); - when(builder.getSubscribedTypes()).thenReturn(Collections.singleton(Type.ADDED)); - } - - @Test - void assertOnChangeWithUnsupportedType() { - DataChangedEvent event = new DataChangedEvent("key", "value", Type.DELETED); - listener.onChange(event); - verify(builder, times(0)).build(event); - } - - @Test - void assertOnChangeWithSupportedType() { - DataChangedEvent event = new DataChangedEvent("key", "value", Type.ADDED); - Object builtEvent = mock(Object.class); - when(builder.build(event)).thenReturn(Optional.of(builtEvent)); - listener.onChange(event); - verify(eventBusContext).post(builtEvent); - } -} From 2122b153c07c64e9744e070a51c16d84088e86c6 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 1 Jan 2025 17:05:34 +0800 Subject: [PATCH 06/10] Add event.dispatch.handler.global package (#34218) --- .../ClusterStateChangedHandler.java | 2 +- .../ComputeNodeOnlineHandler.java | 2 +- .../ComputeNodeStateChangedHandler.java | 2 +- .../GlobalRuleChangedHandler.java | 2 +- .../{type => global}/KillProcessHandler.java | 2 +- .../ListenerAssistedChangedHandler.java | 2 +- .../PropertiesChangedHandler.java | 2 +- .../QualifiedDataSourceChangedHandler.java | 2 +- .../ShardingSphereDataChangedHandler.java | 2 +- .../ShowProcessListHandler.java | 2 +- ...t.dispatch.handler.DataChangedEventHandler | 20 +++++++++---------- .../ClusterStateChangedHandlerTest.java | 2 +- .../ComputeNodeOnlineHandlerTest.java | 2 +- .../ComputeNodeStateChangedHandlerTest.java | 2 +- .../GlobalRuleChangedHandlerTest.java | 2 +- .../KillProcessHandlerTest.java | 2 +- .../ListenerAssistedChangedHandlerTest.java | 2 +- .../PropertiesChangedHandlerTest.java | 2 +- ...QualifiedDataSourceChangedHandlerTest.java | 2 +- .../ShardingSphereDataChangedHandlerTest.java | 2 +- .../ShowProcessListHandlerTest.java | 2 +- 21 files changed, 30 insertions(+), 30 deletions(-) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ClusterStateChangedHandler.java (98%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ComputeNodeOnlineHandler.java (99%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ComputeNodeStateChangedHandler.java (99%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/GlobalRuleChangedHandler.java (99%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/KillProcessHandler.java (99%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ListenerAssistedChangedHandler.java (99%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/PropertiesChangedHandler.java (99%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/QualifiedDataSourceChangedHandler.java (99%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ShardingSphereDataChangedHandler.java (99%) rename mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ShowProcessListHandler.java (99%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ClusterStateChangedHandlerTest.java (99%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ComputeNodeOnlineHandlerTest.java (99%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ComputeNodeStateChangedHandlerTest.java (99%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/GlobalRuleChangedHandlerTest.java (99%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/KillProcessHandlerTest.java (99%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ListenerAssistedChangedHandlerTest.java (99%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/PropertiesChangedHandlerTest.java (99%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/QualifiedDataSourceChangedHandlerTest.java (99%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ShardingSphereDataChangedHandlerTest.java (99%) rename mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/{type => global}/ShowProcessListHandlerTest.java (99%) diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ClusterStateChangedHandler.java similarity index 98% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandler.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ClusterStateChangedHandler.java index 5117361c659c7..11fb4ec40796f 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ClusterStateChangedHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.metadata.persist.node.StatesNode; import org.apache.shardingsphere.mode.event.DataChangedEvent; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeOnlineHandler.java similarity index 99% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandler.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeOnlineHandler.java index e60cc13c2959a..e69ed412ea28c 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeOnlineHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.instance.ComputeNodeData; import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeStateChangedHandler.java similarity index 99% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandler.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeStateChangedHandler.java index 7df07f90e1c0e..960d01cd26080 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeStateChangedHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import com.google.common.base.Strings; import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/GlobalRuleChangedHandler.java similarity index 99% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandler.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/GlobalRuleChangedHandler.java index f950991266d24..1d6679d9c3448 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/GlobalRuleChangedHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import com.google.common.base.Preconditions; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/KillProcessHandler.java similarity index 99% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandler.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/KillProcessHandler.java index 08e1ecb0ecd91..b4b0a7d881e6a 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/KillProcessHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException; import org.apache.shardingsphere.infra.executor.sql.process.ProcessRegistry; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ListenerAssistedChangedHandler.java similarity index 99% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandler.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ListenerAssistedChangedHandler.java index 19fdf3f7f9aee..b82eaf814fd19 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ListenerAssistedChangedHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.instance.metadata.InstanceType; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/PropertiesChangedHandler.java similarity index 99% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandler.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/PropertiesChangedHandler.java index bc147b22e327e..280302582a1ae 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/PropertiesChangedHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import com.google.common.base.Preconditions; import org.apache.shardingsphere.metadata.persist.node.GlobalNode; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/QualifiedDataSourceChangedHandler.java similarity index 99% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandler.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/QualifiedDataSourceChangedHandler.java index 30b7a1af79868..de544fa19c01e 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/QualifiedDataSourceChangedHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import com.google.common.base.Preconditions; import com.google.common.base.Strings; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShardingSphereDataChangedHandler.java similarity index 99% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandler.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShardingSphereDataChangedHandler.java index a6f42a53f37cc..31b2dc1ba6fbc 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShardingSphereDataChangedHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import com.google.common.base.Strings; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShowProcessListHandler.java similarity index 99% rename from mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandler.java rename to mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShowProcessListHandler.java index f9d98414be2e2..a6403476e64f3 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShowProcessListHandler.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.executor.sql.process.lock.ProcessOperationLockRegistry; import org.apache.shardingsphere.metadata.persist.node.ComputeNode; diff --git a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler index e21cc73a2ad4b..185a6e24dd7fd 100644 --- a/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler +++ b/mode/type/cluster/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler @@ -15,13 +15,13 @@ # limitations under the License. # -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ClusterStateChangedHandler -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.QualifiedDataSourceChangedHandler -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ComputeNodeOnlineHandler -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ComputeNodeStateChangedHandler -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ShardingSphereDataChangedHandler -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.GlobalRuleChangedHandler -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.PropertiesChangedHandler -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ListenerAssistedChangedHandler -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.ShowProcessListHandler -org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type.KillProcessHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global.ClusterStateChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global.QualifiedDataSourceChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global.ComputeNodeOnlineHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global.ComputeNodeStateChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global.ShardingSphereDataChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global.GlobalRuleChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global.PropertiesChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global.ListenerAssistedChangedHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global.ShowProcessListHandler +org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global.KillProcessHandler diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ClusterStateChangedHandlerTest.java similarity index 99% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandlerTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ClusterStateChangedHandlerTest.java index b8fc6a033ad41..3bedc7fb98167 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ClusterStateChangedHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ClusterStateChangedHandlerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.mode.event.DataChangedEvent; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeOnlineHandlerTest.java similarity index 99% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandlerTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeOnlineHandlerTest.java index 43f44ee288f64..7c0c8f8a69e04 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeOnlineHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeOnlineHandlerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeStateChangedHandlerTest.java similarity index 99% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandlerTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeStateChangedHandlerTest.java index c9c66df4ef6e4..35181f9dcd9a3 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ComputeNodeStateChangedHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ComputeNodeStateChangedHandlerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.mode.event.DataChangedEvent; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/GlobalRuleChangedHandlerTest.java similarity index 99% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandlerTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/GlobalRuleChangedHandlerTest.java index 83086a2e620fc..653aefe911c41 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/GlobalRuleChangedHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/GlobalRuleChangedHandlerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/KillProcessHandlerTest.java similarity index 99% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/KillProcessHandlerTest.java index a9e7a7936c662..fa72d3907398c 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/KillProcessHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/KillProcessHandlerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.executor.sql.process.lock.ProcessOperationLockRegistry; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ListenerAssistedChangedHandlerTest.java similarity index 99% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandlerTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ListenerAssistedChangedHandlerTest.java index a97ed45b7d667..3f6e12181e8ad 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ListenerAssistedChangedHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ListenerAssistedChangedHandlerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationProperties; import org.apache.shardingsphere.infra.instance.metadata.InstanceType; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/PropertiesChangedHandlerTest.java similarity index 99% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandlerTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/PropertiesChangedHandlerTest.java index d251a203780be..bbcffe435b6a1 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/PropertiesChangedHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/PropertiesChangedHandlerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.mode.event.DataChangedEvent; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/QualifiedDataSourceChangedHandlerTest.java similarity index 99% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandlerTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/QualifiedDataSourceChangedHandlerTest.java index cedf77812c457..afe17c85cd840 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/QualifiedDataSourceChangedHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/QualifiedDataSourceChangedHandlerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource; import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShardingSphereDataChangedHandlerTest.java similarity index 99% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandlerTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShardingSphereDataChangedHandlerTest.java index 909b60f876b20..c1077743cc22f 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShardingSphereDataChangedHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShardingSphereDataChangedHandlerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData; diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShowProcessListHandlerTest.java similarity index 99% rename from mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandlerTest.java rename to mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShowProcessListHandlerTest.java index 44c7baeed7ec2..b8eb3518a0332 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/type/ShowProcessListHandlerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ShowProcessListHandlerTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.type; +package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.global; import org.apache.shardingsphere.infra.executor.sql.process.ProcessRegistry; import org.apache.shardingsphere.infra.executor.sql.process.lock.ProcessOperationLockRegistry; From 736ca922dea579093f8bc8392e8b5e350dd70378 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 1 Jan 2025 22:48:43 +0800 Subject: [PATCH 07/10] Use MetaDataChangedHandler to instead of MetaDataChangedSubscriber (#34219) * Use MetaDataChangedHandler to instead of MetaDataChangedSubscriber --- ...ShardingSphereStatisticsRefreshEngine.java | 5 +- .../builder/MetaDataChangedEventBuilder.java | 160 ------------------ .../metadata/schema/SchemaAddedEvent.java | 34 ---- .../metadata/schema/SchemaDeletedEvent.java | 34 ---- .../table/TableCreatedOrAlteredEvent.java | 40 ----- .../schema/table/TableDroppedEvent.java | 36 ---- .../view/ViewCreatedOrAlteredEvent.java | 40 ----- .../schema/view/ViewDroppedEvent.java | 40 ----- .../database/MetaDataChangedHandler.java | 154 +++++++++++++++++ .../database/SchemaChangedHandler.java | 58 +++++++ .../database/StorageNodeChangedHandler.java | 63 +++++++ .../database/StorageUnitChangedHandler.java | 76 +++++++++ .../handler/database/TableChangedHandler.java | 70 ++++++++ .../handler/database/ViewChangedHandler.java | 70 ++++++++ .../ListenerAssistedChangedHandler.java | 2 +- .../DataChangedEventListenerRegistry.java | 2 +- .../type/DatabaseMetaDataChangedListener.java | 15 +- ...lusterDispatchEventSubscriberRegistry.java | 7 +- .../type/MetaDataChangedSubscriber.java | 126 -------------- .../MetaDataChangedEventBuilderTest.java | 98 ----------- .../database/MetaDataChangedHandlerTest.java | 148 ++++++++++++++++ .../DatabaseMetaDataChangedListenerTest.java | 15 +- .../type/MetaDataChangedSubscriberTest.java | 105 ------------ .../type/StorageUnitEventSubscriberTest.java | 73 -------- 24 files changed, 663 insertions(+), 808 deletions(-) delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/MetaDataChangedEventBuilder.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/SchemaAddedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/SchemaDeletedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/table/TableCreatedOrAlteredEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/table/TableDroppedEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/view/ViewCreatedOrAlteredEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/view/ViewDroppedEvent.java create mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/MetaDataChangedHandler.java create mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/SchemaChangedHandler.java create mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/StorageNodeChangedHandler.java create mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/StorageUnitChangedHandler.java create mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/TableChangedHandler.java create mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/ViewChangedHandler.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/MetaDataChangedSubscriber.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/MetaDataChangedEventBuilderTest.java create mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/MetaDataChangedHandlerTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/MetaDataChangedSubscriberTest.java delete mode 100644 mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StorageUnitEventSubscriberTest.java diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java index 073be7e5a019b..bfad2f96fc728 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/ShardingSphereStatisticsRefreshEngine.java @@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey; import org.apache.shardingsphere.infra.executor.kernel.thread.ExecutorThreadFactoryBuilder; +import org.apache.shardingsphere.infra.instance.metadata.InstanceType; import org.apache.shardingsphere.infra.lock.LockContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -68,7 +69,9 @@ public ShardingSphereStatisticsRefreshEngine(final ContextManager contextManager * Async refresh. */ public void asyncRefresh() { - EXECUTOR_SERVICE.execute(this::refresh); + if (InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) { + EXECUTOR_SERVICE.execute(this::refresh); + } } /** diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/MetaDataChangedEventBuilder.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/MetaDataChangedEventBuilder.java deleted file mode 100644 index 44621086837c7..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/MetaDataChangedEventBuilder.java +++ /dev/null @@ -1,160 +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.mode.manager.cluster.event.dispatch.builder; - -import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; -import org.apache.shardingsphere.metadata.persist.node.metadata.DataSourceMetaDataNode; -import org.apache.shardingsphere.metadata.persist.node.metadata.TableMetaDataNode; -import org.apache.shardingsphere.metadata.persist.node.metadata.ViewMetaDataNode; -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.node.StorageNodeAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.node.StorageNodeRegisteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.node.StorageNodeUnregisteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitRegisteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitUnregisteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.SchemaAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.SchemaDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.table.TableCreatedOrAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.table.TableDroppedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.view.ViewCreatedOrAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.view.ViewDroppedEvent; - -import java.util.Optional; - -/** - * Meta data changed event builder. - */ -public final class MetaDataChangedEventBuilder { - - /** - * Build meta data changed event. - * - * @param databaseName database name - * @param event data changed event - * @return built event - */ - public Optional build(final String databaseName, final DataChangedEvent event) { - String key = event.getKey(); - Optional schemaName = DatabaseMetaDataNode.getSchemaName(key); - if (schemaName.isPresent()) { - return buildSchemaChangedEvent(databaseName, schemaName.get(), event); - } - schemaName = DatabaseMetaDataNode.getSchemaNameByTableNode(key); - if (schemaName.isPresent() && isTableMetaDataChanged(event.getKey())) { - return buildTableChangedEvent(databaseName, schemaName.get(), event); - } - if (schemaName.isPresent() && isViewMetaDataChanged(event.getKey())) { - return buildViewChangedEvent(databaseName, schemaName.get(), event); - } - if (DataSourceMetaDataNode.isDataSourcesNode(key)) { - return buildDataSourceChangedEvent(databaseName, event); - } - return Optional.empty(); - } - - private Optional buildSchemaChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { - switch (event.getType()) { - case ADDED: - case UPDATED: - return Optional.of(new SchemaAddedEvent(databaseName, schemaName)); - case DELETED: - return Optional.of(new SchemaDeletedEvent(databaseName, schemaName)); - default: - return Optional.empty(); - } - } - - private boolean isTableMetaDataChanged(final String key) { - return TableMetaDataNode.isTableActiveVersionNode(key) || TableMetaDataNode.isTableNode(key); - } - - private Optional buildTableChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { - if ((Type.ADDED == event.getType() || Type.UPDATED == event.getType()) && TableMetaDataNode.isTableActiveVersionNode(event.getKey())) { - String tableName = TableMetaDataNode.getTableNameByActiveVersionNode(event.getKey()).orElseThrow(() -> new IllegalStateException("Table name not found.")); - return Optional.of(new TableCreatedOrAlteredEvent(databaseName, schemaName, tableName, event.getKey(), event.getValue())); - } - if (Type.DELETED == event.getType() && TableMetaDataNode.isTableNode(event.getKey())) { - String tableName = TableMetaDataNode.getTableName(event.getKey()).orElseThrow(() -> new IllegalStateException("Table name not found.")); - return Optional.of(new TableDroppedEvent(databaseName, schemaName, tableName)); - } - return Optional.empty(); - } - - private boolean isViewMetaDataChanged(final String key) { - return ViewMetaDataNode.isViewActiveVersionNode(key) || ViewMetaDataNode.isViewNode(key); - } - - private Optional buildViewChangedEvent(final String databaseName, final String schemaName, final DataChangedEvent event) { - if ((Type.ADDED == event.getType() || Type.UPDATED == event.getType()) && ViewMetaDataNode.isViewActiveVersionNode(event.getKey())) { - String viewName = ViewMetaDataNode.getViewNameByActiveVersionNode(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found.")); - return Optional.of(new ViewCreatedOrAlteredEvent(databaseName, schemaName, viewName, event.getKey(), event.getValue())); - } - if (Type.DELETED == event.getType() && ViewMetaDataNode.isViewNode(event.getKey())) { - String viewName = ViewMetaDataNode.getViewName(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found.")); - return Optional.of(new ViewDroppedEvent(databaseName, schemaName, viewName, event.getKey(), event.getValue())); - } - return Optional.empty(); - } - - private Optional buildDataSourceChangedEvent(final String databaseName, final DataChangedEvent event) { - if (DataSourceMetaDataNode.isDataSourceUnitActiveVersionNode(event.getKey()) || DataSourceMetaDataNode.isDataSourceUnitNode(event.getKey())) { - return buildStorageUnitChangedEvent(databaseName, event); - } - if (DataSourceMetaDataNode.isDataSourceNodeActiveVersionNode(event.getKey()) || DataSourceMetaDataNode.isDataSourceNodeNode(event.getKey())) { - return buildStorageNodeChangedEvent(databaseName, event); - } - return Optional.empty(); - } - - private Optional buildStorageUnitChangedEvent(final String databaseName, final DataChangedEvent event) { - Optional dataSourceUnitName = DataSourceMetaDataNode.getDataSourceNameByDataSourceUnitActiveVersionNode(event.getKey()); - if (dataSourceUnitName.isPresent()) { - if (Type.ADDED == event.getType()) { - return Optional.of(new StorageUnitRegisteredEvent(databaseName, dataSourceUnitName.get(), event.getKey(), event.getValue())); - } - if (Type.UPDATED == event.getType()) { - return Optional.of(new StorageUnitAlteredEvent(databaseName, dataSourceUnitName.get(), event.getKey(), event.getValue())); - } - } - dataSourceUnitName = DataSourceMetaDataNode.getDataSourceNameByDataSourceUnitNode(event.getKey()); - if (Type.DELETED == event.getType() && dataSourceUnitName.isPresent()) { - return Optional.of(new StorageUnitUnregisteredEvent(databaseName, dataSourceUnitName.get())); - } - return Optional.empty(); - } - - private Optional buildStorageNodeChangedEvent(final String databaseName, final DataChangedEvent event) { - Optional dataSourceNodeName = DataSourceMetaDataNode.getDataSourceNameByDataSourceNodeActiveVersionNode(event.getKey()); - if (dataSourceNodeName.isPresent()) { - if (Type.ADDED == event.getType()) { - return Optional.of(new StorageNodeRegisteredEvent(databaseName, dataSourceNodeName.get(), event.getKey(), event.getValue())); - } - if (Type.UPDATED == event.getType()) { - return Optional.of(new StorageNodeAlteredEvent(databaseName, dataSourceNodeName.get(), event.getKey(), event.getValue())); - } - } - dataSourceNodeName = DataSourceMetaDataNode.getDataSourceNameByDataSourceNodeNode(event.getKey()); - if (Type.DELETED == event.getType() && dataSourceNodeName.isPresent()) { - return Optional.of(new StorageNodeUnregisteredEvent(databaseName, dataSourceNodeName.get())); - } - return Optional.empty(); - } -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/SchemaAddedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/SchemaAddedEvent.java deleted file mode 100644 index 79a9259687ae3..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/SchemaAddedEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.metadata.schema; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Schema added event. - */ -@RequiredArgsConstructor -@Getter -public final class SchemaAddedEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/SchemaDeletedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/SchemaDeletedEvent.java deleted file mode 100644 index 72efaa3eeee46..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/SchemaDeletedEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.metadata.schema; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Schema deleted event. - */ -@RequiredArgsConstructor -@Getter -public final class SchemaDeletedEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/table/TableCreatedOrAlteredEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/table/TableCreatedOrAlteredEvent.java deleted file mode 100644 index bc72d30cd9c1d..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/table/TableCreatedOrAlteredEvent.java +++ /dev/null @@ -1,40 +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.mode.manager.cluster.event.dispatch.event.metadata.schema.table; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Table created or altered event. - */ -@RequiredArgsConstructor -@Getter -public final class TableCreatedOrAlteredEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; - - private final String tableName; - - private final String activeVersionKey; - - private final String activeVersion; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/table/TableDroppedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/table/TableDroppedEvent.java deleted file mode 100644 index f8aca800dbab1..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/table/TableDroppedEvent.java +++ /dev/null @@ -1,36 +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.mode.manager.cluster.event.dispatch.event.metadata.schema.table; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Table dropped event. - */ -@RequiredArgsConstructor -@Getter -public final class TableDroppedEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; - - private final String tableName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/view/ViewCreatedOrAlteredEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/view/ViewCreatedOrAlteredEvent.java deleted file mode 100644 index 6357a4612e3e5..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/view/ViewCreatedOrAlteredEvent.java +++ /dev/null @@ -1,40 +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.mode.manager.cluster.event.dispatch.event.metadata.schema.view; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * View created or altered event. - */ -@RequiredArgsConstructor -@Getter -public final class ViewCreatedOrAlteredEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; - - private final String viewName; - - private final String activeVersionKey; - - private final String activeVersion; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/view/ViewDroppedEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/view/ViewDroppedEvent.java deleted file mode 100644 index 0e5984c5636ff..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/metadata/schema/view/ViewDroppedEvent.java +++ /dev/null @@ -1,40 +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.mode.manager.cluster.event.dispatch.event.metadata.schema.view; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * View dropped event. - */ -@RequiredArgsConstructor -@Getter -public final class ViewDroppedEvent implements DispatchEvent { - - private final String databaseName; - - private final String schemaName; - - private final String viewName; - - private final String activeVersionKey; - - private final String activeVersion; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/MetaDataChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/MetaDataChangedHandler.java new file mode 100644 index 0000000000000..45b3ae2c54a66 --- /dev/null +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/MetaDataChangedHandler.java @@ -0,0 +1,154 @@ +/* + * 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.dispatch.handler.database; + +import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; +import org.apache.shardingsphere.metadata.persist.node.metadata.DataSourceMetaDataNode; +import org.apache.shardingsphere.metadata.persist.node.metadata.TableMetaDataNode; +import org.apache.shardingsphere.metadata.persist.node.metadata.ViewMetaDataNode; +import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; +import org.apache.shardingsphere.mode.manager.ContextManager; + +import java.util.Optional; + +/** + * Meta data changed handler. + */ +public final class MetaDataChangedHandler { + + private final SchemaChangedHandler schemaChangedHandler; + + private final TableChangedHandler tableChangedHandler; + + private final ViewChangedHandler viewChangedHandler; + + private final StorageUnitChangedHandler storageUnitChangedHandler; + + private final StorageNodeChangedHandler storageNodeChangedHandler; + + public MetaDataChangedHandler(final ContextManager contextManager) { + schemaChangedHandler = new SchemaChangedHandler(contextManager); + tableChangedHandler = new TableChangedHandler(contextManager); + viewChangedHandler = new ViewChangedHandler(contextManager); + storageUnitChangedHandler = new StorageUnitChangedHandler(contextManager); + storageNodeChangedHandler = new StorageNodeChangedHandler(contextManager); + } + + /** + * Handle meta data changed. + * + * @param databaseName database name + * @param event data changed event + * @return handle completed or not + */ + public boolean handle(final String databaseName, final DataChangedEvent event) { + String eventKey = event.getKey(); + Optional schemaName = DatabaseMetaDataNode.getSchemaName(eventKey); + if (schemaName.isPresent()) { + handleSchemaChanged(databaseName, schemaName.get(), event); + return true; + } + schemaName = DatabaseMetaDataNode.getSchemaNameByTableNode(eventKey); + if (schemaName.isPresent() && isTableMetaDataChanged(eventKey)) { + handleTableChanged(databaseName, schemaName.get(), event); + return true; + } + if (schemaName.isPresent() && isViewMetaDataChanged(eventKey)) { + handleViewChanged(databaseName, schemaName.get(), event); + return true; + } + if (DataSourceMetaDataNode.isDataSourcesNode(eventKey)) { + handleDataSourceChanged(databaseName, event); + return true; + } + return false; + } + + private void handleSchemaChanged(final String databaseName, final String schemaName, final DataChangedEvent event) { + if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) { + schemaChangedHandler.handleCreated(databaseName, schemaName); + } else if (Type.DELETED == event.getType()) { + schemaChangedHandler.handleDropped(databaseName, schemaName); + } + } + + private boolean isTableMetaDataChanged(final String key) { + return TableMetaDataNode.isTableActiveVersionNode(key) || TableMetaDataNode.isTableNode(key); + } + + private void handleTableChanged(final String databaseName, final String schemaName, final DataChangedEvent event) { + if ((Type.ADDED == event.getType() || Type.UPDATED == event.getType()) && TableMetaDataNode.isTableActiveVersionNode(event.getKey())) { + tableChangedHandler.handleCreatedOrAltered(databaseName, schemaName, event); + } else if (Type.DELETED == event.getType() && TableMetaDataNode.isTableNode(event.getKey())) { + tableChangedHandler.handleDropped(databaseName, schemaName, event); + } + } + + private boolean isViewMetaDataChanged(final String key) { + return ViewMetaDataNode.isViewActiveVersionNode(key) || ViewMetaDataNode.isViewNode(key); + } + + private void handleViewChanged(final String databaseName, final String schemaName, final DataChangedEvent event) { + if ((Type.ADDED == event.getType() || Type.UPDATED == event.getType()) && ViewMetaDataNode.isViewActiveVersionNode(event.getKey())) { + viewChangedHandler.handleCreatedOrAltered(databaseName, schemaName, event); + } else if (Type.DELETED == event.getType() && ViewMetaDataNode.isViewNode(event.getKey())) { + viewChangedHandler.handleDropped(databaseName, schemaName, event); + } + } + + private void handleDataSourceChanged(final String databaseName, final DataChangedEvent event) { + if (DataSourceMetaDataNode.isDataSourceUnitActiveVersionNode(event.getKey()) || DataSourceMetaDataNode.isDataSourceUnitNode(event.getKey())) { + handleStorageUnitChanged(databaseName, event); + } else if (DataSourceMetaDataNode.isDataSourceNodeActiveVersionNode(event.getKey()) || DataSourceMetaDataNode.isDataSourceNodeNode(event.getKey())) { + handleStorageNodeChanged(databaseName, event); + } + } + + private void handleStorageUnitChanged(final String databaseName, final DataChangedEvent event) { + Optional dataSourceUnitName = DataSourceMetaDataNode.getDataSourceNameByDataSourceUnitActiveVersionNode(event.getKey()); + if (dataSourceUnitName.isPresent()) { + if (Type.ADDED == event.getType()) { + storageUnitChangedHandler.handleRegistered(databaseName, dataSourceUnitName.get(), event); + } else if (Type.UPDATED == event.getType()) { + storageUnitChangedHandler.handleAltered(databaseName, dataSourceUnitName.get(), event); + } + return; + } + dataSourceUnitName = DataSourceMetaDataNode.getDataSourceNameByDataSourceUnitNode(event.getKey()); + if (Type.DELETED == event.getType() && dataSourceUnitName.isPresent()) { + storageUnitChangedHandler.handleUnregistered(databaseName, dataSourceUnitName.get()); + } + } + + private void handleStorageNodeChanged(final String databaseName, final DataChangedEvent event) { + Optional dataSourceNodeName = DataSourceMetaDataNode.getDataSourceNameByDataSourceNodeActiveVersionNode(event.getKey()); + if (dataSourceNodeName.isPresent()) { + if (Type.ADDED == event.getType()) { + storageNodeChangedHandler.handleRegistered(databaseName, dataSourceNodeName.get(), event); + } else if (Type.UPDATED == event.getType()) { + storageNodeChangedHandler.handleAltered(databaseName, dataSourceNodeName.get(), event); + } + return; + } + dataSourceNodeName = DataSourceMetaDataNode.getDataSourceNameByDataSourceNodeNode(event.getKey()); + if (Type.DELETED == event.getType() && dataSourceNodeName.isPresent()) { + storageNodeChangedHandler.handleUnregistered(databaseName, dataSourceNodeName.get()); + } + } +} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/SchemaChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/SchemaChangedHandler.java new file mode 100644 index 0000000000000..e06af0c2afbd4 --- /dev/null +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/SchemaChangedHandler.java @@ -0,0 +1,58 @@ +/* + * 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.dispatch.handler.database; + +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; + +/** + * Schema changed handler. + */ +public final class SchemaChangedHandler { + + private final ContextManager contextManager; + + private final ShardingSphereStatisticsRefreshEngine statisticsRefreshEngine; + + public SchemaChangedHandler(final ContextManager contextManager) { + this.contextManager = contextManager; + statisticsRefreshEngine = new ShardingSphereStatisticsRefreshEngine(contextManager); + } + + /** + * Handle schema created. + * + * @param databaseName database name + * @param schemaName schema name + */ + public void handleCreated(final String databaseName, final String schemaName) { + contextManager.getMetaDataContextManager().getSchemaMetaDataManager().addSchema(databaseName, schemaName); + statisticsRefreshEngine.asyncRefresh(); + } + + /** + * Handle schema dropped. + * + * @param databaseName database name + * @param schemaName schema name + */ + public void handleDropped(final String databaseName, final String schemaName) { + contextManager.getMetaDataContextManager().getSchemaMetaDataManager().dropSchema(databaseName, schemaName); + statisticsRefreshEngine.asyncRefresh(); + } +} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/StorageNodeChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/StorageNodeChangedHandler.java new file mode 100644 index 0000000000000..081180797376c --- /dev/null +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/StorageNodeChangedHandler.java @@ -0,0 +1,63 @@ +/* + * 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.dispatch.handler.database; + +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.manager.ContextManager; + +/** + * Storage node changed handler. + */ +@RequiredArgsConstructor +public final class StorageNodeChangedHandler { + + private final ContextManager contextManager; + + /** + * Handle storage node registered. + * + * @param databaseName database name + * @param dataSourceUnitName data source unit name + * @param event data changed event + */ + public void handleRegistered(final String databaseName, final String dataSourceUnitName, final DataChangedEvent event) { + // TODO + } + + /** + * Handle storage node altered. + * + * @param databaseName database name + * @param dataSourceUnitName data source unit name + * @param event data changed event + */ + public void handleAltered(final String databaseName, final String dataSourceUnitName, final DataChangedEvent event) { + // TODO + } + + /** + * Handle storage node unregistered. + * + * @param databaseName database name + * @param dataSourceUnitName data source unit name + */ + public void handleUnregistered(final String databaseName, final String dataSourceUnitName) { + // TODO + } +} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/StorageUnitChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/StorageUnitChangedHandler.java new file mode 100644 index 0000000000000..fff5ccacaf515 --- /dev/null +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/StorageUnitChangedHandler.java @@ -0,0 +1,76 @@ +/* + * 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.dispatch.handler.database; + +import com.google.common.base.Preconditions; +import lombok.RequiredArgsConstructor; +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 java.util.Collections; + +/** + * Storage unit changed handler. + */ +@RequiredArgsConstructor +public final class StorageUnitChangedHandler { + + private final ContextManager contextManager; + + /** + * Handle storage unit registered. + * + * @param databaseName database name + * @param dataSourceUnitName data source unit name + * @param event data changed event + */ + public void handleRegistered(final String databaseName, final String dataSourceUnitName, final DataChangedEvent event) { + Preconditions.checkArgument(event.getValue().equals( + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getKey())), + "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + DataSourcePoolProperties dataSourcePoolProps = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load(databaseName, dataSourceUnitName); + contextManager.getMetaDataContextManager().getStorageUnitManager().registerStorageUnit(databaseName, Collections.singletonMap(dataSourceUnitName, dataSourcePoolProps)); + } + + /** + * Handle storage unit altered. + * + * @param databaseName database name + * @param dataSourceUnitName data source unit name + * @param event data changed event + */ + public void handleAltered(final String databaseName, final String dataSourceUnitName, final DataChangedEvent event) { + Preconditions.checkArgument(event.getValue().equals( + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getKey())), + "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + DataSourcePoolProperties dataSourcePoolProps = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load(databaseName, dataSourceUnitName); + contextManager.getMetaDataContextManager().getStorageUnitManager().alterStorageUnit(databaseName, Collections.singletonMap(dataSourceUnitName, dataSourcePoolProps)); + } + + /** + * Handle storage unit unregistered. + * + * @param databaseName database name + * @param dataSourceUnitName data source unit name + */ + public void handleUnregistered(final String databaseName, final String dataSourceUnitName) { + Preconditions.checkState(contextManager.getMetaDataContexts().getMetaData().containsDatabase(databaseName), "No database '%s' exists.", databaseName); + contextManager.getMetaDataContextManager().getStorageUnitManager().unregisterStorageUnit(databaseName, dataSourceUnitName); + } +} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/TableChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/TableChangedHandler.java new file mode 100644 index 0000000000000..1cb6634ab9dac --- /dev/null +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/TableChangedHandler.java @@ -0,0 +1,70 @@ +/* + * 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.dispatch.handler.database; + +import com.google.common.base.Preconditions; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; +import org.apache.shardingsphere.metadata.persist.node.metadata.TableMetaDataNode; +import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; + +/** + * Table changed handler. + */ +public final class TableChangedHandler { + + private final ContextManager contextManager; + + private final ShardingSphereStatisticsRefreshEngine statisticsRefreshEngine; + + public TableChangedHandler(final ContextManager contextManager) { + this.contextManager = contextManager; + statisticsRefreshEngine = new ShardingSphereStatisticsRefreshEngine(contextManager); + } + + /** + * Handle table created or altered. + * + * @param databaseName database name + * @param schemaName schema name + * @param event data changed event + */ + public void handleCreatedOrAltered(final String databaseName, final String schemaName, final DataChangedEvent event) { + String tableName = TableMetaDataNode.getTableNameByActiveVersionNode(event.getKey()).orElseThrow(() -> new IllegalStateException("Table name not found.")); + Preconditions.checkArgument(event.getValue().equals( + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getKey())), + "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + ShardingSphereTable table = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getTable().load(databaseName, schemaName, tableName); + contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(databaseName, schemaName, table, null); + statisticsRefreshEngine.asyncRefresh(); + } + + /** + * Handle table altered. + * + * @param databaseName database name + * @param schemaName schema name + * @param event data changed event + */ + public void handleDropped(final String databaseName, final String schemaName, final DataChangedEvent event) { + String tableName = TableMetaDataNode.getTableName(event.getKey()).orElseThrow(() -> new IllegalStateException("Table name not found.")); + contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(databaseName, schemaName, tableName, null); + statisticsRefreshEngine.asyncRefresh(); + } +} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/ViewChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/ViewChangedHandler.java new file mode 100644 index 0000000000000..b40a6225dad7f --- /dev/null +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/ViewChangedHandler.java @@ -0,0 +1,70 @@ +/* + * 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.dispatch.handler.database; + +import com.google.common.base.Preconditions; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; +import org.apache.shardingsphere.metadata.persist.node.metadata.ViewMetaDataNode; +import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; + +/** + * View changed handler. + */ +public final class ViewChangedHandler { + + private final ContextManager contextManager; + + private final ShardingSphereStatisticsRefreshEngine statisticsRefreshEngine; + + public ViewChangedHandler(final ContextManager contextManager) { + this.contextManager = contextManager; + statisticsRefreshEngine = new ShardingSphereStatisticsRefreshEngine(contextManager); + } + + /** + * Handle view created or altered. + * + * @param databaseName database name + * @param schemaName schema name + * @param event data changed event + */ + public void handleCreatedOrAltered(final String databaseName, final String schemaName, final DataChangedEvent event) { + String viewName = ViewMetaDataNode.getViewNameByActiveVersionNode(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found.")); + Preconditions.checkArgument(event.getValue().equals( + contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getKey())), + "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); + ShardingSphereView view = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load(databaseName, schemaName, viewName); + contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(databaseName, schemaName, null, view); + new ShardingSphereStatisticsRefreshEngine(contextManager).asyncRefresh(); + } + + /** + * Handle view dropped. + * + * @param databaseName database name + * @param schemaName schema name + * @param event data changed event + */ + public void handleDropped(final String databaseName, final String schemaName, final DataChangedEvent event) { + String viewName = ViewMetaDataNode.getViewName(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found.")); + contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(databaseName, schemaName, null, viewName); + new ShardingSphereStatisticsRefreshEngine(contextManager).asyncRefresh(); + } +} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ListenerAssistedChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ListenerAssistedChangedHandler.java index b82eaf814fd19..99c096c64f02a 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ListenerAssistedChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/global/ListenerAssistedChangedHandler.java @@ -55,7 +55,7 @@ public void handle(final ContextManager contextManager, final DataChangedEvent e private static void handle(final ContextManager contextManager, final String databaseName, final ListenerAssistedType listenerAssistedType) { ClusterPersistRepository repository = (ClusterPersistRepository) contextManager.getPersistServiceFacade().getRepository(); if (ListenerAssistedType.CREATE_DATABASE == listenerAssistedType) { - repository.watch(DatabaseMetaDataNode.getDatabaseNamePath(databaseName), new DatabaseMetaDataChangedListener(contextManager.getComputeNodeInstanceContext().getEventBusContext())); + repository.watch(DatabaseMetaDataNode.getDatabaseNamePath(databaseName), new DatabaseMetaDataChangedListener(contextManager)); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().addDatabase(databaseName); } else if (ListenerAssistedType.DROP_DATABASE == listenerAssistedType) { repository.removeDataListener(DatabaseMetaDataNode.getDatabaseNamePath(databaseName)); diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java index 052a89e143473..4017ce828e773 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java @@ -57,7 +57,7 @@ public void register() { } private void registerDatabaseListeners(final String databaseName) { - repository.watch(DatabaseMetaDataNode.getDatabaseNamePath(databaseName), new DatabaseMetaDataChangedListener(eventBusContext)); + repository.watch(DatabaseMetaDataNode.getDatabaseNamePath(databaseName), new DatabaseMetaDataChangedListener(contextManager)); } private void registerGlobalHandlers(final DataChangedEventHandler handler) { diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListener.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListener.java index b6d3322e8b45e..37fa76bfb7568 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListener.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListener.java @@ -18,12 +18,12 @@ package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.mode.event.DataChangedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.MetaDataChangedEventBuilder; +import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.builder.RuleConfigurationChangedEventBuilder; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; +import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.database.MetaDataChangedHandler; import org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEventListener; import java.util.Optional; @@ -34,11 +34,11 @@ @RequiredArgsConstructor public final class DatabaseMetaDataChangedListener implements DataChangedEventListener { - private final EventBusContext eventBusContext; + private final ContextManager contextManager; @Override public void onChange(final DataChangedEvent event) { - createDispatchEvent(event).ifPresent(eventBusContext::post); + createDispatchEvent(event).ifPresent(contextManager.getComputeNodeInstanceContext().getEventBusContext()::post); } private Optional createDispatchEvent(final DataChangedEvent event) { @@ -47,7 +47,10 @@ private Optional createDispatchEvent(final DataChangedEvent event if (!databaseName.isPresent()) { return Optional.empty(); } - Optional metaDataChangedEvent = new MetaDataChangedEventBuilder().build(databaseName.get(), event); - return metaDataChangedEvent.isPresent() ? metaDataChangedEvent : new RuleConfigurationChangedEventBuilder().build(databaseName.get(), event); + boolean handleCompleted = new MetaDataChangedHandler(contextManager).handle(databaseName.get(), event); + if (handleCompleted) { + return Optional.empty(); + } + return new RuleConfigurationChangedEventBuilder().build(databaseName.get(), event); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java index 969b6117ebb49..8d6f5ebf1d43a 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/ClusterDispatchEventSubscriberRegistry.java @@ -21,9 +21,7 @@ import org.apache.shardingsphere.infra.util.eventbus.EventSubscriber; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.CacheEvictedSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.MetaDataChangedSubscriber; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.RuleItemChangedSubscriber; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.type.StorageUnitEventSubscriber; import java.util.Arrays; import java.util.Collection; @@ -37,9 +35,6 @@ public final class ClusterDispatchEventSubscriberRegistry { private final Collection subscribers; public ClusterDispatchEventSubscriberRegistry(final ContextManager contextManager) { - subscribers = Arrays.asList(new RuleItemChangedSubscriber(contextManager), - new MetaDataChangedSubscriber(contextManager), - new CacheEvictedSubscriber(), - new StorageUnitEventSubscriber(contextManager)); + subscribers = Arrays.asList(new RuleItemChangedSubscriber(contextManager), new CacheEvictedSubscriber()); } } 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 deleted file mode 100644 index 936ff6d05cf3d..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/MetaDataChangedSubscriber.java +++ /dev/null @@ -1,126 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import com.google.common.base.Preconditions; -import com.google.common.eventbus.Subscribe; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.instance.metadata.InstanceType; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.SchemaAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.SchemaDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.table.TableCreatedOrAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.table.TableDroppedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.view.ViewCreatedOrAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.view.ViewDroppedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; -import org.apache.shardingsphere.mode.metadata.refresher.ShardingSphereStatisticsRefreshEngine; - -/** - * Meta data changed subscriber. - */ -@RequiredArgsConstructor -public final class MetaDataChangedSubscriber implements DispatchEventSubscriber { - - private final ContextManager contextManager; - - /** - * Renew to added schema. - * - * @param event schema added event - */ - @Subscribe - public synchronized void renew(final SchemaAddedEvent event) { - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().addSchema(event.getDatabaseName(), event.getSchemaName()); - refreshStatisticsData(); - } - - /** - * Renew to delete schema. - * - * @param event schema delete event - */ - @Subscribe - public synchronized void renew(final SchemaDeletedEvent event) { - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().dropSchema(event.getDatabaseName(), event.getSchemaName()); - refreshStatisticsData(); - } - - /** - * Renew table. - * - * @param event create or alter table event - */ - @Subscribe - public synchronized void renew(final TableCreatedOrAlteredEvent event) { - Preconditions.checkArgument(event.getActiveVersion().equals( - contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())), - "Invalid active version: %s of key: %s", event.getActiveVersion(), event.getActiveVersionKey()); - ShardingSphereTable table = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getTable() - .load(event.getDatabaseName(), event.getSchemaName(), event.getTableName()); - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), table, null); - refreshStatisticsData(); - } - - /** - * Renew table. - * - * @param event drop table event - */ - @Subscribe - public synchronized void renew(final TableDroppedEvent event) { - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), event.getTableName(), null); - refreshStatisticsData(); - } - - /** - * Renew view. - * - * @param event create or alter view event - */ - @Subscribe - public synchronized void renew(final ViewCreatedOrAlteredEvent event) { - Preconditions.checkArgument(event.getActiveVersion().equals( - contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())), - "Invalid active version: %s of key: %s", event.getActiveVersion(), event.getActiveVersionKey()); - ShardingSphereView view = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getView() - .load(event.getDatabaseName(), event.getSchemaName(), event.getViewName()); - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, view); - refreshStatisticsData(); - } - - /** - * Renew view. - * - * @param event drop view event - */ - @Subscribe - public synchronized void renew(final ViewDroppedEvent event) { - contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(event.getDatabaseName(), event.getSchemaName(), null, event.getViewName()); - refreshStatisticsData(); - } - - private void refreshStatisticsData() { - if (contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster() - && InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) { - new ShardingSphereStatisticsRefreshEngine(contextManager).asyncRefresh(); - } - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/MetaDataChangedEventBuilderTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/MetaDataChangedEventBuilderTest.java deleted file mode 100644 index 1e6f71f6af0da..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/builder/MetaDataChangedEventBuilderTest.java +++ /dev/null @@ -1,98 +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.mode.manager.cluster.event.dispatch.builder; - -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.DispatchEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.node.StorageNodeAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.node.StorageNodeRegisteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.node.StorageNodeUnregisteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitRegisteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitUnregisteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.SchemaAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.SchemaDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.table.TableCreatedOrAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.table.TableDroppedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.view.ViewCreatedOrAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.view.ViewDroppedEvent; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.ArgumentsProvider; -import org.junit.jupiter.params.provider.ArgumentsSource; - -import java.util.Optional; -import java.util.stream.Stream; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class MetaDataChangedEventBuilderTest { - - @ParameterizedTest(name = "{0}") - @ArgumentsSource(TestCaseArgumentsProvider.class) - void assertBuild(final String name, final String eventKey, final Type type, final Class toBePostedEventType) { - Optional actual = new MetaDataChangedEventBuilder().build("foo_db", new DataChangedEvent(eventKey, "value", type)); - if (null == toBePostedEventType) { - assertFalse(actual.isPresent()); - } else { - assertTrue(actual.isPresent()); - assertThat(actual.get(), instanceOf(toBePostedEventType)); - } - } - - private static class TestCaseArgumentsProvider implements ArgumentsProvider { - - @Override - public final Stream provideArguments(final ExtensionContext extensionContext) { - return Stream.of( - Arguments.of("changeWithoutDatabase", "/metadata", Type.IGNORED, null), - Arguments.of("addSchema", "/metadata/foo_db/schemas/foo_schema", Type.ADDED, SchemaAddedEvent.class), - Arguments.of("updateSchema", "/metadata/foo_db/schemas/foo_schema", Type.UPDATED, SchemaAddedEvent.class), - Arguments.of("deleteSchema", "/metadata/foo_db/schemas/foo_schema", Type.DELETED, SchemaDeletedEvent.class), - Arguments.of("ignoreChangeSchema", "/metadata/foo_db/schemas/foo_schema", Type.IGNORED, null), - Arguments.of("addTable", "/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version/0", Type.ADDED, TableCreatedOrAlteredEvent.class), - Arguments.of("updateTable", "/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version/0", Type.UPDATED, TableCreatedOrAlteredEvent.class), - Arguments.of("deleteTable", "/metadata/foo_db/schemas/foo_schema/tables/foo_tbl", Type.DELETED, TableDroppedEvent.class), - Arguments.of("invalidAddTable", "/metadata/foo_db/schemas/foo_schema/tables/foo_tbl", Type.ADDED, null), - Arguments.of("invalidDeleteTable", "/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version/0", Type.DELETED, null), - Arguments.of("addView", "/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version/0", Type.ADDED, ViewCreatedOrAlteredEvent.class), - Arguments.of("updateView", "/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version/0", Type.UPDATED, ViewCreatedOrAlteredEvent.class), - Arguments.of("deleteView", "/metadata/foo_db/schemas/foo_schema/views/foo_view", Type.DELETED, ViewDroppedEvent.class), - Arguments.of("invalidAddView", "/metadata/foo_db/schemas/foo_schema/views/foo_view", Type.ADDED, null), - Arguments.of("invalidDeleteView", "/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version/0", Type.DELETED, null), - Arguments.of("registerStorageUnit", "/metadata/foo_db/data_sources/units/foo_unit/active_version/0", Type.ADDED, StorageUnitRegisteredEvent.class), - Arguments.of("alterStorageUnit", "/metadata/foo_db/data_sources/units/foo_unit/active_version/0", Type.UPDATED, StorageUnitAlteredEvent.class), - Arguments.of("unregisterStorageUnit", "/metadata/foo_db/data_sources/units/foo_unit", Type.DELETED, StorageUnitUnregisteredEvent.class), - Arguments.of("invalidRegisterStorageNode", "/metadata/foo_db/data_sources/units/foo_unit", Type.ADDED, null), - Arguments.of("invalidUnregisterStorageNode", "/metadata/foo_db/data_sources/units/foo_unit/active_version/0", Type.DELETED, null), - Arguments.of("ignoreChangeStorageUnit", "/metadata/foo_db/data_sources/units/foo_unit", Type.IGNORED, null), - Arguments.of("registerStorageNode", "/metadata/foo_db/data_sources/nodes/foo_node/active_version/0", Type.ADDED, StorageNodeRegisteredEvent.class), - Arguments.of("alterStorageNode", "/metadata/foo_db/data_sources/nodes/foo_node/active_version/0", Type.UPDATED, StorageNodeAlteredEvent.class), - Arguments.of("unregisterStorageNode", "/metadata/foo_db/data_sources/nodes/foo_node", Type.DELETED, StorageNodeUnregisteredEvent.class), - Arguments.of("invalidRegisterStorageNode", "/metadata/foo_db/data_sources/nodes/foo_node", Type.ADDED, null), - Arguments.of("invalidUnregisterStorageNode", "/metadata/foo_db/data_sources/nodes/foo_node/active_version/0", Type.DELETED, null), - Arguments.of("ignoreChangeStorageNode", "/metadata/foo_db/data_sources/nodes/foo_node", Type.IGNORED, null), - Arguments.of("invalidChangeDataSource", "/metadata/foo_db/data_sources/other", Type.ADDED, null)); - } - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/MetaDataChangedHandlerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/MetaDataChangedHandlerTest.java new file mode 100644 index 0000000000000..63cc3544f2426 --- /dev/null +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/MetaDataChangedHandlerTest.java @@ -0,0 +1,148 @@ +/* + * 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.dispatch.handler.database; + +import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey; +import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; +import org.apache.shardingsphere.infra.instance.metadata.InstanceType; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; +import org.apache.shardingsphere.mode.event.DataChangedEvent; +import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +class MetaDataChangedHandlerTest { + + private MetaDataChangedHandler handler; + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private ContextManager contextManager; + + @BeforeEach + void setUp() { + when(contextManager.getMetaDataContexts().getMetaData().getTemporaryProps().getValue(TemporaryConfigurationPropertyKey.PROXY_META_DATA_COLLECTOR_ENABLED)).thenReturn(false); + when(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()).thenReturn(true); + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(any())).thenReturn("0"); + handler = new MetaDataChangedHandler(contextManager); + } + + @Test + void assertHandleSchemaCreated() { + when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()).thenReturn(InstanceType.PROXY); + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/schemas/foo_schema", "", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).addSchema("foo_db", "foo_schema"); + } + + @Test + void assertHandleSchemaDropped() { + when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()).thenReturn(InstanceType.PROXY); + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/schemas/foo_schema", "", Type.DELETED)); + verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).dropSchema("foo_db", "foo_schema"); + } + + @Test + void assertHandleTableCreated() { + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getTable().load("foo_db", "foo_schema", "foo_tbl")) + .thenReturn(table); + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version/0", "0", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", table, null); + } + + @Test + void assertHandleTableAltered() { + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); + ShardingSphereTable table = mock(ShardingSphereTable.class); + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getTable().load("foo_db", "foo_schema", "foo_tbl")) + .thenReturn(table); + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl/active_version/0", "0", Type.UPDATED)); + verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", table, null); + } + + @Test + void assertHandleTableDropped() { + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/schemas/foo_schema/tables/foo_tbl", "", Type.DELETED)); + verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", "foo_tbl", null); + } + + @Test + void assertHandleViewCreated() { + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); + ShardingSphereView view = mock(ShardingSphereView.class); + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load("foo_db", "foo_schema", "foo_view")) + .thenReturn(view); + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version/0", "0", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", null, view); + } + + @Test + void assertHandleViewAltered() { + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); + ShardingSphereView view = mock(ShardingSphereView.class); + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load("foo_db", "foo_schema", "foo_view")) + .thenReturn(view); + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/schemas/foo_schema/views/foo_view/active_version/0", "0", Type.UPDATED)); + verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", null, view); + } + + @Test + void assertHandleViewDropped() { + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/schemas/foo_schema/views/foo_view", "", Type.DELETED)); + verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", null, "foo_view"); + } + + @Test + void assertHandleStorageUnitRegistered() { + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load("foo_db", "foo_unit")).thenReturn(mock(DataSourcePoolProperties.class)); + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/data_sources/units/foo_unit/active_version/0", "0", Type.ADDED)); + verify(contextManager.getMetaDataContextManager().getStorageUnitManager()).registerStorageUnit(eq("foo_db"), any()); + } + + @Test + void assertHandleStorageUnitAltered() { + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); + when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load("foo_db", "foo_unit")).thenReturn(mock(DataSourcePoolProperties.class)); + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/data_sources/units/foo_unit/active_version/0", "0", Type.UPDATED)); + verify(contextManager.getMetaDataContextManager().getStorageUnitManager()).alterStorageUnit(eq("foo_db"), any()); + } + + @Test + void assertHandleStorageUnitUnregistered() { + when(contextManager.getMetaDataContexts().getMetaData().containsDatabase("foo_db")).thenReturn(true); + handler.handle("foo_db", new DataChangedEvent("/metadata/foo_db/data_sources/units/foo_unit", "", Type.DELETED)); + verify(contextManager.getMetaDataContextManager().getStorageUnitManager()).unregisterStorageUnit("foo_db", "foo_unit"); + } +} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListenerTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListenerTest.java index d1cdabd4faa51..6dfb047211b17 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListenerTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/type/DatabaseMetaDataChangedListenerTest.java @@ -17,12 +17,13 @@ package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener.type; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; +import org.apache.shardingsphere.mode.manager.ContextManager; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Answers; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -35,29 +36,29 @@ class DatabaseMetaDataChangedListenerTest { private DatabaseMetaDataChangedListener listener; - @Mock - private EventBusContext eventBusContext; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private ContextManager contextManager; @BeforeEach void setUp() { - listener = new DatabaseMetaDataChangedListener(eventBusContext); + listener = new DatabaseMetaDataChangedListener(contextManager); } @Test void assertOnChangeWithoutDatabase() { listener.onChange(new DataChangedEvent("/metadata", "value", Type.IGNORED)); - verify(eventBusContext, times(0)).post(any()); + verify(contextManager.getComputeNodeInstanceContext().getEventBusContext(), times(0)).post(any()); } @Test void assertOnChangeWithMetaDataChanged() { listener.onChange(new DataChangedEvent("/metadata/foo_db/schemas/foo_schema", "value", Type.ADDED)); - verify(eventBusContext).post(any()); + verify(contextManager.getComputeNodeInstanceContext().getEventBusContext(), times(0)).post(any()); } @Test void assertOnChangeWithRuleConfigurationChanged() { listener.onChange(new DataChangedEvent("/metadata/foo_db/schemas/foo_schema/rule/", "value", Type.ADDED)); - verify(eventBusContext, times(0)).post(any()); + verify(contextManager.getComputeNodeInstanceContext().getEventBusContext(), times(0)).post(any()); } } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/MetaDataChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/MetaDataChangedSubscriberTest.java deleted file mode 100644 index 2482e8721094f..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/MetaDataChangedSubscriberTest.java +++ /dev/null @@ -1,105 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey; -import org.apache.shardingsphere.infra.instance.metadata.InstanceType; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.SchemaAddedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.SchemaDeletedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.table.TableCreatedOrAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.table.TableDroppedEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.view.ViewCreatedOrAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.metadata.schema.view.ViewDroppedEvent; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.junit.jupiter.MockitoSettings; -import org.mockito.quality.Strictness; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -@MockitoSettings(strictness = Strictness.LENIENT) -class MetaDataChangedSubscriberTest { - - private MetaDataChangedSubscriber subscriber; - - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ContextManager contextManager; - - @BeforeEach - void setUp() { - when(contextManager.getMetaDataContexts().getMetaData().getTemporaryProps().getValue(TemporaryConfigurationPropertyKey.PROXY_META_DATA_COLLECTOR_ENABLED)).thenReturn(false); - when(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()).thenReturn(true); - subscriber = new MetaDataChangedSubscriber(contextManager); - } - - @Test - void assertRenewWithSchemaAddedEvent() { - when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()).thenReturn(InstanceType.PROXY); - subscriber.renew(new SchemaAddedEvent("foo_db", "foo_schema")); - verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).addSchema("foo_db", "foo_schema"); - } - - @Test - void assertRenewWithSchemaDeletedEvent() { - when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()).thenReturn(InstanceType.PROXY); - subscriber.renew(new SchemaDeletedEvent("foo_db", "foo_schema")); - verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).dropSchema("foo_db", "foo_schema"); - } - - @Test - void assertRenewWithCreateOrAlterTableEvent() { - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); - ShardingSphereTable table = mock(ShardingSphereTable.class); - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getTable().load("foo_db", "foo_schema", "foo_tbl")) - .thenReturn(table); - subscriber.renew(new TableCreatedOrAlteredEvent("foo_db", "foo_schema", "foo_tbl", "key", "value")); - verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", table, null); - } - - @Test - void assertRenewWithDropTableEvent() { - subscriber.renew(new TableDroppedEvent("foo_db", "foo_schema", "foo_tbl")); - verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", "foo_tbl", null); - } - - @Test - void assertRenewWithCreateOrAlterViewEvent() { - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); - ShardingSphereView view = mock(ShardingSphereView.class); - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load("foo_db", "foo_schema", "foo_view")) - .thenReturn(view); - subscriber.renew(new ViewCreatedOrAlteredEvent("foo_db", "foo_schema", "foo_view", "key", "value")); - verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", null, view); - } - - @Test - void assertRenewWithDropViewEvent() { - subscriber.renew(new ViewDroppedEvent("foo_db", "foo_schema", "foo_view", "key", "value")); - verify(contextManager.getMetaDataContextManager().getSchemaMetaDataManager()).alterSchema("foo_db", "foo_schema", null, "foo_view"); - } -} diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StorageUnitEventSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StorageUnitEventSubscriberTest.java deleted file mode 100644 index 60598865b7bdf..0000000000000 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StorageUnitEventSubscriberTest.java +++ /dev/null @@ -1,73 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitRegisteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitUnregisteredEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class StorageUnitEventSubscriberTest { - - private StorageUnitEventSubscriber subscriber; - - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ContextManager contextManager; - - @BeforeEach - void setUp() { - subscriber = new StorageUnitEventSubscriber(contextManager); - } - - @Test - void assertRenewWithRegisterStorageUnitEvent() { - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load("foo_db", "foo_unit")).thenReturn(mock(DataSourcePoolProperties.class)); - subscriber.renew(new StorageUnitRegisteredEvent("foo_db", "foo_unit", "key", "value")); - verify(contextManager.getMetaDataContextManager().getStorageUnitManager()).registerStorageUnit(eq("foo_db"), any()); - } - - @Test - void assertRenewWithAlterStorageUnitEvent() { - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value"); - when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load("foo_db", "foo_unit")).thenReturn(mock(DataSourcePoolProperties.class)); - subscriber.renew(new StorageUnitAlteredEvent("foo_db", "foo_unit", "key", "value")); - verify(contextManager.getMetaDataContextManager().getStorageUnitManager()).alterStorageUnit(eq("foo_db"), any()); - } - - @Test - void assertRenewWithUnregisterStorageUnitEvent() { - when(contextManager.getMetaDataContexts().getMetaData().containsDatabase("foo_db")).thenReturn(true); - subscriber.renew(new StorageUnitUnregisteredEvent("foo_db", "foo_unit")); - verify(contextManager.getMetaDataContextManager().getStorageUnitManager()).unregisterStorageUnit("foo_db", "foo_unit"); - } -} From 1138cdeb238e9bb72e238d4f0e930b064836371f Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 1 Jan 2025 23:31:51 +0800 Subject: [PATCH 08/10] Remove useless DispatchEvent (#34220) * Remove useless DispatchEvent * Remove useless DispatchEvent * Remove useless DispatchEvent --- .../node/StorageNodeAlteredEvent.java | 38 --------- .../node/StorageNodeRegisteredEvent.java | 38 --------- .../node/StorageNodeUnregisteredEvent.java | 34 -------- .../unit/StorageUnitAlteredEvent.java | 38 --------- .../unit/StorageUnitRegisteredEvent.java | 38 --------- .../unit/StorageUnitUnregisteredEvent.java | 34 -------- .../handler/database/TableChangedHandler.java | 2 +- .../handler/database/ViewChangedHandler.java | 4 +- .../DataChangedEventListenerRegistry.java | 4 - .../type/StorageUnitEventSubscriber.java | 80 ------------------- 10 files changed, 3 insertions(+), 307 deletions(-) delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeAlteredEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeRegisteredEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeUnregisteredEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitAlteredEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitRegisteredEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitUnregisteredEvent.java delete mode 100644 mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StorageUnitEventSubscriber.java diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeAlteredEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeAlteredEvent.java deleted file mode 100644 index 6eb93e2d7df97..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeAlteredEvent.java +++ /dev/null @@ -1,38 +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.mode.manager.cluster.event.dispatch.event.datasource.node; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Storage node altered event. - */ -@RequiredArgsConstructor -@Getter -public final class StorageNodeAlteredEvent implements DispatchEvent { - - private final String databaseName; - - private final String storageNodeName; - - private final String activeVersionKey; - - private final String activeVersion; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeRegisteredEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeRegisteredEvent.java deleted file mode 100644 index 8316a6686ce2f..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeRegisteredEvent.java +++ /dev/null @@ -1,38 +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.mode.manager.cluster.event.dispatch.event.datasource.node; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Storage node registered event. - */ -@RequiredArgsConstructor -@Getter -public final class StorageNodeRegisteredEvent implements DispatchEvent { - - private final String databaseName; - - private final String storageNodeName; - - private final String activeVersionKey; - - private final String activeVersion; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeUnregisteredEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeUnregisteredEvent.java deleted file mode 100644 index da2f7e5a164ee..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/node/StorageNodeUnregisteredEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.datasource.node; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Storage node unregistered event. - */ -@RequiredArgsConstructor -@Getter -public final class StorageNodeUnregisteredEvent implements DispatchEvent { - - private final String databaseName; - - private final String storageNodeName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitAlteredEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitAlteredEvent.java deleted file mode 100644 index f205f46675bc0..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitAlteredEvent.java +++ /dev/null @@ -1,38 +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.mode.manager.cluster.event.dispatch.event.datasource.unit; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Storage unit altered event. - */ -@RequiredArgsConstructor -@Getter -public final class StorageUnitAlteredEvent implements DispatchEvent { - - private final String databaseName; - - private final String storageUnitName; - - private final String activeVersionKey; - - private final String activeVersion; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitRegisteredEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitRegisteredEvent.java deleted file mode 100644 index 2a61d4ce64944..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitRegisteredEvent.java +++ /dev/null @@ -1,38 +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.mode.manager.cluster.event.dispatch.event.datasource.unit; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Storage unit registered event. - */ -@RequiredArgsConstructor -@Getter -public final class StorageUnitRegisteredEvent implements DispatchEvent { - - private final String databaseName; - - private final String storageUnitName; - - private final String activeVersionKey; - - private final String activeVersion; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitUnregisteredEvent.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitUnregisteredEvent.java deleted file mode 100644 index 4f33e00c64c45..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/event/datasource/unit/StorageUnitUnregisteredEvent.java +++ /dev/null @@ -1,34 +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.mode.manager.cluster.event.dispatch.event.datasource.unit; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.DispatchEvent; - -/** - * Storage unit unregistered event. - */ -@RequiredArgsConstructor -@Getter -public final class StorageUnitUnregisteredEvent implements DispatchEvent { - - private final String databaseName; - - private final String storageUnitName; -} diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/TableChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/TableChangedHandler.java index 1cb6634ab9dac..860fbc52c6528 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/TableChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/TableChangedHandler.java @@ -56,7 +56,7 @@ public void handleCreatedOrAltered(final String databaseName, final String schem } /** - * Handle table altered. + * Handle table dropped. * * @param databaseName database name * @param schemaName schema name diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/ViewChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/ViewChangedHandler.java index b40a6225dad7f..1612947e17443 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/ViewChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/handler/database/ViewChangedHandler.java @@ -52,7 +52,7 @@ public void handleCreatedOrAltered(final String databaseName, final String schem "Invalid active version: %s of key: %s", event.getValue(), event.getKey()); ShardingSphereView view = contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseMetaDataFacade().getView().load(databaseName, schemaName, viewName); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(databaseName, schemaName, null, view); - new ShardingSphereStatisticsRefreshEngine(contextManager).asyncRefresh(); + statisticsRefreshEngine.asyncRefresh(); } /** @@ -65,6 +65,6 @@ public void handleCreatedOrAltered(final String databaseName, final String schem public void handleDropped(final String databaseName, final String schemaName, final DataChangedEvent event) { String viewName = ViewMetaDataNode.getViewName(event.getKey()).orElseThrow(() -> new IllegalStateException("View name not found.")); contextManager.getMetaDataContextManager().getSchemaMetaDataManager().alterSchema(databaseName, schemaName, null, viewName); - new ShardingSphereStatisticsRefreshEngine(contextManager).asyncRefresh(); + statisticsRefreshEngine.asyncRefresh(); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java index 4017ce828e773..cea875543658c 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/listener/DataChangedEventListenerRegistry.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.mode.manager.cluster.event.dispatch.listener; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.metadata.persist.node.DatabaseMetaDataNode; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.handler.DataChangedEventHandler; @@ -37,14 +36,11 @@ public final class DataChangedEventListenerRegistry { private final ClusterPersistRepository repository; - private final EventBusContext eventBusContext; - private final Collection databaseNames; public DataChangedEventListenerRegistry(final ContextManager contextManager, final Collection databaseNames) { this.contextManager = contextManager; repository = (ClusterPersistRepository) contextManager.getPersistServiceFacade().getRepository(); - eventBusContext = contextManager.getComputeNodeInstanceContext().getEventBusContext(); this.databaseNames = databaseNames; } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StorageUnitEventSubscriber.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StorageUnitEventSubscriber.java deleted file mode 100644 index 257039b94553d..0000000000000 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/dispatch/subscriber/type/StorageUnitEventSubscriber.java +++ /dev/null @@ -1,80 +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.mode.manager.cluster.event.dispatch.subscriber.type; - -import com.google.common.base.Preconditions; -import com.google.common.eventbus.Subscribe; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitAlteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitRegisteredEvent; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.event.datasource.unit.StorageUnitUnregisteredEvent; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.manager.cluster.event.dispatch.subscriber.DispatchEventSubscriber; - -import java.util.Collections; - -/** - * Storage unit event subscriber. - */ -@RequiredArgsConstructor -public final class StorageUnitEventSubscriber implements DispatchEventSubscriber { - - private final ContextManager contextManager; - - /** - * Renew for register storage unit. - * - * @param event register storage unit event - */ - @Subscribe - public synchronized void renew(final StorageUnitRegisteredEvent event) { - Preconditions.checkArgument(event.getActiveVersion().equals( - contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())), - "Invalid active version: %s of key: %s", event.getActiveVersion(), event.getActiveVersionKey()); - DataSourcePoolProperties dataSourcePoolProps = contextManager - .getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load(event.getDatabaseName(), event.getStorageUnitName()); - contextManager.getMetaDataContextManager().getStorageUnitManager().registerStorageUnit(event.getDatabaseName(), Collections.singletonMap(event.getStorageUnitName(), dataSourcePoolProps)); - } - - /** - * Renew for alter storage unit. - * - * @param event register storage unit event - */ - @Subscribe - public synchronized void renew(final StorageUnitAlteredEvent event) { - Preconditions.checkArgument(event.getActiveVersion().equals( - contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())), - "Invalid active version: %s of key: %s", event.getActiveVersion(), event.getActiveVersionKey()); - DataSourcePoolProperties dataSourcePoolProps = contextManager - .getPersistServiceFacade().getMetaDataPersistService().getDataSourceUnitService().load(event.getDatabaseName(), event.getStorageUnitName()); - contextManager.getMetaDataContextManager().getStorageUnitManager().alterStorageUnit(event.getDatabaseName(), Collections.singletonMap(event.getStorageUnitName(), dataSourcePoolProps)); - } - - /** - * Renew for unregister storage unit. - * - * @param event register storage unit event - */ - @Subscribe - public synchronized void renew(final StorageUnitUnregisteredEvent event) { - Preconditions.checkState(contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName()), "No database '%s' exists.", event.getDatabaseName()); - contextManager.getMetaDataContextManager().getStorageUnitManager().unregisterStorageUnit(event.getDatabaseName(), event.getStorageUnitName()); - } -} From 680c0db5af1b0faaa10d6402dda916daf41c4e20 Mon Sep 17 00:00:00 2001 From: Zhengqiang Duan Date: Thu, 2 Jan 2025 10:45:56 +0800 Subject: [PATCH 09/10] Support postgresql and opengauss CHARACTER VARYING type metadata load (#34221) * Support postgresql and opengauss CHARACTER VARYING type metadata load * update release note --- RELEASE-NOTES.md | 1 + .../opengauss/metadata/database/OpenGaussDatabaseMetaData.java | 1 + .../postgresql/metadata/database/PostgreSQLDatabaseMetaData.java | 1 + 3 files changed, 3 insertions(+) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 0df46139d40c5..8ecc54deff577 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -141,6 +141,7 @@ 1. Pipeline: Use case-insensitive identifiers to enhance the table metadata loader 1. Pipeline: Support primary key columns ordering for standard pipeline table metadata loader 1. Sharding: Optimize sharding table index name rewriting rules and remove unnecessary suffix rewriting - [#31171](https://github.com/apache/shardingsphere/issues/31171) +1. Metadata: Support postgresql and opengauss CHARACTER VARYING type metadata load - [#34221](https://github.com/apache/shardingsphere/pull/34221) ### Bug Fixes diff --git a/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/OpenGaussDatabaseMetaData.java b/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/OpenGaussDatabaseMetaData.java index 46bc5ce6cf27a..af362026d55a9 100644 --- a/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/OpenGaussDatabaseMetaData.java +++ b/infra/database/type/opengauss/src/main/java/org/apache/shardingsphere/infra/database/opengauss/metadata/database/OpenGaussDatabaseMetaData.java @@ -64,6 +64,7 @@ public Map getExtraDataTypes() { result.put("NUMERIC", Types.NUMERIC); result.put("REAL", Types.REAL); result.put("BOOL", Types.BOOLEAN); + result.put("CHARACTER VARYING", Types.VARCHAR); return result; } diff --git a/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/PostgreSQLDatabaseMetaData.java b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/PostgreSQLDatabaseMetaData.java index e9fd6a531af4b..a46cff8aec93f 100644 --- a/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/PostgreSQLDatabaseMetaData.java +++ b/infra/database/type/postgresql/src/main/java/org/apache/shardingsphere/infra/database/postgresql/metadata/database/PostgreSQLDatabaseMetaData.java @@ -61,6 +61,7 @@ public Map getExtraDataTypes() { result.put("NUMERIC", Types.NUMERIC); result.put("REAL", Types.REAL); result.put("BOOL", Types.BOOLEAN); + result.put("CHARACTER VARYING", Types.VARCHAR); return result; } From a6a09ba68baf3bad8dc088f2af8ebecf9ad58fa5 Mon Sep 17 00:00:00 2001 From: Haoran Meng Date: Thu, 2 Jan 2025 14:44:54 +0800 Subject: [PATCH 10/10] Remove use original database to build new MetaDataContexts (#34222) --- .../manager/DatabaseRuleConfigurationManager.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java index 9cf95b320a923..50361e42ecd85 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/DatabaseRuleConfigurationManager.java @@ -36,6 +36,7 @@ import java.util.LinkedList; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; /** * Database rule configuration manager. @@ -74,7 +75,7 @@ public synchronized void alterRuleConfiguration(final String databaseName, final rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass())); rules.addAll(DatabaseRulesBuilder.build( databaseName, database.getProtocolType(), database.getRuleMetaData().getRules(), ruleConfig, computeNodeInstanceContext, database.getResourceMetaData())); - refreshMetadata(databaseName, database, rules); + refreshMetadata(databaseName, getRuleConfigurations(rules)); } /** @@ -98,14 +99,16 @@ public synchronized void dropRuleConfiguration(final String databaseName, final rules.addAll(DatabaseRulesBuilder.build( databaseName, database.getProtocolType(), database.getRuleMetaData().getRules(), ruleConfig, computeNodeInstanceContext, database.getResourceMetaData())); } - refreshMetadata(databaseName, database, rules); + refreshMetadata(databaseName, getRuleConfigurations(rules)); } - private void refreshMetadata(final String databaseName, final ShardingSphereDatabase database, final Collection rules) throws SQLException { - database.getRuleMetaData().getRules().clear(); - database.getRuleMetaData().getRules().addAll(rules); + private void refreshMetadata(final String databaseName, final Collection ruleConfigurations) throws SQLException { MetaDataContexts reloadMetaDataContexts = MetaDataContextsFactory.createByAlterRule(databaseName, false, - database.getRuleMetaData().getConfigurations(), metaDataContexts.get(), metaDataPersistService, computeNodeInstanceContext); + ruleConfigurations, metaDataContexts.get(), metaDataPersistService, computeNodeInstanceContext); metaDataContexts.set(reloadMetaDataContexts); } + + private Collection getRuleConfigurations(final Collection rules) { + return rules.stream().map(ShardingSphereRule::getConfiguration).collect(Collectors.toList()); + } }