Skip to content

Commit

Permalink
Add UnregisterStorageUnitRuleUsageChecker (apache#30007)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Feb 6, 2024
1 parent 0c03cff commit 35cd5a7
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.broadcast.distsql.handler.update;

import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
import org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker;
import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.UnregisterStorageUnitStatement;

/**
* Unregister storage unit broadcast rule usage checker.
*/
public final class UnregisterStorageUnitBroadcastRuleUsageChecker implements UnregisterStorageUnitRuleUsageChecker {

@Override
public boolean isIgnored(final UnregisterStorageUnitStatement sqlStatement) {
return sqlStatement.isIgnoreBroadcastTables();
}

@Override
public Class<BroadcastRule> getRule() {
return BroadcastRule.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.broadcast.distsql.handler.update.UnregisterStorageUnitBroadcastRuleUsageChecker
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.distsql.handler.type.update.rdl.resource;

import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.UnregisterStorageUnitStatement;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;

/**
* Unregister storage unit rule usage checker.
*/
@SingletonSPI
public interface UnregisterStorageUnitRuleUsageChecker {

/**
* Whether to ignore rule usage checking when unregister storage unit.
*
* @param sqlStatement unregister storage unit statement
* @return ignored or not
*/
boolean isIgnored(UnregisterStorageUnitStatement sqlStatement);

/**
* Get rule.
*
* @return rule
*/
Class<? extends ShardingSphereRule> getRule();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.single.distsql.handler.update;

import org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker;
import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.UnregisterStorageUnitStatement;
import org.apache.shardingsphere.single.rule.SingleRule;

/**
* Unregister storage unit single rule usage checker.
*/
public final class UnregisterStorageUnitSingleRuleUsageChecker implements UnregisterStorageUnitRuleUsageChecker {

@Override
public boolean isIgnored(final UnregisterStorageUnitStatement sqlStatement) {
return sqlStatement.isIgnoreSingleTables();
}

@Override
public Class<SingleRule> getRule() {
return SingleRule.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.single.distsql.handler.update.UnregisterStorageUnitSingleRuleUsageChecker
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,25 @@

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.StorageUnitInUsedException;
import org.apache.shardingsphere.distsql.handler.type.update.DistSQLUpdateExecutor;
import org.apache.shardingsphere.distsql.handler.type.update.rdl.resource.UnregisterStorageUnitRuleUsageChecker;
import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.UnregisterStorageUnitStatement;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.core.external.server.ShardingSphereServerException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.single.rule.SingleRule;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -75,26 +74,21 @@ private void checkInUsed(final UnregisterStorageUnitStatement sqlStatement) {
Map<String, Collection<Class<? extends ShardingSphereRule>>> inUsedStorageUnits = database.getRuleMetaData().getInUsedStorageUnitNameAndRulesMap();
Collection<String> inUsedStorageUnitNames = inUsedStorageUnits.keySet();
inUsedStorageUnitNames.retainAll(sqlStatement.getStorageUnitNames());
if (!inUsedStorageUnitNames.isEmpty()) {
Collection<Class<? extends ShardingSphereRule>> ignoreShardingSphereRules = getIgnoreShardingSphereRules(sqlStatement);
if (!ignoreShardingSphereRules.isEmpty()) {
checkInUsedIgnoreTables(new HashSet<>(inUsedStorageUnitNames), inUsedStorageUnits, ignoreShardingSphereRules);
} else {
String firstResource = inUsedStorageUnitNames.iterator().next();
throw new StorageUnitInUsedException(firstResource, inUsedStorageUnits.get(firstResource));
}
if (inUsedStorageUnitNames.isEmpty()) {
return;
}
Collection<Class<? extends ShardingSphereRule>> ignoreShardingSphereRules = getIgnoreShardingSphereRules(sqlStatement);
if (!ignoreShardingSphereRules.isEmpty()) {
checkInUsedIgnoreTables(new HashSet<>(inUsedStorageUnitNames), inUsedStorageUnits, ignoreShardingSphereRules);
} else {
String firstResource = inUsedStorageUnitNames.iterator().next();
throw new StorageUnitInUsedException(firstResource, inUsedStorageUnits.get(firstResource));
}
}

private Collection<Class<? extends ShardingSphereRule>> getIgnoreShardingSphereRules(final UnregisterStorageUnitStatement sqlStatement) {
Collection<Class<? extends ShardingSphereRule>> result = new LinkedList<>();
if (sqlStatement.isIgnoreSingleTables()) {
result.add(SingleRule.class);
}
if (sqlStatement.isIgnoreBroadcastTables()) {
result.add(BroadcastRule.class);
}
return result;
return ShardingSphereServiceLoader.getServiceInstances(UnregisterStorageUnitRuleUsageChecker.class).stream()
.filter(each -> each.isIgnored(sqlStatement)).map(UnregisterStorageUnitRuleUsageChecker::getRule).collect(Collectors.toList());
}

private void checkInUsedIgnoreTables(final Collection<String> inUsedResourceNames, final Map<String, Collection<Class<? extends ShardingSphereRule>>> inUsedStorageUnits,
Expand Down

0 comments on commit 35cd5a7

Please sign in to comment.