Skip to content

Commit

Permalink
Add unused algorithm finder for readwrite-splitting (#33656)
Browse files Browse the repository at this point in the history
* Add unused algorithm finder for readwrite-splitting

* Remove useless TODO comment
  • Loading branch information
RaigorJiang authored Nov 14, 2024
1 parent cb3dc82 commit c6988a0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.distsql.handler.checker.ReadwriteSplittingRuleStatementChecker;
import org.apache.shardingsphere.readwritesplitting.distsql.handler.converter.ReadwriteSplittingRuleStatementConverter;
import org.apache.shardingsphere.readwritesplitting.distsql.statement.AlterReadwriteSplittingRuleStatement;
import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Alter readwrite-splitting rule executor.
Expand All @@ -59,16 +56,10 @@ public ReadwriteSplittingRuleConfiguration buildToBeAlteredRuleConfiguration(fin

@Override
public ReadwriteSplittingRuleConfiguration buildToBeDroppedRuleConfiguration(final ReadwriteSplittingRuleConfiguration toBeAlteredRuleConfig) {
Collection<ReadwriteSplittingDataSourceGroupRuleConfiguration> dataSourceGroups = new LinkedList<>();
Map<String, AlgorithmConfiguration> loadBalancers = new HashMap<>(rule.getConfiguration().getDataSourceGroups().size(), 1F);
List<String> toBeAlteredDataSourceNames = toBeAlteredRuleConfig.getDataSourceGroups().stream().map(ReadwriteSplittingDataSourceGroupRuleConfiguration::getName).collect(Collectors.toList());
for (ReadwriteSplittingDataSourceGroupRuleConfiguration each : rule.getConfiguration().getDataSourceGroups()) {
if (toBeAlteredDataSourceNames.contains(each.getName())) {
dataSourceGroups.add(each);
loadBalancers.put(each.getLoadBalancerName(), rule.getConfiguration().getLoadBalancers().get(each.getLoadBalancerName()));
}
}
return new ReadwriteSplittingRuleConfiguration(dataSourceGroups, loadBalancers);
Collection<String> unusedLoadBalancers = UnusedAlgorithmFinder.findUnusedLoadBalancers(rule.getConfiguration());
Map<String, AlgorithmConfiguration> toBeDroppedLoadBalancers = new HashMap<>(unusedLoadBalancers.size(), 1F);
unusedLoadBalancers.forEach(each -> toBeDroppedLoadBalancers.put(each, rule.getConfiguration().getLoadBalancers().get(each)));
return new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), toBeDroppedLoadBalancers);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.readwritesplitting.distsql.handler.update;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;

import java.util.Collection;
import java.util.stream.Collectors;

/**
* Unused algorithm finder.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class UnusedAlgorithmFinder {

/**
* Find unused load balancers.
*
* @param ruleConfig readwrite-splitting rule configuration
* @return found unused load balancers
*/
public static Collection<String> findUnusedLoadBalancers(final ReadwriteSplittingRuleConfiguration ruleConfig) {
Collection<String> inUsedLoadBalancers = ruleConfig.getDataSourceGroups().stream().map(ReadwriteSplittingDataSourceGroupRuleConfiguration::getLoadBalancerName)
.collect(Collectors.toSet());
return ruleConfig.getLoadBalancers().keySet().stream().filter(each -> !inUsedLoadBalancers.contains(each)).collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public final class AlterDatabaseRuleOperator implements DatabaseRuleOperator {
public void operate(final DatabaseRuleDefinitionStatement sqlStatement, final ShardingSphereDatabase database, final RuleConfiguration currentRuleConfig) throws SQLException {
RuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(sqlStatement);
MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
metaDataManagerPersistService.alterRuleConfiguration(database.getName(), decorateRuleConfiguration(database, toBeAlteredRuleConfig));
RuleConfiguration toBeDroppedRuleConfig = executor.buildToBeDroppedRuleConfiguration(toBeAlteredRuleConfig);
metaDataManagerPersistService.removeRuleConfigurationItem(database.getName(), toBeDroppedRuleConfig);
metaDataManagerPersistService.alterRuleConfiguration(database.getName(), decorateRuleConfiguration(database, toBeAlteredRuleConfig));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public interface DatabaseRuleAlterExecutor<T extends SQLStatement, R extends Sha
C buildToBeAlteredRuleConfiguration(T sqlStatement);

/**
* TODO Remove temporary default implementation
* Build to be dropped rule configuration.
*
* @param toBeAlteredRuleConfig new rule configuration to be renewed
Expand Down

0 comments on commit c6988a0

Please sign in to comment.