You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/02/07 10:44:17 UTC
[shardingsphere] branch master updated: Reduce the number of readwrite-splitting algorithms(#23974) (#24014)
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 db0b6f356d8 Reduce the number of readwrite-splitting algorithms(#23974) (#24014)
db0b6f356d8 is described below
commit db0b6f356d8cbff80ec8cd37165c14d471397e77
Author: ZhangCheng <fl...@outlook.com>
AuthorDate: Tue Feb 7 18:44:00 2023 +0800
Reduce the number of readwrite-splitting algorithms(#23974) (#24014)
* Reduce the number of readwrite-splitting algorithms(#23974)
* Rename transaction aware name(#23974)
* Modify test(#23974)
* Update loadBalance algorithm(#23974)
* Update loadBalance algorithm(#23974)
---
.../content/dev-manual/readwrite-splitting.cn.md | 6 -
.../content/dev-manual/readwrite-splitting.en.md | 6 -
.../builtin-algorithm/load-balance.cn.md | 83 +++----------
.../builtin-algorithm/load-balance.en.md | 83 +++----------
.../transaction/TransactionReadQueryStrategy.java} | 17 +--
.../TransactionReadQueryStrategyAware.java} | 19 +--
...ReplicaRandomReadQueryLoadBalanceAlgorithm.java | 46 --------
...icaRoundRobinReadQueryLoadBalanceAlgorithm.java | 48 --------
...ReplicaWeightReadQueryLoadBalanceAlgorithm.java | 130 --------------------
.../RandomReadQueryLoadBalanceAlgorithm.java | 24 +++-
.../RoundRobinReadQueryLoadBalanceAlgorithm.java | 24 +++-
...sactionRandomReadQueryLoadBalanceAlgorithm.java | 40 -------
...ionRoundRobinReadQueryLoadBalanceAlgorithm.java | 42 -------
...sactionWeightReadQueryLoadBalanceAlgorithm.java | 131 ---------------------
.../WeightReadQueryLoadBalanceAlgorithm.java | 27 ++++-
...ReadwriteSplittingRuleConfigurationChecker.java | 10 +-
.../TransactionReadQueryStrategyUtil.java | 57 +++++++++
...ritesplitting.spi.ReadQueryLoadBalanceAlgorithm | 6 -
...icaRandomReadQueryLoadBalanceAlgorithmTest.java | 62 ----------
...oundRobinReadQueryLoadBalanceAlgorithmTest.java | 63 ----------
...icaWeightReadQueryLoadBalanceAlgorithmTest.java | 99 ----------------
.../RandomReadQueryLoadBalanceAlgorithmTest.java | 70 +++++++++--
...oundRobinReadQueryLoadBalanceAlgorithmTest.java | 58 ++++++++-
...ionRandomReadQueryLoadBalanceAlgorithmTest.java | 44 -------
...oundRobinReadQueryLoadBalanceAlgorithmTest.java | 57 ---------
...ionWeightReadQueryLoadBalanceAlgorithmTest.java | 62 ----------
.../WeightReadQueryLoadBalanceAlgorithmTest.java | 59 +++++++++-
27 files changed, 351 insertions(+), 1022 deletions(-)
diff --git a/docs/document/content/dev-manual/readwrite-splitting.cn.md b/docs/document/content/dev-manual/readwrite-splitting.cn.md
index e8d5e044fe6..c205da7ba56 100644
--- a/docs/document/content/dev-manual/readwrite-splitting.cn.md
+++ b/docs/document/content/dev-manual/readwrite-splitting.cn.md
@@ -22,9 +22,3 @@ chapter = true
| ROUND_ROBIN | 基于轮询的读库负载均衡算法 | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.RoundRobinReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RoundRobinReadQueryLoadBalanceAlgorithm.java) |
| RANDOM | 基于随机的读库负载均衡算法 | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.RandomReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReadQueryLoadBalanceAlgorithm.java) |
| WEIGHT | 基于权重的读库负载均衡算法 | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.WeightReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java) |
-| TRANSACTION_RANDOM | 无论是否在事务中,读请求采用随机策略路由到多个读库 | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.TransactionRandomReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithm.java) |
-| TRANSACTION_ROUND_ROBIN | 无论是否在事务中,读请求采用轮询策略路由到多个读库 | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.TransactionRoundRobinReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithm.java) |
-| TRANSACTION_WEIGHT | 无论是否在事务中,读请求采用权重策略路由到多个读库 | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.TransactionWeightReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java) |
-| FIXED_REPLICA_RANDOM | 显式开启事务,读请求采用随机策略路由到一个固定读库;不开事务,每次读流量使用指定算法路由到不同的读库 | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.FixedReplicaRandomReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithm.java) |
-| FIXED_REPLICA_ROUND_ROBIN | 显式开启事务,读请求采用轮询策略路由到一个固定读库;不开事务,每次读流量使用指定算法路由到不同的读库 | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm.java) |
-| FIXED_REPLICA_WEIGHT | 显式开启事务,读请求采用权重策略路由到多个读库;不开事务,每次读流量使用指定算法路由到不同的读库 | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.FixedReplicaWeightReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java) |
diff --git a/docs/document/content/dev-manual/readwrite-splitting.en.md b/docs/document/content/dev-manual/readwrite-splitting.en.md
index 24911c18aa4..961af3d225f 100644
--- a/docs/document/content/dev-manual/readwrite-splitting.en.md
+++ b/docs/document/content/dev-manual/readwrite-splitting.en.md
@@ -22,9 +22,3 @@ Read query load balance algorithm's definition
| ROUND_ROBIN | the read database load balancer algorithm based on polling | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.RoundRobinReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwrite [...]
| RANDOM | the read database load balancer algorithm based on random | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.RandomReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritespli [...]
| WEIGHT | the read database load balancer algorithm based on weight | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.WeightReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritespli [...]
-| TRANSACTION_RANDOM | Whether in a transaction or not, read requests are routed to multiple replicas using a random strategy | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.TransactionRandomReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/re [...]
-| TRANSACTION_ROUND_ROBIN | Whether in a transaction or not, read requests are routed to multiple replicas using a round-robin strategy | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.TransactionRoundRobinReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingspher [...]
-| TRANSACTION_WEIGHT | Whether in a transaction or not, read requests are routed to multiple replicas using a weight strategy | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.TransactionWeightReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/re [...]
-| FIXED_REPLICA_RANDOM | Open transaction, and the read request is routed to a fixed replica using a random strategy; if the transaction is not opened, each read traffic is routed to a different replica using the specified algorithm | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.FixedReplicaRandomReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/r [...]
-| FIXED_REPLICA_ROUND_ROBIN | Open transaction, and the read request is routed to a fixed replica using a round-robin strategy; if the transaction is not opened, each read traffic is routed to a different replica using the specified algorithm | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphe [...]
-| FIXED_REPLICA_WEIGHT | Open transaction, and the read request is routed to a fixed replica using a weight strategy; if the transaction is not opened, each read traffic is routed to a different replica using the specified algorithm | [`org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.FixedReplicaWeightReadQueryLoadBalanceAlgorithm`](https://github.com/apache/shardingsphere/blob/master/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/r [...]
diff --git a/docs/document/content/user-manual/common-config/builtin-algorithm/load-balance.cn.md b/docs/document/content/user-manual/common-config/builtin-algorithm/load-balance.cn.md
index 655d583ebcd..bf4e1cfa478 100644
--- a/docs/document/content/user-manual/common-config/builtin-algorithm/load-balance.cn.md
+++ b/docs/document/content/user-manual/common-config/builtin-algorithm/load-balance.cn.md
@@ -13,85 +13,38 @@ ShardingSphere 内置提供了多种负载均衡算法,具体包括了轮询
类型:ROUND_ROBIN
-说明:事务内,读请求路由到 primary,事务外,采用轮询策略路由到 replica。
-
-可配置属性:无
-
-### 随机负载均衡算法
-
-类型:RANDOM
-
-说明:事务内,读请求路由到 primary,事务外,采用随机策略路由到 replica。
-
-可配置属性:无
-
-### 权重负载均衡算法
-
-类型:WEIGHT
-
-说明:事务内,读请求路由到 primary,事务外,采用权重策略路由到 replica。
+说明:事务内,读请求根据 `transactionReadQueryStrategy` 属性的配置进行路由。事务外,采用轮询策略路由到 replica。
可配置属性:
-| *属性名称* | *数据类型* | *说明* |
-| -------------- | -------- |--------------------------------------------------------------|
-| ${replica-name} | double | 属性名使用读库名称,参数填写读库对应的权重值。权重参数范围最小值 > 0,合计 <= Double.MAX_VALUE。 |
-
-### 事务随机负载均衡算法
-
-类型:TRANSACTION_RANDOM
-
-说明:显式/非显式开启事务,读请求采用随机策略路由到多个 replica。
-
-可配置属性:无
+| *属性名称* | *数据类型* | *说明* |
+| -------------- |--------|-------------------------------------------------------------------------------------------------------------------------------------------|
+| transactionReadQueryStrategy | String | 事务内读请求路由策略,可选值:FIXED_PRIMARY(路由到 primary)、FIXED_REPLICA(根据轮询策略选择一个固定的 replica)、DYNAMIC_REPLICA(根据轮询策略路由到不同的 replica),默认值:FIXED_PRIMARY。 |
-### 事务轮询负载均衡算法
-
-类型:TRANSACTION_ROUND_ROBIN
-
-说明:显式/非显式开启事务,读请求采用轮询策略路由到多个 replica。
-
-可配置属性:无
-
-### 事务权重负载均衡算法
+### 随机负载均衡算法
-类型:TRANSACTION_WEIGHT
+类型:RANDOM
-说明:显式/非显式开启事务,读请求采用权重策略路由到多个 replica。
+说明:事务内,读请求根据 `transactionReadQueryStrategy` 属性的配置进行路由。事务外,采用随机策略路由到 replica。
可配置属性:
-| *属性名称* | *数据类型* | *说明* |
-| -------------- | -------- |--------------------------------------------------------------|
-| ${replica-name} | double | 属性名使用读库名称,参数填写读库对应的权重值。权重参数范围最小值 > 0,合计 <= Double.MAX_VALUE。 |
-
-### 固定副本随机负载均衡算法
-
-类型:FIXED_REPLICA_RANDOM
+| *属性名称* | *数据类型* | *说明* |
+| -------------- |--------|-----------------------------------------------------------------------------------------------------------------------------------------|
+| transactionReadQueryStrategy | String | 事务内读请求路由策略,可选值:FIXED_PRIMARY(路由到 primary)、FIXED_REPLICA(根据随机策略选择一个固定的 replica)、DYNAMIC_REPLICA(根据随机策略路由到不同的 replica),默认值:FIXED_PRIMARY。 |
-说明:显式开启事务,读请求采用随机策略路由到一个固定 replica;不开事务,每次读流量使用随机策略路由到不同的 replica。
-
-可配置属性:无
-
-### 固定副本轮询负载均衡算法
-
-类型:FIXED_REPLICA_ROUND_ROBIN
-
-说明:显式开启事务,读请求采用轮询策略路由到一个固定 replica;不开事务,每次读流量使用轮询策略路由到不同的 replica。
-
-可配置属性:无
-
-### 固定副本权重负载均衡算法
+### 权重负载均衡算法
-类型:FIXED_REPLICA_WEIGHT
+类型:WEIGHT
-说明:显式开启事务,读请求采用权重策略路由到一个固定 replica;不开事务,每次读流量使用权重策略路由到不同的 replica。
+说明:事务内,读请求根据 `transactionReadQueryStrategy` 属性的配置进行路由。事务外,采用权重策略路由到 replica。
可配置属性:
-| *属性名称* | *数据类型* | *说明* |
-| -------------- | -------- |--------------------------------------------------------------|
-| ${replica-name} | double | 属性名使用读库名称,参数填写读库对应的权重值。权重参数范围最小值 > 0,合计 <= Double.MAX_VALUE。 |
+| *属性名称* | *数据类型* | *说明* |
+| -------------- |--------|-----------------------------------------------------------------------------------------------------------------------------------------|
+| ${replica-name} | double | 属性名使用读库名称,参数填写读库对应的权重值。权重参数范围最小值 > 0,合计 <= Double.MAX_VALUE。 |
+| transactionReadQueryStrategy | String | 事务内读请求路由策略,可选值:FIXED_PRIMARY(路由到 primary)、FIXED_REPLICA(根据权重策略选择一个固定的 replica)、DYNAMIC_REPLICA(根据权重策略路由到不同的 replica),默认值:FIXED_PRIMARY。 |
## 操作步骤
@@ -113,6 +66,8 @@ rules:
loadBalancers:
random:
type: RANDOM
+ props:
+ transactionReadQueryStrategy: FIXED_PRIMARY
```
## 相关参考
diff --git a/docs/document/content/user-manual/common-config/builtin-algorithm/load-balance.en.md b/docs/document/content/user-manual/common-config/builtin-algorithm/load-balance.en.md
index 78923b12e36..62e2e40bf0a 100644
--- a/docs/document/content/user-manual/common-config/builtin-algorithm/load-balance.en.md
+++ b/docs/document/content/user-manual/common-config/builtin-algorithm/load-balance.en.md
@@ -15,85 +15,38 @@ Moreover, considering the complexity of the business scenario, the built-in algo
Type: ROUND_ROBIN
-Description: Within the transaction, read query are routed to the primary, and outside the transaction, the round-robin strategy is used to route to the replica.
+Description: Within the transaction, read query are routed according to the configuration of the `transactionReadQueryStrategy` property, and outside the transaction, the round-robin strategy is used to route to the replica.
-Attributes: None
+Attributes:
+
+| *Name* | *DataType* | *Description* |
+| -------------- |------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| transactionReadQueryStrategy | String | Routing strategy for read query within a transaction, optional values: FIXED_PRIMARY (route to primary), FIXED_REPLICA (select a fixed replica according to the round-robin strategy), DYNAMIC_REPLICA (route to different replicas according to the round-robin strategy), default value: FIXED_PRIMARY. |
### Random Load Balance Algorithm
Type: RANDOM
-Description: Within the transaction, read query are routed to the primary, and outside the transaction, the random strategy is used to route to the replica.
+Description: Within the transaction, read query are routed according to the configuration of the `transactionReadQueryStrategy` property, and outside the transaction, the random strategy is used to route to the replica.
+
+Attributes:
-Attributes: None
+| *Name* | *DataType* | *Description* |
+| -------------- |------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| transactionReadQueryStrategy | String | Routing strategy for read query within a transaction, optional values: FIXED_PRIMARY (route to primary), FIXED_REPLICA (select a fixed replica according to the random strategy), DYNAMIC_REPLICA (route to different replicas according to the random strategy), default value: FIXED_PRIMARY. |
### Weight Load Balance Algorithm
Type: WEIGHT
-Description: Within the transaction, read query are routed to the primary, and outside the transaction, the weight strategy is used to route to the replica.
-
-Attributes:
-
-| *Name* | *DataType* | *Description* |
-| -------------- |------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| ${replica-name} | double | Attribute name uses the name of the replica, and the parameter fills in the weight value corresponding to the replica. Weight parameter range min > 0, total <= Double.MAX_VALUE. |
-
-### Transaction Random Load Balance Algorithm
-
-Type: TRANSACTION_RANDOM
-
-Description: Display/non-display open transaction, read query are routed to multiple replicas using random strategy.
-
-Attributes: None
-
-### Transaction Round-robin Load Balance Algorithm
-
-Type: TRANSACTION_ROUND_ROBIN
-
-Description: Display/non-display open transaction, read query are routed to multiple replicas using round-robin strategy.
-
-Attributes: None
-
-### Transaction Weight Load Balance Algorithm
-
-Type: TRANSACTION_WEIGHT
-
-Description: Display/non-display open transaction, read query are routed to multiple replicas using weight strategy.
-
-Attributes:
-
-| *Name* | *DataType* | *Description* |
-| -------------- |------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| ${replica-name} | double | Attribute name uses the name of the replica, and the parameter fills in the weight value corresponding to the replica. Weight parameter range min > 0, total <= Double.MAX_VALUE. |
-
-### Fixed Replica Random Load Balance Algorithm
-
-Type: FIXED_REPLICA_RANDOM
-
-Description: Open transaction displayed, and the read query is routed to a fixed replica using random strategy; otherwise, each read traffic is routed to a different replica using random strategy.
-
-Attributes: None
-
-### Fixed Replica Round-robin Load Balance Algorithm
-
-Type: FIXED_REPLICA_ROUND_ROBIN
-
-Description: Open transaction displayed, and the read query is routed to a fixed replica using round-robin strategy; otherwise, each read traffic is routed to a different replica using round-robin strategy.
-
-Attributes: None
-
-### Fixed Replica Weight Load Balance Algorithm
-
-Type: FIXED_REPLICA_WEIGHT
-
-Description: Open transaction displayed, and the read query is routed to a fixed replica using weight strategy; otherwise, each read traffic is routed to a different replica using weight strategy.
+Description: Within the transaction, read query are routed according to the configuration of the `transactionReadQueryStrategy` property, and outside the transaction, the weight strategy is used to route to the replica.
Attributes:
-| *Name* | *DataType* | *Description* |
-| -------------- |------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| ${replica-name} | double | Attribute name uses the name of the replica, and the parameter fills in the weight value corresponding to the replica. Weight parameter range min > 0, total <= Double.MAX_VALUE. |
+| *Name* | *DataType* | *Description* |
+| -------------- |------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| ${replica-name} | double | Attribute name uses the name of the replica, and the parameter fills in the weight value corresponding to the replica. Weight parameter range min > 0, total <= Double.MAX_VALUE. |
+| transactionReadQueryStrategy | String | Routing strategy for read query within a transaction, optional values: FIXED_PRIMARY (route to primary), FIXED_REPLICA (select a fixed replica according to the weight strategy), DYNAMIC_REPLICA (route to different replicas according to the weight strategy), default value: FIXED_PRIMARY. |
## Procedure
@@ -115,6 +68,8 @@ rules:
loadBalancers:
random:
type: RANDOM
+ props:
+ transactionReadQueryStrategy: FIXED_PRIMARY
```
## Related References
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightAware.java b/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/api/transaction/TransactionReadQueryStrategy.java
similarity index 66%
copy from features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightAware.java
copy to features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/api/transaction/TransactionReadQueryStrategy.java
index d91fb946773..211dfa85681 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightAware.java
+++ b/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/api/transaction/TransactionReadQueryStrategy.java
@@ -15,20 +15,11 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
-
-import java.util.Collection;
+package org.apache.shardingsphere.readwritesplitting.api.transaction;
/**
- * Weight aware.
+ * Transaction read query strategy.
*/
-// TODO should remove after merge TransactionWeightReadQueryLoadBalanceAlgorithm and WeightReadQueryLoadBalanceAlgorithm
-public interface WeightAware {
-
- /**
- * Get data source names.
- *
- * @return data source names
- */
- Collection<String> getDataSourceNames();
+public enum TransactionReadQueryStrategy {
+ FIXED_PRIMARY, FIXED_REPLICA, DYNAMIC_REPLICA
}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightAware.java b/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/api/transaction/TransactionReadQueryStrategyAware.java
similarity index 62%
rename from features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightAware.java
rename to features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/api/transaction/TransactionReadQueryStrategyAware.java
index d91fb946773..d0e7cbfd1e2 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightAware.java
+++ b/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/api/transaction/TransactionReadQueryStrategyAware.java
@@ -15,20 +15,23 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
+package org.apache.shardingsphere.readwritesplitting.api.transaction;
-import java.util.Collection;
+import java.util.List;
/**
- * Weight aware.
+ * Transaction read query strategy aware.
*/
-// TODO should remove after merge TransactionWeightReadQueryLoadBalanceAlgorithm and WeightReadQueryLoadBalanceAlgorithm
-public interface WeightAware {
+public interface TransactionReadQueryStrategyAware {
+
+ String TRANSACTION_READ_QUERY_STRATEGY = "transactionReadQueryStrategy";
/**
- * Get data source names.
+ * Get data source name.
*
- * @return data source names
+ * @param name name
+ * @param readDataSourceNames names of read data sources
+ * @return name of selected data source
*/
- Collection<String> getDataSourceNames();
+ String getDataSourceName(String name, List<String> readDataSourceNames);
}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithm.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithm.java
deleted file mode 100644
index b3beab3d1cd..00000000000
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithm.java
+++ /dev/null
@@ -1,46 +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.readwritesplitting.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
-import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
-
-import java.util.List;
-import java.util.concurrent.ThreadLocalRandom;
-
-/**
- * Fixed replica random read query load-balance algorithm.
- */
-public final class FixedReplicaRandomReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm {
-
- @Override
- public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames, final TransactionConnectionContext context) {
- if (context.isInTransaction()) {
- if (null == context.getReadWriteSplitReplicaRoute()) {
- context.setReadWriteSplitReplicaRoute(readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size())));
- }
- return context.getReadWriteSplitReplicaRoute();
- }
- return readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size()));
- }
-
- @Override
- public String getType() {
- return "FIXED_REPLICA_RANDOM";
- }
-}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm.java
deleted file mode 100644
index 3d0023eb73e..00000000000
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm.java
+++ /dev/null
@@ -1,48 +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.readwritesplitting.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
-import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
-
-import java.util.List;
-import java.util.concurrent.atomic.AtomicInteger;
-
-/**
- * Fixed replica round-robin read query load-balance algorithm.
- */
-public final class FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm {
-
- private final AtomicInteger count = new AtomicInteger(0);
-
- @Override
- public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames, final TransactionConnectionContext context) {
- if (context.isInTransaction()) {
- if (null == context.getReadWriteSplitReplicaRoute()) {
- context.setReadWriteSplitReplicaRoute(readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size()));
- }
- return context.getReadWriteSplitReplicaRoute();
- }
- return readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size());
- }
-
- @Override
- public String getType() {
- return "FIXED_REPLICA_ROUND_ROBIN";
- }
-}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java
deleted file mode 100644
index 4978fb84c11..00000000000
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithm.java
+++ /dev/null
@@ -1,130 +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.readwritesplitting.algorithm.loadbalance;
-
-import com.google.common.base.Preconditions;
-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;
-import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.Properties;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ThreadLocalRandom;
-
-/**
- * Fixed replica weight read query load-balance algorithm.
- */
-public final class FixedReplicaWeightReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm {
-
- private static final double ACCURACY_THRESHOLD = 0.0001;
-
- private static final ConcurrentHashMap<String, double[]> WEIGHT_MAP = new ConcurrentHashMap<>();
-
- private Properties props;
-
- @Override
- public void init(final Properties props) {
- this.props = props;
- }
-
- @Override
- public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames, final TransactionConnectionContext context) {
- double[] weight = WEIGHT_MAP.containsKey(name) && WEIGHT_MAP.get(name).length == readDataSourceNames.size() ? WEIGHT_MAP.get(name) : initWeight(readDataSourceNames);
- WEIGHT_MAP.put(name, weight);
- if (context.isInTransaction()) {
- if (null == context.getReadWriteSplitReplicaRoute()) {
- context.setReadWriteSplitReplicaRoute(getDataSourceName(readDataSourceNames, weight));
- }
- return context.getReadWriteSplitReplicaRoute();
- }
- return getDataSourceName(readDataSourceNames, weight);
- }
-
- private String getDataSourceName(final List<String> readDataSourceNames, final double[] weight) {
- double randomWeight = ThreadLocalRandom.current().nextDouble(0, 1);
- int index = Arrays.binarySearch(weight, randomWeight);
- if (index < 0) {
- index = -index - 1;
- return index < weight.length && randomWeight < weight[index] ? readDataSourceNames.get(index) : readDataSourceNames.get(readDataSourceNames.size() - 1);
- }
- return readDataSourceNames.get(index);
- }
-
- private double[] initWeight(final List<String> readDataSourceNames) {
- double[] result = getWeights(readDataSourceNames);
- Preconditions.checkState(0 == result.length || !(Math.abs(result[result.length - 1] - 1.0D) >= ACCURACY_THRESHOLD),
- "The cumulative weight is calculated incorrectly, and the sum of the probabilities is not equal to 1");
- return result;
- }
-
- private double[] getWeights(final List<String> readDataSourceNames) {
- double[] exactWeights = new double[readDataSourceNames.size()];
- int index = 0;
- double sum = 0D;
- for (String readDataSourceName : readDataSourceNames) {
- double weight = getWeightValue(readDataSourceName);
- exactWeights[index++] = weight;
- sum += weight;
- }
- for (int i = 0; i < index; i++) {
- if (exactWeights[i] <= 0) {
- continue;
- }
- exactWeights[i] = exactWeights[i] / sum;
- }
- return calcWeight(exactWeights);
- }
-
- private double[] calcWeight(final double[] exactWeights) {
- double[] result = new double[exactWeights.length];
- double randomRange = 0D;
- for (int i = 0; i < result.length; i++) {
- result[i] = randomRange + exactWeights[i];
- randomRange += exactWeights[i];
- }
- return result;
- }
-
- 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)));
- double result;
- try {
- result = Double.parseDouble(weightObject.toString());
- } catch (final NumberFormatException ex) {
- throw new InvalidReadDatabaseWeightException(weightObject);
- }
- if (Double.isInfinite(result)) {
- result = 10000.0D;
- }
- if (Double.isNaN(result)) {
- result = 1.0D;
- }
- return result;
- }
-
- @Override
- public String getType() {
- return "FIXED_REPLICA_WEIGHT";
- }
-}
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 d4723e55b4c..91e90283935 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
@@ -18,26 +18,44 @@
package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
+import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionReadQueryStrategy;
+import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionReadQueryStrategyAware;
import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
+import org.apache.shardingsphere.readwritesplitting.transaction.TransactionReadQueryStrategyUtil;
import java.util.List;
+import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;
/**
* Random read query load-balance algorithm.
*/
-public final class RandomReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm {
+public final class RandomReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm, TransactionReadQueryStrategyAware {
+
+ private TransactionReadQueryStrategy transactionReadQueryStrategy;
+
+ @Override
+ public void init(final Properties props) {
+ transactionReadQueryStrategy = props.containsKey(TRANSACTION_READ_QUERY_STRATEGY)
+ ? TransactionReadQueryStrategy.valueOf(props.getProperty(TRANSACTION_READ_QUERY_STRATEGY))
+ : TransactionReadQueryStrategy.FIXED_PRIMARY;
+ }
@Override
public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames, final TransactionConnectionContext context) {
if (context.isInTransaction()) {
- return writeDataSourceName;
+ return TransactionReadQueryStrategyUtil.routeInTransaction(name, writeDataSourceName, readDataSourceNames, context, transactionReadQueryStrategy, this);
}
- return readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size()));
+ return getDataSourceName(name, readDataSourceNames);
}
@Override
public String getType() {
return "RANDOM";
}
+
+ @Override
+ public String getDataSourceName(final String name, 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 d3c652ca464..04bf63fb1ff 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
@@ -18,24 +18,37 @@
package org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance;
import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
+import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionReadQueryStrategyAware;
+import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionReadQueryStrategy;
import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
+import org.apache.shardingsphere.readwritesplitting.transaction.TransactionReadQueryStrategyUtil;
import java.util.List;
+import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Round-robin read query load-balance algorithm.
*/
-public final class RoundRobinReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm {
+public final class RoundRobinReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm, TransactionReadQueryStrategyAware {
private final AtomicInteger count = new AtomicInteger(0);
+ private TransactionReadQueryStrategy transactionReadQueryStrategy;
+
+ @Override
+ public void init(final Properties props) {
+ transactionReadQueryStrategy = props.containsKey(TRANSACTION_READ_QUERY_STRATEGY)
+ ? TransactionReadQueryStrategy.valueOf(props.getProperty(TRANSACTION_READ_QUERY_STRATEGY))
+ : TransactionReadQueryStrategy.FIXED_PRIMARY;
+ }
+
@Override
public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames, final TransactionConnectionContext context) {
if (context.isInTransaction()) {
- return writeDataSourceName;
+ return TransactionReadQueryStrategyUtil.routeInTransaction(name, writeDataSourceName, readDataSourceNames, context, transactionReadQueryStrategy, this);
}
- return readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size());
+ return getDataSourceName(name, readDataSourceNames);
}
@Override
@@ -47,4 +60,9 @@ public final class RoundRobinReadQueryLoadBalanceAlgorithm implements ReadQueryL
public boolean isDefault() {
return true;
}
+
+ @Override
+ public String getDataSourceName(final String name, 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/TransactionRandomReadQueryLoadBalanceAlgorithm.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithm.java
deleted file mode 100644
index e33e15e6b19..00000000000
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithm.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.readwritesplitting.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
-import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
-
-import java.util.List;
-import java.util.concurrent.ThreadLocalRandom;
-
-/**
- * Transaction random read query load-balance algorithm.
- */
-public final class TransactionRandomReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm {
-
- @Override
- public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames, final TransactionConnectionContext context) {
- return readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size()));
- }
-
- @Override
- public String getType() {
- return "TRANSACTION_RANDOM";
- }
-}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithm.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithm.java
deleted file mode 100644
index 3d0497c1761..00000000000
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithm.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.readwritesplitting.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
-import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
-
-import java.util.List;
-import java.util.concurrent.atomic.AtomicInteger;
-
-/**
- * Transaction round-robin read query load-balance algorithm.
- */
-public final class TransactionRoundRobinReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm {
-
- private final AtomicInteger count = new AtomicInteger(0);
-
- @Override
- public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames, final TransactionConnectionContext context) {
- return readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size());
- }
-
- @Override
- public String getType() {
- return "TRANSACTION_ROUND_ROBIN";
- }
-}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java
deleted file mode 100644
index 6553b6de9d7..00000000000
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithm.java
+++ /dev/null
@@ -1,131 +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.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;
-import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ThreadLocalRandom;
-
-/**
- * Transaction weight read query load-balance algorithm.
- */
-public final class TransactionWeightReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm, WeightAware {
-
- private static final double ACCURACY_THRESHOLD = 0.0001;
-
- private final Map<String, double[]> weightMap = new ConcurrentHashMap<>();
-
- private Properties props;
-
- @Getter
- private Collection<String> dataSourceNames;
-
- @Override
- public void init(final Properties props) {
- this.props = props;
- dataSourceNames = props.stringPropertyNames();
- }
-
- @Override
- public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames, final TransactionConnectionContext context) {
- double[] weight = weightMap.containsKey(name) ? weightMap.get(name) : initWeight(readDataSourceNames);
- weightMap.putIfAbsent(name, weight);
- return getDataSourceNames(readDataSourceNames, weight);
- }
-
- private String getDataSourceNames(final List<String> readDataSourceNames, final double[] weight) {
- double randomWeight = ThreadLocalRandom.current().nextDouble(0, 1);
- int index = Arrays.binarySearch(weight, randomWeight);
- if (index < 0) {
- index = -index - 1;
- return index < weight.length && randomWeight < weight[index] ? readDataSourceNames.get(index) : readDataSourceNames.get(readDataSourceNames.size() - 1);
- }
- return readDataSourceNames.get(index);
- }
-
- private double[] initWeight(final List<String> readDataSourceNames) {
- double[] result = getWeights(readDataSourceNames);
- Preconditions.checkState(0 == result.length || !(Math.abs(result[result.length - 1] - 1.0D) >= ACCURACY_THRESHOLD),
- "The cumulative weight is calculated incorrectly, and the sum of the probabilities is not equal to 1");
- return result;
- }
-
- private double[] getWeights(final List<String> readDataSourceNames) {
- double[] exactWeights = new double[readDataSourceNames.size()];
- int index = 0;
- double sum = 0D;
- for (String readDataSourceName : readDataSourceNames) {
- double weight = getWeightValue(readDataSourceName);
- exactWeights[index++] = weight;
- sum += weight;
- }
- for (int i = 0; i < index; i++) {
- if (exactWeights[i] <= 0) {
- continue;
- }
- exactWeights[i] = exactWeights[i] / sum;
- }
- return calcWeight(exactWeights);
- }
-
- private double[] calcWeight(final double[] exactWeights) {
- double[] result = new double[exactWeights.length];
- double randomRange = 0D;
- for (int i = 0; i < result.length; i++) {
- result[i] = randomRange + exactWeights[i];
- randomRange += exactWeights[i];
- }
- return result;
- }
-
- 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)));
- double result;
- try {
- result = Double.parseDouble(weightObject.toString());
- } catch (final NumberFormatException ex) {
- throw new InvalidReadDatabaseWeightException(weightObject);
- }
- if (Double.isInfinite(result)) {
- result = 10000.0D;
- }
- if (Double.isNaN(result)) {
- result = 1.0D;
- }
- return result;
- }
-
- @Override
- public String getType() {
- return "TRANSACTION_WEIGHT";
- }
-}
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 a96d09edb43..5e3f2eefa77 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
@@ -21,9 +21,12 @@ 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.api.transaction.TransactionReadQueryStrategyAware;
+import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionReadQueryStrategy;
import org.apache.shardingsphere.readwritesplitting.exception.algorithm.InvalidReadDatabaseWeightException;
import org.apache.shardingsphere.readwritesplitting.exception.algorithm.MissingRequiredReadDatabaseWeightException;
import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
+import org.apache.shardingsphere.readwritesplitting.transaction.TransactionReadQueryStrategyUtil;
import java.util.Arrays;
import java.util.Collection;
@@ -32,11 +35,12 @@ import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
+import java.util.stream.Collectors;
/**
* Weight read query load-balance algorithm.
*/
-public final class WeightReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm, WeightAware {
+public final class WeightReadQueryLoadBalanceAlgorithm implements ReadQueryLoadBalanceAlgorithm, TransactionReadQueryStrategyAware {
private static final double ACCURACY_THRESHOLD = 0.0001;
@@ -44,20 +48,33 @@ public final class WeightReadQueryLoadBalanceAlgorithm implements ReadQueryLoadB
private Properties props;
+ private TransactionReadQueryStrategy transactionReadQueryStrategy;
+
@Getter
private Collection<String> dataSourceNames;
@Override
public void init(final Properties props) {
this.props = props;
- dataSourceNames = props.stringPropertyNames();
+ if (props.containsKey(TRANSACTION_READ_QUERY_STRATEGY)) {
+ transactionReadQueryStrategy = TransactionReadQueryStrategy.valueOf(props.getProperty(TRANSACTION_READ_QUERY_STRATEGY));
+ dataSourceNames = props.stringPropertyNames().stream().filter(each -> !each.equals(TRANSACTION_READ_QUERY_STRATEGY)).collect(Collectors.toList());
+ } else {
+ transactionReadQueryStrategy = TransactionReadQueryStrategy.FIXED_PRIMARY;
+ dataSourceNames = props.stringPropertyNames();
+ }
}
@Override
public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames, final TransactionConnectionContext context) {
if (context.isInTransaction()) {
- return writeDataSourceName;
+ return TransactionReadQueryStrategyUtil.routeInTransaction(name, writeDataSourceName, readDataSourceNames, context, transactionReadQueryStrategy, this);
}
+ return getDataSourceName(name, readDataSourceNames);
+ }
+
+ @Override
+ public String getDataSourceName(final String name, 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);
@@ -95,10 +112,10 @@ public final class WeightReadQueryLoadBalanceAlgorithm implements ReadQueryLoadB
}
exactWeights[i] = exactWeights[i] / sum;
}
- return calcWeight(exactWeights);
+ return calculateWeight(exactWeights);
}
- private double[] calcWeight(final double[] exactWeights) {
+ private double[] calculateWeight(final double[] exactWeights) {
double[] result = new double[exactWeights.length];
double randomRange = 0D;
for (int i = 0; i < result.length; i++) {
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java
index 611bf4333c3..0bb23cbcbc4 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationChecker.java
@@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.rule.identifier.type.DynamicDataSourceCon
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.util.expr.InlineExpressionParser;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.WeightAware;
+import org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.WeightReadQueryLoadBalanceAlgorithm;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.strategy.DynamicReadwriteSplittingStrategyConfiguration;
@@ -131,11 +131,11 @@ public final class ReadwriteSplittingRuleConfigurationChecker implements RuleCon
}
ReadQueryLoadBalanceAlgorithm loadBalancer = loadBalancers.get(each.getLoadBalancerName());
ShardingSpherePreconditions.checkNotNull(loadBalancer, () -> new LoadBalancerAlgorithmNotFoundException(databaseName));
- if (loadBalancer instanceof WeightAware) {
- ShardingSpherePreconditions.checkState(!((WeightAware) loadBalancer).getDataSourceNames().isEmpty(), () -> new MissingRequiredReadDatabaseWeightException(loadBalancer.getType(),
- String.format("Read data source weight config are required in database `%s`", databaseName)));
+ if (loadBalancer instanceof WeightReadQueryLoadBalanceAlgorithm) {
+ ShardingSpherePreconditions.checkState(!((WeightReadQueryLoadBalanceAlgorithm) loadBalancer).getDataSourceNames().isEmpty(),
+ () -> new MissingRequiredReadDatabaseWeightException(loadBalancer.getType(), String.format("Read data source weight config are required in database `%s`", databaseName)));
Collection<String> dataSourceNames = getDataSourceNames(each, rules);
- ((WeightAware) loadBalancer).getDataSourceNames().forEach(dataSourceName -> ShardingSpherePreconditions.checkState(dataSourceNames.contains(dataSourceName),
+ ((WeightReadQueryLoadBalanceAlgorithm) loadBalancer).getDataSourceNames().forEach(dataSourceName -> ShardingSpherePreconditions.checkState(dataSourceNames.contains(dataSourceName),
() -> new InvalidWeightLoadBalancerConfigurationException(databaseName)));
}
}
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/transaction/TransactionReadQueryStrategyUtil.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/transaction/TransactionReadQueryStrategyUtil.java
new file mode 100644
index 00000000000..ccf7642fbea
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/transaction/TransactionReadQueryStrategyUtil.java
@@ -0,0 +1,57 @@
+/*
+ * 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.transaction;
+
+import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
+import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionReadQueryStrategy;
+import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionReadQueryStrategyAware;
+
+import java.util.List;
+
+/**
+ * Transaction read query strategy util.
+ */
+public class TransactionReadQueryStrategyUtil {
+
+ /**
+ * Get data source name in transaction.
+ *
+ * @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
+ * @param readQueryStrategy read query strategy in transaction
+ * @param readQueryStrategyAware Transaction read query strategy aware
+ * @return name of selected data source
+ */
+ public static String routeInTransaction(final String name, final String writeDataSourceName, final List<String> readDataSourceNames, final TransactionConnectionContext context,
+ final TransactionReadQueryStrategy readQueryStrategy, final TransactionReadQueryStrategyAware readQueryStrategyAware) {
+ switch (readQueryStrategy) {
+ case FIXED_REPLICA:
+ if (null == context.getReadWriteSplitReplicaRoute()) {
+ context.setReadWriteSplitReplicaRoute(readQueryStrategyAware.getDataSourceName(name, readDataSourceNames));
+ }
+ return context.getReadWriteSplitReplicaRoute();
+ case DYNAMIC_REPLICA:
+ return readQueryStrategyAware.getDataSourceName(name, readDataSourceNames);
+ case FIXED_PRIMARY:
+ default:
+ return writeDataSourceName;
+ }
+ }
+}
diff --git a/features/readwrite-splitting/core/src/main/resources/META-INF/services/org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm b/features/readwrite-splitting/core/src/main/resources/META-INF/services/org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm
index e29145febfd..7dcea8c82eb 100644
--- a/features/readwrite-splitting/core/src/main/resources/META-INF/services/org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm
+++ b/features/readwrite-splitting/core/src/main/resources/META-INF/services/org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm
@@ -18,9 +18,3 @@
org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.RoundRobinReadQueryLoadBalanceAlgorithm
org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.RandomReadQueryLoadBalanceAlgorithm
org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.WeightReadQueryLoadBalanceAlgorithm
-org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.FixedReplicaRandomReadQueryLoadBalanceAlgorithm
-org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm
-org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.FixedReplicaWeightReadQueryLoadBalanceAlgorithm
-org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.TransactionRandomReadQueryLoadBalanceAlgorithm
-org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.TransactionRoundRobinReadQueryLoadBalanceAlgorithm
-org.apache.shardingsphere.readwritesplitting.algorithm.loadbalance.TransactionWeightReadQueryLoadBalanceAlgorithm
diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithmTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithmTest.java
deleted file mode 100644
index 809b37fe932..00000000000
--- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRandomReadQueryLoadBalanceAlgorithmTest.java
+++ /dev/null
@@ -1,62 +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.readwritesplitting.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertTrue;
-
-public final class FixedReplicaRandomReadQueryLoadBalanceAlgorithmTest {
-
- private final FixedReplicaRandomReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new FixedReplicaRandomReadQueryLoadBalanceAlgorithm();
-
- @Test
- public void assertGetDataSourceInTransaction() {
- String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_replica_ds_1";
- String readDataSourceName2 = "test_replica_ds_2";
- List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
- TransactionConnectionContext context = new TransactionConnectionContext();
- context.setInTransaction(true);
- String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context);
- assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
- }
-
- @Test
- public void assertGetDataSourceWithoutTransaction() {
- String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_replica_ds_1";
- String readDataSourceName2 = "test_replica_ds_2";
- List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
- List<String> noTransactionReadDataSourceNames = new LinkedList<>();
- for (int i = 0; i < 5; i++) {
- String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, new TransactionConnectionContext());
- noTransactionReadDataSourceNames.add(routeDataSource);
- }
- assertTrue(noTransactionReadDataSourceNames.size() > 1);
- }
-}
diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithmTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithmTest.java
deleted file mode 100644
index 8319e274938..00000000000
--- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithmTest.java
+++ /dev/null
@@ -1,63 +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.readwritesplitting.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertTrue;
-
-public final class FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithmTest {
-
- private final FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new FixedReplicaRoundRobinReadQueryLoadBalanceAlgorithm();
-
- @Test
- public void assertGetDataSourceInTransaction() {
- String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_replica_ds_1";
- String readDataSourceName2 = "test_replica_ds_2";
- List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
- TransactionConnectionContext context = new TransactionConnectionContext();
- context.setInTransaction(true);
- String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context);
- assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
- }
-
- @Test
- public void assertGetDataSourceWithoutTransaction() {
- String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_replica_ds_1";
- String readDataSourceName2 = "test_replica_ds_2";
- List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
- List<String> noTransactionReadDataSourceNames = new LinkedList<>();
- for (int i = 0; i < 5; i++) {
- String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, new TransactionConnectionContext());
- noTransactionReadDataSourceNames.add(routeDataSource);
- }
- assertTrue(noTransactionReadDataSourceNames.size() > 1);
- }
-}
diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithmTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithmTest.java
deleted file mode 100644
index 055eb06cd81..00000000000
--- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/FixedReplicaWeightReadQueryLoadBalanceAlgorithmTest.java
+++ /dev/null
@@ -1,99 +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.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;
-import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.internal.configuration.plugins.Plugins;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertTrue;
-
-public final class FixedReplicaWeightReadQueryLoadBalanceAlgorithmTest {
-
- @Before
- @After
- public void reset() throws NoSuchFieldException, IllegalAccessException {
- ((Map<?, ?>) Plugins.getMemberAccessor().get(
- FixedReplicaWeightReadQueryLoadBalanceAlgorithm.class.getDeclaredField("WEIGHT_MAP"), FixedReplicaWeightReadQueryLoadBalanceAlgorithm.class)).clear();
- }
-
- @Test
- public void assertGetSingleReadDataSourceInTransaction() {
- ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(
- ReadQueryLoadBalanceAlgorithm.class, "FIXED_REPLICA_WEIGHT", PropertiesBuilder.build(new Property("test_read_ds_1", "5")));
- TransactionConnectionContext context = new TransactionConnectionContext();
- context.setInTransaction(true);
- String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1"), context);
- assertThat(routeDataSource, is("test_read_ds_1"));
- assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1"), context), is(routeDataSource));
- }
-
- @Test
- public void assertGetMultipleReadDataSourcesWithoutTransaction() {
- ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class,
- "FIXED_REPLICA_WEIGHT", PropertiesBuilder.build(new Property("test_read_ds_1", "5"), new Property("test_read_ds_2", "5")));
- String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_read_ds_1";
- String readDataSourceName2 = "test_read_ds_2";
- List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
- List<String> noTransactionReadDataSourceNames = new LinkedList<>();
- for (int i = 0; i < 5; i++) {
- String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, new TransactionConnectionContext());
- noTransactionReadDataSourceNames.add(routeDataSource);
- }
- assertTrue(noTransactionReadDataSourceNames.size() > 1);
- }
-
- @Test
- public void assertGetMultipleReadDataSourcesInTransaction() {
- ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class,
- "FIXED_REPLICA_WEIGHT", PropertiesBuilder.build(new Property("test_read_ds_1", "5"), new Property("test_read_ds_2", "5")));
- String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_read_ds_1";
- String readDataSourceName2 = "test_read_ds_2";
- List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
- TransactionConnectionContext transactionConnectionContext = new TransactionConnectionContext();
- transactionConnectionContext.setInTransaction(true);
- String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", readDataSourceNames, transactionConnectionContext);
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, transactionConnectionContext), is(routeDataSource));
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, transactionConnectionContext), is(routeDataSource));
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, transactionConnectionContext), is(routeDataSource));
- }
-
- @Test
- public void assertGetDataSourceWhenReadDataSourceChanged() {
- ReadQueryLoadBalanceAlgorithm algorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class,
- "FIXED_REPLICA_WEIGHT", PropertiesBuilder.build(new Property("test_read_ds_1", "1"), new Property("test_read_ds_2", "2")));
- algorithm.getDataSource("ds", "test_write_ds", Arrays.asList("test_read_ds_1", "test_read_ds_1"), new TransactionConnectionContext());
- assertThat(algorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1"), new TransactionConnectionContext()), is("test_read_ds_1"));
- }
-}
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 424a9ac3b2c..625f790db7e 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
@@ -18,37 +18,89 @@
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.api.transaction.TransactionReadQueryStrategyAware;
+import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionReadQueryStrategy;
+import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
+import org.apache.shardingsphere.test.util.PropertiesBuilder;
+import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
+import java.util.Properties;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
public final class RandomReadQueryLoadBalanceAlgorithmTest {
- private final RandomReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new RandomReadQueryLoadBalanceAlgorithm();
+ @Test
+ public void assertGetDataSourceWithDefaultStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "RANDOM", new Properties());
+ String writeDataSourceName = "test_write_ds";
+ 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);
+ context.setInTransaction(true);
+ assertTrue(writeDataSourceName.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
+ assertTrue(writeDataSourceName.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
+ }
+
+ 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)));
+ }
@Test
- public void assertGetDataSourceInTransaction() {
+ public void assertGetDataSourceWithFixedPrimaryStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "RANDOM",
+ PropertiesBuilder.build(new Property(TransactionReadQueryStrategyAware.TRANSACTION_READ_QUERY_STRATEGY, TransactionReadQueryStrategy.FIXED_PRIMARY.name())));
String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_replica_ds_1";
- String readDataSourceName2 = "test_replica_ds_2";
+ 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);
context.setInTransaction(true);
assertTrue(writeDataSourceName.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
+ assertTrue(writeDataSourceName.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
+
}
@Test
- public void assertGetDataSourceNotInTransaction() {
+ public void assertGetDataSourceWithFixedReplicaStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "RANDOM",
+ PropertiesBuilder.build(new Property(TransactionReadQueryStrategyAware.TRANSACTION_READ_QUERY_STRATEGY, TransactionReadQueryStrategy.FIXED_REPLICA.name())));
String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_replica_ds_1";
- String readDataSourceName2 = "test_replica_ds_2";
+ 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);
+ context.setInTransaction(true);
+ String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, 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)));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
+ }
+
+ @Test
+ public void assertGetDataSourceWithDynamicReplicaStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "RANDOM",
+ PropertiesBuilder.build(new Property(TransactionReadQueryStrategyAware.TRANSACTION_READ_QUERY_STRATEGY, TransactionReadQueryStrategy.DYNAMIC_REPLICA.name())));
+ String writeDataSourceName = "test_write_ds";
+ 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);
+ context.setInTransaction(true);
+ assertRandomReadQueryLoadBalance(readDataSourceNames, loadBalanceAlgorithm, writeDataSourceName, context);
}
}
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 e2f4620897d..b7593e33ef3 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
@@ -18,38 +18,86 @@
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.api.transaction.TransactionReadQueryStrategyAware;
+import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionReadQueryStrategy;
+import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
+import org.apache.shardingsphere.test.util.PropertiesBuilder;
+import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
+import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertTrue;
public final class RoundRobinReadQueryLoadBalanceAlgorithmTest {
@Test
- public void assertGetDataSource() {
+ public void assertGetDataSourceWithDefaultStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "ROUND_ROBIN", new Properties());
String writeDataSourceName = "test_write_ds";
String readDataSourceName1 = "test_read_ds_1";
String readDataSourceName2 = "test_read_ds_2";
- RoundRobinReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new RoundRobinReadQueryLoadBalanceAlgorithm();
List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
TransactionConnectionContext context = new TransactionConnectionContext();
+ assertRoundRobinReadQueryLoadBalance(writeDataSourceName, readDataSourceName1, readDataSourceName2, loadBalanceAlgorithm, readDataSourceNames, context);
+ }
+
+ 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));
+ }
+
+ @Test
+ public void assertGetDataSourceWithFixedPrimaryStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "ROUND_ROBIN",
+ PropertiesBuilder.build(new Property(TransactionReadQueryStrategyAware.TRANSACTION_READ_QUERY_STRATEGY, TransactionReadQueryStrategy.FIXED_PRIMARY.name())));
+ String writeDataSourceName = "test_write_ds";
+ 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);
+ context.setInTransaction(true);
+ assertTrue(writeDataSourceName.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
+ assertTrue(writeDataSourceName.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
+ }
+
+ @Test
+ public void assertGetDataSourceWithFixedReplicaStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "ROUND_ROBIN",
+ PropertiesBuilder.build(new Property(TransactionReadQueryStrategyAware.TRANSACTION_READ_QUERY_STRATEGY, TransactionReadQueryStrategy.FIXED_REPLICA.name())));
+ String writeDataSourceName = "test_write_ds";
+ 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);
+ context.setInTransaction(true);
+ String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context);
+ assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
}
@Test
- public void assertGetDataSourceInTransaction() {
+ public void assertGetDataSourceWithDynamicReplicaStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "ROUND_ROBIN",
+ PropertiesBuilder.build(new Property(TransactionReadQueryStrategyAware.TRANSACTION_READ_QUERY_STRATEGY, TransactionReadQueryStrategy.DYNAMIC_REPLICA.name())));
String writeDataSourceName = "test_write_ds";
String readDataSourceName1 = "test_read_ds_1";
String readDataSourceName2 = "test_read_ds_2";
- RoundRobinReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new RoundRobinReadQueryLoadBalanceAlgorithm();
List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
TransactionConnectionContext context = new TransactionConnectionContext();
+ assertRoundRobinReadQueryLoadBalance(writeDataSourceName, readDataSourceName1, readDataSourceName2, loadBalanceAlgorithm, readDataSourceNames, context);
context.setInTransaction(true);
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(writeDataSourceName));
+ assertRoundRobinReadQueryLoadBalance(writeDataSourceName, readDataSourceName1, readDataSourceName2, loadBalanceAlgorithm, readDataSourceNames, context);
}
}
diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithmTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithmTest.java
deleted file mode 100644
index d1a09644202..00000000000
--- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRandomReadQueryLoadBalanceAlgorithmTest.java
+++ /dev/null
@@ -1,44 +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.readwritesplitting.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.List;
-
-import static org.junit.Assert.assertTrue;
-
-public final class TransactionRandomReadQueryLoadBalanceAlgorithmTest {
-
- private final TransactionRandomReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new TransactionRandomReadQueryLoadBalanceAlgorithm();
-
- @Test
- public void assertGetDataSourceInTransaction() {
- String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_replica_ds_1";
- String readDataSourceName2 = "test_replica_ds_2";
- List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
- TransactionConnectionContext context = new TransactionConnectionContext();
- context.setInTransaction(true);
- 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)));
- }
-}
diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithmTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithmTest.java
deleted file mode 100644
index d23f45f52d9..00000000000
--- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionRoundRobinReadQueryLoadBalanceAlgorithmTest.java
+++ /dev/null
@@ -1,57 +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.readwritesplitting.algorithm.loadbalance;
-
-import org.apache.shardingsphere.infra.context.transaction.TransactionConnectionContext;
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
-public final class TransactionRoundRobinReadQueryLoadBalanceAlgorithmTest {
-
- @Test
- public void assertGetDataSource() {
- String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_read_ds_1";
- String readDataSourceName2 = "test_read_ds_2";
- TransactionRoundRobinReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new TransactionRoundRobinReadQueryLoadBalanceAlgorithm();
- List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
- TransactionConnectionContext context = new TransactionConnectionContext();
- 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));
- }
-
- @Test
- public void assertGetDataSourceInTransaction() {
- String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_read_ds_1";
- String readDataSourceName2 = "test_read_ds_2";
- TransactionRoundRobinReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = new TransactionRoundRobinReadQueryLoadBalanceAlgorithm();
- List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
- TransactionConnectionContext context = new TransactionConnectionContext();
- context.setInTransaction(true);
- 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));
- }
-}
diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithmTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithmTest.java
deleted file mode 100644
index 3fac838c1ed..00000000000
--- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/TransactionWeightReadQueryLoadBalanceAlgorithmTest.java
+++ /dev/null
@@ -1,62 +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.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;
-import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.notNullValue;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertTrue;
-
-public final class TransactionWeightReadQueryLoadBalanceAlgorithmTest {
-
- @Test
- public void assertGetSingleReadDataSource() {
- ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(
- ReadQueryLoadBalanceAlgorithm.class, "TRANSACTION_WEIGHT", PropertiesBuilder.build(new Property("test_read_ds_1", "5")));
- TransactionConnectionContext context = new TransactionConnectionContext();
- context.setInTransaction(true);
- assertThat(loadBalanceAlgorithm.getDataSource("ds", "test_write_ds", Collections.singletonList("test_read_ds_1"), context), is("test_read_ds_1"));
- }
-
- @Test
- public void assertGetMultipleReadDataSources() {
- ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(
- ReadQueryLoadBalanceAlgorithm.class, "TRANSACTION_WEIGHT", PropertiesBuilder.build(new Property("test_read_ds_1", "5"), new Property("test_read_ds_2", "5")));
- String writeDataSourceName = "test_write_ds";
- String readDataSourceName1 = "test_read_ds_1";
- String readDataSourceName2 = "test_read_ds_2";
- List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
- TransactionConnectionContext context = new TransactionConnectionContext();
- context.setInTransaction(true);
- assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), notNullValue());
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), notNullValue());
- assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), notNullValue());
- }
-}
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 1216f3c3d87..264d74de8be 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
@@ -19,6 +19,8 @@ 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.api.transaction.TransactionReadQueryStrategyAware;
+import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionReadQueryStrategy;
import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
@@ -31,6 +33,7 @@ import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertTrue;
public final class WeightReadQueryLoadBalanceAlgorithmTest {
@@ -48,13 +51,17 @@ public final class WeightReadQueryLoadBalanceAlgorithmTest {
String readDataSourceName1 = "test_read_ds_1";
String readDataSourceName2 = "test_read_ds_2";
List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
+ assertWeightReadQueryLoadBalance(loadBalanceAlgorithm, writeDataSourceName, readDataSourceNames);
+ }
+
+ 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());
}
@Test
- public void assertGetReadDataSourceInTransaction() {
+ public void assertGetReadDataSourceDefaultStrategy() {
ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class,
"WEIGHT", PropertiesBuilder.build(new Property("test_read_ds_1", "5"), new Property("test_read_ds_2", "5")));
String writeDataSourceName = "test_write_ds";
@@ -62,10 +69,60 @@ public final class WeightReadQueryLoadBalanceAlgorithmTest {
String readDataSourceName2 = "test_read_ds_2";
List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
TransactionConnectionContext context = new TransactionConnectionContext();
+ assertWeightReadQueryLoadBalance(loadBalanceAlgorithm, writeDataSourceName, readDataSourceNames);
context.setInTransaction(true);
assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(writeDataSourceName));
}
+ @Test
+ public void assertGetDataSourceWithFixedPrimaryStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "WEIGHT",
+ PropertiesBuilder.build(new Property("test_read_ds_1", "5"), new Property("test_read_ds_2", "5"),
+ new Property(TransactionReadQueryStrategyAware.TRANSACTION_READ_QUERY_STRATEGY, TransactionReadQueryStrategy.FIXED_PRIMARY.name())));
+ String writeDataSourceName = "test_write_ds";
+ String readDataSourceName1 = "test_read_ds_1";
+ String readDataSourceName2 = "test_read_ds_2";
+ List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
+ TransactionConnectionContext context = new TransactionConnectionContext();
+ assertWeightReadQueryLoadBalance(loadBalanceAlgorithm, writeDataSourceName, readDataSourceNames);
+ context.setInTransaction(true);
+ assertTrue(writeDataSourceName.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
+ assertTrue(writeDataSourceName.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
+ }
+
+ @Test
+ public void assertGetDataSourceWithFixedReplicaStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "WEIGHT",
+ PropertiesBuilder.build(new Property("test_read_ds_1", "5"), new Property("test_read_ds_2", "5"),
+ new Property(TransactionReadQueryStrategyAware.TRANSACTION_READ_QUERY_STRATEGY, TransactionReadQueryStrategy.FIXED_REPLICA.name())));
+ String writeDataSourceName = "test_write_ds";
+ String readDataSourceName1 = "test_read_ds_1";
+ String readDataSourceName2 = "test_read_ds_2";
+ List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
+ TransactionConnectionContext context = new TransactionConnectionContext();
+ assertWeightReadQueryLoadBalance(loadBalanceAlgorithm, writeDataSourceName, readDataSourceNames);
+ context.setInTransaction(true);
+ String routeDataSource = loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context);
+ assertTrue(readDataSourceNames.contains(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context)));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
+ assertThat(loadBalanceAlgorithm.getDataSource("ds", writeDataSourceName, readDataSourceNames, context), is(routeDataSource));
+ }
+
+ @Test
+ public void assertGetDataSourceWithDynamicReplicaStrategy() {
+ ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class, "WEIGHT",
+ PropertiesBuilder.build(new Property("test_read_ds_1", "5"), new Property("test_read_ds_2", "5"),
+ new Property(TransactionReadQueryStrategyAware.TRANSACTION_READ_QUERY_STRATEGY, TransactionReadQueryStrategy.DYNAMIC_REPLICA.name())));
+ String writeDataSourceName = "test_write_ds";
+ String readDataSourceName1 = "test_read_ds_1";
+ String readDataSourceName2 = "test_read_ds_2";
+ List<String> readDataSourceNames = Arrays.asList(readDataSourceName1, readDataSourceName2);
+ TransactionConnectionContext context = new TransactionConnectionContext();
+ assertWeightReadQueryLoadBalance(loadBalanceAlgorithm, writeDataSourceName, readDataSourceNames);
+ context.setInTransaction(true);
+ assertWeightReadQueryLoadBalance(loadBalanceAlgorithm, writeDataSourceName, readDataSourceNames);
+ }
+
@Test
public void assertGetDataSourceWhenReadDataSourceChanged() {
ReadQueryLoadBalanceAlgorithm loadBalanceAlgorithm = TypedSPILoader.getService(ReadQueryLoadBalanceAlgorithm.class,