2020import com .google .api .core .ApiFutureCallback ;
2121import com .google .api .core .ApiFutures ;
2222import com .google .api .core .InternalApi ;
23- import com .google .api .gax .grpc .GrpcStatusCode ;
2423import com .google .api .gax .retrying .RetryingFuture ;
2524import com .google .api .gax .retrying .ScheduledRetryingExecutor ;
2625import com .google .api .gax .retrying .ServerStreamingAttemptException ;
2726import com .google .api .gax .rpc .ApiCallContext ;
28- import com .google .api .gax .rpc .ApiException ;
2927import com .google .api .gax .rpc .ResponseObserver ;
3028import com .google .api .gax .rpc .ServerStreamingCallable ;
31- import com .google .api .gax .rpc .StatusCode ;
3229import com .google .bigtable .v2 .ReadRowsRequest ;
3330import com .google .cloud .bigtable .data .v2 .models .LargeRowException ;
3431import com .google .cloud .bigtable .data .v2 .stub .readrows .LargeReadRowsResumptionStrategy ;
3532import com .google .protobuf .ByteString ;
36- import io .grpc .Status ;
37- import io .grpc .StatusRuntimeException ;
3833import java .util .List ;
3934
40- /**
41- * A ServerStreamingCallable that implements resumable retries for large rows.
42- *
43- * <p>This class is a specialized version of {@link RetryingServerStreamingCallable} that uses
44- * {@link LargeRowServerStreamingAttemptCallable} and {@link LargeReadRowsResumptionStrategy}.
45- */
35+ /** A ServerStreamingCallable that throws large row keys at the end of the stream. */
4636@ InternalApi
4737public final class RetryingLargeRowServerStreamingCallable <ResponseT >
4838 extends ServerStreamingCallable <ReadRowsRequest , ResponseT > {
4939
5040 private final ServerStreamingCallable <ReadRowsRequest , ResponseT > innerCallable ;
5141 private final ScheduledRetryingExecutor <Void > executor ;
52- private final LargeReadRowsResumptionStrategy <ResponseT > resumptionStrategyPrototype ;
42+ private final LargeReadRowsResumptionStrategy <ResponseT > resumptionStrategy ;
5343
5444 public RetryingLargeRowServerStreamingCallable (
5545 ServerStreamingCallable <ReadRowsRequest , ResponseT > innerCallable ,
5646 ScheduledRetryingExecutor <Void > executor ,
57- LargeReadRowsResumptionStrategy <ResponseT > resumptionStrategyPrototype ) {
47+ LargeReadRowsResumptionStrategy <ResponseT > resumptionStrategy ) {
5848 this .innerCallable = innerCallable ;
5949 this .executor = executor ;
60- this .resumptionStrategyPrototype = resumptionStrategyPrototype ;
50+ this .resumptionStrategy = resumptionStrategy ;
6151 }
6252
6353 @ Override
@@ -66,13 +56,12 @@ public void call(
6656 final ResponseObserver <ResponseT > responseObserver ,
6757 ApiCallContext context ) {
6858
69- LargeRowServerStreamingAttemptCallable <ResponseT > attemptCallable =
70- new LargeRowServerStreamingAttemptCallable <>(
71- innerCallable ,
72- (LargeReadRowsResumptionStrategy <ResponseT >) resumptionStrategyPrototype .createNew (),
73- request ,
74- context ,
75- responseObserver );
59+ final LargeReadRowsResumptionStrategy <ResponseT > strategy =
60+ (LargeReadRowsResumptionStrategy <ResponseT >) resumptionStrategy .createNew ();
61+
62+ ServerStreamingAttemptCallable <ReadRowsRequest , ResponseT > attemptCallable =
63+ new ServerStreamingAttemptCallable <>(
64+ innerCallable , strategy , request , context , responseObserver );
7665
7766 RetryingFuture <Void > retryingFuture = executor .createFuture (attemptCallable , context );
7867 attemptCallable .setExternalFuture (retryingFuture );
@@ -88,35 +77,18 @@ public void onFailure(Throwable throwable) {
8877 if (throwable instanceof ServerStreamingAttemptException ) {
8978 throwable = throwable .getCause ();
9079 }
91- List <ByteString > encounteredKeys =
92- resumptionStrategyPrototype .getEncounteredLargeRowKeys ();
80+ List <ByteString > encounteredKeys = strategy .getLargeRowKeys ();
9381 if (!encounteredKeys .isEmpty ()) {
94- StatusCode statusCode = GrpcStatusCode .of (Status .Code .FAILED_PRECONDITION );
95- boolean isRetryable = false ;
96- if (throwable instanceof ApiException ) {
97- statusCode = ((ApiException ) throwable ).getStatusCode ();
98- isRetryable = ((ApiException ) throwable ).isRetryable ();
99- } else if (throwable instanceof StatusRuntimeException ) {
100- statusCode =
101- GrpcStatusCode .of (((StatusRuntimeException ) throwable ).getStatus ().getCode ());
102- }
103- throwable =
104- new LargeRowException (throwable , statusCode , isRetryable , encounteredKeys );
82+ throwable .addSuppressed (new LargeRowException (encounteredKeys ));
10583 }
10684 responseObserver .onError (throwable );
10785 }
10886
10987 @ Override
11088 public void onSuccess (Void ignored ) {
111- List <ByteString > encounteredKeys =
112- resumptionStrategyPrototype .getEncounteredLargeRowKeys ();
89+ List <ByteString > encounteredKeys = strategy .getLargeRowKeys ();
11390 if (!encounteredKeys .isEmpty ()) {
114- responseObserver .onError (
115- new LargeRowException (
116- null ,
117- GrpcStatusCode .of (Status .Code .FAILED_PRECONDITION ),
118- false ,
119- encounteredKeys ));
91+ responseObserver .onError (new LargeRowException (encounteredKeys ));
12092 } else {
12193 responseObserver .onComplete ();
12294 }
0 commit comments