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

Reconcile RetrySettings & Clock #235

Merged
merged 17 commits into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from 13 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 @@ -27,20 +27,25 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.grpc;
package com.google.api.gax.core;

/**
* An interface for getting the current value of a high-resolution time source, in nanoseconds.
*
* Clocks other than DefaultNanoClock are typically used only for testing.
* Clocks other than NanoClock are typically used only for testing.
*
* This interface is required in addition to Java 8's Clock, because nanoTime is required to compare
* values with io.grpc.CallOptions.getDeadlineNanoTime().
*/
public interface NanoClock {
public interface ApiClock {

/**
* Returns the current value of this clock's high-resolution time source, in nanoseconds.
*/
long nanoTime();

/**
* Returns the current value of this clock's high-resolution time source, in milliseconds.
*/
long millisTime();
}
65 changes: 65 additions & 0 deletions src/main/java/com/google/api/gax/core/CurrentMillisClock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2017, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.google.api.gax.core;

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
* Implementation of the {@link ApiClock} interface, which uses {@link System#currentTimeMillis()}
* as time source.
*/
public final class CurrentMillisClock implements ApiClock, Serializable {

private static final long serialVersionUID = -6019259882852183285L;
private static final ApiClock DEFAULT_CLOCK = new CurrentMillisClock();

public static ApiClock getDefaultClock() {
return DEFAULT_CLOCK;
}

private CurrentMillisClock() {}

@Override
public long nanoTime() {
return TimeUnit.NANOSECONDS.convert(millisTime(), TimeUnit.MILLISECONDS);
}

@Override
public long millisTime() {
return System.currentTimeMillis();
}

private Object readResolve() throws ObjectStreamException {
return DEFAULT_CLOCK;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,35 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.grpc;
package com.google.api.gax.core;

/**
* Default implementation of the NanoClock interface, using call to System.nanoTime().
*/
public final class DefaultNanoClock implements NanoClock {
public static NanoClock create() {
return new DefaultNanoClock();
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/** Default implementation of the ApiClock interface, using call to System.nanoTime(). */
public final class NanoClock implements ApiClock, Serializable {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


private static final ApiClock DEFAULT_CLOCK = new NanoClock();
private static final long serialVersionUID = 5541462688633944865L;

public static ApiClock getDefaultClock() {
return DEFAULT_CLOCK;
}

private DefaultNanoClock() {}
private NanoClock() {}

@Override
public final long nanoTime() {
return System.nanoTime();
}

@Override
public final long millisTime() {
return TimeUnit.MILLISECONDS.convert(nanoTime(), TimeUnit.NANOSECONDS);
}

private Object readResolve() throws ObjectStreamException {
return DEFAULT_CLOCK;
}
}
32 changes: 30 additions & 2 deletions src/main/java/com/google/api/gax/core/RetrySettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package com.google.api.gax.core;

import com.google.auto.value.AutoValue;
import java.io.Serializable;
import org.joda.time.Duration;

/**
Expand Down Expand Up @@ -64,7 +65,9 @@
* is computed which will terminate the call when the total time is up.
*/
@AutoValue
public abstract class RetrySettings {
public abstract class RetrySettings implements Serializable {

private static final long serialVersionUID = 8258475264439710899L;

/**
* The TotalTimeout parameter has ultimate control over how long the logic should keep trying the
Expand All @@ -91,6 +94,13 @@ public abstract class RetrySettings {
*/
public abstract Duration getMaxRetryDelay();

/**
* MaxAttempts defines the maximum number of attempts to perform. The default value is 0. If this
* value is greater than 0, and the number of attempts reaches this limit, the logic will give up
* retrying even if the total retry time is still lower than TotalTimeout.
*/
public abstract int getMaxAttempts();

/**
* The InitialRpcTimeout parameter controls the timeout for the initial RPC. Subsequent calls will
* use this value adjusted according to the RpcTimeoutMultiplier.
Expand All @@ -110,7 +120,7 @@ public abstract class RetrySettings {
public abstract Duration getMaxRpcTimeout();

public static Builder newBuilder() {
return new AutoValue_RetrySettings.Builder();
return new AutoValue_RetrySettings.Builder().setMaxAttempts(0);
}

public Builder toBuilder() {
Expand Down Expand Up @@ -150,6 +160,13 @@ public abstract static class Builder {
*/
public abstract Builder setMaxRetryDelay(Duration maxDelay);

/**
* MaxAttempts defines the maximum number of attempts to perform. If number of attempts reaches
* this limit the logic will give up retrying even if the total retry time is still lower than
* TotalTimeout.
*/
public abstract Builder setMaxAttempts(int maxAttempts);

This comment was marked as spam.

This comment was marked as spam.


/**
* The InitialRpcTimeout parameter controls the timeout for the initial RPC. Subsequent calls
* will use this value adjusted according to the RpcTimeoutMultiplier.
Expand Down Expand Up @@ -188,6 +205,13 @@ public abstract static class Builder {
*/
public abstract double getRetryDelayMultiplier();

/**
* MaxAttempts defines the maximum number of attempts to perform. If the number of attempts
* reaches this limit, the logic will give up retrying even if the total retry time is still
* lower than TotalTimeout.
*/
public abstract int getMaxAttempts();

/**
* The MaxRetryDelay puts a limit on the value of the retry delay, so that the
* RetryDelayMultiplier can't increase the retry delay higher than this amount.
Expand Down Expand Up @@ -228,6 +252,9 @@ public RetrySettings build() {
if (params.getMaxRetryDelay().compareTo(params.getInitialRetryDelay()) < 0) {
throw new IllegalStateException("max retry delay must not be shorter than initial delay");
}
if (params.getMaxAttempts() < 0) {
throw new IllegalStateException("max attempts must be non-negative");
}
if (params.getInitialRpcTimeout().getMillis() < 0) {
throw new IllegalStateException("initial rpc timeout must not be negative");
}
Expand All @@ -253,6 +280,7 @@ public RetrySettings.Builder merge(RetrySettings.Builder newSettings) {
if (newSettings.getMaxRetryDelay() != null) {
setMaxRetryDelay(newSettings.getMaxRetryDelay());
}
setMaxAttempts(newSettings.getMaxAttempts());
if (newSettings.getInitialRpcTimeout() != null) {
setInitialRpcTimeout(newSettings.getInitialRpcTimeout());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
@ExperimentalApi
public class ApiFutureToListenableFuture<V> implements ListenableFuture<V> {
private ApiFuture<V> apiFuture;
private final ApiFuture<V> apiFuture;

public ApiFutureToListenableFuture(ApiFuture<V> apiFuture) {
this.apiFuture = apiFuture;
Expand Down
Loading