This is an automated email from the ASF dual-hosted git repository.
chengzhang 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 07fa7b990d8 Replace load balancer algorithm with infra load balancer
algorithm in traffic module (#30311)
07fa7b990d8 is described below
commit 07fa7b990d8f098be1cf3b57a51e1b3cdf21989a
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Tue Feb 27 13:38:32 2024 +0800
Replace load balancer algorithm with infra load balancer algorithm in
traffic module (#30311)
---
.../infra/instance/InstanceContext.java | 14 +++----
.../DriverDatabaseConnectionManager.java | 2 +-
.../DriverDatabaseConnectionManagerTest.java | 2 +-
.../traffic/spi/TrafficLoadBalanceAlgorithm.java | 38 -----------------
kernel/traffic/core/pom.xml | 15 +++++++
.../RandomTrafficLoadBalanceAlgorithm.java | 40 ------------------
.../RoundRobinTrafficLoadBalanceAlgorithm.java | 47 ----------------------
.../traffic/engine/TrafficEngine.java | 15 +++----
.../shardingsphere/traffic/rule/TrafficRule.java | 20 ++++-----
.../traffic/rule/TrafficStrategyRule.java | 4 +-
...gsphere.traffic.spi.TrafficLoadBalanceAlgorithm | 19 ---------
.../algorithm/engine/TrafficEngineTest.java | 21 +++++-----
.../RandomTrafficLoadBalanceAlgorithmTest.java | 40 ------------------
.../RoundRobinTrafficLoadBalanceAlgorithmTest.java | 42 -------------------
.../traffic/rule/TrafficRuleTest.java | 8 ++--
.../handler/update/AlterTrafficRuleExecutor.java | 6 +--
.../DistSQLTrafficLoadBalanceAlgorithmFixture.java | 7 ++--
17 files changed, 65 insertions(+), 275 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
index 331ae5b3b53..b8508ac6750 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/InstanceContext.java
@@ -21,17 +21,17 @@ import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
-import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
-import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
+import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
import org.apache.shardingsphere.infra.instance.workerid.WorkerIdGenerator;
import org.apache.shardingsphere.infra.lock.LockContext;
+import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
-import java.util.ArrayList;
import java.util.Collection;
+import java.util.LinkedHashMap;
import java.util.LinkedList;
-import java.util.List;
+import java.util.Map;
import java.util.Optional;
import java.util.Properties;
@@ -152,11 +152,11 @@ public final class InstanceContext {
* @param labels collection of contained label
* @return compute node instances
*/
- public List<InstanceMetaData> getAllClusterInstances(final InstanceType
instanceType, final Collection<String> labels) {
- List<InstanceMetaData> result = new
ArrayList<>(allClusterInstances.size());
+ public Map<String, InstanceMetaData> getAllClusterInstances(final
InstanceType instanceType, final Collection<String> labels) {
+ Map<String, InstanceMetaData> result = new
LinkedHashMap<>(allClusterInstances.size(), 1F);
for (ComputeNodeInstance each : allClusterInstances) {
if (each.getMetaData().getType() == instanceType &&
labels.stream().anyMatch(((Collection<String>) each.getLabels())::contains)) {
- result.add(each.getMetaData());
+ result.put(each.getMetaData().getId(), each.getMetaData());
}
}
return result;
diff --git
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
index 1fe9db95f8f..ebc17b7ef57 100644
---
a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
+++
b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java
@@ -118,7 +118,7 @@ public final class DriverDatabaseConnectionManager
implements DatabaseConnection
Preconditions.checkState(!propsMap.isEmpty(), "Can not get data source
properties from meta data.");
DataSourcePoolProperties propsSample =
propsMap.values().iterator().next();
Collection<ShardingSphereUser> users =
persistService.getGlobalRuleService().loadUsers();
- Collection<InstanceMetaData> instances =
contextManager.getInstanceContext().getAllClusterInstances(InstanceType.PROXY,
rule.getLabels());
+ Collection<InstanceMetaData> instances =
contextManager.getInstanceContext().getAllClusterInstances(InstanceType.PROXY,
rule.getLabels()).values();
return
DataSourcePoolCreator.create(createDataSourcePoolPropertiesMap(instances,
users, propsSample, actualDatabaseName), true);
}
diff --git
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
index 2ec99176ea4..51a97656508 100644
---
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
+++
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
@@ -82,7 +82,7 @@ class DriverDatabaseConnectionManagerTest {
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(
new RuleMetaData(Arrays.asList(mock(TransactionRule.class,
RETURNS_DEEP_STUBS), mock(TrafficRule.class, RETURNS_DEEP_STUBS))));
when(result.getInstanceContext().getAllClusterInstances(InstanceType.PROXY,
Arrays.asList("OLTP", "OLAP"))).thenReturn(
- Collections.singletonList(new ProxyInstanceMetaData("foo_id",
"127.0.0.1@3307", "foo_version")));
+ Collections.singletonMap("foo_id", new
ProxyInstanceMetaData("foo_id", "127.0.0.1@3307", "foo_version")));
Map<String, DataSource> trafficDataSourceMap =
mockTrafficDataSourceMap();
when(DataSourcePoolCreator.create(any(),
eq(true))).thenReturn(trafficDataSourceMap);
return result;
diff --git
a/kernel/traffic/api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficLoadBalanceAlgorithm.java
b/kernel/traffic/api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficLoadBalanceAlgorithm.java
deleted file mode 100644
index f1c5018fcf4..00000000000
---
a/kernel/traffic/api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficLoadBalanceAlgorithm.java
+++ /dev/null
@@ -1,38 +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.traffic.spi;
-
-import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
-import org.apache.shardingsphere.infra.algorithm.core.ShardingSphereAlgorithm;
-
-import java.util.List;
-
-/**
- * Traffic load balance algorithm.
- */
-public interface TrafficLoadBalanceAlgorithm extends ShardingSphereAlgorithm {
-
- /**
- * Get instance meta data.
- *
- * @param name traffic strategy name
- * @param instances instances
- * @return got meta data
- */
- InstanceMetaData getInstanceMetaData(String name, List<InstanceMetaData>
instances);
-}
diff --git a/kernel/traffic/core/pom.xml b/kernel/traffic/core/pom.xml
index af1dd64bce8..b855b1be0ad 100644
--- a/kernel/traffic/core/pom.xml
+++ b/kernel/traffic/core/pom.xml
@@ -47,6 +47,21 @@
<artifactId>shardingsphere-cluster-mode-core</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+
<artifactId>shardingsphere-infra-algorithm-load-balancer-random</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+
<artifactId>shardingsphere-infra-algorithm-load-balancer-round-robin</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+
<artifactId>shardingsphere-infra-algorithm-load-balancer-weight</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
diff --git
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithm.java
b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithm.java
deleted file mode 100644
index 5918ec50bac..00000000000
---
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithm.java
+++ /dev/null
@@ -1,40 +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.traffic.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
-import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
-
-import java.util.List;
-import java.util.concurrent.ThreadLocalRandom;
-
-/**
- * Random traffic load balance algorithm.
- */
-public final class RandomTrafficLoadBalanceAlgorithm implements
TrafficLoadBalanceAlgorithm {
-
- @Override
- public InstanceMetaData getInstanceMetaData(final String name, final
List<InstanceMetaData> instances) {
- return
instances.get(ThreadLocalRandom.current().nextInt(instances.size()));
- }
-
- @Override
- public String getType() {
- return "RANDOM";
- }
-}
diff --git
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithm.java
b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithm.java
deleted file mode 100644
index 6ecb74cc274..00000000000
---
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithm.java
+++ /dev/null
@@ -1,47 +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.traffic.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
-import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
-
-import java.util.List;
-import java.util.concurrent.atomic.AtomicInteger;
-
-/**
- * Round-robin traffic load balance algorithm.
- */
-public final class RoundRobinTrafficLoadBalanceAlgorithm implements
TrafficLoadBalanceAlgorithm {
-
- private final AtomicInteger count = new AtomicInteger(0);
-
- @Override
- public InstanceMetaData getInstanceMetaData(final String name, final
List<InstanceMetaData> instances) {
- return instances.get(Math.abs(count.getAndIncrement()) %
instances.size());
- }
-
- @Override
- public String getType() {
- return "ROUND_ROBIN";
- }
-
- @Override
- public boolean isDefault() {
- return true;
- }
-}
diff --git
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java
b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java
index fae5374577a..a2e84d574c1 100644
---
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java
+++
b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/engine/TrafficEngine.java
@@ -18,15 +18,16 @@
package org.apache.shardingsphere.traffic.engine;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.session.query.QueryContext;
+import
org.apache.shardingsphere.infra.algorithm.load.balancer.core.LoadBalanceAlgorithm;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
+import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.traffic.rule.TrafficStrategyRule;
-import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
-import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
import java.util.Optional;
/**
@@ -51,11 +52,11 @@ public final class TrafficEngine {
if (!strategyRule.isPresent() ||
isInvalidStrategyRule(strategyRule.get())) {
return Optional.empty();
}
- List<InstanceMetaData> instances =
instanceContext.getAllClusterInstances(InstanceType.PROXY,
strategyRule.get().getLabels());
+ Map<String, InstanceMetaData> instances =
instanceContext.getAllClusterInstances(InstanceType.PROXY,
strategyRule.get().getLabels());
if (!instances.isEmpty()) {
- TrafficLoadBalanceAlgorithm loadBalancer =
strategyRule.get().getLoadBalancer();
- InstanceMetaData instanceMetaData = 1 == instances.size() ?
instances.iterator().next() :
loadBalancer.getInstanceMetaData(strategyRule.get().getName(), instances);
- return Optional.of(instanceMetaData.getId());
+ LoadBalanceAlgorithm loadBalancer =
strategyRule.get().getLoadBalancer();
+ String instanceId = 1 == instances.size() ?
instances.keySet().iterator().next() :
loadBalancer.getAvailableTargetName(strategyRule.get().getName(), new
ArrayList<>(instances.keySet()));
+ return Optional.of(instanceId);
}
return Optional.empty();
}
diff --git
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
index 548d202bf9b..09bd182ef82 100644
---
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
+++
b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.traffic.rule;
import com.google.common.base.Preconditions;
import lombok.Getter;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.load.balancer.core.LoadBalanceAlgorithm;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.rule.identifier.scope.GlobalRule;
import org.apache.shardingsphere.infra.session.query.QueryContext;
@@ -35,7 +36,6 @@ import
org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficValue
import
org.apache.shardingsphere.traffic.api.traffic.transaction.TransactionTrafficAlgorithm;
import
org.apache.shardingsphere.traffic.api.traffic.transaction.TransactionTrafficValue;
import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
-import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
import java.util.Collection;
import java.util.Collections;
@@ -59,7 +59,7 @@ public final class TrafficRule implements GlobalRule {
public TrafficRule(final TrafficRuleConfiguration ruleConfig) {
configuration = ruleConfig;
Map<String, TrafficAlgorithm> trafficAlgorithms =
createTrafficAlgorithms(ruleConfig.getTrafficAlgorithms());
- Map<String, TrafficLoadBalanceAlgorithm> loadBalancers =
createTrafficLoadBalanceAlgorithms(ruleConfig.getLoadBalancers());
+ Map<String, LoadBalanceAlgorithm> loadBalancers =
createLoadBalanceAlgorithms(ruleConfig.getLoadBalancers());
strategyRules =
createTrafficStrategyRules(ruleConfig.getTrafficStrategies(),
trafficAlgorithms, loadBalancers);
}
@@ -75,20 +75,20 @@ public final class TrafficRule implements GlobalRule {
return result;
}
- private Map<String, TrafficLoadBalanceAlgorithm>
createTrafficLoadBalanceAlgorithms(final Map<String, AlgorithmConfiguration>
loadBalancers) {
- Map<String, TrafficLoadBalanceAlgorithm> result = new
LinkedHashMap<>();
+ private Map<String, LoadBalanceAlgorithm>
createLoadBalanceAlgorithms(final Map<String, AlgorithmConfiguration>
loadBalancers) {
+ Map<String, LoadBalanceAlgorithm> result = new LinkedHashMap<>();
for (TrafficStrategyConfiguration each :
configuration.getTrafficStrategies()) {
if (null == loadBalancers.get(each.getLoadBalancerName())) {
break;
}
AlgorithmConfiguration algorithmConfig =
loadBalancers.get(each.getLoadBalancerName());
- result.put(each.getName() + "." + each.getLoadBalancerName(),
TypedSPILoader.getService(TrafficLoadBalanceAlgorithm.class,
algorithmConfig.getType(), algorithmConfig.getProps()));
+ result.put(each.getName() + "." + each.getLoadBalancerName(),
TypedSPILoader.getService(LoadBalanceAlgorithm.class,
algorithmConfig.getType(), algorithmConfig.getProps()));
}
return result;
}
private Collection<TrafficStrategyRule> createTrafficStrategyRules(final
Collection<TrafficStrategyConfiguration> trafficStrategies,
- final
Map<String, TrafficAlgorithm> trafficAlgorithms, final Map<String,
TrafficLoadBalanceAlgorithm> loadBalancers) {
+ final
Map<String, TrafficAlgorithm> trafficAlgorithms, final Map<String,
LoadBalanceAlgorithm> loadBalancers) {
Collection<TrafficStrategyRule> noneTransactionStrategyRules = new
LinkedList<>();
Collection<TrafficStrategyRule> result = new LinkedList<>();
for (TrafficStrategyConfiguration each : trafficStrategies) {
@@ -105,12 +105,12 @@ public final class TrafficRule implements GlobalRule {
}
private TrafficStrategyRule createTrafficStrategyRule(final
TrafficStrategyConfiguration strategyConfig, final TrafficAlgorithm
trafficAlgorithm,
- final Map<String,
TrafficLoadBalanceAlgorithm> loadBalancers) {
+ final Map<String,
LoadBalanceAlgorithm> loadBalancers) {
TrafficStrategyRule result;
if (trafficAlgorithm instanceof SimplifiedTrafficAlgorithm) {
result = new TrafficStrategyRule(strategyConfig.getName(),
Collections.emptyList(), trafficAlgorithm, null);
} else {
- TrafficLoadBalanceAlgorithm loadBalancer =
getLoadBalancer(strategyConfig, loadBalancers);
+ LoadBalanceAlgorithm loadBalancer =
getLoadBalancer(strategyConfig, loadBalancers);
result = new TrafficStrategyRule(strategyConfig.getName(), new
LinkedHashSet<>(strategyConfig.getLabels()), trafficAlgorithm, loadBalancer);
}
return result;
@@ -171,8 +171,8 @@ public final class TrafficRule implements GlobalRule {
return trafficAlgorithm.match(transactionTrafficValue);
}
- private TrafficLoadBalanceAlgorithm getLoadBalancer(final
TrafficStrategyConfiguration strategyConfig, final Map<String,
TrafficLoadBalanceAlgorithm> loadBalancers) {
- TrafficLoadBalanceAlgorithm result =
loadBalancers.get(strategyConfig.getName() + "." +
strategyConfig.getLoadBalancerName());
+ private LoadBalanceAlgorithm getLoadBalancer(final
TrafficStrategyConfiguration strategyConfig, final Map<String,
LoadBalanceAlgorithm> loadBalancers) {
+ LoadBalanceAlgorithm result =
loadBalancers.get(strategyConfig.getName() + "." +
strategyConfig.getLoadBalancerName());
Preconditions.checkState(null != result, "Traffic load balance
algorithm can not be null.");
return result;
}
diff --git
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficStrategyRule.java
b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficStrategyRule.java
index 78dfa46f054..0f7a1ec2a64 100644
---
a/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficStrategyRule.java
+++
b/kernel/traffic/core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficStrategyRule.java
@@ -19,8 +19,8 @@ package org.apache.shardingsphere.traffic.rule;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
+import
org.apache.shardingsphere.infra.algorithm.load.balancer.core.LoadBalanceAlgorithm;
import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
-import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
import java.util.Collection;
@@ -37,5 +37,5 @@ public final class TrafficStrategyRule {
private final TrafficAlgorithm trafficAlgorithm;
- private final TrafficLoadBalanceAlgorithm loadBalancer;
+ private final LoadBalanceAlgorithm loadBalancer;
}
diff --git
a/kernel/traffic/core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
b/kernel/traffic/core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
deleted file mode 100644
index 9a11ae2d8f6..00000000000
---
a/kernel/traffic/core/src/main/resources/META-INF/services/org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm
+++ /dev/null
@@ -1,19 +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.
-#
-
-org.apache.shardingsphere.traffic.algorithm.loadbalance.RandomTrafficLoadBalanceAlgorithm
-org.apache.shardingsphere.traffic.algorithm.loadbalance.RoundRobinTrafficLoadBalanceAlgorithm
diff --git
a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
index 9f8878edb10..f1f59b4111b 100644
---
a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
+++
b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/engine/TrafficEngineTest.java
@@ -17,15 +17,15 @@
package org.apache.shardingsphere.traffic.algorithm.engine;
-import org.apache.shardingsphere.infra.session.query.QueryContext;
+import
org.apache.shardingsphere.infra.algorithm.load.balancer.core.LoadBalanceAlgorithm;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import
org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
+import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.traffic.engine.TrafficEngine;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.traffic.rule.TrafficStrategyRule;
-import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
@@ -34,7 +34,8 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
-import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
@@ -90,9 +91,9 @@ class TrafficEngineTest {
TrafficEngine trafficEngine = new TrafficEngine(trafficRule,
instanceContext);
when(trafficRule.findMatchedStrategyRule(queryContext,
false)).thenReturn(Optional.of(strategyRule));
when(strategyRule.getLabels()).thenReturn(Arrays.asList("OLTP",
"OLAP"));
- TrafficLoadBalanceAlgorithm loadBalancer =
mock(TrafficLoadBalanceAlgorithm.class);
- List<InstanceMetaData> instanceIds = mockComputeNodeInstances();
- when(loadBalancer.getInstanceMetaData("traffic",
instanceIds)).thenReturn(new ProxyInstanceMetaData("foo_id", 3307));
+ LoadBalanceAlgorithm loadBalancer = mock(LoadBalanceAlgorithm.class);
+ Map<String, InstanceMetaData> instanceIds = mockComputeNodeInstances();
+ when(loadBalancer.getAvailableTargetName("traffic", new
ArrayList<>(instanceIds.keySet()))).thenReturn("foo_id");
when(strategyRule.getLoadBalancer()).thenReturn(loadBalancer);
when(strategyRule.getName()).thenReturn("traffic");
when(instanceContext.getAllClusterInstances(InstanceType.PROXY,
Arrays.asList("OLTP", "OLAP"))).thenReturn(instanceIds);
@@ -100,10 +101,10 @@ class TrafficEngineTest {
assertThat(actual, is(Optional.of("foo_id")));
}
- private List<InstanceMetaData> mockComputeNodeInstances() {
- List<InstanceMetaData> result = new ArrayList<>();
- result.add(new ProxyInstanceMetaData("foo_id", "127.0.0.1@3307",
"foo_version"));
- result.add(new ProxyInstanceMetaData("bar_id", "127.0.0.1@3308",
"foo_version"));
+ private Map<String, InstanceMetaData> mockComputeNodeInstances() {
+ Map<String, InstanceMetaData> result = new HashMap<>();
+ result.put("foo_id", new ProxyInstanceMetaData("foo_id",
"127.0.0.1@3307", "foo_version"));
+ result.put("bar_id", new ProxyInstanceMetaData("bar_id",
"127.0.0.1@3308", "foo_version"));
return result;
}
}
diff --git
a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithmTest.java
b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithmTest.java
deleted file mode 100644
index 71ada02ede6..00000000000
---
a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RandomTrafficLoadBalanceAlgorithmTest.java
+++ /dev/null
@@ -1,40 +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.traffic.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
-import
org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
-import org.junit.jupiter.api.Test;
-
-import java.util.Arrays;
-import java.util.List;
-
-import static org.junit.jupiter.api.Assertions.assertTrue;
-
-class RandomTrafficLoadBalanceAlgorithmTest {
-
- @Test
- void assertGetInstanceId() {
- RandomTrafficLoadBalanceAlgorithm randomAlgorithm = new
RandomTrafficLoadBalanceAlgorithm();
- List<InstanceMetaData> instances = Arrays.asList(new
ProxyInstanceMetaData("foo_id", "127.0.0.1@3307", "foo_verison"),
- new ProxyInstanceMetaData("bar_id", "127.0.0.1@3308",
"foo_verison"));
-
assertTrue(instances.contains(randomAlgorithm.getInstanceMetaData("simple_traffic",
instances)));
-
assertTrue(instances.contains(randomAlgorithm.getInstanceMetaData("simple_traffic",
instances)));
-
assertTrue(instances.contains(randomAlgorithm.getInstanceMetaData("simple_traffic",
instances)));
- }
-}
diff --git
a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithmTest.java
b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithmTest.java
deleted file mode 100644
index b36f75c0473..00000000000
---
a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/algorithm/loadbalance/RoundRobinTrafficLoadBalanceAlgorithmTest.java
+++ /dev/null
@@ -1,42 +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.traffic.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
-import
org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
-import org.junit.jupiter.api.Test;
-
-import java.util.Arrays;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
-class RoundRobinTrafficLoadBalanceAlgorithmTest {
-
- @Test
- void assertGetInstanceId() {
- InstanceMetaData instance1 = new
ProxyInstanceMetaData("127.0.0.1@3307", "127.0.0.1@3307", "foo_version");
- InstanceMetaData instance2 = new
ProxyInstanceMetaData("127.0.0.1@3308", "127.0.0.1@3308", "foo_version");
- List<InstanceMetaData> instances = Arrays.asList(instance1, instance2);
- RoundRobinTrafficLoadBalanceAlgorithm roundRobinAlgorithm = new
RoundRobinTrafficLoadBalanceAlgorithm();
- assertThat(roundRobinAlgorithm.getInstanceMetaData("simple_traffic",
instances), is(instance1));
- assertThat(roundRobinAlgorithm.getInstanceMetaData("simple_traffic",
instances), is(instance2));
- assertThat(roundRobinAlgorithm.getInstanceMetaData("simple_traffic",
instances), is(instance1));
- }
-}
diff --git
a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
index af34c9885b5..74e73e7d9db 100644
---
a/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
+++
b/kernel/traffic/core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
@@ -17,9 +17,10 @@
package org.apache.shardingsphere.traffic.rule;
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.load.balancer.random.RandomLoadBalanceAlgorithm;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
-import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
@@ -33,7 +34,6 @@ import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.CommentSe
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import
org.apache.shardingsphere.traffic.algorithm.loadbalance.RandomTrafficLoadBalanceAlgorithm;
import
org.apache.shardingsphere.traffic.algorithm.traffic.hint.SQLHintTrafficAlgorithm;
import
org.apache.shardingsphere.traffic.algorithm.traffic.transaction.ProxyTrafficAlgorithm;
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
@@ -64,7 +64,7 @@ class TrafficRuleTest {
assertThat(actual.get().getName(), is("sql_hint_traffic"));
assertThat(actual.get().getLabels(), is(new
HashSet<>(Arrays.asList("OLTP", "OLAP"))));
assertThat(actual.get().getTrafficAlgorithm(),
instanceOf(SQLHintTrafficAlgorithm.class));
- assertThat(actual.get().getLoadBalancer(),
instanceOf(RandomTrafficLoadBalanceAlgorithm.class));
+ assertThat(actual.get().getLoadBalancer(),
instanceOf(RandomLoadBalanceAlgorithm.class));
}
@Test
@@ -80,7 +80,7 @@ class TrafficRuleTest {
assertThat(actual.get().getName(), is("transaction_traffic"));
assertThat(actual.get().getLabels(),
is(Collections.singleton("OLAP")));
assertThat(actual.get().getTrafficAlgorithm(),
instanceOf(ProxyTrafficAlgorithm.class));
- assertThat(actual.get().getLoadBalancer(),
instanceOf(RandomTrafficLoadBalanceAlgorithm.class));
+ assertThat(actual.get().getLoadBalancer(),
instanceOf(RandomLoadBalanceAlgorithm.class));
}
@Test
diff --git
a/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleExecutor.java
b/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleExecutor.java
index f6f2f678e52..6333fcc5b2d 100644
---
a/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleExecutor.java
+++
b/kernel/traffic/distsql/handler/src/main/java/org/apache/shardingsphere/traffic/distsql/handler/update/AlterTrafficRuleExecutor.java
@@ -18,9 +18,10 @@
package org.apache.shardingsphere.traffic.distsql.handler.update;
import lombok.Setter;
-import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
import
org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
+import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.load.balancer.core.LoadBalanceAlgorithm;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.traffic.api.config.TrafficRuleConfiguration;
@@ -30,7 +31,6 @@ import
org.apache.shardingsphere.traffic.distsql.segment.TrafficRuleSegment;
import
org.apache.shardingsphere.traffic.distsql.statement.updatable.AlterTrafficRuleStatement;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
-import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
import java.util.Collection;
import java.util.LinkedHashMap;
@@ -66,7 +66,7 @@ public final class AlterTrafficRuleExecutor implements
GlobalRuleDefinitionExecu
sqlStatement.getSegments().forEach(each -> {
TypedSPILoader.checkService(TrafficAlgorithm.class,
each.getAlgorithm().getName(), each.getAlgorithm().getProps());
if (null != each.getLoadBalancer()) {
- TypedSPILoader.checkService(TrafficLoadBalanceAlgorithm.class,
each.getLoadBalancer().getName(), each.getLoadBalancer().getProps());
+ TypedSPILoader.checkService(LoadBalanceAlgorithm.class,
each.getLoadBalancer().getName(), each.getLoadBalancer().getProps());
}
});
}
diff --git
a/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/fixture/DistSQLTrafficLoadBalanceAlgorithmFixture.java
b/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/fixture/DistSQLTrafficLoadBalanceAlgorithmFixture.java
index c319474d923..b5864013f5c 100644
---
a/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/fixture/DistSQLTrafficLoadBalanceAlgorithmFixture.java
+++
b/kernel/traffic/distsql/handler/src/test/java/org/apache/shardingsphere/traffic/distsql/handler/fixture/DistSQLTrafficLoadBalanceAlgorithmFixture.java
@@ -17,15 +17,14 @@
package org.apache.shardingsphere.traffic.distsql.handler.fixture;
-import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
-import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
+import
org.apache.shardingsphere.infra.algorithm.load.balancer.core.LoadBalanceAlgorithm;
import java.util.List;
-public final class DistSQLTrafficLoadBalanceAlgorithmFixture implements
TrafficLoadBalanceAlgorithm {
+public final class DistSQLTrafficLoadBalanceAlgorithmFixture implements
LoadBalanceAlgorithm {
@Override
- public InstanceMetaData getInstanceMetaData(final String name, final
List<InstanceMetaData> instances) {
+ public String getAvailableTargetName(final String groupName, final
List<String> availableTargetNames) {
return null;
}