From be1698a8c8875513f1416aadeb4714007a892f5f Mon Sep 17 00:00:00 2001 From: repolevedavaj Date: Mon, 15 Apr 2024 21:16:28 +0200 Subject: [PATCH] Log used cacheValidityDecidingFile(s) and hash --- .../plugins/jobcacher/ArbitraryFileCache.java | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java b/src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java index 538a886..690b554 100644 --- a/src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java +++ b/src/main/java/jenkins/plugins/jobcacher/ArbitraryFileCache.java @@ -57,7 +57,9 @@ import java.security.NoSuchAlgorithmException; import java.time.Duration; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import java.util.zip.Deflater; /** @@ -265,7 +267,7 @@ private boolean isCacheOutdated(ObjectPath cachesRoot, FilePath workspace, TaskL return true; } - if (!matchesCurrentCacheValidityDecidingFileHash(previousClearCacheTriggerFileHash, workspace)) { + if (!matchesCurrentCacheValidityDecidingFileHash(previousClearCacheTriggerFileHash, workspace, listener)) { logMessage("cacheValidityDecidingFile configured, but previous hash does not match - cache outdated", listener); return true; } @@ -287,7 +289,7 @@ private String createCacheValidityDecidingFileHashFileName(String baseName) { return baseName + CACHE_VALIDITY_DECIDING_FILE_HASH_FILE_EXTENSION; } - private boolean matchesCurrentCacheValidityDecidingFileHash(ObjectPath previousCacheValidityDecidingFileHashFile, FilePath workspace) throws IOException, InterruptedException { + private boolean matchesCurrentCacheValidityDecidingFileHash(ObjectPath previousCacheValidityDecidingFileHashFile, FilePath workspace, TaskListener listener) throws IOException, InterruptedException { if (!isOneCacheValidityDecidingFilePresent(workspace)) { return false; } @@ -297,33 +299,44 @@ private boolean matchesCurrentCacheValidityDecidingFileHash(ObjectPath previousC try (InputStream inputStream = tempFile.get().read()) { String previousCacheValidityDecidingFileHash = IOUtils.toString(inputStream, StandardCharsets.UTF_8); - String currentCacheValidityDecidingFileHash = getCurrentCacheValidityDecidingFileHash(workspace); + String currentCacheValidityDecidingFileHash = getCurrentCacheValidityDecidingFileHash(workspace, listener); return StringUtils.equals(previousCacheValidityDecidingFileHash, currentCacheValidityDecidingFileHash); } } } - private String getCurrentCacheValidityDecidingFileHash(FilePath workspace) throws IOException, InterruptedException { + private String getCurrentCacheValidityDecidingFileHash(FilePath workspace, TaskListener listener) throws IOException, InterruptedException { if (!isOneCacheValidityDecidingFilePresent(workspace)) { throw new IllegalStateException("path " + cacheValidityDecidingFile + " cannot be resolved within the current workspace"); } try { MessageDigest messageDigest = MessageDigest.getInstance(CACHE_VALIDITY_DECIDING_FILE_DIGEST_ALGORITHM); - for (FilePath cacheValidityDecidingFile : resolveCacheValidityDecidingFiles(workspace)) { + + FilePath[] cacheValidityDecidingFiles = resolveCacheValidityDecidingFiles(workspace); + for (FilePath cacheValidityDecidingFile : cacheValidityDecidingFiles) { try (InputStream inputStream = cacheValidityDecidingFile.read()) { DigestInputStream digestInputStream = new DigestInputStream(inputStream, messageDigest); IOUtils.copy(digestInputStream, NullOutputStream.NULL_OUTPUT_STREAM); } } - return Util.toHexString(messageDigest.digest()); + String hash = Util.toHexString(messageDigest.digest()); + logMessage("got hash " + hash + " for cacheValidityDecidingFile(s) - actual file(s): " + joinAsRelativePaths(cacheValidityDecidingFiles), listener); + + return hash; } catch (NoSuchAlgorithmException e) { throw new IOException(e); } } + private String joinAsRelativePaths(FilePath[] cacheValidityDecidingFiles) { + return Arrays.stream(cacheValidityDecidingFiles) + .map(FilePath::getRemote) + .collect(Collectors.joining(", ")); + } + private boolean isOneCacheValidityDecidingFilePresent(FilePath workspace) throws IOException, InterruptedException { return resolveCacheValidityDecidingFiles(workspace).length > 0; } @@ -390,7 +403,7 @@ public void save(ObjectPath cachesRoot, Run build, FilePath workspace, Lau try { compressionMethod.getCacheStrategy().cache(resolvedPath, includes, excludes, useDefaultExcludes, cache, workspace); if (isCacheValidityDecidingFileConfigured() && isOneCacheValidityDecidingFilePresent(workspace)) { - updateSkipCacheTriggerFileHash(cachesRoot, workspace); + updateSkipCacheTriggerFileHash(cachesRoot, workspace, listener); } long cacheCreationEndTime = System.nanoTime(); logMessage("Cache created in " + Duration.ofNanos(cacheCreationEndTime - cacheCreationStartTime).toMillis() + "ms", listener); @@ -407,9 +420,9 @@ private boolean isMaybeInsideDockerContainer(FilePath workspace) { return workspace.getChannel() == null || workspace.getChannel() instanceof LocalChannel; } - private void updateSkipCacheTriggerFileHash(ObjectPath cachesRoot, FilePath workspace) throws IOException, InterruptedException { + private void updateSkipCacheTriggerFileHash(ObjectPath cachesRoot, FilePath workspace, TaskListener listener) throws IOException, InterruptedException { try (TempFile tempFile = WorkspaceHelper.createTempFile(workspace, CACHE_VALIDITY_DECIDING_FILE_HASH_FILE_EXTENSION)) { - tempFile.get().write(getCurrentCacheValidityDecidingFileHash(workspace), StandardCharsets.UTF_8.displayName()); + tempFile.get().write(getCurrentCacheValidityDecidingFileHash(workspace, listener), StandardCharsets.UTF_8.displayName()); ObjectPath skipCacheTriggerFileHashFile = cachesRoot.child(getSkipCacheTriggerFileHashFileName()); skipCacheTriggerFileHashFile.copyFrom(tempFile.get());