Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DistSQL]Support
alter traffic rule
syntax. (#15155)
* Support `alter traffic rule` syntax. * Rename variable. * Add successful test. * Add converter.
- Loading branch information
Showing
10 changed files
with
340 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
.../apache/shardingsphere/distsql/parser/statement/rdl/create/AlterTrafficRuleStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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.parser.statement.rdl.create; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment; | ||
import org.apache.shardingsphere.distsql.parser.statement.ral.common.AlterDistSQLStatement; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Alter traffic rule statement. | ||
*/ | ||
@RequiredArgsConstructor | ||
@Getter | ||
public final class AlterTrafficRuleStatement extends AlterDistSQLStatement { | ||
|
||
private final Collection<TrafficRuleSegment> segments; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...gsphere/proxy/backend/text/distsql/ral/common/alter/excutor/AlterTrafficRuleExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* 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.proxy.backend.text.distsql.ral.common.alter.excutor; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment; | ||
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.AlterTrafficRuleStatement; | ||
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException; | ||
import org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmConfigurationException; | ||
import org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException; | ||
import org.apache.shardingsphere.mode.metadata.MetaDataContexts; | ||
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService; | ||
import org.apache.shardingsphere.proxy.backend.context.ProxyContext; | ||
import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader; | ||
import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader; | ||
import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.alter.AlterStatementExecutor; | ||
import org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.convert.TrafficRuleConverter; | ||
import org.apache.shardingsphere.spi.typed.TypedSPIRegistry; | ||
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration; | ||
import org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration; | ||
import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm; | ||
import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Alter traffic rule handler. | ||
*/ | ||
@RequiredArgsConstructor | ||
public final class AlterTrafficRuleExecutor implements AlterStatementExecutor { | ||
|
||
private final AlterTrafficRuleStatement sqlStatement; | ||
|
||
@Override | ||
public ResponseHeader execute() throws DistSQLException { | ||
Optional<TrafficRuleConfiguration> currentConfiguration = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getGlobalRuleMetaData() | ||
.findRuleConfiguration(TrafficRuleConfiguration.class).stream().findAny(); | ||
check(sqlStatement, currentConfiguration); | ||
TrafficRuleConfiguration toBeAlteredConfiguration = TrafficRuleConverter.convert(sqlStatement.getSegments()); | ||
updateToRepository(toBeAlteredConfiguration, currentConfiguration.get()); | ||
return new UpdateResponseHeader(sqlStatement); | ||
} | ||
|
||
private void check(final AlterTrafficRuleStatement sqlStatement, final Optional<TrafficRuleConfiguration> currentConfiguration) throws DistSQLException { | ||
DistSQLException.predictionThrow(currentConfiguration.isPresent(), new RequiredRuleMissedException("Traffic")); | ||
Collection<String> currentRuleNames = currentConfiguration.get().getTrafficStrategies().stream().map(TrafficStrategyConfiguration::getName).collect(Collectors.toSet()); | ||
Set<String> notExistRuleNames = sqlStatement.getSegments().stream().map(TrafficRuleSegment::getName).filter(each -> !currentRuleNames.contains(each)).collect(Collectors.toSet()); | ||
DistSQLException.predictionThrow(notExistRuleNames.isEmpty(), new RequiredRuleMissedException("Traffic", notExistRuleNames)); | ||
Collection<String> invalidAlgorithmNames = getInvalidAlgorithmNames(sqlStatement.getSegments()); | ||
DistSQLException.predictionThrow(invalidAlgorithmNames.isEmpty(), new InvalidAlgorithmConfigurationException("traffic", invalidAlgorithmNames)); | ||
} | ||
|
||
private Collection<String> getInvalidAlgorithmNames(final Collection<TrafficRuleSegment> segments) { | ||
Collection<String> result = new ArrayList<>(segments.size()); | ||
sqlStatement.getSegments().forEach(each -> { | ||
if (!TypedSPIRegistry.findRegisteredService(TrafficAlgorithm.class, each.getAlgorithm().getName(), new Properties()).isPresent()) { | ||
result.add(each.getAlgorithm().getName()); | ||
} | ||
if (!TypedSPIRegistry.findRegisteredService(TrafficLoadBalanceAlgorithm.class, each.getLoadBalancer().getName(), new Properties()).isPresent()) { | ||
result.add(each.getLoadBalancer().getName()); | ||
} | ||
}); | ||
return result; | ||
} | ||
|
||
private void updateToRepository(final TrafficRuleConfiguration toBeAlteredConfiguration, final TrafficRuleConfiguration currentConfiguration) { | ||
currentConfiguration.getTrafficStrategies().addAll(toBeAlteredConfiguration.getTrafficStrategies()); | ||
currentConfiguration.getTrafficAlgorithms().putAll(toBeAlteredConfiguration.getTrafficAlgorithms()); | ||
currentConfiguration.getLoadBalancers().putAll(toBeAlteredConfiguration.getLoadBalancers()); | ||
MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts(); | ||
Optional<MetaDataPersistService> metaDataPersistService = metaDataContexts.getMetaDataPersistService(); | ||
metaDataPersistService.ifPresent(op -> op.getGlobalRuleService().persist(metaDataContexts.getGlobalRuleMetaData().getConfigurations(), true)); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...he/shardingsphere/proxy/backend/text/distsql/ral/common/convert/TrafficRuleConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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.proxy.backend.text.distsql.ral.common.convert; | ||
|
||
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment; | ||
import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment; | ||
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration; | ||
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration; | ||
import org.apache.shardingsphere.traffic.api.config.TrafficStrategyConfiguration; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Traffic rule converter. | ||
*/ | ||
public final class TrafficRuleConverter { | ||
|
||
/** | ||
* Convert traffic rule segment to traffic rule configuration. | ||
* | ||
* @param segments traffic rule segments | ||
* @return traffic rule configuration | ||
*/ | ||
public static TrafficRuleConfiguration convert(final Collection<TrafficRuleSegment> segments) { | ||
TrafficRuleConfiguration result = new TrafficRuleConfiguration(); | ||
segments.forEach(each -> setConfigurationData(result, each)); | ||
return result; | ||
} | ||
|
||
private static void setConfigurationData(final TrafficRuleConfiguration result, final TrafficRuleSegment each) { | ||
ShardingSphereAlgorithmConfiguration trafficAlgorithm = createAlgorithmConfiguration(each.getAlgorithm()); | ||
ShardingSphereAlgorithmConfiguration loadBalancer = createAlgorithmConfiguration(each.getLoadBalancer()); | ||
String trafficAlgorithmName = createAlgorithmName(each.getName(), trafficAlgorithm); | ||
String loadBalancerName = createAlgorithmName(each.getName(), loadBalancer); | ||
TrafficStrategyConfiguration trafficStrategy = createTrafficStrategy(each, trafficAlgorithmName, loadBalancerName); | ||
result.getTrafficStrategies().add(trafficStrategy); | ||
result.getTrafficAlgorithms().put(trafficAlgorithmName, trafficAlgorithm); | ||
result.getLoadBalancers().put(loadBalancerName, loadBalancer); | ||
} | ||
|
||
private static ShardingSphereAlgorithmConfiguration createAlgorithmConfiguration(final AlgorithmSegment segment) { | ||
return new ShardingSphereAlgorithmConfiguration(segment.getName(), segment.getProps()); | ||
} | ||
|
||
private static TrafficStrategyConfiguration createTrafficStrategy(final TrafficRuleSegment trafficRuleSegment, final String trafficAlgorithmName, final String loadBalancerName) { | ||
return new TrafficStrategyConfiguration(trafficRuleSegment.getName(), trafficRuleSegment.getLabels(), trafficAlgorithmName, loadBalancerName); | ||
} | ||
|
||
private static String createAlgorithmName(final String ruleName, final ShardingSphereAlgorithmConfiguration algorithm) { | ||
return String.format("%s_%s", ruleName, algorithm.getType()).toLowerCase(); | ||
} | ||
} |
Oops, something went wrong.