Repository navigation
Make ValidatesRunner faster: Improvements to file staging #40320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,12 +22,18 @@ | |
| import java.io.File; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
| import org.apache.beam.model.pipeline.v1.Endpoints.ApiServiceDescriptor; | ||
|
|
@@ -55,10 +61,13 @@ | |
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.MoreObjects; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Strings; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.Cache; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.cache.CacheBuilder; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableSet; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.hash.HashCode; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.hash.Hasher; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.hash.Hashing; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.Files; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
@@ -98,6 +107,11 @@ public class Environments { | |
| .put(ENVIRONMENT_PROCESS, ImmutableSet.of(processCommandOption, processVariablesOption)) | ||
| .build(); | ||
|
|
||
| private static final Cache<FileHashCacheKey, HashCode> FILE_HASH_CACHE = | ||
| CacheBuilder.newBuilder().maximumSize(10_000).build(); | ||
| private static final ConcurrentHashMap<HashCode, File> DIRECTORY_ZIP_CACHE = | ||
| new ConcurrentHashMap<>(); | ||
|
|
||
| public enum JavaVersion { | ||
| java11("java11", "11", 11), | ||
| java17("java17", "17", 17), | ||
|
|
@@ -434,7 +448,7 @@ public static List<ArtifactInformation> getArtifacts(List<String> stagingFiles) | |
| File zippedFile; | ||
| try { | ||
| zippedFile = zipDirectory(file); | ||
| hashCode = Files.asByteSource(zippedFile).hash(Hashing.sha256()); | ||
| hashCode = getFileHash(zippedFile); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
@@ -448,7 +462,7 @@ public static List<ArtifactInformation> getArtifacts(List<String> stagingFiles) | |
|
|
||
| } else { | ||
| try { | ||
| hashCode = Files.asByteSource(file).hash(Hashing.sha256()); | ||
| hashCode = getFileHash(file); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
@@ -538,6 +552,53 @@ public static String createStagingFileName(File path, HashCode hash) { | |
| return String.format("%s-%s%s", fileName, encodedHash, suffix); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the SHA-256 {@link HashCode} for {@code file}, caching the result in memory keyed by | ||
| * the file's absolute path, length, and last-modified timestamp. | ||
| */ | ||
| public static HashCode getFileHash(File file) throws IOException { | ||
| FileHashCacheKey key = new FileHashCacheKey(file); | ||
| try { | ||
| return FILE_HASH_CACHE.get(key, () -> Files.asByteSource(file).hash(Hashing.sha256())); | ||
| } catch (ExecutionException e) { | ||
| if (e.getCause() instanceof IOException) { | ||
| throw (IOException) e.getCause(); | ||
| } | ||
| throw new RuntimeException(e.getCause()); | ||
| } | ||
| } | ||
|
|
||
| private static final class FileHashCacheKey { | ||
| private final String absolutePath; | ||
| private final long length; | ||
| private final long lastModified; | ||
|
|
||
| FileHashCacheKey(File file) { | ||
| this.absolutePath = file.getAbsolutePath(); | ||
| this.length = file.length(); | ||
| this.lastModified = file.lastModified(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(@Nullable Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (!(o instanceof FileHashCacheKey)) { | ||
| return false; | ||
| } | ||
| FileHashCacheKey that = (FileHashCacheKey) o; | ||
| return length == that.length | ||
| && lastModified == that.lastModified | ||
| && Objects.equals(absolutePath, that.absolutePath); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(absolutePath, length, lastModified); | ||
| } | ||
| } | ||
|
|
||
| public static String getExternalServiceAddress(PortablePipelineOptions options) { | ||
| String environmentConfig = options.getDefaultEnvironmentConfig(); | ||
| String environmentOption = | ||
|
|
@@ -549,11 +610,52 @@ public static String getExternalServiceAddress(PortablePipelineOptions options) | |
| } | ||
|
|
||
| private static File zipDirectory(File directory) throws IOException { | ||
| File zipFile = File.createTempFile(directory.getName(), ".zip"); | ||
| try (FileOutputStream fos = new FileOutputStream(zipFile)) { | ||
| ZipFiles.zipDirectory(directory, fos); | ||
| HashCode metadataHash = computeDirectoryMetadataHash(directory); | ||
| try { | ||
| return DIRECTORY_ZIP_CACHE.compute( | ||
| metadataHash, | ||
| (k, cachedZip) -> { | ||
| if (cachedZip != null && cachedZip.exists()) { | ||
| return cachedZip; | ||
| } | ||
| try { | ||
| File zipFile = File.createTempFile(directory.getName(), ".zip"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Note: this fixed a pre-existing temp file leak which is good) |
||
| zipFile.deleteOnExit(); | ||
| try (FileOutputStream fos = new FileOutputStream(zipFile)) { | ||
| ZipFiles.zipDirectory(directory, fos); | ||
| } | ||
| return zipFile; | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| }); | ||
| } catch (UncheckedIOException e) { | ||
| throw e.getCause(); | ||
| } | ||
| } | ||
|
|
||
| private static HashCode computeDirectoryMetadataHash(File directory) { | ||
| Hasher hasher = Hashing.sha256().newHasher(); | ||
| hasher.putString(directory.getAbsolutePath(), StandardCharsets.UTF_8); | ||
| hashDirectoryMetadataRecursive(directory, "", hasher); | ||
| return hasher.hash(); | ||
| } | ||
|
|
||
| private static void hashDirectoryMetadataRecursive(File inputFile, String prefix, Hasher hasher) { | ||
| String entryName = prefix + inputFile.getName(); | ||
| hasher.putString(entryName, StandardCharsets.UTF_8); | ||
| if (inputFile.isDirectory()) { | ||
| File[] children = inputFile.listFiles(); | ||
| if (children != null) { | ||
| Arrays.sort(children); | ||
| for (File child : children) { | ||
| hashDirectoryMetadataRecursive(child, entryName + "/", hasher); | ||
| } | ||
| } | ||
| } else { | ||
| hasher.putLong(inputFile.length()); | ||
| hasher.putLong(inputFile.lastModified()); | ||
| } | ||
| return zipFile; | ||
| } | ||
|
|
||
| private static class ProcessPayloadReferenceJSON { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this isn't a true "equal" but just for file of same path, length, and same modified time, it will be treated equal.
We had a couple of security reports about file staging recently not sure if this would have security implications
Again, setting a retention time for the cache may mitigate potential conflicts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unclear how this would be exploited. any exploit means that an attacker has full write access to the staging bucket, in which case they could anyway overwrite any binary with malicious code.