You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/05/31 09:37:35 UTC
[shardingsphere] branch master updated: Minor refactor
CreateShardingTableRuleBackendHandler (#10587)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new b554be7 Minor refactor CreateShardingTableRuleBackendHandler (#10587)
b554be7 is described below
commit b554be79f23d5f4ed3a0cb9b2a97579f3cb57dc2
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Mon May 31 17:37:03 2021 +0800
Minor refactor CreateShardingTableRuleBackendHandler (#10587)
---
.../listener/impl/RuleChangedListenerTest.java | 70 ----------------------
.../CreateShardingTableRuleBackendHandler.java | 44 +++++++-------
2 files changed, 20 insertions(+), 94 deletions(-)
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/listener/impl/RuleChangedListenerTest.java b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/listener/impl/RuleChangedListenerTest.java
deleted file mode 100644
index a7a399f..0000000
--- a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/registry/listener/impl/RuleChangedListenerTest.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.governance.core.registry.listener.impl;
-
-import org.apache.shardingsphere.governance.core.registry.listener.event.GovernanceEvent;
-import org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent;
-import org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent.Type;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import java.util.Arrays;
-import java.util.Optional;
-
-import static org.junit.Assert.assertFalse;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class RuleChangedListenerTest extends GovernanceListenerTest {
-
- private static final String RULE_FILE = "yaml/rule.yaml";
-
- private RuleChangedListener ruleChangedListener;
-
- @Before
- public void setUp() {
- ruleChangedListener = new RuleChangedListener(getRegistryCenterRepository(), Arrays.asList("foo_db", "bar_db"));
- }
-
- @Test
- public void assertCreateEventWithoutSchema() {
- DataChangedEvent dataChangedEvent = new DataChangedEvent("/metadata", readYAML(RULE_FILE), Type.UPDATED);
- Optional<GovernanceEvent> actual = ruleChangedListener.createEvent(dataChangedEvent);
- assertFalse(actual.isPresent());
- }
-
- @Test
- public void assertCreateEventWithEmptyValue() {
- DataChangedEvent dataChangedEvent = new DataChangedEvent("/metadata/foo_db/rules", "", Type.UPDATED);
- Optional<GovernanceEvent> actual = ruleChangedListener.createEvent(dataChangedEvent);
- assertFalse(actual.isPresent());
- }
-
- @Test
- public void assertCreateEventWithRule() {
- // TODO use RuleConfigurationFixture instead of ShardingRuleConfiguration for test case
-// DataChangedEvent dataChangedEvent = new DataChangedEvent("/metadata/foo_db/rules", readYAML(RULE_FILE), Type.UPDATED);
-// Optional<GovernanceEvent> actual = ruleChangedListener.createEvent(dataChangedEvent);
-// assertTrue(actual.isPresent());
-// assertThat(((RuleConfigurationsChangedEvent) actual.get()).getSchemaName(), is("foo_db"));
-// Collection<RuleConfiguration> ruleConfigs = ((RuleConfigurationsChangedEvent) actual.get()).getRuleConfigurations();
-// assertThat(ruleConfigs.size(), is(1));
-// assertThat(((ShardingRuleConfiguration) ruleConfigs.iterator().next()).getTables().size(), is(1));
- }
-}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
index 377f8ba..8863a1c 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
@@ -17,10 +17,8 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl;
-import org.apache.shardingsphere.distsql.parser.segment.TableRuleSegment;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingTableRuleStatement;
import org.apache.shardingsphere.governance.core.registry.listener.event.rule.RuleConfigurationsAlteredEvent;
-import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
@@ -32,12 +30,11 @@ import org.apache.shardingsphere.proxy.backend.text.SchemaRequiredBackendHandler
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.converter.ShardingRuleStatementConverter;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
-import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
+import java.util.stream.Collectors;
/**
* Create sharding table rule backend handler.
@@ -51,6 +48,22 @@ public final class CreateShardingTableRuleBackendHandler extends SchemaRequiredB
@Override
public ResponseHeader execute(final String schemaName, final CreateShardingTableRuleStatement sqlStatement) {
check(schemaName, sqlStatement);
+ create(schemaName, sqlStatement);
+ post(schemaName);
+ return new UpdateResponseHeader(sqlStatement);
+ }
+
+ private void check(final String schemaName, final CreateShardingTableRuleStatement sqlStatement) {
+ Collection<String> existLogicTables = getLogicTables(schemaName);
+ Set<String> duplicateTableNames = sqlStatement.getTables().stream().collect(Collectors.toMap(each -> each.getLogicTable(), each -> 1, (a, b) -> a + b))
+ .entrySet().stream().filter(entry -> entry.getValue() > 1).map(entry -> entry.getKey()).collect(Collectors.toSet());
+ duplicateTableNames.addAll(sqlStatement.getTables().stream().map(each -> each.getLogicTable()).filter(existLogicTables::contains).collect(Collectors.toSet()));
+ if (!duplicateTableNames.isEmpty()) {
+ throw new DuplicateTablesException(duplicateTableNames);
+ }
+ }
+
+ private void create(final String schemaName, final CreateShardingTableRuleStatement sqlStatement) {
ShardingRuleConfiguration shardingRuleConfiguration = (ShardingRuleConfiguration) new YamlRuleConfigurationSwapperEngine()
.swapToRuleConfigurations(Collections.singleton(ShardingRuleStatementConverter.convert(sqlStatement))).iterator().next();
Optional<ShardingRuleConfiguration> existShardingRuleConfiguration = getShardingRuleConfiguration(schemaName);
@@ -60,24 +73,6 @@ public final class CreateShardingTableRuleBackendHandler extends SchemaRequiredB
} else {
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().add(shardingRuleConfiguration);
}
- post(schemaName, ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations());
- return new UpdateResponseHeader(sqlStatement);
- }
-
- private void check(final String schemaName, final CreateShardingTableRuleStatement sqlStatement) {
- Collection<String> logicTableNames = new ArrayList<>(sqlStatement.getTables().size());
- Collection<String> existLogicTables = getLogicTables(schemaName);
- Set<String> duplicateTableNames = new HashSet<>(sqlStatement.getTables().size(), 1);
- for (TableRuleSegment tableRuleSegment : sqlStatement.getTables()) {
- if (logicTableNames.contains(tableRuleSegment.getLogicTable())
- || existLogicTables.contains(tableRuleSegment.getLogicTable())) {
- duplicateTableNames.add(tableRuleSegment.getLogicTable());
- }
- logicTableNames.add(tableRuleSegment.getLogicTable());
- }
- if (!duplicateTableNames.isEmpty()) {
- throw new DuplicateTablesException(duplicateTableNames);
- }
}
private Collection<String> getLogicTables(final String schemaName) {
@@ -89,8 +84,9 @@ public final class CreateShardingTableRuleBackendHandler extends SchemaRequiredB
.filter(each -> each instanceof ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration) each).findFirst();
}
- private void post(final String schemaName, final Collection<RuleConfiguration> rules) {
- ShardingSphereEventBus.getInstance().post(new RuleConfigurationsAlteredEvent(schemaName, rules));
+ private void post(final String schemaName) {
+ ShardingSphereEventBus.getInstance().post(new RuleConfigurationsAlteredEvent(schemaName,
+ ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations()));
// TODO Need to get the executed feedback from registry center for returning.
}
}