Skip to content

Commit e4471f9

Browse files
committed
Merge pull request #357 from mziccard/options-default-instance
Add getDefaultInstance method to StorageOptions and DatastoreOptions
2 parents c440ac5 + abc11ce commit e4471f9

File tree

17 files changed

+79
-66
lines changed

17 files changed

+79
-66
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ import com.google.gcloud.datastore.Entity;
121121
import com.google.gcloud.datastore.Key;
122122
import com.google.gcloud.datastore.KeyFactory;
123123
124-
Datastore datastore = DatastoreOptions.getDefaultInstance().service();
124+
Datastore datastore = DatastoreOptions.defaultInstance().service();
125125
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
126126
Key key = keyFactory.newKey(keyName);
127127
Entity entity = datastore.get(key);

gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ void verifyCaller(Callable<?> callable) {
231231
}
232232
}
233233

234-
public Set<Class<? extends Exception>> getRetriableExceptions() {
234+
public Set<Class<? extends Exception>> retriableExceptions() {
235235
return retriableExceptions;
236236
}
237237

238-
public Set<Class<? extends Exception>> getNonRetriableExceptions() {
238+
public Set<Class<? extends Exception>> nonRetriableExceptions() {
239239
return nonRetriableExceptions;
240240
}
241241

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

gcloud-java-core/src/main/java/com/google/gcloud/RetryHelper.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ private V doRetry() throws RetryHelperException {
194194
}
195195
exception = e;
196196
}
197-
if (attemptNumber >= params.getRetryMaxAttempts()
198-
|| attemptNumber >= params.getRetryMinAttempts()
199-
&& stopwatch.elapsed(MILLISECONDS) >= params.getTotalRetryPeriodMillis()) {
197+
if (attemptNumber >= params.retryMaxAttempts()
198+
|| attemptNumber >= params.retryMinAttempts()
199+
&& stopwatch.elapsed(MILLISECONDS) >= params.totalRetryPeriodMillis()) {
200200
throw new RetriesExhaustedException(this + ": Too many failures, giving up", exception);
201201
}
202202
long sleepDurationMillis = getSleepDuration(params, attemptNumber);
@@ -215,9 +215,9 @@ private V doRetry() throws RetryHelperException {
215215

216216
@VisibleForTesting
217217
static long getSleepDuration(RetryParams retryParams, int attemptsSoFar) {
218-
long initialDelay = retryParams.getInitialRetryDelayMillis();
219-
double backoffFactor = retryParams.getRetryDelayBackoffFactor();
220-
long maxDelay = retryParams.getMaxRetryDelayMillis();
218+
long initialDelay = retryParams.initialRetryDelayMillis();
219+
double backoffFactor = retryParams.retryDelayBackoffFactor();
220+
long maxDelay = retryParams.maxRetryDelayMillis();
221221
long retryDelay = getExponentialValue(initialDelay, backoffFactor, maxDelay, attemptsSoFar);
222222
return (long) ((random() / 2.0 + .75) * retryDelay);
223223
}
@@ -228,8 +228,8 @@ private static long getExponentialValue(long initialDelay, double backoffFactor,
228228
}
229229

230230
public static <V> V runWithRetries(Callable<V> callable) throws RetryHelperException {
231-
return runWithRetries(callable, RetryParams.getDefaultInstance(),
232-
ExceptionHandler.getDefaultInstance());
231+
return runWithRetries(callable, RetryParams.defaultInstance(),
232+
ExceptionHandler.defaultInstance());
233233
}
234234

235235
public static <V> V runWithRetries(Callable<V> callable, RetryParams params,

gcloud-java-core/src/main/java/com/google/gcloud/RetryParams.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
* {@code RetryParams}, first create a {@link RetryParams.Builder}. The builder is mutable and each
3939
* of the parameters can be set (any unset parameters will fallback to the defaults). The
4040
* {@code Builder} can be then used to create an immutable {@code RetryParams} object. For default
41-
* {@code RetryParams} use {@link #getDefaultInstance}. Default settings are subject to change
42-
* release to release. If you require specific settings, explicitly create an instance of
41+
* {@code RetryParams} use {@link #defaultInstance}. Default settings are subject to change release
42+
* to release. If you require specific settings, explicitly create an instance of
4343
* {@code RetryParams} with all the required settings.
4444
*
4545
* @see RetryHelper
@@ -91,12 +91,12 @@ private Builder() {
9191
retryDelayBackoffFactor = DEFAULT_RETRY_DELAY_BACKOFF_FACTOR;
9292
totalRetryPeriodMillis = DEFAULT_TOTAL_RETRY_PERIOD_MILLIS;
9393
} else {
94-
retryMinAttempts = retryParams.getRetryMinAttempts();
95-
retryMaxAttempts = retryParams.getRetryMaxAttempts();
96-
initialRetryDelayMillis = retryParams.getInitialRetryDelayMillis();
97-
maxRetryDelayMillis = retryParams.getMaxRetryDelayMillis();
98-
retryDelayBackoffFactor = retryParams.getRetryDelayBackoffFactor();
99-
totalRetryPeriodMillis = retryParams.getTotalRetryPeriodMillis();
94+
retryMinAttempts = retryParams.retryMinAttempts();
95+
retryMaxAttempts = retryParams.retryMaxAttempts();
96+
initialRetryDelayMillis = retryParams.initialRetryDelayMillis();
97+
maxRetryDelayMillis = retryParams.maxRetryDelayMillis();
98+
retryDelayBackoffFactor = retryParams.retryDelayBackoffFactor();
99+
totalRetryPeriodMillis = retryParams.totalRetryPeriodMillis();
100100
}
101101
}
102102

@@ -201,7 +201,7 @@ private RetryParams(Builder builder) {
201201
/**
202202
* Returns an instance with the default parameters.
203203
*/
204-
public static RetryParams getDefaultInstance() {
204+
public static RetryParams defaultInstance() {
205205
return DEFAULT_INSTANCE;
206206
}
207207

@@ -216,45 +216,45 @@ public static RetryParams noRetries() {
216216
/**
217217
* Returns the retryMinAttempts. Default value is {@value #DEFAULT_RETRY_MIN_ATTEMPTS}.
218218
*/
219-
public int getRetryMinAttempts() {
219+
public int retryMinAttempts() {
220220
return retryMinAttempts;
221221
}
222222

223223
/**
224224
* Returns the retryMaxAttempts. Default value is {@value #DEFAULT_RETRY_MAX_ATTEMPTS}.
225225
*/
226-
public int getRetryMaxAttempts() {
226+
public int retryMaxAttempts() {
227227
return retryMaxAttempts;
228228
}
229229

230230
/**
231231
* Returns the initialRetryDelayMillis. Default value is
232232
* {@value #DEFAULT_INITIAL_RETRY_DELAY_MILLIS}.
233233
*/
234-
public long getInitialRetryDelayMillis() {
234+
public long initialRetryDelayMillis() {
235235
return initialRetryDelayMillis;
236236
}
237237

238238
/**
239239
* Returns the maxRetryDelayMillis. Default values is {@value #DEFAULT_MAX_RETRY_DELAY_MILLIS}.
240240
*/
241-
public long getMaxRetryDelayMillis() {
241+
public long maxRetryDelayMillis() {
242242
return maxRetryDelayMillis;
243243
}
244244

245245
/**
246246
* Returns the maxRetryDelayBackoffFactor. Default values is
247247
* {@value #DEFAULT_RETRY_DELAY_BACKOFF_FACTOR}.
248248
*/
249-
public double getRetryDelayBackoffFactor() {
249+
public double retryDelayBackoffFactor() {
250250
return retryDelayBackoffFactor;
251251
}
252252

253253
/**
254254
* Returns the totalRetryPeriodMillis. Default value is
255255
* {@value #DEFAULT_TOTAL_RETRY_PERIOD_MILLIS}.
256256
*/
257-
public long getTotalRetryPeriodMillis() {
257+
public long totalRetryPeriodMillis() {
258258
return totalRetryPeriodMillis;
259259
}
260260

gcloud-java-core/src/test/java/com/google/gcloud/ExceptionHandlerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public Object call() throws Error {
8282
}
8383

8484
// using default exception handler (retry upon any non-runtime exceptions)
85-
ExceptionHandler handler = ExceptionHandler.getDefaultInstance();
85+
ExceptionHandler handler = ExceptionHandler.defaultInstance();
8686
assertValidCallable(new A(), handler);
8787
assertValidCallable(new B(), handler);
8888
assertValidCallable(new C(), handler);

gcloud-java-core/src/test/java/com/google/gcloud/RetryHelperTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ public void testTriesAtLeastMinTimes() {
118118
@Override public Integer call() throws IOException {
119119
timesCalled++;
120120
assertEquals(timesCalled, RetryHelper.getContext().getAttemptNumber());
121-
assertEquals(10, RetryHelper.getContext().getRetryParams().getRetryMaxAttempts());
121+
assertEquals(10, RetryHelper.getContext().getRetryParams().retryMaxAttempts());
122122
if (timesCalled <= timesToFail) {
123123
throw new IOException();
124124
}
125125
return timesCalled;
126126
}
127-
}, params, ExceptionHandler.getDefaultInstance());
127+
}, params, ExceptionHandler.defaultInstance());
128128
assertEquals(timesToFail + 1, attempted);
129129
assertNull(RetryHelper.getContext());
130130
}

gcloud-java-core/src/test/java/com/google/gcloud/RetryParamsTest.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public class RetryParamsTest {
4141

4242
@Test
4343
public void testDefaults() {
44-
RetryParams params1 = RetryParams.getDefaultInstance();
44+
RetryParams params1 = RetryParams.defaultInstance();
4545
RetryParams params2 = RetryParams.builder().build();
4646
for (RetryParams params : Arrays.asList(params1, params2)) {
47-
assertEquals(DEFAULT_INITIAL_RETRY_DELAY_MILLIS, params.getInitialRetryDelayMillis());
48-
assertEquals(DEFAULT_MAX_RETRY_DELAY_MILLIS, params.getMaxRetryDelayMillis());
49-
assertEquals(DEFAULT_RETRY_DELAY_BACKOFF_FACTOR, params.getRetryDelayBackoffFactor(), 0);
50-
assertEquals(DEFAULT_RETRY_MAX_ATTEMPTS, params.getRetryMaxAttempts());
51-
assertEquals(DEFAULT_RETRY_MIN_ATTEMPTS, params.getRetryMinAttempts());
52-
assertEquals(DEFAULT_TOTAL_RETRY_PERIOD_MILLIS, params.getTotalRetryPeriodMillis());
47+
assertEquals(DEFAULT_INITIAL_RETRY_DELAY_MILLIS, params.initialRetryDelayMillis());
48+
assertEquals(DEFAULT_MAX_RETRY_DELAY_MILLIS, params.maxRetryDelayMillis());
49+
assertEquals(DEFAULT_RETRY_DELAY_BACKOFF_FACTOR, params.retryDelayBackoffFactor(), 0);
50+
assertEquals(DEFAULT_RETRY_MAX_ATTEMPTS, params.retryMaxAttempts());
51+
assertEquals(DEFAULT_RETRY_MIN_ATTEMPTS, params.retryMinAttempts());
52+
assertEquals(DEFAULT_TOTAL_RETRY_PERIOD_MILLIS, params.totalRetryPeriodMillis());
5353
}
5454
}
5555

@@ -65,12 +65,12 @@ public void testSetAndCopy() {
6565
RetryParams params1 = builder.build();
6666
RetryParams params2 = new RetryParams.Builder(params1).build();
6767
for (RetryParams params : Arrays.asList(params1, params2)) {
68-
assertEquals(101, params.getInitialRetryDelayMillis());
69-
assertEquals(102, params.getMaxRetryDelayMillis());
70-
assertEquals(103, params.getRetryDelayBackoffFactor(), 0);
71-
assertEquals(107, params.getRetryMinAttempts());
72-
assertEquals(108, params.getRetryMaxAttempts());
73-
assertEquals(109, params.getTotalRetryPeriodMillis());
68+
assertEquals(101, params.initialRetryDelayMillis());
69+
assertEquals(102, params.maxRetryDelayMillis());
70+
assertEquals(103, params.retryDelayBackoffFactor(), 0);
71+
assertEquals(107, params.retryMinAttempts());
72+
assertEquals(108, params.retryMaxAttempts());
73+
assertEquals(109, params.totalRetryPeriodMillis());
7474
}
7575
}
7676

@@ -79,19 +79,19 @@ public void testBadSettings() {
7979
RetryParams.Builder builder = RetryParams.builder();
8080
builder.initialRetryDelayMillis(-1);
8181
builder = assertFailure(builder);
82-
builder.maxRetryDelayMillis(RetryParams.getDefaultInstance().getInitialRetryDelayMillis() - 1);
82+
builder.maxRetryDelayMillis(RetryParams.defaultInstance().initialRetryDelayMillis() - 1);
8383
builder = assertFailure(builder);
8484
builder.retryDelayBackoffFactor(-1);
8585
builder = assertFailure(builder);
8686
builder.retryMinAttempts(-1);
8787
builder = assertFailure(builder);
88-
builder.retryMaxAttempts(RetryParams.getDefaultInstance().getRetryMinAttempts() - 1);
88+
builder.retryMaxAttempts(RetryParams.defaultInstance().retryMinAttempts() - 1);
8989
builder = assertFailure(builder);
9090
builder.totalRetryPeriodMillis(-1);
9191
builder = assertFailure(builder);
9292
// verify that it is OK for min and max to be equal
93-
builder.retryMaxAttempts(RetryParams.getDefaultInstance().getRetryMinAttempts());
94-
builder.maxRetryDelayMillis(RetryParams.getDefaultInstance().getInitialRetryDelayMillis());
93+
builder.retryMaxAttempts(RetryParams.defaultInstance().retryMinAttempts());
94+
builder.maxRetryDelayMillis(RetryParams.defaultInstance().initialRetryDelayMillis());
9595
builder.build();
9696
}
9797

gcloud-java-datastore/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import com.google.gcloud.datastore.Entity;
6666
import com.google.gcloud.datastore.Key;
6767
import com.google.gcloud.datastore.KeyFactory;
6868

69-
Datastore datastore = DatastoreOptions.getDefaultInstance().service();
69+
Datastore datastore = DatastoreOptions.defaultInstance().service();
7070
KeyFactory keyFactory = datastore.newKeyFactory().kind(KIND);
7171
Key key = keyFactory.newKey(keyName);
7272
Entity entity = datastore.get(key);

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java

+7
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ public String namespace() {
171171
return namespace;
172172
}
173173

174+
/**
175+
* Returns a default {@code DatastoreOptions} instance.
176+
*/
177+
public static DatastoreOptions defaultInstance() {
178+
return builder().build();
179+
}
180+
174181
private static String defaultNamespace() {
175182
try {
176183
Class<?> clazz = Class.forName("com.google.appengine.api.NamespaceManager");

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ private Datastore createDatastoreForDeferredLookup() throws DatastoreRpcExceptio
625625
EasyMock.replay(rpcFactoryMock, rpcMock);
626626
DatastoreOptions options =
627627
this.options.toBuilder()
628-
.retryParams(RetryParams.getDefaultInstance())
628+
.retryParams(RetryParams.defaultInstance())
629629
.serviceRpcFactory(rpcFactoryMock)
630630
.build();
631631
return options.service();
@@ -738,7 +738,7 @@ public void testRetryableException() throws Exception {
738738
.andReturn(responsePb);
739739
EasyMock.replay(rpcFactoryMock, rpcMock);
740740
DatastoreOptions options = this.options.toBuilder()
741-
.retryParams(RetryParams.getDefaultInstance())
741+
.retryParams(RetryParams.defaultInstance())
742742
.serviceRpcFactory(rpcFactoryMock)
743743
.build();
744744
Datastore datastore = options.service();
@@ -784,7 +784,7 @@ public void testRuntimeException() throws Exception {
784784
.andThrow(new RuntimeException(exceptionMessage));
785785
EasyMock.replay(rpcFactoryMock, rpcMock);
786786
DatastoreOptions options = this.options.toBuilder()
787-
.retryParams(RetryParams.getDefaultInstance())
787+
.retryParams(RetryParams.defaultInstance())
788788
.serviceRpcFactory(rpcFactoryMock)
789789
.build();
790790
Datastore datastore = options.service();

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public void testServiceOptions() throws Exception {
143143

144144
options = options.toBuilder()
145145
.namespace("ns1")
146-
.retryParams(RetryParams.getDefaultInstance())
146+
.retryParams(RetryParams.defaultInstance())
147147
.authCredentials(AuthCredentials.noCredentials())
148148
.force(true)
149149
.build();

gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.google.gcloud.AuthCredentials;
2020
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
21-
import com.google.gcloud.Page;
2221
import com.google.gcloud.RetryParams;
2322
import com.google.gcloud.spi.StorageRpc.Tuple;
2423
import com.google.gcloud.storage.Blob;
@@ -546,7 +545,7 @@ public static void main(String... args) throws Exception {
546545
return;
547546
}
548547
StorageOptions.Builder optionsBuilder =
549-
StorageOptions.builder().retryParams(RetryParams.getDefaultInstance());
548+
StorageOptions.builder().retryParams(RetryParams.defaultInstance());
550549
StorageAction action;
551550
String actionName;
552551
if (args.length >= 2 && !ACTIONS.containsKey(args[0])) {

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageOptions.java

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ public String pathDelimiter() {
111111
return pathDelimiter;
112112
}
113113

114+
/**
115+
* Returns a default {@code StorageOptions} instance.
116+
*/
117+
public static StorageOptions defaultInstance() {
118+
return builder().build();
119+
}
120+
114121
@Override
115122
public Builder toBuilder() {
116123
return new Builder(this);

gcloud-java-storage/src/test/java/com/google/gcloud/storage/RemoteGcsHelperTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ public void testCreateFromStream() {
159159
assertEquals(PROJECT_ID, options.projectId());
160160
assertEquals(60000, options.connectTimeout());
161161
assertEquals(60000, options.readTimeout());
162-
assertEquals(10, options.retryParams().getRetryMaxAttempts());
163-
assertEquals(6, options.retryParams().getRetryMinAttempts());
164-
assertEquals(30000, options.retryParams().getMaxRetryDelayMillis());
165-
assertEquals(120000, options.retryParams().getTotalRetryPeriodMillis());
166-
assertEquals(250, options.retryParams().getInitialRetryDelayMillis());
162+
assertEquals(10, options.retryParams().retryMaxAttempts());
163+
assertEquals(6, options.retryParams().retryMinAttempts());
164+
assertEquals(30000, options.retryParams().maxRetryDelayMillis());
165+
assertEquals(120000, options.retryParams().totalRetryPeriodMillis());
166+
assertEquals(250, options.retryParams().initialRetryDelayMillis());
167167
}
168168

169169
@Test

gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void testServiceOptions() throws Exception {
8282

8383
options = options.toBuilder()
8484
.projectId("p2")
85-
.retryParams(RetryParams.getDefaultInstance())
85+
.retryParams(RetryParams.defaultInstance())
8686
.authCredentials(AuthCredentials.noCredentials())
8787
.pathDelimiter(":")
8888
.build();
@@ -110,7 +110,7 @@ public void testModelAndRequests() throws Exception {
110110
public void testReadChannelState() throws IOException, ClassNotFoundException {
111111
StorageOptions options = StorageOptions.builder()
112112
.projectId("p2")
113-
.retryParams(RetryParams.getDefaultInstance())
113+
.retryParams(RetryParams.defaultInstance())
114114
.authCredentials(AuthCredentials.noCredentials())
115115
.build();
116116
BlobReadChannel reader =
@@ -126,7 +126,7 @@ public void testReadChannelState() throws IOException, ClassNotFoundException {
126126
public void testWriteChannelState() throws IOException, ClassNotFoundException {
127127
StorageOptions options = StorageOptions.builder()
128128
.projectId("p2")
129-
.retryParams(RetryParams.getDefaultInstance())
129+
.retryParams(RetryParams.defaultInstance())
130130
.authCredentials(AuthCredentials.noCredentials())
131131
.build();
132132
BlobWriteChannelImpl writer = new BlobWriteChannelImpl(

0 commit comments

Comments
 (0)