Skip to content

Commit

Permalink
[fix][broker]fix arithmetic exception for LeastResourceUsageWithWeigh…
Browse files Browse the repository at this point in the history
…t strategy (apache#17149)
  • Loading branch information
HQebupt authored Aug 24, 2022
1 parent 117afff commit 3160edc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*/
@Slf4j
public class LeastResourceUsageWithWeight implements ModularLoadManagerStrategy {
private static final double MAX_RESOURCE_USAGE = 1.0d;
// Maintain this list to reduce object creation.
private final ArrayList<String> bestBrokers;
private final Map<String, Double> brokerAvgResourceUsageWithWeight;
Expand Down Expand Up @@ -144,7 +145,7 @@ public Optional<String> selectBroker(Set<String> candidates, BundleData bundleTo
final double diffThreshold =
conf.getLoadBalancerAverageResourceUsageDifferenceThresholdPercentage() / 100.0;
candidates.forEach(broker -> {
Double avgResUsage = brokerAvgResourceUsageWithWeight.getOrDefault(broker, Double.MAX_VALUE);
Double avgResUsage = brokerAvgResourceUsageWithWeight.getOrDefault(broker, MAX_RESOURCE_USAGE);
if ((avgResUsage + diffThreshold <= avgUsage)) {
bestBrokers.add(broker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import static org.testng.Assert.assertEquals;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -131,6 +133,46 @@ public void testLeastResourceUsageWithWeight() {
assertEquals(strategy.selectBroker(candidates, bundleData, loadData, conf), Optional.of("2"));
}

public void testLeastResourceUsageWithWeightWithArithmeticException()
throws NoSuchFieldException, IllegalAccessException {
BundleData bundleData = new BundleData();
BrokerData brokerData1 = initBrokerData(10, 100);
BrokerData brokerData2 = initBrokerData(30, 100);
BrokerData brokerData3 = initBrokerData(60, 100);
BrokerData brokerData4 = initBrokerData(5, 100);
LoadData loadData = new LoadData();
Map<String, BrokerData> brokerDataMap = loadData.getBrokerData();
brokerDataMap.put("1", brokerData1);
brokerDataMap.put("2", brokerData2);
brokerDataMap.put("3", brokerData3);
brokerDataMap.put("4", brokerData4);

ServiceConfiguration conf = new ServiceConfiguration();
conf.setLoadBalancerCPUResourceWeight(1.0);
conf.setLoadBalancerMemoryResourceWeight(0.1);
conf.setLoadBalancerDirectMemoryResourceWeight(0.1);
conf.setLoadBalancerBandwithInResourceWeight(1.0);
conf.setLoadBalancerBandwithOutResourceWeight(1.0);
conf.setLoadBalancerHistoryResourcePercentage(0.5);
conf.setLoadBalancerAverageResourceUsageDifferenceThresholdPercentage(5);

LeastResourceUsageWithWeight strategy = new LeastResourceUsageWithWeight();

// Should choice broker from broker1 2 3.
Set<String> candidates = new HashSet<>();
candidates.add("1");
candidates.add("2");
candidates.add("3");
Field strategyUpdater = LeastResourceUsageWithWeight.class.getDeclaredField("brokerAvgResourceUsageWithWeight");
strategyUpdater.setAccessible(true);
Map<String, Double> brokerAvgResourceUsageWithWeight = new HashMap<>();
brokerAvgResourceUsageWithWeight.put("1", 0.1d);
brokerAvgResourceUsageWithWeight.put("2", 0.3d);
brokerAvgResourceUsageWithWeight.put("4", 0.05d);
strategyUpdater.set(strategy, brokerAvgResourceUsageWithWeight);
assertEquals(strategy.selectBroker(candidates, bundleData, loadData, conf), Optional.of("1"));
}

private BrokerData initBrokerData(double usage, double limit) {
LocalBrokerData localBrokerData = new LocalBrokerData();
localBrokerData.setCpu(new ResourceUsage(usage, limit));
Expand Down

0 comments on commit 3160edc

Please sign in to comment.