|
34 | 34 | import com.google.common.annotations.VisibleForTesting;
|
35 | 35 | import com.google.common.base.Ascii;
|
36 | 36 | import com.google.common.base.Preconditions;
|
| 37 | +import com.google.common.base.VerifyException; |
37 | 38 | import com.google.common.collect.ImmutableSet;
|
38 | 39 | import com.google.common.collect.Iterables;
|
39 | 40 | import com.google.common.flogger.GoogleLogger;
|
|
58 | 59 | import com.google.devtools.build.lib.vfs.Path;
|
59 | 60 | import com.google.protobuf.ByteString;
|
60 | 61 | import io.grpc.Channel;
|
61 |
| -import io.grpc.Context; |
62 | 62 | import io.grpc.Status;
|
63 | 63 | import io.grpc.Status.Code;
|
64 | 64 | import io.grpc.StatusRuntimeException;
|
65 |
| -import io.grpc.stub.StreamObserver; |
| 65 | +import io.grpc.stub.ClientCallStreamObserver; |
| 66 | +import io.grpc.stub.ClientResponseObserver; |
66 | 67 | import java.io.IOException;
|
67 | 68 | import java.io.OutputStream;
|
68 | 69 | import java.util.ArrayList;
|
@@ -371,81 +372,87 @@ private ListenableFuture<Long> requestRead(
|
371 | 372 | } catch (IOException e) {
|
372 | 373 | return Futures.immediateFailedFuture(e);
|
373 | 374 | }
|
374 |
| - Context.CancellableContext grpcContext = Context.current().withCancellation(); |
375 |
| - future.addListener(() -> grpcContext.cancel(null), MoreExecutors.directExecutor()); |
376 |
| - grpcContext.run( |
377 |
| - () -> |
378 |
| - bsAsyncStub(context, channel) |
379 |
| - .read( |
380 |
| - ReadRequest.newBuilder() |
381 |
| - .setResourceName(resourceName) |
382 |
| - .setReadOffset(rawOut.getCount()) |
383 |
| - .build(), |
384 |
| - new StreamObserver<ReadResponse>() { |
385 |
| - @Override |
386 |
| - public void onNext(ReadResponse readResponse) { |
387 |
| - ByteString data = readResponse.getData(); |
388 |
| - try { |
389 |
| - data.writeTo(out); |
390 |
| - } catch (IOException e) { |
391 |
| - // Cancel the call. |
392 |
| - throw new RuntimeException(e); |
393 |
| - } |
394 |
| - // reset the stall backoff because we've made progress or been kept alive |
395 |
| - progressiveBackoff.reset(); |
396 |
| - } |
397 |
| - |
398 |
| - @Override |
399 |
| - public void onError(Throwable t) { |
400 |
| - if (rawOut.getCount() == digest.getSizeBytes()) { |
401 |
| - // If the file was fully downloaded, it doesn't matter if there was an |
402 |
| - // error at |
403 |
| - // the end of the stream. |
404 |
| - logger.atInfo().withCause(t).log( |
405 |
| - "ignoring error because file was fully received"); |
406 |
| - onCompleted(); |
407 |
| - return; |
408 |
| - } |
409 |
| - releaseOut(); |
410 |
| - Status status = Status.fromThrowable(t); |
411 |
| - if (status.getCode() == Status.Code.NOT_FOUND) { |
412 |
| - future.setException(new CacheNotFoundException(digest)); |
413 |
| - } else { |
414 |
| - future.setException(t); |
415 |
| - } |
416 |
| - } |
417 |
| - |
418 |
| - @Override |
419 |
| - public void onCompleted() { |
420 |
| - try { |
421 |
| - try { |
422 |
| - out.flush(); |
423 |
| - } finally { |
424 |
| - releaseOut(); |
425 |
| - } |
426 |
| - if (digestSupplier != null) { |
427 |
| - Utils.verifyBlobContents(digest, digestSupplier.get()); |
428 |
| - } |
429 |
| - } catch (IOException e) { |
430 |
| - future.setException(e); |
431 |
| - } catch (RuntimeException e) { |
432 |
| - logger.atWarning().withCause(e).log("Unexpected exception"); |
433 |
| - future.setException(e); |
434 |
| - } |
435 |
| - future.set(rawOut.getCount()); |
| 375 | + bsAsyncStub(context, channel) |
| 376 | + .read( |
| 377 | + ReadRequest.newBuilder() |
| 378 | + .setResourceName(resourceName) |
| 379 | + .setReadOffset(rawOut.getCount()) |
| 380 | + .build(), |
| 381 | + new ClientResponseObserver<ReadRequest, ReadResponse>() { |
| 382 | + @Override |
| 383 | + public void beforeStart(ClientCallStreamObserver<ReadRequest> requestStream) { |
| 384 | + future.addListener( |
| 385 | + () -> { |
| 386 | + if (future.isCancelled()) { |
| 387 | + requestStream.cancel("canceled by user", null); |
436 | 388 | }
|
437 |
| - |
438 |
| - private void releaseOut() { |
439 |
| - if (out instanceof ZstdDecompressingOutputStream) { |
440 |
| - try { |
441 |
| - ((ZstdDecompressingOutputStream) out).closeShallow(); |
442 |
| - } catch (IOException e) { |
443 |
| - logger.atWarning().withCause(e).log( |
444 |
| - "failed to cleanly close output stream"); |
445 |
| - } |
446 |
| - } |
447 |
| - } |
448 |
| - })); |
| 389 | + }, |
| 390 | + MoreExecutors.directExecutor()); |
| 391 | + } |
| 392 | + |
| 393 | + @Override |
| 394 | + public void onNext(ReadResponse readResponse) { |
| 395 | + ByteString data = readResponse.getData(); |
| 396 | + try { |
| 397 | + data.writeTo(out); |
| 398 | + } catch (IOException e) { |
| 399 | + // Cancel the call. |
| 400 | + throw new VerifyException(e); |
| 401 | + } |
| 402 | + // reset the stall backoff because we've made progress or been kept alive |
| 403 | + progressiveBackoff.reset(); |
| 404 | + } |
| 405 | + |
| 406 | + @Override |
| 407 | + public void onError(Throwable t) { |
| 408 | + if (rawOut.getCount() == digest.getSizeBytes()) { |
| 409 | + // If the file was fully downloaded, it doesn't matter if there was an |
| 410 | + // error at |
| 411 | + // the end of the stream. |
| 412 | + logger.atInfo().withCause(t).log( |
| 413 | + "ignoring error because file was fully received"); |
| 414 | + onCompleted(); |
| 415 | + return; |
| 416 | + } |
| 417 | + releaseOut(); |
| 418 | + Status status = Status.fromThrowable(t); |
| 419 | + if (status.getCode() == Status.Code.NOT_FOUND) { |
| 420 | + future.setException(new CacheNotFoundException(digest)); |
| 421 | + } else { |
| 422 | + future.setException(t); |
| 423 | + } |
| 424 | + } |
| 425 | + |
| 426 | + @Override |
| 427 | + public void onCompleted() { |
| 428 | + try { |
| 429 | + try { |
| 430 | + out.flush(); |
| 431 | + } finally { |
| 432 | + releaseOut(); |
| 433 | + } |
| 434 | + if (digestSupplier != null) { |
| 435 | + Utils.verifyBlobContents(digest, digestSupplier.get()); |
| 436 | + } |
| 437 | + } catch (IOException e) { |
| 438 | + future.setException(e); |
| 439 | + } catch (RuntimeException e) { |
| 440 | + logger.atWarning().withCause(e).log("Unexpected exception"); |
| 441 | + future.setException(e); |
| 442 | + } |
| 443 | + future.set(rawOut.getCount()); |
| 444 | + } |
| 445 | + |
| 446 | + private void releaseOut() { |
| 447 | + if (out instanceof ZstdDecompressingOutputStream) { |
| 448 | + try { |
| 449 | + ((ZstdDecompressingOutputStream) out).closeShallow(); |
| 450 | + } catch (IOException e) { |
| 451 | + logger.atWarning().withCause(e).log("failed to cleanly close output stream"); |
| 452 | + } |
| 453 | + } |
| 454 | + } |
| 455 | + }); |
449 | 456 | return future;
|
450 | 457 | }
|
451 | 458 |
|
|
0 commit comments