Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstream weighting #283

Merged
merged 3 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public CharSequence preferSameRackWeighting(Collection<UpstreamInfo> upstreams,
}
final BigDecimal totalPendingLoad = rackHelper.getTotalPendingLoad(allRacks);
final BigDecimal capacity = rackHelper.calculateCapacity(allRacks);
return preferSameRackWeightingOperation(upstreams, currentUpstream, allRacks, capacity, totalPendingLoad, null);
final BigDecimal multiplier = rackHelper.calculateMultiplier(allRacks);
return preferSameRackWeightingOperation(upstreams, currentUpstream, allRacks, capacity, multiplier, totalPendingLoad, null);
}

/**
Expand All @@ -70,6 +71,7 @@ public CharSequence preferSameRackWeightingOperation(Collection<UpstreamInfo> up
UpstreamInfo currentUpstream,
List<String> allRacks,
BigDecimal capacity,
BigDecimal multiplier,
BigDecimal totalPendingLoad,
Options options) {

Expand All @@ -79,8 +81,6 @@ public CharSequence preferSameRackWeightingOperation(Collection<UpstreamInfo> up

final String currentRack = agentMetadata.getEc2().getAvailabilityZone().get();
final String testingRack = currentUpstream.getRackId().get();

final BigDecimal countOfAllRacks = new BigDecimal(allRacks.size());
final BigDecimal countOfCurrentRack = new BigDecimal(Collections.frequency(allRacks, currentRack));
final BigDecimal countOfTestingRack = new BigDecimal((Collections.frequency(allRacks, testingRack))); // assume this is always in upstream

Expand All @@ -94,7 +94,7 @@ public CharSequence preferSameRackWeightingOperation(Collection<UpstreamInfo> up
if (load.compareTo(capacity) == -1) { // load is less than capacity
return "";
}
final BigDecimal weight = capacity.multiply(countOfAllRacks).multiply(countOfTestingRack);
final BigDecimal weight = capacity.multiply(multiplier);
return getWeight(weight);
}

Expand All @@ -110,7 +110,7 @@ public CharSequence preferSameRackWeightingOperation(Collection<UpstreamInfo> up
}

final BigDecimal pendingLoadFromCurrentRackToTestingRack = (extraCapacityInTestingRack.divide(totalPendingLoad, 10, BigDecimal.ROUND_HALF_UP)).multiply(pendingLoadInCurrentRack);
final BigDecimal weight = pendingLoadFromCurrentRackToTestingRack.multiply(countOfAllRacks).multiply(countOfTestingRack);
final BigDecimal weight = pendingLoadFromCurrentRackToTestingRack.multiply(multiplier);
return getWeight(weight);
}
return ""; // If the required data isn't present for some reason, send even traffic everywhere (i.e. everything has a weight of 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public BigDecimal calculateCapacity(List<String> allRacks) {
return new BigDecimal((new HashSet<>(allRacks)).size()).divide(new BigDecimal(allRacks.size()), 10, BigDecimal.ROUND_HALF_UP);
}

public BigDecimal calculateMultiplier(List<String> allRacks) {
/* multiplier is the factor by which fractional weights should be scaled to get an integer value*/
return new BigDecimal((new HashSet<>(allRacks)).size()).multiply(new BigDecimal(allRacks.size()));
}

/**
* @param allRacks
* @return the total pending load that have to be distributed to other upstreams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testSimpleCase1B() {
CharSequence result = helper.preferSameRackWeighting(UPSTREAMS, currentUpstream, null);
results.add(result.toString());
}
Assert.assertEquals(Arrays.asList("", "", "weight=4", "backup", "backup"), results);
Assert.assertEquals(Arrays.asList("weight=2", "weight=2", "weight=16", "backup", "backup"), results);
}

@Test
Expand All @@ -74,7 +74,7 @@ public void testSimpleCase1C() {
CharSequence result = helper.preferSameRackWeighting(UPSTREAMS, currentUpstream, null);
results.add(result.toString());
}
Assert.assertEquals(Arrays.asList("", "", "backup", "weight=4", "backup"), results);
Assert.assertEquals(Arrays.asList("weight=2", "weight=2", "backup", "weight=16", "backup"), results);
}

@Test
Expand All @@ -87,7 +87,7 @@ public void testSimpleCase1E() {
CharSequence result = helper.preferSameRackWeighting(UPSTREAMS, currentUpstream, null);
results.add(result.toString());
}
Assert.assertEquals(Arrays.asList("", "", "backup", "backup", "weight=4"), results);
Assert.assertEquals(Arrays.asList("weight=2", "weight=2", "backup", "backup", "weight=16"), results);
}

@Test
Expand All @@ -104,7 +104,7 @@ public void testSimpleCase1D() {
}


private static final Collection<String> LARGER_AVAILABILITY_ZONES = Arrays.asList("us-east-1a", "us-east-1a", "us-east-1b", "us-east-1b", "us-east-1b", "us-east-1b", "us-east-1c", "us-east-1c", "us-east-1c", "us-east-1e", "us-east-1e");
private static final Collection<String> LARGER_AVAILABILITY_ZONES = Arrays.asList("us-east-1a", "us-east-1a", "us-east-1b", "us-east-1b", "us-east-1b", "us-east-1b", "us-east-1c", "us-east-1c", "us-east-1c", "us-east-1c", "us-east-1e", "us-east-1e");
private static final Collection<UpstreamInfo> MANY_UPSTREAMS = LARGER_AVAILABILITY_ZONES.stream().map((availabilityZone) -> new UpstreamInfo("testhost:8080", Optional.absent(), Optional.of(availabilityZone))).collect(Collectors.toList());


Expand All @@ -118,7 +118,7 @@ public void testLargerCase1A() {
CharSequence result = helper.preferSameRackWeighting(MANY_UPSTREAMS, currentUpstream, null);
results.add(result.toString());
}
Assert.assertEquals(Arrays.asList("weight=8", "weight=8", "weight=2", "weight=2", "weight=2", "weight=2", "", "", "", "backup", "backup"), results);
Assert.assertEquals(Arrays.asList("weight=16", "weight=16", "weight=2", "weight=2", "weight=2", "weight=2", "weight=2", "weight=2", "weight=2", "weight=2", "backup", "backup"), results);
}

@Test
Expand All @@ -131,7 +131,7 @@ public void testLargerCase1B() {
CharSequence result = helper.preferSameRackWeighting(MANY_UPSTREAMS, currentUpstream, null);
results.add(result.toString());
}
Assert.assertEquals(Arrays.asList("backup", "backup", "", "", "", "", "backup", "backup", "backup", "backup", "backup"), results);
Assert.assertEquals(Arrays.asList("backup", "backup", "", "", "", "", "backup", "backup", "backup", "backup", "backup", "backup"), results);
}

@Test
Expand All @@ -144,7 +144,7 @@ public void testLargerCase1C() {
CharSequence result = helper.preferSameRackWeighting(MANY_UPSTREAMS, currentUpstream, null);
results.add(result.toString());
}
Assert.assertEquals(Arrays.asList("backup", "backup", "backup", "backup", "backup", "backup", "", "", "", "backup", "backup"), results);
Assert.assertEquals(Arrays.asList("backup", "backup", "backup", "backup", "backup", "backup", "", "", "", "", "backup", "backup"), results);
}

@Test
Expand All @@ -157,7 +157,7 @@ public void testLargerCase1D() {
CharSequence result = helper.preferSameRackWeighting(MANY_UPSTREAMS, currentUpstream, null);
results.add(result.toString());
}
Assert.assertEquals(Arrays.asList("", "", "", "", "", "", "", "", "", "", ""), results);
Assert.assertEquals(Arrays.asList("", "", "", "", "", "", "", "", "", "", "", ""), results);
}

@Test
Expand All @@ -170,7 +170,7 @@ public void testLargerCase1E() {
CharSequence result = helper.preferSameRackWeighting(MANY_UPSTREAMS, currentUpstream, null);
results.add(result.toString());
}
Assert.assertEquals(Arrays.asList("backup", "backup", "weight=2", "weight=2", "weight=2", "weight=2", "", "", "", "weight=8", "weight=8"), results);
Assert.assertEquals(Arrays.asList("backup", "backup", "weight=2", "weight=2", "weight=2", "weight=2", "weight=2", "weight=2", "weight=2", "weight=2", "weight=16", "weight=16"), results);
}

private static final Collection<String> NULL_AVAILABILITY_ZONES = Arrays.asList(null, null, null, null);
Expand Down Expand Up @@ -202,7 +202,7 @@ public void testBigDecimalToIntegerCase1B() {
CharSequence result = helper.preferSameRackWeighting(NEW_UPSTREAMS, currentUpstream, null);
results.add(result.toString());
}
Assert.assertEquals(Arrays.asList("weight=6", "weight=6", "", "", "", "", "", ""), results);
Assert.assertEquals(Arrays.asList("weight=9", "weight=9", "", "", "", "", "", ""), results);
}
@Test
public void testBigDecimalToIntegerCase1E() {
Expand Down