From 5c3467f2d251ae85889caca627794a8f9ff726b2 Mon Sep 17 00:00:00 2001 From: ulfjack Date: Fri, 8 Sep 2017 15:58:43 +0200 Subject: ActionInputFileCache: move getMetadata to a new super-interface Update the callers that only need getMetadata to use the new interface. PiperOrigin-RevId: 167992239 --- .../devtools/build/lib/actions/AbstractAction.java | 4 +- .../build/lib/actions/ActionInputFileCache.java | 23 +---------- .../build/lib/actions/MetadataProvider.java | 44 ++++++++++++++++++++++ .../google/devtools/build/lib/remote/Chunker.java | 6 +-- .../google/devtools/build/lib/remote/Digests.java | 4 +- .../devtools/build/lib/remote/GrpcRemoteCache.java | 6 +-- .../lib/remote/SimpleBlobStoreActionCache.java | 4 +- 7 files changed, 57 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/google/devtools/build/lib/actions/MetadataProvider.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java b/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java index 1257308718..477baa15da 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java +++ b/src/main/java/com/google/devtools/build/lib/actions/AbstractAction.java @@ -419,13 +419,13 @@ public abstract class AbstractAction implements Action, SkylarkValue { * checking, this method must be called. */ protected void checkInputsForDirectories( - EventHandler eventHandler, ActionInputFileCache metadataHandler) throws ExecException { + EventHandler eventHandler, MetadataProvider metadataProvider) throws ExecException { // Report "directory dependency checking" warning only for non-generated directories (generated // ones will be reported earlier). for (Artifact input : getMandatoryInputs()) { // Assume that if the file did not exist, we would not have gotten here. try { - if (input.isSourceArtifact() && !metadataHandler.getMetadata(input).isFile()) { + if (input.isSourceArtifact() && !metadataProvider.getMetadata(input).isFile()) { eventHandler.handle(Event.warn(getOwner().getLocation(), "input '" + input.prettyPrint() + "' to " + getOwner().getLabel() + " is a directory; dependency checking of directories is unsound")); diff --git a/src/main/java/com/google/devtools/build/lib/actions/ActionInputFileCache.java b/src/main/java/com/google/devtools/build/lib/actions/ActionInputFileCache.java index 1590445eb7..27b219d588 100644 --- a/src/main/java/com/google/devtools/build/lib/actions/ActionInputFileCache.java +++ b/src/main/java/com/google/devtools/build/lib/actions/ActionInputFileCache.java @@ -13,10 +13,8 @@ // limitations under the License. package com.google.devtools.build.lib.actions; -import com.google.devtools.build.lib.actions.cache.Metadata; import com.google.devtools.build.lib.vfs.Path; import com.google.protobuf.ByteString; -import java.io.IOException; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; @@ -26,26 +24,7 @@ import javax.annotation.concurrent.ThreadSafe; * NOTE: Implementations must be thread safe. */ @ThreadSafe -public interface ActionInputFileCache { - /** - * Returns digest for the given artifact. This digest is current as of some time t >= the start of - * the present build. If the artifact is an output of an action that already executed at time p, - * then t >= p. Aside from these properties, t can be any value and may vary arbitrarily across - * calls. - * - * The return value is owned by the cache and must not be modified. - * - * @param input the input to retrieve the digest for - * @return the artifact's digest or null if digest cannot be obtained (due to artifact - * non-existence, lookup errors, or any other reason) - * - * @throws DigestOfDirectoryException in case {@code input} is a directory. - * @throws IOException If the file cannot be digested. - * - */ - @Nullable - Metadata getMetadata(ActionInput input) throws IOException; - +public interface ActionInputFileCache extends MetadataProvider { /** * Checks if the file is available locally, based on the assumption that previous operations on * the ActionInputFileCache would have created a cache entry for it. diff --git a/src/main/java/com/google/devtools/build/lib/actions/MetadataProvider.java b/src/main/java/com/google/devtools/build/lib/actions/MetadataProvider.java new file mode 100644 index 0000000000..40d54b6c92 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/actions/MetadataProvider.java @@ -0,0 +1,44 @@ +// Copyright 2017 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.actions; + +import com.google.devtools.build.lib.actions.cache.Metadata; +import java.io.IOException; +import javax.annotation.Nullable; + +/** + * The interface for Action inputs metadata (Digest and size). + * + * NOTE: Implementations must be thread safe. + */ +public interface MetadataProvider { + /** + * Returns digest for the given artifact. This digest is current as of some time t >= the start of + * the present build. If the artifact is an output of an action that already executed at time p, + * then t >= p. Aside from these properties, t can be any value and may vary arbitrarily across + * calls. + * + * The return value is owned by the cache and must not be modified. + * + * @param input the input to retrieve the digest for + * @return the artifact's digest or null if digest cannot be obtained (due to artifact + * non-existence, lookup errors, or any other reason) + * + * @throws DigestOfDirectoryException in case {@code input} is a directory. + * @throws IOException If the file cannot be digested. + * + */ + @Nullable + Metadata getMetadata(ActionInput input) throws IOException; +} \ No newline at end of file diff --git a/src/main/java/com/google/devtools/build/lib/remote/Chunker.java b/src/main/java/com/google/devtools/build/lib/remote/Chunker.java index 8ffdb71e5d..1c7a66fda2 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/Chunker.java +++ b/src/main/java/com/google/devtools/build/lib/remote/Chunker.java @@ -21,7 +21,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Throwables; import com.google.common.io.ByteStreams; import com.google.devtools.build.lib.actions.ActionInput; -import com.google.devtools.build.lib.actions.ActionInputFileCache; +import com.google.devtools.build.lib.actions.MetadataProvider; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.remoteexecution.v1test.Digest; import com.google.protobuf.ByteString; @@ -137,12 +137,12 @@ public final class Chunker { }, Digests.computeDigest(file), chunkSize); } - public Chunker(ActionInput actionInput, ActionInputFileCache inputCache, Path execRoot) throws + public Chunker(ActionInput actionInput, MetadataProvider inputCache, Path execRoot) throws IOException{ this(actionInput, inputCache, execRoot, getDefaultChunkSize()); } - public Chunker(ActionInput actionInput, ActionInputFileCache inputCache, Path execRoot, + public Chunker(ActionInput actionInput, MetadataProvider inputCache, Path execRoot, int chunkSize) throws IOException { this(() -> { diff --git a/src/main/java/com/google/devtools/build/lib/remote/Digests.java b/src/main/java/com/google/devtools/build/lib/remote/Digests.java index ecb1626cdd..eadcb4c57e 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/Digests.java +++ b/src/main/java/com/google/devtools/build/lib/remote/Digests.java @@ -18,7 +18,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.hash.HashCode; import com.google.devtools.build.lib.actions.ActionInput; -import com.google.devtools.build.lib.actions.ActionInputFileCache; +import com.google.devtools.build.lib.actions.MetadataProvider; import com.google.devtools.build.lib.actions.cache.DigestUtils; import com.google.devtools.build.lib.actions.cache.Metadata; import com.google.devtools.build.lib.actions.cache.VirtualActionInput; @@ -102,7 +102,7 @@ public final class Digests { return Digest.newBuilder().setHash(hexHash).setSizeBytes(size).build(); } - public static Digest getDigestFromInputCache(ActionInput input, ActionInputFileCache cache) + public static Digest getDigestFromInputCache(ActionInput input, MetadataProvider cache) throws IOException { Metadata metadata = cache.getMetadata(input); return buildDigest(metadata.getDigest(), metadata.getSize()); diff --git a/src/main/java/com/google/devtools/build/lib/remote/GrpcRemoteCache.java b/src/main/java/com/google/devtools/build/lib/remote/GrpcRemoteCache.java index f9785a8909..ee7ef666e3 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/GrpcRemoteCache.java +++ b/src/main/java/com/google/devtools/build/lib/remote/GrpcRemoteCache.java @@ -24,9 +24,9 @@ import com.google.common.collect.ImmutableSet; import com.google.common.util.concurrent.ListeningScheduledExecutorService; import com.google.common.util.concurrent.MoreExecutors; import com.google.devtools.build.lib.actions.ActionInput; -import com.google.devtools.build.lib.actions.ActionInputFileCache; import com.google.devtools.build.lib.actions.EnvironmentalExecException; import com.google.devtools.build.lib.actions.ExecException; +import com.google.devtools.build.lib.actions.MetadataProvider; import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; import com.google.devtools.build.lib.remote.Digests.ActionKey; import com.google.devtools.build.lib.remote.TreeNodeRepository.TreeNode; @@ -156,7 +156,7 @@ public class GrpcRemoteCache implements RemoteActionCache { uploadBlob(command.toByteArray()); if (!missingActionInputs.isEmpty()) { List inputsToUpload = new ArrayList<>(); - ActionInputFileCache inputFileCache = repository.getInputFileCache(); + MetadataProvider inputFileCache = repository.getInputFileCache(); for (ActionInput actionInput : missingActionInputs) { inputsToUpload.add(new Chunker(actionInput, inputFileCache, execRoot)); } @@ -374,7 +374,7 @@ public class GrpcRemoteCache implements RemoteActionCache { * * @return The key for fetching the file contents blob from cache. */ - Digest uploadFileContents(ActionInput input, Path execRoot, ActionInputFileCache inputCache) + Digest uploadFileContents(ActionInput input, Path execRoot, MetadataProvider inputCache) throws IOException, InterruptedException { Digest digest = Digests.getDigestFromInputCache(input, inputCache); ImmutableSet missing = getMissingDigests(ImmutableList.of(digest)); diff --git a/src/main/java/com/google/devtools/build/lib/remote/SimpleBlobStoreActionCache.java b/src/main/java/com/google/devtools/build/lib/remote/SimpleBlobStoreActionCache.java index 641cf64f11..6fef48f178 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/SimpleBlobStoreActionCache.java +++ b/src/main/java/com/google/devtools/build/lib/remote/SimpleBlobStoreActionCache.java @@ -15,9 +15,9 @@ package com.google.devtools.build.lib.remote; import com.google.devtools.build.lib.actions.ActionInput; -import com.google.devtools.build.lib.actions.ActionInputFileCache; import com.google.devtools.build.lib.actions.EnvironmentalExecException; import com.google.devtools.build.lib.actions.ExecException; +import com.google.devtools.build.lib.actions.MetadataProvider; import com.google.devtools.build.lib.actions.cache.VirtualActionInput; import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; import com.google.devtools.build.lib.remote.Digests.ActionKey; @@ -99,7 +99,7 @@ public final class SimpleBlobStoreActionCache implements RemoteActionCache { } private Digest uploadFileContents( - ActionInput input, Path execRoot, ActionInputFileCache inputCache) + ActionInput input, Path execRoot, MetadataProvider inputCache) throws IOException, InterruptedException { // This unconditionally reads the whole file into memory first! if (input instanceof VirtualActionInput) { -- cgit v1.2.3