-
Notifications
You must be signed in to change notification settings - Fork 337
Gitlab corrupted cache metadata mitigation #11515
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
Open
AlexeyKuznetsov-DD
wants to merge
9
commits into
master
Choose a base branch
from
alexeyk/gitlab-cache-metadata-recovery
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
888f1a2
Band-Aid in case of corrupted cache.
AlexeyKuznetsov-DD 9d4ade1
Fixed script.
AlexeyKuznetsov-DD a6c7a58
Fixed false-positive.
AlexeyKuznetsov-DD 386c242
Refactored to Java checker.
AlexeyKuznetsov-DD 4256a43
Improved Java code.
AlexeyKuznetsov-DD b95da02
Polished log messages to simplify log search.
AlexeyKuznetsov-DD c27a320
Merge branch 'master' into alexeyk/gitlab-cache-metadata-recovery
AlexeyKuznetsov-DD 031201e
Simplify code per review comments.
AlexeyKuznetsov-DD 922533b
Merge branch 'master' into alexeyk/gitlab-cache-metadata-recovery
AlexeyKuznetsov-DD 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
136 changes: 136 additions & 0 deletions
136
.gitlab/gradle-cache/CorruptedGradleCacheMitigator.java
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 |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.lang.reflect.Method; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.regex.Pattern; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Band-aid for "FATAL: unexpected EOF" during GitLab cache extraction. | ||
| * <p> | ||
| * This removes only the damaged workspaces so Gradle regenerates them. | ||
| */ | ||
| class CorruptedGradleCacheMitigator { | ||
| private static final Path CACHES_DIR = Path.of(".gradle/caches"); | ||
|
|
||
| // Immutable-workspace categories and the directory depth at which their workspaces live. | ||
| private static final Map<String, Integer> WORKSPACE_CATEGORIES = | ||
| Map.of("dependencies-accessors", 1, "groovy-dsl", 1, "kotlin-dsl", 2, "transforms", 1); | ||
|
|
||
| // Gradle temporary workspaces are <workspace>-<uuid> and may legitimately lack metadata.bin. | ||
| private static final Pattern TEMPORARY_WORKSPACE = | ||
| Pattern.compile( | ||
| ".*-[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"); | ||
|
|
||
| // Gradle's own metadata reader, resolved reflectively. | ||
| private static Object metadataStore; | ||
| private static Method metadataLoadMethod; | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| var gradleVersion = args[0]; | ||
|
|
||
| var damaged = new ArrayList<Path>(); | ||
| try { | ||
| loadMetadataReader(); | ||
| } catch (Throwable e) { | ||
| System.out.println("Gradle metadata reader unavailable; leaving cache unchanged"); | ||
| e.printStackTrace(); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| for (var workspace : enumerateWorkspaces(gradleVersion)) { | ||
| if (isDamaged(workspace)) { | ||
| damaged.add(workspace); | ||
| } | ||
| } | ||
| } catch (Throwable e) { | ||
| System.out.println("Failed to collect damaged workspaces"); | ||
| e.printStackTrace(); | ||
| return; | ||
| } | ||
|
|
||
| if (!damaged.isEmpty()) { | ||
| System.out.println("Damaged Gradle metadata found, removing:"); | ||
|
|
||
| for (var workspace : damaged) { | ||
| System.out.println(" - " + workspace); | ||
| try { | ||
| remove(workspace); | ||
| } catch (Throwable e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static List<Path> enumerateWorkspaces(String gradleVersion) throws IOException { | ||
| var versionDir = CACHES_DIR.resolve(gradleVersion); | ||
| var workspaces = new ArrayList<Path>(); | ||
| for (var category : WORKSPACE_CATEGORIES.entrySet()) { | ||
| collectAtDepth(versionDir.resolve(category.getKey()), category.getValue(), workspaces); | ||
| } | ||
| return workspaces; | ||
| } | ||
|
|
||
| private static void collectAtDepth(Path dir, int depth, List<Path> out) throws IOException { | ||
| if (!Files.isDirectory(dir)) { | ||
| return; | ||
| } | ||
|
|
||
| if (depth == 0) { | ||
| out.add(dir); | ||
| return; | ||
| } | ||
|
|
||
| try (var entries = Files.list(dir)) { | ||
| for (var child : entries.filter(Files::isDirectory).collect(Collectors.toList())) { | ||
| collectAtDepth(child, depth - 1, out); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void loadMetadataReader() { | ||
| try { | ||
| var storeClass = Class.forName( | ||
| "org.gradle.internal.execution.history.impl.DefaultImmutableWorkspaceMetadataStore"); | ||
| metadataStore = storeClass.getDeclaredConstructor().newInstance(); | ||
| metadataLoadMethod = storeClass.getMethod("loadWorkspaceMetadata", File.class); | ||
| } catch (Throwable e) { | ||
| throw new IllegalStateException("Failed to load Gradle metadata reader", e); | ||
| } | ||
| } | ||
|
|
||
| private static boolean isDamaged(Path workspace) { | ||
| if (TEMPORARY_WORKSPACE.matcher(workspace.getFileName().toString()).matches()) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!Files.isRegularFile(workspace.resolve("metadata.bin"))) { | ||
| return true; | ||
| } | ||
|
|
||
| // A successful return means Gradle's own reader fully deserialized `metadata.bin`. | ||
| try { | ||
| metadataLoadMethod.invoke(metadataStore, workspace.toFile()); | ||
| return false; | ||
| } catch (Throwable e) { | ||
| return true; // truncated/unreadable -> remove it | ||
| } | ||
| } | ||
|
|
||
| private static void remove(Path workspace) { | ||
| try (var paths = Files.walk(workspace)) { | ||
| for (var path : paths.sorted(Comparator.reverseOrder()).collect(Collectors.toList())) { | ||
| Files.deleteIfExists(path); | ||
| } | ||
| } catch (Throwable e) { | ||
| throw new IllegalStateException("Failed to remove: " + workspace, e); | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -uo pipefail | ||
|
|
||
| gradle_version="${1:?usage: mitigate_corrupted_gradle_cache.sh <gradle-version>}" | ||
| script_dir="$(cd "$(dirname "$0")" && pwd)" | ||
|
|
||
| java_home="${JAVA_25_HOME:-}" | ||
| java_bin="${java_home:+$java_home/bin/}java" | ||
| javac_bin="${java_home:+$java_home/bin/}javac" | ||
|
|
||
| gradle_lib="" | ||
| for gradle_home in "${GRADLE_USER_HOME:-$PWD/.gradle}" "$HOME/.gradle"; do | ||
| [[ -d "$gradle_home/wrapper/dists" ]] || continue | ||
| gradle_lib="$( | ||
| find "$gradle_home/wrapper/dists" -path "*/gradle-${gradle_version}/lib" -type d -print -quit \ | ||
| 2>/dev/null | ||
| )" | ||
| [[ -n "$gradle_lib" ]] && break | ||
| done | ||
| if [[ -z "$gradle_lib" ]]; then | ||
| echo "Gradle $gradle_version distribution not found; leaving cache unchanged." >&2 | ||
| exit 0 | ||
| fi | ||
|
|
||
| build_dir="$(mktemp -d)" | ||
| trap 'rm -rf "$build_dir"' EXIT | ||
|
|
||
| # -proc:none keeps the compiler from running annotation processors bundled in the Gradle jars. | ||
| if ! "$javac_bin" -proc:none -classpath "$gradle_lib/*" -d "$build_dir" \ | ||
| "$script_dir/CorruptedGradleCacheMitigator.java"; then | ||
| echo "Could not compile CorruptedGradleCacheMitigator; leaving cache unchanged." >&2 | ||
| exit 0 | ||
| fi | ||
|
|
||
| "$java_bin" -classpath "$build_dir:$gradle_lib/*" \ | ||
| CorruptedGradleCacheMitigator "$gradle_version" |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
issue: This changed Gradle 9.
DefaultImmutableWorkspaceMetadataStore.loadWorkspaceMetadataAssignImmutableWorkspaceStep.loadImmutableWorkspaceIfConsistentDefaultImmutableWorkspaceMetadataStore.loadWorkspaceMetadataAssignImmutableWorkspaceStep.loadImmutableWorkspaceIfCompleteI would believe that Gradle 9.x handle the bad metadata more gracefully due to the use of
Optional, before it raised anUncheckedIOExcewption, now it returnsOptional.empty()