|
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.v1beta2.BigQueryReadGrpc; |
23 | 34 | import com.google.cloud.bigquery.storage.v1beta2.CreateReadSessionRequest; |
24 | 35 | import com.google.cloud.bigquery.storage.v1beta2.ReadRowsRequest; |
25 | 36 | import com.google.cloud.bigquery.storage.v1beta2.ReadRowsResponse; |
26 | 37 | import com.google.cloud.bigquery.storage.v1beta2.ReadSession; |
27 | 38 | import com.google.cloud.bigquery.storage.v1beta2.SplitReadStreamRequest; |
28 | 39 | import com.google.cloud.bigquery.storage.v1beta2.SplitReadStreamResponse; |
| 40 | +import com.google.cloud.bigquery.storage.v1beta2.stub.readrows.ApiResultRetryAlgorithm; |
| 41 | +import com.google.cloud.bigquery.storage.v1beta2.stub.readrows.ReadRowsRetryingCallable; |
| 42 | +import com.google.common.collect.ImmutableMap; |
29 | 43 | import java.io.IOException; |
| 44 | +import java.util.Map; |
30 | 45 | import java.util.concurrent.TimeUnit; |
31 | 46 |
|
32 | 47 | /** |
|
35 | 50 | * <p>This class is for advanced usage and reflects the underlying API directly. |
36 | 51 | */ |
37 | 52 | public class EnhancedBigQueryReadStub implements BackgroundResource { |
| 53 | + |
| 54 | + private static final String TRACING_OUTER_CLIENT_NAME = "BigQueryStorage"; |
38 | 55 | private final GrpcBigQueryReadStub stub; |
| 56 | + private final BigQueryReadStubSettings stubSettings; |
| 57 | + private final ClientContext context; |
39 | 58 |
|
40 | 59 | public static EnhancedBigQueryReadStub create(EnhancedBigQueryReadStubSettings settings) |
41 | 60 | throws IOException { |
@@ -69,20 +88,64 @@ public static EnhancedBigQueryReadStub create(EnhancedBigQueryReadStubSettings s |
69 | 88 | BigQueryReadStubSettings baseSettings = baseSettingsBuilder.build(); |
70 | 89 | ClientContext clientContext = ClientContext.create(baseSettings); |
71 | 90 | GrpcBigQueryReadStub stub = new GrpcBigQueryReadStub(baseSettings, clientContext); |
72 | | - return new EnhancedBigQueryReadStub(stub); |
| 91 | + return new EnhancedBigQueryReadStub(stub, baseSettings, clientContext); |
73 | 92 | } |
74 | 93 |
|
75 | 94 | @InternalApi("Visible for testing") |
76 | | - EnhancedBigQueryReadStub(GrpcBigQueryReadStub stub) { |
| 95 | + EnhancedBigQueryReadStub( |
| 96 | + GrpcBigQueryReadStub stub, BigQueryReadStubSettings stubSettings, ClientContext context) { |
77 | 97 | this.stub = stub; |
| 98 | + this.stubSettings = stubSettings; |
| 99 | + this.context = context; |
78 | 100 | } |
79 | 101 |
|
80 | 102 | public UnaryCallable<CreateReadSessionRequest, ReadSession> createReadSessionCallable() { |
81 | 103 | return stub.createReadSessionCallable(); |
82 | 104 | } |
83 | 105 |
|
84 | 106 | public ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> readRowsCallable() { |
85 | | - return stub.readRowsCallable(); |
| 107 | + ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> innerCallable = |
| 108 | + GrpcRawCallableFactory.createServerStreamingCallable( |
| 109 | + GrpcCallSettings.<ReadRowsRequest, ReadRowsResponse>newBuilder() |
| 110 | + .setMethodDescriptor(BigQueryReadGrpc.getReadRowsMethod()) |
| 111 | + .setParamsExtractor( |
| 112 | + new RequestParamsExtractor<ReadRowsRequest>() { |
| 113 | + @Override |
| 114 | + public Map<String, String> extract(ReadRowsRequest request) { |
| 115 | + return ImmutableMap.of( |
| 116 | + "read_stream", String.valueOf(request.getReadStream())); |
| 117 | + } |
| 118 | + }) |
| 119 | + .build(), |
| 120 | + stubSettings.readRowsSettings().getRetryableCodes()); |
| 121 | + ServerStreamingCallSettings<ReadRowsRequest, ReadRowsResponse> callSettings = |
| 122 | + stubSettings.readRowsSettings(); |
| 123 | + |
| 124 | + StreamingRetryAlgorithm<Void> retryAlgorithm = |
| 125 | + new StreamingRetryAlgorithm<>( |
| 126 | + new ApiResultRetryAlgorithm<Void>(), |
| 127 | + new ExponentialRetryAlgorithm(callSettings.getRetrySettings(), context.getClock())); |
| 128 | + |
| 129 | + ScheduledRetryingExecutor<Void> retryingExecutor = |
| 130 | + new ScheduledRetryingExecutor<>(retryAlgorithm, context.getExecutor()); |
| 131 | + |
| 132 | + if (context.getStreamWatchdog() != null) { |
| 133 | + innerCallable = Callables.watched(innerCallable, callSettings, context); |
| 134 | + } |
| 135 | + |
| 136 | + ReadRowsRetryingCallable outerCallable = |
| 137 | + new ReadRowsRetryingCallable( |
| 138 | + context.getDefaultCallContext(), |
| 139 | + innerCallable, |
| 140 | + retryingExecutor, |
| 141 | + callSettings.getResumptionStrategy()); |
| 142 | + |
| 143 | + ServerStreamingCallable<ReadRowsRequest, ReadRowsResponse> traced = |
| 144 | + new TracedServerStreamingCallable<>( |
| 145 | + outerCallable, |
| 146 | + context.getTracerFactory(), |
| 147 | + SpanName.of(TRACING_OUTER_CLIENT_NAME, "ReadRows")); |
| 148 | + return traced.withDefaultCallContext(context.getDefaultCallContext()); |
86 | 149 | } |
87 | 150 |
|
88 | 151 | public UnaryCallable<SplitReadStreamRequest, SplitReadStreamResponse> splitReadStreamCallable() { |
|
0 commit comments