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 3 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
61 changes: 61 additions & 0 deletions src/main/java/com/google/api/gax/core/DefaultNanoClock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2016, Google Inc. All rights reserved.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

*
* 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;

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

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

public static NanoClock getDefaultClock() {
return DEFAULT_CLOCK;
}

private DefaultNanoClock() {}

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* (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.
Expand All @@ -43,4 +43,9 @@ public interface NanoClock {
* 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();
}
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. If number of attempts reaches

This comment was marked as spam.

* this limit the logic will give up retrying even if the total retry time is still lower than

This comment was marked as spam.

* TotalTimeout.

This comment was marked as spam.

This comment was marked as spam.

*/
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 number of attempts reaches

This comment was marked as spam.

* 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
61 changes: 61 additions & 0 deletions src/main/java/com/google/api/gax/core/SystemClock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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;

public final class SystemClock implements NanoClock, Serializable {

This comment was marked as spam.

This comment was marked as spam.


private static final long serialVersionUID = -6019259882852183285L;
private static final NanoClock DEFAULT_CLOCK = new SystemClock();

public static NanoClock getDefaultClock() {
return DEFAULT_CLOCK;
}

private SystemClock() {}

@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;
}
}
Loading