|
17 | 17 |
|
18 | 18 | import com.google.api.core.InternalApi; |
19 | 19 | import com.google.api.gax.core.BackgroundResource; |
| 20 | +import com.google.api.gax.grpc.GrpcCallSettings; |
| 21 | +import com.google.api.gax.grpc.GrpcRawCallableFactory; |
| 22 | +import com.google.api.gax.retrying.ExponentialRetryAlgorithm; |
| 23 | +import com.google.api.gax.retrying.ScheduledRetryingExecutor; |
| 24 | +import com.google.api.gax.retrying.StreamingRetryAlgorithm; |
| 25 | +import com.google.api.gax.rpc.Callables; |
20 | 26 | import com.google.api.gax.rpc.ClientContext; |
| 27 | +import com.google.api.gax.rpc.RequestParamsExtractor; |
| 28 | +import com.google.api.gax.rpc.ServerStreamingCallSettings; |
21 | 29 | import com.google.api.gax.rpc.ServerStreamingCallable; |
22 | 30 | import com.google.api.gax.rpc.UnaryCallable; |
| 31 | +import com.google.api.gax.tracing.SpanName; |
| 32 | +import com.google.api.gax.tracing.TracedServerStreamingCallable; |
| 33 | +import com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageGrpc; |
23 | 34 | import com.google.cloud.bigquery.storage.v1beta1.Storage.BatchCreateReadSessionStreamsRequest; |
24 | 35 | import com.google.cloud.bigquery.storage.v1beta1.Storage.BatchCreateReadSessionStreamsResponse; |
25 | 36 | import com.google.cloud.bigquery.storage.v1beta1.Storage.CreateReadSessionRequest; |
|
29 | 40 | import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession; |
30 | 41 | import com.google.cloud.bigquery.storage.v1beta1.Storage.SplitReadStreamRequest; |
31 | 42 | import com.google.cloud.bigquery.storage.v1beta1.Storage.SplitReadStreamResponse; |
| 43 | +import com.google.cloud.bigquery.storage.v1beta1.stub.readrows.ApiResultRetryAlgorithm; |
| 44 | +import com.google.cloud.bigquery.storage.v1beta1.stub.readrows.ReadRowsRetryingCallable; |
| 45 | +import com.google.common.collect.ImmutableMap; |
32 | 46 | import com.google.protobuf.Empty; |
33 | 47 | import java.io.IOException; |
| 48 | +import java.util.Map; |
34 | 49 | import java.util.concurrent.TimeUnit; |
35 | 50 |
|
36 | 51 | /** |
|
40 | 55 | */ |
41 | 56 | public class EnhancedBigQueryStorageStub implements BackgroundResource { |
42 | 57 |
|
| 58 | + private static final String TRACING_OUTER_CLIENT_NAME = "BigQueryStorage"; |
43 | 59 | private final GrpcBigQueryStorageStub stub; |
| 60 | + private final BigQueryStorageStubSettings stubSettings; |
| 61 | + private final ClientContext context; |
44 | 62 |
|
45 | 63 | public static EnhancedBigQueryStorageStub create(EnhancedBigQueryStorageStubSettings settings) |
46 | 64 | throws IOException { |
@@ -89,20 +107,67 @@ public static EnhancedBigQueryStorageStub create(EnhancedBigQueryStorageStubSett |
89 | 107 | BigQueryStorageStubSettings baseSettings = baseSettingsBuilder.build(); |
90 | 108 | ClientContext clientContext = ClientContext.create(baseSettings); |
91 | 109 | GrpcBigQueryStorageStub stub = new GrpcBigQueryStorageStub(baseSettings, clientContext); |
92 | | - return new EnhancedBigQueryStorageStub(stub); |
| 110 | + return new EnhancedBigQueryStorageStub(stub, baseSettings, clientContext); |
93 | 111 | } |
94 | 112 |
|
95 | 113 | @InternalApi("Visible for testing") |
96 | | - EnhancedBigQueryStorageStub(GrpcBigQueryStorageStub stub) { |
| 114 | + EnhancedBigQueryStorageStub( |
| 115 | + GrpcBigQueryStorageStub stub, |
| 116 | + BigQueryStorageStubSettings stubSettings, |
| 117 | + ClientContext context) { |
97 | 118 | this.stub = stub; |
| 119 | + this.stubSettings = stubSettings; |
| 120 | + this.context = context; |
98 | 121 | } |
99 | 122 |
|
100 | 123 | public UnaryCallable<CreateReadSessionRequest, ReadSession> createReadSessionCallable() { |
101 | 124 | return stub.createReadSessionCallable(); |
102 | 125 | } |
103 | 126 |
|
104 | 127 | public ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> readRowsCallable() { |
105 | | - return stub.readRowsCallable(); |
| 128 | + ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> innerCallable = |
| 129 | + GrpcRawCallableFactory.createServerStreamingCallable( |
| 130 | + GrpcCallSettings.<ReadRowsRequest, ReadRowsResponse>newBuilder() |
| 131 | + .setMethodDescriptor(BigQueryStorageGrpc.getReadRowsMethod()) |
| 132 | + .setParamsExtractor( |
| 133 | + new RequestParamsExtractor<ReadRowsRequest>() { |
| 134 | + @Override |
| 135 | + public Map<String, String> extract(ReadRowsRequest request) { |
| 136 | + return ImmutableMap.of( |
| 137 | + "read_position.stream.name", |
| 138 | + String.valueOf(request.getReadPosition().getStream().getName())); |
| 139 | + } |
| 140 | + }) |
| 141 | + .build(), |
| 142 | + stubSettings.readRowsSettings().getRetryableCodes()); |
| 143 | + ServerStreamingCallSettings<ReadRowsRequest, ReadRowsResponse> callSettings = |
| 144 | + stubSettings.readRowsSettings(); |
| 145 | + |
| 146 | + StreamingRetryAlgorithm<Void> retryAlgorithm = |
| 147 | + new StreamingRetryAlgorithm<>( |
| 148 | + new ApiResultRetryAlgorithm<Void>(), |
| 149 | + new ExponentialRetryAlgorithm(callSettings.getRetrySettings(), context.getClock())); |
| 150 | + |
| 151 | + ScheduledRetryingExecutor<Void> retryingExecutor = |
| 152 | + new ScheduledRetryingExecutor<>(retryAlgorithm, context.getExecutor()); |
| 153 | + |
| 154 | + if (context.getStreamWatchdog() != null) { |
| 155 | + innerCallable = Callables.watched(innerCallable, callSettings, context); |
| 156 | + } |
| 157 | + |
| 158 | + ReadRowsRetryingCallable outerCallable = |
| 159 | + new ReadRowsRetryingCallable( |
| 160 | + context.getDefaultCallContext(), |
| 161 | + innerCallable, |
| 162 | + retryingExecutor, |
| 163 | + callSettings.getResumptionStrategy()); |
| 164 | + |
| 165 | + ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> traced = |
| 166 | + new TracedServerStreamingCallable<>( |
| 167 | + outerCallable, |
| 168 | + context.getTracerFactory(), |
| 169 | + SpanName.of(TRACING_OUTER_CLIENT_NAME, "ReadRows")); |
| 170 | + return traced.withDefaultCallContext(context.getDefaultCallContext()); |
106 | 171 | } |
107 | 172 |
|
108 | 173 | public UnaryCallable<BatchCreateReadSessionStreamsRequest, BatchCreateReadSessionStreamsResponse> |
|
0 commit comments