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

Commit 9496158

Browse files
authored
feat: add an enhanced layer for BigQuery Storage v1beta2 client (#48)
* Create a shim layer for the BigQuery Storage API v1beta2. This change adds a simple shim layer to the BigQuery Read API client. It is modeled after the shim layer in the BigQuery Storage API v1beta1, where the parameters are simple pass-throughs to the underlying gRPC client stub. The shim currently does nothing but read default values from the base client layer and pass them back through. * Add a resumption strategy for v1beta2 ReadRows. This change modifies the configuration for the v1beta2 ReadRows API to add a resumption strategy. The resumption strategy will allow the connection to be resumed transparently in the case of transient errors. * Address feedback about "pass-through" comments. * Formatting files with mvn com.coveo:fmt-maven-plugin:format
1 parent 8c124a2 commit 9496158

File tree

10 files changed

+1709
-0
lines changed

10 files changed

+1709
-0
lines changed

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1beta2/BigQueryReadClient.java

Lines changed: 376 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
* Copyright 2020 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+
* https://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+
package com.google.cloud.bigquery.storage.v1beta2;
17+
18+
import com.google.api.core.ApiFunction;
19+
import com.google.api.core.BetaApi;
20+
import com.google.api.gax.core.GoogleCredentialsProvider;
21+
import com.google.api.gax.core.InstantiatingExecutorProvider;
22+
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
23+
import com.google.api.gax.rpc.ApiClientHeaderProvider;
24+
import com.google.api.gax.rpc.ClientContext;
25+
import com.google.api.gax.rpc.ClientSettings;
26+
import com.google.api.gax.rpc.ServerStreamingCallSettings;
27+
import com.google.api.gax.rpc.TransportChannelProvider;
28+
import com.google.api.gax.rpc.UnaryCallSettings;
29+
import com.google.cloud.bigquery.storage.v1beta2.stub.EnhancedBigQueryReadStubSettings;
30+
import java.io.IOException;
31+
import java.util.List;
32+
33+
/**
34+
* Settings class to configure an instance of {@link BigQueryReadClient}.
35+
*
36+
* <p>The default instance has everything set to sensible defaults:
37+
*
38+
* <ul>
39+
* <li>The default service address (bigquerystorage.googleapis.com) and default port (443) are
40+
* used.
41+
* <li>Credentials are acquired automatically through Application Default Credentials.
42+
* <li>Retries are configured for idempotent methods but not for non-idempotent methods.
43+
* </ul>
44+
*
45+
* <p>The builder of this class is recursive, so contained classes are themselves builders. When
46+
* build() is called, the tree of builders is called to create the complete settings object.
47+
*
48+
* <p>For example, to set the total timeout of createReadSession to 30 seconds:
49+
*
50+
* <pre>
51+
* <code>
52+
* BigQueryReadSettings.Builder BigQueryReadSettingsBuilder =
53+
* BigQueryReadSettings.newBuilder();
54+
* BigQueryReadSettingsBuilder.createReadSessionSettings().getRetrySettings().toBuilder()
55+
* .setTotalTimeout(Duration.ofSeconds(30));
56+
* BigQueryReadSettings BigQueryReadSettings = BigQueryReadSettingsBuilder.build();
57+
* </code>
58+
* </pre>
59+
*/
60+
@BetaApi
61+
public class BigQueryReadSettings extends ClientSettings<BigQueryReadSettings> {
62+
/** Returns the object with the settings used for calls to createReadSession. */
63+
public UnaryCallSettings<CreateReadSessionRequest, ReadSession> createReadSessionSettings() {
64+
return getTypedStubSettings().createReadSessionSettings();
65+
}
66+
67+
/** Returns the object with the settings used for calls to readRows. */
68+
public ServerStreamingCallSettings<ReadRowsRequest, ReadRowsResponse> readRowsSettings() {
69+
return getTypedStubSettings().readRowsSettings();
70+
}
71+
72+
/** Returns the object with the settings used for calls to splitReadStream. */
73+
public UnaryCallSettings<SplitReadStreamRequest, SplitReadStreamResponse>
74+
splitReadStreamSettings() {
75+
return getTypedStubSettings().splitReadStreamSettings();
76+
}
77+
78+
EnhancedBigQueryReadStubSettings getTypedStubSettings() {
79+
return (EnhancedBigQueryReadStubSettings) getStubSettings();
80+
}
81+
82+
public static final BigQueryReadSettings create(EnhancedBigQueryReadStubSettings stub)
83+
throws IOException {
84+
return new BigQueryReadSettings.Builder(stub.toBuilder()).build();
85+
}
86+
87+
/** Returns a builder for the default ExecutorProvider for this service. */
88+
public static InstantiatingExecutorProvider.Builder defaultExecutorProviderBuilder() {
89+
return EnhancedBigQueryReadStubSettings.defaultExecutorProviderBuilder();
90+
}
91+
92+
/** Returns the default service endpoint. */
93+
public static String getDefaultEndpoint() {
94+
return EnhancedBigQueryReadStubSettings.getDefaultEndpoint();
95+
}
96+
97+
/** Returns the default service scopes. */
98+
public static List<String> getDefaultServiceScopes() {
99+
return EnhancedBigQueryReadStubSettings.getDefaultServiceScopes();
100+
}
101+
102+
/** Returns a builder for the default credentials for this service. */
103+
public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() {
104+
return EnhancedBigQueryReadStubSettings.defaultCredentialsProviderBuilder();
105+
}
106+
107+
/** Returns a builder for the default ChannelProvider for this service. */
108+
public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() {
109+
return EnhancedBigQueryReadStubSettings.defaultGrpcTransportProviderBuilder();
110+
}
111+
112+
public static TransportChannelProvider defaultTransportChannelProvider() {
113+
return EnhancedBigQueryReadStubSettings.defaultTransportChannelProvider();
114+
}
115+
116+
@BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
117+
public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuilder() {
118+
return EnhancedBigQueryReadStubSettings.defaultApiClientHeaderProviderBuilder();
119+
}
120+
121+
/** Returns a new builder for this class. */
122+
public static Builder newBuilder() {
123+
return Builder.createDefault();
124+
}
125+
126+
/** Returns a new builder for this class. */
127+
public static Builder newBuilder(ClientContext clientContext) {
128+
return new Builder(clientContext);
129+
}
130+
131+
/** Returns a builder containing all the values of this settings class. */
132+
public Builder toBuilder() {
133+
return new Builder(this);
134+
}
135+
136+
protected BigQueryReadSettings(Builder settingsBuilder) throws IOException {
137+
super(settingsBuilder);
138+
}
139+
140+
/** Builder for BigQueryReadSettings. */
141+
public static class Builder extends ClientSettings.Builder<BigQueryReadSettings, Builder> {
142+
protected Builder() throws IOException {
143+
this((ClientContext) null);
144+
}
145+
146+
protected Builder(ClientContext clientContext) {
147+
super(EnhancedBigQueryReadStubSettings.newBuilder(clientContext));
148+
}
149+
150+
private static Builder createDefault() {
151+
return new Builder(EnhancedBigQueryReadStubSettings.newBuilder());
152+
}
153+
154+
protected Builder(BigQueryReadSettings settings) {
155+
super(settings.getStubSettings().toBuilder());
156+
}
157+
158+
protected Builder(EnhancedBigQueryReadStubSettings.Builder stubSettings) {
159+
super(stubSettings);
160+
}
161+
162+
public EnhancedBigQueryReadStubSettings.Builder getStubSettingsBuilder() {
163+
return ((EnhancedBigQueryReadStubSettings.Builder) getStubSettings());
164+
}
165+
166+
// NEXT_MAJOR_VER: remove 'throws Exception'
167+
/**
168+
* Applies the given settings updater function to all of the unary API methods in this service.
169+
*
170+
* <p>Note: This method does not support applying settings to streaming methods.
171+
*/
172+
public Builder applyToAllUnaryMethods(
173+
ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> settingsUpdater) throws Exception {
174+
super.applyToAllUnaryMethods(
175+
getStubSettingsBuilder().unaryMethodSettingsBuilders(), settingsUpdater);
176+
return this;
177+
}
178+
179+
/** Returns the builder for the settings used for calls to createReadSession. */
180+
public UnaryCallSettings.Builder<CreateReadSessionRequest, ReadSession>
181+
createReadSessionSettings() {
182+
return getStubSettingsBuilder().createReadSessionSettings();
183+
}
184+
185+
/** Returns the builder for the settings used for calls to readRows. */
186+
public ServerStreamingCallSettings.Builder<ReadRowsRequest, ReadRowsResponse>
187+
readRowsSettings() {
188+
return getStubSettingsBuilder().readRowsSettings();
189+
}
190+
191+
/** Returns the builder for the settings used for calls to splitReadStream. */
192+
public UnaryCallSettings.Builder<SplitReadStreamRequest, SplitReadStreamResponse>
193+
splitReadStreamSettings() {
194+
return getStubSettingsBuilder().splitReadStreamSettings();
195+
}
196+
197+
@Override
198+
public BigQueryReadSettings build() throws IOException {
199+
return new BigQueryReadSettings(this);
200+
}
201+
}
202+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2020 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+
* https://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+
package com.google.cloud.bigquery.storage.v1beta2.stub;
17+
18+
import com.google.api.core.InternalApi;
19+
import com.google.api.gax.core.BackgroundResource;
20+
import com.google.api.gax.rpc.ClientContext;
21+
import com.google.api.gax.rpc.ServerStreamingCallable;
22+
import com.google.api.gax.rpc.UnaryCallable;
23+
import com.google.cloud.bigquery.storage.v1beta2.CreateReadSessionRequest;
24+
import com.google.cloud.bigquery.storage.v1beta2.ReadRowsRequest;
25+
import com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse;
26+
import com.google.cloud.bigquery.storage.v1beta2.ReadSession;
27+
import com.google.cloud.bigquery.storage.v1beta2.SplitReadStreamRequest;
28+
import com.google.cloud.bigquery.storage.v1beta2.SplitReadStreamResponse;
29+
import java.io.IOException;
30+
import java.util.concurrent.TimeUnit;
31+
32+
/**
33+
* Enhanced stub class for BigQuery Storage API.
34+
*
35+
* <p>This class is for advanced usage and reflects the underlying API directly.
36+
*/
37+
public class EnhancedBigQueryReadStub implements BackgroundResource {
38+
private final GrpcBigQueryReadStub stub;
39+
40+
public static EnhancedBigQueryReadStub create(EnhancedBigQueryReadStubSettings settings)
41+
throws IOException {
42+
// Configure the base settings.
43+
BigQueryReadStubSettings.Builder baseSettingsBuilder =
44+
BigQueryReadStubSettings.newBuilder()
45+
.setTransportChannelProvider(settings.getTransportChannelProvider())
46+
.setEndpoint(settings.getEndpoint())
47+
.setHeaderProvider(settings.getHeaderProvider())
48+
.setCredentialsProvider(settings.getCredentialsProvider())
49+
.setStreamWatchdogCheckInterval(settings.getStreamWatchdogCheckInterval())
50+
.setStreamWatchdogProvider(settings.getStreamWatchdogProvider());
51+
52+
baseSettingsBuilder
53+
.createReadSessionSettings()
54+
.setRetryableCodes(settings.createReadSessionSettings().getRetryableCodes())
55+
.setRetrySettings(settings.createReadSessionSettings().getRetrySettings());
56+
57+
baseSettingsBuilder
58+
.readRowsSettings()
59+
.setRetryableCodes(settings.readRowsSettings().getRetryableCodes())
60+
.setRetrySettings(settings.readRowsSettings().getRetrySettings())
61+
.setResumptionStrategy(settings.readRowsSettings().getResumptionStrategy())
62+
.setIdleTimeout(settings.readRowsSettings().getIdleTimeout());
63+
64+
baseSettingsBuilder
65+
.splitReadStreamSettings()
66+
.setRetryableCodes(settings.splitReadStreamSettings().getRetryableCodes())
67+
.setRetrySettings(settings.splitReadStreamSettings().getRetrySettings());
68+
69+
BigQueryReadStubSettings baseSettings = baseSettingsBuilder.build();
70+
ClientContext clientContext = ClientContext.create(baseSettings);
71+
GrpcBigQueryReadStub stub = new GrpcBigQueryReadStub(baseSettings, clientContext);
72+
return new EnhancedBigQueryReadStub(stub);
73+
}
74+
75+
@InternalApi("Visible for testing")
76+
EnhancedBigQueryReadStub(GrpcBigQueryReadStub stub) {
77+
this.stub = stub;
78+
}
79+
80+
public UnaryCallable<CreateReadSessionRequest, ReadSession> createReadSessionCallable() {
81+
return stub.createReadSessionCallable();
82+
}
83+
84+
public ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> readRowsCallable() {
85+
return stub.readRowsCallable();
86+
}
87+
88+
public UnaryCallable<SplitReadStreamRequest, SplitReadStreamResponse> splitReadStreamCallable() {
89+
return stub.splitReadStreamCallable();
90+
}
91+
92+
@Override
93+
public void close() {
94+
stub.close();
95+
}
96+
97+
@Override
98+
public void shutdown() {
99+
stub.shutdown();
100+
}
101+
102+
@Override
103+
public boolean isShutdown() {
104+
return stub.isShutdown();
105+
}
106+
107+
@Override
108+
public boolean isTerminated() {
109+
return stub.isTerminated();
110+
}
111+
112+
@Override
113+
public void shutdownNow() {
114+
stub.shutdownNow();
115+
}
116+
117+
@Override
118+
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
119+
return stub.awaitTermination(duration, unit);
120+
}
121+
}

0 commit comments

Comments
 (0)