You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/07/15 11:49:00 UTC
[GitHub] [shardingsphere] menghaoranss commented on a change in pull request #11346: Add distSQL syntax: 'drop sharding algorithm'.
menghaoranss commented on a change in pull request #11346:
URL: https://github.com/apache/shardingsphere/pull/11346#discussion_r670369266
##########
File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmStatementUpdater.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.sharding.distsql.handler.update;
+
+import org.apache.shardingsphere.infra.distsql.exception.rule.RuleDefinitionViolationException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredAlgorithmMissedException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.AlgorithmInUsedException;
+import org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
+import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingAlgorithmStatement;
+
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.stream.Collectors;
+
+/**
+ * Drop sharding algorithm statement updater.
+ */
+public final class DropShardingAlgorithmStatementUpdater implements RuleDefinitionDropUpdater<DropShardingAlgorithmStatement, ShardingRuleConfiguration> {
+
+ @Override
+ public void checkSQLStatement(final String schemaName, final DropShardingAlgorithmStatement sqlStatement,
+ final ShardingRuleConfiguration currentRuleConfig, final ShardingSphereResource resource) throws RuleDefinitionViolationException {
+ checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
+ checkShardingAlgorithmsExisted(schemaName, sqlStatement, currentRuleConfig);
+ checkShardingAlgorithmsInUsed(schemaName, sqlStatement, currentRuleConfig);
+ }
+
+ private void checkCurrentRuleConfiguration(final String schemaName, final ShardingRuleConfiguration currentRuleConfig) throws RequiredAlgorithmMissedException {
+ if (null == currentRuleConfig) {
+ throw new RequiredAlgorithmMissedException(schemaName);
Review comment:
RequiredRuleMissedException should be thrown here
##########
File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmStatementUpdater.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.sharding.distsql.handler.update;
+
+import org.apache.shardingsphere.infra.distsql.exception.rule.RuleDefinitionViolationException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredAlgorithmMissedException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.AlgorithmInUsedException;
+import org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
+import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingAlgorithmStatement;
+
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.stream.Collectors;
+
+/**
+ * Drop sharding algorithm statement updater.
+ */
+public final class DropShardingAlgorithmStatementUpdater implements RuleDefinitionDropUpdater<DropShardingAlgorithmStatement, ShardingRuleConfiguration> {
+
+ @Override
+ public void checkSQLStatement(final String schemaName, final DropShardingAlgorithmStatement sqlStatement,
+ final ShardingRuleConfiguration currentRuleConfig, final ShardingSphereResource resource) throws RuleDefinitionViolationException {
+ checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
+ checkShardingAlgorithmsExisted(schemaName, sqlStatement, currentRuleConfig);
+ checkShardingAlgorithmsInUsed(schemaName, sqlStatement, currentRuleConfig);
+ }
+
+ private void checkCurrentRuleConfiguration(final String schemaName, final ShardingRuleConfiguration currentRuleConfig) throws RequiredAlgorithmMissedException {
+ if (null == currentRuleConfig) {
+ throw new RequiredAlgorithmMissedException(schemaName);
+ }
+ }
+
+ private void checkShardingAlgorithmsExisted(final String schemaName, final DropShardingAlgorithmStatement sqlStatement,
Review comment:
checkShardingAlgorithmsIsExisted is better?
##########
File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmStatementUpdater.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.sharding.distsql.handler.update;
+
+import org.apache.shardingsphere.infra.distsql.exception.rule.RuleDefinitionViolationException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredAlgorithmMissedException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.AlgorithmInUsedException;
+import org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
+import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingAlgorithmStatement;
+
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.stream.Collectors;
+
+/**
+ * Drop sharding algorithm statement updater.
+ */
+public final class DropShardingAlgorithmStatementUpdater implements RuleDefinitionDropUpdater<DropShardingAlgorithmStatement, ShardingRuleConfiguration> {
+
+ @Override
+ public void checkSQLStatement(final String schemaName, final DropShardingAlgorithmStatement sqlStatement,
+ final ShardingRuleConfiguration currentRuleConfig, final ShardingSphereResource resource) throws RuleDefinitionViolationException {
+ checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
+ checkShardingAlgorithmsExisted(schemaName, sqlStatement, currentRuleConfig);
+ checkShardingAlgorithmsInUsed(schemaName, sqlStatement, currentRuleConfig);
+ }
+
+ private void checkCurrentRuleConfiguration(final String schemaName, final ShardingRuleConfiguration currentRuleConfig) throws RequiredAlgorithmMissedException {
+ if (null == currentRuleConfig) {
+ throw new RequiredAlgorithmMissedException(schemaName);
+ }
+ }
+
+ private void checkShardingAlgorithmsExisted(final String schemaName, final DropShardingAlgorithmStatement sqlStatement,
+ final ShardingRuleConfiguration currentRuleConfig) throws RequiredAlgorithmMissedException {
+ Collection<String> currentShardingAlgorithms = getCurrentShardingAlgorithms(currentRuleConfig);
+ Collection<String> notExistedAlgorithms = sqlStatement.getAlgorithmNames().stream().filter(each -> !currentShardingAlgorithms.contains(each)).collect(Collectors.toList());
+ if (!notExistedAlgorithms.isEmpty()) {
+ throw new RequiredAlgorithmMissedException(schemaName, notExistedAlgorithms);
+ }
+ }
+
+ private void checkShardingAlgorithmsInUsed(final String schemaName, final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig)
Review comment:
checkShardingAlgorithmsIsInUsed
##########
File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmStatementUpdater.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.sharding.distsql.handler.update;
+
+import org.apache.shardingsphere.infra.distsql.exception.rule.RuleDefinitionViolationException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredAlgorithmMissedException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.AlgorithmInUsedException;
+import org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
+import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingAlgorithmStatement;
+
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.stream.Collectors;
+
+/**
+ * Drop sharding algorithm statement updater.
+ */
+public final class DropShardingAlgorithmStatementUpdater implements RuleDefinitionDropUpdater<DropShardingAlgorithmStatement, ShardingRuleConfiguration> {
+
+ @Override
+ public void checkSQLStatement(final String schemaName, final DropShardingAlgorithmStatement sqlStatement,
+ final ShardingRuleConfiguration currentRuleConfig, final ShardingSphereResource resource) throws RuleDefinitionViolationException {
+ checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
+ checkShardingAlgorithmsExisted(schemaName, sqlStatement, currentRuleConfig);
+ checkShardingAlgorithmsInUsed(schemaName, sqlStatement, currentRuleConfig);
+ }
+
+ private void checkCurrentRuleConfiguration(final String schemaName, final ShardingRuleConfiguration currentRuleConfig) throws RequiredAlgorithmMissedException {
+ if (null == currentRuleConfig) {
+ throw new RequiredAlgorithmMissedException(schemaName);
+ }
+ }
+
+ private void checkShardingAlgorithmsExisted(final String schemaName, final DropShardingAlgorithmStatement sqlStatement,
+ final ShardingRuleConfiguration currentRuleConfig) throws RequiredAlgorithmMissedException {
+ Collection<String> currentShardingAlgorithms = getCurrentShardingAlgorithms(currentRuleConfig);
+ Collection<String> notExistedAlgorithms = sqlStatement.getAlgorithmNames().stream().filter(each -> !currentShardingAlgorithms.contains(each)).collect(Collectors.toList());
+ if (!notExistedAlgorithms.isEmpty()) {
+ throw new RequiredAlgorithmMissedException(schemaName, notExistedAlgorithms);
+ }
+ }
+
+ private void checkShardingAlgorithmsInUsed(final String schemaName, final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig)
+ throws AlgorithmInUsedException {
+ Collection<String> allInUsed = getAllOfAlgorithmsInUsed(currentRuleConfig);
+ Collection<String> usedAlgorithms = sqlStatement.getAlgorithmNames().stream().filter(allInUsed::contains).collect(Collectors.toList());
+ if (!usedAlgorithms.isEmpty()) {
+ throw new AlgorithmInUsedException(schemaName, usedAlgorithms);
+ }
+ }
+
+ private Collection<String> getAllOfAlgorithmsInUsed(final ShardingRuleConfiguration shardingRuleConfig) {
+ Collection<String> result = new LinkedHashSet<>();
+ shardingRuleConfig.getTables().stream().forEach(each -> {
+ if (null != each.getDatabaseShardingStrategy()) {
+ result.add(each.getDatabaseShardingStrategy().getShardingAlgorithmName());
+ }
+ if (null != each.getTableShardingStrategy()) {
+ result.add(each.getTableShardingStrategy().getShardingAlgorithmName());
+ }
+ });
+ return result;
+ }
+
+ private Collection<String> getCurrentShardingAlgorithms(final ShardingRuleConfiguration shardingRuleConfig) {
+ return shardingRuleConfig.getShardingAlgorithms().keySet();
+ }
+
+ @Override
+ public boolean updateCurrentRuleConfiguration(final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) {
+ for (String each : sqlStatement.getAlgorithmNames()) {
+ dropShardingAlgorithm(currentRuleConfig, each);
+ }
+ return false;
Review comment:
should real result be returned here?
##########
File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmStatementUpdater.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.sharding.distsql.handler.update;
+
+import org.apache.shardingsphere.infra.distsql.exception.rule.RuleDefinitionViolationException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredAlgorithmMissedException;
+import org.apache.shardingsphere.infra.distsql.exception.rule.AlgorithmInUsedException;
+import org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
+import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingAlgorithmStatement;
+
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.stream.Collectors;
+
+/**
+ * Drop sharding algorithm statement updater.
+ */
+public final class DropShardingAlgorithmStatementUpdater implements RuleDefinitionDropUpdater<DropShardingAlgorithmStatement, ShardingRuleConfiguration> {
+
+ @Override
+ public void checkSQLStatement(final String schemaName, final DropShardingAlgorithmStatement sqlStatement,
+ final ShardingRuleConfiguration currentRuleConfig, final ShardingSphereResource resource) throws RuleDefinitionViolationException {
+ checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
+ checkShardingAlgorithmsExisted(schemaName, sqlStatement, currentRuleConfig);
+ checkShardingAlgorithmsInUsed(schemaName, sqlStatement, currentRuleConfig);
+ }
+
+ private void checkCurrentRuleConfiguration(final String schemaName, final ShardingRuleConfiguration currentRuleConfig) throws RequiredAlgorithmMissedException {
+ if (null == currentRuleConfig) {
+ throw new RequiredAlgorithmMissedException(schemaName);
+ }
+ }
+
+ private void checkShardingAlgorithmsExisted(final String schemaName, final DropShardingAlgorithmStatement sqlStatement,
+ final ShardingRuleConfiguration currentRuleConfig) throws RequiredAlgorithmMissedException {
+ Collection<String> currentShardingAlgorithms = getCurrentShardingAlgorithms(currentRuleConfig);
+ Collection<String> notExistedAlgorithms = sqlStatement.getAlgorithmNames().stream().filter(each -> !currentShardingAlgorithms.contains(each)).collect(Collectors.toList());
+ if (!notExistedAlgorithms.isEmpty()) {
+ throw new RequiredAlgorithmMissedException(schemaName, notExistedAlgorithms);
+ }
+ }
+
+ private void checkShardingAlgorithmsInUsed(final String schemaName, final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig)
+ throws AlgorithmInUsedException {
+ Collection<String> allInUsed = getAllOfAlgorithmsInUsed(currentRuleConfig);
+ Collection<String> usedAlgorithms = sqlStatement.getAlgorithmNames().stream().filter(allInUsed::contains).collect(Collectors.toList());
+ if (!usedAlgorithms.isEmpty()) {
+ throw new AlgorithmInUsedException(schemaName, usedAlgorithms);
+ }
+ }
+
+ private Collection<String> getAllOfAlgorithmsInUsed(final ShardingRuleConfiguration shardingRuleConfig) {
+ Collection<String> result = new LinkedHashSet<>();
+ shardingRuleConfig.getTables().stream().forEach(each -> {
Review comment:
autoTables in ShardingRuleConfiguration should also be added
##########
File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-parser/src/main/antlr4/imports/sharding/RDLStatement.g4
##########
@@ -54,6 +54,10 @@ dropShardingBindingTableRules
dropShardingBroadcastTableRules
: DROP SHARDING BROADCAST TABLE RULES
;
+
+dropShardingAlgorithm
+ : DROP SHARDING ALGORITHM algorithmName (COMMA algorithmName)*
Review comment:
Does we need support `from schema`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org