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"); - } -}