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

Traffic resiliency outstanding improvements (comments) #2917

Merged
merged 6 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -20,10 +20,10 @@
import java.util.function.LongSupplier;
import javax.annotation.Nullable;

import static io.servicetalk.capacity.limiter.api.Preconditions.ensureBetweenZeroAndOneExclusive;
import static io.servicetalk.capacity.limiter.api.Preconditions.ensureRange;
import static io.servicetalk.utils.internal.DurationUtils.ensureNonNegative;
import static io.servicetalk.utils.internal.NumberUtils.ensureBetweenZeroAndOneExclusive;
import static io.servicetalk.utils.internal.NumberUtils.ensurePositive;
idelpivnitskiy marked this conversation as resolved.
Show resolved Hide resolved
import static io.servicetalk.utils.internal.NumberUtils.ensureRange;
import static java.lang.Integer.MAX_VALUE;
import static java.util.Objects.requireNonNull;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
import static io.servicetalk.capacity.limiter.api.GradientCapacityLimiterProfiles.DEFAULT_SUSPEND_INCR;
import static io.servicetalk.capacity.limiter.api.GradientCapacityLimiterProfiles.GREEDY_HEADROOM;
import static io.servicetalk.capacity.limiter.api.GradientCapacityLimiterProfiles.MIN_SAMPLING_DURATION;
import static io.servicetalk.utils.internal.NumberUtils.ensureBetweenZeroAndOne;
import static io.servicetalk.utils.internal.NumberUtils.ensureBetweenZeroAndOneExclusive;
import static io.servicetalk.utils.internal.NumberUtils.ensureGreaterThan;
import static io.servicetalk.capacity.limiter.api.Preconditions.ensureBetweenZeroAndOne;
import static io.servicetalk.capacity.limiter.api.Preconditions.ensureBetweenZeroAndOneExclusive;
import static io.servicetalk.capacity.limiter.api.Preconditions.ensureGreaterThan;
import static java.util.Objects.requireNonNull;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors
*
* Licensed 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 io.servicetalk.capacity.limiter.api;

/**
* Number utilities
*/
class Preconditions {

private Preconditions() {
}

/**
* Ensures the float is greater than the min specified.
*
* @param value the float value to validate
* @param min the float min to validate against
* @param field name of the variable
* @return the passed value if all checks pass
* @throws IllegalArgumentException if the passed float doesn't meet the requirements
*/
public static float ensureGreaterThan(final float value, final float min, final String field) {
if (value <= min) {
throw new IllegalArgumentException(field + ": " + value + " (expected: > " + min + ")");
}
return value;
}

/**
* Ensures the float is between 0 and 1 (inclusive).
*
* @param value the float value to validate
* @param field name of the variable
* @return the passed value if all checks pass
* @throws IllegalArgumentException if the passed float doesn't meet the requirements
*/
public static float ensureBetweenZeroAndOne(final float value, final String field) {
if (value < 0.0f || value > 1.0f) {
throw new IllegalArgumentException(field + ": " + value + " (expected: 0.0f <= " + field + " <= 1.0f)");
}
return value;
}

/**
* Ensures the int is between the provided range (inclusive).
*
* @param value the int value to validate
* @param min the min int value to validate against (inclusive)
* @param max the max int value to validate against (inclusive)
* @param field name of the variable
* @return the passed value if all checks pass
* @throws IllegalArgumentException if the passed int doesn't meet the requirements
*/
public static int ensureRange(final int value, final int min, final int max, final String field) {
if (value < min || value > max) {
throw new IllegalArgumentException(field + ": " + value +
" (expected: " + min + " <= " + field + " <= " + max + ")");
}
return value;
}

/**
* Ensures the float is between 0 and 1 (exclusive).
*
* @param value the float value to validate
* @param field name of the variable
* @return the passed value if all checks pass
* @throws IllegalArgumentException if the passed float doesn't meet the requirements
*/
public static float ensureBetweenZeroAndOneExclusive(final float value, final String field) {
if (value <= 0.0f || value >= 1.0f) {
throw new IllegalArgumentException(field + ": " + value + " (expected: 0.0f < " + field + " < 1.0f)");
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
/**
* Policy for peer capacity rejections that allows customization of behavior (retries or pass-through).
* This is meant to be used as a policy on the {@link TrafficResilienceHttpServiceFilter}.
* @see TrafficResilienceHttpClientFilter.Builder#peerRejection(ClientPeerRejectionPolicy)
* @see TrafficResilienceHttpClientFilter.Builder#rejectionPolicy(ClientPeerRejectionPolicy)
*/
public final class ClientPeerRejectionPolicy {

/**
* Default rejection observer for dropped requests from an external sourced.
* see. {@link TrafficResilienceHttpClientFilter.Builder#peerRejection(ClientPeerRejectionPolicy)}.
* see. {@link TrafficResilienceHttpClientFilter.Builder#rejectionPolicy(ClientPeerRejectionPolicy)}.
* <p>
* The default predicate matches the following HTTP response codes:
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/**
* Rejection Policy to rule the behavior of service rejections due to capacity or open circuit.
* This is meant to be used as a policy on the {@link TrafficResilienceHttpServiceFilter}.
* @see TrafficResilienceHttpServiceFilter.Builder#onRejectionPolicy(ServiceRejectionPolicy)
* @see TrafficResilienceHttpServiceFilter.Builder#rejectionPolicy(ServiceRejectionPolicy)
*/
public final class ServiceRejectionPolicy {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public Builder circuitBreakerPartitions(
* @return {@code this}.
* @see ClientPeerRejectionPolicy#DEFAULT_PEER_REJECTION_POLICY
*/
public Builder peerRejection(final ClientPeerRejectionPolicy policy) {
public Builder rejectionPolicy(final ClientPeerRejectionPolicy policy) {
this.clientPeerRejectionPolicy = requireNonNull(policy);
return this;
}
Expand Down Expand Up @@ -476,7 +476,7 @@ public Builder dontForceOpenCircuitOnPeerCircuitRejections() {
/**
* {@link Ticket Ticket} terminal callback override upon erroneous completion of the request operation.
* Erroneous completion in this context means, that an error occurred as part of the operation or the
* {@link #peerRejection(ClientPeerRejectionPolicy)} triggered an exception.
* {@link #rejectionPolicy(ClientPeerRejectionPolicy)} triggered an exception.
* By default the terminal callback is {@link Ticket#failed(Throwable)}.
*
* @param onError Callback to override default {@link Ticket ticket} terminal event for an erroneous
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public Builder onCancelTicketTerminal(final Consumer<Ticket> onCancellation) {
* @return {@code this}.
* @see ServiceRejectionPolicy#DEFAULT_REJECTION_POLICY
*/
public Builder onRejectionPolicy(final ServiceRejectionPolicy policy) {
public Builder rejectionPolicy(final ServiceRejectionPolicy policy) {
this.onServiceRejectionPolicy = requireNonNull(policy, "policy");
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx,
if (!applyOnClient) {
TrafficResilienceHttpServiceFilter serviceFilter =
new TrafficResilienceHttpServiceFilter.Builder(limiterSupplier)
.onRejectionPolicy(new ServiceRejectionPolicy.Builder()
.rejectionPolicy(new ServiceRejectionPolicy.Builder()
.onLimitResponseBuilder(ServiceRejectionPolicy.serviceUnavailable()).build())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void verifyPeerRejection() {
final TrafficResilienceHttpClientFilter trafficResilienceHttpClientFilter =
new TrafficResilienceHttpClientFilter.Builder(
() -> CapacityLimiters.fixedCapacity(1).build())
.peerRejection(ofRejection(resp -> BAD_GATEWAY.equals(resp.status())))
.rejectionPolicy(ofRejection(resp -> BAD_GATEWAY.equals(resp.status())))
.build();

FilterableStreamingHttpClient client = mock(FilterableStreamingHttpClient.class);
Expand All @@ -114,7 +114,7 @@ void verifyPeerRejectionPassthrough() throws Exception {
final TrafficResilienceHttpClientFilter trafficResilienceHttpClientFilter =
new TrafficResilienceHttpClientFilter.Builder(
() -> CapacityLimiters.fixedCapacity(1).build())
.peerRejection(ofPassthrough(resp -> BAD_GATEWAY.equals(resp.status())))
.rejectionPolicy(ofPassthrough(resp -> BAD_GATEWAY.equals(resp.status())))
.build();

FilterableStreamingHttpClient client = mock(FilterableStreamingHttpClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void testStopAcceptingConnections(final Protocol protocol) throws Exception {
.build();
TrafficResilienceHttpServiceFilter filter = new TrafficResilienceHttpServiceFilter
.Builder(() -> limiter)
.onRejectionPolicy(serviceRejectionPolicy)
.rejectionPolicy(serviceRejectionPolicy)
.build();

final HttpServerContext serverContext = forAddress(localAddress(0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,68 +98,4 @@ public static long ensureNonNegative(final long value, final String name) {
}
return value;
}

/**
* Ensures the float is greater than the min specified.
*
* @param value the float value to validate
* @param min the float min to validate against
* @param field name of the variable
* @return the passed value if all checks pass
* @throws IllegalArgumentException if the passed float doesn't meet the requirements
*/
public static float ensureGreaterThan(final float value, final float min, final String field) {
if (value <= min) {
throw new IllegalArgumentException(field + ": " + value + " (expected: > " + min + ")");
}
return value;
}

/**
* Ensures the float is between 0 and 1 (inclusive).
*
* @param value the float value to validate
* @param field name of the variable
* @return the passed value if all checks pass
* @throws IllegalArgumentException if the passed float doesn't meet the requirements
*/
public static float ensureBetweenZeroAndOne(final float value, final String field) {
if (value < 0.0f || value > 1.0f) {
throw new IllegalArgumentException(field + ": " + value + " (expected: 0.0f <= " + field + " <= 1.0f)");
}
return value;
}

/**
* Ensures the int is between the provided range (inclusive).
*
* @param value the int value to validate
* @param min the min int value to validate against (inclusive)
* @param max the max int value to validate against (inclusive)
* @param field name of the variable
* @return the passed value if all checks pass
* @throws IllegalArgumentException if the passed int doesn't meet the requirements
*/
public static int ensureRange(final int value, final int min, final int max, final String field) {
if (value < min || value > max) {
throw new IllegalArgumentException(field + ": " + value +
" (expected: " + min + " <= " + field + " <= " + max + ")");
}
return value;
}

/**
* Ensures the float is between 0 and 1 (exclusive).
*
* @param value the float value to validate
* @param field name of the variable
* @return the passed value if all checks pass
* @throws IllegalArgumentException if the passed float doesn't meet the requirements
*/
public static float ensureBetweenZeroAndOneExclusive(final float value, final String field) {
if (value <= 0.0f || value >= 1.0f) {
throw new IllegalArgumentException(field + ": " + value + " (expected: 0.0f < " + field + " < 1.0f)");
}
return value;
}
}
Loading