5151import com .google .devtools .build .lib .vfs .FileSystemUtils ;
5252import com .google .devtools .build .lib .vfs .Path ;
5353import com .google .protobuf .ByteString ;
54+ import io .netty .util .AbstractReferenceCounted ;
5455import io .reactivex .rxjava3 .core .Completable ;
5556import java .io .ByteArrayOutputStream ;
5657import java .io .IOException ;
5758import java .io .OutputStream ;
5859import java .util .ArrayList ;
5960import java .util .List ;
61+ import java .util .concurrent .CountDownLatch ;
6062import java .util .concurrent .atomic .AtomicLong ;
6163import java .util .regex .Matcher ;
6264import java .util .regex .Pattern ;
6365
64- /** A cache for storing artifacts (input and output) as well as the output of running an action. */
66+ /**
67+ * A cache for storing artifacts (input and output) as well as the output of running an action.
68+ *
69+ * <p>The cache is reference counted. Initially, the reference count is 1. Use {@link #retain()} to
70+ * increase and {@link #release()} to decrease the reference count respectively. Once the reference
71+ * count is reached to 0, the underlying resources will be released (after network I/Os finished).
72+ *
73+ * <p>Use {@link #awaitTermination()} to wait for the underlying network I/Os to finish. Use {@link
74+ * #shutdownNow()} to cancel all active network I/Os and reject new requests.
75+ */
6576@ ThreadSafety .ThreadSafe
66- public class RemoteCache implements AutoCloseable {
77+ public class RemoteCache extends AbstractReferenceCounted {
6778 private static final GoogleLogger logger = GoogleLogger .forEnclosingClass ();
6879
6980 private static final ListenableFuture <Void > COMPLETED_SUCCESS = immediateFuture (null );
7081 private static final ListenableFuture <byte []> EMPTY_BYTES = immediateFuture (new byte [0 ]);
7182
83+ private final CountDownLatch closeCountDownLatch = new CountDownLatch (1 );
7284 protected final AsyncTaskCache .NoResult <Digest > casUploadCache = AsyncTaskCache .NoResult .create ();
7385
7486 protected final RemoteCacheClient cacheProtocol ;
@@ -95,7 +107,7 @@ public CachedActionResult downloadActionResult(
95107 public ListenableFuture <ImmutableSet <Digest >> findMissingDigests (
96108 RemoteActionExecutionContext context , Iterable <Digest > digests ) {
97109 if (Iterables .isEmpty (digests )) {
98- return Futures . immediateFuture (ImmutableSet .of ());
110+ return immediateFuture (ImmutableSet .of ());
99111 }
100112 return cacheProtocol .findMissingDigests (context , digests );
101113 }
@@ -426,10 +438,34 @@ public final List<ListenableFuture<Void>> downloadOutErr(
426438 return downloads ;
427439 }
428440
429- /** Release resources associated with the cache. The cache may not be used after calling this. */
430441 @ Override
431- public void close () {
442+ protected void deallocate () {
443+ casUploadCache .shutdown ();
432444 cacheProtocol .close ();
445+
446+ closeCountDownLatch .countDown ();
447+ }
448+
449+ @ Override
450+ public RemoteCache touch (Object o ) {
451+ return this ;
452+ }
453+
454+ @ Override
455+ public RemoteCache retain () {
456+ super .retain ();
457+ return this ;
458+ }
459+
460+ /** Waits for active network I/Os to finish. */
461+ public void awaitTermination () throws InterruptedException {
462+ casUploadCache .awaitTermination ();
463+ closeCountDownLatch .await ();
464+ }
465+
466+ /** Shuts the cache down and cancels active network I/Os. */
467+ public void shutdownNow () {
468+ casUploadCache .shutdownNow ();
433469 }
434470
435471 public static FailureDetail createFailureDetail (String message , Code detailedCode ) {
0 commit comments