This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 c00776dd6b5 Remove useless TransactionConnectionContext param from
ReadQueryLoadBalanceAlgorithm (#24810)
c00776dd6b5 is described below
commit c00776dd6b5deebd4be4d471d729c2ce5245f229
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Mar 24 19:19:27 2023 +0800
Remove useless TransactionConnectionContext param from
ReadQueryLoadBalanceAlgorithm (#24810)
---
.../spi/ReadQueryLoadBalanceAlgorithm.java | 4 +---
.../RandomReadQueryLoadBalanceAlgorithm.java | 3 +--
.../RoundRobinReadQueryLoadBalanceAlgorithm.java | 3 +--
.../WeightReadQueryLoadBalanceAlgorithm.java | 7 +++----
.../route/impl/ReadwriteSplittingDataSourceRouter.java | 2 +-
.../RandomReadQueryLoadBalanceAlgorithmTest.java | 17 +++++++----------
.../RoundRobinReadQueryLoadBalanceAlgorithmTest.java | 14 ++++++--------
.../WeightReadQueryLoadBalanceAlgorithmTest.java | 13 ++++++-------
8 files changed, 26 insertions(+), 37 deletions(-)
diff --git
a/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/spi/ReadQueryLoadBalanceAlgorithm.java
b/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/spi/ReadQueryLoadBalanceAlgorithm.java
index 92040daf39c..e7e7c0b03a7 100644
---
a/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/spi/ReadQueryLoadBalanceAlgorithm.java
+++
b/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/spi/ReadQueryLoadBalanceAlgorithm.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.readwritesplitting.spi;
-import
org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
import
org.apache.shardingsphere.infra.util.spi.type.typed.algorithm.ShardingSphereAlgorithm;
import java.util.List;
@@ -33,8 +32,7 @@ public interface ReadQueryLoadBalanceAlgorithm extends
ShardingSphereAlgorithm {
* @param name read query logic data source name
* @param writeDataSourceName name of write data source
* @param readDataSourceNames names of read data sources
- * @param context context
* @return name of selected data source
*/
- String getDataSource(String name, String writeDataSourceName, List<String>
readDataSourceNames, TransactionConnectionContext context);
+ String getDataSource(String name, String writeDataSourceName, List<String>
readDataSourceNames);
}
diff --git
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithm.java
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithm.java
index a2191463616..c9494400dc9 100644
---
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithm.java
+++
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithm.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
-import
org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
import
org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import java.util.List;
@@ -29,7 +28,7 @@ import java.util.concurrent.ThreadLocalRandom;
public final class RandomReadQueryLoadBalanceAlgorithm implements
ReadQueryLoadBalanceAlgorithm {
@Override
- public String getDataSource(final String name, final String
writeDataSourceName, final List<String> readDataSourceNames, final
TransactionConnectionContext context) {
+ public String getDataSource(final String name, final String
writeDataSourceName, final List<String> readDataSourceNames) {
return
readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size()));
}
diff --git
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithm.java
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithm.java
index c9a29abaaab..8484d1433fb 100644
---
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithm.java
+++
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithm.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
-import
org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
import
org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import java.util.List;
@@ -31,7 +30,7 @@ public final class RoundRobinReadQueryLoadBalanceAlgorithm
implements ReadQueryL
private final AtomicInteger count = new AtomicInteger(0);
@Override
- public String getDataSource(final String name, final String
writeDataSourceName, final List<String> readDataSourceNames, final
TransactionConnectionContext context) {
+ public String getDataSource(final String name, final String
writeDataSourceName, final List<String> readDataSourceNames) {
return readDataSourceNames.get(Math.abs(count.getAndIncrement()) %
readDataSourceNames.size());
}
diff --git
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
index d6f4e2a069d..0c55a4da283 100644
---
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
+++
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
@@ -19,7 +19,6 @@ package
org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
import com.google.common.base.Preconditions;
import lombok.Getter;
-import
org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import
org.apache.shardingsphere.readwritesplitting.exception.algorithm.InvalidReadDatabaseWeightException;
import
org.apache.shardingsphere.readwritesplitting.exception.algorithm.MissingRequiredReadDatabaseWeightException;
@@ -54,7 +53,7 @@ public final class WeightReadQueryLoadBalanceAlgorithm
implements ReadQueryLoadB
}
@Override
- public String getDataSource(final String name, final String
writeDataSourceName, final List<String> readDataSourceNames, final
TransactionConnectionContext context) {
+ public String getDataSource(final String name, final String
writeDataSourceName, final List<String> readDataSourceNames) {
double[] weight = weightMap.containsKey(name) &&
weightMap.get(name).length == readDataSourceNames.size() ? weightMap.get(name)
: initWeight(readDataSourceNames);
weightMap.put(name, weight);
return getDataSourceName(readDataSourceNames, weight);
@@ -107,8 +106,8 @@ public final class WeightReadQueryLoadBalanceAlgorithm
implements ReadQueryLoadB
private double getWeightValue(final String readDataSourceName) {
Object weightObject = props.get(readDataSourceName);
- ShardingSpherePreconditions.checkNotNull(weightObject, () -> new
MissingRequiredReadDatabaseWeightException(getType(),
- String.format("Read database `%s` access weight is not
configured", readDataSourceName)));
+ ShardingSpherePreconditions.checkNotNull(weightObject,
+ () -> new
MissingRequiredReadDatabaseWeightException(getType(), String.format("Read
database `%s` access weight is not configured.", readDataSourceName)));
double result;
try {
result = Double.parseDouble(weightObject.toString());
diff --git
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/impl/ReadwriteSplittingDataSourceRouter.java
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/impl/ReadwriteSplittingDataSourceRouter.java
index 9da8d7086f1..51e5a072857 100644
---
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/impl/ReadwriteSplittingDataSourceRouter.java
+++
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/route/impl/ReadwriteSplittingDataSourceRouter.java
@@ -91,6 +91,6 @@ public final class ReadwriteSplittingDataSourceRouter {
}
private String routeWithLoadBalancer() {
- return rule.getLoadBalancer().getDataSource(rule.getName(),
rule.getWriteDataSource(), rule.getEnabledReplicaDataSources(),
connectionContext.getTransactionContext());
+ return rule.getLoadBalancer().getDataSource(rule.getName(),
rule.getWriteDataSource(), rule.getEnabledReplicaDataSources());
}
}
diff --git
a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithmTest.java
b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithmTest.java
index 104150c7760..e2510540afa 100644
---
a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithmTest.java
+++
b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithmTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
-import
org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import org.junit.jupiter.api.Test;
@@ -37,16 +36,14 @@ public final class RandomReadQueryLoadBalanceAlgorithmTest {
String readDataSourceName1 = "test_read_ds_1";
String readDataSourceName2 = "test_read_ds_2";
List<String> readDataSourceNames = Arrays.asList(readDataSourceName1,
readDataSourceName2);
- TransactionConnectionContext context = new
TransactionConnectionContext();
- assertRandomReadQueryLoadBalance(readDataSourceNames,
loadBalanceAlgorithm, writeDataSourceName, context);
-
assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, context)));
-
assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, context)));
+ assertRandomReadQueryLoadBalance(readDataSourceNames,
loadBalanceAlgorithm, writeDataSourceName);
+
assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames)));
+
assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames)));
}
- private void assertRandomReadQueryLoadBalance(final List<String>
readDataSourceNames, final ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm,
final String writeDataSourceName,
- final
TransactionConnectionContext context) {
-
assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, context)));
-
assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, context)));
-
assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, context)));
+ private void assertRandomReadQueryLoadBalance(final List<String>
readDataSourceNames, final ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm,
final String writeDataSourceName) {
+
assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames)));
+
assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames)));
+
assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames)));
}
}
diff --git
a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithmTest.java
b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithmTest.java
index 23a07713ad6..b245a1f9929 100644
---
a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithmTest.java
+++
b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithmTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
-import
org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import org.junit.jupiter.api.Test;
@@ -38,15 +37,14 @@ public final class
RoundRobinReadQueryLoadBalanceAlgorithmTest {
String readDataSourceName1 = "test_read_ds_1";
String readDataSourceName2 = "test_read_ds_2";
List<String> readDataSourceNames = Arrays.asList(readDataSourceName1,
readDataSourceName2);
- TransactionConnectionContext context = new
TransactionConnectionContext();
- assertRoundRobinReadQueryLoadBalance(writeDataSourceName,
readDataSourceName1, readDataSourceName2, loadBalanceAlgorithm,
readDataSourceNames, context);
+ assertRoundRobinReadQueryLoadBalance(writeDataSourceName,
readDataSourceName1, readDataSourceName2, loadBalanceAlgorithm,
readDataSourceNames);
}
private void assertRoundRobinReadQueryLoadBalance(final String
writeDataSourceName, final String readDataSourceName1, final String
readDataSourceName2,
- final
ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm, final List<String>
readDataSourceNames, final TransactionConnectionContext context) {
- assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, context), is(readDataSourceName1));
- assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, context), is(readDataSourceName2));
- assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, context), is(readDataSourceName1));
- assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, context), is(readDataSourceName2));
+ final
ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm, final List<String>
readDataSourceNames) {
+ assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames), is(readDataSourceName1));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames), is(readDataSourceName2));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames), is(readDataSourceName1));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames), is(readDataSourceName2));
}
}
diff --git
a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithmTest.java
b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithmTest.java
index 4d39d150297..bb38cfbe7f4 100644
---
a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithmTest.java
+++
b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithmTest.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
-import
org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
@@ -37,7 +36,7 @@ public final class WeightReadQueryLoadBalanceAlgorithmTest {
@Test
public void assertGetSingleReadDataSource() {
ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm =
TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "WEIGHT",
PropertiesBuilder.build(new Property("test_read_ds_1", "5")));
- assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds",
Collections.singletonList("test_read_ds_1"), new
TransactionConnectionContext()), is("test_read_ds_1"));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds",
Collections.singletonList("test_read_ds_1")), is("test_read_ds_1"));
}
@Test
@@ -52,16 +51,16 @@ public final class WeightReadQueryLoadBalanceAlgorithmTest {
}
private void assertWeightReadQueryLoadBalance(final
ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm, final String
writeDataSourceName, final List<String> readDataSourceNames) {
- assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, new TransactionConnectionContext()),
notNullValue());
- assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, new TransactionConnectionContext()),
notNullValue());
- assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames, new TransactionConnectionContext()),
notNullValue());
+ assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames), notNullValue());
+ assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames), notNullValue());
+ assertThat(loadBalanceAlgorithm.getDataSource("ds",
writeDataSourceName, readDataSourceNames), notNullValue());
}
@Test
public void assertGetDataSourceWhenReadDataSourceChanged() {
ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm =
TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class,
"WEIGHT", PropertiesBuilder.build(new
Property("test_read_ds_1", "5"), new Property("test_read_ds_2", "5")));
- loadBalanceAlgorithm.getDataSource("ds", "test_write_ds",
Arrays.asList("test_read_ds_1", "test_read_ds_1"), new
TransactionConnectionContext());
- assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds",
Collections.singletonList("test_read_ds_1"), new
TransactionConnectionContext()), is("test_read_ds_1"));
+ loadBalanceAlgorithm.getDataSource("ds", "test_write_ds",
Arrays.asList("test_read_ds_1", "test_read_ds_1"));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds",
Collections.singletonList("test_read_ds_1")), is("test_read_ds_1"));
}
}