From 2dfc76103c869fdb3ffd8909c24a9ccc09b1f239 Mon Sep 17 00:00:00 2001 From: vam Date: Fri, 10 Mar 2017 17:15:06 -0800 Subject: [PATCH 1/5] Reconcile RetrySettings & Clock (google-cloud-java part) Reconcile RetrySettings in GAX with RetryParams in google-cloud-java GoogleCloudPlatform/google-cloud-java#1574 Reconcile NanoClock in GAX with Clock in google-cloud-java GoogleCloudPlatform/google-cloud-java#1575 --- .../cloud/bigquery/BigQueryException.java | 4 +- .../google/cloud/bigquery/BigQueryImpl.java | 34 +- .../java/com/google/cloud/bigquery/Job.java | 8 +- .../cloud/bigquery/TableDataWriteChannel.java | 4 +- .../testing/RemoteBigQueryHelper.java | 33 +- .../cloud/bigquery/BigQueryImplTest.java | 596 ++++++------ .../com/google/cloud/bigquery/JobTest.java | 19 +- .../testing/RemoteBigQueryHelperTest.java | 10 +- .../cloud/compute/ComputeException.java | 4 +- .../com/google/cloud/compute/ComputeImpl.java | 130 +-- .../com/google/cloud/compute/Operation.java | 8 +- .../compute/testing/RemoteComputeHelper.java | 33 +- .../google/cloud/compute/ComputeImplTest.java | 103 ++- .../google/cloud/compute/OperationTest.java | 19 +- .../cloud/compute/SerializationTest.java | 4 +- .../testing/RemoteComputeHelperTest.java | 10 +- .../google/cloud/BaseServiceException.java | 5 +- .../src/main/java/com/google/cloud/Clock.java | 59 -- .../com/google/cloud/ExceptionHandler.java | 16 +- .../com/google/cloud/GrpcServiceOptions.java | 12 +- .../java/com/google/cloud/RetryHelper.java | 248 +---- .../java/com/google/cloud/RetryParams.java | 325 ------- .../java/com/google/cloud/ServiceOptions.java | 96 +- .../cloud/BaseServiceExceptionTest.java | 2 +- .../com/google/cloud/RetryHelperTest.java | 263 ------ .../com/google/cloud/RetryParamsTest.java | 110 --- .../com/google/cloud/SerializationTest.java | 79 +- .../com/google/cloud/ServiceOptionsTest.java | 87 +- .../cloud/datastore/DatastoreException.java | 4 +- .../google/cloud/datastore/DatastoreImpl.java | 25 +- .../testing/LocalDatastoreHelper.java | 4 +- .../testing/RemoteDatastoreHelper.java | 33 +- .../cloud/datastore/DatastoreOptionsTest.java | 2 +- .../google/cloud/datastore/DatastoreTest.java | 4 +- .../com/google/cloud/dns/DnsException.java | 4 +- .../java/com/google/cloud/dns/DnsImpl.java | 18 +- .../com/google/cloud/dns/DnsImplTest.java | 17 +- .../google/cloud/dns/SerializationTest.java | 4 +- .../logging/testing/RemoteLoggingHelper.java | 27 +- .../google/cloud/logging/LoggingImplTest.java | 4 +- .../pubsub/deprecated/AckDeadlineRenewer.java | 12 +- .../deprecated/testing/LocalPubSubHelper.java | 4 +- .../pubsub/spi/v1/MessageDispatcher.java | 16 +- .../spi/v1/PollingSubscriberConnection.java | 4 +- .../spi/v1/StreamingSubscriberConnection.java | 4 +- .../cloud/pubsub/spi/v1/Subscriber.java | 11 +- .../deprecated/AckDeadlineRenewerTest.java | 12 +- .../pubsub/deprecated/PubSubImplTest.java | 5 +- .../google/cloud/pubsub/spi/v1/FakeClock.java | 11 +- .../spi/v1/FakeScheduledExecutorService.java | 12 +- .../ResourceManagerException.java | 4 +- .../resourcemanager/ResourceManagerImpl.java | 18 +- .../com/google/cloud/spanner/Operation.java | 21 +- .../google/cloud/spanner/OperationTest.java | 6 +- .../google/cloud/storage/BlobReadChannel.java | 2 +- .../cloud/storage/BlobWriteChannel.java | 4 +- .../com/google/cloud/storage/CopyWriter.java | 2 +- .../cloud/storage/StorageException.java | 4 +- .../com/google/cloud/storage/StorageImpl.java | 58 +- .../storage/testing/RemoteStorageHelper.java | 32 +- .../cloud/storage/BlobReadChannelTest.java | 4 +- .../google/cloud/storage/CopyWriterTest.java | 4 +- .../google/cloud/storage/StorageImplTest.java | 849 +++++++++++------- .../testing/RemoteStorageHelperTest.java | 10 +- .../cloud/translate/TranslateException.java | 4 +- .../google/cloud/translate/TranslateImpl.java | 6 +- .../testing/RemoteTranslateHelper.java | 33 +- .../cloud/translate/TranslateImplTest.java | 100 ++- pom.xml | 2 +- 69 files changed, 1617 insertions(+), 2104 deletions(-) delete mode 100644 google-cloud-core/src/main/java/com/google/cloud/Clock.java delete mode 100644 google-cloud-core/src/main/java/com/google/cloud/RetryParams.java delete mode 100644 google-cloud-core/src/test/java/com/google/cloud/RetryHelperTest.java delete mode 100644 google-cloud-core/src/test/java/com/google/cloud/RetryParamsTest.java diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryException.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryException.java index ffae57621909..95c5a16bb275 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryException.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryException.java @@ -18,7 +18,6 @@ import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; -import com.google.cloud.RetryHelper.RetryInterruptedException; import com.google.common.collect.ImmutableSet; import java.io.IOException; @@ -102,10 +101,9 @@ public int hashCode() { * always throw an exception. * * @throws BigQueryException when {@code ex} was caused by a {@code BigQueryException} - * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ static BaseServiceException translateAndThrow(RetryHelperException ex) { - BaseServiceException.translateAndPropagateIfPossible(ex); + BaseServiceException.translate(ex); throw new BigQueryException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index 101f84e0dbe3..f8daf93edc6c 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -171,7 +171,7 @@ public Dataset create(DatasetInfo datasetInfo, DatasetOption... options) { public com.google.api.services.bigquery.model.Dataset call() { return bigQueryRpc.create(datasetPb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -189,7 +189,7 @@ public Table create(TableInfo tableInfo, TableOption... options) { public com.google.api.services.bigquery.model.Table call() { return bigQueryRpc.create(tablePb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -207,7 +207,7 @@ public Job create(JobInfo jobInfo, JobOption... options) { public com.google.api.services.bigquery.model.Job call() { return bigQueryRpc.create(jobPb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -230,7 +230,7 @@ public com.google.api.services.bigquery.model.Dataset call() { return bigQueryRpc.getDataset( completeDatasetId.getProject(), completeDatasetId.getDataset(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Dataset.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); @@ -258,7 +258,7 @@ private static Page listDatasets(final String projectId, Iterable> call() { return serviceOptions.getRpc().listDatasets(projectId, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); return new PageImpl<>(new DatasetPageFetcher(projectId, serviceOptions, cursor, optionsMap), cursor, Iterables.transform(result.y(), @@ -289,7 +289,7 @@ public Boolean call() { return bigQueryRpc.deleteDataset( completeDatasetId.getProject(), completeDatasetId.getDataset(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -310,7 +310,7 @@ public Boolean call() { return bigQueryRpc.deleteTable(completeTableId.getProject(), completeTableId.getDataset(), completeTableId.getTable()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -328,7 +328,7 @@ public Dataset update(DatasetInfo datasetInfo, DatasetOption... options) { public com.google.api.services.bigquery.model.Dataset call() { return bigQueryRpc.patch(datasetPb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -346,7 +346,7 @@ public Table update(TableInfo tableInfo, TableOption... options) { public com.google.api.services.bigquery.model.Table call() { return bigQueryRpc.patch(tablePb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -369,7 +369,7 @@ public com.google.api.services.bigquery.model.Table call() { return bigQueryRpc.getTable(completeTableId.getProject(), completeTableId.getDataset(), completeTableId.getTable(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Table.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); @@ -400,7 +400,7 @@ private static Page listTables(final DatasetId datasetId, return serviceOptions.getRpc().listTables( datasetId.getProject(), datasetId.getDataset(), optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable
tables = Iterables.transform(result.y(), new Function() { @@ -458,7 +458,7 @@ public BigQueryRpc.Tuple> call() { .listTableData(completeTableId.getProject(), completeTableId.getDataset(), completeTableId.getTable(), optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); return new PageImpl<>(new TableDataPageFetcher(tableId, serviceOptions, cursor, optionsMap), cursor, transformTableData(result.y())); @@ -495,7 +495,7 @@ public com.google.api.services.bigquery.model.Job call() { return bigQueryRpc.getJob(completeJobId.getProject(), completeJobId.getJob(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Job.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); @@ -517,7 +517,7 @@ private static Page listJobs(final BigQueryOptions serviceOptions, call() { return serviceOptions.getRpc().listJobs(serviceOptions.getProjectId(), optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable jobs = Iterables.transform(result.y(), new Function() { @@ -543,7 +543,7 @@ public boolean cancel(JobId jobId) { public Boolean call() { return bigQueryRpc.cancel(completeJobId.getProject(), completeJobId.getJob()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } @@ -558,7 +558,7 @@ public QueryResponse query(final QueryRequest request) { public com.google.api.services.bigquery.model.QueryResponse call() { return bigQueryRpc.query(request.setProjectId(getOptions().getProjectId()).toPb()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); QueryResponse.Builder builder = QueryResponse.newBuilder(); JobId completeJobId = JobId.fromPb(results.getJobReference()); builder.setJobId(completeJobId); @@ -605,7 +605,7 @@ public GetQueryResultsResponse call() { return serviceOptions.getRpc().getQueryResults( completeJobId.getProject(), completeJobId.getJob(), optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); QueryResponse.Builder builder = QueryResponse.newBuilder(); builder.setJobId(JobId.fromPb(results.getJobReference())); builder.setEtag(results.getEtag()); diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java index 0b4d7ee51f46..5d254c4fabd1 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java @@ -18,7 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.cloud.Clock; +import com.google.api.gax.core.NanoClock; import com.google.cloud.WaitForOption; import com.google.cloud.WaitForOption.CheckingPeriod; import com.google.cloud.WaitForOption.Timeout; @@ -209,10 +209,10 @@ public Job waitFor(WaitForOption... waitOptions) throws InterruptedException, Ti Timeout timeout = Timeout.getOrDefault(waitOptions); CheckingPeriod checkingPeriod = CheckingPeriod.getOrDefault(waitOptions); long timeoutMillis = timeout.getTimeoutMillis(); - Clock clock = options.getClock(); - long startTime = clock.millis(); + NanoClock clock = options.getClock(); + long startTime = clock.millisTime(); while (!isDone()) { - if (timeoutMillis != -1 && (clock.millis() - startTime) >= timeoutMillis) { + if (timeoutMillis != -1 && (clock.millisTime() - startTime) >= timeoutMillis) { throw new TimeoutException(); } checkingPeriod.sleep(); diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableDataWriteChannel.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableDataWriteChannel.java index 34f027f3ced6..a81c5ab090f6 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableDataWriteChannel.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableDataWriteChannel.java @@ -57,7 +57,7 @@ public com.google.api.services.bigquery.model.Job call() { return getOptions().getRpc().write( getUploadId(), getBuffer(), 0, getPosition(), length, last); } - }, getOptions().getRetryParams(), BigQueryImpl.EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), BigQueryImpl.EXCEPTION_HANDLER, getOptions().getClock()); job = jobPb != null ? Job.fromPb(getOptions().getService(), jobPb) : null; } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); @@ -77,7 +77,7 @@ private static String open(final BigQueryOptions options, public String call() { return options.getRpc().open(writeChannelConfiguration.toPb()); } - }, options.getRetryParams(), BigQueryImpl.EXCEPTION_HANDLER, options.getClock()); + }, options.getRetrySettings(), BigQueryImpl.EXCEPTION_HANDLER, options.getClock()); } catch (RetryHelper.RetryHelperException e) { throw BigQueryException.translateAndThrow(e); } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java index 330e21b707e1..3be3db54ad06 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelper.java @@ -17,7 +17,7 @@ package com.google.cloud.bigquery.testing; import com.google.auth.oauth2.ServiceAccountCredentials; -import com.google.cloud.RetryParams; +import com.google.api.gax.core.RetrySettings; import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryException; import com.google.cloud.bigquery.BigQueryOptions; @@ -27,15 +27,16 @@ import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; +import org.joda.time.Duration; /** * Utility to create a remote BigQuery configuration for testing. BigQuery options can be obtained * via the {@link #getOptions()} method. Returned options have custom - * {@link BigQueryOptions#getRetryParams()}: {@link RetryParams#getRetryMaxAttempts()} is - * {@code 10}, {@link RetryParams#getRetryMinAttempts()} is {@code 6}, - * {@link RetryParams#getMaxRetryDelayMillis()} is {@code 30000}, - * {@link RetryParams#getTotalRetryPeriodMillis()} is {@code 120000} and - * {@link RetryParams#getInitialRetryDelayMillis()} is {@code 250}. + * {@link BigQueryOptions#getRetrySettings()}: {@link RetrySettings#getMaxAttempts()} is + * {@code 10}, + * {@link RetrySettings#getMaxRetryDelay()} is {@code 30000}, + * {@link RetrySettings#getTotalTimeout()} is {@code 120000} and + * {@link RetrySettings#getInitialRetryDelay()} is {@code 250}. * {@link BigQueryOptions#getConnectTimeout()} and {@link BigQueryOptions#getReadTimeout()} are both * set to {@code 60000}. */ @@ -91,7 +92,7 @@ public static RemoteBigQueryHelper create(String projectId, InputStream keyStrea BigQueryOptions bigqueryOptions = BigQueryOptions.newBuilder() .setCredentials(ServiceAccountCredentials.fromStream(keyStream)) .setProjectId(projectId) - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setConnectTimeout(60000) .setReadTimeout(60000) .build(); @@ -110,20 +111,22 @@ public static RemoteBigQueryHelper create(String projectId, InputStream keyStrea */ public static RemoteBigQueryHelper create() { BigQueryOptions bigqueryOptions = BigQueryOptions.newBuilder() - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setConnectTimeout(60000) .setReadTimeout(60000) .build(); return new RemoteBigQueryHelper(bigqueryOptions); } - private static RetryParams retryParams() { - return RetryParams.newBuilder() - .setRetryMaxAttempts(10) - .setRetryMinAttempts(6) - .setMaxRetryDelayMillis(30000) - .setTotalRetryPeriodMillis(120000) - .setInitialRetryDelayMillis(250) + private static RetrySettings retryParams() { + return RetrySettings.newBuilder().setMaxAttempts(10) + .setMaxRetryDelay(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(120000L)) + .setInitialRetryDelay(Duration.millis(250L)) + .setRetryDelayMultiplier(1.0) + .setInitialRpcTimeout(Duration.millis(120000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(120000L)) .build(); } diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index 3a56df01c559..afab5fe7b642 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import com.google.api.gax.core.RetrySettings; import com.google.api.services.bigquery.model.ErrorProto; import com.google.api.services.bigquery.model.GetQueryResultsResponse; import com.google.api.services.bigquery.model.TableCell; @@ -33,7 +34,7 @@ import com.google.api.services.bigquery.model.TableDataInsertAllResponse; import com.google.api.services.bigquery.model.TableRow; import com.google.cloud.Page; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.cloud.WriteChannel; import com.google.cloud.bigquery.InsertAllRequest.RowToInsert; import com.google.cloud.bigquery.spi.BigQueryRpc; @@ -66,16 +67,16 @@ public class BigQueryImplTest { private static final String JOB = "job"; private static final String OTHER_TABLE = "otherTable"; private static final String OTHER_DATASET = "otherDataset"; - private static final List ACCESS_RULES = ImmutableList.of( - Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER), - Acl.of(new Acl.View(TableId.of("dataset", "table")), Acl.Role.WRITER)); - private static final List ACCESS_RULES_WITH_PROJECT = ImmutableList.of( - Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER), - Acl.of(new Acl.View(TableId.of(PROJECT, "dataset", "table")))); - private static final DatasetInfo DATASET_INFO = DatasetInfo.newBuilder(DATASET) - .setAcl(ACCESS_RULES) - .setDescription("description") - .build(); + private static final List ACCESS_RULES = + ImmutableList.of( + Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER), + Acl.of(new Acl.View(TableId.of("dataset", "table")), Acl.Role.WRITER)); + private static final List ACCESS_RULES_WITH_PROJECT = + ImmutableList.of( + Acl.of(Acl.Group.ofAllAuthenticatedUsers(), Acl.Role.READER), + Acl.of(new Acl.View(TableId.of(PROJECT, "dataset", "table")))); + private static final DatasetInfo DATASET_INFO = + DatasetInfo.newBuilder(DATASET).setAcl(ACCESS_RULES).setDescription("description").build(); private static final DatasetInfo DATASET_INFO_WITH_PROJECT = DatasetInfo.newBuilder(PROJECT, DATASET) .setAcl(ACCESS_RULES_WITH_PROJECT) @@ -115,15 +116,14 @@ public class BigQueryImplTest { LoadJobConfiguration.of(TABLE_ID, "URI"); private static final LoadJobConfiguration LOAD_JOB_CONFIGURATION_WITH_PROJECT = LoadJobConfiguration.of(TABLE_ID_WITH_PROJECT, "URI"); - private static final JobInfo LOAD_JOB = - JobInfo.of(LOAD_JOB_CONFIGURATION); + private static final JobInfo LOAD_JOB = JobInfo.of(LOAD_JOB_CONFIGURATION); private static final JobInfo COMPLETE_LOAD_JOB = JobInfo.of(JobId.of(PROJECT, JOB), LOAD_JOB_CONFIGURATION_WITH_PROJECT); private static final CopyJobConfiguration COPY_JOB_CONFIGURATION = CopyJobConfiguration.of(TABLE_ID, ImmutableList.of(TABLE_ID, TABLE_ID)); private static final CopyJobConfiguration COPY_JOB_CONFIGURATION_WITH_PROJECT = - CopyJobConfiguration.of(TABLE_ID_WITH_PROJECT, ImmutableList.of(TABLE_ID_WITH_PROJECT, - TABLE_ID_WITH_PROJECT)); + CopyJobConfiguration.of( + TABLE_ID_WITH_PROJECT, ImmutableList.of(TABLE_ID_WITH_PROJECT, TABLE_ID_WITH_PROJECT)); private static final JobInfo COPY_JOB = JobInfo.of(COPY_JOB_CONFIGURATION); private static final JobInfo COMPLETE_COPY_JOB = JobInfo.of(JobId.of(PROJECT, JOB), COPY_JOB_CONFIGURATION_WITH_PROJECT); @@ -151,26 +151,31 @@ public class BigQueryImplTest { private static final TableCell INTEGER_FIELD = new TableCell().setV("1"); private static final TableRow TABLE_ROW = new TableRow().setF(ImmutableList.of(BOOLEAN_FIELD, INTEGER_FIELD)); - private static final QueryRequest QUERY_REQUEST = QueryRequest.newBuilder("SQL") - .setPageSize(42L) - .setUseQueryCache(false) - .setDefaultDataset(DatasetId.of(DATASET)) - .build(); - private static final QueryRequest QUERY_REQUEST_WITH_PROJECT = QueryRequest.newBuilder("SQL") - .setPageSize(42L) - .setUseQueryCache(false) - .setDefaultDataset(DatasetId.of(PROJECT, DATASET)) - .build(); + private static final QueryRequest QUERY_REQUEST = + QueryRequest.newBuilder("SQL") + .setPageSize(42L) + .setUseQueryCache(false) + .setDefaultDataset(DatasetId.of(DATASET)) + .build(); + private static final QueryRequest QUERY_REQUEST_WITH_PROJECT = + QueryRequest.newBuilder("SQL") + .setPageSize(42L) + .setUseQueryCache(false) + .setDefaultDataset(DatasetId.of(PROJECT, DATASET)) + .build(); private static final String CURSOR = "cursor"; private static final TableCell CELL_PB1 = new TableCell().setV("Value1"); private static final TableCell CELL_PB2 = new TableCell().setV("Value2"); - private static final ImmutableList> TABLE_DATA = ImmutableList.of( - (List) ImmutableList.of(FieldValue.fromPb(CELL_PB1)), - ImmutableList.of(FieldValue.fromPb(CELL_PB2))); + private static final ImmutableList> TABLE_DATA = + ImmutableList.of( + (List) ImmutableList.of(FieldValue.fromPb(CELL_PB1)), + ImmutableList.of(FieldValue.fromPb(CELL_PB2))); private static final Tuple> TABLE_DATA_PB = - Tuple.>of(CURSOR, ImmutableList.of( - new TableRow().setF(ImmutableList.of(new TableCell().setV("Value1"))), - new TableRow().setF(ImmutableList.of(new TableCell().setV("Value2"))))); + Tuple.>of( + CURSOR, + ImmutableList.of( + new TableRow().setF(ImmutableList.of(new TableCell().setV("Value1"))), + new TableRow().setF(ImmutableList.of(new TableCell().setV("Value2"))))); // Empty BigQueryRpc options private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); @@ -186,16 +191,20 @@ public class BigQueryImplTest { BigQuery.DatasetListOption.pageToken(CURSOR); private static final BigQuery.DatasetListOption DATASET_LIST_PAGE_SIZE = BigQuery.DatasetListOption.pageSize(42L); - private static final Map DATASET_LIST_OPTIONS = ImmutableMap.of( - BigQueryRpc.Option.ALL_DATASETS, true, - BigQueryRpc.Option.PAGE_TOKEN, CURSOR, - BigQueryRpc.Option.MAX_RESULTS, 42L); + private static final Map DATASET_LIST_OPTIONS = + ImmutableMap.of( + BigQueryRpc.Option.ALL_DATASETS, + true, + BigQueryRpc.Option.PAGE_TOKEN, + CURSOR, + BigQueryRpc.Option.MAX_RESULTS, + 42L); // Dataset delete options private static final BigQuery.DatasetDeleteOption DATASET_DELETE_CONTENTS = BigQuery.DatasetDeleteOption.deleteContents(); - private static final Map DATASET_DELETE_OPTIONS = ImmutableMap.of( - BigQueryRpc.Option.DELETE_CONTENTS, true); + private static final Map DATASET_DELETE_OPTIONS = + ImmutableMap.of(BigQueryRpc.Option.DELETE_CONTENTS, true); // Table options private static final BigQuery.TableOption TABLE_OPTION_FIELDS = @@ -206,9 +215,8 @@ public class BigQueryImplTest { BigQuery.TableListOption.pageSize(42L); private static final BigQuery.TableListOption TABLE_LIST_PAGE_TOKEN = BigQuery.TableListOption.pageToken(CURSOR); - private static final Map TABLE_LIST_OPTIONS = ImmutableMap.of( - BigQueryRpc.Option.MAX_RESULTS, 42L, - BigQueryRpc.Option.PAGE_TOKEN, CURSOR); + private static final Map TABLE_LIST_OPTIONS = + ImmutableMap.of(BigQueryRpc.Option.MAX_RESULTS, 42L, BigQueryRpc.Option.PAGE_TOKEN, CURSOR); // TableData list options private static final BigQuery.TableDataListOption TABLE_DATA_LIST_PAGE_SIZE = @@ -217,10 +225,11 @@ public class BigQueryImplTest { BigQuery.TableDataListOption.pageToken(CURSOR); private static final BigQuery.TableDataListOption TABLE_DATA_LIST_START_INDEX = BigQuery.TableDataListOption.startIndex(0L); - private static final Map TABLE_DATA_LIST_OPTIONS = ImmutableMap.of( - BigQueryRpc.Option.MAX_RESULTS, 42L, - BigQueryRpc.Option.PAGE_TOKEN, CURSOR, - BigQueryRpc.Option.START_INDEX, 0L); + private static final Map TABLE_DATA_LIST_OPTIONS = + ImmutableMap.of( + BigQueryRpc.Option.MAX_RESULTS, 42L, + BigQueryRpc.Option.PAGE_TOKEN, CURSOR, + BigQueryRpc.Option.START_INDEX, 0L); // Job options private static final BigQuery.JobOption JOB_OPTION_FIELDS = @@ -237,11 +246,16 @@ public class BigQueryImplTest { BigQuery.JobListOption.pageToken(CURSOR); private static final BigQuery.JobListOption JOB_LIST_PAGE_SIZE = BigQuery.JobListOption.pageSize(42L); - private static final Map JOB_LIST_OPTIONS = ImmutableMap.of( - BigQueryRpc.Option.ALL_USERS, true, - BigQueryRpc.Option.STATE_FILTER, ImmutableList.of("done", "pending"), - BigQueryRpc.Option.PAGE_TOKEN, CURSOR, - BigQueryRpc.Option.MAX_RESULTS, 42L); + private static final Map JOB_LIST_OPTIONS = + ImmutableMap.of( + BigQueryRpc.Option.ALL_USERS, + true, + BigQueryRpc.Option.STATE_FILTER, + ImmutableList.of("done", "pending"), + BigQueryRpc.Option.PAGE_TOKEN, + CURSOR, + BigQueryRpc.Option.MAX_RESULTS, + 42L); // Query Results options private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_TIME = @@ -252,19 +266,19 @@ public class BigQueryImplTest { BigQuery.QueryResultsOption.pageToken(CURSOR); private static final BigQuery.QueryResultsOption QUERY_RESULTS_OPTION_PAGE_SIZE = BigQuery.QueryResultsOption.pageSize(0L); - private static final Map QUERY_RESULTS_OPTIONS = ImmutableMap.of( - BigQueryRpc.Option.TIMEOUT, 42L, - BigQueryRpc.Option.START_INDEX, 1024L, - BigQueryRpc.Option.PAGE_TOKEN, CURSOR, - BigQueryRpc.Option.MAX_RESULTS, 0L); + private static final Map QUERY_RESULTS_OPTIONS = + ImmutableMap.of( + BigQueryRpc.Option.TIMEOUT, 42L, + BigQueryRpc.Option.START_INDEX, 1024L, + BigQueryRpc.Option.PAGE_TOKEN, CURSOR, + BigQueryRpc.Option.MAX_RESULTS, 0L); private BigQueryOptions options; private BigQueryRpcFactory rpcFactoryMock; private BigQueryRpc bigqueryRpcMock; private BigQuery bigquery; - @Rule - public ExpectedException thrown = ExpectedException.none(); + @Rule public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() { @@ -273,11 +287,12 @@ public void setUp() { EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(BigQueryOptions.class))) .andReturn(bigqueryRpcMock); EasyMock.replay(rpcFactoryMock); - options = BigQueryOptions.newBuilder() - .setProjectId(PROJECT) - .setServiceRpcFactory(rpcFactoryMock) - .setRetryParams(RetryParams.noRetries()) - .build(); + options = + BigQueryOptions.newBuilder() + .setProjectId(PROJECT) + .setServiceRpcFactory(rpcFactoryMock) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) + .build(); } @After @@ -306,8 +321,8 @@ public void testCreateDataset() { @Test public void testCreateDatasetWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(bigqueryRpcMock.create(eq(DATASET_INFO_WITH_PROJECT.toPb()), - capture(capturedOptions))) + EasyMock.expect( + bigqueryRpcMock.create(eq(DATASET_INFO_WITH_PROJECT.toPb()), capture(capturedOptions))) .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); @@ -317,8 +332,8 @@ public void testCreateDatasetWithSelectedFields() { assertTrue(selector.contains("access")); assertTrue(selector.contains("etag")); assertEquals(28, selector.length()); - assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), - dataset); + assertEquals( + new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); } @Test @@ -328,8 +343,8 @@ public void testGetDataset() { EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Dataset dataset = bigquery.getDataset(DATASET); - assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), - dataset); + assertEquals( + new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); } @Test @@ -339,8 +354,8 @@ public void testGetDatasetFromDatasetId() { EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Dataset dataset = bigquery.getDataset(DatasetId.of(DATASET)); - assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), - dataset); + assertEquals( + new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); } @Test @@ -368,31 +383,34 @@ public void testGetDatasetWithSelectedFields() { assertTrue(selector.contains("access")); assertTrue(selector.contains("etag")); assertEquals(28, selector.length()); - assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), - dataset); + assertEquals( + new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); } @Test public void testListDatasets() { bigquery = options.getService(); - ImmutableList datasetList = ImmutableList.of( - new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), - new Dataset(bigquery, new DatasetInfo.BuilderImpl(OTHER_DATASET_INFO))); + ImmutableList datasetList = + ImmutableList.of( + new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), + new Dataset(bigquery, new DatasetInfo.BuilderImpl(OTHER_DATASET_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listDatasets(PROJECT, EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(bigqueryRpcMock); Page page = bigquery.listDatasets(); assertEquals(CURSOR, page.getNextPageCursor()); - assertArrayEquals(datasetList.toArray(), - Iterables.toArray(page.getValues(), DatasetInfo.class)); + assertArrayEquals( + datasetList.toArray(), Iterables.toArray(page.getValues(), DatasetInfo.class)); } @Test public void testListDatasetsWithProjects() { bigquery = options.getService(); - ImmutableList datasetList = ImmutableList.of(new Dataset(bigquery, - new DatasetInfo.BuilderImpl(DATASET_INFO.setProjectId(OTHER_PROJECT)))); + ImmutableList datasetList = + ImmutableList.of( + new Dataset( + bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO.setProjectId(OTHER_PROJECT)))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listDatasets(OTHER_PROJECT, EMPTY_RPC_OPTIONS)) @@ -400,8 +418,8 @@ public void testListDatasetsWithProjects() { EasyMock.replay(bigqueryRpcMock); Page page = bigquery.listDatasets(OTHER_PROJECT); assertEquals(CURSOR, page.getNextPageCursor()); - assertArrayEquals(datasetList.toArray(), - Iterables.toArray(page.getValues(), DatasetInfo.class)); + assertArrayEquals( + datasetList.toArray(), Iterables.toArray(page.getValues(), DatasetInfo.class)); } @Test @@ -414,25 +432,26 @@ public void testListEmptyDatasets() { bigquery = options.getService(); Page page = bigquery.listDatasets(); assertNull(page.getNextPageCursor()); - assertArrayEquals(ImmutableList.of().toArray(), - Iterables.toArray(page.getValues(), Dataset.class)); + assertArrayEquals( + ImmutableList.of().toArray(), Iterables.toArray(page.getValues(), Dataset.class)); } @Test public void testListDatasetsWithOptions() { bigquery = options.getService(); - ImmutableList datasetList = ImmutableList.of( - new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), - new Dataset(bigquery, new DatasetInfo.BuilderImpl(OTHER_DATASET_INFO))); + ImmutableList datasetList = + ImmutableList.of( + new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), + new Dataset(bigquery, new DatasetInfo.BuilderImpl(OTHER_DATASET_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(datasetList, DatasetInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listDatasets(PROJECT, DATASET_LIST_OPTIONS)).andReturn(result); EasyMock.replay(bigqueryRpcMock); - Page page = bigquery.listDatasets(DATASET_LIST_ALL, DATASET_LIST_PAGE_TOKEN, - DATASET_LIST_PAGE_SIZE); + Page page = + bigquery.listDatasets(DATASET_LIST_ALL, DATASET_LIST_PAGE_TOKEN, DATASET_LIST_PAGE_SIZE); assertEquals(CURSOR, page.getNextPageCursor()); - assertArrayEquals(datasetList.toArray(), - Iterables.toArray(page.getValues(), DatasetInfo.class)); + assertArrayEquals( + datasetList.toArray(), Iterables.toArray(page.getValues(), DatasetInfo.class)); } @Test @@ -474,9 +493,12 @@ public void testDeleteDatasetWithOptions() { @Test public void testUpdateDataset() { - DatasetInfo updatedDatasetInfo = DATASET_INFO.setProjectId(OTHER_PROJECT).toBuilder() - .setDescription("newDescription") - .build(); + DatasetInfo updatedDatasetInfo = + DATASET_INFO + .setProjectId(OTHER_PROJECT) + .toBuilder() + .setDescription("newDescription") + .build(); EasyMock.expect(bigqueryRpcMock.patch(updatedDatasetInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(updatedDatasetInfo.toPb()); EasyMock.replay(bigqueryRpcMock); @@ -490,11 +512,11 @@ public void testUpdateDatasetWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); DatasetInfo updatedDatasetInfo = DATASET_INFO.toBuilder().setDescription("newDescription").build(); - DatasetInfo updatedDatasetInfoWithProject = DATASET_INFO_WITH_PROJECT.toBuilder() - .setDescription("newDescription") - .build(); + DatasetInfo updatedDatasetInfoWithProject = + DATASET_INFO_WITH_PROJECT.toBuilder().setDescription("newDescription").build(); EasyMock.expect( - bigqueryRpcMock.patch(eq(updatedDatasetInfoWithProject.toPb()), capture(capturedOptions))) + bigqueryRpcMock.patch( + eq(updatedDatasetInfoWithProject.toPb()), capture(capturedOptions))) .andReturn(updatedDatasetInfoWithProject.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); @@ -504,8 +526,8 @@ public void testUpdateDatasetWithSelectedFields() { assertTrue(selector.contains("access")); assertTrue(selector.contains("etag")); assertEquals(28, selector.length()); - assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(updatedDatasetInfoWithProject)), - dataset); + assertEquals( + new Dataset(bigquery, new DatasetInfo.BuilderImpl(updatedDatasetInfoWithProject)), dataset); } @Test @@ -523,7 +545,7 @@ public void testCreateTable() { public void testCreateTableWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); EasyMock.expect( - bigqueryRpcMock.create(eq(TABLE_INFO_WITH_PROJECT.toPb()), capture(capturedOptions))) + bigqueryRpcMock.create(eq(TABLE_INFO_WITH_PROJECT.toPb()), capture(capturedOptions))) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); @@ -572,7 +594,7 @@ public void testGetTableFromTableIdWithProject() { public void testGetTableWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); EasyMock.expect( - bigqueryRpcMock.getTable(eq(PROJECT), eq(DATASET), eq(TABLE), capture(capturedOptions))) + bigqueryRpcMock.getTable(eq(PROJECT), eq(DATASET), eq(TABLE), capture(capturedOptions))) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); @@ -588,9 +610,10 @@ public void testGetTableWithSelectedFields() { @Test public void testListTables() { bigquery = options.getService(); - ImmutableList
tableList = ImmutableList.of( - new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), - new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); + ImmutableList
tableList = + ImmutableList.of( + new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), + new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listTables(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) @@ -604,9 +627,10 @@ public void testListTables() { @Test public void testListTablesFromDatasetId() { bigquery = options.getService(); - ImmutableList
tableList = ImmutableList.of( - new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), - new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); + ImmutableList
tableList = + ImmutableList.of( + new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), + new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listTables(PROJECT, DATASET, EMPTY_RPC_OPTIONS)) @@ -620,8 +644,9 @@ public void testListTablesFromDatasetId() { @Test public void testListTablesFromDatasetIdWithProject() { bigquery = options.getService(); - ImmutableList
tableList = ImmutableList.of( - new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO.setProjectId(OTHER_PROJECT)))); + ImmutableList
tableList = + ImmutableList.of( + new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO.setProjectId(OTHER_PROJECT)))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listTables(OTHER_PROJECT, DATASET, EMPTY_RPC_OPTIONS)) @@ -635,9 +660,10 @@ public void testListTablesFromDatasetIdWithProject() { @Test public void testListTablesWithOptions() { bigquery = options.getService(); - ImmutableList
tableList = ImmutableList.of( - new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), - new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); + ImmutableList
tableList = + ImmutableList.of( + new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), + new Table(bigquery, new TableInfo.BuilderImpl(OTHER_TABLE_INFO))); Tuple> result = Tuple.of(CURSOR, Iterables.transform(tableList, TableInfo.TO_PB_FUNCTION)); EasyMock.expect(bigqueryRpcMock.listTables(PROJECT, DATASET, TABLE_LIST_OPTIONS)) @@ -675,9 +701,8 @@ public void testDeleteTableFromTableIdWithProject() { @Test public void testUpdateTable() { - TableInfo updatedTableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT).toBuilder() - .setDescription("newDescription") - .build(); + TableInfo updatedTableInfo = + TABLE_INFO.setProjectId(OTHER_PROJECT).toBuilder().setDescription("newDescription").build(); EasyMock.expect(bigqueryRpcMock.patch(updatedTableInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(updatedTableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); @@ -690,11 +715,11 @@ public void testUpdateTable() { public void testUpdateTableWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); TableInfo updatedTableInfo = TABLE_INFO.toBuilder().setDescription("newDescription").build(); - TableInfo updatedTableInfoWithProject = TABLE_INFO_WITH_PROJECT.toBuilder() - .setDescription("newDescription") - .build(); - EasyMock.expect(bigqueryRpcMock.patch(eq(updatedTableInfoWithProject.toPb()), - capture(capturedOptions))).andReturn(updatedTableInfoWithProject.toPb()); + TableInfo updatedTableInfoWithProject = + TABLE_INFO_WITH_PROJECT.toBuilder().setDescription("newDescription").build(); + EasyMock.expect( + bigqueryRpcMock.patch(eq(updatedTableInfoWithProject.toPb()), capture(capturedOptions))) + .andReturn(updatedTableInfoWithProject.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Table table = bigquery.update(updatedTableInfo, TABLE_OPTION_FIELDS); @@ -703,35 +728,46 @@ public void testUpdateTableWithSelectedFields() { assertTrue(selector.contains("schema")); assertTrue(selector.contains("etag")); assertEquals(31, selector.length()); - assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfoWithProject)), - table); + assertEquals( + new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfoWithProject)), table); } @Test public void testInsertAll() { Map row1 = ImmutableMap.of("field", "value1"); Map row2 = ImmutableMap.of("field", "value2"); - List rows = ImmutableList.of( - new RowToInsert("row1", row1), - new RowToInsert("row2", row2) - ); - InsertAllRequest request = InsertAllRequest.newBuilder(TABLE_ID) - .setRows(rows) - .setSkipInvalidRows(false) - .setIgnoreUnknownValues(true) - .setTemplateSuffix("suffix") - .build(); - TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest().setRows( - Lists.transform(rows, new Function() { - @Override - public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { - return new TableDataInsertAllRequest.Rows().setInsertId(rowToInsert.getId()) - .setJson(rowToInsert.getContent()); - } - })).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix"); - TableDataInsertAllResponse responsePb = new TableDataInsertAllResponse().setInsertErrors( - ImmutableList.of(new TableDataInsertAllResponse.InsertErrors().setIndex(0L).setErrors( - ImmutableList.of(new ErrorProto().setMessage("ErrorMessage"))))); + List rows = + ImmutableList.of(new RowToInsert("row1", row1), new RowToInsert("row2", row2)); + InsertAllRequest request = + InsertAllRequest.newBuilder(TABLE_ID) + .setRows(rows) + .setSkipInvalidRows(false) + .setIgnoreUnknownValues(true) + .setTemplateSuffix("suffix") + .build(); + TableDataInsertAllRequest requestPb = + new TableDataInsertAllRequest() + .setRows( + Lists.transform( + rows, + new Function() { + @Override + public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { + return new TableDataInsertAllRequest.Rows() + .setInsertId(rowToInsert.getId()) + .setJson(rowToInsert.getContent()); + } + })) + .setSkipInvalidRows(false) + .setIgnoreUnknownValues(true) + .setTemplateSuffix("suffix"); + TableDataInsertAllResponse responsePb = + new TableDataInsertAllResponse() + .setInsertErrors( + ImmutableList.of( + new TableDataInsertAllResponse.InsertErrors() + .setIndex(0L) + .setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage"))))); EasyMock.expect(bigqueryRpcMock.insertAll(PROJECT, DATASET, TABLE, requestPb)) .andReturn(responsePb); EasyMock.replay(bigqueryRpcMock); @@ -747,28 +783,39 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { public void testInsertAllWithProject() { Map row1 = ImmutableMap.of("field", "value1"); Map row2 = ImmutableMap.of("field", "value2"); - List rows = ImmutableList.of( - new RowToInsert("row1", row1), - new RowToInsert("row2", row2) - ); + List rows = + ImmutableList.of(new RowToInsert("row1", row1), new RowToInsert("row2", row2)); TableId tableId = TableId.of(OTHER_PROJECT, DATASET, TABLE); - InsertAllRequest request = InsertAllRequest.newBuilder(tableId) - .setRows(rows) - .setSkipInvalidRows(false) - .setIgnoreUnknownValues(true) - .setTemplateSuffix("suffix") - .build(); - TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest().setRows( - Lists.transform(rows, new Function() { - @Override - public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { - return new TableDataInsertAllRequest.Rows().setInsertId(rowToInsert.getId()) - .setJson(rowToInsert.getContent()); - } - })).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix"); - TableDataInsertAllResponse responsePb = new TableDataInsertAllResponse().setInsertErrors( - ImmutableList.of(new TableDataInsertAllResponse.InsertErrors().setIndex(0L).setErrors( - ImmutableList.of(new ErrorProto().setMessage("ErrorMessage"))))); + InsertAllRequest request = + InsertAllRequest.newBuilder(tableId) + .setRows(rows) + .setSkipInvalidRows(false) + .setIgnoreUnknownValues(true) + .setTemplateSuffix("suffix") + .build(); + TableDataInsertAllRequest requestPb = + new TableDataInsertAllRequest() + .setRows( + Lists.transform( + rows, + new Function() { + @Override + public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { + return new TableDataInsertAllRequest.Rows() + .setInsertId(rowToInsert.getId()) + .setJson(rowToInsert.getContent()); + } + })) + .setSkipInvalidRows(false) + .setIgnoreUnknownValues(true) + .setTemplateSuffix("suffix"); + TableDataInsertAllResponse responsePb = + new TableDataInsertAllResponse() + .setInsertErrors( + ImmutableList.of( + new TableDataInsertAllResponse.InsertErrors() + .setIndex(0L) + .setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage"))))); EasyMock.expect(bigqueryRpcMock.insertAll(OTHER_PROJECT, DATASET, TABLE, requestPb)) .andReturn(responsePb); EasyMock.replay(bigqueryRpcMock); @@ -805,8 +852,7 @@ public void testListTableDataFromTableId() { @Test public void testListTableDataFromTableIdWithProject() { TableId tableId = TABLE_ID.setProjectId(OTHER_PROJECT); - EasyMock.expect( - bigqueryRpcMock.listTableData(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) + EasyMock.expect(bigqueryRpcMock.listTableData(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(TABLE_DATA_PB); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); @@ -821,17 +867,23 @@ public void testListTableDataWithOptions() { .andReturn(TABLE_DATA_PB); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); - Page> page = bigquery.listTableData(DATASET, TABLE, - TABLE_DATA_LIST_PAGE_SIZE, TABLE_DATA_LIST_PAGE_TOKEN, TABLE_DATA_LIST_START_INDEX); + Page> page = + bigquery.listTableData( + DATASET, + TABLE, + TABLE_DATA_LIST_PAGE_SIZE, + TABLE_DATA_LIST_PAGE_TOKEN, + TABLE_DATA_LIST_START_INDEX); assertEquals(CURSOR, page.getNextPageCursor()); assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.getValues(), List.class)); } @Test public void testCreateQueryJob() { - EasyMock.expect(bigqueryRpcMock.create( - JobInfo.of(QUERY_JOB_CONFIGURATION_WITH_PROJECT).toPb(), EMPTY_RPC_OPTIONS)) - .andReturn(COMPLETE_QUERY_JOB.toPb()); + EasyMock.expect( + bigqueryRpcMock.create( + JobInfo.of(QUERY_JOB_CONFIGURATION_WITH_PROJECT).toPb(), EMPTY_RPC_OPTIONS)) + .andReturn(COMPLETE_QUERY_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Job job = bigquery.create(QUERY_JOB); @@ -840,9 +892,10 @@ public void testCreateQueryJob() { @Test public void testCreateLoadJob() { - EasyMock.expect(bigqueryRpcMock.create( - JobInfo.of(LOAD_JOB_CONFIGURATION_WITH_PROJECT).toPb(), EMPTY_RPC_OPTIONS)) - .andReturn(COMPLETE_LOAD_JOB.toPb()); + EasyMock.expect( + bigqueryRpcMock.create( + JobInfo.of(LOAD_JOB_CONFIGURATION_WITH_PROJECT).toPb(), EMPTY_RPC_OPTIONS)) + .andReturn(COMPLETE_LOAD_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Job job = bigquery.create(LOAD_JOB); @@ -851,9 +904,10 @@ public void testCreateLoadJob() { @Test public void testCreateCopyJob() { - EasyMock.expect(bigqueryRpcMock.create( - JobInfo.of(COPY_JOB_CONFIGURATION_WITH_PROJECT).toPb(), EMPTY_RPC_OPTIONS)) - .andReturn(COMPLETE_COPY_JOB.toPb()); + EasyMock.expect( + bigqueryRpcMock.create( + JobInfo.of(COPY_JOB_CONFIGURATION_WITH_PROJECT).toPb(), EMPTY_RPC_OPTIONS)) + .andReturn(COMPLETE_COPY_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Job job = bigquery.create(COPY_JOB); @@ -862,9 +916,10 @@ public void testCreateCopyJob() { @Test public void testCreateExtractJob() { - EasyMock.expect(bigqueryRpcMock.create( - JobInfo.of(EXTRACT_JOB_CONFIGURATION_WITH_PROJECT).toPb(), EMPTY_RPC_OPTIONS)) - .andReturn(COMPLETE_EXTRACT_JOB.toPb()); + EasyMock.expect( + bigqueryRpcMock.create( + JobInfo.of(EXTRACT_JOB_CONFIGURATION_WITH_PROJECT).toPb(), EMPTY_RPC_OPTIONS)) + .andReturn(COMPLETE_EXTRACT_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Job job = bigquery.create(EXTRACT_JOB); @@ -874,9 +929,11 @@ public void testCreateExtractJob() { @Test public void testCreateJobWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(bigqueryRpcMock.create( - eq(JobInfo.of(QUERY_JOB_CONFIGURATION_WITH_PROJECT).toPb()), capture(capturedOptions))) - .andReturn(COMPLETE_QUERY_JOB.toPb()); + EasyMock.expect( + bigqueryRpcMock.create( + eq(JobInfo.of(QUERY_JOB_CONFIGURATION_WITH_PROJECT).toPb()), + capture(capturedOptions))) + .andReturn(COMPLETE_QUERY_JOB.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Job job = bigquery.create(QUERY_JOB, JOB_OPTION_FIELDS); @@ -890,9 +947,10 @@ public void testCreateJobWithSelectedFields() { @Test public void testCreateJobWithProjectId() { - JobInfo jobInfo = JobInfo.newBuilder(QUERY_JOB_CONFIGURATION.setProjectId(OTHER_PROJECT)) - .setJobId(JobId.of(OTHER_PROJECT, JOB)) - .build(); + JobInfo jobInfo = + JobInfo.newBuilder(QUERY_JOB_CONFIGURATION.setProjectId(OTHER_PROJECT)) + .setJobId(JobId.of(OTHER_PROJECT, JOB)) + .build(); Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(bigqueryRpcMock.create(eq(jobInfo.toPb()), capture(capturedOptions))) .andReturn(jobInfo.toPb()); @@ -942,17 +1000,21 @@ public void testGetJobFromJobIdWithProject() { @Test public void testListJobs() { bigquery = options.getService(); - ImmutableList jobList = ImmutableList.of( - new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_QUERY_JOB)), - new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_LOAD_JOB))); + ImmutableList jobList = + ImmutableList.of( + new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_QUERY_JOB)), + new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_LOAD_JOB))); Tuple> result = - Tuple.of(CURSOR, Iterables.transform(jobList, - new Function() { - @Override - public com.google.api.services.bigquery.model.Job apply(Job job) { - return job.toPb(); - } - })); + Tuple.of( + CURSOR, + Iterables.transform( + jobList, + new Function() { + @Override + public com.google.api.services.bigquery.model.Job apply(Job job) { + return job.toPb(); + } + })); EasyMock.expect(bigqueryRpcMock.listJobs(PROJECT, EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(bigqueryRpcMock); Page page = bigquery.listJobs(); @@ -963,21 +1025,26 @@ public com.google.api.services.bigquery.model.Job apply(Job job) { @Test public void testListJobsWithOptions() { bigquery = options.getService(); - ImmutableList jobList = ImmutableList.of( - new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_QUERY_JOB)), - new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_LOAD_JOB))); + ImmutableList jobList = + ImmutableList.of( + new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_QUERY_JOB)), + new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_LOAD_JOB))); Tuple> result = - Tuple.of(CURSOR, Iterables.transform(jobList, - new Function() { - @Override - public com.google.api.services.bigquery.model.Job apply(Job job) { - return job.toPb(); - } - })); + Tuple.of( + CURSOR, + Iterables.transform( + jobList, + new Function() { + @Override + public com.google.api.services.bigquery.model.Job apply(Job job) { + return job.toPb(); + } + })); EasyMock.expect(bigqueryRpcMock.listJobs(PROJECT, JOB_LIST_OPTIONS)).andReturn(result); EasyMock.replay(bigqueryRpcMock); - Page page = bigquery.listJobs(JOB_LIST_ALL_USERS, JOB_LIST_STATE_FILTER, - JOB_LIST_PAGE_TOKEN, JOB_LIST_PAGE_SIZE); + Page page = + bigquery.listJobs( + JOB_LIST_ALL_USERS, JOB_LIST_STATE_FILTER, JOB_LIST_PAGE_TOKEN, JOB_LIST_PAGE_SIZE); assertEquals(CURSOR, page.getNextPageCursor()); assertArrayEquals(jobList.toArray(), Iterables.toArray(page.getValues(), Job.class)); } @@ -986,17 +1053,21 @@ public com.google.api.services.bigquery.model.Job apply(Job job) { public void testListJobsWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); bigquery = options.getService(); - ImmutableList jobList = ImmutableList.of( - new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_QUERY_JOB)), - new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_LOAD_JOB))); + ImmutableList jobList = + ImmutableList.of( + new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_QUERY_JOB)), + new Job(bigquery, new JobInfo.BuilderImpl(COMPLETE_LOAD_JOB))); Tuple> result = - Tuple.of(CURSOR, Iterables.transform(jobList, - new Function() { - @Override - public com.google.api.services.bigquery.model.Job apply(Job job) { - return job.toPb(); - } - })); + Tuple.of( + CURSOR, + Iterables.transform( + jobList, + new Function() { + @Override + public com.google.api.services.bigquery.model.Job apply(Job job) { + return job.toPb(); + } + })); EasyMock.expect(bigqueryRpcMock.listJobs(eq(PROJECT), capture(capturedOptions))) .andReturn(result); EasyMock.replay(bigqueryRpcMock); @@ -1094,15 +1165,16 @@ public void testQueryRequestCompleted() { @Test public void testGetQueryResults() { JobId queryJob = JobId.of(JOB); - GetQueryResultsResponse responsePb = new GetQueryResultsResponse() - .setEtag("etag") - .setJobReference(queryJob.toPb()) - .setRows(ImmutableList.of(TABLE_ROW)) - .setJobComplete(true) - .setCacheHit(false) - .setPageToken(CURSOR) - .setTotalBytesProcessed(42L) - .setTotalRows(BigInteger.valueOf(1L)); + GetQueryResultsResponse responsePb = + new GetQueryResultsResponse() + .setEtag("etag") + .setJobReference(queryJob.toPb()) + .setRows(ImmutableList.of(TABLE_ROW)) + .setJobComplete(true) + .setCacheHit(false) + .setPageToken(CURSOR) + .setTotalBytesProcessed(42L) + .setTotalRows(BigInteger.valueOf(1L)); EasyMock.expect(bigqueryRpcMock.getQueryResults(PROJECT, JOB, EMPTY_RPC_OPTIONS)) .andReturn(responsePb); EasyMock.replay(bigqueryRpcMock); @@ -1127,15 +1199,16 @@ public void testGetQueryResults() { @Test public void testGetQueryResultsWithProject() { JobId queryJob = JobId.of(OTHER_PROJECT, JOB); - GetQueryResultsResponse responsePb = new GetQueryResultsResponse() - .setEtag("etag") - .setJobReference(queryJob.toPb()) - .setRows(ImmutableList.of(TABLE_ROW)) - .setJobComplete(true) - .setCacheHit(false) - .setPageToken(CURSOR) - .setTotalBytesProcessed(42L) - .setTotalRows(BigInteger.valueOf(1L)); + GetQueryResultsResponse responsePb = + new GetQueryResultsResponse() + .setEtag("etag") + .setJobReference(queryJob.toPb()) + .setRows(ImmutableList.of(TABLE_ROW)) + .setJobComplete(true) + .setCacheHit(false) + .setPageToken(CURSOR) + .setTotalBytesProcessed(42L) + .setTotalRows(BigInteger.valueOf(1L)); EasyMock.expect(bigqueryRpcMock.getQueryResults(OTHER_PROJECT, JOB, EMPTY_RPC_OPTIONS)) .andReturn(responsePb); EasyMock.replay(bigqueryRpcMock); @@ -1160,21 +1233,26 @@ public void testGetQueryResultsWithProject() { @Test public void testGetQueryResultsWithOptions() { JobId queryJob = JobId.of(PROJECT, JOB); - GetQueryResultsResponse responsePb = new GetQueryResultsResponse() - .setJobReference(queryJob.toPb()) - .setRows(ImmutableList.of(TABLE_ROW)) - .setJobComplete(true) - .setCacheHit(false) - .setPageToken(CURSOR) - .setTotalBytesProcessed(42L) - .setTotalRows(BigInteger.valueOf(1L)); + GetQueryResultsResponse responsePb = + new GetQueryResultsResponse() + .setJobReference(queryJob.toPb()) + .setRows(ImmutableList.of(TABLE_ROW)) + .setJobComplete(true) + .setCacheHit(false) + .setPageToken(CURSOR) + .setTotalBytesProcessed(42L) + .setTotalRows(BigInteger.valueOf(1L)); EasyMock.expect(bigqueryRpcMock.getQueryResults(PROJECT, JOB, QUERY_RESULTS_OPTIONS)) .andReturn(responsePb); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); - QueryResponse response = bigquery.getQueryResults(queryJob, QUERY_RESULTS_OPTION_TIME, - QUERY_RESULTS_OPTION_INDEX, QUERY_RESULTS_OPTION_PAGE_SIZE, - QUERY_RESULTS_OPTION_PAGE_TOKEN); + QueryResponse response = + bigquery.getQueryResults( + queryJob, + QUERY_RESULTS_OPTION_TIME, + QUERY_RESULTS_OPTION_INDEX, + QUERY_RESULTS_OPTION_PAGE_SIZE, + QUERY_RESULTS_OPTION_PAGE_TOKEN); assertEquals(queryJob, response.getJobId()); assertEquals(true, response.jobCompleted()); assertEquals(false, response.getResult().cacheHit()); @@ -1194,8 +1272,8 @@ public void testGetQueryResultsWithOptions() { public void testWriter() { WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.of(TABLE_ID); EasyMock.expect( - bigqueryRpcMock.open(WriteChannelConfiguration.of(TABLE_ID_WITH_PROJECT).toPb())) - .andReturn("upload-id"); + bigqueryRpcMock.open(WriteChannelConfiguration.of(TABLE_ID_WITH_PROJECT).toPb())) + .andReturn("upload-id"); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); WriteChannel channel = bigquery.writer(writeChannelConfiguration); @@ -1210,10 +1288,14 @@ public void testRetryableException() { .andReturn(DATASET_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options + .toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) + .build() + .getService(); Dataset dataset = bigquery.getDataset(DATASET); - assertEquals(new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), - dataset); + assertEquals( + new Dataset(bigquery, new DatasetInfo.BuilderImpl(DATASET_INFO_WITH_PROJECT)), dataset); } @Test @@ -1223,7 +1305,11 @@ public void testNonRetryableException() { .andThrow(new BigQueryException(501, exceptionMessage)); EasyMock.replay(bigqueryRpcMock); bigquery = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options + .toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) + .build() + .getService(); thrown.expect(BigQueryException.class); thrown.expectMessage(exceptionMessage); bigquery.getDataset(DatasetId.of(DATASET)); @@ -1236,7 +1322,11 @@ public void testRuntimeException() { .andThrow(new RuntimeException(exceptionMessage)); EasyMock.replay(bigqueryRpcMock); bigquery = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options + .toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) + .build() + .getService(); thrown.expect(BigQueryException.class); thrown.expectMessage(exceptionMessage); bigquery.getDataset(DATASET); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java index 7b185e0421f0..adcd62b5eb71 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java @@ -27,7 +27,8 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -import com.google.cloud.Clock; +import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.SystemClock; import com.google.cloud.WaitForOption; import com.google.cloud.bigquery.JobStatistics.CopyStatistics; @@ -196,7 +197,7 @@ public void testWaitFor() throws InterruptedException, TimeoutException { JobStatus status = createStrictMock(JobStatus.class); expect(status.getState()).andReturn(JobStatus.State.DONE); expect(bigquery.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(Clock.defaultClock()); + expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); Job completedJob = expectedJob.toBuilder().setStatus(status).build(); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(completedJob); expect(bigquery.getJob(JOB_INFO.getJobId())).andReturn(completedJob); @@ -211,7 +212,7 @@ public void testWaitFor_Null() throws InterruptedException, TimeoutException { initializeExpectedJob(1); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; expect(bigquery.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(Clock.defaultClock()); + expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(null); expect(bigquery.getJob(JOB_INFO.getJobId())).andReturn(null); replay(bigquery, mockOptions); @@ -231,7 +232,7 @@ public void testWaitForWithCheckingPeriod() throws InterruptedException, Timeout expect(status.getState()).andReturn(JobStatus.State.RUNNING); expect(status.getState()).andReturn(JobStatus.State.DONE); expect(bigquery.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(Clock.defaultClock()); + expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); Job runningJob = expectedJob.toBuilder().setStatus(status).build(); Job completedJob = expectedJob.toBuilder().setStatus(status).build(); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(runningJob); @@ -251,7 +252,7 @@ public void testWaitForWithCheckingPeriod_Null() throws InterruptedException, Ti timeUnit.sleep(42); EasyMock.expectLastCall(); expect(bigquery.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(Clock.defaultClock()); + expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); Job runningJob = expectedJob.toBuilder().setStatus(new JobStatus(JobStatus.State.RUNNING)).build(); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(runningJob); @@ -270,10 +271,10 @@ public void testWaitForWithTimeout() throws InterruptedException, TimeoutExcepti TimeUnit timeUnit = createStrictMock(TimeUnit.class); timeUnit.sleep(1); EasyMock.expectLastCall(); - Clock clock = createStrictMock(Clock.class); - expect(clock.millis()).andReturn(0L); - expect(clock.millis()).andReturn(1L); - expect(clock.millis()).andReturn(3L); + NanoClock clock = createStrictMock(NanoClock.class); + expect(clock.millisTime()).andReturn(0L); + expect(clock.millisTime()).andReturn(1L); + expect(clock.millisTime()).andReturn(3L); JobStatus status = createStrictMock(JobStatus.class); expect(status.getState()).andReturn(JobStatus.State.RUNNING); expect(status.getState()).andReturn(JobStatus.State.RUNNING); diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelperTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelperTest.java index 2595406b747c..a9d556831017 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelperTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/testing/RemoteBigQueryHelperTest.java @@ -24,6 +24,7 @@ import com.google.cloud.bigquery.BigQueryOptions; import org.easymock.EasyMock; +import org.joda.time.Duration; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -84,10 +85,9 @@ public void testCreateFromStream() { assertEquals(PROJECT_ID, options.getProjectId()); assertEquals(60000, options.getConnectTimeout()); assertEquals(60000, options.getReadTimeout()); - assertEquals(10, options.getRetryParams().getRetryMaxAttempts()); - assertEquals(6, options.getRetryParams().getRetryMinAttempts()); - assertEquals(30000, options.getRetryParams().getMaxRetryDelayMillis()); - assertEquals(120000, options.getRetryParams().getTotalRetryPeriodMillis()); - assertEquals(250, options.getRetryParams().getInitialRetryDelayMillis()); + assertEquals(10, options.getRetrySettings().getMaxAttempts()); + assertEquals(Duration.millis(30000), options.getRetrySettings().getMaxRetryDelay()); + assertEquals(Duration.millis(120000), options.getRetrySettings().getTotalTimeout()); + assertEquals(Duration.millis(250), options.getRetrySettings().getInitialRetryDelay()); } } diff --git a/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeException.java b/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeException.java index 2f46cdb85163..c0d801624c1e 100644 --- a/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeException.java +++ b/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeException.java @@ -18,7 +18,6 @@ import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; -import com.google.cloud.RetryHelper.RetryInterruptedException; import com.google.common.collect.ImmutableSet; import java.io.IOException; @@ -54,10 +53,9 @@ protected Set getRetryableErrors() { * always throw an exception. * * @throws ComputeException when {@code ex} was caused by a {@code ComputeException} - * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ static BaseServiceException translateAndThrow(RetryHelperException ex) { - BaseServiceException.translateAndPropagateIfPossible(ex); + BaseServiceException.translate(ex); throw new ComputeException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } } diff --git a/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeImpl.java b/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeImpl.java index 34cdaf57ea33..e5bcb32e5568 100644 --- a/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeImpl.java +++ b/google-cloud-compute/src/main/java/com/google/cloud/compute/ComputeImpl.java @@ -471,7 +471,7 @@ public DiskType getDiskType(final DiskTypeId diskTypeId, DiskTypeOption... optio public com.google.api.services.compute.model.DiskType call() { return computeRpc.getDiskType(diskTypeId.getZone(), diskTypeId.getType(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : DiskType.fromPb(answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -499,7 +499,7 @@ private static Page listDiskTypes(final String zone, Iterable> call() { return serviceOptions.getRpc().listDiskTypes(zone, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable diskTypes = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -533,7 +533,7 @@ private static Page listDiskTypes(final ComputeOptions serviceOptions, Iterable> call() { return serviceOptions.getRpc().listDiskTypes(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable diskTypes = Iterables.transform(result.y(), new Function() { @@ -560,7 +560,7 @@ public com.google.api.services.compute.model.MachineType call() { return computeRpc.getMachineType(machineType.getZone(), machineType.getType(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : MachineType.fromPb(answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -588,7 +588,7 @@ private static Page listMachineTypes(final String zone, Iterable> call() { return serviceOptions.getRpc().listMachineTypes(zone, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable machineTypes = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -623,7 +623,7 @@ private static Page listMachineTypes(final ComputeOptions serviceOp Iterable> call() { return serviceOptions.getRpc().listMachineTypes(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable machineTypes = Iterables.transform(result.y(), new Function() { @@ -651,7 +651,7 @@ public Region getRegion(final String region, RegionOption... options) { public com.google.api.services.compute.model.Region call() { return computeRpc.getRegion(region, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Region.fromPb(answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -674,7 +674,7 @@ private static Page listRegions(final ComputeOptions serviceOptions, Iterable> call() { return serviceOptions.getRpc().listRegions(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable regions = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -702,7 +702,7 @@ public Zone getZone(final String zone, ZoneOption... options) { public com.google.api.services.compute.model.Zone call() { return computeRpc.getZone(zone, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Zone.fromPb(answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -725,7 +725,7 @@ private static Page listZones(final ComputeOptions serviceOptions, Iterable> call() { return serviceOptions.getRpc().listZones(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable zones = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -759,7 +759,7 @@ public com.google.api.services.compute.model.License call() { return computeRpc.getLicense(completeId.getProject(), completeId.getLicense(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : License.fromPb(answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -789,7 +789,7 @@ public com.google.api.services.compute.model.Operation call() { throw new IllegalArgumentException("Unexpected operation identity type"); } } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -822,7 +822,7 @@ private static Page listGlobalOperations(final ComputeOptions service Iterable> call() { return serviceOptions.getRpc().listGlobalOperations(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable operations = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -850,7 +850,7 @@ private static Page listRegionOperations(final String region, Iterable> call() { return serviceOptions.getRpc().listRegionOperations(region, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable operations = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -878,7 +878,7 @@ private static Page listZoneOperations(final String zone, Iterable> call() { return serviceOptions.getRpc().listZoneOperations(zone, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable operations = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -911,7 +911,7 @@ public Boolean call() { throw new IllegalArgumentException("Unexpected operation identity type"); } } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } @@ -936,7 +936,7 @@ public com.google.api.services.compute.model.Address call() { throw new IllegalArgumentException("Unexpected address identity type"); } } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Address.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -964,7 +964,7 @@ public com.google.api.services.compute.model.Operation call() { throw new IllegalArgumentException("Unexpected address identity type"); } } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } @@ -996,7 +996,7 @@ private static Page
listGlobalAddresses(final ComputeOptions serviceOpt Iterable> call() { return serviceOptions.getRpc().listGlobalAddresses(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable
operations = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1024,7 +1024,7 @@ private static Page
listRegionAddresses(final String region, Iterable> call() { return serviceOptions.getRpc().listRegionAddresses(region, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable
operations = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1052,7 +1052,7 @@ private static Page
listAddresses(final ComputeOptions serviceOptions, Iterable> call() { return serviceOptions.getRpc().listAddresses(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable
operations = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1089,7 +1089,7 @@ public com.google.api.services.compute.model.Operation call() { throw new IllegalArgumentException("Unexpected address identity type"); } } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1110,7 +1110,7 @@ public com.google.api.services.compute.model.Operation call() { completeSnapshot.getSnapshotId().getSnapshot(), completeSnapshot.getDescription(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1127,7 +1127,7 @@ public Snapshot getSnapshot(final String snapshot, SnapshotOption... options) { public com.google.api.services.compute.model.Snapshot call() { return computeRpc.getSnapshot(snapshot, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Snapshot.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1150,7 +1150,7 @@ private static Page listSnapshots(final ComputeOptions serviceOptions, Iterable> call() { return serviceOptions.getRpc().listSnapshots(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable snapshots = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1183,7 +1183,7 @@ public Operation deleteSnapshot(final String snapshot, OperationOption... option public com.google.api.services.compute.model.Operation call() { return computeRpc.deleteSnapshot(snapshot, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1201,7 +1201,7 @@ public Operation create(ImageInfo image, OperationOption... options) { public com.google.api.services.compute.model.Operation call() { return computeRpc.createImage(completeImage.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1220,7 +1220,7 @@ public com.google.api.services.compute.model.Image call() { return computeRpc.getImage(completeImageId.getProject(), completeImageId.getImage(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Image.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1248,7 +1248,7 @@ private static Page listImages(final String project, final ComputeOptions Iterable> call() { return serviceOptions.getRpc().listImages(project, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable images = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1278,7 +1278,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.deleteImage(completeId.getProject(), completeId.getImage(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1298,7 +1298,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.deprecateImage(completeId.getProject(), completeId.getImage(), deprecationStatus.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1315,7 +1315,7 @@ public Disk getDisk(final DiskId diskId, DiskOption... options) { public com.google.api.services.compute.model.Disk call() { return computeRpc.getDisk(diskId.getZone(), diskId.getDisk(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Disk.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1334,7 +1334,7 @@ public Operation create(final DiskInfo disk, OperationOption... options) { public com.google.api.services.compute.model.Operation call() { return computeRpc.createDisk(disk.getDiskId().getZone(), diskPb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } @@ -1366,7 +1366,7 @@ private static Page listDisks(final String zone, final ComputeOptions serv Iterable> call() { return serviceOptions.getRpc().listDisks(zone, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable disks = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1394,7 +1394,7 @@ private static Page listDisks(final ComputeOptions serviceOptions, Iterable> call() { return serviceOptions.getRpc().listDisks(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable disks = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1416,7 +1416,7 @@ public Operation deleteDisk(final DiskId disk, OperationOption... options) { public com.google.api.services.compute.model.Operation call() { return computeRpc.deleteDisk(disk.getZone(), disk.getDisk(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1433,7 +1433,7 @@ public Operation resize(final DiskId disk, final long sizeGb, OperationOption... public com.google.api.services.compute.model.Operation call() { return computeRpc.resizeDisk(disk.getZone(), disk.getDisk(), sizeGb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1452,7 +1452,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.createSubnetwork(completeSubnetwork.getSubnetworkId().getRegion(), completeSubnetwork.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1470,7 +1470,7 @@ public com.google.api.services.compute.model.Subnetwork call() { return computeRpc.getSubnetwork(subnetworkId.getRegion(), subnetworkId.getSubnetwork(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Subnetwork.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1503,7 +1503,7 @@ private static Page listSubnetworks(final String region, Iterable> call() { return serviceOptions.getRpc().listSubnetworks(region, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable subnetworks = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1531,7 +1531,7 @@ private static Page listSubnetworks(final ComputeOptions serviceOpti Iterable> call() { return serviceOptions.getRpc().listSubnetworks(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable subnetworks = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1554,7 +1554,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.deleteSubnetwork(subnetwork.getRegion(), subnetwork.getSubnetwork(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1572,7 +1572,7 @@ public Operation create(NetworkInfo network, OperationOption... options) { public com.google.api.services.compute.model.Operation call() { return computeRpc.createNetwork(completeNetwork.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1589,7 +1589,7 @@ public Network getNetwork(final String network, NetworkOption... options) { public com.google.api.services.compute.model.Network call() { return computeRpc.getNetwork(network, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Network.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1612,7 +1612,7 @@ private static Page listNetworks(final ComputeOptions serviceOptions, Iterable> call() { return serviceOptions.getRpc().listNetworks(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable networks = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1640,7 +1640,7 @@ public Operation deleteNetwork(final NetworkId network, OperationOption... optio public com.google.api.services.compute.model.Operation call() { return computeRpc.deleteNetwork(network.getNetwork(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1664,7 +1664,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.createInstance(completeInstance.getInstanceId().getZone(), completeInstance.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1681,7 +1681,7 @@ public Instance getInstance(final InstanceId instance, InstanceOption... options public com.google.api.services.compute.model.Instance call() { return computeRpc.getInstance(instance.getZone(), instance.getInstance(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Instance.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1714,7 +1714,7 @@ private static Page listInstances(final String zone, Iterable> call() { return serviceOptions.getRpc().listInstances(zone, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable instances = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1742,7 +1742,7 @@ private static Page listInstances(final ComputeOptions serviceOptions, Iterable> call() { return serviceOptions.getRpc().listInstances(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable instances = Iterables.transform( result.y() == null ? ImmutableList.of() @@ -1765,7 +1765,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.deleteInstance(instance.getZone(), instance.getInstance(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1784,7 +1784,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.addAccessConfig(instance.getZone(), instance.getInstance(), networkInterface, accessConfig.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1803,7 +1803,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.attachDisk(instance.getZone(), instance.getInstance(), completeDisk.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1844,7 +1844,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.deleteAccessConfig(instance.getZone(), instance.getInstance(), networkInterface, accessConfig, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1863,7 +1863,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.detachDisk(instance.getZone(), instance.getInstance(), deviceName, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1879,7 +1879,7 @@ public String call() { return computeRpc.getSerialPortOutput(instance.getZone(), instance.getInstance(), port, optionMap()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } @@ -1894,7 +1894,7 @@ public String call() { return computeRpc.getSerialPortOutput(instance.getZone(), instance.getInstance(), null, optionMap()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); } @@ -1910,7 +1910,7 @@ public Operation reset(final InstanceId instance, OperationOption... options) { public com.google.api.services.compute.model.Operation call() { return computeRpc.reset(instance.getZone(), instance.getInstance(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1929,7 +1929,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.setDiskAutoDelete(instance.getZone(), instance.getInstance(), deviceName, autoDelete, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1950,7 +1950,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.setMachineType(instance.getZone(), instance.getInstance(), machineTypeUrl, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1969,7 +1969,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.setMetadata(instance.getZone(), instance.getInstance(), metadata.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -1988,7 +1988,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.setScheduling(instance.getZone(), instance.getInstance(), schedulingOptions.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -2006,7 +2006,7 @@ public com.google.api.services.compute.model.Operation call() { return computeRpc.setTags(instance.getZone(), instance.getInstance(), tags.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -2023,7 +2023,7 @@ public Operation start(final InstanceId instance, OperationOption... options) { public com.google.api.services.compute.model.Operation call() { return computeRpc.start(instance.getZone(), instance.getInstance(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); @@ -2040,7 +2040,7 @@ public Operation stop(final InstanceId instance, OperationOption... options) { public com.google.api.services.compute.model.Operation call() { return computeRpc.stop(instance.getZone(), instance.getInstance(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Operation.fromPb(this, answer); } catch (RetryHelper.RetryHelperException e) { throw ComputeException.translateAndThrow(e); diff --git a/google-cloud-compute/src/main/java/com/google/cloud/compute/Operation.java b/google-cloud-compute/src/main/java/com/google/cloud/compute/Operation.java index a92360197d13..d526d88aecda 100644 --- a/google-cloud-compute/src/main/java/com/google/cloud/compute/Operation.java +++ b/google-cloud-compute/src/main/java/com/google/cloud/compute/Operation.java @@ -18,7 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.cloud.Clock; +import com.google.api.gax.core.NanoClock; import com.google.cloud.WaitForOption; import com.google.cloud.WaitForOption.CheckingPeriod; import com.google.cloud.compute.Compute.OperationOption; @@ -920,10 +920,10 @@ public Operation waitFor(WaitForOption... waitOptions) WaitForOption.Timeout timeout = WaitForOption.Timeout.getOrDefault(waitOptions); CheckingPeriod checkingPeriod = CheckingPeriod.getOrDefault(waitOptions); long timeoutMillis = timeout.getTimeoutMillis(); - Clock clock = options.getClock(); - long startTime = clock.millis(); + NanoClock clock = options.getClock(); + long startTime = clock.millisTime(); while (!isDone()) { - if (timeoutMillis != -1 && (clock.millis() - startTime) >= timeoutMillis) { + if (timeoutMillis != -1 && (clock.millisTime() - startTime) >= timeoutMillis) { throw new TimeoutException(); } checkingPeriod.sleep(); diff --git a/google-cloud-compute/src/main/java/com/google/cloud/compute/testing/RemoteComputeHelper.java b/google-cloud-compute/src/main/java/com/google/cloud/compute/testing/RemoteComputeHelper.java index dcf4e1b462d1..f02531aa94ae 100644 --- a/google-cloud-compute/src/main/java/com/google/cloud/compute/testing/RemoteComputeHelper.java +++ b/google-cloud-compute/src/main/java/com/google/cloud/compute/testing/RemoteComputeHelper.java @@ -17,7 +17,7 @@ package com.google.cloud.compute.testing; import com.google.auth.oauth2.ServiceAccountCredentials; -import com.google.cloud.RetryParams; +import com.google.api.gax.core.RetrySettings; import com.google.cloud.compute.ComputeOptions; import java.io.IOException; @@ -25,15 +25,16 @@ import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; +import org.joda.time.Duration; /** * Utility to create a remote Compute configuration for testing. Compute options can be obtained * via the {@link #options()} method. Returned options have custom - * {@link ComputeOptions#getRetryParams()}: {@link RetryParams#getRetryMaxAttempts()} is {@code 10}, - * {@link RetryParams#getRetryMinAttempts()} is {@code 6}, - * {@link RetryParams#getMaxRetryDelayMillis()} is {@code 30000}, - * {@link RetryParams#getTotalRetryPeriodMillis()} is {@code 120000} and - * {@link RetryParams#getInitialRetryDelayMillis()} is {@code 250}. + * {@link ComputeOptions#getRetrySettings()}: {@link RetrySettings#getMaxAttempts()} is {@code 10}, + * {@link RetrySettings#getRetryAttempts()} is {@code 6}, + * {@link RetrySettings#getMaxRetryDelay()} is {@code 30000}, + * {@link RetrySettings#getTotalTimeout()} is {@code 120000} and + * {@link RetrySettings#getInitialRetryDelay()} is {@code 250}. * {@link ComputeOptions#getConnectTimeout()} and {@link ComputeOptions#getReadTimeout()} are both * set to {@code 60000}. */ @@ -85,7 +86,7 @@ public static RemoteComputeHelper create(String projectId, InputStream keyStream ComputeOptions computeOptions = ComputeOptions.newBuilder() .setCredentials(ServiceAccountCredentials.fromStream(keyStream)) .setProjectId(projectId) - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setConnectTimeout(60000) .setReadTimeout(60000) .build(); @@ -104,20 +105,22 @@ public static RemoteComputeHelper create(String projectId, InputStream keyStream */ public static RemoteComputeHelper create() { ComputeOptions computeOptions = ComputeOptions.newBuilder() - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setConnectTimeout(60000) .setReadTimeout(60000) .build(); return new RemoteComputeHelper(computeOptions); } - private static RetryParams retryParams() { - return RetryParams.newBuilder() - .setRetryMaxAttempts(10) - .setRetryMinAttempts(6) - .setMaxRetryDelayMillis(30000) - .setTotalRetryPeriodMillis(120000) - .setInitialRetryDelayMillis(250) + private static RetrySettings retryParams() { + return RetrySettings.newBuilder().setMaxAttempts(10) + .setMaxRetryDelay(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(120000L)) + .setInitialRetryDelay(Duration.millis(250L)) + .setRetryDelayMultiplier(1.0) + .setInitialRpcTimeout(Duration.millis(120000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(120000L)) .build(); } diff --git a/google-cloud-compute/src/test/java/com/google/cloud/compute/ComputeImplTest.java b/google-cloud-compute/src/test/java/com/google/cloud/compute/ComputeImplTest.java index 27ac4551700d..9c04b748a746 100644 --- a/google-cloud-compute/src/test/java/com/google/cloud/compute/ComputeImplTest.java +++ b/google-cloud-compute/src/test/java/com/google/cloud/compute/ComputeImplTest.java @@ -26,7 +26,7 @@ import static org.junit.Assert.assertTrue; import com.google.cloud.Page; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.cloud.compute.NetworkInterface.AccessConfig; import com.google.cloud.compute.spi.ComputeRpc; import com.google.cloud.compute.spi.ComputeRpcFactory; @@ -453,12 +453,12 @@ public class ComputeImplTest { private static final Function OPERATION_TO_PB_FUNCTION = new Function() { - @Override - public com.google.api.services.compute.model.Operation apply(Operation operation) { - return operation.toPb(); - } - }; + com.google.api.services.compute.model.Operation>() { + @Override + public com.google.api.services.compute.model.Operation apply(Operation operation) { + return operation.toPb(); + } + }; private ComputeOptions options; private ComputeRpcFactory rpcFactoryMock; @@ -482,7 +482,7 @@ public void setUp() { options = ComputeOptions.newBuilder() .setProjectId(PROJECT) .setServiceRpcFactory(rpcFactoryMock) - .setRetryParams(RetryParams.noRetries()) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) .build(); Compute otherService = options.toBuilder().build().getService(); globalOperation = new Operation.Builder(otherService) @@ -657,7 +657,8 @@ public void testListDiskTypesNextPage() { public void testListEmptyDiskTypes() { ImmutableList diskTypes = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, diskTypes); + ComputeRpc.Tuple.>of(null, + diskTypes); EasyMock.expect(computeRpcMock.listDiskTypes(DISK_TYPE_ID.getZone(), EMPTY_RPC_OPTIONS)) .andReturn(result); EasyMock.replay(computeRpcMock); @@ -726,7 +727,8 @@ public void testAggregatedListDiskTypesNextPage() { public void testAggregatedListEmptyDiskTypes() { ImmutableList diskTypes = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, diskTypes); + ComputeRpc.Tuple.>of(null, + diskTypes); EasyMock.expect(computeRpcMock.listDiskTypes(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); compute = options.getService(); @@ -745,7 +747,7 @@ public void testAggregatedListDiskTypesWithOptions() { EasyMock.expect(computeRpcMock.listDiskTypes(DISK_TYPE_LIST_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); Page page = compute.listDiskTypes(DISK_TYPE_AGGREGATED_LIST_PAGE_SIZE, - DISK_TYPE_AGGREGATED_LIST_PAGE_TOKEN, DISK_TYPE_AGGREGATED_LIST_FILTER); + DISK_TYPE_AGGREGATED_LIST_PAGE_TOKEN, DISK_TYPE_AGGREGATED_LIST_FILTER); assertEquals(cursor, page.getNextPageCursor()); assertArrayEquals(diskTypeList.toArray(), Iterables.toArray(page.getValues(), DiskType.class)); } @@ -777,7 +779,7 @@ public void testGetMachineType_Null() { @Test public void testGetMachineTypeFromMachineTypeId() { EasyMock.expect(computeRpcMock.getMachineType( - MACHINE_TYPE_ID.getZone(), MACHINE_TYPE_ID.getType(), EMPTY_RPC_OPTIONS)) + MACHINE_TYPE_ID.getZone(), MACHINE_TYPE_ID.getType(), EMPTY_RPC_OPTIONS)) .andReturn(MACHINE_TYPE.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); @@ -857,7 +859,8 @@ public void testListEmptyMachineTypes() { ImmutableList machineTypes = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, + ComputeRpc.Tuple.>of( + null, machineTypes); EasyMock.expect(computeRpcMock.listMachineTypes(MACHINE_TYPE_ID.getZone(), EMPTY_RPC_OPTIONS)) .andReturn(result); @@ -937,7 +940,8 @@ public void testAggregatedListEmptyMachineTypes() { ImmutableList machineTypes = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, + ComputeRpc.Tuple.>of( + null, machineTypes); EasyMock.expect(computeRpcMock.listMachineTypes(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); @@ -1142,7 +1146,8 @@ public void testListZonesNextPage() { public void testListEmptyZones() { ImmutableList zones = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, zones); + ComputeRpc.Tuple.>of(null, + zones); EasyMock.expect(computeRpcMock.listZones(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); compute = options.getService(); @@ -1193,7 +1198,7 @@ public void testGetLicenseFromStringWithOptions() { .andReturn(LICENSE.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); - License license = compute.getLicense(LICENSE_ID.getLicense(), LICENSE_OPTION_FIELDS); + License license = compute.getLicense(LICENSE_ID.getLicense(), LICENSE_OPTION_FIELDS); assertEquals(LICENSE, license); String selector = (String) capturedOptions.getValue().get(LICENSE_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); @@ -1211,7 +1216,7 @@ public void testGetLicenseFromIdWithOptions() { .andReturn(LICENSE.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); - License license = compute.getLicense(licenseId, LICENSE_OPTION_FIELDS); + License license = compute.getLicense(licenseId, LICENSE_OPTION_FIELDS); assertEquals(LICENSE, license); String selector = (String) capturedOptions.getValue().get(LICENSE_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("selfLink")); @@ -1266,7 +1271,7 @@ public void testGetGlobalOperation_Null() { public void testGetGlobalOperationWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(computeRpcMock.getGlobalOperation( - eq(GLOBAL_OPERATION_ID.getOperation()), capture(capturedOptions))) + eq(GLOBAL_OPERATION_ID.getOperation()), capture(capturedOptions))) .andReturn(globalOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); @@ -1372,7 +1377,7 @@ public void testDeleteGlobalOperation_False() { @Test public void testGetRegionOperation() { EasyMock.expect(computeRpcMock.getRegionOperation(REGION_OPERATION_ID.getRegion(), - REGION_OPERATION_ID.getOperation(), EMPTY_RPC_OPTIONS)) + REGION_OPERATION_ID.getOperation(), EMPTY_RPC_OPTIONS)) .andReturn(regionOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); @@ -1395,7 +1400,7 @@ public void testGetRegionOperation_Null() { public void testGetRegionOperationWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(computeRpcMock.getRegionOperation(eq(REGION_OPERATION_ID.getRegion()), - eq(REGION_OPERATION_ID.getOperation()), capture(capturedOptions))) + eq(REGION_OPERATION_ID.getOperation()), capture(capturedOptions))) .andReturn(regionOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); @@ -1837,7 +1842,8 @@ public void testListGlobalAddressesNextPage() { public void testListEmptyGlobalAddresses() { ImmutableList addresses = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, addresses); + ComputeRpc.Tuple.>of(null, + addresses); EasyMock.expect(computeRpcMock.listGlobalAddresses(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); compute = options.getService(); @@ -1917,7 +1923,8 @@ public void testListRegionAddressesNextPage() { public void testListEmptyRegionAddresses() { ImmutableList addresses = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, addresses); + ComputeRpc.Tuple.>of(null, + addresses); EasyMock.expect( computeRpcMock.listRegionAddresses(REGION_ADDRESS_ID.getRegion(), EMPTY_RPC_OPTIONS)) .andReturn(result); @@ -1995,7 +2002,8 @@ public void testAggregatedListAddressesNextPage() { public void testAggregatedListEmptyAddresses() { ImmutableList addresses = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, addresses); + ComputeRpc.Tuple.>of(null, + addresses); EasyMock.expect(computeRpcMock.listAddresses(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); compute = options.getService(); @@ -2095,7 +2103,7 @@ public void testCreateSnapshotWithOptions() { Capture> capturedOptions = Capture.newInstance(); EasyMock.expect(computeRpcMock.createSnapshot(eq(DISK_ID.getZone()), eq(DISK_ID.getDisk()), eq(SNAPSHOT_ID.getSnapshot()), EasyMock.isNull(), capture(capturedOptions))) - .andReturn(zoneOperation.toPb()); + .andReturn(zoneOperation.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Operation operation = compute.create(SNAPSHOT, OPERATION_OPTION_FIELDS); @@ -2228,7 +2236,8 @@ public void testListEmptySnapshots() { compute = options.getService(); ImmutableList snapshots = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, snapshots); + ComputeRpc.Tuple.>of(null, + snapshots); EasyMock.expect(computeRpcMock.listSnapshots(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); Page page = compute.listSnapshots(); @@ -2284,7 +2293,7 @@ public void testCreateImageWithOptions() { public void testGetImage() { EasyMock.expect( computeRpcMock.getImage(IMAGE_ID.getProject(), IMAGE_ID.getImage(), EMPTY_RPC_OPTIONS)) - .andReturn(IMAGE.toPb()); + .andReturn(IMAGE.toPb()); EasyMock.replay(computeRpcMock); compute = options.getService(); Image image = compute.getImage(IMAGE_ID); @@ -2454,7 +2463,8 @@ public void testListEmptyImages() { compute = options.getService(); ImmutableList images = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, images); + ComputeRpc.Tuple.>of(null, + images); EasyMock.expect(computeRpcMock.listImages(PROJECT, EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); Page page = compute.listImages(); @@ -2467,7 +2477,8 @@ public void testListEmptyImagesForProject() { compute = options.getService(); ImmutableList images = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, images); + ComputeRpc.Tuple.>of(null, + images); EasyMock.expect(computeRpcMock.listImages("otherProject", EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); Page page = compute.listImages("otherProject"); @@ -2633,7 +2644,8 @@ public void testListEmptyDisks() { compute = options.getService(); ImmutableList disks = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, disks); + ComputeRpc.Tuple.>of(null, + disks); EasyMock.expect(computeRpcMock.listDisks(DISK_ID.getZone(), EMPTY_RPC_OPTIONS)) .andReturn(result); EasyMock.replay(computeRpcMock); @@ -2654,7 +2666,7 @@ public void testListDisksWithOptions() { EasyMock.expect(computeRpcMock.listDisks(DISK_ID.getZone(), DISK_LIST_OPTIONS)) .andReturn(result); EasyMock.replay(computeRpcMock); - Page page =compute.listDisks( + Page page = compute.listDisks( DISK_ID.getZone(), DISK_LIST_PAGE_SIZE, DISK_LIST_PAGE_TOKEN, DISK_LIST_FILTER); assertEquals(cursor, page.getNextPageCursor()); assertArrayEquals(diskList.toArray(), Iterables.toArray(page.getValues(), Disk.class)); @@ -2707,7 +2719,8 @@ public void testAggregatedListEmptyDisks() { compute = options.getService(); ImmutableList diskList = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, diskList); + ComputeRpc.Tuple.>of(null, + diskList); EasyMock.expect(computeRpcMock.listDisks(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); Page page = compute.listDisks(); @@ -2908,7 +2921,7 @@ public void testListSubnetworksNextPage() { Iterables.transform(subnetworkList, SubnetworkInfo.TO_PB_FUNCTION)); ComputeRpc.Tuple> nextResult = ComputeRpc.Tuple.of(nextCursor, - Iterables.transform(nextSubnetworkList, SubnetworkInfo.TO_PB_FUNCTION)); + Iterables.transform(nextSubnetworkList, SubnetworkInfo.TO_PB_FUNCTION)); Map nextOptions = ImmutableMap.of(ComputeRpc.Option.PAGE_TOKEN, cursor); EasyMock.expect(computeRpcMock.listSubnetworks(SUBNETWORK_ID.getRegion(), EMPTY_RPC_OPTIONS)) .andReturn(result); @@ -2931,7 +2944,8 @@ public void testListEmptySubnetworks() { ImmutableList subnetworks = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, + ComputeRpc.Tuple.>of( + null, subnetworks); EasyMock.expect(computeRpcMock.listSubnetworks(SUBNETWORK_ID.getRegion(), EMPTY_RPC_OPTIONS)) .andReturn(result); @@ -2995,7 +3009,7 @@ public void testAggregatedListSubnetworksNextPage() { Iterables.transform(subnetworkList, SubnetworkInfo.TO_PB_FUNCTION)); ComputeRpc.Tuple> nextResult = ComputeRpc.Tuple.of(nextCursor, - Iterables.transform(nextSubnetworkList, SubnetworkInfo.TO_PB_FUNCTION)); + Iterables.transform(nextSubnetworkList, SubnetworkInfo.TO_PB_FUNCTION)); Map nextOptions = ImmutableMap.of(ComputeRpc.Option.PAGE_TOKEN, cursor); EasyMock.expect(computeRpcMock.listSubnetworks(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.expect(computeRpcMock.listSubnetworks(nextOptions)).andReturn(nextResult); @@ -3016,7 +3030,8 @@ public void testAggregatedListEmptySubnetworks() { ImmutableList subnetworks = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, + ComputeRpc.Tuple.>of( + null, subnetworks); EasyMock.expect(computeRpcMock.listSubnetworks(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); @@ -3196,7 +3211,8 @@ public void testListEmptyNetworks() { compute = options.getService(); ImmutableList networks = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, networks); + ComputeRpc.Tuple.>of(null, + networks); EasyMock.expect(computeRpcMock.listNetworks(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(computeRpcMock); Page page = compute.listNetworks(); @@ -3374,7 +3390,8 @@ public void testListEmptyInstances() { compute = options.getService(); ImmutableList instances = ImmutableList.of(); ComputeRpc.Tuple> result = - ComputeRpc.Tuple.>of(null, instances); + ComputeRpc.Tuple.>of(null, + instances); EasyMock.expect(computeRpcMock.listInstances(INSTANCE_ID.getZone(), EMPTY_RPC_OPTIONS)) .andReturn(result); EasyMock.replay(computeRpcMock); @@ -4061,7 +4078,9 @@ public void testRetryableException() { .andReturn(DISK_TYPE.toPb()); EasyMock.replay(computeRpcMock); compute = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options.toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()).build() + .getService(); DiskType diskType = compute.getDiskType(DISK_TYPE_ID); assertEquals(DISK_TYPE, diskType); } @@ -4074,7 +4093,9 @@ public void testNonRetryableException() { .andThrow(new ComputeException(501, exceptionMessage)); EasyMock.replay(computeRpcMock); compute = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options.toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()).build() + .getService(); thrown.expect(ComputeException.class); thrown.expectMessage(exceptionMessage); compute.getDiskType(DISK_TYPE_ID); @@ -4088,7 +4109,9 @@ public void testRuntimeException() { .andThrow(new RuntimeException(exceptionMessage)); EasyMock.replay(computeRpcMock); compute = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options.toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()).build() + .getService(); thrown.expect(ComputeException.class); thrown.expectMessage(exceptionMessage); compute.getDiskType(DISK_TYPE_ID); diff --git a/google-cloud-compute/src/test/java/com/google/cloud/compute/OperationTest.java b/google-cloud-compute/src/test/java/com/google/cloud/compute/OperationTest.java index 1e6537727527..3455ff72f60a 100644 --- a/google-cloud-compute/src/test/java/com/google/cloud/compute/OperationTest.java +++ b/google-cloud-compute/src/test/java/com/google/cloud/compute/OperationTest.java @@ -28,7 +28,8 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -import com.google.cloud.Clock; +import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.SystemClock; import com.google.cloud.WaitForOption; import com.google.cloud.compute.Operation.OperationError; import com.google.cloud.compute.Operation.OperationWarning; @@ -440,7 +441,7 @@ public void testWaitFor() throws InterruptedException, TimeoutException { Operation successOperation = Operation.fromPb(serviceMockReturnsOptions, globalOperation.toPb().setError(null)); expect(compute.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(Clock.defaultClock()); + expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(successOperation); expect(compute.getOperation(GLOBAL_OPERATION_ID)).andReturn(successOperation); replay(compute, mockOptions); @@ -455,7 +456,7 @@ public void testWaitFor_Null() throws InterruptedException, TimeoutException { Compute.OperationOption[] expectedOptions = {Compute.OperationOption.fields(Compute.OperationField.STATUS)}; expect(compute.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(Clock.defaultClock()); + expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(null); expect(compute.getOperation(GLOBAL_OPERATION_ID)).andReturn(null); replay(compute, mockOptions); @@ -477,7 +478,7 @@ public void testWaitForCheckingPeriod() throws InterruptedException, TimeoutExce Operation completedOperation = Operation.fromPb(serviceMockReturnsOptions, globalOperation.toPb().setError(null)); expect(compute.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(Clock.defaultClock()); + expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(runningOperation); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)) .andReturn(completedOperation); @@ -499,7 +500,7 @@ public void testWaitForCheckingPeriod_Null() throws InterruptedException, Timeou Operation runningOperation = Operation.fromPb(serviceMockReturnsOptions, globalOperation.toPb().setError(null).setStatus("RUNNING")); expect(compute.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(Clock.defaultClock()); + expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(runningOperation); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(null); expect(compute.getOperation(GLOBAL_OPERATION_ID)).andReturn(null); @@ -517,10 +518,10 @@ public void testWaitForWithTimeout() throws InterruptedException, TimeoutExcepti TimeUnit timeUnit = createStrictMock(TimeUnit.class); timeUnit.sleep(1); EasyMock.expectLastCall(); - Clock clock = createStrictMock(Clock.class); - expect(clock.millis()).andReturn(0L); - expect(clock.millis()).andReturn(1L); - expect(clock.millis()).andReturn(3L); + NanoClock clock = createStrictMock(NanoClock.class); + expect(clock.millisTime()).andReturn(0L); + expect(clock.millisTime()).andReturn(1L); + expect(clock.millisTime()).andReturn(3L); Operation runningOperation = Operation.fromPb(serviceMockReturnsOptions, globalOperation.toPb().setError(null).setStatus("RUNNING")); expect(compute.getOptions()).andReturn(mockOptions); diff --git a/google-cloud-compute/src/test/java/com/google/cloud/compute/SerializationTest.java b/google-cloud-compute/src/test/java/com/google/cloud/compute/SerializationTest.java index 93fe6d16f4a1..fcf146864afc 100644 --- a/google-cloud-compute/src/test/java/com/google/cloud/compute/SerializationTest.java +++ b/google-cloud-compute/src/test/java/com/google/cloud/compute/SerializationTest.java @@ -19,7 +19,7 @@ import com.google.cloud.BaseSerializationTest; import com.google.cloud.NoCredentials; import com.google.cloud.Restorable; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.cloud.compute.AttachedDisk.CreateDiskConfiguration; import com.google.cloud.compute.AttachedDisk.PersistentDiskConfiguration; import com.google.cloud.compute.AttachedDisk.ScratchDiskConfiguration; @@ -269,7 +269,7 @@ protected Serializable[] serializableObjects() { .build(); ComputeOptions otherOptions = options.toBuilder() .setProjectId("p2") - .setRetryParams(RetryParams.getDefaultInstance()) + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build(); return new Serializable[]{DISK_TYPE_ID, DISK_TYPE, MACHINE_TYPE_ID, MACHINE_TYPE, REGION_ID, REGION, ZONE_ID, ZONE, LICENSE_ID, LICENSE, DEPRECATION_STATUS, GLOBAL_OPERATION_ID, diff --git a/google-cloud-compute/src/test/java/com/google/cloud/compute/testing/RemoteComputeHelperTest.java b/google-cloud-compute/src/test/java/com/google/cloud/compute/testing/RemoteComputeHelperTest.java index a859c49f9451..d1cf47b84f67 100644 --- a/google-cloud-compute/src/test/java/com/google/cloud/compute/testing/RemoteComputeHelperTest.java +++ b/google-cloud-compute/src/test/java/com/google/cloud/compute/testing/RemoteComputeHelperTest.java @@ -24,6 +24,7 @@ import com.google.cloud.compute.ComputeOptions; import com.google.cloud.compute.testing.RemoteComputeHelper.ComputeHelperException; +import org.joda.time.Duration; import org.junit.Test; import java.io.ByteArrayInputStream; @@ -79,11 +80,10 @@ public void testCreateFromStream() { assertEquals(PROJECT_ID, options.getProjectId()); assertEquals(60000, options.getConnectTimeout()); assertEquals(60000, options.getReadTimeout()); - assertEquals(10, options.getRetryParams().getRetryMaxAttempts()); - assertEquals(6, options.getRetryParams().getRetryMinAttempts()); - assertEquals(30000, options.getRetryParams().getMaxRetryDelayMillis()); - assertEquals(120000, options.getRetryParams().getTotalRetryPeriodMillis()); - assertEquals(250, options.getRetryParams().getInitialRetryDelayMillis()); + assertEquals(10, options.getRetrySettings().getMaxAttempts()); + assertEquals(Duration.millis(30000), options.getRetrySettings().getMaxRetryDelay()); + assertEquals(Duration.millis(120000), options.getRetrySettings().getTotalTimeout()); + assertEquals(Duration.millis(250), options.getRetrySettings().getInitialRetryDelay()); } @Test diff --git a/google-cloud-core/src/main/java/com/google/cloud/BaseServiceException.java b/google-cloud-core/src/main/java/com/google/cloud/BaseServiceException.java index cb834f8d3608..4bebfc925923 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/BaseServiceException.java +++ b/google-cloud-core/src/main/java/com/google/cloud/BaseServiceException.java @@ -295,12 +295,9 @@ private static String message(IOException exception) { return exception.getMessage(); } - protected static void translateAndPropagateIfPossible(RetryHelper.RetryHelperException ex) { + protected static void translate(RetryHelper.RetryHelperException ex) { if (ex.getCause() instanceof BaseServiceException) { throw (BaseServiceException) ex.getCause(); } - if (ex instanceof RetryHelper.RetryInterruptedException) { - RetryHelper.RetryInterruptedException.propagate(); - } } } diff --git a/google-cloud-core/src/main/java/com/google/cloud/Clock.java b/google-cloud-core/src/main/java/com/google/cloud/Clock.java deleted file mode 100644 index 447200b03bf4..000000000000 --- a/google-cloud-core/src/main/java/com/google/cloud/Clock.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2016 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud; - -import java.io.ObjectStreamException; -import java.io.Serializable; - -/** - * A class providing access to the current time in milliseconds. This class is mainly used for - * testing and will be replaced by Java8's {@code java.time.Clock}. - * - *

Implementations should implement {@code Serializable} wherever possible and must document - * whether or not they do support serialization. - */ -public abstract class Clock { - - private static final Clock DEFAULT_TIME_SOURCE = new DefaultClock(); - - /** - * Returns current time in milliseconds according to this clock. - */ - public abstract long millis(); - - /** - * Returns the default clock. Default clock uses {@link System#currentTimeMillis()} to get time - * in milliseconds. - */ - public static Clock defaultClock() { - return DEFAULT_TIME_SOURCE; - } - - private static class DefaultClock extends Clock implements Serializable { - - private static final long serialVersionUID = -5077300394286703864L; - - @Override - public long millis() { - return System.currentTimeMillis(); - } - - private Object readResolve() throws ObjectStreamException { - return DEFAULT_TIME_SOURCE; - } - } -} diff --git a/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java b/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java index a2a69a737641..c455641090ad 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java @@ -234,14 +234,14 @@ void verifyCaller(Callable callable) { } - public Set> getRetriableExceptions() { - return retriableExceptions; - } - - - public Set> getNonRetriableExceptions() { - return nonRetriableExceptions; - } +// public Set> getRetriableExceptions() { +// return retriableExceptions; +// } +// +// +// public Set> getNonRetriableExceptions() { +// return nonRetriableExceptions; +// } boolean shouldRetry(Exception ex) { for (Interceptor interceptor : interceptors) { diff --git a/google-cloud-core/src/main/java/com/google/cloud/GrpcServiceOptions.java b/google-cloud-core/src/main/java/com/google/cloud/GrpcServiceOptions.java index 8777ac47834b..9b747122eea6 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/GrpcServiceOptions.java +++ b/google-cloud-core/src/main/java/com/google/cloud/GrpcServiceOptions.java @@ -229,17 +229,7 @@ protected ExecutorFactory getExecutorFactory() { * Returns a builder for API call settings. */ protected UnaryCallSettings.Builder getApiCallSettings() { - // todo(mziccard): specify timeout these settings: - // retryParams().retryMaxAttempts(), retryParams().retryMinAttempts() - final RetrySettings.Builder builder = RetrySettings.newBuilder() - .setTotalTimeout(Duration.millis(getRetryParams().getTotalRetryPeriodMillis())) - .setInitialRpcTimeout(Duration.millis(getInitialTimeout())) - .setRpcTimeoutMultiplier(getTimeoutMultiplier()) - .setMaxRpcTimeout(Duration.millis(getMaxTimeout())) - .setInitialRetryDelay(Duration.millis(getRetryParams().getInitialRetryDelayMillis())) - .setRetryDelayMultiplier(getRetryParams().getRetryDelayBackoffFactor()) - .setMaxRetryDelay(Duration.millis(getRetryParams().getMaxRetryDelayMillis())); - return UnaryCallSettings.newBuilder().setRetrySettingsBuilder(builder); + return UnaryCallSettings.newBuilder().setRetrySettingsBuilder(getRetrySettings().toBuilder()); } /** diff --git a/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java b/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java index b23a516ccac4..75a07f4ad050 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java +++ b/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java @@ -16,234 +16,58 @@ package com.google.cloud; -import static com.google.common.base.Preconditions.checkNotNull; -import static java.lang.StrictMath.max; -import static java.lang.StrictMath.min; -import static java.lang.StrictMath.pow; -import static java.lang.StrictMath.random; +import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.RetrySettings; +import com.google.api.gax.retrying.DirectRetryHandler; +import com.google.api.gax.retrying.RetryAttemptSettings; +import com.google.api.gax.retrying.RetryFuture; -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.MoreObjects; -import com.google.common.base.MoreObjects.ToStringHelper; - -import java.io.InterruptedIOException; -import java.nio.channels.ClosedByInterruptException; import java.util.concurrent.Callable; -import java.util.logging.Level; -import java.util.logging.Logger; /** - * Utility class for retrying operations. For more details about the parameters, see - * {@link RetryParams}. If the request is never successful, a {@link RetriesExhaustedException} will - * be thrown. + * Utility class for retrying operations. For more details about the parameters, see {@link + * RetrySettings}. * - * @param return value of the closure that is being run with retries */ -public class RetryHelper { - - private static final Logger log = Logger.getLogger(RetryHelper.class.getName()); - - private final Clock clock; - private final Callable callable; - private final RetryParams params; - private final ExceptionHandler exceptionHandler; - private int attemptNumber; - - - private static final ThreadLocal context = new ThreadLocal<>(); - - public static class RetryHelperException extends RuntimeException { - - private static final long serialVersionUID = -2907061015610448235L; - - RetryHelperException() {} - - RetryHelperException(String message) { - super(message); - } - - RetryHelperException(Throwable cause) { - super(cause); - } - - RetryHelperException(String message, Throwable cause) { - super(message, cause); - } - } - - /** - * Thrown when a RetryHelper failed to complete its work due to interruption. Throwing this - * exception also sets the thread interrupt flag. - */ - public static final class RetryInterruptedException extends RetryHelperException { - - private static final long serialVersionUID = 1678966737697204885L; - - RetryInterruptedException() {} - - /** - * Sets the caller thread interrupt flag and throws {@code RetryInterruptedException}. - */ - public static void propagate() throws RetryInterruptedException { - Thread.currentThread().interrupt(); - throw new RetryInterruptedException(); - } - } - - /** - * Thrown when a RetryHelper has attempted the maximum number of attempts allowed by RetryParams - * and was not successful. - */ - public static final class RetriesExhaustedException extends RetryHelperException { - - private static final long serialVersionUID = 780199686075408083L; - - RetriesExhaustedException(String message) { - super(message); - } - - RetriesExhaustedException(String message, Throwable cause) { - super(message, cause); - } - } - - /** - * Thrown when RetryHelper callable has indicate it should not be retried. - */ - public static final class NonRetriableException extends RetryHelperException { - - private static final long serialVersionUID = -2331878521983499652L; - - NonRetriableException(Throwable throwable) { - super(throwable); +public class RetryHelper { + public static V runWithRetries( + Callable callable, + RetrySettings retrySettings, + ExceptionHandler exceptionHandler, + NanoClock clock) + throws RetryHelperException { + try { + RetryHandler retryHandler = new RetryHandler<>(clock, exceptionHandler); + RetryFuture retryFuture = retryHandler.createFirstAttempt(callable, retrySettings); + retryFuture.setAttemptFuture( + retryHandler.executeAttempt(callable, retryFuture.getAttemptSettings())); + return retryFuture.get(); + } catch (Exception e) { + throw new RetryHelperException(e.getCause()); } } - static class Context { + private static class RetryHandler extends DirectRetryHandler { + private final ExceptionHandler exceptionHandler; - private final RetryHelper helper; - - Context(RetryHelper helper) { - this.helper = helper; - } - - public RetryParams getRetryParams() { - return helper.params; + private RetryHandler(NanoClock clock, ExceptionHandler exceptionHandler) { + super(clock); + this.exceptionHandler = exceptionHandler; } - public int getAttemptNumber() { - return helper.attemptNumber; - } - } - - @VisibleForTesting - static void setContext(Context ctx) { - if (ctx == null) { - context.remove(); - } else { - context.set(ctx); - } - } - - static Context getContext() { - return context.get(); - } - - @VisibleForTesting - RetryHelper(Callable callable, RetryParams params, ExceptionHandler exceptionHandler, - Clock clock) { - this.callable = checkNotNull(callable); - this.params = checkNotNull(params); - this.clock = checkNotNull(clock); - this.exceptionHandler = checkNotNull(exceptionHandler); - exceptionHandler.verifyCaller(callable); - } - - @Override - public String toString() { - ToStringHelper toStringHelper = MoreObjects.toStringHelper(this); - toStringHelper.add("params", params); - toStringHelper.add("clock", clock); - toStringHelper.add("attemptNumber", attemptNumber); - toStringHelper.add("callable", callable); - toStringHelper.add("exceptionHandler", exceptionHandler); - return toStringHelper.toString(); - } - - private V doRetry() throws RetryHelperException { - long start = clock.millis(); - while (true) { - attemptNumber++; - Exception exception; - try { - V value = callable.call(); - if (attemptNumber > 1 && log.isLoggable(Level.FINE)) { - log.fine(this + ": attempt #" + attemptNumber + " succeeded"); - } - return value; - } catch (InterruptedException | InterruptedIOException | ClosedByInterruptException e) { - if (!exceptionHandler.shouldRetry(e)) { - RetryInterruptedException.propagate(); - } - exception = e; - } catch (Exception e) { - if (!exceptionHandler.shouldRetry(e)) { - throw new NonRetriableException(e); - } - exception = e; - } - if (attemptNumber >= params.getRetryMaxAttempts() - || attemptNumber >= params.getRetryMinAttempts() - && clock.millis() - start >= params.getTotalRetryPeriodMillis()) { - throw new RetriesExhaustedException(this + ": Too many failures, giving up", exception); - } - long sleepDurationMillis = getSleepDuration(params, attemptNumber); - if (log.isLoggable(Level.FINE)) { - log.fine(this + ": Attempt #" + attemptNumber + " failed [" + exception - + "], sleeping for " + sleepDurationMillis + " ms"); - } - try { - Thread.sleep(sleepDurationMillis); - } catch (InterruptedException e) { - // propagate as RetryInterruptedException - RetryInterruptedException.propagate(); - } + @Override + public boolean accept(Throwable e, RetryAttemptSettings nextAttemptSettings) { + return super.accept(e, nextAttemptSettings) + && (e instanceof Exception) + && exceptionHandler.shouldRetry((Exception) e); } } - @VisibleForTesting - static long getSleepDuration(RetryParams retryParams, int attemptsSoFar) { - long initialDelay = retryParams.getInitialRetryDelayMillis(); - double backoffFactor = retryParams.getRetryDelayBackoffFactor(); - long maxDelay = retryParams.getMaxRetryDelayMillis(); - long retryDelay = getExponentialValue(initialDelay, backoffFactor, maxDelay, attemptsSoFar); - return (long) ((random() / 2.0 + .75) * retryDelay); - } - - private static long getExponentialValue(long initialDelay, double backoffFactor, long maxDelay, - int attemptsSoFar) { - return (long) min(maxDelay, pow(backoffFactor, max(1, attemptsSoFar) - 1) * initialDelay); - } - - public static V runWithRetries(Callable callable) throws RetryHelperException { - return runWithRetries(callable, RetryParams.getDefaultInstance(), - ExceptionHandler.getDefaultInstance()); - } - - public static V runWithRetries(Callable callable, RetryParams params, - ExceptionHandler exceptionHandler) throws RetryHelperException { - return runWithRetries(callable, params, exceptionHandler, Clock.defaultClock()); - } + public static class RetryHelperException extends RuntimeException { + private static final long serialVersionUID = -8519852520090965314L; - public static V runWithRetries(Callable callable, RetryParams params, - ExceptionHandler exceptionHandler, Clock clock) throws RetryHelperException { - RetryHelper retryHelper = new RetryHelper<>(callable, params, exceptionHandler, clock); - Context previousContext = getContext(); - setContext(new Context(retryHelper)); - try { - return retryHelper.doRetry(); - } finally { - setContext(previousContext); + RetryHelperException(Throwable cause) { + super(cause); } } } diff --git a/google-cloud-core/src/main/java/com/google/cloud/RetryParams.java b/google-cloud-core/src/main/java/com/google/cloud/RetryParams.java deleted file mode 100644 index f737032cfe7a..000000000000 --- a/google-cloud-core/src/main/java/com/google/cloud/RetryParams.java +++ /dev/null @@ -1,325 +0,0 @@ -/* - * Copyright 2015 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud; - -import static com.google.common.base.Preconditions.checkArgument; - -import com.google.common.base.MoreObjects; -import com.google.common.base.MoreObjects.ToStringHelper; - -import java.io.Serializable; -import java.util.Objects; - -/** - * Parameters for configuring retries with an exponential backoff. Initial request is executed - * immediately. If the request fails but passes the {@link ExceptionHandler} criteria the calling - * thread sleeps for {@code initialRetryDelayMillis}. Each subsequent failure the sleep interval is - * calculated as: - *

- * {@code retryDelayBackoffFactor ^ attempts * initialRetryDelayMillis} but would be upper-bounded - * to {@code maxRetryDelayMillis} - *

- * This proceeds until either the request is successful, {@code retryMaxAttempts} are made, or both - * {@code retryMinAttempts} are made and {@code totalRetryPeriodMillis} have elapsed. To construct - * {@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} with all the required settings. - * - * @see RetryHelper - */ -public final class RetryParams implements Serializable { - - private static final long serialVersionUID = -8492751576749007700L; - - /** - * Note that App Engine Standard Environment front-end modules have a 60 second deadline for HTTP - * requests. For that reason, we set the default total retry period to less than 60 seconds. - */ - public static final long DEFAULT_TOTAL_RETRY_PERIOD_MILLIS = 50_000L; - public static final int DEFAULT_RETRY_MIN_ATTEMPTS = 3; - public static final int DEFAULT_RETRY_MAX_ATTEMPTS = 6; - public static final long DEFAULT_INITIAL_RETRY_DELAY_MILLIS = 1000L; - public static final long DEFAULT_MAX_RETRY_DELAY_MILLIS = 32_000L; - public static final double DEFAULT_RETRY_DELAY_BACKOFF_FACTOR = 2.0; - - private final int retryMinAttempts; - private final int retryMaxAttempts; - private final long initialRetryDelayMillis; - private final long maxRetryDelayMillis; - private final double retryDelayBackoffFactor; - private final long totalRetryPeriodMillis; - - // Some services may have different backoff requirements listed in their SLAs. Be sure to override - // ServiceOptions.defaultRetryParams() in options subclasses when the service's backoff - // requirement differs from the default parameters used here. - private static final RetryParams DEFAULT_INSTANCE = new RetryParams(new Builder()); - private static final RetryParams NO_RETRIES = - newBuilder().setRetryMaxAttempts(1).setRetryMinAttempts(1).build(); - - /** - * RetryParams builder. - */ - public static final class Builder { - - private int retryMinAttempts; - private int retryMaxAttempts; - private long initialRetryDelayMillis; - private long maxRetryDelayMillis; - private double retryDelayBackoffFactor; - private long totalRetryPeriodMillis; - - private Builder() { - this(null); - } - - Builder(/* Nullable */RetryParams retryParams) { - if (retryParams == null) { - retryMinAttempts = DEFAULT_RETRY_MIN_ATTEMPTS; - retryMaxAttempts = DEFAULT_RETRY_MAX_ATTEMPTS; - initialRetryDelayMillis = DEFAULT_INITIAL_RETRY_DELAY_MILLIS; - maxRetryDelayMillis = DEFAULT_MAX_RETRY_DELAY_MILLIS; - 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(); - } - } - - - /** - * Sets retryMinAttempts. - * - * @param retryMinAttempts the retryMinAttempts to set - * @return the Builder for chaining - */ - public Builder setRetryMinAttempts(int retryMinAttempts) { - this.retryMinAttempts = retryMinAttempts; - return this; - } - - - /** - * Sets retryMaxAttempts. - * - * @param retryMaxAttempts the retryMaxAttempts to set - * @return the Builder for chaining - */ - public Builder setRetryMaxAttempts(int retryMaxAttempts) { - this.retryMaxAttempts = retryMaxAttempts; - return this; - } - - - /** - * Sets initialRetryDelayMillis. - * - * @param initialRetryDelayMillis the initialRetryDelayMillis to set - * @return the Builder for chaining - */ - public Builder setInitialRetryDelayMillis(long initialRetryDelayMillis) { - this.initialRetryDelayMillis = initialRetryDelayMillis; - return this; - } - - - /** - * Sets maxRetryDelayMillis. - * - * @param maxRetryDelayMillis the maxRetryDelayMillis to set - * @return the Builder for chaining - */ - public Builder setMaxRetryDelayMillis(long maxRetryDelayMillis) { - this.maxRetryDelayMillis = maxRetryDelayMillis; - return this; - } - - - /** - * Sets retryDelayBackoffFactor. - * - * @param retryDelayBackoffFactor the retryDelayBackoffFactor to set - * @return the Builder for chaining - */ - public Builder setRetryDelayBackoffFactor(double retryDelayBackoffFactor) { - this.retryDelayBackoffFactor = retryDelayBackoffFactor; - return this; - } - - - /** - * Sets totalRetryPeriodMillis. Note that App Engine Standard Environment front-end modules have - * a 60 second deadline for HTTP requests. For that reason, you should set the total retry - * period to under 60 seconds if you are using it on an App Engine front-end module. - * - * @param totalRetryPeriodMillis the totalRetryPeriodMillis to set - * @return the Builder for chaining - */ - public Builder setTotalRetryPeriodMillis(long totalRetryPeriodMillis) { - this.totalRetryPeriodMillis = totalRetryPeriodMillis; - return this; - } - - /** - * Create an instance of RetryParams with the parameters set in this builder. - * - * @return a new instance of RetryParams - */ - public RetryParams build() { - return new RetryParams(this); - } - } - - /** - * Create a new RetryParams with the parameters from a {@link RetryParams.Builder} - * - * @param builder the parameters to use to construct the RetryParams object - */ - private RetryParams(Builder builder) { - retryMinAttempts = builder.retryMinAttempts; - retryMaxAttempts = builder.retryMaxAttempts; - initialRetryDelayMillis = builder.initialRetryDelayMillis; - maxRetryDelayMillis = builder.maxRetryDelayMillis; - retryDelayBackoffFactor = builder.retryDelayBackoffFactor; - totalRetryPeriodMillis = builder.totalRetryPeriodMillis; - checkArgument(retryMinAttempts >= 0, "retryMinAttempts must not be negative"); - checkArgument(retryMaxAttempts >= retryMinAttempts, - "retryMaxAttempts must not be smaller than retryMinAttempts"); - checkArgument(initialRetryDelayMillis >= 0, "initialRetryDelayMillis must not be negative"); - checkArgument(maxRetryDelayMillis >= initialRetryDelayMillis, - "maxRetryDelayMillis must not be smaller than initialRetryDelayMillis"); - checkArgument(retryDelayBackoffFactor >= 0, "retryDelayBackoffFactor must not be negative"); - checkArgument(totalRetryPeriodMillis >= 0, "totalRetryPeriodMillis must not be negative"); - } - - - /** - * Returns an instance with the default parameters. - */ - public static RetryParams getDefaultInstance() { - return DEFAULT_INSTANCE; - } - - /** - * Returns an instance of {@code RetryParams} where `retryMaxAttempts` and `retryMinAttempts` are - * set to `1`. - */ - public static RetryParams noRetries() { - return NO_RETRIES; - } - - - /** - * Returns the retryMinAttempts. Default value is {@value #DEFAULT_RETRY_MIN_ATTEMPTS}. - */ - public int getRetryMinAttempts() { - return retryMinAttempts; - } - - - /** - * Returns the retryMaxAttempts. Default value is {@value #DEFAULT_RETRY_MAX_ATTEMPTS}. - */ - public int getRetryMaxAttempts() { - return retryMaxAttempts; - } - - - /** - * Returns the initialRetryDelayMillis. Default value is - * {@value #DEFAULT_INITIAL_RETRY_DELAY_MILLIS}. - */ - public long getInitialRetryDelayMillis() { - return initialRetryDelayMillis; - } - - - /** - * Returns the maxRetryDelayMillis. Default values is {@value #DEFAULT_MAX_RETRY_DELAY_MILLIS}. - */ - public long getMaxRetryDelayMillis() { - return maxRetryDelayMillis; - } - - - /** - * Returns the maxRetryDelayBackoffFactor. Default values is - * {@value #DEFAULT_RETRY_DELAY_BACKOFF_FACTOR}. - */ - public double getRetryDelayBackoffFactor() { - return retryDelayBackoffFactor; - } - - - /** - * Returns the totalRetryPeriodMillis. Default value is - * {@value #DEFAULT_TOTAL_RETRY_PERIOD_MILLIS}. - */ - public long getTotalRetryPeriodMillis() { - return totalRetryPeriodMillis; - } - - @Override - public int hashCode() { - return Objects.hash(retryMinAttempts, retryMaxAttempts, initialRetryDelayMillis, - maxRetryDelayMillis, retryDelayBackoffFactor, totalRetryPeriodMillis); - } - - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } - if (!(obj instanceof RetryParams)) { - return false; - } - RetryParams other = (RetryParams) obj; - return retryMinAttempts == other.retryMinAttempts && retryMaxAttempts == other.retryMaxAttempts - && initialRetryDelayMillis == other.initialRetryDelayMillis - && maxRetryDelayMillis == other.maxRetryDelayMillis - && retryDelayBackoffFactor == other.retryDelayBackoffFactor - && totalRetryPeriodMillis == other.totalRetryPeriodMillis; - } - - @Override - public String toString() { - ToStringHelper toStringHelper = MoreObjects.toStringHelper(this); - toStringHelper.add("retryMinAttempts", retryMinAttempts); - toStringHelper.add("retryMaxAttempts", retryMaxAttempts); - toStringHelper.add("initialRetryDelayMillis", initialRetryDelayMillis); - toStringHelper.add("maxRetryDelayMillis", maxRetryDelayMillis); - toStringHelper.add("retryDelayBackoffFactor", retryDelayBackoffFactor); - toStringHelper.add("totalRetryPeriodMillis", totalRetryPeriodMillis); - return toStringHelper.toString(); - } - - - public static Builder newBuilder() { - return new Builder(); - } - - public Builder toBuilder() { - return new Builder(this); - } -} diff --git a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index d5b155486b58..d163fd58401e 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -21,12 +21,16 @@ import static com.google.common.base.Preconditions.checkNotNull; import static java.nio.charset.StandardCharsets.UTF_8; +import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.RetrySettings; +import com.google.api.gax.core.SystemClock; import com.google.auth.Credentials; import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.spi.ServiceRpcFactory; import com.google.common.collect.Iterables; import com.google.common.io.Files; +import org.joda.time.Duration; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; @@ -78,17 +82,22 @@ public abstract class ServiceOptions, Service private static final String LIBRARY_VERSION = defaultLibraryVersion(); private static final String APPLICATION_NAME = LIBRARY_VERSION == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + LIBRARY_VERSION; - private static final long serialVersionUID = -5714029257168617973L; private static final String META_FILE_ROOT = "/META-INF/maven/"; private static final String META_VERSION_KEY = "version"; + private static final RetrySettings DEFAULT_RETRY_SETTINGS = getDefaultRetrySettingsBuilder() + .build(); + private static final RetrySettings NO_RETRY_SETTINGS = getDefaultRetrySettingsBuilder() + .setMaxAttempts(1).build(); + + private static final long serialVersionUID = 9198896031667942014L; private final String projectId; private final String host; - private final RetryParams retryParams; + private final RetrySettings retrySettings; private final String serviceRpcFactoryClassName; private final String serviceFactoryClassName; - private final Clock clock; + private final NanoClock clock; private final Credentials credentials; private transient ServiceRpcFactory serviceRpcFactory; @@ -111,18 +120,19 @@ public abstract static class Builder, Service private String projectId; private String host; private Credentials credentials; - private RetryParams retryParams; + private RetrySettings retrySettings; private ServiceFactory serviceFactory; private ServiceRpcFactory serviceRpcFactory; - private Clock clock; + private NanoClock clock; - protected Builder() {} + protected Builder() { + } protected Builder(ServiceOptions options) { projectId = options.projectId; host = options.host; credentials = options.credentials; - retryParams = options.retryParams; + retrySettings = options.retrySettings; serviceFactory = options.serviceFactory; serviceRpcFactory = options.serviceRpcFactory; clock = options.clock; @@ -146,13 +156,13 @@ public B setServiceFactory(ServiceFactory serviceFactory) { /** - * Sets the service's clock. The clock is mainly used for testing purpose. {@link Clock} will be - * replaced by Java8's {@code java.time.Clock}. + * Sets the service's clock. The clock is mainly used for testing purpose. {@link NanoClock} + * will be replaced by Java8's {@code java.time.Clock}. * * @param clock the clock to set * @return the builder */ - public B setClock(Clock clock) { + public B setClock(NanoClock clock) { this.clock = clock; return self(); } @@ -189,7 +199,7 @@ public B setHost(String host) { * @param credentials authentication credentials, should not be {@code null} * @return the builder * @throws NullPointerException if {@code credentials} is {@code null}. To disable - * authentication use {@link NoCredentials#getInstance()} + * authentication use {@link NoCredentials#getInstance()} */ public B setCredentials(Credentials credentials) { this.credentials = checkNotNull(credentials); @@ -198,14 +208,12 @@ public B setCredentials(Credentials credentials) { /** - * Sets configuration parameters for request retries. If no configuration is set - * {@link RetryParams#getDefaultInstance()} is used. To disable retries, supply - * {@link RetryParams#noRetries()} here. + * Sets configuration parameters for request retries. * * @return the builder */ - public B setRetryParams(RetryParams retryParams) { - this.retryParams = retryParams; + public B setRetrySettings(RetrySettings retrySettings) { + this.retrySettings = retrySettings; return self(); } @@ -229,18 +237,18 @@ protected ServiceOptions(Class> ser checkArgument( projectId != null, "A project ID is required for this service but could not be determined from the builder " - + "or the environment. Please set a project ID using the builder."); + + "or the environment. Please set a project ID using the builder."); } host = firstNonNull(builder.host, getDefaultHost()); credentials = builder.credentials != null ? builder.credentials : defaultCredentials(); - retryParams = firstNonNull(builder.retryParams, defaultRetryParams()); + retrySettings = firstNonNull(builder.retrySettings, getDefaultRetrySettings()); serviceFactory = firstNonNull(builder.serviceFactory, getFromServiceLoader(serviceFactoryClass, getDefaultServiceFactory())); serviceFactoryClassName = serviceFactory.getClass().getName(); serviceRpcFactory = firstNonNull(builder.serviceRpcFactory, getFromServiceLoader(rpcFactoryClass, getDefaultRpcFactory())); serviceRpcFactoryClassName = serviceRpcFactory.getClass().getName(); - clock = firstNonNull(builder.clock, Clock.defaultClock()); + clock = firstNonNull(builder.clock, SystemClock.getDefaultClock()); } /** @@ -275,12 +283,12 @@ protected String getDefaultProject() { * Returns the default project ID, or {@code null} if no default project ID could be found. This * method returns the first available project ID among the following sources: *
    - *
  1. The project ID specified by the GOOGLE_CLOUD_PROJECT environment variable - *
  2. The App Engine project ID - *
  3. The project ID specified in the JSON credentials file pointed by the - * {@code GOOGLE_APPLICATION_CREDENTIALS} environment variable - *
  4. The Google Cloud SDK project ID - *
  5. The Compute Engine project ID + *
  6. The project ID specified by the GOOGLE_CLOUD_PROJECT environment variable + *
  7. The App Engine project ID + *
  8. The project ID specified in the JSON credentials file pointed by the + * {@code GOOGLE_APPLICATION_CREDENTIALS} environment variable + *
  9. The Google Cloud SDK project ID + *
  10. The Compute Engine project ID *
*/ public static String getDefaultProjectId() { @@ -470,11 +478,10 @@ public Credentials getScopedCredentials() { /** - * Returns configuration parameters for request retries. By default requests are retried: - * {@link RetryParams#getDefaultInstance()} is used. + * Returns configuration parameters for request retries. */ - public RetryParams getRetryParams() { - return retryParams; + public RetrySettings getRetrySettings() { + return retrySettings; } @@ -482,7 +489,7 @@ public RetryParams getRetryParams() { * Returns the service's clock. Default time source uses {@link System#currentTimeMillis()} to get * current time. */ - public Clock getClock() { + public NanoClock getClock() { return clock; } @@ -524,7 +531,7 @@ public String getLibraryVersion() { } protected int baseHashCode() { - return Objects.hash(projectId, host, credentials, retryParams, serviceFactoryClassName, + return Objects.hash(projectId, host, credentials, retrySettings, serviceFactoryClassName, serviceRpcFactoryClassName, clock); } @@ -532,7 +539,7 @@ protected boolean baseEquals(ServiceOptions other) { return Objects.equals(projectId, other.projectId) && Objects.equals(host, other.host) && Objects.equals(credentials, other.credentials) - && Objects.equals(retryParams, other.retryParams) + && Objects.equals(retrySettings, other.retrySettings) && Objects.equals(serviceFactoryClassName, other.serviceFactoryClassName) && Objects.equals(serviceRpcFactoryClassName, other.serviceRpcFactoryClassName) && Objects.equals(clock, clock); @@ -553,6 +560,25 @@ static T newInstance(String className) throws IOException, ClassNotFoundExce } } + public static RetrySettings getDefaultRetrySettings() { + return DEFAULT_RETRY_SETTINGS; + } + + public static RetrySettings getNoRetrySettings() { + return NO_RETRY_SETTINGS; + } + + private static RetrySettings.Builder getDefaultRetrySettingsBuilder() { + return RetrySettings.newBuilder() + .setMaxAttempts(6) + .setInitialRetryDelay(Duration.millis(1000L)) + .setMaxRetryDelay(Duration.millis(32_000L)) + .setRetryDelayMultiplier(2.0) + .setTotalTimeout(Duration.millis(50_000L)) + .setInitialRpcTimeout(Duration.millis(50_000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(50_000L)); + } protected abstract ServiceFactory getDefaultServiceFactory(); @@ -567,10 +593,10 @@ static T newInstance(String className) throws IOException, ClassNotFoundExce /** * Some services may have different backoff requirements listed in their SLAs. Be sure to override * this method in options subclasses when the service's backoff requirement differs from the - * default parameters listed in {@link RetryParams}. + * default parameters listed in {@link RetrySettings}. */ - protected RetryParams defaultRetryParams() { - return RetryParams.getDefaultInstance(); + protected RetrySettings defaultRetrySettings() { + return getDefaultRetrySettings(); } static T getFromServiceLoader(Class clazz, T defaultInstance) { diff --git a/google-cloud-core/src/test/java/com/google/cloud/BaseServiceExceptionTest.java b/google-cloud-core/src/test/java/com/google/cloud/BaseServiceExceptionTest.java index 9ac4b4b9894d..7992f9e803d5 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/BaseServiceExceptionTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/BaseServiceExceptionTest.java @@ -158,7 +158,7 @@ public void testTranslateAndThrow() throws Exception { expect(exceptionMock.getCause()).andReturn(cause).times(2); replay(exceptionMock); try { - BaseServiceException.translateAndPropagateIfPossible(exceptionMock); + BaseServiceException.translate(exceptionMock); } catch (BaseServiceException ex) { assertEquals(CODE, ex.getCode()); assertEquals(MESSAGE, ex.getMessage()); diff --git a/google-cloud-core/src/test/java/com/google/cloud/RetryHelperTest.java b/google-cloud-core/src/test/java/com/google/cloud/RetryHelperTest.java deleted file mode 100644 index 68bb7ce35316..000000000000 --- a/google-cloud-core/src/test/java/com/google/cloud/RetryHelperTest.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - * Copyright 2015 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud; - -import static java.util.concurrent.Executors.callable; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import com.google.cloud.RetryHelper.NonRetriableException; -import com.google.cloud.RetryHelper.RetriesExhaustedException; - -import org.junit.Test; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Iterator; -import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; - -/** - * Tests for {@link RetryHelper}. - */ -public class RetryHelperTest { - - static class E1Exception extends Exception { - private static final long serialVersionUID = 3874933713392137001L; - } - - static class E2Exception extends E1Exception { - private static final long serialVersionUID = -8710227162480133598L; - } - - static class E3Exception extends E1Exception { - private static final long serialVersionUID = -7794256022024001666L; - } - - static class E4Exception extends E2Exception { - private static final long serialVersionUID = -5508018234693709156L; - } - - @Test - public void testTriesWithExceptionHandling() { - assertNull(RetryHelper.getContext()); - RetryParams params = - RetryParams.newBuilder().setInitialRetryDelayMillis(0).setRetryMaxAttempts(3).build(); - ExceptionHandler handler = ExceptionHandler.newBuilder() - .retryOn(IOException.class).abortOn(RuntimeException.class).build(); - final AtomicInteger count = new AtomicInteger(3); - try { - RetryHelper.runWithRetries(new Callable() { - @Override public Void call() throws IOException, NullPointerException { - if (count.decrementAndGet() == 2) { - assertEquals(1, RetryHelper.getContext().getAttemptNumber()); - throw new IOException("should be retried"); - } - assertEquals(2, RetryHelper.getContext().getAttemptNumber()); - throw new NullPointerException("Boo!"); - } - }, params, handler); - fail("Exception should have been thrown"); - } catch (NonRetriableException ex) { - assertEquals("Boo!", ex.getCause().getMessage()); - assertEquals(1, count.intValue()); - } - assertNull(RetryHelper.getContext()); - - params = RetryParams.newBuilder().setInitialRetryDelayMillis(0).setRetryMaxAttempts(5).build(); - handler = ExceptionHandler.newBuilder() - .retryOn(E1Exception.class, E4Exception.class) - .abortOn(E3Exception.class).build(); - final Iterator exceptions = Arrays.asList( - new E1Exception(), new E2Exception(), new E4Exception(), new E3Exception()).iterator(); - try { - RetryHelper.runWithRetries(new Callable() { - @Override public Void call() throws E1Exception { - throw exceptions.next(); - } - }, params, handler); - fail("Exception should have been thrown"); - } catch (NonRetriableException ex) { - assertTrue(ex.getCause() instanceof E3Exception); - } - assertNull(RetryHelper.getContext()); - } - - @Test - public void testTriesAtLeastMinTimes() { - // Total retry period set to 60 seconds so as to not factor into test - RetryParams params = RetryParams.newBuilder().setInitialRetryDelayMillis(0) - .setTotalRetryPeriodMillis(60000) - .setRetryMinAttempts(5) - .setRetryMaxAttempts(10) - .build(); - final int timesToFail = 7; - assertNull(RetryHelper.getContext()); - int attempted = RetryHelper.runWithRetries(new Callable() { - int timesCalled; - @Override public Integer call() throws IOException { - timesCalled++; - assertEquals(timesCalled, RetryHelper.getContext().getAttemptNumber()); - assertEquals(10, RetryHelper.getContext().getRetryParams().getRetryMaxAttempts()); - if (timesCalled <= timesToFail) { - throw new IOException(); - } - return timesCalled; - } - }, params, ExceptionHandler.getDefaultInstance()); - assertEquals(timesToFail + 1, attempted); - assertNull(RetryHelper.getContext()); - } - - - @Test - public void testTriesNoMoreThanMaxTimes() { - // Total retry period set to 60 seconds so as to not factor into test - final int maxAttempts = 10; - RetryParams params = RetryParams.newBuilder().setInitialRetryDelayMillis(0) - .setTotalRetryPeriodMillis(60000) - .setRetryMinAttempts(0) - .setRetryMaxAttempts(maxAttempts) - .build(); - final AtomicInteger timesCalled = new AtomicInteger(0); - try { - RetryHelper.runWithRetries(callable(new Runnable() { - @Override public void run() { - // Throw an exception up to maxAttempts times, should never be called beyond that - if (timesCalled.incrementAndGet() <= maxAttempts) { - throw new RuntimeException(); - } - fail("Body was executed too many times: " + timesCalled.get()); - } - }), params, ExceptionHandler.newBuilder().retryOn(RuntimeException.class).build()); - // Unnecessary as this line should not be possible reach even if RetryHandler is broken - fail("Should not have succeeded, expected all attempts to fail and give up."); - } catch (RetriesExhaustedException expected) { - // Expect the body to run exactly maxAttempts times - assertEquals(maxAttempts, timesCalled.get()); - } - } - - - private static class FakeClock extends Clock { - - private final AtomicLong millis = new AtomicLong(); - - // Advances the clock value by {@code time} in {@code timeUnit}. - void advance(long time, TimeUnit timeUnit) { - millis.addAndGet(timeUnit.toMillis(time)); - } - - @Override - public long millis() { - return millis.get(); - } - } - - @Test - public void testTriesNoMoreLongerThanTotalRetryPeriod() { - final FakeClock fakeClock = new FakeClock(); - // The 8th attempt (after min and before max) will trigger a 1 second (virtual) delay exceeding - // total retry period which is set just under 1 second. Test occurs faster than realtime. - RetryParams params = RetryParams.newBuilder().setInitialRetryDelayMillis(0) - .setTotalRetryPeriodMillis(999) - .setRetryMinAttempts(5) - .setRetryMaxAttempts(10) - .build(); - ExceptionHandler handler = ExceptionHandler.newBuilder().retryOn(RuntimeException.class).build(); - final int sleepOnAttempt = 8; - final AtomicInteger timesCalled = new AtomicInteger(0); - try { - RetryHelper.runWithRetries(callable(new Runnable() { - @Override public void run() { - timesCalled.incrementAndGet(); - if (timesCalled.get() == sleepOnAttempt) { - fakeClock.advance(1000, TimeUnit.MILLISECONDS); - } - throw new RuntimeException(); - } - }), params, handler, fakeClock); - fail(); - } catch (RetriesExhaustedException expected) { - // verify timesCalled - assertEquals(sleepOnAttempt, timesCalled.get()); - } - } - - @Test - public void testBackoffIsExponential() { - // Total retry period set to 60 seconds so as to not factor into test - RetryParams params = RetryParams.newBuilder() - .setInitialRetryDelayMillis(10) - .setMaxRetryDelayMillis(10_000_000) - .setRetryDelayBackoffFactor(2) - .setTotalRetryPeriodMillis(60_000) - .setRetryMinAttempts(0) - .setRetryMaxAttempts(100) - .build(); - long sleepDuration = RetryHelper.getSleepDuration(params, 1); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 13 && sleepDuration >= 7); - sleepDuration = RetryHelper.getSleepDuration(params, 2); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 25 && sleepDuration >= 15); - sleepDuration = RetryHelper.getSleepDuration(params, 3); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 50 && sleepDuration >= 30); - sleepDuration = RetryHelper.getSleepDuration(params, 4); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 100 && sleepDuration >= 60); - sleepDuration = RetryHelper.getSleepDuration(params, 5); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 200 && sleepDuration >= 120); - sleepDuration = RetryHelper.getSleepDuration(params, 6); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 400 && sleepDuration >= 240); - sleepDuration = RetryHelper.getSleepDuration(params, 7); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 800 && sleepDuration >= 480); - sleepDuration = RetryHelper.getSleepDuration(params, 8); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 1600 && sleepDuration >= 960); - sleepDuration = RetryHelper.getSleepDuration(params, 9); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 3200 && sleepDuration >= 1920); - sleepDuration = RetryHelper.getSleepDuration(params, 10); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 6400 && sleepDuration >= 3840); - sleepDuration = RetryHelper.getSleepDuration(params, 11); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 12800 && sleepDuration >= 7680); - sleepDuration = RetryHelper.getSleepDuration(params, 12); - assertTrue(String.valueOf(sleepDuration), sleepDuration < 25600 && sleepDuration >= 15360); - } - - @Test - public void testNestedUsage() { - assertEquals((1 + 3) * 2, invokeNested(3, 2)); - } - - private int invokeNested(final int level, final int retries) { - if (level < 0) { - return 0; - } - return RetryHelper.runWithRetries(new Callable() { - @Override - public Integer call() throws IOException { - if (RetryHelper.getContext().getAttemptNumber() < retries) { - throw new IOException(); - } - assertEquals(retries, RetryHelper.getContext().getAttemptNumber()); - return invokeNested(level - 1, retries) + RetryHelper.getContext().getAttemptNumber(); - } - }); - } - -} diff --git a/google-cloud-core/src/test/java/com/google/cloud/RetryParamsTest.java b/google-cloud-core/src/test/java/com/google/cloud/RetryParamsTest.java deleted file mode 100644 index 5f822fe7ae75..000000000000 --- a/google-cloud-core/src/test/java/com/google/cloud/RetryParamsTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2015 Google Inc. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.cloud; - -import static com.google.cloud.RetryParams.DEFAULT_INITIAL_RETRY_DELAY_MILLIS; -import static com.google.cloud.RetryParams.DEFAULT_MAX_RETRY_DELAY_MILLIS; -import static com.google.cloud.RetryParams.DEFAULT_RETRY_DELAY_BACKOFF_FACTOR; -import static com.google.cloud.RetryParams.DEFAULT_RETRY_MAX_ATTEMPTS; -import static com.google.cloud.RetryParams.DEFAULT_RETRY_MIN_ATTEMPTS; -import static com.google.cloud.RetryParams.DEFAULT_TOTAL_RETRY_PERIOD_MILLIS; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import com.google.cloud.RetryParams.Builder; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import java.util.Arrays; - -/** - * Tests for {@link RetryParams}. - */ -@RunWith(JUnit4.class) -public class RetryParamsTest { - - @Test - public void testDefaults() { - RetryParams params1 = RetryParams.getDefaultInstance(); - RetryParams params2 = RetryParams.newBuilder().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()); - } - } - - - @Test - public void testSetAndCopy() { - RetryParams.Builder builder = RetryParams.newBuilder(); - builder.setInitialRetryDelayMillis(101); - builder.setMaxRetryDelayMillis(102); - builder.setRetryDelayBackoffFactor(103); - builder.setRetryMinAttempts(107); - builder.setRetryMaxAttempts(108); - builder.setTotalRetryPeriodMillis(109); - 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()); - } - } - - - @Test - public void testBadSettings() { - RetryParams.Builder builder = RetryParams.newBuilder(); - builder.setInitialRetryDelayMillis(-1); - builder = assertFailure(builder); - builder.setMaxRetryDelayMillis( - RetryParams.getDefaultInstance().getInitialRetryDelayMillis() - 1); - builder = assertFailure(builder); - builder.setRetryDelayBackoffFactor(-1); - builder = assertFailure(builder); - builder.setRetryMinAttempts(-1); - builder = assertFailure(builder); - builder.setRetryMaxAttempts(RetryParams.getDefaultInstance().getRetryMinAttempts() - 1); - builder = assertFailure(builder); - builder.setTotalRetryPeriodMillis(-1); - builder = assertFailure(builder); - // verify that it is OK for min and max to be equal - builder.setRetryMaxAttempts(RetryParams.getDefaultInstance().getRetryMinAttempts()); - builder.setMaxRetryDelayMillis(RetryParams.getDefaultInstance().getInitialRetryDelayMillis()); - builder.build(); - } - - private static Builder assertFailure(Builder builder) { - try { - builder.build(); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException ex) { - // expected - } - return RetryParams.newBuilder(); - } -} diff --git a/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java b/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java index d2e2de4f4afd..70989b839bbe 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java @@ -16,6 +16,7 @@ package com.google.cloud; +import com.google.api.gax.core.RetrySettings; import com.google.cloud.MonitoredResourceDescriptor.LabelDescriptor; import com.google.cloud.MonitoredResourceDescriptor.LabelDescriptor.ValueType; import com.google.common.collect.ImmutableList; @@ -32,7 +33,7 @@ public class SerializationTest extends BaseSerializationTest { private static final Identity IDENTITY = Identity.allAuthenticatedUsers(); private static final PageImpl PAGE = new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2")); - private static final RetryParams RETRY_PARAMS = RetryParams.getDefaultInstance(); + private static final RetrySettings RETRY_SETTINGS = ServiceOptions.getDefaultRetrySettings(); private static final Role SOME_ROLE = Role.viewer(); private static final Policy SOME_IAM_POLICY = Policy.newBuilder().build(); private static final WaitForOption CHECKING_PERIOD = @@ -43,41 +44,53 @@ public class SerializationTest extends BaseSerializationTest { MonitoredResourceDescriptor.newBuilder("global") .setLabels(ImmutableList.of(LABEL_DESCRIPTOR)) .build(); - private static final MonitoredResource MONITORED_RESOURCE = MonitoredResource.newBuilder("global") - .setLabels(ImmutableMap.of("project_id", "project")) - .build(); - private static final String JSON_KEY = "{\n" - + " \"private_key_id\": \"somekeyid\",\n" - + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" - + "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" - + "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" - + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" - + "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" - + "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" - + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" - + "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" - + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" - + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" - + "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" - + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" - + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" - + "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" - + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" - + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" - + "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" - + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" - + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" - + "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" - + " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n" - + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" - + " \"type\": \"service_account\"\n" - + "}"; + private static final MonitoredResource MONITORED_RESOURCE = + MonitoredResource.newBuilder("global") + .setLabels(ImmutableMap.of("project_id", "project")) + .build(); + private static final String JSON_KEY = + "{\n" + + " \"private_key_id\": \"somekeyid\",\n" + + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" + + "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" + + "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" + + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" + + "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" + + "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" + + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" + + "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" + + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" + + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" + + "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" + + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" + + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" + + "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" + + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" + + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" + + "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" + + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" + + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" + + "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" + + " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n" + + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" + + " \"type\": \"service_account\"\n" + + "}"; @Override protected Serializable[] serializableObjects() { - return new Serializable[]{BASE_SERVICE_EXCEPTION, EXCEPTION_HANDLER, IDENTITY, PAGE, - RETRY_PARAMS, SOME_ROLE, SOME_IAM_POLICY, CHECKING_PERIOD, LABEL_DESCRIPTOR, - MONITORED_RESOURCE_DESCRIPTOR, MONITORED_RESOURCE}; + return new Serializable[] { + BASE_SERVICE_EXCEPTION, + EXCEPTION_HANDLER, + IDENTITY, + PAGE, + RETRY_SETTINGS, + SOME_ROLE, + SOME_IAM_POLICY, + CHECKING_PERIOD, + LABEL_DESCRIPTOR, + MONITORED_RESOURCE_DESCRIPTOR, + MONITORED_RESOURCE + }; } @Override diff --git a/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java b/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java index e08b5c35ce69..5199937dabdf 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java @@ -22,6 +22,8 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.SystemClock; import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.spi.ServiceRpcFactory; @@ -38,33 +40,34 @@ public class ServiceOptionsTest { private static final String JSON_KEY = "{\n" - + " \"private_key_id\": \"somekeyid\",\n" - + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" - + "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" - + "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" - + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" - + "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" - + "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" - + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" - + "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" - + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" - + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" - + "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" - + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" - + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" - + "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" - + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" - + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" - + "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" - + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" - + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" - + "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" - + " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n" - + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" - + " \"type\": \"service_account\"\n" - + "}"; + + " \"private_key_id\": \"somekeyid\",\n" + + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" + + "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" + + "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" + + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" + + "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" + + "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" + + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" + + "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" + + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" + + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" + + "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" + + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" + + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" + + "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" + + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" + + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" + + "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" + + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" + + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" + + "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" + + " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n" + + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" + + " \"type\": \"service_account\"\n" + + "}"; private static final InputStream JSON_KEY_STREAM = new ByteArrayInputStream(JSON_KEY.getBytes()); private static GoogleCredentials credentials; + static { try { credentials = GoogleCredentials.fromStream(JSON_KEY_STREAM); @@ -72,14 +75,15 @@ public class ServiceOptionsTest { fail("Couldn't create fake JSON credentials."); } } - private static final Clock TEST_CLOCK = new TestClock(); + + private static final NanoClock TEST_CLOCK = new TestClock(); private static final TestServiceOptions OPTIONS = TestServiceOptions.newBuilder() .setCredentials(credentials) .setClock(TEST_CLOCK) .setHost("host") .setProjectId("project-id") - .setRetryParams(RetryParams.noRetries()) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) .build(); private static final TestServiceOptions OPTIONS_NO_CREDENTIALS = TestServiceOptions.newBuilder() @@ -87,7 +91,7 @@ public class ServiceOptionsTest { .setClock(TEST_CLOCK) .setHost("host") .setProjectId("project-id") - .setRetryParams(RetryParams.noRetries()) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) .build(); private static final TestServiceOptions DEFAULT_OPTIONS = TestServiceOptions.newBuilder().setProjectId("project-id").build(); @@ -96,20 +100,24 @@ public class ServiceOptionsTest { private static final Pattern APPLICATION_NAME_PATTERN = Pattern.compile(LIBRARY_NAME + "(/[0-9]+.[0-9]+.[0-9]+)?"); - @Rule - public ExpectedException thrown = ExpectedException.none(); + @Rule public ExpectedException thrown = ExpectedException.none(); + + private static class TestClock implements NanoClock { + @Override + public long nanoTime() { + return 123_456_789_000_000L; + } - private static class TestClock extends Clock { @Override - public long millis() { - return 123456789L; + public long millisTime() { + return 123_456_789L; } } private interface TestService extends Service {} - private static class TestServiceImpl - extends BaseService implements TestService { + private static class TestServiceImpl extends BaseService + implements TestService { private TestServiceImpl(TestServiceOptions options) { super(options); } @@ -205,10 +213,11 @@ public void testBuilder() { assertSame(TEST_CLOCK, OPTIONS.getClock()); assertEquals("host", OPTIONS.getHost()); assertEquals("project-id", OPTIONS.getProjectId()); - assertSame(RetryParams.noRetries(), OPTIONS.getRetryParams()); - assertSame(Clock.defaultClock(), DEFAULT_OPTIONS.getClock()); + assertSame(ServiceOptions.getNoRetrySettings(), + OPTIONS.getRetrySettings()); + assertSame(SystemClock.getDefaultClock(), DEFAULT_OPTIONS.getClock()); assertEquals("https://www.googleapis.com", DEFAULT_OPTIONS.getHost()); - assertSame(RetryParams.getDefaultInstance(), DEFAULT_OPTIONS.getRetryParams()); + assertSame(ServiceOptions.getDefaultRetrySettings(), DEFAULT_OPTIONS.getRetrySettings()); } @Test @@ -217,7 +226,7 @@ public void testBuilderNoCredentials() { assertSame(TEST_CLOCK, OPTIONS_NO_CREDENTIALS.getClock()); assertEquals("host", OPTIONS_NO_CREDENTIALS.getHost()); assertEquals("project-id", OPTIONS_NO_CREDENTIALS.getProjectId()); - assertSame(RetryParams.noRetries(), OPTIONS_NO_CREDENTIALS.getRetryParams()); + assertSame(ServiceOptions.getNoRetrySettings(), OPTIONS_NO_CREDENTIALS.getRetrySettings()); } @Test diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java index 45975730fc04..739d87ec002e 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreException.java @@ -18,7 +18,6 @@ import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; -import com.google.cloud.RetryHelper.RetryInterruptedException; import com.google.common.collect.ImmutableSet; import java.io.IOException; @@ -66,10 +65,9 @@ protected Set getRetryableErrors() { * will always throw an exception. * * @throws DatastoreException when {@code ex} was caused by a {@code DatastoreException} - * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ static DatastoreException translateAndThrow(RetryHelperException ex) { - BaseServiceException.translateAndPropagateIfPossible(ex); + BaseServiceException.translate(ex); throw new DatastoreException(UNKNOWN_CODE, ex.getMessage(), null, ex.getCause()); } diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreImpl.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreImpl.java index 542b8aa7b853..e1ce65625ca0 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreImpl.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreImpl.java @@ -19,7 +19,8 @@ import com.google.cloud.BaseService; import com.google.cloud.RetryHelper; import com.google.cloud.RetryHelper.RetryHelperException; -import com.google.cloud.RetryParams; +import com.google.api.gax.core.RetrySettings; +import com.google.cloud.ServiceOptions; import com.google.cloud.datastore.ReadOption.EventualConsistency; import com.google.cloud.datastore.spi.DatastoreRpc; import com.google.common.base.MoreObjects; @@ -45,12 +46,13 @@ final class DatastoreImpl extends BaseService implements Datastore { private final DatastoreRpc datastoreRpc; - private final RetryParams retryParams; + private final RetrySettings retrySettings; DatastoreImpl(DatastoreOptions options) { super(options); this.datastoreRpc = options.getRpc(); - retryParams = MoreObjects.firstNonNull(options.getRetryParams(), RetryParams.noRetries()); + retrySettings = MoreObjects + .firstNonNull(options.getRetrySettings(), ServiceOptions.getNoRetrySettings()); } @Override @@ -92,7 +94,7 @@ public com.google.datastore.v1.RunQueryResponse call() throws DatastoreException { return datastoreRpc.runQuery(requestPb); } - }, retryParams, EXCEPTION_HANDLER, getOptions().getClock()); + }, retrySettings, EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw DatastoreException.translateAndThrow(e); } @@ -131,7 +133,7 @@ public com.google.datastore.v1.AllocateIdsResponse call() throws DatastoreException { return datastoreRpc.allocateIds(requestPb); } - }, retryParams, EXCEPTION_HANDLER, getOptions().getClock()); + }, retrySettings, EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw DatastoreException.translateAndThrow(e); } @@ -165,7 +167,7 @@ public List add(FullEntity... entities) { if (completeEntity != null) { if (completeEntities.put(completeEntity.getKey(), completeEntity) != null) { throw DatastoreException.throwInvalidRequest( - "Duplicate entity with the key %s", entity.getKey()); + "Duplicate entity with the key %s", entity.getKey()); } } else { Preconditions.checkArgument(entity.hasKey(), "Entity %s is missing a key", entity); @@ -288,7 +290,7 @@ public com.google.datastore.v1.LookupResponse call() throws DatastoreException { return datastoreRpc.lookup(requestPb); } - }, retryParams, EXCEPTION_HANDLER, getOptions().getClock()); + }, retrySettings, EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw DatastoreException.translateAndThrow(e); } @@ -390,7 +392,7 @@ public com.google.datastore.v1.CommitResponse call() throws DatastoreException { return datastoreRpc.commit(requestPb); } }, - retryParams, + retrySettings, EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw DatastoreException.translateAndThrow(e); @@ -413,7 +415,7 @@ public com.google.datastore.v1.BeginTransactionResponse call() return datastoreRpc.beginTransaction(requestPb); } }, - retryParams, + retrySettings, EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw DatastoreException.translateAndThrow(e); @@ -430,11 +432,12 @@ void rollbackTransaction(ByteString transaction) { void rollback(final com.google.datastore.v1.RollbackRequest requestPb) { try { RetryHelper.runWithRetries(new Callable() { - @Override public Void call() throws DatastoreException { + @Override + public Void call() throws DatastoreException { datastoreRpc.rollback(requestPb); return null; } - }, retryParams, EXCEPTION_HANDLER, getOptions().getClock()); + }, retrySettings, EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw DatastoreException.translateAndThrow(e); } diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java index 21ec57b84ab7..d486ff250999 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/LocalDatastoreHelper.java @@ -17,7 +17,7 @@ package com.google.cloud.datastore.testing; import com.google.cloud.NoCredentials; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.cloud.datastore.DatastoreOptions; import com.google.cloud.testing.BaseEmulatorHelper; import com.google.common.collect.ImmutableList; @@ -124,7 +124,7 @@ private DatastoreOptions.Builder optionsBuilder() { .setProjectId(getProjectId()) .setHost(DEFAULT_HOST + ":" + Integer.toString(getPort())) .setCredentials(NoCredentials.getInstance()) - .setRetryParams(RetryParams.noRetries()); + .setRetrySettings(ServiceOptions.getNoRetrySettings()); } diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java index 144af9c17f88..c6a1fb6517e8 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/testing/RemoteDatastoreHelper.java @@ -16,7 +16,7 @@ package com.google.cloud.datastore.testing; -import com.google.cloud.RetryParams; +import com.google.api.gax.core.RetrySettings; import com.google.cloud.datastore.Datastore; import com.google.cloud.datastore.DatastoreOptions; import com.google.cloud.datastore.Key; @@ -25,18 +25,19 @@ import com.google.cloud.datastore.StructuredQuery; import java.util.UUID; +import org.joda.time.Duration; /** * Utility to create a remote datastore configuration for testing. Datastore options can be obtained * via the {@link #getOptions()} method. Returned options use a randomly generated namespace * ({@link DatastoreOptions#getNamespace()}) that can be used to run the tests. Once the tests are * run, all entities in the namespace can be deleted using {@link #deleteNamespace()}. Returned - * options also have custom {@link DatastoreOptions#getRetryParams()}: - * {@link RetryParams#getRetryMaxAttempts()} is {@code 10}, - * {@link RetryParams#getRetryMinAttempts()} is {@code 6}, - * {@link RetryParams#getMaxRetryDelayMillis()} is {@code 30000}, - * {@link RetryParams#getTotalRetryPeriodMillis()} is {@code 120000} and - * {@link RetryParams#getInitialRetryDelayMillis()} is {@code 250}. + * options also have custom {@link DatastoreOptions#getRetrySettings()}: + * {@link RetrySettings#getMaxAttempts()} is {@code 10}, + * {@link RetrySettings#getRetryAttempts()} is {@code 6}, + * {@link RetrySettings#getMaxRetryDelay()} is {@code 30000}, + * {@link RetrySettings#getTotalTimeout()} is {@code 120000} and + * {@link RetrySettings#getInitialRetryDelay()} is {@code 250}. * {@link DatastoreOptions#getConnectTimeout()} and {@link DatastoreOptions#getReadTimeout()} are * both set to {@code 60000}. */ @@ -78,20 +79,22 @@ public void deleteNamespace() { public static RemoteDatastoreHelper create() { DatastoreOptions datastoreOption = DatastoreOptions.newBuilder() .setNamespace(UUID.randomUUID().toString()) - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setConnectTimeout(60000) .setReadTimeout(60000) .build(); return new RemoteDatastoreHelper(datastoreOption); } - private static RetryParams retryParams() { - return RetryParams.newBuilder() - .setRetryMaxAttempts(10) - .setRetryMinAttempts(6) - .setMaxRetryDelayMillis(30000) - .setTotalRetryPeriodMillis(120000) - .setInitialRetryDelayMillis(250) + private static RetrySettings retryParams() { + return RetrySettings.newBuilder().setMaxAttempts(10) + .setMaxRetryDelay(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(120000L)) + .setInitialRetryDelay(Duration.millis(250L)) + .setRetryDelayMultiplier(1.0) + .setInitialRpcTimeout(Duration.millis(120000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(120000L)) .build(); } } diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java index 2bda0f3dc8dd..4149281981b2 100644 --- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java +++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreOptionsTest.java @@ -77,7 +77,7 @@ public void testToBuilder() throws Exception { assertEquals(original.getProjectId(), copy.getProjectId()); assertEquals(original.getNamespace(), copy.getNamespace()); assertEquals(original.getHost(), copy.getHost()); - assertEquals(original.getRetryParams(), copy.getRetryParams()); + assertEquals(original.getRetrySettings(), copy.getRetrySettings()); assertEquals(original.getCredentials(), copy.getCredentials()); } } diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java index e741d959d995..55d21f37688c 100644 --- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java +++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/DatastoreTest.java @@ -25,7 +25,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.cloud.datastore.Query.ResultType; import com.google.cloud.datastore.StructuredQuery.OrderBy; import com.google.cloud.datastore.StructuredQuery.PropertyFilter; @@ -141,7 +141,7 @@ public void setUp() { rpcMock = EasyMock.createStrictMock(DatastoreRpc.class); rpcMockOptions = options .toBuilder() - .setRetryParams(RetryParams.getDefaultInstance()) + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .setServiceRpcFactory(rpcFactoryMock) .build(); EasyMock.expect(rpcFactoryMock.create(rpcMockOptions)).andReturn(rpcMock); diff --git a/google-cloud-dns/src/main/java/com/google/cloud/dns/DnsException.java b/google-cloud-dns/src/main/java/com/google/cloud/dns/DnsException.java index c30a841fb454..8a06c8b98529 100644 --- a/google-cloud-dns/src/main/java/com/google/cloud/dns/DnsException.java +++ b/google-cloud-dns/src/main/java/com/google/cloud/dns/DnsException.java @@ -19,7 +19,6 @@ import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; -import com.google.cloud.RetryHelper.RetryInterruptedException; import com.google.common.collect.ImmutableSet; import java.io.IOException; @@ -62,10 +61,9 @@ protected Set getRetryableErrors() { * always throw an exception. * * @throws DnsException when {@code ex} was caused by a {@code DnsException} - * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ static DnsException translateAndThrow(RetryHelperException ex) { - BaseServiceException.translateAndPropagateIfPossible(ex); + BaseServiceException.translate(ex); throw new DnsException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } } diff --git a/google-cloud-dns/src/main/java/com/google/cloud/dns/DnsImpl.java b/google-cloud-dns/src/main/java/com/google/cloud/dns/DnsImpl.java index aadcb0085b83..1db4fd6b4f09 100644 --- a/google-cloud-dns/src/main/java/com/google/cloud/dns/DnsImpl.java +++ b/google-cloud-dns/src/main/java/com/google/cloud/dns/DnsImpl.java @@ -138,7 +138,7 @@ private static Page listZones(final DnsOptions serviceOptions, public DnsRpc.ListResult call() { return rpc.listZones(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.pageToken(); // transform that list into zone objects Iterable zones = result.results() == null ? ImmutableList.of() @@ -166,7 +166,7 @@ private static Page listChangeRequests(final String zoneName, public DnsRpc.ListResult call() { return rpc.listChangeRequests(zoneName, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.pageToken(); // transform that list into change request objects Iterable changes = result.results() == null @@ -196,7 +196,7 @@ private static Page listRecordSets(final String zoneName, public DnsRpc.ListResult call() { return rpc.listRecordSets(zoneName, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.pageToken(); // transform that list into record sets Iterable recordSets = result.results() == null @@ -219,7 +219,7 @@ public Zone create(final ZoneInfo zoneInfo, Dns.ZoneOption... options) { public ManagedZone call() { return dnsRpc.create(zoneInfo.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Zone.fromPb(this, answer); } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); @@ -236,7 +236,7 @@ public Zone getZone(final String zoneName, Dns.ZoneOption... options) { public ManagedZone call() { return dnsRpc.getZone(zoneName, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Zone.fromPb(this, answer); } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); @@ -251,7 +251,7 @@ public boolean delete(final String zoneName) { public Boolean call() { return dnsRpc.deleteZone(zoneName); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); } @@ -267,7 +267,7 @@ public ProjectInfo getProject(Dns.ProjectOption... fields) { public Project call() { return dnsRpc.getProject(optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : ProjectInfo.fromPb(answer); // should never be null } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); @@ -285,7 +285,7 @@ public ChangeRequest applyChangeRequest(final String zoneName, public Change call() { return dnsRpc.applyChangeRequest(zoneName, changeRequest.toPb(), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : ChangeRequest.fromPb(this, zoneName, answer); // not null } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); @@ -303,7 +303,7 @@ public ChangeRequest getChangeRequest(final String zoneName, final String change public Change call() { return dnsRpc.getChangeRequest(zoneName, changeRequestId, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : ChangeRequest.fromPb(this, zoneName, answer); } catch (RetryHelper.RetryHelperException ex) { throw DnsException.translateAndThrow(ex); diff --git a/google-cloud-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java b/google-cloud-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java index 534a939d7c1e..c7f30bb376cf 100644 --- a/google-cloud-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java +++ b/google-cloud-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java @@ -19,12 +19,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import com.google.api.gax.core.NanoClock; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.ResourceRecordSet; -import com.google.cloud.Clock; import com.google.cloud.Page; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.cloud.dns.spi.DnsRpc; import com.google.cloud.dns.spi.DnsRpcFactory; import com.google.common.collect.ImmutableList; @@ -100,10 +100,15 @@ public class DnsImplTest { // Other private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); - private static final Clock TIME_SOURCE = new Clock() { + private static final NanoClock TIME_SOURCE = new NanoClock() { @Override - public long millis() { - return 42000L; + public long nanoTime() { + return 42_000_000_000L; + } + + @Override + public long millisTime() { + return 42_000L; } }; @@ -123,7 +128,7 @@ public void setUp() { .setProjectId("projectId") .setClock(TIME_SOURCE) .setServiceRpcFactory(rpcFactoryMock) - .setRetryParams(RetryParams.noRetries()) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) .build(); } diff --git a/google-cloud-dns/src/test/java/com/google/cloud/dns/SerializationTest.java b/google-cloud-dns/src/test/java/com/google/cloud/dns/SerializationTest.java index 8364f56960bf..3bdc5a9ef832 100644 --- a/google-cloud-dns/src/test/java/com/google/cloud/dns/SerializationTest.java +++ b/google-cloud-dns/src/test/java/com/google/cloud/dns/SerializationTest.java @@ -19,7 +19,7 @@ import com.google.cloud.BaseSerializationTest; import com.google.cloud.NoCredentials; import com.google.cloud.Restorable; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.common.collect.ImmutableList; import java.io.Serializable; @@ -58,7 +58,7 @@ public class SerializationTest extends BaseSerializationTest { Dns.ProjectOption.fields(Dns.ProjectField.QUOTA); private static final DnsOptions OPTIONS = DnsOptions.newBuilder() .setProjectId("some-unnecessary-project-ID") - .setRetryParams(RetryParams.getDefaultInstance()) + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) .build(); private static final Dns DNS = OPTIONS.getService(); private static final Zone FULL_ZONE = new Zone(DNS, new ZoneInfo.BuilderImpl(FULL_ZONE_INFO)); diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/testing/RemoteLoggingHelper.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/testing/RemoteLoggingHelper.java index 4fdd02c4c09c..63f6ede19dc6 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/testing/RemoteLoggingHelper.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/testing/RemoteLoggingHelper.java @@ -17,7 +17,7 @@ package com.google.cloud.logging.testing; import com.google.auth.oauth2.ServiceAccountCredentials; -import com.google.cloud.RetryParams; +import com.google.api.gax.core.RetrySettings; import com.google.cloud.logging.LoggingOptions; import java.io.IOException; @@ -25,13 +25,14 @@ import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; +import org.joda.time.Duration; /** * Utility to create a remote logging configuration for testing. Logging options can be obtained via * the {@link #getOptions()} method. Returned options have custom - * {@link LoggingOptions#getRetryParams()}: {@link RetryParams#getMaxRetryDelayMillis()} is - * {@code 30000}, {@link RetryParams#getTotalRetryPeriodMillis()} is {@code 120000} and - * {@link RetryParams#getInitialRetryDelayMillis()} is {@code 250}. + * {@link LoggingOptions#getRetrySettings()}: {@link RetrySettings#getMaxRetryDelay()} is + * {@code 30000}, {@link RetrySettings#getTotalTimeout()} is {@code 120000} and + * {@link RetrySettings#getInitialRetryDelay()} is {@code 250}. * {@link LoggingOptions#getInitialTimeout()} is set to 60000, * {@link LoggingOptions#getMaxTimeout()} is set to {@code 240000} and * {@link LoggingOptions#getTimeoutMultiplier()} is set to {@code 1.5}. @@ -69,7 +70,7 @@ public static RemoteLoggingHelper create(String projectId, InputStream keyStream LoggingOptions storageOptions = LoggingOptions.newBuilder() .setCredentials(ServiceAccountCredentials.fromStream(keyStream)) .setProjectId(projectId) - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setInitialTimeout(60000) .setMaxTimeout(120000) .setTimeoutMultiplier(1.5) @@ -89,7 +90,7 @@ public static RemoteLoggingHelper create(String projectId, InputStream keyStream */ public static RemoteLoggingHelper create() throws LoggingHelperException { LoggingOptions loggingOptions = LoggingOptions.newBuilder() - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setInitialTimeout(60000) .setMaxTimeout(240000) .setTimeoutMultiplier(1.5) @@ -105,11 +106,15 @@ public static String formatForTest(String name) { return name + "-" + UUID.randomUUID().toString(); } - private static RetryParams retryParams() { - return RetryParams.newBuilder() - .setMaxRetryDelayMillis(30000) - .setTotalRetryPeriodMillis(120000) - .setInitialRetryDelayMillis(250) + private static RetrySettings retryParams() { + return RetrySettings.newBuilder() + .setMaxRetryDelay(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(120000L)) + .setInitialRetryDelay(Duration.millis(250L)) + .setRetryDelayMultiplier(1.0) + .setInitialRpcTimeout(Duration.millis(120000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(120000L)) .build(); } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java index e08570cf76e7..0cf99e59cf13 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingImplTest.java @@ -29,7 +29,7 @@ import com.google.cloud.MonitoredResource; import com.google.cloud.MonitoredResourceDescriptor; import com.google.cloud.Page; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.cloud.logging.Logging.EntryListOption; import com.google.cloud.logging.Logging.ListOption; import com.google.cloud.logging.Logging.SortingField; @@ -147,7 +147,7 @@ public void setUp() { options = LoggingOptions.newBuilder() .setProjectId(PROJECT) .setServiceRpcFactory(rpcFactoryMock) - .setRetryParams(RetryParams.noRetries()) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) .build(); } diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewer.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewer.java index bbd9362fd505..e939f40427e1 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewer.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewer.java @@ -16,7 +16,7 @@ package com.google.cloud.pubsub.deprecated; -import com.google.cloud.Clock; +import com.google.api.gax.core.NanoClock; import com.google.cloud.GrpcServiceOptions.ExecutorFactory; import com.google.common.base.MoreObjects; import com.google.common.collect.LinkedListMultimap; @@ -49,7 +49,7 @@ class AckDeadlineRenewer implements AutoCloseable { private final PubSub pubsub; private final ScheduledExecutorService executor; private final ExecutorFactory executorFactory; - private final Clock clock; + private final NanoClock clock; private final Queue messageQueue; private final Map messageDeadlines; private final Object lock = new Object(); @@ -188,7 +188,7 @@ private void scheduleNextRenewal() { synchronized (futureLock) { if (renewerFuture == null && nextMessage != null) { long delay = - (nextMessage.expectedDeadline() - clock.millis()) - NEXT_RENEWAL_THRESHOLD_MILLIS; + (nextMessage.expectedDeadline() - clock.millisTime()) - NEXT_RENEWAL_THRESHOLD_MILLIS; renewerFuture = executor.schedule(new Runnable() { @Override public void run() { @@ -203,7 +203,7 @@ private void renewAckDeadlines() { ListMultimap messagesToRenewNext = LinkedListMultimap.create(); // At every activation we renew all ack deadlines that will expire in the following // RENEW_THRESHOLD_MILLIS - long threshold = clock.millis() + RENEW_THRESHOLD_MILLIS; + long threshold = clock.millisTime() + RENEW_THRESHOLD_MILLIS; Message message; while ((message = nextMessageToRenew(threshold)) != null) { // If the expected deadline is null the message was removed and we must ignore it, otherwise @@ -254,7 +254,7 @@ private Message nextMessageToRenew(long threshold) { */ void add(String subscription, String ackId) { synchronized (lock) { - long deadline = clock.millis() + MIN_DEADLINE_MILLIS - DEADLINE_SLACK_MILLIS; + long deadline = clock.millisTime() + MIN_DEADLINE_MILLIS - DEADLINE_SLACK_MILLIS; Message message = new Message(new MessageId(subscription, ackId), deadline); messageQueue.add(message); messageDeadlines.put(message.messageId(), deadline); @@ -273,7 +273,7 @@ void add(String subscription, String ackId) { */ void add(String subscription, Iterable ackIds) { synchronized (lock) { - long deadline = clock.millis() + MIN_DEADLINE_MILLIS - DEADLINE_SLACK_MILLIS; + long deadline = clock.millisTime() + MIN_DEADLINE_MILLIS - DEADLINE_SLACK_MILLIS; for (String ackId : ackIds) { Message message = new Message(new MessageId(subscription, ackId), deadline); messageQueue.add(message); diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/testing/LocalPubSubHelper.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/testing/LocalPubSubHelper.java index 46eb3fef7fa9..59131b30f1d2 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/testing/LocalPubSubHelper.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/testing/LocalPubSubHelper.java @@ -17,7 +17,7 @@ package com.google.cloud.pubsub.deprecated.testing; import com.google.cloud.NoCredentials; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.cloud.pubsub.deprecated.PubSubOptions; import com.google.cloud.testing.BaseEmulatorHelper; import com.google.common.collect.ImmutableList; @@ -111,7 +111,7 @@ public PubSubOptions getOptions() { .setProjectId(getProjectId()) .setHost(DEFAULT_HOST + ":" + getPort()) .setCredentials(NoCredentials.getInstance()) - .setRetryParams(RetryParams.noRetries()) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) .build(); } diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/MessageDispatcher.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/MessageDispatcher.java index ce0bd5c8cc49..998691f7c03b 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/MessageDispatcher.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/MessageDispatcher.java @@ -17,8 +17,8 @@ package com.google.cloud.pubsub.spi.v1; import com.google.api.gax.core.FlowController; +import com.google.api.gax.core.NanoClock; import com.google.api.stats.Distribution; -import com.google.cloud.Clock; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; import com.google.common.primitives.Ints; @@ -58,7 +58,7 @@ class MessageDispatcher { private static final int MAX_ACK_DEADLINE_EXTENSION_SECS = 10 * 60; // 10m private final ScheduledExecutorService executor; - private final Clock clock; + private final NanoClock clock; private final Duration ackExpirationPadding; private final MessageReceiver receiver; @@ -160,7 +160,7 @@ private class AckHandler implements FutureCallback { this.ackId = ackId; this.outstandingBytes = outstandingBytes; acked = new AtomicBoolean(false); - receivedTime = new Instant(clock.millis()); + receivedTime = new Instant(clock.millisTime()); } @Override @@ -191,7 +191,7 @@ public void onSuccess(AckReply reply) { // Record the latency rounded to the next closest integer. ackLatencyDistribution.record( Ints.saturatedCast( - (long) Math.ceil((clock.millis() - receivedTime.getMillis()) / 1000D))); + (long) Math.ceil((clock.millisTime() - receivedTime.getMillis()) / 1000D))); messagesWaiter.incrementPendingMessages(-1); return; case NACK: @@ -220,7 +220,7 @@ void sendAckOperations( Distribution ackLatencyDistribution, FlowController flowController, ScheduledExecutorService executor, - Clock clock) { + NanoClock clock) { this.executor = executor; this.ackExpirationPadding = ackExpirationPadding; this.receiver = receiver; @@ -264,7 +264,7 @@ public void processReceivedMessages(List r if (receivedMessagesCount == 0) { return; } - Instant now = new Instant(clock.millis()); + Instant now = new Instant(clock.millisTime()); int totalByteCount = 0; final ArrayList ackHandlers = new ArrayList<>(responseMessages.size()); for (ReceivedMessage pubsubMessage : responseMessages) { @@ -356,7 +356,7 @@ public void run() { alarmsLock.unlock(); } - Instant now = new Instant(clock.millis()); + Instant now = new Instant(clock.millisTime()); // Rounded to the next second, so we only schedule future alarms at the second // resolution. Instant cutOverTime = @@ -453,7 +453,7 @@ private void setupNextAckDeadlineExtensionAlarm(Instant expiration) { ackDeadlineExtensionAlarm = executor.schedule( new AckDeadlineAlarm(), - nextAckDeadlineExtensionAlarmTime.getMillis() - clock.millis(), + nextAckDeadlineExtensionAlarmTime.getMillis() - clock.millisTime(), TimeUnit.MILLISECONDS); } diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PollingSubscriberConnection.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PollingSubscriberConnection.java index e2a8073f431f..07023b0c2e12 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PollingSubscriberConnection.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PollingSubscriberConnection.java @@ -19,9 +19,9 @@ import static com.google.cloud.pubsub.spi.v1.StatusUtil.isRetryable; import com.google.api.gax.core.FlowController; +import com.google.api.gax.core.NanoClock; import com.google.api.stats.Distribution; import com.google.auth.Credentials; -import com.google.cloud.Clock; import com.google.cloud.pubsub.spi.v1.MessageDispatcher.AckProcessor; import com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline; import com.google.common.collect.Lists; @@ -75,7 +75,7 @@ public PollingSubscriberConnection( Channel channel, FlowController flowController, ScheduledExecutorService executor, - Clock clock) { + NanoClock clock) { this.subscription = subscription; this.executor = executor; stub = diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/StreamingSubscriberConnection.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/StreamingSubscriberConnection.java index 7c3a6a8b7c4b..0045bd83db6d 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/StreamingSubscriberConnection.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/StreamingSubscriberConnection.java @@ -19,9 +19,9 @@ import static com.google.cloud.pubsub.spi.v1.StatusUtil.isRetryable; import com.google.api.gax.core.FlowController; +import com.google.api.gax.core.NanoClock; import com.google.api.stats.Distribution; import com.google.auth.Credentials; -import com.google.cloud.Clock; import com.google.cloud.pubsub.spi.v1.MessageDispatcher.AckProcessor; import com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline; import com.google.common.annotations.VisibleForTesting; @@ -77,7 +77,7 @@ public StreamingSubscriberConnection( Channel channel, FlowController flowController, ScheduledExecutorService executor, - Clock clock) { + NanoClock clock) { this.subscription = subscription; this.executor = executor; this.credentials = credentials; diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java index d71799d937ce..fada1ea56787 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java @@ -18,12 +18,13 @@ import com.google.api.gax.core.FlowControlSettings; import com.google.api.gax.core.FlowController; +import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.DefaultNanoClock; import com.google.api.gax.grpc.ExecutorProvider; import com.google.api.gax.grpc.InstantiatingExecutorProvider; import com.google.api.stats.Distribution; import com.google.auth.Credentials; import com.google.auth.oauth2.GoogleCredentials; -import com.google.cloud.Clock; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; import com.google.common.base.Preconditions; @@ -266,7 +267,7 @@ private static class SubscriberImpl extends AbstractService { private final MessageReceiver receiver; private final List streamingSubscriberConnections; private final List pollingSubscriberConnections; - private final Clock clock; + private final NanoClock clock; private final List closeables = new ArrayList<>(); private ScheduledFuture ackDeadlineUpdater; private int streamAckDeadlineSeconds; @@ -281,7 +282,7 @@ private SubscriberImpl(Builder builder) throws IOException { Math.max( INITIAL_ACK_DEADLINE_SECONDS, Ints.saturatedCast(ackExpirationPadding.getStandardSeconds())); - clock = builder.clock.isPresent() ? builder.clock.get() : Clock.defaultClock(); + clock = builder.clock.isPresent() ? builder.clock.get() : DefaultNanoClock.getDefaultClock(); flowController = new FlowController(builder.flowControlSettings); @@ -536,7 +537,7 @@ public static final class Builder { ExecutorProvider executorProvider = DEFAULT_EXECUTOR_PROVIDER; Optional>> channelBuilder = Optional.absent(); - Optional clock = Optional.absent(); + Optional clock = Optional.absent(); Builder(SubscriptionName subscriptionName, MessageReceiver receiver) { this.subscriptionName = subscriptionName; @@ -597,7 +598,7 @@ public Builder setExecutorProvider(ExecutorProvider executorProvider) { } /** Gives the ability to set a custom clock. */ - Builder setClock(Clock clock) { + Builder setClock(NanoClock clock) { this.clock = Optional.of(clock); return this; } diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewerTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewerTest.java index b11d9c72847d..2753ff48065e 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewerTest.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewerTest.java @@ -92,7 +92,7 @@ private IAnswer> createAnswer(final CountDownLatch latch, @Override public ApiFuture answer() throws Throwable { latch.countDown(); - renewal.set(executorService.getClock().millis()); + renewal.set(executorService.getClock().millisTime()); return null; } }; @@ -112,7 +112,7 @@ public void testAddOneMessage() throws InterruptedException { TimeUnit.MILLISECONDS, ImmutableList.of(ACK_ID1))) .andAnswer(createAnswer(secondLatch, secondRenewal)); EasyMock.replay(pubsub); - long addTime = executorService.getClock().millis(); + long addTime = executorService.getClock().millisTime(); ackDeadlineRenewer.add(SUBSCRIPTION1, ACK_ID1); executorService.advanceTime(TIME_ADVANCE); firstLatch.await(); @@ -144,7 +144,7 @@ public void testAddMessages() throws InterruptedException { TimeUnit.MILLISECONDS, ImmutableList.of(ACK_ID1, ACK_ID3))) .andAnswer(createAnswer(secondLatch, secondRenewalSub2)); EasyMock.replay(pubsub); - long addTime1 = executorService.getClock().millis(); + long addTime1 = executorService.getClock().millisTime(); ackDeadlineRenewer.add(SUBSCRIPTION1, ImmutableList.of(ACK_ID1, ACK_ID2)); ackDeadlineRenewer.add(SUBSCRIPTION2, ACK_ID1); executorService.advanceTime(TIME_ADVANCE); @@ -180,7 +180,7 @@ public void testAddExistingMessage() throws InterruptedException { TimeUnit.MILLISECONDS, ImmutableList.of(ACK_ID1))) .andAnswer(createAnswer(secondLatch, secondRenewalSub2)); EasyMock.replay(pubsub); - long addTime1 = executorService.getClock().millis(); + long addTime1 = executorService.getClock().millisTime(); ackDeadlineRenewer.add(SUBSCRIPTION1, ImmutableList.of(ACK_ID1, ACK_ID2)); ackDeadlineRenewer.add(SUBSCRIPTION2, ACK_ID1); executorService.advanceTime(TIME_ADVANCE); @@ -216,7 +216,7 @@ public void testRemoveNonExistingMessage() throws InterruptedException { TimeUnit.MILLISECONDS, ImmutableList.of(ACK_ID1))) .andAnswer(createAnswer(secondLatch, secondRenewalSub2)); EasyMock.replay(pubsub); - long addTime1 = executorService.getClock().millis(); + long addTime1 = executorService.getClock().millisTime(); ackDeadlineRenewer.add(SUBSCRIPTION1, ImmutableList.of(ACK_ID1, ACK_ID2)); ackDeadlineRenewer.add(SUBSCRIPTION2, ACK_ID1); executorService.advanceTime(TIME_ADVANCE); @@ -252,7 +252,7 @@ public void testRemoveMessage() throws InterruptedException { TimeUnit.MILLISECONDS, ImmutableList.of(ACK_ID1))) .andAnswer(createAnswer(secondLatch, secondRenewalSub2)); EasyMock.replay(pubsub); - long addTime1 = executorService.getClock().millis(); + long addTime1 = executorService.getClock().millisTime(); ackDeadlineRenewer.add(SUBSCRIPTION1, ImmutableList.of(ACK_ID1, ACK_ID2)); ackDeadlineRenewer.add(SUBSCRIPTION2, ACK_ID1); executorService.advanceTime(TIME_ADVANCE); diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/deprecated/PubSubImplTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/deprecated/PubSubImplTest.java index 6111a1ae2398..dcfe1d8775db 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/deprecated/PubSubImplTest.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/deprecated/PubSubImplTest.java @@ -31,8 +31,8 @@ import com.google.cloud.Identity; import com.google.cloud.Page; import com.google.cloud.Policy; -import com.google.cloud.RetryParams; import com.google.cloud.Role; +import com.google.cloud.ServiceOptions; import com.google.cloud.pubsub.deprecated.PubSub.ListOption; import com.google.cloud.pubsub.deprecated.PubSub.MessageConsumer; import com.google.cloud.pubsub.deprecated.PubSub.MessageProcessor; @@ -166,7 +166,8 @@ public void setUp() { options = EasyMock.createMock(PubSubOptions.class); EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes(); EasyMock.expect(options.getRpc()).andReturn(pubsubRpcMock).anyTimes(); - EasyMock.expect(options.getRetryParams()).andReturn(RetryParams.noRetries()).anyTimes(); + EasyMock.expect(options.getRetrySettings()).andReturn(ServiceOptions.getNoRetrySettings()) + .anyTimes(); EasyMock.replay(rpcFactoryMock, pubsubRpcMock, renewerMock, options); EasyMock.reset(pubsubRpcMock, renewerMock); } diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeClock.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeClock.java index 76bf3f8e570f..eb021b368af8 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeClock.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeClock.java @@ -16,12 +16,12 @@ package com.google.cloud.pubsub.spi.v1; -import com.google.cloud.Clock; +import com.google.api.gax.core.NanoClock; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; /** A Clock to help with testing time-based logic. */ -public class FakeClock extends Clock { +public class FakeClock implements NanoClock { private final AtomicLong millis = new AtomicLong(); @@ -31,7 +31,12 @@ public void advance(long time, TimeUnit timeUnit) { } @Override - public long millis() { + public long nanoTime() { + return millisTime() * 1000_000L; + } + + @Override + public long millisTime() { return millis.get(); } } diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeScheduledExecutorService.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeScheduledExecutorService.java index 7daea232e25d..fbbeb9a278f3 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeScheduledExecutorService.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeScheduledExecutorService.java @@ -16,7 +16,7 @@ package com.google.cloud.pubsub.spi.v1; -import com.google.cloud.Clock; +import com.google.api.gax.core.NanoClock; import com.google.common.primitives.Ints; import com.google.common.util.concurrent.SettableFuture; import java.util.ArrayList; @@ -45,7 +45,7 @@ public class FakeScheduledExecutorService extends AbstractExecutorService private final PriorityQueue> pendingCallables = new PriorityQueue<>(); private final FakeClock clock = new FakeClock(); - public Clock getClock() { + public NanoClock getClock() { return clock; } @@ -89,7 +89,7 @@ public void advanceTime(Duration toAdvance) { } private void work() { - DateTime cmpTime = new DateTime(clock.millis()); + DateTime cmpTime = new DateTime(clock.millisTime()); for (;;) { PendingCallable callable = null; @@ -193,7 +193,7 @@ static enum PendingCallableType { /** Class that saves the state of an scheduled pending callable. */ class PendingCallable implements Comparable> { - DateTime creationTime = new DateTime(clock.millis()); + DateTime creationTime = new DateTime(clock.millisTime()); Duration delay; Callable pendingCallable; SettableFuture future = SettableFuture.create(); @@ -228,7 +228,7 @@ ScheduledFuture getScheduledFuture() { return new ScheduledFuture() { @Override public long getDelay(TimeUnit unit) { - return unit.convert(getScheduledTime().getMillis() - clock.millis(), TimeUnit.MILLISECONDS); + return unit.convert(getScheduledTime().getMillis() - clock.millisTime(), TimeUnit.MILLISECONDS); } @Override @@ -285,7 +285,7 @@ T call() { done.set(true); break; case FIXED_DELAY: - this.creationTime = new DateTime(clock.millis()); + this.creationTime = new DateTime(clock.millisTime()); schedulePendingCallable(this); break; case FIXED_RATE: diff --git a/google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerException.java b/google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerException.java index e6171955f516..97165cacfe7a 100644 --- a/google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerException.java +++ b/google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerException.java @@ -18,7 +18,6 @@ import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; -import com.google.cloud.RetryHelper.RetryInterruptedException; import com.google.common.collect.ImmutableSet; import java.io.IOException; @@ -70,10 +69,9 @@ protected Set getRetryableErrors() { * * @throws ResourceManagerException when {@code ex} was caused by a {@code * ResourceManagerException} - * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ static ResourceManagerException translateAndThrow(RetryHelperException ex) { - BaseServiceException.translateAndPropagateIfPossible(ex); + BaseServiceException.translate(ex); throw new ResourceManagerException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } } diff --git a/google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerImpl.java b/google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerImpl.java index 5c1069d1c170..b5f7bac307fb 100644 --- a/google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerImpl.java +++ b/google-cloud-resourcemanager/src/main/java/com/google/cloud/resourcemanager/ResourceManagerImpl.java @@ -56,7 +56,7 @@ public Project create(final ProjectInfo project) { public com.google.api.services.cloudresourcemanager.model.Project call() { return resourceManagerRpc.create(project.toPb()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); } @@ -71,7 +71,7 @@ public Void call() { resourceManagerRpc.delete(projectId); return null; } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); } @@ -87,7 +87,7 @@ public Project get(final String projectId, ProjectGetOption... options) { public com.google.api.services.cloudresourcemanager.model.Project call() { return resourceManagerRpc.get(projectId, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Project.fromPb(this, answer); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); @@ -131,7 +131,7 @@ Iterable> call() { return serviceOptions.getRpc().list(optionsMap); } }, - serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable projects = result.y() == null @@ -163,7 +163,7 @@ public Project replace(final ProjectInfo newProject) { public com.google.api.services.cloudresourcemanager.model.Project call() { return resourceManagerRpc.replace(newProject.toPb()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); } @@ -178,7 +178,7 @@ public Void call() { resourceManagerRpc.undelete(projectId); return null; } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); } @@ -194,7 +194,7 @@ public Policy getPolicy(final String projectId) { public com.google.api.services.cloudresourcemanager.model.Policy call() { return resourceManagerRpc.getPolicy(projectId); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : PolicyMarshaller.INSTANCE.fromPb(answer); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); @@ -211,7 +211,7 @@ public com.google.api.services.cloudresourcemanager.model.Policy call() { return resourceManagerRpc.replacePolicy(projectId, PolicyMarshaller.INSTANCE.toPb(newPolicy)); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); } @@ -226,7 +226,7 @@ public List testPermissions(final String projectId, final List public List call() { return resourceManagerRpc.testPermissions(projectId, permissions); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException ex) { throw ResourceManagerException.translateAndThrow(ex); } diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Operation.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Operation.java index 956820cb4c17..d638809187f6 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Operation.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Operation.java @@ -16,7 +16,8 @@ package com.google.cloud.spanner; -import com.google.cloud.Clock; +import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.SystemClock; import com.google.cloud.WaitForOption; import com.google.cloud.WaitForOption.CheckingPeriod; import com.google.cloud.WaitForOption.Timeout; @@ -44,7 +45,7 @@ static interface Parser { private final SpannerRpc rpc; private final String name; private final Parser parser; - private final Clock clock; + private final NanoClock clock; @VisibleForTesting Operation( @@ -55,7 +56,7 @@ static interface Parser { @Nullable SpannerException exception, boolean isDone, Parser parser, - Clock clock) { + NanoClock clock) { this.rpc = rpc; this.name = name; this.metadata = metadata; @@ -67,7 +68,7 @@ static interface Parser { } private static Operation failed( - SpannerRpc rpc, String name, Status status, M metadata, Parser parser, Clock clock) { + SpannerRpc rpc, String name, Status status, M metadata, Parser parser, NanoClock clock) { SpannerException e = SpannerExceptionFactory.newSpannerException( ErrorCode.fromRpcStatus(status), status.getMessage(), null); @@ -75,22 +76,22 @@ private static Operation failed( } private static Operation successful( - SpannerRpc rpc, String name, M metadata, R result, Parser parser, Clock clock) { + SpannerRpc rpc, String name, M metadata, R result, Parser parser, NanoClock clock) { return new Operation<>(rpc, name, metadata, result, null, true, parser, clock); } private static Operation pending( - SpannerRpc rpc, String name, M metadata, Parser parser, Clock clock) { + SpannerRpc rpc, String name, M metadata, Parser parser, NanoClock clock) { return new Operation<>(rpc, name, metadata, null, null, false, parser, clock); } static Operation create( SpannerRpc rpc, com.google.longrunning.Operation proto, Parser parser) { - return Operation.create(rpc, proto, parser, Clock.defaultClock()); + return Operation.create(rpc, proto, parser, SystemClock.getDefaultClock()); } static Operation create( - SpannerRpc rpc, com.google.longrunning.Operation proto, Parser parser, Clock clock) { + SpannerRpc rpc, com.google.longrunning.Operation proto, Parser parser, NanoClock clock) { M metadata = proto.hasMetadata() ? parser.parseMetadata(proto.getMetadata()) : null; String name = proto.getName(); if (proto.getDone()) { @@ -126,14 +127,14 @@ public Operation waitFor(WaitForOption... options) throws SpannerException long timeoutMillis = Timeout.getOrDefault(options).getTimeoutMillis(); boolean hasTimeout = timeoutMillis != -1; CheckingPeriod period = CheckingPeriod.getOrDefault(options); - long startMillis = clock.millis(); + long startMillis = clock.millisTime(); while (true) { try { com.google.longrunning.Operation proto = rpc.getOperation(name); if (proto.getDone()) { return Operation.create(rpc, proto, parser); } - long elapsed = clock.millis() - startMillis; + long elapsed = clock.millisTime() - startMillis; if (hasTimeout && elapsed >= timeoutMillis) { throw SpannerExceptionFactory.newSpannerException( ErrorCode.DEADLINE_EXCEEDED, "Operation did not complete in the given time"); diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/OperationTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/OperationTest.java index 30bf31390bc5..b338e2a16e1a 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/OperationTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/OperationTest.java @@ -22,7 +22,7 @@ import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; -import com.google.cloud.Clock; +import com.google.api.gax.core.NanoClock; import com.google.cloud.WaitForOption; import com.google.cloud.spanner.spi.SpannerRpc; import com.google.protobuf.Any; @@ -47,7 +47,7 @@ public class OperationTest { @Mock SpannerRpc rpc; @Mock DatabaseAdminClient dbClient; - @Mock Clock clock; + @Mock NanoClock clock; @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -174,7 +174,7 @@ public void waitForTimesout() throws Exception { com.google.longrunning.Operation proto = newBuilder().setName("op1").setDone(false).build(); Operation op = Operation.create(rpc, proto, new ParserImpl(), clock); when(rpc.getOperation("op1")).thenReturn(proto); - when(clock.millis()).thenReturn(0L, 50L, 100L, 150L); + when(clock.millisTime()).thenReturn(0L, 50L, 100L, 150L); expectedException.expect(isSpannerException(ErrorCode.DEADLINE_EXCEEDED)); op.waitFor( diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobReadChannel.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobReadChannel.java index 48f95a1cb240..6e691c28b97b 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobReadChannel.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobReadChannel.java @@ -127,7 +127,7 @@ public int read(ByteBuffer byteBuffer) throws IOException { public Tuple call() { return storageRpc.read(storageObject, requestOptions, position, toRead); } - }, serviceOptions.getRetryParams(), StorageImpl.EXCEPTION_HANDLER, + }, serviceOptions.getRetrySettings(), StorageImpl.EXCEPTION_HANDLER, serviceOptions.getClock()); if (result.y().length > 0 && lastEtag != null && !Objects.equals(result.x(), lastEtag)) { StringBuilder messageBuilder = new StringBuilder(); diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobWriteChannel.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobWriteChannel.java index 18cf3a559833..e9a5617b71d8 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobWriteChannel.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/BlobWriteChannel.java @@ -49,7 +49,7 @@ protected void flushBuffer(final int length, final boolean last) { public void run() { getOptions().getRpc().write(getUploadId(), getBuffer(), 0, getPosition(), length, last); } - }), getOptions().getRetryParams(), StorageImpl.EXCEPTION_HANDLER, getOptions().getClock()); + }), getOptions().getRetrySettings(), StorageImpl.EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelper.RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -67,7 +67,7 @@ private static String open(final StorageOptions options, final BlobInfo blob, public String call() { return options.getRpc().open(blob.toPb(), optionsMap); } - }, options.getRetryParams(), StorageImpl.EXCEPTION_HANDLER, options.getClock()); + }, options.getRetrySettings(), StorageImpl.EXCEPTION_HANDLER, options.getClock()); } catch (RetryHelper.RetryHelperException e) { throw StorageException.translateAndThrow(e); } diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/CopyWriter.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/CopyWriter.java index f0d477890ab0..9540a2201540 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/CopyWriter.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/CopyWriter.java @@ -113,7 +113,7 @@ public void copyChunk() { public RewriteResponse call() { return storageRpc.continueRewrite(rewriteResponse); } - }, serviceOptions.getRetryParams(), StorageImpl.EXCEPTION_HANDLER, + }, serviceOptions.getRetrySettings(), StorageImpl.EXCEPTION_HANDLER, serviceOptions.getClock()); } catch (RetryHelper.RetryHelperException e) { throw StorageException.translateAndThrow(e); diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageException.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageException.java index 16b2332b1b5c..0bf43b57faee 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageException.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageException.java @@ -19,7 +19,6 @@ import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; -import com.google.cloud.RetryHelper.RetryInterruptedException; import com.google.common.collect.ImmutableSet; import java.io.IOException; @@ -71,10 +70,9 @@ protected Set getRetryableErrors() { * always throw an exception. * * @throws StorageException when {@code ex} was caused by a {@code StorageException} - * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ static StorageException translateAndThrow(RetryHelperException ex) { - BaseServiceException.translateAndPropagateIfPossible(ex); + BaseServiceException.translate(ex); throw new StorageException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } } diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index 55d4ead5bf0a..f981b5252ac2 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -104,7 +104,7 @@ public Bucket create(BucketInfo bucketInfo, BucketTargetOption... options) { public com.google.api.services.storage.model.Bucket call() { return storageRpc.create(bucketPb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -146,7 +146,7 @@ public StorageObject call() { return storageRpc.create(blobPb, firstNonNull(content, new ByteArrayInputStream(EMPTY_BYTE_ARRAY)), optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -163,7 +163,7 @@ public Bucket get(String bucket, BucketGetOption... options) { public com.google.api.services.storage.model.Bucket call() { return storageRpc.get(bucketPb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Bucket.fromPb(this, answer); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); @@ -185,7 +185,7 @@ public Blob get(BlobId blob, BlobGetOption... options) { public StorageObject call() { return storageRpc.get(storedObject, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return storageObject == null ? null : Blob.fromPb(this, storageObject); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); @@ -259,7 +259,7 @@ private static Page listBuckets(final StorageOptions serviceOptions, public Tuple> call() { return serviceOptions.getRpc().list(optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable buckets = result.y() == null ? ImmutableList.of() : Iterables.transform(result.y(), @@ -286,7 +286,7 @@ private static Page listBlobs(final String bucket, public Tuple> call() { return serviceOptions.getRpc().list(bucket, optionsMap); } - }, serviceOptions.getRetryParams(), EXCEPTION_HANDLER, serviceOptions.getClock()); + }, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock()); String cursor = result.x(); Iterable blobs = result.y() == null @@ -317,7 +317,7 @@ public Bucket update(BucketInfo bucketInfo, BucketTargetOption... options) { public com.google.api.services.storage.model.Bucket call() { return storageRpc.patch(bucketPb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -333,7 +333,7 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) { public StorageObject call() { return storageRpc.patch(storageObject, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -354,7 +354,7 @@ public boolean delete(String bucket, BucketSourceOption... options) { public Boolean call() { return storageRpc.delete(bucketPb, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -375,7 +375,7 @@ public boolean delete(BlobId blob, BlobSourceOption... options) { public Boolean call() { return storageRpc.delete(storageObject, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -406,7 +406,7 @@ public Blob compose(final ComposeRequest composeRequest) { public StorageObject call() { return storageRpc.compose(sources, target, targetOptions); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -429,7 +429,7 @@ public RewriteResponse call() { copyRequest.overrideInfo(), targetObject, targetOptions, copyRequest.getMegabytesCopiedPerChunk())); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return new CopyWriter(getOptions(), rewriteResponse); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); @@ -451,7 +451,7 @@ public byte[] readAllBytes(BlobId blob, BlobSourceOption... options) { public byte[] call() { return storageRpc.load(storageObject, optionsMap); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -517,7 +517,7 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio } stBuilder.append('\n'); long expiration = TimeUnit.SECONDS.convert( - getOptions().getClock().millis() + unit.toMillis(duration), TimeUnit.MILLISECONDS); + getOptions().getClock().millisTime() + unit.toMillis(duration), TimeUnit.MILLISECONDS); stBuilder.append(expiration).append('\n'); StringBuilder path = new StringBuilder(); if (!blobInfo.getBucket().startsWith("/")) { @@ -633,7 +633,7 @@ public Acl getAcl(final String bucket, final Entity entity) { public BucketAccessControl call() { return storageRpc.getAcl(bucket, entity.toPb()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Acl.fromPb(answer); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); @@ -648,7 +648,7 @@ public boolean deleteAcl(final String bucket, final Entity entity) { public Boolean call() { return storageRpc.deleteAcl(bucket, entity.toPb()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -663,7 +663,7 @@ public Acl createAcl(String bucket, Acl acl) { public BucketAccessControl call() { return storageRpc.createAcl(aclPb); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -678,7 +678,7 @@ public Acl updateAcl(String bucket, Acl acl) { public BucketAccessControl call() { return storageRpc.patchAcl(aclPb); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -692,7 +692,7 @@ public List listAcls(final String bucket) { public List call() { return storageRpc.listAcls(bucket); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return Lists.transform(answer, Acl.FROM_BUCKET_PB_FUNCTION); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); @@ -707,7 +707,7 @@ public Acl getDefaultAcl(final String bucket, final Entity entity) { public ObjectAccessControl call() { return storageRpc.getDefaultAcl(bucket, entity.toPb()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Acl.fromPb(answer); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); @@ -722,7 +722,7 @@ public boolean deleteDefaultAcl(final String bucket, final Entity entity) { public Boolean call() { return storageRpc.deleteDefaultAcl(bucket, entity.toPb()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -737,7 +737,7 @@ public Acl createDefaultAcl(String bucket, Acl acl) { public ObjectAccessControl call() { return storageRpc.createDefaultAcl(aclPb); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -752,7 +752,7 @@ public Acl updateDefaultAcl(String bucket, Acl acl) { public ObjectAccessControl call() { return storageRpc.patchDefaultAcl(aclPb); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -766,7 +766,7 @@ public List listDefaultAcls(final String bucket) { public List call() { return storageRpc.listDefaultAcls(bucket); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return Lists.transform(answer, Acl.FROM_OBJECT_PB_FUNCTION); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); @@ -782,7 +782,7 @@ public ObjectAccessControl call() { return storageRpc.getAcl( blob.getBucket(), blob.getName(), blob.getGeneration(), entity.toPb()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return answer == null ? null : Acl.fromPb(answer); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); @@ -798,7 +798,7 @@ public Boolean call() { return storageRpc.deleteAcl( blob.getBucket(), blob.getName(), blob.getGeneration(), entity.toPb()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -816,7 +816,7 @@ public Acl createAcl(final BlobId blob, final Acl acl) { public ObjectAccessControl call() { return storageRpc.createAcl(aclPb); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -834,7 +834,7 @@ public Acl updateAcl(BlobId blob, Acl acl) { public ObjectAccessControl call() { return storageRpc.patchAcl(aclPb); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock())); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock())); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -848,7 +848,7 @@ public List listAcls(final BlobId blob) { public List call() { return storageRpc.listAcls(blob.getBucket(), blob.getName(), blob.getGeneration()); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); return Lists.transform(answer, Acl.FROM_OBJECT_PB_FUNCTION); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/RemoteStorageHelper.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/RemoteStorageHelper.java index 9806da27f481..df6c58b834b9 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/RemoteStorageHelper.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/testing/RemoteStorageHelper.java @@ -17,7 +17,7 @@ package com.google.cloud.storage.testing; import com.google.auth.oauth2.GoogleCredentials; -import com.google.cloud.RetryParams; +import com.google.api.gax.core.RetrySettings; import com.google.cloud.storage.BlobInfo; import com.google.cloud.storage.Storage; import com.google.cloud.storage.Storage.BlobListOption; @@ -36,15 +36,15 @@ import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; +import org.joda.time.Duration; /** * Utility to create a remote storage configuration for testing. Storage options can be obtained via * the {@link #getOptions()} ()} method. Returned options have custom - * {@link StorageOptions#getRetryParams()}: {@link RetryParams#getRetryMaxAttempts()} is {@code 10}, - * {@link RetryParams#getRetryMinAttempts()} is {@code 6}, - * {@link RetryParams#getMaxRetryDelayMillis()} is {@code 30000}, - * {@link RetryParams#getTotalRetryPeriodMillis()} is {@code 120000} and - * {@link RetryParams#getInitialRetryDelayMillis()} is {@code 250}. + * {@link StorageOptions#getRetrySettings()}: {@link RetrySettings#getMaxAttempts()} is {@code 10}, + * {@link RetrySettings#getMaxRetryDelay()} is {@code 30000}, + * {@link RetrySettings#getTotalTimeout()} is {@code 120000} and + * {@link RetrySettings#getInitialRetryDelay()} is {@code 250}. * {@link StorageOptions#getConnectTimeout()} and {@link StorageOptions#getReadTimeout()} are both * set to {@code 60000}. */ @@ -127,7 +127,7 @@ public static RemoteStorageHelper create(String projectId, InputStream keyStream StorageOptions storageOptions = StorageOptions.newBuilder() .setCredentials(GoogleCredentials.fromStream(keyStream)) .setProjectId(projectId) - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setConnectTimeout(60000) .setReadTimeout(60000) .build(); @@ -146,20 +146,22 @@ public static RemoteStorageHelper create(String projectId, InputStream keyStream */ public static RemoteStorageHelper create() throws StorageHelperException { StorageOptions storageOptions = StorageOptions.newBuilder() - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setConnectTimeout(60000) .setReadTimeout(60000) .build(); return new RemoteStorageHelper(storageOptions); } - private static RetryParams retryParams() { - return RetryParams.newBuilder() - .setRetryMaxAttempts(10) - .setRetryMinAttempts(6) - .setMaxRetryDelayMillis(30000) - .setTotalRetryPeriodMillis(120000) - .setInitialRetryDelayMillis(250) + private static RetrySettings retryParams() { + return RetrySettings.newBuilder().setMaxAttempts(10) + .setMaxRetryDelay(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(120000L)) + .setInitialRetryDelay(Duration.millis(250L)) + .setRetryDelayMultiplier(1.0) + .setInitialRpcTimeout(Duration.millis(120000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(120000L)) .build(); } diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobReadChannelTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobReadChannelTest.java index e8980a8c2626..ed5bd03f58e7 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobReadChannelTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/BlobReadChannelTest.java @@ -28,7 +28,7 @@ import com.google.cloud.ReadChannel; import com.google.cloud.RestorableState; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.cloud.storage.spi.StorageRpc; import com.google.cloud.storage.spi.StorageRpcFactory; import com.google.common.collect.ImmutableMap; @@ -68,7 +68,7 @@ public void setUp() { options = StorageOptions.newBuilder() .setProjectId("projectId") .setServiceRpcFactory(rpcFactoryMock) - .setRetryParams(RetryParams.noRetries()) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) .build(); } diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/CopyWriterTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/CopyWriterTest.java index fba04665b8b0..3b000a0bd4ed 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/CopyWriterTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/CopyWriterTest.java @@ -25,7 +25,7 @@ import static org.junit.Assert.assertTrue; import com.google.cloud.RestorableState; -import com.google.cloud.RetryParams; +import com.google.cloud.ServiceOptions; import com.google.cloud.storage.spi.StorageRpc; import com.google.cloud.storage.spi.StorageRpc.RewriteRequest; import com.google.cloud.storage.spi.StorageRpc.RewriteResponse; @@ -83,7 +83,7 @@ public void setUp() { options = StorageOptions.newBuilder() .setProjectId("projectid") .setServiceRpcFactory(rpcFactoryMock) - .setRetryParams(RetryParams.noRetries()) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) .build(); result = new Blob(options.getService(), new BlobInfo.BuilderImpl(RESULT_INFO)); } diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java index bc6fded437f1..b70b4183c034 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java @@ -26,12 +26,13 @@ import static org.junit.Assert.assertTrue; import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.gax.core.NanoClock; import com.google.api.services.storage.model.StorageObject; import com.google.auth.oauth2.ServiceAccountCredentials; -import com.google.cloud.Clock; import com.google.cloud.Page; import com.google.cloud.ReadChannel; -import com.google.cloud.RetryParams; +import com.google.api.gax.core.RetrySettings; +import com.google.cloud.ServiceOptions; import com.google.cloud.WriteChannel; import com.google.cloud.storage.Acl.Project; import com.google.cloud.storage.Acl.Project.ProjectRole; @@ -106,8 +107,12 @@ public class StorageImplTest { private static final BucketInfo BUCKET_INFO2 = BucketInfo.newBuilder(BUCKET_NAME2).build(); // BlobInfo objects - private static final BlobInfo BLOB_INFO1 = BlobInfo.newBuilder(BUCKET_NAME1, BLOB_NAME1, 24L) - .setMetageneration(42L).setContentType("application/json").setMd5("md5string").build(); + private static final BlobInfo BLOB_INFO1 = + BlobInfo.newBuilder(BUCKET_NAME1, BLOB_NAME1, 24L) + .setMetageneration(42L) + .setContentType("application/json") + .setMd5("md5string") + .build(); private static final BlobInfo BLOB_INFO2 = BlobInfo.newBuilder(BUCKET_NAME1, BLOB_NAME2).build(); private static final BlobInfo BLOB_INFO3 = BlobInfo.newBuilder(BUCKET_NAME1, BLOB_NAME3).build(); @@ -119,9 +124,10 @@ public class StorageImplTest { Storage.BucketTargetOption.metagenerationMatch(); private static final Storage.BucketTargetOption BUCKET_TARGET_PREDEFINED_ACL = Storage.BucketTargetOption.predefinedAcl(Storage.PredefinedAcl.PRIVATE); - private static final Map BUCKET_TARGET_OPTIONS = ImmutableMap.of( - StorageRpc.Option.IF_METAGENERATION_MATCH, BUCKET_INFO1.getMetageneration(), - StorageRpc.Option.PREDEFINED_ACL, BUCKET_TARGET_PREDEFINED_ACL.getValue()); + private static final Map BUCKET_TARGET_OPTIONS = + ImmutableMap.of( + StorageRpc.Option.IF_METAGENERATION_MATCH, BUCKET_INFO1.getMetageneration(), + StorageRpc.Option.PREDEFINED_ACL, BUCKET_TARGET_PREDEFINED_ACL.getValue()); // Blob target options (create, update, compose) private static final BlobTargetOption BLOB_TARGET_GENERATION = BlobTargetOption.generationMatch(); @@ -130,16 +136,19 @@ public class StorageImplTest { private static final BlobTargetOption BLOB_TARGET_NOT_EXIST = BlobTargetOption.doesNotExist(); private static final BlobTargetOption BLOB_TARGET_PREDEFINED_ACL = BlobTargetOption.predefinedAcl(Storage.PredefinedAcl.PRIVATE); - private static final Map BLOB_TARGET_OPTIONS_CREATE = ImmutableMap.of( - StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_INFO1.getMetageneration(), - StorageRpc.Option.IF_GENERATION_MATCH, 0L, - StorageRpc.Option.PREDEFINED_ACL, BUCKET_TARGET_PREDEFINED_ACL.getValue()); - private static final Map BLOB_TARGET_OPTIONS_UPDATE = ImmutableMap.of( - StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_INFO1.getMetageneration(), - StorageRpc.Option.PREDEFINED_ACL, BUCKET_TARGET_PREDEFINED_ACL.getValue()); - private static final Map BLOB_TARGET_OPTIONS_COMPOSE = ImmutableMap.of( - StorageRpc.Option.IF_GENERATION_MATCH, BLOB_INFO1.getGeneration(), - StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_INFO1.getMetageneration()); + private static final Map BLOB_TARGET_OPTIONS_CREATE = + ImmutableMap.of( + StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_INFO1.getMetageneration(), + StorageRpc.Option.IF_GENERATION_MATCH, 0L, + StorageRpc.Option.PREDEFINED_ACL, BUCKET_TARGET_PREDEFINED_ACL.getValue()); + private static final Map BLOB_TARGET_OPTIONS_UPDATE = + ImmutableMap.of( + StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_INFO1.getMetageneration(), + StorageRpc.Option.PREDEFINED_ACL, BUCKET_TARGET_PREDEFINED_ACL.getValue()); + private static final Map BLOB_TARGET_OPTIONS_COMPOSE = + ImmutableMap.of( + StorageRpc.Option.IF_GENERATION_MATCH, BLOB_INFO1.getGeneration(), + StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_INFO1.getMetageneration()); // Blob write options (create, writer) private static final BlobWriteOption BLOB_WRITE_METAGENERATION = @@ -153,16 +162,18 @@ public class StorageImplTest { // Bucket get/source options private static final BucketSourceOption BUCKET_SOURCE_METAGENERATION = BucketSourceOption.metagenerationMatch(BUCKET_INFO1.getMetageneration()); - private static final Map BUCKET_SOURCE_OPTIONS = ImmutableMap.of( - StorageRpc.Option.IF_METAGENERATION_MATCH, BUCKET_SOURCE_METAGENERATION.getValue()); + private static final Map BUCKET_SOURCE_OPTIONS = + ImmutableMap.of( + StorageRpc.Option.IF_METAGENERATION_MATCH, BUCKET_SOURCE_METAGENERATION.getValue()); private static final Storage.BucketGetOption BUCKET_GET_METAGENERATION = Storage.BucketGetOption.metagenerationMatch(BUCKET_INFO1.getMetageneration()); private static final Storage.BucketGetOption BUCKET_GET_FIELDS = Storage.BucketGetOption.fields(Storage.BucketField.LOCATION, Storage.BucketField.ACL); private static final Storage.BucketGetOption BUCKET_GET_EMPTY_FIELDS = Storage.BucketGetOption.fields(); - private static final Map BUCKET_GET_OPTIONS = ImmutableMap.of( - StorageRpc.Option.IF_METAGENERATION_MATCH, BUCKET_SOURCE_METAGENERATION.getValue()); + private static final Map BUCKET_GET_OPTIONS = + ImmutableMap.of( + StorageRpc.Option.IF_METAGENERATION_MATCH, BUCKET_SOURCE_METAGENERATION.getValue()); // Blob get/source options private static final Storage.BlobGetOption BLOB_GET_METAGENERATION = @@ -173,23 +184,25 @@ public class StorageImplTest { Storage.BlobGetOption.generationMatch(); private static final Storage.BlobGetOption BLOB_GET_FIELDS = Storage.BlobGetOption.fields(Storage.BlobField.CONTENT_TYPE, Storage.BlobField.CRC32C); - private static final Storage.BlobGetOption BLOB_GET_EMPTY_FIELDS = - Storage.BlobGetOption.fields(); - private static final Map BLOB_GET_OPTIONS = ImmutableMap.of( - StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_GET_METAGENERATION.getValue(), - StorageRpc.Option.IF_GENERATION_MATCH, BLOB_GET_GENERATION.getValue()); + private static final Storage.BlobGetOption BLOB_GET_EMPTY_FIELDS = Storage.BlobGetOption.fields(); + private static final Map BLOB_GET_OPTIONS = + ImmutableMap.of( + StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_GET_METAGENERATION.getValue(), + StorageRpc.Option.IF_GENERATION_MATCH, BLOB_GET_GENERATION.getValue()); private static final BlobSourceOption BLOB_SOURCE_METAGENERATION = BlobSourceOption.metagenerationMatch(BLOB_INFO1.getMetageneration()); private static final BlobSourceOption BLOB_SOURCE_GENERATION = BlobSourceOption.generationMatch(BLOB_INFO1.getGeneration()); private static final BlobSourceOption BLOB_SOURCE_GENERATION_FROM_BLOB_ID = BlobSourceOption.generationMatch(); - private static final Map BLOB_SOURCE_OPTIONS = ImmutableMap.of( - StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_SOURCE_METAGENERATION.getValue(), - StorageRpc.Option.IF_GENERATION_MATCH, BLOB_SOURCE_GENERATION.getValue()); - private static final Map BLOB_SOURCE_OPTIONS_COPY = ImmutableMap.of( - StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH, BLOB_SOURCE_METAGENERATION.getValue(), - StorageRpc.Option.IF_SOURCE_GENERATION_MATCH, BLOB_SOURCE_GENERATION.getValue()); + private static final Map BLOB_SOURCE_OPTIONS = + ImmutableMap.of( + StorageRpc.Option.IF_METAGENERATION_MATCH, BLOB_SOURCE_METAGENERATION.getValue(), + StorageRpc.Option.IF_GENERATION_MATCH, BLOB_SOURCE_GENERATION.getValue()); + private static final Map BLOB_SOURCE_OPTIONS_COPY = + ImmutableMap.of( + StorageRpc.Option.IF_SOURCE_METAGENERATION_MATCH, BLOB_SOURCE_METAGENERATION.getValue(), + StorageRpc.Option.IF_SOURCE_GENERATION_MATCH, BLOB_SOURCE_GENERATION.getValue()); // Bucket list options private static final Storage.BucketListOption BUCKET_LIST_PAGE_SIZE = @@ -200,9 +213,10 @@ public class StorageImplTest { Storage.BucketListOption.fields(Storage.BucketField.LOCATION, Storage.BucketField.ACL); private static final Storage.BucketListOption BUCKET_LIST_EMPTY_FIELDS = Storage.BucketListOption.fields(); - private static final Map BUCKET_LIST_OPTIONS = ImmutableMap.of( - StorageRpc.Option.MAX_RESULTS, BUCKET_LIST_PAGE_SIZE.getValue(), - StorageRpc.Option.PREFIX, BUCKET_LIST_PREFIX.getValue()); + private static final Map BUCKET_LIST_OPTIONS = + ImmutableMap.of( + StorageRpc.Option.MAX_RESULTS, BUCKET_LIST_PAGE_SIZE.getValue(), + StorageRpc.Option.PREFIX, BUCKET_LIST_PREFIX.getValue()); // Blob list options private static final Storage.BlobListOption BLOB_LIST_PAGE_SIZE = @@ -215,20 +229,22 @@ public class StorageImplTest { Storage.BlobListOption.versions(false); private static final Storage.BlobListOption BLOB_LIST_EMPTY_FIELDS = Storage.BlobListOption.fields(); - private static final Map BLOB_LIST_OPTIONS = ImmutableMap.of( - StorageRpc.Option.MAX_RESULTS, BLOB_LIST_PAGE_SIZE.getValue(), - StorageRpc.Option.PREFIX, BLOB_LIST_PREFIX.getValue(), - StorageRpc.Option.VERSIONS, BLOB_LIST_VERSIONS.getValue()); + private static final Map BLOB_LIST_OPTIONS = + ImmutableMap.of( + StorageRpc.Option.MAX_RESULTS, BLOB_LIST_PAGE_SIZE.getValue(), + StorageRpc.Option.PREFIX, BLOB_LIST_PREFIX.getValue(), + StorageRpc.Option.VERSIONS, BLOB_LIST_VERSIONS.getValue()); // ACLs private static final Acl ACL = Acl.of(User.ofAllAuthenticatedUsers(), Role.OWNER); private static final Acl OTHER_ACL = Acl.of(new Project(ProjectRole.OWNERS, "p"), Role.READER); // Customer supplied encryption key options - private static final Map ENCRYPTION_KEY_OPTIONS = ImmutableMap.of( - StorageRpc.Option.CUSTOMER_SUPPLIED_KEY, BASE64_KEY); + private static final Map ENCRYPTION_KEY_OPTIONS = + ImmutableMap.of(StorageRpc.Option.CUSTOMER_SUPPLIED_KEY, BASE64_KEY); - private static final String PRIVATE_KEY_STRING = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoG" + private static final String PRIVATE_KEY_STRING = + "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoG" + "BAL2xolH1zrISQ8+GzOV29BNjjzq4/HIP8Psd1+cZb81vDklSF+95wB250MSE0BDc81pvIMwj5OmIfLg1NY6uB" + "1xavOPpVdx1z664AGc/BEJ1zInXGXaQ6s+SxGenVq40Yws57gikQGMZjttpf1Qbz4DjkxsbRoeaRHn06n9pH1e" + "jAgMBAAECgYEAkWcm0AJF5LMhbWKbjkxm/LG06UNApkHX6vTOOOODkonM/qDBnhvKCj8Tan+PaU2j7679Cd19q" @@ -240,16 +256,23 @@ public class StorageImplTest { + "ZRQEIoscyn1y2v/No/F5iYQJBAKBOGASoQcBjGTOg/H/SfcE8QVNsKEpthRrs6CkpT80aZ/AV+ksfoIf2zw2M3" + "mAHfrO+TBLdz4sicuFQvlN9SEc="; - private static final String PUBLIC_KEY_STRING = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9saJR9c6y" + private static final String PUBLIC_KEY_STRING = + "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9saJR9c6y" + "EkPPhszldvQTY486uPxyD/D7HdfnGW/Nbw5JUhfvecAdudDEhNAQ3PNabyDMI+TpiHy4NTWOrgdcWrzj6VXcdc" + "+uuABnPwRCdcyJ1xl2kOrPksRnp1auNGMLOe4IpEBjGY7baX9UG8+A45MbG0aHmkR59Op/aR9XowIDAQAB"; - private static final Clock TIME_SOURCE = new Clock() { - @Override - public long millis() { - return 42000L; - } - }; + private static final NanoClock TIME_SOURCE = + new NanoClock() { + @Override + public long nanoTime() { + return 42_000_000_000L; + } + + @Override + public long millisTime() { + return 42_000L; + } + }; private static final String ACCOUNT = "account"; private static PrivateKey privateKey; @@ -263,8 +286,7 @@ public long millis() { private Blob expectedBlob1, expectedBlob2, expectedBlob3; private Bucket expectedBucket1, expectedBucket2; - @Rule - public ExpectedException thrown = ExpectedException.none(); + @Rule public ExpectedException thrown = ExpectedException.none(); @BeforeClass public static void beforeClass() throws NoSuchAlgorithmException, InvalidKeySpecException { @@ -284,12 +306,13 @@ public void setUp() { EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(StorageOptions.class))) .andReturn(storageRpcMock); EasyMock.replay(rpcFactoryMock); - options = StorageOptions.newBuilder() - .setProjectId("projectId") - .setClock(TIME_SOURCE) - .setServiceRpcFactory(rpcFactoryMock) - .setRetryParams(RetryParams.noRetries()) - .build(); + options = + StorageOptions.newBuilder() + .setProjectId("projectId") + .setClock(TIME_SOURCE) + .setServiceRpcFactory(rpcFactoryMock) + .setRetrySettings(ServiceOptions.getNoRetrySettings()) + .build(); } @After @@ -341,13 +364,17 @@ public void testCreateBucketWithOptions() { @Test public void testCreateBlob() throws IOException { Capture capturedStream = Capture.newInstance(); - EasyMock.expect(storageRpcMock.create( - EasyMock.eq(BLOB_INFO1.toBuilder() - .setMd5(CONTENT_MD5) - .setCrc32c(CONTENT_CRC32C) - .build().toPb()), - EasyMock.capture(capturedStream), - EasyMock.eq(EMPTY_RPC_OPTIONS))) + EasyMock.expect( + storageRpcMock.create( + EasyMock.eq( + BLOB_INFO1 + .toBuilder() + .setMd5(CONTENT_MD5) + .setCrc32c(CONTENT_CRC32C) + .build() + .toPb()), + EasyMock.capture(capturedStream), + EasyMock.eq(EMPTY_RPC_OPTIONS))) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); @@ -363,14 +390,17 @@ public void testCreateBlob() throws IOException { @Test public void testCreateEmptyBlob() throws IOException { Capture capturedStream = Capture.newInstance(); - EasyMock.expect(storageRpcMock.create( - EasyMock.eq(BLOB_INFO1.toBuilder() - .setMd5("1B2M2Y8AsgTpgAmY7PhCfg==") - .setCrc32c("AAAAAA==") - .build() - .toPb()), - EasyMock.capture(capturedStream), - EasyMock.eq(EMPTY_RPC_OPTIONS))) + EasyMock.expect( + storageRpcMock.create( + EasyMock.eq( + BLOB_INFO1 + .toBuilder() + .setMd5("1B2M2Y8AsgTpgAmY7PhCfg==") + .setCrc32c("AAAAAA==") + .build() + .toPb()), + EasyMock.capture(capturedStream), + EasyMock.eq(EMPTY_RPC_OPTIONS))) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); @@ -384,19 +414,26 @@ public void testCreateEmptyBlob() throws IOException { @Test public void testCreateBlobWithOptions() throws IOException { Capture capturedStream = Capture.newInstance(); - EasyMock.expect(storageRpcMock.create( - EasyMock.eq(BLOB_INFO1.toBuilder() - .setMd5(CONTENT_MD5) - .setCrc32c(CONTENT_CRC32C) - .build() - .toPb()), - EasyMock.capture(capturedStream), - EasyMock.eq(BLOB_TARGET_OPTIONS_CREATE))) + EasyMock.expect( + storageRpcMock.create( + EasyMock.eq( + BLOB_INFO1 + .toBuilder() + .setMd5(CONTENT_MD5) + .setCrc32c(CONTENT_CRC32C) + .build() + .toPb()), + EasyMock.capture(capturedStream), + EasyMock.eq(BLOB_TARGET_OPTIONS_CREATE))) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = - storage.create(BLOB_INFO1, BLOB_CONTENT, BLOB_TARGET_METAGENERATION, BLOB_TARGET_NOT_EXIST, + storage.create( + BLOB_INFO1, + BLOB_CONTENT, + BLOB_TARGET_METAGENERATION, + BLOB_TARGET_NOT_EXIST, BLOB_TARGET_PREDEFINED_ACL); assertEquals(expectedBlob1, blob); ByteArrayInputStream byteStream = capturedStream.getValue(); @@ -409,13 +446,17 @@ public void testCreateBlobWithOptions() throws IOException { @Test public void testCreateBlobWithEncryptionKey() throws IOException { Capture capturedStream = Capture.newInstance(); - EasyMock.expect(storageRpcMock.create( - EasyMock.eq(BLOB_INFO1.toBuilder() - .setMd5(CONTENT_MD5). - setCrc32c(CONTENT_CRC32C) - .build().toPb()), - EasyMock.capture(capturedStream), - EasyMock.eq(ENCRYPTION_KEY_OPTIONS))) + EasyMock.expect( + storageRpcMock.create( + EasyMock.eq( + BLOB_INFO1 + .toBuilder() + .setMd5(CONTENT_MD5) + .setCrc32c(CONTENT_CRC32C) + .build() + .toPb()), + EasyMock.capture(capturedStream), + EasyMock.eq(ENCRYPTION_KEY_OPTIONS))) .andReturn(BLOB_INFO1.toPb()) .times(2); EasyMock.replay(storageRpcMock); @@ -457,8 +498,9 @@ public void testCreateBlobFromStreamWithEncryptionKey() throws IOException { BlobInfo infoWithHashes = infoBuilder.setMd5(CONTENT_MD5).setCrc32c(CONTENT_CRC32C).build(); BlobInfo infoWithoutHashes = infoBuilder.setMd5(null).setCrc32c(null).build(); EasyMock.expect( - storageRpcMock.create(infoWithoutHashes.toPb(), fileStream, ENCRYPTION_KEY_OPTIONS)) - .andReturn(BLOB_INFO1.toPb()).times(2); + storageRpcMock.create(infoWithoutHashes.toPb(), fileStream, ENCRYPTION_KEY_OPTIONS)) + .andReturn(BLOB_INFO1.toPb()) + .times(2); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = @@ -491,12 +533,15 @@ public void testGetBucketWithOptions() { @Test public void testGetBucketWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(storageRpcMock.get(EasyMock.eq(BucketInfo.of(BUCKET_NAME1).toPb()), - EasyMock.capture(capturedOptions))).andReturn(BUCKET_INFO1.toPb()); + EasyMock.expect( + storageRpcMock.get( + EasyMock.eq(BucketInfo.of(BUCKET_NAME1).toPb()), EasyMock.capture(capturedOptions))) + .andReturn(BUCKET_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = storage.get(BUCKET_NAME1, BUCKET_GET_METAGENERATION, BUCKET_GET_FIELDS); - assertEquals(BUCKET_GET_METAGENERATION.getValue(), + assertEquals( + BUCKET_GET_METAGENERATION.getValue(), capturedOptions.getValue().get(BUCKET_GET_METAGENERATION.getRpcOption())); String selector = (String) capturedOptions.getValue().get(BLOB_GET_FIELDS.getRpcOption()); assertTrue(selector.contains("name")); @@ -509,13 +554,15 @@ public void testGetBucketWithSelectedFields() { @Test public void testGetBucketWithEmptyFields() { Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(storageRpcMock.get(EasyMock.eq(BucketInfo.of(BUCKET_NAME1).toPb()), - EasyMock.capture(capturedOptions))).andReturn(BUCKET_INFO1.toPb()); + EasyMock.expect( + storageRpcMock.get( + EasyMock.eq(BucketInfo.of(BUCKET_NAME1).toPb()), EasyMock.capture(capturedOptions))) + .andReturn(BUCKET_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); - Bucket bucket = storage.get(BUCKET_NAME1, BUCKET_GET_METAGENERATION, - BUCKET_GET_EMPTY_FIELDS); - assertEquals(BUCKET_GET_METAGENERATION.getValue(), + Bucket bucket = storage.get(BUCKET_NAME1, BUCKET_GET_METAGENERATION, BUCKET_GET_EMPTY_FIELDS); + assertEquals( + BUCKET_GET_METAGENERATION.getValue(), capturedOptions.getValue().get(BUCKET_GET_METAGENERATION.getRpcOption())); String selector = (String) capturedOptions.getValue().get(BLOB_GET_FIELDS.getRpcOption()); assertTrue(selector.contains("name")); @@ -526,7 +573,7 @@ public void testGetBucketWithEmptyFields() { @Test public void testGetBlob() { EasyMock.expect( - storageRpcMock.get(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), EMPTY_RPC_OPTIONS)) + storageRpcMock.get(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), EMPTY_RPC_OPTIONS)) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); @@ -537,12 +584,11 @@ public void testGetBlob() { @Test public void testGetBlobWithOptions() { EasyMock.expect( - storageRpcMock.get(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), BLOB_GET_OPTIONS)) + storageRpcMock.get(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), BLOB_GET_OPTIONS)) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); - Blob blob = - storage.get(BUCKET_NAME1, BLOB_NAME1, BLOB_GET_METAGENERATION, BLOB_GET_GENERATION); + Blob blob = storage.get(BUCKET_NAME1, BLOB_NAME1, BLOB_GET_METAGENERATION, BLOB_GET_GENERATION); assertEquals(expectedBlob1, blob); } @@ -552,24 +598,34 @@ public void testGetBlobWithOptionsFromBlobId() { .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); - Blob blob = storage.get(BLOB_INFO1.getBlobId(), BLOB_GET_METAGENERATION, - BLOB_GET_GENERATION_FROM_BLOB_ID); + Blob blob = + storage.get( + BLOB_INFO1.getBlobId(), BLOB_GET_METAGENERATION, BLOB_GET_GENERATION_FROM_BLOB_ID); assertEquals(expectedBlob1, blob); } @Test public void testGetBlobWithSelectedFields() { Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(storageRpcMock.get(EasyMock.eq(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb()), - EasyMock.capture(capturedOptions))).andReturn(BLOB_INFO1.toPb()); + EasyMock.expect( + storageRpcMock.get( + EasyMock.eq(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb()), + EasyMock.capture(capturedOptions))) + .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); - Blob blob = storage.get( - BUCKET_NAME1, BLOB_NAME1, BLOB_GET_METAGENERATION, - BLOB_GET_GENERATION, BLOB_GET_FIELDS); - assertEquals(BLOB_GET_METAGENERATION.getValue(), + Blob blob = + storage.get( + BUCKET_NAME1, + BLOB_NAME1, + BLOB_GET_METAGENERATION, + BLOB_GET_GENERATION, + BLOB_GET_FIELDS); + assertEquals( + BLOB_GET_METAGENERATION.getValue(), capturedOptions.getValue().get(BLOB_GET_METAGENERATION.getRpcOption())); - assertEquals(BLOB_GET_GENERATION.getValue(), + assertEquals( + BLOB_GET_GENERATION.getValue(), capturedOptions.getValue().get(BLOB_GET_GENERATION.getRpcOption())); String selector = (String) capturedOptions.getValue().get(BLOB_GET_FIELDS.getRpcOption()); assertTrue(selector.contains("bucket")); @@ -583,15 +639,25 @@ public void testGetBlobWithSelectedFields() { @Test public void testGetBlobWithEmptyFields() { Capture> capturedOptions = Capture.newInstance(); - EasyMock.expect(storageRpcMock.get(EasyMock.eq(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb()), - EasyMock.capture(capturedOptions))).andReturn(BLOB_INFO1.toPb()); + EasyMock.expect( + storageRpcMock.get( + EasyMock.eq(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb()), + EasyMock.capture(capturedOptions))) + .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); - Blob blob = storage.get(BUCKET_NAME1, BLOB_NAME1, BLOB_GET_METAGENERATION, - BLOB_GET_GENERATION, BLOB_GET_EMPTY_FIELDS); - assertEquals(BLOB_GET_METAGENERATION.getValue(), + Blob blob = + storage.get( + BUCKET_NAME1, + BLOB_NAME1, + BLOB_GET_METAGENERATION, + BLOB_GET_GENERATION, + BLOB_GET_EMPTY_FIELDS); + assertEquals( + BLOB_GET_METAGENERATION.getValue(), capturedOptions.getValue().get(BLOB_GET_METAGENERATION.getRpcOption())); - assertEquals(BLOB_GET_GENERATION.getValue(), + assertEquals( + BLOB_GET_GENERATION.getValue(), capturedOptions.getValue().get(BLOB_GET_GENERATION.getRpcOption())); String selector = (String) capturedOptions.getValue().get(BLOB_GET_FIELDS.getRpcOption()); assertTrue(selector.contains("bucket")); @@ -617,14 +683,15 @@ public void testListBuckets() { @Test public void testListBucketsEmpty() { - EasyMock.expect(storageRpcMock.list(EMPTY_RPC_OPTIONS)).andReturn( - Tuple.>of(null, null)); + EasyMock.expect(storageRpcMock.list(EMPTY_RPC_OPTIONS)) + .andReturn( + Tuple.>of(null, null)); EasyMock.replay(storageRpcMock); initializeService(); Page page = storage.list(); assertNull(page.getNextPageCursor()); - assertArrayEquals(ImmutableList.of().toArray(), - Iterables.toArray(page.getValues(), Bucket.class)); + assertArrayEquals( + ImmutableList.of().toArray(), Iterables.toArray(page.getValues(), Bucket.class)); } @Test @@ -707,14 +774,15 @@ public void testListBlobs() { @Test public void testListBlobsEmpty() { EasyMock.expect(storageRpcMock.list(BUCKET_NAME1, EMPTY_RPC_OPTIONS)) - .andReturn(Tuple.>of( - null, null)); + .andReturn( + Tuple.>of( + null, null)); EasyMock.replay(storageRpcMock); initializeService(); Page page = storage.list(BUCKET_NAME1); assertNull(page.getNextPageCursor()); - assertArrayEquals(ImmutableList.of().toArray(), - Iterables.toArray(page.getValues(), Blob.class)); + assertArrayEquals( + ImmutableList.of().toArray(), Iterables.toArray(page.getValues(), Blob.class)); } @Test @@ -741,16 +809,18 @@ public void testListBlobsWithSelectedFields() { Tuple> result = Tuple.of(cursor, Iterables.transform(blobInfoList, BlobInfo.INFO_TO_PB_FUNCTION)); EasyMock.expect( - storageRpcMock.list(EasyMock.eq(BUCKET_NAME1), EasyMock.capture(capturedOptions))) + storageRpcMock.list(EasyMock.eq(BUCKET_NAME1), EasyMock.capture(capturedOptions))) .andReturn(result); EasyMock.replay(storageRpcMock); initializeService(); ImmutableList blobList = ImmutableList.of(expectedBlob1, expectedBlob2); Page page = storage.list(BUCKET_NAME1, BLOB_LIST_PAGE_SIZE, BLOB_LIST_PREFIX, BLOB_LIST_FIELDS); - assertEquals(BLOB_LIST_PAGE_SIZE.getValue(), + assertEquals( + BLOB_LIST_PAGE_SIZE.getValue(), capturedOptions.getValue().get(BLOB_LIST_PAGE_SIZE.getRpcOption())); - assertEquals(BLOB_LIST_PREFIX.getValue(), + assertEquals( + BLOB_LIST_PREFIX.getValue(), capturedOptions.getValue().get(BLOB_LIST_PREFIX.getRpcOption())); String selector = (String) capturedOptions.getValue().get(BLOB_LIST_FIELDS.getRpcOption()); assertTrue(selector.contains("prefixes")); @@ -774,16 +844,18 @@ public void testListBlobsWithEmptyFields() { Tuple> result = Tuple.of(cursor, Iterables.transform(blobInfoList, BlobInfo.INFO_TO_PB_FUNCTION)); EasyMock.expect( - storageRpcMock.list(EasyMock.eq(BUCKET_NAME1), EasyMock.capture(capturedOptions))) + storageRpcMock.list(EasyMock.eq(BUCKET_NAME1), EasyMock.capture(capturedOptions))) .andReturn(result); EasyMock.replay(storageRpcMock); initializeService(); ImmutableList blobList = ImmutableList.of(expectedBlob1, expectedBlob2); Page page = storage.list(BUCKET_NAME1, BLOB_LIST_PAGE_SIZE, BLOB_LIST_PREFIX, BLOB_LIST_EMPTY_FIELDS); - assertEquals(BLOB_LIST_PAGE_SIZE.getValue(), + assertEquals( + BLOB_LIST_PAGE_SIZE.getValue(), capturedOptions.getValue().get(BLOB_LIST_PAGE_SIZE.getRpcOption())); - assertEquals(BLOB_LIST_PREFIX.getValue(), + assertEquals( + BLOB_LIST_PREFIX.getValue(), capturedOptions.getValue().get(BLOB_LIST_PREFIX.getRpcOption())); String selector = (String) capturedOptions.getValue().get(BLOB_LIST_EMPTY_FIELDS.getRpcOption()); @@ -833,8 +905,8 @@ public void testUpdateBucketWithOptions() { EasyMock.replay(storageRpcMock); initializeService(); Bucket bucket = - storage.update(updatedBucketInfo, BUCKET_TARGET_METAGENERATION, - BUCKET_TARGET_PREDEFINED_ACL); + storage.update( + updatedBucketInfo, BUCKET_TARGET_METAGENERATION, BUCKET_TARGET_PREDEFINED_ACL); assertEquals(new Bucket(storage, new BucketInfo.BuilderImpl(updatedBucketInfo)), bucket); } @@ -872,8 +944,8 @@ public void testDeleteBucket() { @Test public void testDeleteBucketWithOptions() { - EasyMock - .expect(storageRpcMock.delete(BucketInfo.of(BUCKET_NAME1).toPb(), BUCKET_SOURCE_OPTIONS)) + EasyMock.expect( + storageRpcMock.delete(BucketInfo.of(BUCKET_NAME1).toPb(), BUCKET_SOURCE_OPTIONS)) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); @@ -883,7 +955,7 @@ public void testDeleteBucketWithOptions() { @Test public void testDeleteBlob() { EasyMock.expect( - storageRpcMock.delete(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), EMPTY_RPC_OPTIONS)) + storageRpcMock.delete(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), EMPTY_RPC_OPTIONS)) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); @@ -893,12 +965,13 @@ public void testDeleteBlob() { @Test public void testDeleteBlobWithOptions() { EasyMock.expect( - storageRpcMock.delete(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), BLOB_SOURCE_OPTIONS)) + storageRpcMock.delete(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), BLOB_SOURCE_OPTIONS)) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); - assertTrue(storage.delete(BUCKET_NAME1, BLOB_NAME1, BLOB_SOURCE_GENERATION, - BLOB_SOURCE_METAGENERATION)); + assertTrue( + storage.delete( + BUCKET_NAME1, BLOB_NAME1, BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION)); } @Test @@ -907,18 +980,26 @@ public void testDeleteBlobWithOptionsFromBlobId() { .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); - assertTrue(storage.delete(BLOB_INFO1.getBlobId(), BLOB_SOURCE_GENERATION_FROM_BLOB_ID, - BLOB_SOURCE_METAGENERATION)); + assertTrue( + storage.delete( + BLOB_INFO1.getBlobId(), + BLOB_SOURCE_GENERATION_FROM_BLOB_ID, + BLOB_SOURCE_METAGENERATION)); } @Test public void testCompose() { - Storage.ComposeRequest req = Storage.ComposeRequest.newBuilder() - .addSource(BLOB_NAME2, BLOB_NAME3) - .setTarget(BLOB_INFO1) - .build(); - EasyMock.expect(storageRpcMock.compose(ImmutableList.of(BLOB_INFO2.toPb(), BLOB_INFO3.toPb()), - BLOB_INFO1.toPb(), EMPTY_RPC_OPTIONS)).andReturn(BLOB_INFO1.toPb()); + Storage.ComposeRequest req = + Storage.ComposeRequest.newBuilder() + .addSource(BLOB_NAME2, BLOB_NAME3) + .setTarget(BLOB_INFO1) + .build(); + EasyMock.expect( + storageRpcMock.compose( + ImmutableList.of(BLOB_INFO2.toPb(), BLOB_INFO3.toPb()), + BLOB_INFO1.toPb(), + EMPTY_RPC_OPTIONS)) + .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.compose(req); @@ -927,13 +1008,18 @@ public void testCompose() { @Test public void testComposeWithOptions() { - Storage.ComposeRequest req = Storage.ComposeRequest.newBuilder() - .addSource(BLOB_NAME2, BLOB_NAME3) - .setTarget(BLOB_INFO1) - .setTargetOptions(BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) - .build(); - EasyMock.expect(storageRpcMock.compose(ImmutableList.of(BLOB_INFO2.toPb(), BLOB_INFO3.toPb()), - BLOB_INFO1.toPb(), BLOB_TARGET_OPTIONS_COMPOSE)).andReturn(BLOB_INFO1.toPb()); + Storage.ComposeRequest req = + Storage.ComposeRequest.newBuilder() + .addSource(BLOB_NAME2, BLOB_NAME3) + .setTarget(BLOB_INFO1) + .setTargetOptions(BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) + .build(); + EasyMock.expect( + storageRpcMock.compose( + ImmutableList.of(BLOB_INFO2.toPb(), BLOB_INFO3.toPb()), + BLOB_INFO1.toPb(), + BLOB_TARGET_OPTIONS_COMPOSE)) + .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.compose(req); @@ -943,10 +1029,16 @@ public void testComposeWithOptions() { @Test public void testCopy() { CopyRequest request = Storage.CopyRequest.of(BLOB_INFO1.getBlobId(), BLOB_INFO2.getBlobId()); - StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.getSource().toPb(), - EMPTY_RPC_OPTIONS, false, BLOB_INFO2.toPb(), EMPTY_RPC_OPTIONS, null); - StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, - false, "token", 21L); + StorageRpc.RewriteRequest rpcRequest = + new StorageRpc.RewriteRequest( + request.getSource().toPb(), + EMPTY_RPC_OPTIONS, + false, + BLOB_INFO2.toPb(), + EMPTY_RPC_OPTIONS, + null); + StorageRpc.RewriteResponse rpcResponse = + new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); EasyMock.replay(storageRpcMock); initializeService(); @@ -958,16 +1050,22 @@ public void testCopy() { @Test public void testCopyWithOptions() { - CopyRequest request = Storage.CopyRequest.newBuilder() - .setSource(BLOB_INFO2.getBlobId()) - .setSourceOptions(BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION) - .setTarget(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) - .build(); - StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.getSource().toPb(), - BLOB_SOURCE_OPTIONS_COPY, true, request.getTarget().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, - null); - StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, - false, "token", 21L); + CopyRequest request = + Storage.CopyRequest.newBuilder() + .setSource(BLOB_INFO2.getBlobId()) + .setSourceOptions(BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION) + .setTarget(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) + .build(); + StorageRpc.RewriteRequest rpcRequest = + new StorageRpc.RewriteRequest( + request.getSource().toPb(), + BLOB_SOURCE_OPTIONS_COPY, + true, + request.getTarget().toPb(), + BLOB_TARGET_OPTIONS_COMPOSE, + null); + StorageRpc.RewriteResponse rpcResponse = + new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); EasyMock.replay(storageRpcMock); initializeService(); @@ -979,15 +1077,22 @@ public void testCopyWithOptions() { @Test public void testCopyWithEncryptionKey() { - CopyRequest request = Storage.CopyRequest.newBuilder() - .setSource(BLOB_INFO2.getBlobId()) - .setSourceOptions(BlobSourceOption.decryptionKey(KEY)) - .setTarget(BLOB_INFO1, BlobTargetOption.encryptionKey(BASE64_KEY)) - .build(); - StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.getSource().toPb(), - ENCRYPTION_KEY_OPTIONS, true, request.getTarget().toPb(), ENCRYPTION_KEY_OPTIONS, null); - StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, - false, "token", 21L); + CopyRequest request = + Storage.CopyRequest.newBuilder() + .setSource(BLOB_INFO2.getBlobId()) + .setSourceOptions(BlobSourceOption.decryptionKey(KEY)) + .setTarget(BLOB_INFO1, BlobTargetOption.encryptionKey(BASE64_KEY)) + .build(); + StorageRpc.RewriteRequest rpcRequest = + new StorageRpc.RewriteRequest( + request.getSource().toPb(), + ENCRYPTION_KEY_OPTIONS, + true, + request.getTarget().toPb(), + ENCRYPTION_KEY_OPTIONS, + null); + StorageRpc.RewriteResponse rpcResponse = + new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse).times(2); EasyMock.replay(storageRpcMock); initializeService(); @@ -995,11 +1100,12 @@ public void testCopyWithEncryptionKey() { assertEquals(42L, writer.getBlobSize()); assertEquals(21L, writer.getTotalBytesCopied()); assertTrue(!writer.isDone()); - request = Storage.CopyRequest.newBuilder() - .setSource(BLOB_INFO2.getBlobId()) - .setSourceOptions(BlobSourceOption.decryptionKey(BASE64_KEY)) - .setTarget(BLOB_INFO1, BlobTargetOption.encryptionKey(KEY)) - .build(); + request = + Storage.CopyRequest.newBuilder() + .setSource(BLOB_INFO2.getBlobId()) + .setSourceOptions(BlobSourceOption.decryptionKey(BASE64_KEY)) + .setTarget(BLOB_INFO1, BlobTargetOption.encryptionKey(KEY)) + .build(); writer = storage.copy(request); assertEquals(42L, writer.getBlobSize()); assertEquals(21L, writer.getTotalBytesCopied()); @@ -1008,14 +1114,20 @@ public void testCopyWithEncryptionKey() { @Test public void testCopyWithOptionsFromBlobId() { - CopyRequest request = Storage.CopyRequest.newBuilder() - .setSource(BLOB_INFO1.getBlobId()) - .setSourceOptions(BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION) - .setTarget(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) - .build(); - StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.getSource().toPb(), - BLOB_SOURCE_OPTIONS_COPY, true, request.getTarget().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, - null); + CopyRequest request = + Storage.CopyRequest.newBuilder() + .setSource(BLOB_INFO1.getBlobId()) + .setSourceOptions(BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION) + .setTarget(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) + .build(); + StorageRpc.RewriteRequest rpcRequest = + new StorageRpc.RewriteRequest( + request.getSource().toPb(), + BLOB_SOURCE_OPTIONS_COPY, + true, + request.getTarget().toPb(), + BLOB_TARGET_OPTIONS_COMPOSE, + null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); @@ -1030,12 +1142,18 @@ public void testCopyWithOptionsFromBlobId() { @Test public void testCopyMultipleRequests() { CopyRequest request = Storage.CopyRequest.of(BLOB_INFO1.getBlobId(), BLOB_INFO2.getBlobId()); - StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest(request.getSource().toPb(), - EMPTY_RPC_OPTIONS, false, BLOB_INFO2.toPb(), EMPTY_RPC_OPTIONS, null); - StorageRpc.RewriteResponse rpcResponse1 = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, - false, "token", 21L); - StorageRpc.RewriteResponse rpcResponse2 = new StorageRpc.RewriteResponse(rpcRequest, - BLOB_INFO1.toPb(), 42L, true, "token", 42L); + StorageRpc.RewriteRequest rpcRequest = + new StorageRpc.RewriteRequest( + request.getSource().toPb(), + EMPTY_RPC_OPTIONS, + false, + BLOB_INFO2.toPb(), + EMPTY_RPC_OPTIONS, + null); + StorageRpc.RewriteResponse rpcResponse1 = + new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); + StorageRpc.RewriteResponse rpcResponse2 = + new StorageRpc.RewriteResponse(rpcRequest, BLOB_INFO1.toPb(), 42L, true, "token", 42L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse1); EasyMock.expect(storageRpcMock.continueRewrite(rpcResponse1)).andReturn(rpcResponse2); EasyMock.replay(storageRpcMock); @@ -1053,7 +1171,7 @@ public void testCopyMultipleRequests() { @Test public void testReadAllBytes() { EasyMock.expect( - storageRpcMock.load(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), EMPTY_RPC_OPTIONS)) + storageRpcMock.load(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), EMPTY_RPC_OPTIONS)) .andReturn(BLOB_CONTENT); EasyMock.replay(storageRpcMock); initializeService(); @@ -1064,47 +1182,51 @@ public void testReadAllBytes() { @Test public void testReadAllBytesWithOptions() { EasyMock.expect( - storageRpcMock.load(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), BLOB_SOURCE_OPTIONS)) + storageRpcMock.load(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), BLOB_SOURCE_OPTIONS)) .andReturn(BLOB_CONTENT); EasyMock.replay(storageRpcMock); initializeService(); - byte[] readBytes = storage.readAllBytes(BUCKET_NAME1, BLOB_NAME1, BLOB_SOURCE_GENERATION, - BLOB_SOURCE_METAGENERATION); + byte[] readBytes = + storage.readAllBytes( + BUCKET_NAME1, BLOB_NAME1, BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION); assertArrayEquals(BLOB_CONTENT, readBytes); } @Test public void testReadAllBytesWithDecriptionKey() { EasyMock.expect( - storageRpcMock.load(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), ENCRYPTION_KEY_OPTIONS)) - .andReturn(BLOB_CONTENT).times(2); + storageRpcMock.load(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), ENCRYPTION_KEY_OPTIONS)) + .andReturn(BLOB_CONTENT) + .times(2); EasyMock.replay(storageRpcMock); initializeService(); - byte[] readBytes = storage.readAllBytes(BUCKET_NAME1, BLOB_NAME1, - BlobSourceOption.decryptionKey(KEY)); + byte[] readBytes = + storage.readAllBytes(BUCKET_NAME1, BLOB_NAME1, BlobSourceOption.decryptionKey(KEY)); assertArrayEquals(BLOB_CONTENT, readBytes); - readBytes = storage.readAllBytes(BUCKET_NAME1, BLOB_NAME1, - BlobSourceOption.decryptionKey(BASE64_KEY)); + readBytes = + storage.readAllBytes(BUCKET_NAME1, BLOB_NAME1, BlobSourceOption.decryptionKey(BASE64_KEY)); assertArrayEquals(BLOB_CONTENT, readBytes); } @Test public void testReadAllBytesFromBlobIdWithOptions() { - EasyMock.expect( - storageRpcMock.load(BLOB_INFO1.getBlobId().toPb(), BLOB_SOURCE_OPTIONS)) + EasyMock.expect(storageRpcMock.load(BLOB_INFO1.getBlobId().toPb(), BLOB_SOURCE_OPTIONS)) .andReturn(BLOB_CONTENT); EasyMock.replay(storageRpcMock); initializeService(); - byte[] readBytes = storage.readAllBytes(BLOB_INFO1.getBlobId(), - BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION); + byte[] readBytes = + storage.readAllBytes( + BLOB_INFO1.getBlobId(), + BLOB_SOURCE_GENERATION_FROM_BLOB_ID, + BLOB_SOURCE_METAGENERATION); assertArrayEquals(BLOB_CONTENT, readBytes); } @Test public void testReadAllBytesFromBlobIdWithDecriptionKey() { - EasyMock.expect( - storageRpcMock.load(BLOB_INFO1.getBlobId().toPb(), ENCRYPTION_KEY_OPTIONS)) - .andReturn(BLOB_CONTENT).times(2); + EasyMock.expect(storageRpcMock.load(BLOB_INFO1.getBlobId().toPb(), ENCRYPTION_KEY_OPTIONS)) + .andReturn(BLOB_CONTENT) + .times(2); EasyMock.replay(storageRpcMock); initializeService(); byte[] readBytes = @@ -1141,12 +1263,13 @@ public void testReader() { public void testReaderWithOptions() throws IOException { byte[] result = new byte[DEFAULT_CHUNK_SIZE]; EasyMock.expect( - storageRpcMock.read(BLOB_INFO2.toPb(), BLOB_SOURCE_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) + storageRpcMock.read(BLOB_INFO2.toPb(), BLOB_SOURCE_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(StorageRpc.Tuple.of("etag", result)); EasyMock.replay(storageRpcMock); initializeService(); - ReadChannel channel = storage.reader(BUCKET_NAME1, BLOB_NAME2, BLOB_SOURCE_GENERATION, - BLOB_SOURCE_METAGENERATION); + ReadChannel channel = + storage.reader( + BUCKET_NAME1, BLOB_NAME2, BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION); assertNotNull(channel); assertTrue(channel.isOpen()); channel.read(ByteBuffer.allocate(42)); @@ -1156,8 +1279,9 @@ public void testReaderWithOptions() throws IOException { public void testReaderWithDecryptionKey() throws IOException { byte[] result = new byte[DEFAULT_CHUNK_SIZE]; EasyMock.expect( - storageRpcMock.read(BLOB_INFO2.toPb(), ENCRYPTION_KEY_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) - .andReturn(StorageRpc.Tuple.of("etag", result)).times(2); + storageRpcMock.read(BLOB_INFO2.toPb(), ENCRYPTION_KEY_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) + .andReturn(StorageRpc.Tuple.of("etag", result)) + .times(2); EasyMock.replay(storageRpcMock); initializeService(); ReadChannel channel = @@ -1174,12 +1298,17 @@ public void testReaderWithDecryptionKey() throws IOException { @Test public void testReaderWithOptionsFromBlobId() throws IOException { byte[] result = new byte[DEFAULT_CHUNK_SIZE]; - EasyMock.expect(storageRpcMock.read(BLOB_INFO1.getBlobId().toPb(), BLOB_SOURCE_OPTIONS, 0, - DEFAULT_CHUNK_SIZE)).andReturn(StorageRpc.Tuple.of("etag", result)); + EasyMock.expect( + storageRpcMock.read( + BLOB_INFO1.getBlobId().toPb(), BLOB_SOURCE_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) + .andReturn(StorageRpc.Tuple.of("etag", result)); EasyMock.replay(storageRpcMock); initializeService(); - ReadChannel channel = storage.reader(BLOB_INFO1.getBlobId(), - BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION); + ReadChannel channel = + storage.reader( + BLOB_INFO1.getBlobId(), + BLOB_SOURCE_GENERATION_FROM_BLOB_ID, + BLOB_SOURCE_METAGENERATION); assertNotNull(channel); assertTrue(channel.isOpen()); channel.read(ByteBuffer.allocate(42)); @@ -1206,8 +1335,14 @@ public void testWriterWithOptions() { .andReturn("upload-id"); EasyMock.replay(storageRpcMock); initializeService(); - WriteChannel channel = storage.writer(info, BLOB_WRITE_METAGENERATION, BLOB_WRITE_NOT_EXIST, - BLOB_WRITE_PREDEFINED_ACL, BLOB_WRITE_CRC2C, BLOB_WRITE_MD5_HASH); + WriteChannel channel = + storage.writer( + info, + BLOB_WRITE_METAGENERATION, + BLOB_WRITE_NOT_EXIST, + BLOB_WRITE_PREDEFINED_ACL, + BLOB_WRITE_CRC2C, + BLOB_WRITE_MD5_HASH); assertNotNull(channel); assertTrue(channel.isOpen()); } @@ -1216,7 +1351,8 @@ public void testWriterWithOptions() { public void testWriterWithEncryptionKey() { BlobInfo info = BLOB_INFO1.toBuilder().setMd5(null).setCrc32c(null).build(); EasyMock.expect(storageRpcMock.open(info.toPb(), ENCRYPTION_KEY_OPTIONS)) - .andReturn("upload-id").times(2); + .andReturn("upload-id") + .times(2); EasyMock.replay(storageRpcMock); initializeService(); WriteChannel channel = storage.writer(info, BlobWriteOption.encryptionKey(KEY)); @@ -1228,34 +1364,50 @@ public void testWriterWithEncryptionKey() { } @Test - public void testSignUrl() throws NoSuchAlgorithmException, InvalidKeyException, - SignatureException, UnsupportedEncodingException { + public void testSignUrl() + throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, + UnsupportedEncodingException { EasyMock.replay(storageRpcMock); ServiceAccountCredentials credentials = new ServiceAccountCredentials(null, ACCOUNT, privateKey, null, null); storage = options.toBuilder().setCredentials(credentials).build().getService(); URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS); String stringUrl = url.toString(); - String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1) - .append('/').append(BLOB_NAME1).append("?GoogleAccessId=").append(ACCOUNT) - .append("&Expires=").append(42L + 1209600).append("&Signature=").toString(); + String expectedUrl = + new StringBuilder("https://storage.googleapis.com/") + .append(BUCKET_NAME1) + .append('/') + .append(BLOB_NAME1) + .append("?GoogleAccessId=") + .append(ACCOUNT) + .append("&Expires=") + .append(42L + 1209600) + .append("&Signature=") + .toString(); assertTrue(stringUrl.startsWith(expectedUrl)); String signature = stringUrl.substring(expectedUrl.length()); StringBuilder signedMessageBuilder = new StringBuilder(); - signedMessageBuilder.append(HttpMethod.GET).append("\n\n\n").append(42L + 1209600).append("\n/") - .append(BUCKET_NAME1).append('/').append(BLOB_NAME1); + signedMessageBuilder + .append(HttpMethod.GET) + .append("\n\n\n") + .append(42L + 1209600) + .append("\n/") + .append(BUCKET_NAME1) + .append('/') + .append(BLOB_NAME1); Signature signer = Signature.getInstance("SHA256withRSA"); signer.initVerify(publicKey); signer.update(signedMessageBuilder.toString().getBytes(UTF_8)); - assertTrue(signer.verify(BaseEncoding.base64().decode( - URLDecoder.decode(signature, UTF_8.name())))); + assertTrue( + signer.verify(BaseEncoding.base64().decode(URLDecoder.decode(signature, UTF_8.name())))); } @Test - public void testSignUrlLeadingSlash() throws NoSuchAlgorithmException, InvalidKeyException, - SignatureException, UnsupportedEncodingException { + public void testSignUrlLeadingSlash() + throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, + UnsupportedEncodingException { String blobName = "/b1"; EasyMock.replay(storageRpcMock); ServiceAccountCredentials credentials = @@ -1265,59 +1417,97 @@ public void testSignUrlLeadingSlash() throws NoSuchAlgorithmException, InvalidKe storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS); String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName); String stringUrl = url.toString(); - String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1) - .append(escapedBlobName).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=") - .append(42L + 1209600).append("&Signature=").toString(); + String expectedUrl = + new StringBuilder("https://storage.googleapis.com/") + .append(BUCKET_NAME1) + .append(escapedBlobName) + .append("?GoogleAccessId=") + .append(ACCOUNT) + .append("&Expires=") + .append(42L + 1209600) + .append("&Signature=") + .toString(); assertTrue(stringUrl.startsWith(expectedUrl)); String signature = stringUrl.substring(expectedUrl.length()); StringBuilder signedMessageBuilder = new StringBuilder(); - signedMessageBuilder.append(HttpMethod.GET).append("\n\n\n").append(42L + 1209600).append("\n/") - .append(BUCKET_NAME1).append(escapedBlobName); + signedMessageBuilder + .append(HttpMethod.GET) + .append("\n\n\n") + .append(42L + 1209600) + .append("\n/") + .append(BUCKET_NAME1) + .append(escapedBlobName); Signature signer = Signature.getInstance("SHA256withRSA"); signer.initVerify(publicKey); signer.update(signedMessageBuilder.toString().getBytes(UTF_8)); - assertTrue(signer.verify(BaseEncoding.base64().decode( - URLDecoder.decode(signature, UTF_8.name())))); + assertTrue( + signer.verify(BaseEncoding.base64().decode(URLDecoder.decode(signature, UTF_8.name())))); } @Test - public void testSignUrlWithOptions() throws NoSuchAlgorithmException, InvalidKeyException, - SignatureException, UnsupportedEncodingException { + public void testSignUrlWithOptions() + throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, + UnsupportedEncodingException { EasyMock.replay(storageRpcMock); ServiceAccountCredentials credentials = new ServiceAccountCredentials(null, ACCOUNT, privateKey, null, null); storage = options.toBuilder().setCredentials(credentials).build().getService(); - URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS, - Storage.SignUrlOption.httpMethod(HttpMethod.POST), Storage.SignUrlOption.withContentType(), - Storage.SignUrlOption.withMd5()); + URL url = + storage.signUrl( + BLOB_INFO1, + 14, + TimeUnit.DAYS, + Storage.SignUrlOption.httpMethod(HttpMethod.POST), + Storage.SignUrlOption.withContentType(), + Storage.SignUrlOption.withMd5()); String stringUrl = url.toString(); - String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1) - .append('/').append(BLOB_NAME1).append("?GoogleAccessId=").append(ACCOUNT) - .append("&Expires=").append(42L + 1209600).append("&Signature=").toString(); + String expectedUrl = + new StringBuilder("https://storage.googleapis.com/") + .append(BUCKET_NAME1) + .append('/') + .append(BLOB_NAME1) + .append("?GoogleAccessId=") + .append(ACCOUNT) + .append("&Expires=") + .append(42L + 1209600) + .append("&Signature=") + .toString(); assertTrue(stringUrl.startsWith(expectedUrl)); String signature = stringUrl.substring(expectedUrl.length()); StringBuilder signedMessageBuilder = new StringBuilder(); - signedMessageBuilder.append(HttpMethod.POST).append('\n').append(BLOB_INFO1.getMd5()) - .append('\n').append(BLOB_INFO1.getContentType()).append('\n').append(42L + 1209600) - .append("\n/").append(BUCKET_NAME1).append('/').append(BLOB_NAME1); + signedMessageBuilder + .append(HttpMethod.POST) + .append('\n') + .append(BLOB_INFO1.getMd5()) + .append('\n') + .append(BLOB_INFO1.getContentType()) + .append('\n') + .append(42L + 1209600) + .append("\n/") + .append(BUCKET_NAME1) + .append('/') + .append(BLOB_NAME1); Signature signer = Signature.getInstance("SHA256withRSA"); signer.initVerify(publicKey); signer.update(signedMessageBuilder.toString().getBytes(UTF_8)); - assertTrue(signer.verify(BaseEncoding.base64().decode( - URLDecoder.decode(signature, UTF_8.name())))); + assertTrue( + signer.verify(BaseEncoding.base64().decode(URLDecoder.decode(signature, UTF_8.name())))); } @Test - public void testSignUrlForBlobWithSpecialChars() throws NoSuchAlgorithmException, - InvalidKeyException, SignatureException, UnsupportedEncodingException { + public void testSignUrlForBlobWithSpecialChars() + throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, + UnsupportedEncodingException { // List of chars under test were taken from // https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters char[] specialChars = - new char[]{'!','#','$','&','\'','(',')','*','+',',',':',';','=','?','@','[',']'}; + new char[] { + '!', '#', '$', '&', '\'', '(', ')', '*', '+', ',', ':', ';', '=', '?', '@', '[', ']' + }; EasyMock.replay(storageRpcMock); ServiceAccountCredentials credentials = new ServiceAccountCredentials(null, ACCOUNT, privateKey, null, null); @@ -1330,27 +1520,40 @@ public void testSignUrlForBlobWithSpecialChars() throws NoSuchAlgorithmException String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName).replace("?", "%3F"); String stringUrl = url.toString(); - String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1) - .append(escapedBlobName).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=") - .append(42L + 1209600).append("&Signature=").toString(); + String expectedUrl = + new StringBuilder("https://storage.googleapis.com/") + .append(BUCKET_NAME1) + .append(escapedBlobName) + .append("?GoogleAccessId=") + .append(ACCOUNT) + .append("&Expires=") + .append(42L + 1209600) + .append("&Signature=") + .toString(); assertTrue(stringUrl.startsWith(expectedUrl)); String signature = stringUrl.substring(expectedUrl.length()); StringBuilder signedMessageBuilder = new StringBuilder(); - signedMessageBuilder.append(HttpMethod.GET).append("\n\n\n").append(42L + 1209600) - .append("\n/").append(BUCKET_NAME1).append(escapedBlobName); + signedMessageBuilder + .append(HttpMethod.GET) + .append("\n\n\n") + .append(42L + 1209600) + .append("\n/") + .append(BUCKET_NAME1) + .append(escapedBlobName); Signature signer = Signature.getInstance("SHA256withRSA"); signer.initVerify(publicKey); signer.update(signedMessageBuilder.toString().getBytes(UTF_8)); - assertTrue(signer.verify(BaseEncoding.base64().decode( - URLDecoder.decode(signature, UTF_8.name())))); + assertTrue( + signer.verify(BaseEncoding.base64().decode(URLDecoder.decode(signature, UTF_8.name())))); } } @Test - public void testSignUrlForBlobWithSlashes() throws NoSuchAlgorithmException, - InvalidKeyException, SignatureException, UnsupportedEncodingException { + public void testSignUrlForBlobWithSlashes() + throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, + UnsupportedEncodingException { EasyMock.replay(storageRpcMock); ServiceAccountCredentials credentials = new ServiceAccountCredentials(null, ACCOUNT, privateKey, null, null); @@ -1358,24 +1561,36 @@ public void testSignUrlForBlobWithSlashes() throws NoSuchAlgorithmException, String blobName = "/foo/bar/baz #%20other cool stuff.txt"; URL url = - storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS); + storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS); String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName); String stringUrl = url.toString(); - String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1) - .append(escapedBlobName).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=") - .append(42L + 1209600).append("&Signature=").toString(); + String expectedUrl = + new StringBuilder("https://storage.googleapis.com/") + .append(BUCKET_NAME1) + .append(escapedBlobName) + .append("?GoogleAccessId=") + .append(ACCOUNT) + .append("&Expires=") + .append(42L + 1209600) + .append("&Signature=") + .toString(); assertTrue(stringUrl.startsWith(expectedUrl)); String signature = stringUrl.substring(expectedUrl.length()); StringBuilder signedMessageBuilder = new StringBuilder(); - signedMessageBuilder.append(HttpMethod.GET).append("\n\n\n").append(42L + 1209600) - .append("\n/").append(BUCKET_NAME1).append(escapedBlobName); + signedMessageBuilder + .append(HttpMethod.GET) + .append("\n\n\n") + .append(42L + 1209600) + .append("\n/") + .append(BUCKET_NAME1) + .append(escapedBlobName); Signature signer = Signature.getInstance("SHA256withRSA"); signer.initVerify(publicKey); signer.update(signedMessageBuilder.toString().getBytes(UTF_8)); - assertTrue(signer.verify(BaseEncoding.base64().decode( - URLDecoder.decode(signature, UTF_8.name())))); + assertTrue( + signer.verify(BaseEncoding.base64().decode(URLDecoder.decode(signature, UTF_8.name())))); } @Test @@ -1385,9 +1600,13 @@ public void testGetAllArray() { RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture> callback1 = Capture.newInstance(); Capture> callback2 = Capture.newInstance(); - batchMock.addGet(EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), + batchMock.addGet( + EasyMock.eq(blobId1.toPb()), + EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.of())); - batchMock.addGet(EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), + batchMock.addGet( + EasyMock.eq(blobId2.toPb()), + EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); @@ -1409,9 +1628,13 @@ public void testGetAllArrayIterable() { RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture> callback1 = Capture.newInstance(); Capture> callback2 = Capture.newInstance(); - batchMock.addGet(EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), + batchMock.addGet( + EasyMock.eq(blobId1.toPb()), + EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.of())); - batchMock.addGet(EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), + batchMock.addGet( + EasyMock.eq(blobId2.toPb()), + EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); @@ -1433,9 +1656,13 @@ public void testDeleteAllArray() { RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture> callback1 = Capture.newInstance(); Capture> callback2 = Capture.newInstance(); - batchMock.addDelete(EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), + batchMock.addDelete( + EasyMock.eq(blobId1.toPb()), + EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.of())); - batchMock.addDelete(EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), + batchMock.addDelete( + EasyMock.eq(blobId2.toPb()), + EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); @@ -1457,9 +1684,13 @@ public void testDeleteAllIterable() { RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture> callback1 = Capture.newInstance(); Capture> callback2 = Capture.newInstance(); - batchMock.addDelete(EasyMock.eq(blobId1.toPb()), EasyMock.capture(callback1), + batchMock.addDelete( + EasyMock.eq(blobId1.toPb()), + EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.of())); - batchMock.addDelete(EasyMock.eq(blobId2.toPb()), EasyMock.capture(callback2), + batchMock.addDelete( + EasyMock.eq(blobId2.toPb()), + EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); @@ -1479,9 +1710,13 @@ public void testUpdateAllArray() { RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture> callback1 = Capture.newInstance(); Capture> callback2 = Capture.newInstance(); - batchMock.addPatch(EasyMock.eq(BLOB_INFO1.toPb()), EasyMock.capture(callback1), + batchMock.addPatch( + EasyMock.eq(BLOB_INFO1.toPb()), + EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.of())); - batchMock.addPatch(EasyMock.eq(BLOB_INFO2.toPb()), EasyMock.capture(callback2), + batchMock.addPatch( + EasyMock.eq(BLOB_INFO2.toPb()), + EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); @@ -1501,9 +1736,13 @@ public void testUpdateAllIterable() { RpcBatch batchMock = EasyMock.createMock(RpcBatch.class); Capture> callback1 = Capture.newInstance(); Capture> callback2 = Capture.newInstance(); - batchMock.addPatch(EasyMock.eq(BLOB_INFO1.toPb()), EasyMock.capture(callback1), + batchMock.addPatch( + EasyMock.eq(BLOB_INFO1.toPb()), + EasyMock.capture(callback1), EasyMock.eq(ImmutableMap.of())); - batchMock.addPatch(EasyMock.eq(BLOB_INFO2.toPb()), EasyMock.capture(callback2), + batchMock.addPatch( + EasyMock.eq(BLOB_INFO2.toPb()), + EasyMock.capture(callback2), EasyMock.eq(ImmutableMap.of())); EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock); batchMock.submit(); @@ -1662,7 +1901,7 @@ public void testGetBlobAclNull() { public void testDeleteBlobAcl() { BlobId blobId = BlobId.of(BUCKET_NAME1, BLOB_NAME1, 42L); EasyMock.expect( - storageRpcMock.deleteAcl(BUCKET_NAME1, BLOB_NAME1, 42L, "allAuthenticatedUsers")) + storageRpcMock.deleteAcl(BUCKET_NAME1, BLOB_NAME1, 42L, "allAuthenticatedUsers")) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); @@ -1673,10 +1912,9 @@ public void testDeleteBlobAcl() { public void testCreateBlobAcl() { BlobId blobId = BlobId.of(BUCKET_NAME1, BLOB_NAME1, 42L); Acl returnedAcl = ACL.toBuilder().setEtag("ETAG").setId("ID").build(); - EasyMock.expect(storageRpcMock.createAcl(ACL.toObjectPb() - .setBucket(BUCKET_NAME1) - .setObject(BLOB_NAME1) - .setGeneration(42L))) + EasyMock.expect( + storageRpcMock.createAcl( + ACL.toObjectPb().setBucket(BUCKET_NAME1).setObject(BLOB_NAME1).setGeneration(42L))) .andReturn(returnedAcl.toObjectPb()); EasyMock.replay(storageRpcMock); initializeService(); @@ -1688,10 +1926,9 @@ public void testCreateBlobAcl() { public void testUpdateBlobAcl() { BlobId blobId = BlobId.of(BUCKET_NAME1, BLOB_NAME1, 42L); Acl returnedAcl = ACL.toBuilder().setEtag("ETAG").setId("ID").build(); - EasyMock.expect(storageRpcMock.patchAcl(ACL.toObjectPb() - .setBucket(BUCKET_NAME1) - .setObject(BLOB_NAME1) - .setGeneration(42L))) + EasyMock.expect( + storageRpcMock.patchAcl( + ACL.toObjectPb().setBucket(BUCKET_NAME1).setObject(BLOB_NAME1).setGeneration(42L))) .andReturn(returnedAcl.toObjectPb()); EasyMock.replay(storageRpcMock); initializeService(); @@ -1718,7 +1955,11 @@ public void testRetryableException() { .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); storage = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options + .toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) + .build() + .getService(); initializeServiceDependentObjects(); Blob readBlob = storage.get(blob); assertEquals(expectedBlob1, readBlob); @@ -1732,7 +1973,11 @@ public void testNonRetryableException() { .andThrow(new StorageException(501, exceptionMessage)); EasyMock.replay(storageRpcMock); storage = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options + .toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) + .build() + .getService(); initializeServiceDependentObjects(); thrown.expect(StorageException.class); thrown.expectMessage(exceptionMessage); @@ -1747,7 +1992,11 @@ public void testRuntimeException() { .andThrow(new RuntimeException(exceptionMessage)); EasyMock.replay(storageRpcMock); storage = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options + .toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) + .build() + .getService(); thrown.expect(StorageException.class); thrown.expectMessage(exceptionMessage); storage.get(blob); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/testing/RemoteStorageHelperTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/testing/RemoteStorageHelperTest.java index 04b54a5ae59a..b3137222a3ae 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/testing/RemoteStorageHelperTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/testing/RemoteStorageHelperTest.java @@ -30,6 +30,7 @@ import com.google.common.collect.ImmutableList; import org.easymock.EasyMock; +import org.joda.time.Duration; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -210,10 +211,9 @@ public void testCreateFromStream() { assertEquals(PROJECT_ID, options.getProjectId()); assertEquals(60000, options.getConnectTimeout()); assertEquals(60000, options.getReadTimeout()); - assertEquals(10, options.getRetryParams().getRetryMaxAttempts()); - assertEquals(6, options.getRetryParams().getRetryMinAttempts()); - assertEquals(30000, options.getRetryParams().getMaxRetryDelayMillis()); - assertEquals(120000, options.getRetryParams().getTotalRetryPeriodMillis()); - assertEquals(250, options.getRetryParams().getInitialRetryDelayMillis()); + assertEquals(10, options.getRetrySettings().getMaxAttempts()); + assertEquals(Duration.millis(30000), options.getRetrySettings().getMaxRetryDelay()); + assertEquals(Duration.millis(120000), options.getRetrySettings().getTotalTimeout()); + assertEquals(Duration.millis(250), options.getRetrySettings().getInitialRetryDelay()); } } diff --git a/google-cloud-translate/src/main/java/com/google/cloud/translate/TranslateException.java b/google-cloud-translate/src/main/java/com/google/cloud/translate/TranslateException.java index b3e2a49880fe..de176255add9 100644 --- a/google-cloud-translate/src/main/java/com/google/cloud/translate/TranslateException.java +++ b/google-cloud-translate/src/main/java/com/google/cloud/translate/TranslateException.java @@ -18,7 +18,6 @@ import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; -import com.google.cloud.RetryHelper.RetryInterruptedException; import com.google.common.collect.ImmutableSet; import java.io.IOException; @@ -54,10 +53,9 @@ protected Set getRetryableErrors() { * will always throw an exception. * * @throws TranslateException when {@code ex} was caused by a {@code TranslateException} - * @throws RetryInterruptedException when {@code ex} is a {@code RetryInterruptedException} */ static BaseServiceException translateAndThrow(RetryHelperException ex) { - BaseServiceException.translateAndPropagateIfPossible(ex); + BaseServiceException.translate(ex); throw new TranslateException(UNKNOWN_CODE, ex.getMessage(), ex.getCause()); } } diff --git a/google-cloud-translate/src/main/java/com/google/cloud/translate/TranslateImpl.java b/google-cloud-translate/src/main/java/com/google/cloud/translate/TranslateImpl.java index b35293e9234d..21061299ead3 100644 --- a/google-cloud-translate/src/main/java/com/google/cloud/translate/TranslateImpl.java +++ b/google-cloud-translate/src/main/java/com/google/cloud/translate/TranslateImpl.java @@ -62,7 +62,7 @@ public List listSupportedLanguages(final LanguageListOption... options public List call() { return translateRpc.listSupportedLanguages(optionMap(options)); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()), + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()), Language.FROM_PB_FUNCTION); } catch (RetryHelperException e) { throw TranslateException.translateAndThrow(e); @@ -78,7 +78,7 @@ public List detect(final List texts) { public List> call() { return translateRpc.detect(texts); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()); + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); Iterator> detectionIterator = detectionsPb.iterator(); Iterator textIterator = texts.iterator(); while (detectionIterator.hasNext() && textIterator.hasNext()) { @@ -112,7 +112,7 @@ public List translate(final List texts, final TranslateOpti public List call() { return translateRpc.translate(texts, optionMap(options)); } - }, getOptions().getRetryParams(), EXCEPTION_HANDLER, getOptions().getClock()), + }, getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()), Translation.FROM_PB_FUNCTION); } catch (RetryHelperException e) { throw TranslateException.translateAndThrow(e); diff --git a/google-cloud-translate/src/main/java/com/google/cloud/translate/testing/RemoteTranslateHelper.java b/google-cloud-translate/src/main/java/com/google/cloud/translate/testing/RemoteTranslateHelper.java index 5938f526e5ea..7ca246367ebc 100644 --- a/google-cloud-translate/src/main/java/com/google/cloud/translate/testing/RemoteTranslateHelper.java +++ b/google-cloud-translate/src/main/java/com/google/cloud/translate/testing/RemoteTranslateHelper.java @@ -16,17 +16,18 @@ package com.google.cloud.translate.testing; -import com.google.cloud.RetryParams; +import com.google.api.gax.core.RetrySettings; import com.google.cloud.translate.TranslateOptions; +import org.joda.time.Duration; /** * Utility to create a remote translate configuration for testing. Translate options can be obtained * via the {@link #getOptions()} ()} method. Returned options have custom - * {@link TranslateOptions#getRetryParams()}: {@link RetryParams#getRetryMaxAttempts()} is - * {@code 10}, {@link RetryParams#getRetryMinAttempts()} is {@code 6}, - * {@link RetryParams#getMaxRetryDelayMillis()} is {@code 30000}, - * {@link RetryParams#getTotalRetryPeriodMillis()} is {@code 120000} and - * {@link RetryParams#getInitialRetryDelayMillis()} is {@code 250}. + * {@link TranslateOptions#getRetrySettings()}: {@link RetrySettings#getMaxAttempts()} is + * {@code 10}, + * {@link RetrySettings#getMaxRetryDelay()} is {@code 30000}, + * {@link RetrySettings#getTotalTimeout()} is {@code 120000} and + * {@link RetrySettings#getInitialRetryDelay()} is {@code 250}. * {@link TranslateOptions#getConnectTimeout()} and {@link TranslateOptions#getReadTimeout()} are * both set to {@code 60000}. */ @@ -54,7 +55,7 @@ public TranslateOptions getOptions() { public static RemoteTranslateHelper create(String apiKey) { TranslateOptions translateOptions = TranslateOptions.newBuilder() .setApiKey(apiKey) - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setConnectTimeout(60000) .setReadTimeout(60000) .build(); @@ -66,20 +67,22 @@ public static RemoteTranslateHelper create(String apiKey) { */ public static RemoteTranslateHelper create() { TranslateOptions translateOption = TranslateOptions.newBuilder() - .setRetryParams(retryParams()) + .setRetrySettings(retryParams()) .setConnectTimeout(60000) .setReadTimeout(60000) .build(); return new RemoteTranslateHelper(translateOption); } - private static RetryParams retryParams() { - return RetryParams.newBuilder() - .setRetryMaxAttempts(10) - .setRetryMinAttempts(6) - .setMaxRetryDelayMillis(30000) - .setTotalRetryPeriodMillis(120000) - .setInitialRetryDelayMillis(250) + private static RetrySettings retryParams() { + return RetrySettings.newBuilder().setMaxAttempts(10) + .setMaxRetryDelay(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(120000L)) + .setInitialRetryDelay(Duration.millis(250L)) + .setRetryDelayMultiplier(1.0) + .setInitialRpcTimeout(Duration.millis(120000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.millis(120000L)) .build(); } } diff --git a/google-cloud-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java b/google-cloud-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java index 7e7aa0e0df17..1ab0991c757c 100644 --- a/google-cloud-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java +++ b/google-cloud-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java @@ -22,7 +22,8 @@ import com.google.api.services.translate.model.DetectionsResourceItems; import com.google.api.services.translate.model.LanguagesResource; import com.google.api.services.translate.model.TranslationsResource; -import com.google.cloud.RetryParams; +import com.google.api.gax.core.RetrySettings; +import com.google.cloud.ServiceOptions; import com.google.cloud.translate.Translate.LanguageListOption; import com.google.cloud.translate.Translate.TranslateOption; import com.google.cloud.translate.spi.TranslateRpc; @@ -78,8 +79,8 @@ public class TranslateImplTest { // Language list options private static final LanguageListOption LANGUAGE_LIST_OPTION = LanguageListOption.targetLanguage(TARGET_LANGUAGE); - private static final Map LANGUAGE_LIST_OPTIONS = ImmutableMap.of( - TranslateRpc.Option.TARGET_LANGUAGE, LANGUAGE_LIST_OPTION.getValue()); + private static final Map LANGUAGE_LIST_OPTIONS = + ImmutableMap.of(TranslateRpc.Option.TARGET_LANGUAGE, LANGUAGE_LIST_OPTION.getValue()); // Translate options private static final TranslateOption TARGET_LANGUAGE_OPTION = @@ -87,18 +88,19 @@ public class TranslateImplTest { private static final TranslateOption SOURCE_LANGUAGE_OPTION = TranslateOption.sourceLanguage("de"); private static final TranslateOption MODEL_OPTION = TranslateOption.model("nmt"); - private static final Map TRANSLATE_OPTIONS = ImmutableMap.of( - TranslateRpc.Option.TARGET_LANGUAGE, TARGET_LANGUAGE_OPTION.getValue(), - TranslateRpc.Option.SOURCE_LANGUAGE, SOURCE_LANGUAGE_OPTION.getValue(), - TranslateRpc.Option.MODEL, "nmt"); + private static final Map TRANSLATE_OPTIONS = + ImmutableMap.of( + TranslateRpc.Option.TARGET_LANGUAGE, TARGET_LANGUAGE_OPTION.getValue(), + TranslateRpc.Option.SOURCE_LANGUAGE, SOURCE_LANGUAGE_OPTION.getValue(), + TranslateRpc.Option.MODEL, "nmt"); + private static final RetrySettings NO_RETRY_SETTINGS = ServiceOptions.getNoRetrySettings(); private TranslateOptions options; private TranslateRpcFactory rpcFactoryMock; private TranslateRpc translateRpcMock; private Translate translate; - @Rule - public ExpectedException thrown = ExpectedException.none(); + @Rule public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() { @@ -107,11 +109,12 @@ public void setUp() { EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(TranslateOptions.class))) .andReturn(translateRpcMock); EasyMock.replay(rpcFactoryMock); - options = TranslateOptions.newBuilder() - .setApiKey(API_KEY) - .setServiceRpcFactory(rpcFactoryMock) - .setRetryParams(RetryParams.noRetries()) - .build(); + options = + TranslateOptions.newBuilder() + .setApiKey(API_KEY) + .setServiceRpcFactory(rpcFactoryMock) + .setRetrySettings(NO_RETRY_SETTINGS) + .build(); } @After @@ -145,8 +148,9 @@ public void testListSupportedLanguagesWithOptions() { .andReturn(ImmutableList.of(LANGUAGE3_PB, LANGUAGE4_PB)); EasyMock.replay(translateRpcMock); initializeService(); - assertEquals(LANGUAGES2, translate.listSupportedLanguages( - LanguageListOption.targetLanguage(TARGET_LANGUAGE))); + assertEquals( + LANGUAGES2, + translate.listSupportedLanguages(LanguageListOption.targetLanguage(TARGET_LANGUAGE))); } @Test @@ -164,8 +168,9 @@ public void testDetect() { public void testDetectMultipleDetections() { String text = "text"; EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text))) - .andReturn(ImmutableList.>of( - ImmutableList.of(DETECTION1_PB, DETECTION2_PB))); + .andReturn( + ImmutableList.>of( + ImmutableList.of(DETECTION1_PB, DETECTION2_PB))); EasyMock.replay(translateRpcMock); initializeService(); thrown.expect(IllegalStateException.class); @@ -177,8 +182,9 @@ public void testDetectMultipleDetections() { public void testDetectNoDetection() { String text = "text"; EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text))) - .andReturn(ImmutableList.>of( - ImmutableList.of())); + .andReturn( + ImmutableList.>of( + ImmutableList.of())); EasyMock.replay(translateRpcMock); initializeService(); thrown.expect(IllegalStateException.class); @@ -192,8 +198,9 @@ public void testDetectList() { String text2 = "other text"; List texts = ImmutableList.of(text1, text2); EasyMock.expect(translateRpcMock.detect(texts)) - .andReturn(ImmutableList.>of( - ImmutableList.of(DETECTION1_PB), ImmutableList.of(DETECTION2_PB))); + .andReturn( + ImmutableList.>of( + ImmutableList.of(DETECTION1_PB), ImmutableList.of(DETECTION2_PB))); EasyMock.replay(translateRpcMock); initializeService(); assertEquals(ImmutableList.of(DETECTION1, DETECTION2), translate.detect(texts)); @@ -205,8 +212,9 @@ public void testDetectListMultipleDetections() { String text2 = "other text"; List texts = ImmutableList.of(text1, text2); EasyMock.expect(translateRpcMock.detect(texts)) - .andReturn(ImmutableList.>of( - ImmutableList.of(DETECTION1_PB, DETECTION2_PB), ImmutableList.of(DETECTION1_PB))); + .andReturn( + ImmutableList.>of( + ImmutableList.of(DETECTION1_PB, DETECTION2_PB), ImmutableList.of(DETECTION1_PB))); EasyMock.replay(translateRpcMock); initializeService(); thrown.expect(IllegalStateException.class); @@ -220,8 +228,9 @@ public void testDetectListNoDetection() { String text2 = "other text"; List texts = ImmutableList.of(text1, text2); EasyMock.expect(translateRpcMock.detect(texts)) - .andReturn(ImmutableList.>of( - ImmutableList.of(DETECTION1_PB), ImmutableList.of())); + .andReturn( + ImmutableList.>of( + ImmutableList.of(DETECTION1_PB), ImmutableList.of())); EasyMock.replay(translateRpcMock); initializeService(); thrown.expect(IllegalStateException.class); @@ -234,8 +243,9 @@ public void testDetectVararg() { String text1 = "text"; String text2 = "other text"; EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text1, text2))) - .andReturn(ImmutableList.>of( - ImmutableList.of(DETECTION1_PB), ImmutableList.of(DETECTION2_PB))); + .andReturn( + ImmutableList.>of( + ImmutableList.of(DETECTION1_PB), ImmutableList.of(DETECTION2_PB))); EasyMock.replay(translateRpcMock); initializeService(); assertEquals(ImmutableList.of(DETECTION1, DETECTION2), translate.detect(text1, text2)); @@ -246,8 +256,9 @@ public void testDetectVarargMultipleDetections() { String text1 = "text"; String text2 = "other text"; EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text1, text2))) - .andReturn(ImmutableList.>of( - ImmutableList.of(DETECTION1_PB, DETECTION2_PB), ImmutableList.of(DETECTION1_PB))); + .andReturn( + ImmutableList.>of( + ImmutableList.of(DETECTION1_PB, DETECTION2_PB), ImmutableList.of(DETECTION1_PB))); EasyMock.replay(translateRpcMock); initializeService(); thrown.expect(IllegalStateException.class); @@ -260,8 +271,9 @@ public void testDetectVarargNoDetection() { String text1 = "text"; String text2 = "other text"; EasyMock.expect(translateRpcMock.detect(ImmutableList.of(text1, text2))) - .andReturn(ImmutableList.>of( - ImmutableList.of(DETECTION1_PB), ImmutableList.of())); + .andReturn( + ImmutableList.>of( + ImmutableList.of(DETECTION1_PB), ImmutableList.of())); EasyMock.replay(translateRpcMock); initializeService(); thrown.expect(IllegalStateException.class); @@ -286,7 +298,8 @@ public void testTranslateWithOptions() { .andReturn(ImmutableList.of(TRANSLATION2_PB)); EasyMock.replay(translateRpcMock); initializeService(); - assertEquals(TRANSLATION2, + assertEquals( + TRANSLATION2, translate.translate(text, TARGET_LANGUAGE_OPTION, SOURCE_LANGUAGE_OPTION, MODEL_OPTION)); } @@ -310,7 +323,8 @@ public void testTranslateListWithOptions() { .andReturn(ImmutableList.of(TRANSLATION2_PB)); EasyMock.replay(translateRpcMock); initializeService(); - assertEquals(ImmutableList.of(TRANSLATION2), + assertEquals( + ImmutableList.of(TRANSLATION2), translate.translate(texts, TARGET_LANGUAGE_OPTION, SOURCE_LANGUAGE_OPTION, MODEL_OPTION)); } @@ -321,7 +335,11 @@ public void testRetryableException() { .andReturn(ImmutableList.of(LANGUAGE1_PB, LANGUAGE2_PB)); EasyMock.replay(translateRpcMock); translate = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options + .toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) + .build() + .getService(); assertEquals(LANGUAGES1, translate.listSupportedLanguages()); } @@ -332,7 +350,11 @@ public void testNonRetryableException() { .andThrow(new TranslateException(501, exceptionMessage)); EasyMock.replay(translateRpcMock); translate = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options + .toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) + .build() + .getService(); thrown.expect(TranslateException.class); thrown.expectMessage(exceptionMessage); translate.listSupportedLanguages(); @@ -345,7 +367,11 @@ public void testRuntimeException() { .andThrow(new RuntimeException(exceptionMessage)); EasyMock.replay(translateRpcMock); translate = - options.toBuilder().setRetryParams(RetryParams.getDefaultInstance()).build().getService(); + options + .toBuilder() + .setRetrySettings(ServiceOptions.getDefaultRetrySettings()) + .build() + .getService(); thrown.expect(TranslateException.class); thrown.expectMessage(exceptionMessage); translate.listSupportedLanguages(); diff --git a/pom.xml b/pom.xml index 207164f29494..9b00a6335fd5 100644 --- a/pom.xml +++ b/pom.xml @@ -92,7 +92,7 @@ github 0.6.0 1.0.3 - 0.3.0 + 0.3.2-SNAPSHOT 0.1.5 0.9.5-alpha-SNAPSHOT 0.9.5-beta-SNAPSHOT From 45a1e66082fc46fbf32a95d6618c9586cba2fe36 Mon Sep 17 00:00:00 2001 From: vam Date: Fri, 17 Mar 2017 11:44:42 -0700 Subject: [PATCH 2/5] After merge fixes. --- .../main/java/com/google/cloud/ExceptionHandler.java | 10 ---------- .../java/com/google/cloud/GrpcTransportOptions.java | 5 ++--- .../google/cloud/logging/spi/v2/GrpcLoggingRpc.java | 2 +- .../cloud/pubsub/deprecated/spi/v1/GrpcPubSubRpc.java | 2 +- pom.xml | 2 +- 5 files changed, 5 insertions(+), 16 deletions(-) diff --git a/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java b/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java index c455641090ad..4aac01227cf2 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java @@ -233,16 +233,6 @@ void verifyCaller(Callable callable) { } } - -// public Set> getRetriableExceptions() { -// return retriableExceptions; -// } -// -// -// public Set> getNonRetriableExceptions() { -// return nonRetriableExceptions; -// } - boolean shouldRetry(Exception ex) { for (Interceptor interceptor : interceptors) { Interceptor.RetryResult retryResult = checkNotNull(interceptor.beforeEval(ex)); diff --git a/google-cloud-core/src/main/java/com/google/cloud/GrpcTransportOptions.java b/google-cloud-core/src/main/java/com/google/cloud/GrpcTransportOptions.java index 9be530a247ef..0295b00972c9 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/GrpcTransportOptions.java +++ b/google-cloud-core/src/main/java/com/google/cloud/GrpcTransportOptions.java @@ -36,7 +36,6 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import org.joda.time.Duration; /** * Class representing service options for those services that use gRPC as the transport @@ -206,8 +205,8 @@ public ExecutorFactory getExecutorFactory() { /** * Returns a builder for API call settings. */ - public UnaryCallSettings.Builder getApiCallSettings() { - return UnaryCallSettings.newBuilder().setRetrySettingsBuilder(getRetrySettings().toBuilder()); + public UnaryCallSettings.Builder getApiCallSettings(RetrySettings retrySettings) { + return UnaryCallSettings.newBuilder().setRetrySettingsBuilder(retrySettings.toBuilder()); } /** diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java index 6e19c420aa4b..31f5c4ef2221 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/spi/v2/GrpcLoggingRpc.java @@ -97,7 +97,7 @@ public GrpcLoggingRpc(LoggingOptions options) throws IOException { .setExecutorProvider(executorProvider) .build(); UnaryCallSettings.Builder callSettingsBuilder = transportOptions - .getApiCallSettings(options.getRetryParams()); + .getApiCallSettings(options.getRetrySettings()); ConfigSettings.Builder confBuilder = ConfigSettings.defaultBuilder() .setExecutorProvider(providerManager) diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/spi/v1/GrpcPubSubRpc.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/spi/v1/GrpcPubSubRpc.java index eccf14119e9d..1773659381ca 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/spi/v1/GrpcPubSubRpc.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/spi/v1/GrpcPubSubRpc.java @@ -104,7 +104,7 @@ public GrpcPubSubRpc(PubSubOptions options) throws IOException { .setExecutorProvider(executorProvider) .build(); UnaryCallSettings.Builder callSettingsBuilder = transportOptions - .getApiCallSettings(options.getRetryParams()); + .getApiCallSettings(options.getRetrySettings()); PublisherSettings.Builder pubBuilder = PublisherSettings.defaultBuilder() .setExecutorProvider(providerManager) .setChannelProvider(providerManager) diff --git a/pom.xml b/pom.xml index 044c7e32db8b..e0d136ab5bb1 100644 --- a/pom.xml +++ b/pom.xml @@ -92,7 +92,7 @@ github 0.6.0 1.0.3 - 0.4.0 + 0.3.2-SNAPSHOT 0.1.5 0.10.1-alpha-SNAPSHOT 0.10.1-beta-SNAPSHOT From f64baa1100f8572781c4e740d89bd05db0753ce2 Mon Sep 17 00:00:00 2001 From: vam Date: Mon, 20 Mar 2017 12:01:19 -0700 Subject: [PATCH 3/5] Integrated recent changes in gax-java (caused by code review comments and NanoClock -> ApiClock renaming). --- .../java/com/google/cloud/bigquery/Job.java | 4 +- .../com/google/cloud/bigquery/JobTest.java | 14 +++--- .../com/google/cloud/compute/Operation.java | 4 +- .../google/cloud/compute/OperationTest.java | 14 +++--- .../com/google/cloud/ExceptionHandler.java | 17 ++++++- .../java/com/google/cloud/RetryHelper.java | 43 ++++++---------- .../java/com/google/cloud/ServiceOptions.java | 16 +++--- .../google/cloud/ExceptionHandlerTest.java | 50 +++++++++---------- .../com/google/cloud/ServiceOptionsTest.java | 10 ++-- .../com/google/cloud/dns/DnsImplTest.java | 4 +- .../pubsub/deprecated/AckDeadlineRenewer.java | 4 +- .../pubsub/spi/v1/MessageDispatcher.java | 6 +-- .../spi/v1/PollingSubscriberConnection.java | 4 +- .../spi/v1/StreamingSubscriberConnection.java | 4 +- .../cloud/pubsub/spi/v1/Subscriber.java | 12 ++--- .../google/cloud/pubsub/spi/v1/FakeClock.java | 4 +- .../spi/v1/FakeScheduledExecutorService.java | 4 +- .../com/google/cloud/spanner/Operation.java | 18 +++---- .../google/cloud/spanner/OperationTest.java | 4 +- .../google/cloud/storage/StorageImplTest.java | 6 +-- 20 files changed, 122 insertions(+), 120 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java index 5d254c4fabd1..59f02f2ffd92 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java @@ -18,7 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.cloud.WaitForOption; import com.google.cloud.WaitForOption.CheckingPeriod; import com.google.cloud.WaitForOption.Timeout; @@ -209,7 +209,7 @@ public Job waitFor(WaitForOption... waitOptions) throws InterruptedException, Ti Timeout timeout = Timeout.getOrDefault(waitOptions); CheckingPeriod checkingPeriod = CheckingPeriod.getOrDefault(waitOptions); long timeoutMillis = timeout.getTimeoutMillis(); - NanoClock clock = options.getClock(); + ApiClock clock = options.getClock(); long startTime = clock.millisTime(); while (!isDone()) { if (timeoutMillis != -1 && (clock.millisTime() - startTime) >= timeoutMillis) { diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java index adcd62b5eb71..28caf7bcf7ba 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/JobTest.java @@ -27,8 +27,8 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -import com.google.api.gax.core.NanoClock; -import com.google.api.gax.core.SystemClock; +import com.google.api.gax.core.ApiClock; +import com.google.api.gax.core.CurrentMillisClock; import com.google.cloud.WaitForOption; import com.google.cloud.bigquery.JobStatistics.CopyStatistics; @@ -197,7 +197,7 @@ public void testWaitFor() throws InterruptedException, TimeoutException { JobStatus status = createStrictMock(JobStatus.class); expect(status.getState()).andReturn(JobStatus.State.DONE); expect(bigquery.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); + expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); Job completedJob = expectedJob.toBuilder().setStatus(status).build(); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(completedJob); expect(bigquery.getJob(JOB_INFO.getJobId())).andReturn(completedJob); @@ -212,7 +212,7 @@ public void testWaitFor_Null() throws InterruptedException, TimeoutException { initializeExpectedJob(1); BigQuery.JobOption[] expectedOptions = {BigQuery.JobOption.fields(BigQuery.JobField.STATUS)}; expect(bigquery.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); + expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(null); expect(bigquery.getJob(JOB_INFO.getJobId())).andReturn(null); replay(bigquery, mockOptions); @@ -232,7 +232,7 @@ public void testWaitForWithCheckingPeriod() throws InterruptedException, Timeout expect(status.getState()).andReturn(JobStatus.State.RUNNING); expect(status.getState()).andReturn(JobStatus.State.DONE); expect(bigquery.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); + expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); Job runningJob = expectedJob.toBuilder().setStatus(status).build(); Job completedJob = expectedJob.toBuilder().setStatus(status).build(); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(runningJob); @@ -252,7 +252,7 @@ public void testWaitForWithCheckingPeriod_Null() throws InterruptedException, Ti timeUnit.sleep(42); EasyMock.expectLastCall(); expect(bigquery.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); + expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); Job runningJob = expectedJob.toBuilder().setStatus(new JobStatus(JobStatus.State.RUNNING)).build(); expect(bigquery.getJob(JOB_INFO.getJobId(), expectedOptions)).andReturn(runningJob); @@ -271,7 +271,7 @@ public void testWaitForWithTimeout() throws InterruptedException, TimeoutExcepti TimeUnit timeUnit = createStrictMock(TimeUnit.class); timeUnit.sleep(1); EasyMock.expectLastCall(); - NanoClock clock = createStrictMock(NanoClock.class); + ApiClock clock = createStrictMock(ApiClock.class); expect(clock.millisTime()).andReturn(0L); expect(clock.millisTime()).andReturn(1L); expect(clock.millisTime()).andReturn(3L); diff --git a/google-cloud-compute/src/main/java/com/google/cloud/compute/Operation.java b/google-cloud-compute/src/main/java/com/google/cloud/compute/Operation.java index 7b097a45caf8..2d260ced1b7c 100644 --- a/google-cloud-compute/src/main/java/com/google/cloud/compute/Operation.java +++ b/google-cloud-compute/src/main/java/com/google/cloud/compute/Operation.java @@ -18,7 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.cloud.WaitForOption; import com.google.cloud.WaitForOption.CheckingPeriod; import com.google.cloud.compute.Compute.OperationOption; @@ -707,7 +707,7 @@ public Operation waitFor(WaitForOption... waitOptions) WaitForOption.Timeout timeout = WaitForOption.Timeout.getOrDefault(waitOptions); CheckingPeriod checkingPeriod = CheckingPeriod.getOrDefault(waitOptions); long timeoutMillis = timeout.getTimeoutMillis(); - NanoClock clock = options.getClock(); + ApiClock clock = options.getClock(); long startTime = clock.millisTime(); while (!isDone()) { if (timeoutMillis != -1 && (clock.millisTime() - startTime) >= timeoutMillis) { diff --git a/google-cloud-compute/src/test/java/com/google/cloud/compute/OperationTest.java b/google-cloud-compute/src/test/java/com/google/cloud/compute/OperationTest.java index e7d85f09eccb..3b7c70b18768 100644 --- a/google-cloud-compute/src/test/java/com/google/cloud/compute/OperationTest.java +++ b/google-cloud-compute/src/test/java/com/google/cloud/compute/OperationTest.java @@ -28,8 +28,8 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -import com.google.api.gax.core.NanoClock; -import com.google.api.gax.core.SystemClock; +import com.google.api.gax.core.ApiClock; +import com.google.api.gax.core.CurrentMillisClock; import com.google.cloud.WaitForOption; import com.google.cloud.compute.Operation.OperationError; import com.google.cloud.compute.Operation.OperationWarning; @@ -373,7 +373,7 @@ public void testWaitFor() throws InterruptedException, TimeoutException { Operation successOperation = Operation.fromPb(serviceMockReturnsOptions, globalOperation.toPb().setError(null)); expect(compute.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); + expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(successOperation); expect(compute.getOperation(GLOBAL_OPERATION_ID)).andReturn(successOperation); replay(compute, mockOptions); @@ -388,7 +388,7 @@ public void testWaitFor_Null() throws InterruptedException, TimeoutException { Compute.OperationOption[] expectedOptions = {Compute.OperationOption.fields(Compute.OperationField.STATUS)}; expect(compute.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); + expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(null); expect(compute.getOperation(GLOBAL_OPERATION_ID)).andReturn(null); replay(compute, mockOptions); @@ -410,7 +410,7 @@ public void testWaitForCheckingPeriod() throws InterruptedException, TimeoutExce Operation completedOperation = Operation.fromPb(serviceMockReturnsOptions, globalOperation.toPb().setError(null)); expect(compute.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); + expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(runningOperation); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)) .andReturn(completedOperation); @@ -432,7 +432,7 @@ public void testWaitForCheckingPeriod_Null() throws InterruptedException, Timeou Operation runningOperation = Operation.fromPb(serviceMockReturnsOptions, globalOperation.toPb().setError(null).setStatus("RUNNING")); expect(compute.getOptions()).andReturn(mockOptions); - expect(mockOptions.getClock()).andReturn(SystemClock.getDefaultClock()); + expect(mockOptions.getClock()).andReturn(CurrentMillisClock.getDefaultClock()); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(runningOperation); expect(compute.getOperation(GLOBAL_OPERATION_ID, expectedOptions)).andReturn(null); expect(compute.getOperation(GLOBAL_OPERATION_ID)).andReturn(null); @@ -450,7 +450,7 @@ public void testWaitForWithTimeout() throws InterruptedException, TimeoutExcepti TimeUnit timeUnit = createStrictMock(TimeUnit.class); timeUnit.sleep(1); EasyMock.expectLastCall(); - NanoClock clock = createStrictMock(NanoClock.class); + ApiClock clock = createStrictMock(ApiClock.class); expect(clock.millisTime()).andReturn(0L); expect(clock.millisTime()).andReturn(1L); expect(clock.millisTime()).andReturn(3L); diff --git a/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java b/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java index 4aac01227cf2..81695e127b18 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java @@ -18,6 +18,8 @@ import static com.google.common.base.Preconditions.checkNotNull; +import com.google.api.gax.retrying.ExceptionRetryAlgorithm; +import com.google.api.gax.retrying.TimedAttemptSettings; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; @@ -33,7 +35,7 @@ /** * Exception handling used by {@link RetryHelper}. */ -public final class ExceptionHandler implements Serializable { +public final class ExceptionHandler implements ExceptionRetryAlgorithm, Serializable { private static final long serialVersionUID = -2460707015779532919L; @@ -233,7 +235,12 @@ void verifyCaller(Callable callable) { } } - boolean shouldRetry(Exception ex) { + @Override + public boolean accept(Throwable prevThrowable) { + if(!(prevThrowable instanceof Exception)) { + return false; + } + Exception ex = (Exception) prevThrowable; for (Interceptor interceptor : interceptors) { Interceptor.RetryResult retryResult = checkNotNull(interceptor.beforeEval(ex)); if (retryResult != Interceptor.RetryResult.CONTINUE_EVALUATION) { @@ -253,6 +260,12 @@ boolean shouldRetry(Exception ex) { return retryResult == Interceptor.RetryResult.RETRY; } + @Override + public TimedAttemptSettings createNextAttempt(Throwable prevThrowable, + TimedAttemptSettings prevSettings) { + return null; + } + @Override public int hashCode() { return Objects.hash(interceptors, retriableExceptions, nonRetriableExceptions, retryInfo); diff --git a/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java b/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java index 75a07f4ad050..45246af6d0c1 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java +++ b/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java @@ -16,54 +16,43 @@ package com.google.cloud; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.api.gax.core.RetrySettings; -import com.google.api.gax.retrying.DirectRetryHandler; -import com.google.api.gax.retrying.RetryAttemptSettings; -import com.google.api.gax.retrying.RetryFuture; +import com.google.api.gax.retrying.DirectRetryingExecutor; +import com.google.api.gax.retrying.ExponentialRetryAlgorithm; +import com.google.api.gax.retrying.RetryAlgorithm; +import com.google.api.gax.retrying.RetryingExecutor; +import com.google.api.gax.retrying.RetryingFuture; import java.util.concurrent.Callable; /** * Utility class for retrying operations. For more details about the parameters, see {@link * RetrySettings}. - * */ public class RetryHelper { public static V runWithRetries( Callable callable, RetrySettings retrySettings, ExceptionHandler exceptionHandler, - NanoClock clock) + ApiClock clock) throws RetryHelperException { try { - RetryHandler retryHandler = new RetryHandler<>(clock, exceptionHandler); - RetryFuture retryFuture = retryHandler.createFirstAttempt(callable, retrySettings); - retryFuture.setAttemptFuture( - retryHandler.executeAttempt(callable, retryFuture.getAttemptSettings())); - return retryFuture.get(); - } catch (Exception e) { - throw new RetryHelperException(e.getCause()); - } - } + RetryAlgorithm retryAlgorithm = + new RetryAlgorithm(exceptionHandler, new ExponentialRetryAlgorithm(retrySettings, clock)); + RetryingExecutor executor = new DirectRetryingExecutor<>(retryAlgorithm); - private static class RetryHandler extends DirectRetryHandler { - private final ExceptionHandler exceptionHandler; + RetryingFuture retryingFuture = executor.createFuture(callable); + executor.submit(retryingFuture); - private RetryHandler(NanoClock clock, ExceptionHandler exceptionHandler) { - super(clock); - this.exceptionHandler = exceptionHandler; - } - - @Override - public boolean accept(Throwable e, RetryAttemptSettings nextAttemptSettings) { - return super.accept(e, nextAttemptSettings) - && (e instanceof Exception) - && exceptionHandler.shouldRetry((Exception) e); + return retryingFuture.get(); + } catch (Exception e) { + throw new RetryHelperException(e.getCause()); } } public static class RetryHelperException extends RuntimeException { + private static final long serialVersionUID = -8519852520090965314L; RetryHelperException(Throwable cause) { diff --git a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index e386960ba34d..843f775eca01 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -21,9 +21,9 @@ import static com.google.common.base.Preconditions.checkNotNull; import static java.nio.charset.StandardCharsets.UTF_8; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.CurrentMillisClock; +import com.google.api.gax.core.ApiClock; import com.google.api.gax.core.RetrySettings; -import com.google.api.gax.core.SystemClock; import com.google.auth.Credentials; import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.spi.ServiceRpcFactory; @@ -96,7 +96,7 @@ public abstract class ServiceOptions, private final RetrySettings retrySettings; private final String serviceRpcFactoryClassName; private final String serviceFactoryClassName; - private final NanoClock clock; + private final ApiClock clock; private final Credentials credentials; private final TransportOptions transportOptions; @@ -122,7 +122,7 @@ public abstract static class Builder, private RetrySettings retrySettings; private ServiceFactory serviceFactory; private ServiceRpcFactory serviceRpcFactory; - private NanoClock clock; + private ApiClock clock; private TransportOptions transportOptions; protected Builder() {} @@ -156,13 +156,13 @@ public B setServiceFactory(ServiceFactory serviceFactory) { /** - * Sets the service's clock. The clock is mainly used for testing purpose. {@link NanoClock} + * Sets the service's clock. The clock is mainly used for testing purpose. {@link ApiClock} * will be replaced by Java8's {@code java.time.Clock}. * * @param clock the clock to set * @return the builder */ - public B setClock(NanoClock clock) { + public B setClock(ApiClock clock) { this.clock = clock; return self(); } @@ -259,7 +259,7 @@ protected ServiceOptions(Class> ser serviceRpcFactory = firstNonNull(builder.serviceRpcFactory, getFromServiceLoader(rpcFactoryClass, serviceDefaults.getDefaultRpcFactory())); serviceRpcFactoryClassName = serviceRpcFactory.getClass().getName(); - clock = firstNonNull(builder.clock, SystemClock.getDefaultClock()); + clock = firstNonNull(builder.clock, CurrentMillisClock.getDefaultClock()); transportOptions = firstNonNull(builder.transportOptions, serviceDefaults.getDefaultTransportOptions()); } @@ -511,7 +511,7 @@ public RetrySettings getRetrySettings() { * Returns the service's clock. Default time source uses {@link System#currentTimeMillis()} to get * current time. */ - public NanoClock getClock() { + public ApiClock getClock() { return clock; } diff --git a/google-cloud-core/src/test/java/com/google/cloud/ExceptionHandlerTest.java b/google-cloud-core/src/test/java/com/google/cloud/ExceptionHandlerTest.java index f489207019ac..c78f7773d3fb 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/ExceptionHandlerTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/ExceptionHandlerTest.java @@ -117,9 +117,9 @@ private static void assertInvalidCallable(Callable callable, ExceptionHan @Test public void testShouldTry() { ExceptionHandler handler = ExceptionHandler.newBuilder().retryOn(IOException.class).build(); - assertTrue(handler.shouldRetry(new IOException())); - assertTrue(handler.shouldRetry(new ClosedByInterruptException())); - assertFalse(handler.shouldRetry(new RuntimeException())); + assertTrue(handler.accept(new IOException())); + assertTrue(handler.accept(new ClosedByInterruptException())); + assertFalse(handler.accept(new RuntimeException())); ExceptionHandler.Builder builder = ExceptionHandler.newBuilder() .retryOn(IOException.class, NullPointerException.class) @@ -127,11 +127,11 @@ public void testShouldTry() { InterruptedException.class); handler = builder.build(); - assertTrue(handler.shouldRetry(new IOException())); - assertFalse(handler.shouldRetry(new ClosedByInterruptException())); - assertFalse(handler.shouldRetry(new InterruptedException())); - assertFalse(handler.shouldRetry(new RuntimeException())); - assertTrue(handler.shouldRetry(new NullPointerException())); + assertTrue(handler.accept(new IOException())); + assertFalse(handler.accept(new ClosedByInterruptException())); + assertFalse(handler.accept(new InterruptedException())); + assertFalse(handler.accept(new RuntimeException())); + assertTrue(handler.accept(new NullPointerException())); final AtomicReference before = new AtomicReference<>(RetryResult.NO_RETRY); @SuppressWarnings("serial") @@ -150,25 +150,25 @@ public RetryResult beforeEval(Exception exception) { builder.addInterceptors(interceptor); handler = builder.build(); - assertFalse(handler.shouldRetry(new IOException())); - assertFalse(handler.shouldRetry(new ClosedByInterruptException())); - assertFalse(handler.shouldRetry(new InterruptedException())); - assertFalse(handler.shouldRetry(new RuntimeException())); - assertFalse(handler.shouldRetry(new NullPointerException())); + assertFalse(handler.accept(new IOException())); + assertFalse(handler.accept(new ClosedByInterruptException())); + assertFalse(handler.accept(new InterruptedException())); + assertFalse(handler.accept(new RuntimeException())); + assertFalse(handler.accept(new NullPointerException())); before.set(RetryResult.RETRY); - assertTrue(handler.shouldRetry(new IOException())); - assertTrue(handler.shouldRetry(new ClosedByInterruptException())); - assertTrue(handler.shouldRetry(new InterruptedException())); - assertTrue(handler.shouldRetry(new RuntimeException())); - assertTrue(handler.shouldRetry(new NullPointerException())); + assertTrue(handler.accept(new IOException())); + assertTrue(handler.accept(new ClosedByInterruptException())); + assertTrue(handler.accept(new InterruptedException())); + assertTrue(handler.accept(new RuntimeException())); + assertTrue(handler.accept(new NullPointerException())); before.set(RetryResult.CONTINUE_EVALUATION); - assertFalse(handler.shouldRetry(new IOException())); - assertTrue(handler.shouldRetry(new ClosedByInterruptException())); - assertTrue(handler.shouldRetry(new InterruptedException())); - assertTrue(handler.shouldRetry(new RuntimeException())); - assertFalse(handler.shouldRetry(new NullPointerException())); + assertFalse(handler.accept(new IOException())); + assertTrue(handler.accept(new ClosedByInterruptException())); + assertTrue(handler.accept(new InterruptedException())); + assertTrue(handler.accept(new RuntimeException())); + assertFalse(handler.accept(new NullPointerException())); } @Test @@ -190,7 +190,7 @@ public RetryResult afterEval(Exception exception, RetryResult retryResult) { ExceptionHandler handler = ExceptionHandler.newBuilder().addInterceptors(interceptor).build(); thrown.expect(NullPointerException.class); - handler.shouldRetry(new Exception()); + handler.accept(new Exception()); } @Test @@ -212,6 +212,6 @@ public RetryResult afterEval(Exception exception, RetryResult retryResult) { ExceptionHandler handler = ExceptionHandler.newBuilder().addInterceptors(interceptor).build(); thrown.expect(NullPointerException.class); - handler.shouldRetry(new Exception()); + handler.accept(new Exception()); } } diff --git a/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java b/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java index 30e315060784..3ef3f21e3cff 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java @@ -22,8 +22,8 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.api.gax.core.NanoClock; -import com.google.api.gax.core.SystemClock; +import com.google.api.gax.core.ApiClock; +import com.google.api.gax.core.CurrentMillisClock; import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.spi.ServiceRpcFactory; @@ -76,7 +76,7 @@ public class ServiceOptionsTest { } } - private static final NanoClock TEST_CLOCK = new TestClock(); + private static final ApiClock TEST_CLOCK = new TestClock(); private static final TestServiceOptions OPTIONS = TestServiceOptions.newBuilder() .setCredentials(credentials) @@ -102,7 +102,7 @@ public class ServiceOptionsTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private static class TestClock implements NanoClock { + private static class TestClock implements ApiClock { @Override public long nanoTime() { return 123_456_789_000_000L; @@ -225,7 +225,7 @@ public void testBuilder() { assertEquals("project-id", OPTIONS.getProjectId()); assertSame(ServiceOptions.getNoRetrySettings(), OPTIONS.getRetrySettings()); - assertSame(SystemClock.getDefaultClock(), DEFAULT_OPTIONS.getClock()); + assertSame(CurrentMillisClock.getDefaultClock(), DEFAULT_OPTIONS.getClock()); assertEquals("https://www.googleapis.com", DEFAULT_OPTIONS.getHost()); assertSame(ServiceOptions.getDefaultRetrySettings(), DEFAULT_OPTIONS.getRetrySettings()); } diff --git a/google-cloud-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java b/google-cloud-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java index 2556500c2d0e..0df4f8afff5b 100644 --- a/google-cloud-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java +++ b/google-cloud-dns/src/test/java/com/google/cloud/dns/DnsImplTest.java @@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.api.services.dns.model.Change; import com.google.api.services.dns.model.ManagedZone; import com.google.api.services.dns.model.ResourceRecordSet; @@ -100,7 +100,7 @@ public class DnsImplTest { // Other private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); - private static final NanoClock TIME_SOURCE = new NanoClock() { + private static final ApiClock TIME_SOURCE = new ApiClock() { @Override public long nanoTime() { return 42_000_000_000L; diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewer.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewer.java index 9fec7d04908d..8260dd16c3ba 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewer.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/deprecated/AckDeadlineRenewer.java @@ -16,7 +16,7 @@ package com.google.cloud.pubsub.deprecated; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.cloud.GrpcTransportOptions; import com.google.cloud.GrpcTransportOptions.ExecutorFactory; import com.google.common.base.MoreObjects; @@ -50,7 +50,7 @@ class AckDeadlineRenewer implements AutoCloseable { private final PubSub pubsub; private final ScheduledExecutorService executor; private final ExecutorFactory executorFactory; - private final NanoClock clock; + private final ApiClock clock; private final Queue messageQueue; private final Map messageDeadlines; private final Object lock = new Object(); diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/MessageDispatcher.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/MessageDispatcher.java index 87b4727675a1..3592bd84c20c 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/MessageDispatcher.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/MessageDispatcher.java @@ -17,7 +17,7 @@ package com.google.cloud.pubsub.spi.v1; import com.google.api.gax.core.FlowController; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.api.stats.Distribution; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; @@ -58,7 +58,7 @@ class MessageDispatcher { private static final int MAX_ACK_DEADLINE_EXTENSION_SECS = 10 * 60; // 10m private final ScheduledExecutorService executor; - private final NanoClock clock; + private final ApiClock clock; private final Duration ackExpirationPadding; private final MessageReceiver receiver; @@ -220,7 +220,7 @@ void sendAckOperations( Distribution ackLatencyDistribution, FlowController flowController, ScheduledExecutorService executor, - NanoClock clock) { + ApiClock clock) { this.executor = executor; this.ackExpirationPadding = ackExpirationPadding; this.receiver = receiver; diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PollingSubscriberConnection.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PollingSubscriberConnection.java index d2f4b1a1e378..729ca3875ddf 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PollingSubscriberConnection.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PollingSubscriberConnection.java @@ -17,7 +17,7 @@ package com.google.cloud.pubsub.spi.v1; import com.google.api.gax.core.FlowController; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.api.stats.Distribution; import com.google.auth.Credentials; import com.google.cloud.pubsub.spi.v1.MessageDispatcher.AckProcessor; @@ -72,7 +72,7 @@ public PollingSubscriberConnection( Channel channel, FlowController flowController, ScheduledExecutorService executor, - NanoClock clock) { + ApiClock clock) { this.subscription = subscription; this.executor = executor; stub = diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/StreamingSubscriberConnection.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/StreamingSubscriberConnection.java index 3a4aa86f838d..120868f23483 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/StreamingSubscriberConnection.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/StreamingSubscriberConnection.java @@ -17,7 +17,7 @@ package com.google.cloud.pubsub.spi.v1; import com.google.api.gax.core.FlowController; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.api.stats.Distribution; import com.google.auth.Credentials; import com.google.cloud.pubsub.spi.v1.MessageDispatcher.AckProcessor; @@ -75,7 +75,7 @@ public StreamingSubscriberConnection( Channel channel, FlowController flowController, ScheduledExecutorService executor, - NanoClock clock) { + ApiClock clock) { this.subscription = subscription; this.executor = executor; this.credentials = credentials; diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java index 110f1d2c3153..32978fd1be18 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/Subscriber.java @@ -16,10 +16,10 @@ package com.google.cloud.pubsub.spi.v1; +import com.google.api.gax.core.CurrentMillisClock; import com.google.api.gax.core.FlowControlSettings; import com.google.api.gax.core.FlowController; -import com.google.api.gax.core.NanoClock; -import com.google.api.gax.core.DefaultNanoClock; +import com.google.api.gax.core.ApiClock;; import com.google.api.gax.grpc.ExecutorProvider; import com.google.api.gax.grpc.InstantiatingExecutorProvider; import com.google.api.stats.Distribution; @@ -267,7 +267,7 @@ private static class SubscriberImpl extends AbstractService { private final MessageReceiver receiver; private final List streamingSubscriberConnections; private final List pollingSubscriberConnections; - private final NanoClock clock; + private final ApiClock clock; private final List closeables = new ArrayList<>(); private ScheduledFuture ackDeadlineUpdater; private int streamAckDeadlineSeconds; @@ -282,7 +282,7 @@ private SubscriberImpl(Builder builder) throws IOException { Math.max( INITIAL_ACK_DEADLINE_SECONDS, Ints.saturatedCast(ackExpirationPadding.getStandardSeconds())); - clock = builder.clock.isPresent() ? builder.clock.get() : DefaultNanoClock.getDefaultClock(); + clock = builder.clock.isPresent() ? builder.clock.get() : CurrentMillisClock.getDefaultClock(); flowController = new FlowController(builder.flowControlSettings); @@ -537,7 +537,7 @@ public static final class Builder { ExecutorProvider executorProvider = DEFAULT_EXECUTOR_PROVIDER; Optional>> channelBuilder = Optional.absent(); - Optional clock = Optional.absent(); + Optional clock = Optional.absent(); Builder(SubscriptionName subscriptionName, MessageReceiver receiver) { this.subscriptionName = subscriptionName; @@ -598,7 +598,7 @@ public Builder setExecutorProvider(ExecutorProvider executorProvider) { } /** Gives the ability to set a custom clock. */ - Builder setClock(NanoClock clock) { + Builder setClock(ApiClock clock) { this.clock = Optional.of(clock); return this; } diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeClock.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeClock.java index eb021b368af8..581a11074728 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeClock.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeClock.java @@ -16,12 +16,12 @@ package com.google.cloud.pubsub.spi.v1; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; /** A Clock to help with testing time-based logic. */ -public class FakeClock implements NanoClock { +public class FakeClock implements ApiClock { private final AtomicLong millis = new AtomicLong(); diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeScheduledExecutorService.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeScheduledExecutorService.java index fbbeb9a278f3..0d757a3be6af 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeScheduledExecutorService.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/FakeScheduledExecutorService.java @@ -16,7 +16,7 @@ package com.google.cloud.pubsub.spi.v1; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.common.primitives.Ints; import com.google.common.util.concurrent.SettableFuture; import java.util.ArrayList; @@ -45,7 +45,7 @@ public class FakeScheduledExecutorService extends AbstractExecutorService private final PriorityQueue> pendingCallables = new PriorityQueue<>(); private final FakeClock clock = new FakeClock(); - public NanoClock getClock() { + public ApiClock getClock() { return clock; } diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Operation.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Operation.java index 53dc5d3c8e4e..8445d602018f 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Operation.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Operation.java @@ -16,8 +16,8 @@ package com.google.cloud.spanner; -import com.google.api.gax.core.NanoClock; -import com.google.api.gax.core.SystemClock; +import com.google.api.gax.core.ApiClock; +import com.google.api.gax.core.CurrentMillisClock; import com.google.cloud.WaitForOption; import com.google.cloud.WaitForOption.CheckingPeriod; import com.google.cloud.WaitForOption.Timeout; @@ -45,7 +45,7 @@ static interface Parser { private final SpannerRpc rpc; private final String name; private final Parser parser; - private final NanoClock clock; + private final ApiClock clock; @VisibleForTesting Operation( @@ -56,7 +56,7 @@ static interface Parser { @Nullable SpannerException exception, boolean isDone, Parser parser, - NanoClock clock) { + ApiClock clock) { this.rpc = rpc; this.name = name; this.metadata = metadata; @@ -68,7 +68,7 @@ static interface Parser { } private static Operation failed( - SpannerRpc rpc, String name, Status status, M metadata, Parser parser, NanoClock clock) { + SpannerRpc rpc, String name, Status status, M metadata, Parser parser, ApiClock clock) { SpannerException e = SpannerExceptionFactory.newSpannerException( ErrorCode.fromRpcStatus(status), status.getMessage(), null); @@ -76,22 +76,22 @@ private static Operation failed( } private static Operation successful( - SpannerRpc rpc, String name, M metadata, R result, Parser parser, NanoClock clock) { + SpannerRpc rpc, String name, M metadata, R result, Parser parser, ApiClock clock) { return new Operation<>(rpc, name, metadata, result, null, true, parser, clock); } private static Operation pending( - SpannerRpc rpc, String name, M metadata, Parser parser, NanoClock clock) { + SpannerRpc rpc, String name, M metadata, Parser parser, ApiClock clock) { return new Operation<>(rpc, name, metadata, null, null, false, parser, clock); } static Operation create( SpannerRpc rpc, com.google.longrunning.Operation proto, Parser parser) { - return Operation.create(rpc, proto, parser, SystemClock.getDefaultClock()); + return Operation.create(rpc, proto, parser, CurrentMillisClock.getDefaultClock()); } static Operation create( - SpannerRpc rpc, com.google.longrunning.Operation proto, Parser parser, NanoClock clock) { + SpannerRpc rpc, com.google.longrunning.Operation proto, Parser parser, ApiClock clock) { M metadata = proto.hasMetadata() ? parser.parseMetadata(proto.getMetadata()) : null; String name = proto.getName(); if (proto.getDone()) { diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/OperationTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/OperationTest.java index 97b46b7a003d..6b72f01ab95f 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/OperationTest.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/OperationTest.java @@ -22,7 +22,7 @@ import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.cloud.WaitForOption; import com.google.cloud.spanner.spi.v1.SpannerRpc; import com.google.protobuf.Any; @@ -47,7 +47,7 @@ public class OperationTest { @Mock SpannerRpc rpc; @Mock DatabaseAdminClient dbClient; - @Mock NanoClock clock; + @Mock ApiClock clock; @Rule public ExpectedException expectedException = ExpectedException.none(); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java index 35fea8d2c9f3..397a167503ee 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplTest.java @@ -26,7 +26,7 @@ import static org.junit.Assert.assertTrue; import com.google.api.client.googleapis.json.GoogleJsonError; -import com.google.api.gax.core.NanoClock; +import com.google.api.gax.core.ApiClock; import com.google.api.services.storage.model.StorageObject; import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.cloud.Page; @@ -261,8 +261,8 @@ public class StorageImplTest { + "EkPPhszldvQTY486uPxyD/D7HdfnGW/Nbw5JUhfvecAdudDEhNAQ3PNabyDMI+TpiHy4NTWOrgdcWrzj6VXcdc" + "+uuABnPwRCdcyJ1xl2kOrPksRnp1auNGMLOe4IpEBjGY7baX9UG8+A45MbG0aHmkR59Op/aR9XowIDAQAB"; - private static final NanoClock TIME_SOURCE = - new NanoClock() { + private static final ApiClock TIME_SOURCE = + new ApiClock() { @Override public long nanoTime() { return 42_000_000_000L; From f9a0b1f80228981b625e3b28033da8afe6ed5f35 Mon Sep 17 00:00:00 2001 From: vam Date: Mon, 20 Mar 2017 16:38:56 -0700 Subject: [PATCH 4/5] Adressed PR comments. --- .../java/com/google/cloud/BaseService.java | 49 ++++++++--------- ....java => HttpExceptionRetryAlgorithm.java} | 24 +++++---- .../java/com/google/cloud/RetryHelper.java | 7 +-- ...a => HttpExceptionRetryAlgorithmTest.java} | 24 ++++----- .../com/google/cloud/SerializationTest.java | 53 ++++++++++--------- .../cloud/translate/TranslateImplTest.java | 3 +- pom.xml | 2 +- 7 files changed, 84 insertions(+), 78 deletions(-) rename google-cloud-core/src/main/java/com/google/cloud/{ExceptionHandler.java => HttpExceptionRetryAlgorithm.java} (92%) rename google-cloud-core/src/test/java/com/google/cloud/{ExceptionHandlerTest.java => HttpExceptionRetryAlgorithmTest.java} (87%) diff --git a/google-cloud-core/src/main/java/com/google/cloud/BaseService.java b/google-cloud-core/src/main/java/com/google/cloud/BaseService.java index 619341bc3729..7b7caa5de2b8 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/BaseService.java +++ b/google-cloud-core/src/main/java/com/google/cloud/BaseService.java @@ -16,7 +16,7 @@ package com.google.cloud; -import com.google.cloud.ExceptionHandler.Interceptor; +import com.google.cloud.HttpExceptionRetryAlgorithm.Interceptor; /** * Base class for service objects. @@ -26,28 +26,30 @@ public abstract class BaseService> implements Service { - public static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() { - - private static final long serialVersionUID = -8429573486870467828L; - - @Override - public RetryResult afterEval(Exception exception, RetryResult retryResult) { - return Interceptor.RetryResult.CONTINUE_EVALUATION; - } - - @Override - public RetryResult beforeEval(Exception exception) { - if (exception instanceof BaseServiceException) { - boolean retriable = ((BaseServiceException) exception).isRetryable(); - return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY; - } - return Interceptor.RetryResult.CONTINUE_EVALUATION; - } - }; - public static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.newBuilder() - .abortOn(RuntimeException.class) - .addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR) - .build(); + public static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = + new Interceptor() { + + private static final long serialVersionUID = -8429573486870467828L; + + @Override + public RetryResult afterEval(Exception exception, RetryResult retryResult) { + return Interceptor.RetryResult.CONTINUE_EVALUATION; + } + + @Override + public RetryResult beforeEval(Exception exception) { + if (exception instanceof BaseServiceException) { + boolean retriable = ((BaseServiceException) exception).isRetryable(); + return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY; + } + return Interceptor.RetryResult.CONTINUE_EVALUATION; + } + }; + public static final HttpExceptionRetryAlgorithm EXCEPTION_HANDLER = + HttpExceptionRetryAlgorithm.newBuilder() + .abortOn(RuntimeException.class) + .addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR) + .build(); private final OptionsT options; @@ -55,7 +57,6 @@ protected BaseService(OptionsT options) { this.options = options; } - @Override public OptionsT getOptions() { return options; diff --git a/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java b/google-cloud-core/src/main/java/com/google/cloud/HttpExceptionRetryAlgorithm.java similarity index 92% rename from google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java rename to google-cloud-core/src/main/java/com/google/cloud/HttpExceptionRetryAlgorithm.java index 81695e127b18..e5d3e908cf0d 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java +++ b/google-cloud-core/src/main/java/com/google/cloud/HttpExceptionRetryAlgorithm.java @@ -33,13 +33,13 @@ import java.util.concurrent.Callable; /** - * Exception handling used by {@link RetryHelper}. + * Exception retry algorithm implementation used by {@link RetryHelper}. */ -public final class ExceptionHandler implements ExceptionRetryAlgorithm, Serializable { +public final class HttpExceptionRetryAlgorithm implements ExceptionRetryAlgorithm, Serializable { private static final long serialVersionUID = -2460707015779532919L; - private static final ExceptionHandler DEFAULT_INSTANCE = + private static final HttpExceptionRetryAlgorithm DEFAULT_INSTANCE = newBuilder().retryOn(Exception.class).abortOn(RuntimeException.class).build(); private final ImmutableList interceptors; @@ -76,7 +76,7 @@ enum RetryResult { } /** - * ExceptionHandler builder. + * HttpExceptionRetryAlgorithm builder. */ public static class Builder { @@ -132,10 +132,10 @@ public final Builder abortOn(Class... exceptions) { } /** - * Returns a new ExceptionHandler instance. + * Returns a new HttpExceptionRetryAlgorithm instance. */ - public ExceptionHandler build() { - return new ExceptionHandler(this); + public HttpExceptionRetryAlgorithm build() { + return new HttpExceptionRetryAlgorithm(this); } } @@ -170,7 +170,7 @@ public boolean equals(Object obj) { } } - private ExceptionHandler(Builder builder) { + private HttpExceptionRetryAlgorithm(Builder builder) { interceptors = builder.interceptors.build(); retriableExceptions = builder.retriableExceptions.build(); nonRetriableExceptions = builder.nonRetriableExceptions.build(); @@ -263,6 +263,8 @@ public boolean accept(Throwable prevThrowable) { @Override public TimedAttemptSettings createNextAttempt(Throwable prevThrowable, TimedAttemptSettings prevSettings) { + // Return null to indicate that this implementaiton does not provide any specific attempt + // settings, so by default the TimedRetryAlgorithm options can be used instead. return null; } @@ -276,10 +278,10 @@ public boolean equals(Object obj) { if (obj == this) { return true; } - if (!(obj instanceof ExceptionHandler)) { + if (!(obj instanceof HttpExceptionRetryAlgorithm)) { return false; } - ExceptionHandler other = (ExceptionHandler) obj; + HttpExceptionRetryAlgorithm other = (HttpExceptionRetryAlgorithm) obj; return Objects.equals(interceptors, other.interceptors) && Objects.equals(retriableExceptions, other.retriableExceptions) && Objects.equals(nonRetriableExceptions, other.nonRetriableExceptions) @@ -290,7 +292,7 @@ public boolean equals(Object obj) { /** * Returns an instance which retry any checked exception and abort on any runtime exception. */ - public static ExceptionHandler getDefaultInstance() { + public static HttpExceptionRetryAlgorithm getDefaultInstance() { return DEFAULT_INSTANCE; } diff --git a/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java b/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java index 45246af6d0c1..6372518bce79 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java +++ b/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java @@ -28,18 +28,19 @@ /** * Utility class for retrying operations. For more details about the parameters, see {@link - * RetrySettings}. + * RetrySettings}. In case if retrying is unsuccessful, {@link RetryHelperException} will be + * thrown. */ public class RetryHelper { public static V runWithRetries( Callable callable, RetrySettings retrySettings, - ExceptionHandler exceptionHandler, + HttpExceptionRetryAlgorithm exceptionRetryAlgorithm, ApiClock clock) throws RetryHelperException { try { RetryAlgorithm retryAlgorithm = - new RetryAlgorithm(exceptionHandler, new ExponentialRetryAlgorithm(retrySettings, clock)); + new RetryAlgorithm(exceptionRetryAlgorithm, new ExponentialRetryAlgorithm(retrySettings, clock)); RetryingExecutor executor = new DirectRetryingExecutor<>(retryAlgorithm); RetryingFuture retryingFuture = executor.createFuture(callable); diff --git a/google-cloud-core/src/test/java/com/google/cloud/ExceptionHandlerTest.java b/google-cloud-core/src/test/java/com/google/cloud/HttpExceptionRetryAlgorithmTest.java similarity index 87% rename from google-cloud-core/src/test/java/com/google/cloud/ExceptionHandlerTest.java rename to google-cloud-core/src/test/java/com/google/cloud/HttpExceptionRetryAlgorithmTest.java index c78f7773d3fb..8d8fb1f5f0dc 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/ExceptionHandlerTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/HttpExceptionRetryAlgorithmTest.java @@ -20,8 +20,8 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.cloud.ExceptionHandler.Interceptor; -import com.google.cloud.ExceptionHandler.Interceptor.RetryResult; +import com.google.cloud.HttpExceptionRetryAlgorithm.Interceptor; +import com.google.cloud.HttpExceptionRetryAlgorithm.Interceptor.RetryResult; import org.junit.Rule; import org.junit.Test; @@ -34,9 +34,9 @@ import java.util.concurrent.atomic.AtomicReference; /** - * Tests for {@link ExceptionHandler}. + * Tests for {@link HttpExceptionRetryAlgorithm}. */ -public class ExceptionHandlerTest { +public class HttpExceptionRetryAlgorithmTest { @Rule public ExpectedException thrown = ExpectedException.none(); @@ -82,7 +82,7 @@ public Object call() throws Error { } // using default exception handler (retry upon any non-runtime exceptions) - ExceptionHandler handler = ExceptionHandler.getDefaultInstance(); + HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.getDefaultInstance(); assertValidCallable(new A(), handler); assertValidCallable(new B(), handler); assertValidCallable(new C(), handler); @@ -90,7 +90,7 @@ public Object call() throws Error { assertValidCallable(new E(), handler); assertInvalidCallable(new F(), handler); - handler = ExceptionHandler.newBuilder() + handler = HttpExceptionRetryAlgorithm.newBuilder() .retryOn(FileNotFoundException.class, NullPointerException.class) .build(); assertInvalidCallable(new A(), handler); @@ -101,11 +101,11 @@ public Object call() throws Error { assertInvalidCallable(new F(), handler); } - private static void assertValidCallable(Callable callable, ExceptionHandler handler) { + private static void assertValidCallable(Callable callable, HttpExceptionRetryAlgorithm handler) { handler.verifyCaller(callable); } - private static void assertInvalidCallable(Callable callable, ExceptionHandler handler) { + private static void assertInvalidCallable(Callable callable, HttpExceptionRetryAlgorithm handler) { try { handler.verifyCaller(callable); fail("Expected RetryHelper constructor to fail"); @@ -116,12 +116,12 @@ private static void assertInvalidCallable(Callable callable, ExceptionHan @Test public void testShouldTry() { - ExceptionHandler handler = ExceptionHandler.newBuilder().retryOn(IOException.class).build(); + HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.newBuilder().retryOn(IOException.class).build(); assertTrue(handler.accept(new IOException())); assertTrue(handler.accept(new ClosedByInterruptException())); assertFalse(handler.accept(new RuntimeException())); - ExceptionHandler.Builder builder = ExceptionHandler.newBuilder() + HttpExceptionRetryAlgorithm.Builder builder = HttpExceptionRetryAlgorithm.newBuilder() .retryOn(IOException.class, NullPointerException.class) .abortOn(RuntimeException.class, ClosedByInterruptException.class, InterruptedException.class); @@ -188,7 +188,7 @@ public RetryResult afterEval(Exception exception, RetryResult retryResult) { }; - ExceptionHandler handler = ExceptionHandler.newBuilder().addInterceptors(interceptor).build(); + HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.newBuilder().addInterceptors(interceptor).build(); thrown.expect(NullPointerException.class); handler.accept(new Exception()); } @@ -210,7 +210,7 @@ public RetryResult afterEval(Exception exception, RetryResult retryResult) { }; - ExceptionHandler handler = ExceptionHandler.newBuilder().addInterceptors(interceptor).build(); + HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.newBuilder().addInterceptors(interceptor).build(); thrown.expect(NullPointerException.class); handler.accept(new Exception()); } diff --git a/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java b/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java index 70989b839bbe..44c510a3c984 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java @@ -29,7 +29,8 @@ public class SerializationTest extends BaseSerializationTest { private static final BaseServiceException BASE_SERVICE_EXCEPTION = new BaseServiceException(42, "message", "reason", true); - private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.getDefaultInstance(); + private static final HttpExceptionRetryAlgorithm EXCEPTION_HANDLER = + HttpExceptionRetryAlgorithm.getDefaultInstance(); private static final Identity IDENTITY = Identity.allAuthenticatedUsers(); private static final PageImpl PAGE = new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2")); @@ -50,31 +51,31 @@ public class SerializationTest extends BaseSerializationTest { .build(); private static final String JSON_KEY = "{\n" - + " \"private_key_id\": \"somekeyid\",\n" - + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" - + "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" - + "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" - + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" - + "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" - + "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" - + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" - + "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" - + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" - + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" - + "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" - + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" - + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" - + "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" - + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" - + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" - + "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" - + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" - + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" - + "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" - + " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n" - + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" - + " \"type\": \"service_account\"\n" - + "}"; + + " \"private_key_id\": \"somekeyid\",\n" + + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" + + "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" + + "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" + + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" + + "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" + + "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" + + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" + + "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" + + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" + + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" + + "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" + + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" + + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" + + "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" + + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" + + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" + + "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" + + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" + + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" + + "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" + + " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n" + + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" + + " \"type\": \"service_account\"\n" + + "}"; @Override protected Serializable[] serializableObjects() { diff --git a/google-cloud-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java b/google-cloud-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java index 022bdc0686fd..b6b762bd209d 100644 --- a/google-cloud-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java +++ b/google-cloud-translate/src/test/java/com/google/cloud/translate/TranslateImplTest.java @@ -100,7 +100,8 @@ public class TranslateImplTest { private TranslateRpc translateRpcMock; private Translate translate; - @Rule public ExpectedException thrown = ExpectedException.none(); + @Rule + public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() { diff --git a/pom.xml b/pom.xml index e0d136ab5bb1..1fb6e12d0ac1 100644 --- a/pom.xml +++ b/pom.xml @@ -92,7 +92,7 @@ github 0.6.0 1.0.3 - 0.3.2-SNAPSHOT + 0.5.0 0.1.5 0.10.1-alpha-SNAPSHOT 0.10.1-beta-SNAPSHOT From d2c24a97f974fbaede5b2e2c634df0dee27d4ef2 Mon Sep 17 00:00:00 2001 From: vam Date: Mon, 20 Mar 2017 17:14:12 -0700 Subject: [PATCH 5/5] Reverted ExceptionHandler rename. --- .../java/com/google/cloud/BaseService.java | 49 +++++++++---------- ...ryAlgorithm.java => ExceptionHandler.java} | 20 ++++---- .../java/com/google/cloud/RetryHelper.java | 2 +- ...thmTest.java => ExceptionHandlerTest.java} | 24 ++++----- .../com/google/cloud/SerializationTest.java | 3 +- 5 files changed, 48 insertions(+), 50 deletions(-) rename google-cloud-core/src/main/java/com/google/cloud/{HttpExceptionRetryAlgorithm.java => ExceptionHandler.java} (94%) rename google-cloud-core/src/test/java/com/google/cloud/{HttpExceptionRetryAlgorithmTest.java => ExceptionHandlerTest.java} (87%) diff --git a/google-cloud-core/src/main/java/com/google/cloud/BaseService.java b/google-cloud-core/src/main/java/com/google/cloud/BaseService.java index 7b7caa5de2b8..619341bc3729 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/BaseService.java +++ b/google-cloud-core/src/main/java/com/google/cloud/BaseService.java @@ -16,7 +16,7 @@ package com.google.cloud; -import com.google.cloud.HttpExceptionRetryAlgorithm.Interceptor; +import com.google.cloud.ExceptionHandler.Interceptor; /** * Base class for service objects. @@ -26,30 +26,28 @@ public abstract class BaseService> implements Service { - public static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = - new Interceptor() { - - private static final long serialVersionUID = -8429573486870467828L; - - @Override - public RetryResult afterEval(Exception exception, RetryResult retryResult) { - return Interceptor.RetryResult.CONTINUE_EVALUATION; - } - - @Override - public RetryResult beforeEval(Exception exception) { - if (exception instanceof BaseServiceException) { - boolean retriable = ((BaseServiceException) exception).isRetryable(); - return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY; - } - return Interceptor.RetryResult.CONTINUE_EVALUATION; - } - }; - public static final HttpExceptionRetryAlgorithm EXCEPTION_HANDLER = - HttpExceptionRetryAlgorithm.newBuilder() - .abortOn(RuntimeException.class) - .addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR) - .build(); + public static final Interceptor EXCEPTION_HANDLER_INTERCEPTOR = new Interceptor() { + + private static final long serialVersionUID = -8429573486870467828L; + + @Override + public RetryResult afterEval(Exception exception, RetryResult retryResult) { + return Interceptor.RetryResult.CONTINUE_EVALUATION; + } + + @Override + public RetryResult beforeEval(Exception exception) { + if (exception instanceof BaseServiceException) { + boolean retriable = ((BaseServiceException) exception).isRetryable(); + return retriable ? Interceptor.RetryResult.RETRY : Interceptor.RetryResult.NO_RETRY; + } + return Interceptor.RetryResult.CONTINUE_EVALUATION; + } + }; + public static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.newBuilder() + .abortOn(RuntimeException.class) + .addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR) + .build(); private final OptionsT options; @@ -57,6 +55,7 @@ protected BaseService(OptionsT options) { this.options = options; } + @Override public OptionsT getOptions() { return options; diff --git a/google-cloud-core/src/main/java/com/google/cloud/HttpExceptionRetryAlgorithm.java b/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java similarity index 94% rename from google-cloud-core/src/main/java/com/google/cloud/HttpExceptionRetryAlgorithm.java rename to google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java index e5d3e908cf0d..81703b561285 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/HttpExceptionRetryAlgorithm.java +++ b/google-cloud-core/src/main/java/com/google/cloud/ExceptionHandler.java @@ -35,11 +35,11 @@ /** * Exception retry algorithm implementation used by {@link RetryHelper}. */ -public final class HttpExceptionRetryAlgorithm implements ExceptionRetryAlgorithm, Serializable { +public final class ExceptionHandler implements ExceptionRetryAlgorithm, Serializable { private static final long serialVersionUID = -2460707015779532919L; - private static final HttpExceptionRetryAlgorithm DEFAULT_INSTANCE = + private static final ExceptionHandler DEFAULT_INSTANCE = newBuilder().retryOn(Exception.class).abortOn(RuntimeException.class).build(); private final ImmutableList interceptors; @@ -76,7 +76,7 @@ enum RetryResult { } /** - * HttpExceptionRetryAlgorithm builder. + * ExceptionHandler builder. */ public static class Builder { @@ -132,10 +132,10 @@ public final Builder abortOn(Class... exceptions) { } /** - * Returns a new HttpExceptionRetryAlgorithm instance. + * Returns a new ExceptionHandler instance. */ - public HttpExceptionRetryAlgorithm build() { - return new HttpExceptionRetryAlgorithm(this); + public ExceptionHandler build() { + return new ExceptionHandler(this); } } @@ -170,7 +170,7 @@ public boolean equals(Object obj) { } } - private HttpExceptionRetryAlgorithm(Builder builder) { + private ExceptionHandler(Builder builder) { interceptors = builder.interceptors.build(); retriableExceptions = builder.retriableExceptions.build(); nonRetriableExceptions = builder.nonRetriableExceptions.build(); @@ -278,10 +278,10 @@ public boolean equals(Object obj) { if (obj == this) { return true; } - if (!(obj instanceof HttpExceptionRetryAlgorithm)) { + if (!(obj instanceof ExceptionHandler)) { return false; } - HttpExceptionRetryAlgorithm other = (HttpExceptionRetryAlgorithm) obj; + ExceptionHandler other = (ExceptionHandler) obj; return Objects.equals(interceptors, other.interceptors) && Objects.equals(retriableExceptions, other.retriableExceptions) && Objects.equals(nonRetriableExceptions, other.nonRetriableExceptions) @@ -292,7 +292,7 @@ public boolean equals(Object obj) { /** * Returns an instance which retry any checked exception and abort on any runtime exception. */ - public static HttpExceptionRetryAlgorithm getDefaultInstance() { + public static ExceptionHandler getDefaultInstance() { return DEFAULT_INSTANCE; } diff --git a/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java b/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java index 6372518bce79..6d36096bb68b 100644 --- a/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java +++ b/google-cloud-core/src/main/java/com/google/cloud/RetryHelper.java @@ -35,7 +35,7 @@ public class RetryHelper { public static V runWithRetries( Callable callable, RetrySettings retrySettings, - HttpExceptionRetryAlgorithm exceptionRetryAlgorithm, + ExceptionHandler exceptionRetryAlgorithm, ApiClock clock) throws RetryHelperException { try { diff --git a/google-cloud-core/src/test/java/com/google/cloud/HttpExceptionRetryAlgorithmTest.java b/google-cloud-core/src/test/java/com/google/cloud/ExceptionHandlerTest.java similarity index 87% rename from google-cloud-core/src/test/java/com/google/cloud/HttpExceptionRetryAlgorithmTest.java rename to google-cloud-core/src/test/java/com/google/cloud/ExceptionHandlerTest.java index 8d8fb1f5f0dc..c78f7773d3fb 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/HttpExceptionRetryAlgorithmTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/ExceptionHandlerTest.java @@ -20,8 +20,8 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.cloud.HttpExceptionRetryAlgorithm.Interceptor; -import com.google.cloud.HttpExceptionRetryAlgorithm.Interceptor.RetryResult; +import com.google.cloud.ExceptionHandler.Interceptor; +import com.google.cloud.ExceptionHandler.Interceptor.RetryResult; import org.junit.Rule; import org.junit.Test; @@ -34,9 +34,9 @@ import java.util.concurrent.atomic.AtomicReference; /** - * Tests for {@link HttpExceptionRetryAlgorithm}. + * Tests for {@link ExceptionHandler}. */ -public class HttpExceptionRetryAlgorithmTest { +public class ExceptionHandlerTest { @Rule public ExpectedException thrown = ExpectedException.none(); @@ -82,7 +82,7 @@ public Object call() throws Error { } // using default exception handler (retry upon any non-runtime exceptions) - HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.getDefaultInstance(); + ExceptionHandler handler = ExceptionHandler.getDefaultInstance(); assertValidCallable(new A(), handler); assertValidCallable(new B(), handler); assertValidCallable(new C(), handler); @@ -90,7 +90,7 @@ public Object call() throws Error { assertValidCallable(new E(), handler); assertInvalidCallable(new F(), handler); - handler = HttpExceptionRetryAlgorithm.newBuilder() + handler = ExceptionHandler.newBuilder() .retryOn(FileNotFoundException.class, NullPointerException.class) .build(); assertInvalidCallable(new A(), handler); @@ -101,11 +101,11 @@ public Object call() throws Error { assertInvalidCallable(new F(), handler); } - private static void assertValidCallable(Callable callable, HttpExceptionRetryAlgorithm handler) { + private static void assertValidCallable(Callable callable, ExceptionHandler handler) { handler.verifyCaller(callable); } - private static void assertInvalidCallable(Callable callable, HttpExceptionRetryAlgorithm handler) { + private static void assertInvalidCallable(Callable callable, ExceptionHandler handler) { try { handler.verifyCaller(callable); fail("Expected RetryHelper constructor to fail"); @@ -116,12 +116,12 @@ private static void assertInvalidCallable(Callable callable, HttpExceptio @Test public void testShouldTry() { - HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.newBuilder().retryOn(IOException.class).build(); + ExceptionHandler handler = ExceptionHandler.newBuilder().retryOn(IOException.class).build(); assertTrue(handler.accept(new IOException())); assertTrue(handler.accept(new ClosedByInterruptException())); assertFalse(handler.accept(new RuntimeException())); - HttpExceptionRetryAlgorithm.Builder builder = HttpExceptionRetryAlgorithm.newBuilder() + ExceptionHandler.Builder builder = ExceptionHandler.newBuilder() .retryOn(IOException.class, NullPointerException.class) .abortOn(RuntimeException.class, ClosedByInterruptException.class, InterruptedException.class); @@ -188,7 +188,7 @@ public RetryResult afterEval(Exception exception, RetryResult retryResult) { }; - HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.newBuilder().addInterceptors(interceptor).build(); + ExceptionHandler handler = ExceptionHandler.newBuilder().addInterceptors(interceptor).build(); thrown.expect(NullPointerException.class); handler.accept(new Exception()); } @@ -210,7 +210,7 @@ public RetryResult afterEval(Exception exception, RetryResult retryResult) { }; - HttpExceptionRetryAlgorithm handler = HttpExceptionRetryAlgorithm.newBuilder().addInterceptors(interceptor).build(); + ExceptionHandler handler = ExceptionHandler.newBuilder().addInterceptors(interceptor).build(); thrown.expect(NullPointerException.class); handler.accept(new Exception()); } diff --git a/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java b/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java index 44c510a3c984..11bdae8b2807 100644 --- a/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java +++ b/google-cloud-core/src/test/java/com/google/cloud/SerializationTest.java @@ -29,8 +29,7 @@ public class SerializationTest extends BaseSerializationTest { private static final BaseServiceException BASE_SERVICE_EXCEPTION = new BaseServiceException(42, "message", "reason", true); - private static final HttpExceptionRetryAlgorithm EXCEPTION_HANDLER = - HttpExceptionRetryAlgorithm.getDefaultInstance(); + private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.getDefaultInstance(); private static final Identity IDENTITY = Identity.allAuthenticatedUsers(); private static final PageImpl PAGE = new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2"));