Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit f21e292

Browse files
committed
feat(bigquery): implement custom retry algo samples
1 parent 93bf582 commit f21e292

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_set_custom_retry_algorithm]
20+
import com.google.api.gax.retrying.ResultRetryAlgorithm;
21+
import com.google.cloud.ExceptionHandler;
22+
import com.google.cloud.bigquery.BigQuery;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import java.util.concurrent.TimeoutException;
25+
26+
public class SetCustomRetryAlgorithm {
27+
// In order to use a custom retry algorithm, you must implement a custom
28+
// Interceptor that implements the ExceptionHandler.Interceptor interface.
29+
public static final ExceptionHandler.Interceptor EXCEPTION_HANDLER_INTERCEPTOR =
30+
new ExceptionHandler.Interceptor() {
31+
public ExceptionHandler.Interceptor.RetryResult afterEval(
32+
Exception exception, ExceptionHandler.Interceptor.RetryResult retryResult) {
33+
return RetryResult.CONTINUE_EVALUATION;
34+
}
35+
36+
public ExceptionHandler.Interceptor.RetryResult beforeEval(Exception exception) {
37+
return RetryResult.CONTINUE_EVALUATION;
38+
}
39+
};
40+
41+
public static void main(String... args) {
42+
// TODO(developer): Replace projectId and exception classes before running
43+
// the sample.
44+
String projectId = "project-id";
45+
ResultRetryAlgorithm<?> retryAlgorithm =
46+
ExceptionHandler.newBuilder()
47+
.abortOn(RuntimeException.class)
48+
.retryOn(TimeoutException.class)
49+
.addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR)
50+
.build();
51+
setCustomRetryAlgorithm(projectId, retryAlgorithm);
52+
}
53+
54+
public static void setCustomRetryAlgorithm(
55+
String projectId, ResultRetryAlgorithm<?> retryAlgorithm) {
56+
BigQueryOptions options =
57+
BigQueryOptions.newBuilder()
58+
.setProjectId(projectId)
59+
.setResultRetryAlgorithm(retryAlgorithm)
60+
.build();
61+
62+
BigQuery bigquery = options.getService();
63+
64+
System.out.println(bigquery.getOptions().getResultRetryAlgorithm());
65+
}
66+
}
67+
// [END bigquery_set_custom_retry_algorithm]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import java.util.logging.Level;
24+
import java.util.logging.Logger;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
public class SetCustomRetryAlgorithmTest {
30+
private final Logger log = Logger.getLogger(this.getClass().getName());
31+
private ByteArrayOutputStream bout;
32+
private PrintStream out;
33+
private PrintStream originalPrintStream;
34+
35+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
36+
37+
@Before
38+
public void setUp() throws Exception {
39+
bout = new ByteArrayOutputStream();
40+
out = new PrintStream(bout);
41+
originalPrintStream = System.out;
42+
System.setOut(out);
43+
}
44+
45+
@After
46+
public void tearDown() {
47+
System.out.flush();
48+
System.setOut(originalPrintStream);
49+
log.log(Level.INFO, "\n" + bout.toString());
50+
}
51+
52+
@Test
53+
public void testSetCustomRetryAlgorithm() {
54+
ResultRetryAlgorithm<?> retryAlgorithm =
55+
ExceptionHandler.newBuilder()
56+
.abortOn(RuntimeException.class)
57+
.retryOn(TimeoutException.class)
58+
.addInterceptors(SetCustomRetryAlgorithm.EXCEPTION_HANDLER_INTERCEPTOR)
59+
.build();
60+
61+
SetCustomRetryAlgorithm.setCustomRetryAlgorithm(PROJECT_ID, retryAlgorithm);
62+
assertThat(bout.toString().contains("r"));
63+
}
64+
}

0 commit comments

Comments
 (0)