From 84a10b778417920483f6a9f1e9484b775143acb9 Mon Sep 17 00:00:00 2001 From: Liam Huffman Date: Tue, 12 Aug 2025 17:41:33 -0700 Subject: [PATCH 1/7] feat(bigquery): Add custom ExceptionHandler to BigQueryOptions --- .../cloud/bigquery/BigQueryBaseService.java | 2 +- .../google/cloud/bigquery/BigQueryImpl.java | 62 +++++++------- .../cloud/bigquery/BigQueryOptions.java | 37 ++++++++ .../google/cloud/bigquery/ConnectionImpl.java | 12 +-- .../cloud/bigquery/TableDataWriteChannel.java | 4 +- .../cloud/bigquery/BigQueryExceptionTest.java | 84 +++++++++++++++++++ 6 files changed, 161 insertions(+), 40 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryBaseService.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryBaseService.java index 976015873..bdcefb3d9 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryBaseService.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryBaseService.java @@ -26,7 +26,7 @@ protected BigQueryBaseService(ServiceOptions options) { super(options); } - public static final ExceptionHandler BIGQUERY_EXCEPTION_HANDLER = + public static final ExceptionHandler DEFAULT_BIGQUERY_EXCEPTION_HANDLER = ExceptionHandler.newBuilder() .abortOn(RuntimeException.class) .retryOn(java.net.ConnectException.class) // retry on Connection Exception 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 a32a600c3..298de338d 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 @@ -293,7 +293,7 @@ public com.google.api.services.bigquery.model.Dataset call() throws IOException } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -340,7 +340,7 @@ public com.google.api.services.bigquery.model.Table call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -396,7 +396,7 @@ public com.google.api.services.bigquery.model.Routine call() throws IOException } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -490,7 +490,7 @@ public com.google.api.services.bigquery.model.Job call() throws IOException { ? RetryOption.mergeToSettings( getOptions().getRetrySettings(), getRetryOptions(optionsMap)) : getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), getBigQueryRetryConfig(optionsMap) != null ? getBigQueryRetryConfig(optionsMap) @@ -586,7 +586,7 @@ public com.google.api.services.bigquery.model.Dataset call() throws IOException } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -652,7 +652,7 @@ private static Page listDatasets( } }, serviceOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + serviceOptions.getExceptionHandler(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -704,7 +704,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -755,7 +755,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -801,7 +801,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -847,7 +847,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -891,7 +891,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -932,7 +932,7 @@ public com.google.api.services.bigquery.model.Dataset call() throws IOException } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -979,7 +979,7 @@ public com.google.api.services.bigquery.model.Table call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1025,7 +1025,7 @@ public com.google.api.services.bigquery.model.Model call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1071,7 +1071,7 @@ public com.google.api.services.bigquery.model.Routine call() throws IOException } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1125,7 +1125,7 @@ public com.google.api.services.bigquery.model.Table call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1184,7 +1184,7 @@ public com.google.api.services.bigquery.model.Model call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1243,7 +1243,7 @@ public com.google.api.services.bigquery.model.Routine call() throws IOException } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1461,7 +1461,7 @@ public Tuple> cal } }, serviceOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + serviceOptions.getExceptionHandler(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -1502,7 +1502,7 @@ public Tuple> cal } }, serviceOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + serviceOptions.getExceptionHandler(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -1543,7 +1543,7 @@ private static Page listRoutines( } }, serviceOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + serviceOptions.getExceptionHandler(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -1625,7 +1625,7 @@ public TableDataInsertAllResponse call() throws Exception { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1719,7 +1719,7 @@ public TableDataList call() throws IOException { } }, serviceOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + serviceOptions.getExceptionHandler(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -1793,7 +1793,7 @@ public com.google.api.services.bigquery.model.Job call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1850,7 +1850,7 @@ public Tuple> call( } }, serviceOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + serviceOptions.getExceptionHandler(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -1905,7 +1905,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1992,7 +1992,7 @@ public com.google.api.services.bigquery.model.QueryResponse call() } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), DEFAULT_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -2169,7 +2169,7 @@ public GetQueryResultsResponse call() throws IOException { } }, serviceOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + serviceOptions.getExceptionHandler(), serviceOptions.getClock(), DEFAULT_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -2240,7 +2240,7 @@ public com.google.api.services.bigquery.model.Policy call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -2286,7 +2286,7 @@ public com.google.api.services.bigquery.model.Policy call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -2334,7 +2334,7 @@ public com.google.api.services.bigquery.model.TestIamPermissionsResponse call() } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java index 36eeb2086..4efad6bcb 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java @@ -17,6 +17,8 @@ package com.google.cloud.bigquery; import com.google.api.core.BetaApi; +import com.google.cloud.BaseService; +import com.google.cloud.ExceptionHandler; import com.google.cloud.ServiceDefaults; import com.google.cloud.ServiceOptions; import com.google.cloud.ServiceRpc; @@ -43,6 +45,7 @@ public class BigQueryOptions extends ServiceOptions { private JobCreationMode defaultJobCreationMode = JobCreationMode.JOB_CREATION_MODE_UNSPECIFIED; private boolean enableOpenTelemetryTracing; private Tracer openTelemetryTracer; + private ExceptionHandler exceptionHandler; public static class DefaultBigQueryFactory implements BigQueryFactory { @@ -70,11 +73,14 @@ public static class Builder extends ServiceOptions.Builder exception) { + if (!customExceptionHandler) { + exceptionHandlerBuilder = ExceptionHandler.newBuilder(); + customExceptionHandler = true; + } + this.exceptionHandlerBuilder.abortOn(exception); + return this; + } + + public Builder retryOn(Class exception) { + if (!customExceptionHandler) { + exceptionHandlerBuilder = ExceptionHandler.newBuilder(); + customExceptionHandler = true; + } + this.exceptionHandlerBuilder.retryOn(exception); + return this; + } + @Override public BigQueryOptions build() { return new BigQueryOptions(this); @@ -130,6 +154,15 @@ private BigQueryOptions(Builder builder) { this.useInt64Timestamps = builder.useInt64Timestamps; this.enableOpenTelemetryTracing = builder.enableOpenTelemetryTracing; this.openTelemetryTracer = builder.openTelemetryTracer; + if (builder.customExceptionHandler) { + this.exceptionHandler = + builder + .exceptionHandlerBuilder + .addInterceptors(BaseService.EXCEPTION_HANDLER_INTERCEPTOR) + .build(); + } else { + this.exceptionHandler = BigQueryBaseService.DEFAULT_BIGQUERY_EXCEPTION_HANDLER; + } } private static class BigQueryDefaults implements ServiceDefaults { @@ -221,6 +254,10 @@ public Tracer getOpenTelemetryTracer() { return openTelemetryTracer; } + public ExceptionHandler getExceptionHandler() { + return exceptionHandler; + } + @SuppressWarnings("unchecked") @Override public Builder toBuilder() { diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java index b12ad8556..e90af8977 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java @@ -474,7 +474,7 @@ private BigQueryResult queryRpc( .getBigQueryRpcV2() .queryRpcSkipExceptionTranslation(projectId, queryRequest), bigQueryOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + bigQueryOptions.getExceptionHandler(), bigQueryOptions.getClock(), retryConfig, false, @@ -932,7 +932,7 @@ private Job getQueryJobRpc(JobId jobId) { completeJobId.getJob(), completeJobId.getLocation()), bigQueryOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + bigQueryOptions.getExceptionHandler(), bigQueryOptions.getClock(), EMPTY_RETRY_CONFIG, false, @@ -979,7 +979,7 @@ TableDataList tableDataListRpc(TableId destinationTable, String pageToken) { connectionSettings.getMaxResultPerPage(), pageToken), bigQueryOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + bigQueryOptions.getExceptionHandler(), bigQueryOptions.getClock(), EMPTY_RETRY_CONFIG, false, @@ -1212,7 +1212,7 @@ GetQueryResultsResponse getQueryResultsFirstPage(JobId jobId) { connectionSettings.getMaxResultPerPage(), timeoutMs), bigQueryOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + bigQueryOptions.getExceptionHandler(), bigQueryOptions.getClock(), retryConfig, false, @@ -1477,7 +1477,7 @@ com.google.api.services.bigquery.model.Job createQueryJob( .getBigQueryRpcV2() .createJobForQuerySkipExceptionTranslation(jobPb), bigQueryOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + bigQueryOptions.getExceptionHandler(), bigQueryOptions.getClock(), retryConfig, false, @@ -1522,7 +1522,7 @@ com.google.api.services.bigquery.model.Job createDryRunJob(String sql) { .getBigQueryRpcV2() .createJobForQuerySkipExceptionTranslation(jobPb), bigQueryOptions.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + bigQueryOptions.getExceptionHandler(), bigQueryOptions.getClock(), retryConfig, false, 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 cc64ec72f..8b876696d 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 @@ -77,7 +77,7 @@ public com.google.api.services.bigquery.model.Job call() throws IOException { } }, getOptions().getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + getOptions().getExceptionHandler(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -126,7 +126,7 @@ public String call() throws IOException { } }, options.getRetrySettings(), - BigQueryBaseService.BIGQUERY_EXCEPTION_HANDLER, + options.getExceptionHandler(), options.getClock(), EMPTY_RETRY_CONFIG, options.isOpenTelemetryTracingEnabled(), diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java index 41915e283..a9c627e8c 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java @@ -21,7 +21,10 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -30,8 +33,12 @@ import com.google.api.client.http.HttpResponseException; import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; +import com.google.cloud.bigquery.spi.v2.BigQueryRpc; +import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc; import java.io.IOException; import java.net.SocketTimeoutException; +import java.util.HashMap; +import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; @@ -158,4 +165,81 @@ public void testTranslateAndThrow() throws Exception { verify(exceptionMock, times(2)).getCause(); } } + + @Test + public void testDefaultExceptionHandler() throws java.io.IOException { + BigQueryOptions defaultOptions = BigQueryOptions.getDefaultInstance(); + DatasetInfo info = DatasetInfo.newBuilder("dataset").build(); + Dataset dataset = null; + + final com.google.api.services.bigquery.model.Dataset datasetPb = + info.setProjectId(defaultOptions.getProjectId()).toPb(); + final Map optionsMap = new HashMap<>(); + + BigQueryOptions mockOptions = spy(defaultOptions); + HttpBigQueryRpc bigQueryRpcMock = mock(HttpBigQueryRpc.class); + doReturn(bigQueryRpcMock).when(mockOptions).getBigQueryRpcV2(); + // java.net.SocketException is retry-able in the default exception handler. + doThrow(java.net.SocketException.class) + .when(bigQueryRpcMock) + .createSkipExceptionTranslation(datasetPb, optionsMap); + + BigQuery bigquery = mockOptions.getService(); + try { + dataset = bigquery.create(info); + } catch (BigQueryException e) { + assertEquals(e.getCause().getClass(), java.net.SocketException.class); + assertNull(dataset); + } finally { + verify(bigQueryRpcMock, times(6)).createSkipExceptionTranslation(datasetPb, optionsMap); + } + } + + @Test + public void testCustomExceptionHandler() throws java.io.IOException { + BigQueryOptions defaultOptions = + BigQueryOptions.getDefaultInstance().toBuilder() + .abortOn(RuntimeException.class) + .retryOn(java.util.EmptyStackException.class) + .build(); + DatasetInfo info = DatasetInfo.newBuilder("dataset").build(); + Dataset dataset = null; + + final com.google.api.services.bigquery.model.Dataset datasetPb = + info.setProjectId(defaultOptions.getProjectId()).toPb(); + final Map optionsMap = new HashMap<>(); + + BigQueryOptions mockOptions = spy(defaultOptions); + HttpBigQueryRpc bigQueryRpcRetryMock = mock(HttpBigQueryRpc.class); + doReturn(bigQueryRpcRetryMock).when(mockOptions).getBigQueryRpcV2(); + doThrow(java.util.EmptyStackException.class) + .when(bigQueryRpcRetryMock) + .createSkipExceptionTranslation(datasetPb, optionsMap); + + BigQuery bigquery = mockOptions.getService(); + try { + dataset = bigquery.create(info); + } catch (BigQueryException e) { + assertEquals(e.getCause().getClass(), java.util.EmptyStackException.class); + assertNull(dataset); + } finally { + verify(bigQueryRpcRetryMock, times(6)).createSkipExceptionTranslation(datasetPb, optionsMap); + } + + BigQueryOptions mockOptionsAbort = spy(defaultOptions); + HttpBigQueryRpc bigQueryRpcAbortMock = mock(HttpBigQueryRpc.class); + doReturn(bigQueryRpcAbortMock).when(mockOptionsAbort).getBigQueryRpcV2(); + doThrow(RuntimeException.class) + .when(bigQueryRpcAbortMock) + .createSkipExceptionTranslation(datasetPb, optionsMap); + bigquery = mockOptionsAbort.getService(); + try { + dataset = bigquery.create(info); + } catch (BigQueryException e) { + assertEquals(e.getCause().getClass(), RuntimeException.class); + assertNull(dataset); + } finally { + verify(bigQueryRpcAbortMock, times(1)).createSkipExceptionTranslation(datasetPb, optionsMap); + } + } } From 669e64323a2a35636db5caf4f4896d9ca0fbd31a Mon Sep 17 00:00:00 2001 From: Liam Huffman Date: Wed, 13 Aug 2025 11:02:42 -0700 Subject: [PATCH 2/7] add default project id to tests --- .../com/google/cloud/bigquery/BigQueryExceptionTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java index a9c627e8c..e64eecef4 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java @@ -33,6 +33,7 @@ import com.google.api.client.http.HttpResponseException; import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; +import com.google.cloud.ServiceOptions; import com.google.cloud.bigquery.spi.v2.BigQueryRpc; import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc; import java.io.IOException; @@ -168,7 +169,8 @@ public void testTranslateAndThrow() throws Exception { @Test public void testDefaultExceptionHandler() throws java.io.IOException { - BigQueryOptions defaultOptions = BigQueryOptions.getDefaultInstance(); + BigQueryOptions defaultOptions = + BigQueryOptions.newBuilder().setProjectId(ServiceOptions.getDefaultProjectId()).build(); DatasetInfo info = DatasetInfo.newBuilder("dataset").build(); Dataset dataset = null; @@ -199,6 +201,7 @@ public void testDefaultExceptionHandler() throws java.io.IOException { public void testCustomExceptionHandler() throws java.io.IOException { BigQueryOptions defaultOptions = BigQueryOptions.getDefaultInstance().toBuilder() + .setProjectId(ServiceOptions.getDefaultProjectId()) .abortOn(RuntimeException.class) .retryOn(java.util.EmptyStackException.class) .build(); From a57704dc2fc895893389c15e68eadfc7c32c1dd0 Mon Sep 17 00:00:00 2001 From: Liam Huffman Date: Wed, 13 Aug 2025 11:11:50 -0700 Subject: [PATCH 3/7] Add dummy project id to tests --- .../com/google/cloud/bigquery/BigQueryExceptionTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java index e64eecef4..bbffef5d1 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java @@ -33,7 +33,6 @@ import com.google.api.client.http.HttpResponseException; import com.google.cloud.BaseServiceException; import com.google.cloud.RetryHelper.RetryHelperException; -import com.google.cloud.ServiceOptions; import com.google.cloud.bigquery.spi.v2.BigQueryRpc; import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc; import java.io.IOException; @@ -170,7 +169,7 @@ public void testTranslateAndThrow() throws Exception { @Test public void testDefaultExceptionHandler() throws java.io.IOException { BigQueryOptions defaultOptions = - BigQueryOptions.newBuilder().setProjectId(ServiceOptions.getDefaultProjectId()).build(); + BigQueryOptions.newBuilder().setProjectId("project-id").build(); DatasetInfo info = DatasetInfo.newBuilder("dataset").build(); Dataset dataset = null; @@ -201,7 +200,7 @@ public void testDefaultExceptionHandler() throws java.io.IOException { public void testCustomExceptionHandler() throws java.io.IOException { BigQueryOptions defaultOptions = BigQueryOptions.getDefaultInstance().toBuilder() - .setProjectId(ServiceOptions.getDefaultProjectId()) + .setProjectId("project-id") .abortOn(RuntimeException.class) .retryOn(java.util.EmptyStackException.class) .build(); From 135391397a2ba01aaf8ef0d997dc624743a3eca7 Mon Sep 17 00:00:00 2001 From: Liam Huffman Date: Wed, 13 Aug 2025 11:18:09 -0700 Subject: [PATCH 4/7] Remove defaultInstance() from test --- .../java/com/google/cloud/bigquery/BigQueryExceptionTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java index bbffef5d1..dc3679667 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java @@ -199,7 +199,7 @@ public void testDefaultExceptionHandler() throws java.io.IOException { @Test public void testCustomExceptionHandler() throws java.io.IOException { BigQueryOptions defaultOptions = - BigQueryOptions.getDefaultInstance().toBuilder() + BigQueryOptions.newBuilder() .setProjectId("project-id") .abortOn(RuntimeException.class) .retryOn(java.util.EmptyStackException.class) From 2b0152fcefd56806925613e619b0ae4d32614bc6 Mon Sep 17 00:00:00 2001 From: Liam Huffman Date: Tue, 26 Aug 2025 11:46:46 -0700 Subject: [PATCH 5/7] add option to specify retry algo instead of just abortOn/retryOn --- .../google/cloud/bigquery/BigQueryImpl.java | 62 +++++++++---------- .../cloud/bigquery/BigQueryOptions.java | 36 +++-------- .../google/cloud/bigquery/ConnectionImpl.java | 12 ++-- .../cloud/bigquery/TableDataWriteChannel.java | 4 +- .../cloud/bigquery/BigQueryExceptionTest.java | 10 ++- 5 files changed, 57 insertions(+), 67 deletions(-) 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 298de338d..088d15c09 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 @@ -293,7 +293,7 @@ public com.google.api.services.bigquery.model.Dataset call() throws IOException } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -340,7 +340,7 @@ public com.google.api.services.bigquery.model.Table call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -396,7 +396,7 @@ public com.google.api.services.bigquery.model.Routine call() throws IOException } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -490,7 +490,7 @@ public com.google.api.services.bigquery.model.Job call() throws IOException { ? RetryOption.mergeToSettings( getOptions().getRetrySettings(), getRetryOptions(optionsMap)) : getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), getBigQueryRetryConfig(optionsMap) != null ? getBigQueryRetryConfig(optionsMap) @@ -586,7 +586,7 @@ public com.google.api.services.bigquery.model.Dataset call() throws IOException } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -652,7 +652,7 @@ private static Page listDatasets( } }, serviceOptions.getRetrySettings(), - serviceOptions.getExceptionHandler(), + serviceOptions.getResultRetryAlgorithm(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -704,7 +704,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -755,7 +755,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -801,7 +801,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -847,7 +847,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -891,7 +891,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -932,7 +932,7 @@ public com.google.api.services.bigquery.model.Dataset call() throws IOException } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -979,7 +979,7 @@ public com.google.api.services.bigquery.model.Table call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1025,7 +1025,7 @@ public com.google.api.services.bigquery.model.Model call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1071,7 +1071,7 @@ public com.google.api.services.bigquery.model.Routine call() throws IOException } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1125,7 +1125,7 @@ public com.google.api.services.bigquery.model.Table call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1184,7 +1184,7 @@ public com.google.api.services.bigquery.model.Model call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1243,7 +1243,7 @@ public com.google.api.services.bigquery.model.Routine call() throws IOException } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1461,7 +1461,7 @@ public Tuple> cal } }, serviceOptions.getRetrySettings(), - serviceOptions.getExceptionHandler(), + serviceOptions.getResultRetryAlgorithm(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -1502,7 +1502,7 @@ public Tuple> cal } }, serviceOptions.getRetrySettings(), - serviceOptions.getExceptionHandler(), + serviceOptions.getResultRetryAlgorithm(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -1543,7 +1543,7 @@ private static Page listRoutines( } }, serviceOptions.getRetrySettings(), - serviceOptions.getExceptionHandler(), + serviceOptions.getResultRetryAlgorithm(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -1625,7 +1625,7 @@ public TableDataInsertAllResponse call() throws Exception { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1719,7 +1719,7 @@ public TableDataList call() throws IOException { } }, serviceOptions.getRetrySettings(), - serviceOptions.getExceptionHandler(), + serviceOptions.getResultRetryAlgorithm(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -1793,7 +1793,7 @@ public com.google.api.services.bigquery.model.Job call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1850,7 +1850,7 @@ public Tuple> call( } }, serviceOptions.getRetrySettings(), - serviceOptions.getExceptionHandler(), + serviceOptions.getResultRetryAlgorithm(), serviceOptions.getClock(), EMPTY_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -1905,7 +1905,7 @@ public Boolean call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -1992,7 +1992,7 @@ public com.google.api.services.bigquery.model.QueryResponse call() } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), DEFAULT_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -2169,7 +2169,7 @@ public GetQueryResultsResponse call() throws IOException { } }, serviceOptions.getRetrySettings(), - serviceOptions.getExceptionHandler(), + serviceOptions.getResultRetryAlgorithm(), serviceOptions.getClock(), DEFAULT_RETRY_CONFIG, serviceOptions.isOpenTelemetryTracingEnabled(), @@ -2240,7 +2240,7 @@ public com.google.api.services.bigquery.model.Policy call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -2286,7 +2286,7 @@ public com.google.api.services.bigquery.model.Policy call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -2334,7 +2334,7 @@ public com.google.api.services.bigquery.model.TestIamPermissionsResponse call() } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java index 4efad6bcb..d9b8342ed 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java @@ -17,6 +17,7 @@ package com.google.cloud.bigquery; import com.google.api.core.BetaApi; +import com.google.api.gax.retrying.ResultRetryAlgorithm; import com.google.cloud.BaseService; import com.google.cloud.ExceptionHandler; import com.google.cloud.ServiceDefaults; @@ -45,7 +46,7 @@ public class BigQueryOptions extends ServiceOptions { private JobCreationMode defaultJobCreationMode = JobCreationMode.JOB_CREATION_MODE_UNSPECIFIED; private boolean enableOpenTelemetryTracing; private Tracer openTelemetryTracer; - private ExceptionHandler exceptionHandler; + private ResultRetryAlgorithm resultRetryAlgorithm; public static class DefaultBigQueryFactory implements BigQueryFactory { @@ -74,7 +75,7 @@ public static class Builder extends ServiceOptions.Builder resultRetryAlgorithm; private Builder() {} @@ -124,21 +125,8 @@ public Builder setOpenTelemetryTracer(Tracer tracer) { return this; } - public Builder abortOn(Class exception) { - if (!customExceptionHandler) { - exceptionHandlerBuilder = ExceptionHandler.newBuilder(); - customExceptionHandler = true; - } - this.exceptionHandlerBuilder.abortOn(exception); - return this; - } - - public Builder retryOn(Class exception) { - if (!customExceptionHandler) { - exceptionHandlerBuilder = ExceptionHandler.newBuilder(); - customExceptionHandler = true; - } - this.exceptionHandlerBuilder.retryOn(exception); + public Builder setResultRetryAlgorithm(ResultRetryAlgorithm resultRetryAlgorithm) { + this.resultRetryAlgorithm = resultRetryAlgorithm; return this; } @@ -154,14 +142,10 @@ private BigQueryOptions(Builder builder) { this.useInt64Timestamps = builder.useInt64Timestamps; this.enableOpenTelemetryTracing = builder.enableOpenTelemetryTracing; this.openTelemetryTracer = builder.openTelemetryTracer; - if (builder.customExceptionHandler) { - this.exceptionHandler = - builder - .exceptionHandlerBuilder - .addInterceptors(BaseService.EXCEPTION_HANDLER_INTERCEPTOR) - .build(); + if (builder.resultRetryAlgorithm != null) { + this.resultRetryAlgorithm = builder.resultRetryAlgorithm; } else { - this.exceptionHandler = BigQueryBaseService.DEFAULT_BIGQUERY_EXCEPTION_HANDLER; + this.resultRetryAlgorithm = BigQueryBaseService.DEFAULT_BIGQUERY_EXCEPTION_HANDLER; } } @@ -254,8 +238,8 @@ public Tracer getOpenTelemetryTracer() { return openTelemetryTracer; } - public ExceptionHandler getExceptionHandler() { - return exceptionHandler; + public ResultRetryAlgorithm getResultRetryAlgorithm() { + return resultRetryAlgorithm; } @SuppressWarnings("unchecked") diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java index e90af8977..c3465c33a 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/ConnectionImpl.java @@ -474,7 +474,7 @@ private BigQueryResult queryRpc( .getBigQueryRpcV2() .queryRpcSkipExceptionTranslation(projectId, queryRequest), bigQueryOptions.getRetrySettings(), - bigQueryOptions.getExceptionHandler(), + bigQueryOptions.getResultRetryAlgorithm(), bigQueryOptions.getClock(), retryConfig, false, @@ -932,7 +932,7 @@ private Job getQueryJobRpc(JobId jobId) { completeJobId.getJob(), completeJobId.getLocation()), bigQueryOptions.getRetrySettings(), - bigQueryOptions.getExceptionHandler(), + bigQueryOptions.getResultRetryAlgorithm(), bigQueryOptions.getClock(), EMPTY_RETRY_CONFIG, false, @@ -979,7 +979,7 @@ TableDataList tableDataListRpc(TableId destinationTable, String pageToken) { connectionSettings.getMaxResultPerPage(), pageToken), bigQueryOptions.getRetrySettings(), - bigQueryOptions.getExceptionHandler(), + bigQueryOptions.getResultRetryAlgorithm(), bigQueryOptions.getClock(), EMPTY_RETRY_CONFIG, false, @@ -1212,7 +1212,7 @@ GetQueryResultsResponse getQueryResultsFirstPage(JobId jobId) { connectionSettings.getMaxResultPerPage(), timeoutMs), bigQueryOptions.getRetrySettings(), - bigQueryOptions.getExceptionHandler(), + bigQueryOptions.getResultRetryAlgorithm(), bigQueryOptions.getClock(), retryConfig, false, @@ -1477,7 +1477,7 @@ com.google.api.services.bigquery.model.Job createQueryJob( .getBigQueryRpcV2() .createJobForQuerySkipExceptionTranslation(jobPb), bigQueryOptions.getRetrySettings(), - bigQueryOptions.getExceptionHandler(), + bigQueryOptions.getResultRetryAlgorithm(), bigQueryOptions.getClock(), retryConfig, false, @@ -1522,7 +1522,7 @@ com.google.api.services.bigquery.model.Job createDryRunJob(String sql) { .getBigQueryRpcV2() .createJobForQuerySkipExceptionTranslation(jobPb), bigQueryOptions.getRetrySettings(), - bigQueryOptions.getExceptionHandler(), + bigQueryOptions.getResultRetryAlgorithm(), bigQueryOptions.getClock(), retryConfig, false, 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 8b876696d..dde4c0818 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 @@ -77,7 +77,7 @@ public com.google.api.services.bigquery.model.Job call() throws IOException { } }, getOptions().getRetrySettings(), - getOptions().getExceptionHandler(), + getOptions().getResultRetryAlgorithm(), getOptions().getClock(), EMPTY_RETRY_CONFIG, getOptions().isOpenTelemetryTracingEnabled(), @@ -126,7 +126,7 @@ public String call() throws IOException { } }, options.getRetrySettings(), - options.getExceptionHandler(), + options.getResultRetryAlgorithm(), options.getClock(), EMPTY_RETRY_CONFIG, options.isOpenTelemetryTracingEnabled(), diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java index dc3679667..ac100116d 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java @@ -32,6 +32,7 @@ import com.google.api.client.http.HttpHeaders; import com.google.api.client.http.HttpResponseException; import com.google.cloud.BaseServiceException; +import com.google.cloud.ExceptionHandler; import com.google.cloud.RetryHelper.RetryHelperException; import com.google.cloud.bigquery.spi.v2.BigQueryRpc; import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc; @@ -201,8 +202,13 @@ public void testCustomExceptionHandler() throws java.io.IOException { BigQueryOptions defaultOptions = BigQueryOptions.newBuilder() .setProjectId("project-id") - .abortOn(RuntimeException.class) - .retryOn(java.util.EmptyStackException.class) + .setResultRetryAlgorithm( + ExceptionHandler.newBuilder() + .abortOn(RuntimeException.class) + .retryOn(java.util.EmptyStackException.class) + .addInterceptors(BigQueryBaseService.EXCEPTION_HANDLER_INTERCEPTOR) + .build() + ) .build(); DatasetInfo info = DatasetInfo.newBuilder("dataset").build(); Dataset dataset = null; From 12f463bc7a72d45d0d2cdc3c4553906f02f71f25 Mon Sep 17 00:00:00 2001 From: Liam Huffman Date: Tue, 26 Aug 2025 11:48:57 -0700 Subject: [PATCH 6/7] fix formatting --- .../main/java/com/google/cloud/bigquery/BigQueryOptions.java | 2 -- .../java/com/google/cloud/bigquery/BigQueryExceptionTest.java | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java index d9b8342ed..9041dd2eb 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java @@ -18,8 +18,6 @@ import com.google.api.core.BetaApi; import com.google.api.gax.retrying.ResultRetryAlgorithm; -import com.google.cloud.BaseService; -import com.google.cloud.ExceptionHandler; import com.google.cloud.ServiceDefaults; import com.google.cloud.ServiceOptions; import com.google.cloud.ServiceRpc; diff --git a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java index ac100116d..8a2094b55 100644 --- a/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java +++ b/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryExceptionTest.java @@ -207,8 +207,7 @@ public void testCustomExceptionHandler() throws java.io.IOException { .abortOn(RuntimeException.class) .retryOn(java.util.EmptyStackException.class) .addInterceptors(BigQueryBaseService.EXCEPTION_HANDLER_INTERCEPTOR) - .build() - ) + .build()) .build(); DatasetInfo info = DatasetInfo.newBuilder("dataset").build(); Dataset dataset = null; From 2918cb4dad5c844f390e457cd7e12354c7e48463 Mon Sep 17 00:00:00 2001 From: Liam Huffman Date: Thu, 28 Aug 2025 12:03:39 -0700 Subject: [PATCH 7/7] remove custom exception handler from bq options --- .../main/java/com/google/cloud/bigquery/BigQueryOptions.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java index 9041dd2eb..7adb58d3a 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java @@ -72,14 +72,12 @@ public static class Builder extends ServiceOptions.Builder resultRetryAlgorithm; private Builder() {} private Builder(BigQueryOptions options) { super(options); - customExceptionHandler = false; } @Override