chamikaramj commented on code in PR #28952: URL: https://github.com/apache/beam/pull/28952#discussion_r1361015687
########## sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponseio/CallShouldBackoffBasedOnRejectionProbability.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 + * + * 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 org.apache.beam.io.requestresponseio; + +import org.checkerframework.checker.nullness.qual.Nullable; + +/** Reports whether to apply backoff based on https://sre.google/sre-book/handling-overload/. */ +class CallShouldBackoffBasedOnRejectionProbability<ResponseT> + implements CallShouldBackoff<ResponseT> { + + // Default multiplier value recommended by https://sre.google/sre-book/handling-overload/ + private static final double DEFAULT_MULTIPLIER = 2.0; + + // The threshold is the value that pReject() must exceed in order to report a value() of true. If + // null, then the computation relies on a random value. + private @Nullable Double threshold; + + // The multiplier drives the impact of accepts on the rejection probability. See <a + // https://sre.google/sre-book/handling-overload/ for details. + private final double multiplier; + + // The number of total requests called to a remote API. + private double requests = 0; + + // The number of total accepts called to a remote API. + private double accepts = 0; + + /** Instantiate class with the {@link #DEFAULT_MULTIPLIER}. */ + CallShouldBackoffBasedOnRejectionProbability() { + this(DEFAULT_MULTIPLIER); + } + + /** + * Instantiates class with the provided multiplier. The multiplier drives the impact of accepts on + * the rejection probability. See https://sre.google/sre-book/handling-overload/ for details. + */ + CallShouldBackoffBasedOnRejectionProbability(double multiplier) { + this.multiplier = multiplier; + } + + /** + * Setter for the threshold that overrides usage of a random value. The threshold is the value + * that pReject() must exceed in order to report a value() of true. + */ + CallShouldBackoffBasedOnRejectionProbability<ResponseT> setThreshold(double threshold) { + this.threshold = threshold; + return this; + } + + /** Update the state of whether to backoff using information about the exception. */ + @Override + public void update(UserCodeExecutionException exception) { + this.requests++; + } + + /** Update the state of whether to backoff using information about the response. */ + @Override + public void update(ResponseT response) { + this.requests++; + this.accepts++; + } + + /** Provide a threshold to evaluate backoff. */ + double threshold() { + if (this.threshold != null) { + return this.threshold; + } + return Math.random(); + } + + /** + * Compute the probability of API call rejection based on + * https://sre.google/sre-book/handling-overload/. + */ + double pReject() { Review Comment: Rename to "getRejectionProbability()" ? Can we make this method private ? ########## sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponseio/CallShouldBackoffBasedOnRejectionProbability.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 + * + * 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 org.apache.beam.io.requestresponseio; + +import org.checkerframework.checker.nullness.qual.Nullable; + +/** Reports whether to apply backoff based on https://sre.google/sre-book/handling-overload/. */ +class CallShouldBackoffBasedOnRejectionProbability<ResponseT> + implements CallShouldBackoff<ResponseT> { + + // Default multiplier value recommended by https://sre.google/sre-book/handling-overload/ + private static final double DEFAULT_MULTIPLIER = 2.0; + + // The threshold is the value that pReject() must exceed in order to report a value() of true. If + // null, then the computation relies on a random value. + private @Nullable Double threshold; + + // The multiplier drives the impact of accepts on the rejection probability. See <a + // https://sre.google/sre-book/handling-overload/ for details. + private final double multiplier; + + // The number of total requests called to a remote API. + private double requests = 0; + + // The number of total accepts called to a remote API. + private double accepts = 0; + + /** Instantiate class with the {@link #DEFAULT_MULTIPLIER}. */ + CallShouldBackoffBasedOnRejectionProbability() { Review Comment: Constructors and setThreshold should be public ? ########## sdks/java/io/rrio/src/test/java/org/apache/beam/io/requestresponseio/CallShouldBackoffBasedOnRejectionProbabilityTest.java: ########## @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 + * + * 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 org.apache.beam.io.requestresponseio; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link CallShouldBackoffBasedOnRejectionProbability}. */ +@RunWith(JUnit4.class) +public class CallShouldBackoffBasedOnRejectionProbabilityTest { + + @Test + public void testValue() { Review Comment: Please add additional tests for: * No requests or exceptions. * Many requests no exceptions. * Many requests all exceptions. ########## sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponseio/CallShouldBackoffBasedOnRejectionProbability.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 + * + * 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 org.apache.beam.io.requestresponseio; + +import org.checkerframework.checker.nullness.qual.Nullable; + +/** Reports whether to apply backoff based on https://sre.google/sre-book/handling-overload/. */ +class CallShouldBackoffBasedOnRejectionProbability<ResponseT> + implements CallShouldBackoff<ResponseT> { + + // Default multiplier value recommended by https://sre.google/sre-book/handling-overload/ + private static final double DEFAULT_MULTIPLIER = 2.0; + + // The threshold is the value that pReject() must exceed in order to report a value() of true. If + // null, then the computation relies on a random value. + private @Nullable Double threshold; + + // The multiplier drives the impact of accepts on the rejection probability. See <a + // https://sre.google/sre-book/handling-overload/ for details. + private final double multiplier; + + // The number of total requests called to a remote API. + private double requests = 0; + + // The number of total accepts called to a remote API. + private double accepts = 0; + + /** Instantiate class with the {@link #DEFAULT_MULTIPLIER}. */ + CallShouldBackoffBasedOnRejectionProbability() { + this(DEFAULT_MULTIPLIER); + } + + /** + * Instantiates class with the provided multiplier. The multiplier drives the impact of accepts on + * the rejection probability. See https://sre.google/sre-book/handling-overload/ for details. + */ + CallShouldBackoffBasedOnRejectionProbability(double multiplier) { + this.multiplier = multiplier; + } + + /** + * Setter for the threshold that overrides usage of a random value. The threshold is the value + * that pReject() must exceed in order to report a value() of true. + */ + CallShouldBackoffBasedOnRejectionProbability<ResponseT> setThreshold(double threshold) { + this.threshold = threshold; + return this; + } + + /** Update the state of whether to backoff using information about the exception. */ + @Override + public void update(UserCodeExecutionException exception) { + this.requests++; + } + + /** Update the state of whether to backoff using information about the response. */ + @Override + public void update(ResponseT response) { + this.requests++; + this.accepts++; + } + + /** Provide a threshold to evaluate backoff. */ + double threshold() { Review Comment: Rename to getThreshold() ? Can we make this method private ? ########## sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponseio/CallShouldBackoffBasedOnRejectionProbability.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 + * + * 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 org.apache.beam.io.requestresponseio; + +import org.checkerframework.checker.nullness.qual.Nullable; + +/** Reports whether to apply backoff based on https://sre.google/sre-book/handling-overload/. */ +class CallShouldBackoffBasedOnRejectionProbability<ResponseT> + implements CallShouldBackoff<ResponseT> { + + // Default multiplier value recommended by https://sre.google/sre-book/handling-overload/ + private static final double DEFAULT_MULTIPLIER = 2.0; + + // The threshold is the value that pReject() must exceed in order to report a value() of true. If + // null, then the computation relies on a random value. + private @Nullable Double threshold; + + // The multiplier drives the impact of accepts on the rejection probability. See <a + // https://sre.google/sre-book/handling-overload/ for details. + private final double multiplier; + + // The number of total requests called to a remote API. + private double requests = 0; + + // The number of total accepts called to a remote API. + private double accepts = 0; + + /** Instantiate class with the {@link #DEFAULT_MULTIPLIER}. */ + CallShouldBackoffBasedOnRejectionProbability() { + this(DEFAULT_MULTIPLIER); + } + + /** + * Instantiates class with the provided multiplier. The multiplier drives the impact of accepts on + * the rejection probability. See https://sre.google/sre-book/handling-overload/ for details. + */ + CallShouldBackoffBasedOnRejectionProbability(double multiplier) { + this.multiplier = multiplier; + } + + /** + * Setter for the threshold that overrides usage of a random value. The threshold is the value + * that pReject() must exceed in order to report a value() of true. Review Comment: How about "The threshold is the value (within range [0, 1)) that pReject() ..." to clarify the value that should be specified here ? (pls update the range if incorrect). ########## sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponseio/CallShouldBackoffBasedOnRejectionProbability.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 + * + * 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 org.apache.beam.io.requestresponseio; + +import org.checkerframework.checker.nullness.qual.Nullable; + +/** Reports whether to apply backoff based on https://sre.google/sre-book/handling-overload/. */ +class CallShouldBackoffBasedOnRejectionProbability<ResponseT> + implements CallShouldBackoff<ResponseT> { + + // Default multiplier value recommended by https://sre.google/sre-book/handling-overload/ + private static final double DEFAULT_MULTIPLIER = 2.0; + + // The threshold is the value that pReject() must exceed in order to report a value() of true. If + // null, then the computation relies on a random value. + private @Nullable Double threshold; + + // The multiplier drives the impact of accepts on the rejection probability. See <a + // https://sre.google/sre-book/handling-overload/ for details. + private final double multiplier; + + // The number of total requests called to a remote API. + private double requests = 0; + + // The number of total accepts called to a remote API. + private double accepts = 0; + + /** Instantiate class with the {@link #DEFAULT_MULTIPLIER}. */ + CallShouldBackoffBasedOnRejectionProbability() { + this(DEFAULT_MULTIPLIER); + } + + /** + * Instantiates class with the provided multiplier. The multiplier drives the impact of accepts on + * the rejection probability. See https://sre.google/sre-book/handling-overload/ for details. + */ + CallShouldBackoffBasedOnRejectionProbability(double multiplier) { + this.multiplier = multiplier; + } + + /** + * Setter for the threshold that overrides usage of a random value. The threshold is the value + * that pReject() must exceed in order to report a value() of true. Review Comment: Pls refer to pReject. ########## sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponseio/CallShouldBackoffBasedOnRejectionProbability.java: ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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 + * + * 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 org.apache.beam.io.requestresponseio; + +import org.checkerframework.checker.nullness.qual.Nullable; + +/** Reports whether to apply backoff based on https://sre.google/sre-book/handling-overload/. */ +class CallShouldBackoffBasedOnRejectionProbability<ResponseT> + implements CallShouldBackoff<ResponseT> { + + // Default multiplier value recommended by https://sre.google/sre-book/handling-overload/ + private static final double DEFAULT_MULTIPLIER = 2.0; + + // The threshold is the value that pReject() must exceed in order to report a value() of true. If + // null, then the computation relies on a random value. + private @Nullable Double threshold; + + // The multiplier drives the impact of accepts on the rejection probability. See <a + // https://sre.google/sre-book/handling-overload/ for details. + private final double multiplier; + + // The number of total requests called to a remote API. + private double requests = 0; + + // The number of total accepts called to a remote API. + private double accepts = 0; + + /** Instantiate class with the {@link #DEFAULT_MULTIPLIER}. */ + CallShouldBackoffBasedOnRejectionProbability() { + this(DEFAULT_MULTIPLIER); + } + + /** + * Instantiates class with the provided multiplier. The multiplier drives the impact of accepts on + * the rejection probability. See https://sre.google/sre-book/handling-overload/ for details. + */ + CallShouldBackoffBasedOnRejectionProbability(double multiplier) { + this.multiplier = multiplier; + } + + /** + * Setter for the threshold that overrides usage of a random value. The threshold is the value + * that pReject() must exceed in order to report a value() of true. + */ + CallShouldBackoffBasedOnRejectionProbability<ResponseT> setThreshold(double threshold) { + this.threshold = threshold; + return this; + } + + /** Update the state of whether to backoff using information about the exception. */ + @Override + public void update(UserCodeExecutionException exception) { + this.requests++; + } + + /** Update the state of whether to backoff using information about the response. */ + @Override + public void update(ResponseT response) { + this.requests++; + this.accepts++; + } + + /** Provide a threshold to evaluate backoff. */ + double threshold() { + if (this.threshold != null) { + return this.threshold; + } + return Math.random(); + } + + /** + * Compute the probability of API call rejection based on + * https://sre.google/sre-book/handling-overload/. + */ + double pReject() { + double numerator = requests - multiplier * accepts; + double denominator = requests + 1; Review Comment: Please remove extra variables here and do the computation in a single line. return Math.max(0, (requests - multiplier * accepts) / (requests + 1)) Also, probably we can just get rid of this and "threshold" methods and do this computation in the value() method (give that there isn't a lot of logic here). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
