Skip to content

Commit

Permalink
clean up EndpointWeightTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhoon committed Feb 20, 2025
1 parent 24a7e34 commit 5ee2869
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.primitives.Ints;

import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.common.loadbalancer.WeightTransition;

Expand All @@ -40,9 +38,9 @@ public interface EndpointWeightTransition {
*/
@Deprecated
static EndpointWeightTransition linear() {
return (endpoint, currentStep, totalSteps) ->
// currentStep is never greater than totalSteps so we can cast long to int.
Ints.saturatedCast((long) endpoint.weight() * currentStep / totalSteps);
return (endpoint, currentStep, totalSteps) -> {
return WeightTransition.linear().compute(endpoint, endpoint.weight(), currentStep, totalSteps);
};
}

/**
Expand All @@ -61,18 +59,9 @@ static EndpointWeightTransition aggression(double aggression, double minWeightPe
"aggression: %s (expected: > 0.0)", aggression);
checkArgument(minWeightPercent >= 0 && minWeightPercent <= 1.0,
"minWeightPercent: %s (expected: >= 0.0, <= 1.0)", minWeightPercent);
final int aggressionPercentage = Ints.saturatedCast(Math.round(aggression * 100));
final double invertedAggression = 100.0 / aggressionPercentage;
return (endpoint, currentStep, totalSteps) -> {
final int weight = endpoint.weight();
final int minWeight = Ints.saturatedCast(Math.round(weight * minWeightPercent));
final int computedWeight;
if (aggressionPercentage == 100) {
computedWeight = linear().compute(endpoint, currentStep, totalSteps);
} else {
computedWeight = (int) (weight * Math.pow(1.0 * currentStep / totalSteps, invertedAggression));
}
return Math.max(computedWeight, minWeight);
return WeightTransition.aggression(aggression, minWeightPercent)
.compute(endpoint, endpoint.weight(), currentStep, totalSteps);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2025 LINE Corporation
*
* LINE Corporation 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:
*
* https://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 com.linecorp.armeria.common.loadbalancer;

import com.google.common.base.MoreObjects;
import com.google.common.primitives.Ints;

final class AggregationWeightTransition<T> implements WeightTransition<T> {

private final double aggressionPercentage;
private final double invertedAggression;
private final double minWeightPercent;

AggregationWeightTransition(double aggression, double minWeightPercent) {
aggressionPercentage = Ints.saturatedCast(Math.round(aggression * 100));
invertedAggression = 100.0 / aggressionPercentage;
this.minWeightPercent = minWeightPercent;
}

@Override
public int compute(T candidate, int weight, int currentStep, int totalSteps) {
final int minWeight = Ints.saturatedCast(Math.round(weight * minWeightPercent));
final int computedWeight;
if (aggressionPercentage == 100) {
computedWeight = WeightTransition.linear().compute(candidate, weight, currentStep, totalSteps);
} else {
computedWeight = (int) (weight * Math.pow(1.0 * currentStep / totalSteps,
invertedAggression));
}
return Math.max(computedWeight, minWeight);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("aggressionPercentage", aggressionPercentage)
.add("invertedAggression", invertedAggression)
.add("minWeightPercent", minWeightPercent)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2025 LINE Corporation
*
* LINE Corporation 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:
*
* https://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 com.linecorp.armeria.common.loadbalancer;

import com.google.common.primitives.Ints;

final class LinearWeightTransition<T> implements WeightTransition<T> {

static final LinearWeightTransition<?> INSTANCE = new LinearWeightTransition<>();

@Override
public int compute(T candidate, int weight, int currentStep, int totalSteps) {
// currentStep is never greater than totalSteps so we can cast long to int.
final int currentWeight =
Ints.saturatedCast((long) weight * currentStep / totalSteps);
if (weight > 0 && currentWeight == 0) {
// If the original weight is not 0,
// we should return 1 to make sure the endpoint is selected.
return 1;
}
return currentWeight;
}

@Override
public String toString() {
return "WeightTransition.linear()";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import static com.google.common.base.Preconditions.checkArgument;

import com.google.common.primitives.Ints;

import com.linecorp.armeria.common.annotation.UnstableApi;

/**
Expand All @@ -34,25 +32,8 @@ public interface WeightTransition<T> {
* step increases.
*/
static <T> WeightTransition<T> linear() {
return new WeightTransition<T>() {
@Override
public int compute(T candidate, int weight, int currentStep, int totalSteps) {
// currentStep is never greater than totalSteps so we can cast long to int.
final int currentWeight =
Ints.saturatedCast((long) weight * currentStep / totalSteps);
if (weight > 0 && currentWeight == 0) {
// If the original weight is not 0,
// we should return 1 to make sure the endpoint is selected.
return 1;
}
return currentWeight;
}

@Override
public String toString() {
return "WeightTransition.linear()";
}
};
//noinspection unchecked
return (WeightTransition<T>) LinearWeightTransition.INSTANCE;
}

/**
Expand All @@ -68,27 +49,7 @@ static <T> WeightTransition<T> aggression(double aggression, double minWeightPer
"aggression: %s (expected: > 0.0)", aggression);
checkArgument(minWeightPercent >= 0 && minWeightPercent <= 1.0,
"minWeightPercent: %s (expected: >= 0.0, <= 1.0)", minWeightPercent);
final int aggressionPercentage = Ints.saturatedCast(Math.round(aggression * 100));
final double invertedAggression = 100.0 / aggressionPercentage;
return new WeightTransition<T>() {
@Override
public int compute(T candidate, int weight, int currentStep, int totalSteps) {
final int minWeight = Ints.saturatedCast(Math.round(weight * minWeightPercent));
final int computedWeight;
if (aggressionPercentage == 100) {
computedWeight = linear().compute(candidate, weight, currentStep, totalSteps);
} else {
computedWeight = (int) (weight * Math.pow(1.0 * currentStep / totalSteps,
invertedAggression));
}
return Math.max(computedWeight, minWeight);
}

@Override
public String toString() {
return "WeightTransition.aggression(" + aggression + ", " + minWeightPercent + ')';
}
};
return new AggregationWeightTransition<>(aggression, minWeightPercent);
}

/**
Expand Down

0 comments on commit 5ee2869

Please sign in to comment.