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

Add getDefaultInstance method to StorageOptions and DatastoreOptions #357

Merged
merged 5 commits into from
Nov 11, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;

Datastore datastore = DatastoreOptions.getDefaultInstance().service();
Datastore datastore = DatastoreOptions.defaultInstance().service();
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
Key key = keyFactory.newKey(keyName);
Entity entity = datastore.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ void verifyCaller(Callable<?> callable) {
}
}

public Set<Class<? extends Exception>> getRetriableExceptions() {
public Set<Class<? extends Exception>> retriableExceptions() {
return retriableExceptions;
}

public Set<Class<? extends Exception>> getNonRetriableExceptions() {
public Set<Class<? extends Exception>> nonRetriableExceptions() {
return nonRetriableExceptions;
}

Expand All @@ -262,7 +262,7 @@ boolean shouldRetry(Exception ex) {
/**
* Returns an instance which retry any checked exception and abort on any runtime exception.
*/
public static ExceptionHandler getDefaultInstance() {
public static ExceptionHandler defaultInstance() {
return DEFAULT_INSTANCE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ private V doRetry() throws RetryHelperException {
}
exception = e;
}
if (attemptNumber >= params.getRetryMaxAttempts()
|| attemptNumber >= params.getRetryMinAttempts()
&& stopwatch.elapsed(MILLISECONDS) >= params.getTotalRetryPeriodMillis()) {
if (attemptNumber >= params.retryMaxAttempts()
|| attemptNumber >= params.retryMinAttempts()
&& stopwatch.elapsed(MILLISECONDS) >= params.totalRetryPeriodMillis()) {
throw new RetriesExhaustedException(this + ": Too many failures, giving up", exception);
}
long sleepDurationMillis = getSleepDuration(params, attemptNumber);
Expand All @@ -215,9 +215,9 @@ private V doRetry() throws RetryHelperException {

@VisibleForTesting
static long getSleepDuration(RetryParams retryParams, int attemptsSoFar) {
long initialDelay = retryParams.getInitialRetryDelayMillis();
double backoffFactor = retryParams.getRetryDelayBackoffFactor();
long maxDelay = retryParams.getMaxRetryDelayMillis();
long initialDelay = retryParams.initialRetryDelayMillis();
double backoffFactor = retryParams.retryDelayBackoffFactor();
long maxDelay = retryParams.maxRetryDelayMillis();
long retryDelay = getExponentialValue(initialDelay, backoffFactor, maxDelay, attemptsSoFar);
return (long) ((random() / 2.0 + .75) * retryDelay);
}
Expand All @@ -228,8 +228,8 @@ private static long getExponentialValue(long initialDelay, double backoffFactor,
}

public static <V> V runWithRetries(Callable<V> callable) throws RetryHelperException {
return runWithRetries(callable, RetryParams.getDefaultInstance(),
ExceptionHandler.getDefaultInstance());
return runWithRetries(callable, RetryParams.defaultInstance(),
ExceptionHandler.defaultInstance());
}

public static <V> V runWithRetries(Callable<V> callable, RetryParams params,
Expand Down
30 changes: 15 additions & 15 deletions gcloud-java-core/src/main/java/com/google/gcloud/RetryParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
* {@code RetryParams}, first create a {@link RetryParams.Builder}. The builder is mutable and each
* of the parameters can be set (any unset parameters will fallback to the defaults). The
* {@code Builder} can be then used to create an immutable {@code RetryParams} object. For default
* {@code RetryParams} use {@link #getDefaultInstance}. Default settings are subject to change
* release to release. If you require specific settings, explicitly create an instance of
* {@code RetryParams} use {@link #defaultInstance}. Default settings are subject to change release
* to release. If you require specific settings, explicitly create an instance of
* {@code RetryParams} with all the required settings.
*
* @see RetryHelper
Expand Down Expand Up @@ -91,12 +91,12 @@ private Builder() {
retryDelayBackoffFactor = DEFAULT_RETRY_DELAY_BACKOFF_FACTOR;
totalRetryPeriodMillis = DEFAULT_TOTAL_RETRY_PERIOD_MILLIS;
} else {
retryMinAttempts = retryParams.getRetryMinAttempts();
retryMaxAttempts = retryParams.getRetryMaxAttempts();
initialRetryDelayMillis = retryParams.getInitialRetryDelayMillis();
maxRetryDelayMillis = retryParams.getMaxRetryDelayMillis();
retryDelayBackoffFactor = retryParams.getRetryDelayBackoffFactor();
totalRetryPeriodMillis = retryParams.getTotalRetryPeriodMillis();
retryMinAttempts = retryParams.retryMinAttempts();
retryMaxAttempts = retryParams.retryMaxAttempts();
initialRetryDelayMillis = retryParams.initialRetryDelayMillis();
maxRetryDelayMillis = retryParams.maxRetryDelayMillis();
retryDelayBackoffFactor = retryParams.retryDelayBackoffFactor();
totalRetryPeriodMillis = retryParams.totalRetryPeriodMillis();
}
}

Expand Down Expand Up @@ -201,7 +201,7 @@ private RetryParams(Builder builder) {
/**
* Returns an instance with the default parameters.
*/
public static RetryParams getDefaultInstance() {
public static RetryParams defaultInstance() {
return DEFAULT_INSTANCE;
}

Expand All @@ -216,45 +216,45 @@ public static RetryParams noRetries() {
/**
* Returns the retryMinAttempts. Default value is {@value #DEFAULT_RETRY_MIN_ATTEMPTS}.
*/
public int getRetryMinAttempts() {
public int retryMinAttempts() {
return retryMinAttempts;
}

/**
* Returns the retryMaxAttempts. Default value is {@value #DEFAULT_RETRY_MAX_ATTEMPTS}.
*/
public int getRetryMaxAttempts() {
public int retryMaxAttempts() {
return retryMaxAttempts;
}

/**
* Returns the initialRetryDelayMillis. Default value is
* {@value #DEFAULT_INITIAL_RETRY_DELAY_MILLIS}.
*/
public long getInitialRetryDelayMillis() {
public long initialRetryDelayMillis() {
return initialRetryDelayMillis;
}

/**
* Returns the maxRetryDelayMillis. Default values is {@value #DEFAULT_MAX_RETRY_DELAY_MILLIS}.
*/
public long getMaxRetryDelayMillis() {
public long maxRetryDelayMillis() {
return maxRetryDelayMillis;
}

/**
* Returns the maxRetryDelayBackoffFactor. Default values is
* {@value #DEFAULT_RETRY_DELAY_BACKOFF_FACTOR}.
*/
public double getRetryDelayBackoffFactor() {
public double retryDelayBackoffFactor() {
return retryDelayBackoffFactor;
}

/**
* Returns the totalRetryPeriodMillis. Default value is
* {@value #DEFAULT_TOTAL_RETRY_PERIOD_MILLIS}.
*/
public long getTotalRetryPeriodMillis() {
public long totalRetryPeriodMillis() {
return totalRetryPeriodMillis;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Object call() throws Error {
}

// using default exception handler (retry upon any non-runtime exceptions)
ExceptionHandler handler = ExceptionHandler.getDefaultInstance();
ExceptionHandler handler = ExceptionHandler.defaultInstance();
assertValidCallable(new A(), handler);
assertValidCallable(new B(), handler);
assertValidCallable(new C(), handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ public void testTriesAtLeastMinTimes() {
@Override public Integer call() throws IOException {
timesCalled++;
assertEquals(timesCalled, RetryHelper.getContext().getAttemptNumber());
assertEquals(10, RetryHelper.getContext().getRetryParams().getRetryMaxAttempts());
assertEquals(10, RetryHelper.getContext().getRetryParams().retryMaxAttempts());
if (timesCalled <= timesToFail) {
throw new IOException();
}
return timesCalled;
}
}, params, ExceptionHandler.getDefaultInstance());
}, params, ExceptionHandler.defaultInstance());
assertEquals(timesToFail + 1, attempted);
assertNull(RetryHelper.getContext());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public class RetryParamsTest {

@Test
public void testDefaults() {
RetryParams params1 = RetryParams.getDefaultInstance();
RetryParams params1 = RetryParams.defaultInstance();
RetryParams params2 = RetryParams.builder().build();
for (RetryParams params : Arrays.asList(params1, params2)) {
assertEquals(DEFAULT_INITIAL_RETRY_DELAY_MILLIS, params.getInitialRetryDelayMillis());
assertEquals(DEFAULT_MAX_RETRY_DELAY_MILLIS, params.getMaxRetryDelayMillis());
assertEquals(DEFAULT_RETRY_DELAY_BACKOFF_FACTOR, params.getRetryDelayBackoffFactor(), 0);
assertEquals(DEFAULT_RETRY_MAX_ATTEMPTS, params.getRetryMaxAttempts());
assertEquals(DEFAULT_RETRY_MIN_ATTEMPTS, params.getRetryMinAttempts());
assertEquals(DEFAULT_TOTAL_RETRY_PERIOD_MILLIS, params.getTotalRetryPeriodMillis());
assertEquals(DEFAULT_INITIAL_RETRY_DELAY_MILLIS, params.initialRetryDelayMillis());
assertEquals(DEFAULT_MAX_RETRY_DELAY_MILLIS, params.maxRetryDelayMillis());
assertEquals(DEFAULT_RETRY_DELAY_BACKOFF_FACTOR, params.retryDelayBackoffFactor(), 0);
assertEquals(DEFAULT_RETRY_MAX_ATTEMPTS, params.retryMaxAttempts());
assertEquals(DEFAULT_RETRY_MIN_ATTEMPTS, params.retryMinAttempts());
assertEquals(DEFAULT_TOTAL_RETRY_PERIOD_MILLIS, params.totalRetryPeriodMillis());
}
}

Expand All @@ -65,12 +65,12 @@ public void testSetAndCopy() {
RetryParams params1 = builder.build();
RetryParams params2 = new RetryParams.Builder(params1).build();
for (RetryParams params : Arrays.asList(params1, params2)) {
assertEquals(101, params.getInitialRetryDelayMillis());
assertEquals(102, params.getMaxRetryDelayMillis());
assertEquals(103, params.getRetryDelayBackoffFactor(), 0);
assertEquals(107, params.getRetryMinAttempts());
assertEquals(108, params.getRetryMaxAttempts());
assertEquals(109, params.getTotalRetryPeriodMillis());
assertEquals(101, params.initialRetryDelayMillis());
assertEquals(102, params.maxRetryDelayMillis());
assertEquals(103, params.retryDelayBackoffFactor(), 0);
assertEquals(107, params.retryMinAttempts());
assertEquals(108, params.retryMaxAttempts());
assertEquals(109, params.totalRetryPeriodMillis());
}
}

Expand All @@ -79,19 +79,19 @@ public void testBadSettings() {
RetryParams.Builder builder = RetryParams.builder();
builder.initialRetryDelayMillis(-1);
builder = assertFailure(builder);
builder.maxRetryDelayMillis(RetryParams.getDefaultInstance().getInitialRetryDelayMillis() - 1);
builder.maxRetryDelayMillis(RetryParams.defaultInstance().initialRetryDelayMillis() - 1);
builder = assertFailure(builder);
builder.retryDelayBackoffFactor(-1);
builder = assertFailure(builder);
builder.retryMinAttempts(-1);
builder = assertFailure(builder);
builder.retryMaxAttempts(RetryParams.getDefaultInstance().getRetryMinAttempts() - 1);
builder.retryMaxAttempts(RetryParams.defaultInstance().retryMinAttempts() - 1);
builder = assertFailure(builder);
builder.totalRetryPeriodMillis(-1);
builder = assertFailure(builder);
// verify that it is OK for min and max to be equal
builder.retryMaxAttempts(RetryParams.getDefaultInstance().getRetryMinAttempts());
builder.maxRetryDelayMillis(RetryParams.getDefaultInstance().getInitialRetryDelayMillis());
builder.retryMaxAttempts(RetryParams.defaultInstance().retryMinAttempts());
builder.maxRetryDelayMillis(RetryParams.defaultInstance().initialRetryDelayMillis());
builder.build();
}

Expand Down
2 changes: 1 addition & 1 deletion gcloud-java-datastore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;

Datastore datastore = DatastoreOptions.getDefaultInstance().service();
Datastore datastore = DatastoreOptions.defaultInstance().service();
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
Key key = keyFactory.newKey(keyName);
Entity entity = datastore.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ public String namespace() {
return namespace;
}

/**
* Returns a default {@code DatastoreOptions} instance.
*/
public static DatastoreOptions defaultInstance() {
return builder().build();
}

private static String defaultNamespace() {
try {
Class<?> clazz = Class.forName("com.google.appengine.api.NamespaceManager");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ private Datastore createDatastoreForDeferredLookup() throws DatastoreRpcExceptio
EasyMock.replay(rpcFactoryMock, rpcMock);
DatastoreOptions options =
this.options.toBuilder()
.retryParams(RetryParams.getDefaultInstance())
.retryParams(RetryParams.defaultInstance())
.serviceRpcFactory(rpcFactoryMock)
.build();
return options.service();
Expand Down Expand Up @@ -738,7 +738,7 @@ public void testRetryableException() throws Exception {
.andReturn(responsePb);
EasyMock.replay(rpcFactoryMock, rpcMock);
DatastoreOptions options = this.options.toBuilder()
.retryParams(RetryParams.getDefaultInstance())
.retryParams(RetryParams.defaultInstance())
.serviceRpcFactory(rpcFactoryMock)
.build();
Datastore datastore = options.service();
Expand Down Expand Up @@ -784,7 +784,7 @@ public void testRuntimeException() throws Exception {
.andThrow(new RuntimeException(exceptionMessage));
EasyMock.replay(rpcFactoryMock, rpcMock);
DatastoreOptions options = this.options.toBuilder()
.retryParams(RetryParams.getDefaultInstance())
.retryParams(RetryParams.defaultInstance())
.serviceRpcFactory(rpcFactoryMock)
.build();
Datastore datastore = options.service();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void testServiceOptions() throws Exception {

options = options.toBuilder()
.namespace("ns1")
.retryParams(RetryParams.getDefaultInstance())
.retryParams(RetryParams.defaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.force(true)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.google.gcloud.AuthCredentials;
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
import com.google.gcloud.Page;
import com.google.gcloud.RetryParams;
import com.google.gcloud.spi.StorageRpc.Tuple;
import com.google.gcloud.storage.Blob;
Expand Down Expand Up @@ -546,7 +545,7 @@ public static void main(String... args) throws Exception {
return;
}
StorageOptions.Builder optionsBuilder =
StorageOptions.builder().retryParams(RetryParams.getDefaultInstance());
StorageOptions.builder().retryParams(RetryParams.defaultInstance());
StorageAction action;
String actionName;
if (args.length >= 2 && !ACTIONS.containsKey(args[0])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ public String pathDelimiter() {
return pathDelimiter;
}

/**
* Returns a default {@code StorageOptions} instance.
*/
public static StorageOptions defaultInstance() {
return builder().build();
}

@Override
public Builder toBuilder() {
return new Builder(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ public void testCreateFromStream() {
assertEquals(PROJECT_ID, options.projectId());
assertEquals(60000, options.connectTimeout());
assertEquals(60000, options.readTimeout());
assertEquals(10, options.retryParams().getRetryMaxAttempts());
assertEquals(6, options.retryParams().getRetryMinAttempts());
assertEquals(30000, options.retryParams().getMaxRetryDelayMillis());
assertEquals(120000, options.retryParams().getTotalRetryPeriodMillis());
assertEquals(250, options.retryParams().getInitialRetryDelayMillis());
assertEquals(10, options.retryParams().retryMaxAttempts());
assertEquals(6, options.retryParams().retryMinAttempts());
assertEquals(30000, options.retryParams().maxRetryDelayMillis());
assertEquals(120000, options.retryParams().totalRetryPeriodMillis());
assertEquals(250, options.retryParams().initialRetryDelayMillis());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void testServiceOptions() throws Exception {

options = options.toBuilder()
.projectId("p2")
.retryParams(RetryParams.getDefaultInstance())
.retryParams(RetryParams.defaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.pathDelimiter(":")
.build();
Expand Down Expand Up @@ -110,7 +110,7 @@ public void testModelAndRequests() throws Exception {
public void testReadChannelState() throws IOException, ClassNotFoundException {
StorageOptions options = StorageOptions.builder()
.projectId("p2")
.retryParams(RetryParams.getDefaultInstance())
.retryParams(RetryParams.defaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.build();
BlobReadChannel reader =
Expand All @@ -126,7 +126,7 @@ public void testReadChannelState() throws IOException, ClassNotFoundException {
public void testWriteChannelState() throws IOException, ClassNotFoundException {
StorageOptions options = StorageOptions.builder()
.projectId("p2")
.retryParams(RetryParams.getDefaultInstance())
.retryParams(RetryParams.defaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.build();
BlobWriteChannelImpl writer = new BlobWriteChannelImpl(
Expand Down
Loading