Skip to content

Commit

Permalink
WeightReplicaLoadBalanceAlgorithm can not be used by multiple load ba…
Browse files Browse the repository at this point in the history
…lance rules (#17395)

* Change static field WEIGHT_MAP to class level
  • Loading branch information
cherubimLV authored Jun 14, 2022
1 parent 6b9927c commit 618b578
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 24 deletions.
11 changes: 3 additions & 8 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
# limitations under the License.
#

coverage:
status:
# pull-requests only
patch:
default:
threshold: 0.1%
ignore:
- "examples/.*"
org.apache.shardingsphere.readwritesplitting.algorithm.RoundRobinReplicaLoadBalanceAlgorithm
org.apache.shardingsphere.readwritesplitting.algorithm.RandomReplicaLoadBalanceAlgorithm
org.apache.shardingsphere.readwritesplitting.algorithm.WeightReplicaLoadBalanceAlgorithm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class WeightReplicaLoadBalanceAlgorithm implements ReadQueryLoadBal

private static final double ACCURACY_THRESHOLD = 0.0001;

private static final ConcurrentHashMap<String, double[]> WEIGHT_MAP = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, double[]> weightMap = new ConcurrentHashMap<>();

private Properties props;

Expand All @@ -49,8 +49,8 @@ public String getDataSource(final String name, final String writeDataSourceName,
if (TransactionHolder.isTransaction()) {
return writeDataSourceName;
}
double[] weight = WEIGHT_MAP.containsKey(name) ? WEIGHT_MAP.get(name) : initWeight(readDataSourceNames);
WEIGHT_MAP.putIfAbsent(name, weight);
double[] weight = weightMap.containsKey(name) ? weightMap.get(name) : initWeight(readDataSourceNames);
weightMap.putIfAbsent(name, weight);
return getDataSourceName(readDataSourceNames, weight);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.readwritesplitting.factory.ReplicaLoadBalanceAlgorithmFactory;
import org.apache.shardingsphere.transaction.TransactionHolder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -37,15 +33,6 @@

public final class WeightReplicaLoadBalanceAlgorithmTest {

@SuppressWarnings("rawtypes")
@Before
@After
public void reset() throws NoSuchFieldException, IllegalAccessException {
Field accuracyThresholdField = WeightReplicaLoadBalanceAlgorithm.class.getDeclaredField("WEIGHT_MAP");
accuracyThresholdField.setAccessible(true);
((Map) accuracyThresholdField.get(WeightReplicaLoadBalanceAlgorithm.class)).clear();
}

@Test
public void assertGetSingleReadDataSource() {
WeightReplicaLoadBalanceAlgorithm weightReplicaLoadBalanceAlgorithm = createReplicaLoadBalanceAlgorithm(createSingleDataSourceProperties());
Expand Down

0 comments on commit 618b578

Please sign in to comment.