Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Flatten RetryParams by removing BackoffParams layer.
Browse files Browse the repository at this point in the history
  • Loading branch information
shinfan committed Mar 30, 2016
1 parent 06d664f commit e2004d5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 101 deletions.
81 changes: 0 additions & 81 deletions src/main/java/com/google/api/gax/core/BackoffParams.java

This file was deleted.

30 changes: 26 additions & 4 deletions src/main/java/com/google/api/gax/core/RetryParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@
*/
@AutoValue
public abstract class RetryParams {
public abstract BackoffParams getRetryBackoff();

public abstract BackoffParams getTimeoutBackoff();

public abstract Duration getTotalTimeout();

public abstract Duration getInitialRetryDelay();
public abstract double getRetryDelayMultiplier();
public abstract Duration getMaxRetryDelay();

public abstract Duration getInitialRpcTimeout();
public abstract double getRpcTimeoutMultiplier();
public abstract Duration getMaxRpcTimeout();

public static Builder newBuilder() {
return new AutoValue_RetryParams.Builder();
}
Expand All @@ -59,9 +65,14 @@ public Builder toBuilder() {

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setRetryBackoff(BackoffParams retryBackoff);

public abstract Builder setTimeoutBackoff(BackoffParams timeoutBackoff);
public abstract Builder setInitialRetryDelay(Duration initialDelay);
public abstract Builder setRetryDelayMultiplier(double multiplier);
public abstract Builder setMaxRetryDelay(Duration maxDelay);

public abstract Builder setInitialRpcTimeout(Duration initialTimeout);
public abstract Builder setRpcTimeoutMultiplier(double multiplier);
public abstract Builder setMaxRpcTimeout(Duration maxTimeout);

public abstract Builder setTotalTimeout(Duration totalTimeout);

Expand All @@ -72,6 +83,17 @@ public RetryParams build() {
if (params.getTotalTimeout().getMillis() < 0) {
throw new IllegalStateException("total timeout must not be negative");
}
if (params.getInitialRetryDelay().getMillis() < 0 ||
params.getInitialRpcTimeout().getMillis() < 0) {
throw new IllegalStateException("initial time must not be negative");
}
if (params.getRetryDelayMultiplier() < 1.0 || params.getRpcTimeoutMultiplier() < 1.0) {
throw new IllegalStateException("multiplier must be at least 1");
}
if (params.getMaxRetryDelay().compareTo(params.getInitialRetryDelay()) < 0 ||
params.getMaxRpcTimeout().compareTo(params.getInitialRpcTimeout()) < 0) {
throw new IllegalStateException("max time must not be smaller than initial time");
}
return params;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/google/api/gax/grpc/RetryingCallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public ListenableFuture<ResponseT> futureCall(CallContext<RequestT> context) {
new Retryer(
context,
result,
retryParams.getRetryBackoff().getInitialDelay(),
retryParams.getTimeoutBackoff().getInitialDelay(),
retryParams.getInitialRetryDelay(),
retryParams.getInitialRpcTimeout(),
null);
retryer.run();
return result;
Expand Down Expand Up @@ -138,17 +138,17 @@ public void onFailure(Throwable throwable) {
}
long newRetryDelay =
(long) (retryDelay.getMillis() *
retryParams.getRetryBackoff().getDelayMultiplier());
retryParams.getRetryDelayMultiplier());
newRetryDelay =
Math.min(newRetryDelay,
retryParams.getRetryBackoff().getMaxDelay().getMillis());
retryParams.getMaxRetryDelay().getMillis());

long newRpcTimeout =
(long) (rpcTimeout.getMillis() *
retryParams.getTimeoutBackoff().getDelayMultiplier());
retryParams.getRpcTimeoutMultiplier());
newRpcTimeout =
Math.min(newRpcTimeout,
retryParams.getTimeoutBackoff().getMaxDelay().getMillis());
retryParams.getMaxRpcTimeout().getMillis());

long randomRetryDelay = ThreadLocalRandom.current().nextLong(retryDelay.getMillis());
Retryer retryer = new Retryer(context, result,
Expand Down
18 changes: 8 additions & 10 deletions src/test/java/com/google/api/gax/grpc/ApiCallableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

package com.google.api.gax.grpc;

import com.google.api.gax.core.BackoffParams;
import com.google.api.gax.core.RetryParams;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -61,6 +60,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
* Tests for {@link ApiCallable}.
*/
Expand All @@ -71,17 +71,15 @@ public class ApiCallableTest {
private static final RetryParams testRetryParams;

static {
BackoffParams backoff =
BackoffParams.newBuilder()
.setInitialDelay(Duration.millis(2L))
.setDelayMultiplier(1)
.setMaxDelay(Duration.millis(2L))
.build();
testRetryParams =
RetryParams.newBuilder()
.setRetryBackoff(backoff)
.setTimeoutBackoff(backoff)
.setTotalTimeout(Duration.millis(100L))
.setInitialRetryDelay(Duration.millis(2L))
.setRetryDelayMultiplier(1)
.setMaxRetryDelay(Duration.millis(2L))
.setInitialRpcTimeout(Duration.millis(2L))
.setRpcTimeoutMultiplier(1)
.setMaxRpcTimeout(Duration.millis(2L))
.setTotalTimeout(Duration.millis(10L))
.build();
}

Expand Down

0 comments on commit e2004d5

Please sign in to comment.