From 4c80b5c150396755a472d99302f8409912e2cf13 Mon Sep 17 00:00:00 2001 From: Marc Hermans Date: Mon, 24 Jun 2024 13:24:55 +0200 Subject: [PATCH] Remove windows specific handling of Locking since it does not work as intended with respect to the gradle worker daemons. --- .../common/caching/CentralCacheService.java | 106 +----------------- 1 file changed, 6 insertions(+), 100 deletions(-) diff --git a/common/src/main/java/net/neoforged/gradle/common/caching/CentralCacheService.java b/common/src/main/java/net/neoforged/gradle/common/caching/CentralCacheService.java index 4cf0350de..1599f57aa 100644 --- a/common/src/main/java/net/neoforged/gradle/common/caching/CentralCacheService.java +++ b/common/src/main/java/net/neoforged/gradle/common/caching/CentralCacheService.java @@ -156,7 +156,7 @@ private void executeCacheLookupOrCreation(Task task, DoCreate onCreate, File loc // Acquiring an exclusive lock on the file debugLog(task, "Acquiring lock on file: " + lockFile.getAbsolutePath()); - try(FileBasedLock fileBasedLock = lockManager.createLock(task, lockFile)) { + try (FileBasedLock fileBasedLock = lockManager.createLock(task, lockFile)) { try { fileBasedLock.updateAccessTime(); debugLog(task, "Lock acquired on file: " + lockFile.getAbsolutePath()); @@ -188,7 +188,7 @@ private void executeCacheLookupOrCreation(Task task, DoCreate onCreate, File loc // Acquiring an exclusive lock on the file debugLog(task, "Acquiring lock on file: " + lockFile.getAbsolutePath()); - try(FileBasedLock fileBasedLock = lockManager.createLock(task, lockFile)) { + try (FileBasedLock fileBasedLock = lockManager.createLock(task, lockFile)) { try { fileBasedLock.updateAccessTime(); debugLog(task, "Lock acquired on file: " + lockFile.getAbsolutePath()); @@ -319,13 +319,13 @@ private void logCacheMiss(Task task) { private void debugLog(Task task, String message) { if (getParameters().getDebugCache().get()) { - task.getLogger().lifecycle( " > [" + System.currentTimeMillis() + "] (" + ProcessHandle.current().pid() + "): " + message); + task.getLogger().lifecycle(" > [" + System.currentTimeMillis() + "] (" + ProcessHandle.current().pid() + "): " + message); } } private void debugLog(Task task, String message, Exception e) { if (getParameters().getDebugCache().get()) { - task.getLogger().lifecycle( " > [" + System.currentTimeMillis() + "] (" + ProcessHandle.current().pid() + "): " + message, e); + task.getLogger().lifecycle(" > [" + System.currentTimeMillis() + "] (" + ProcessHandle.current().pid() + "): " + message, e); } } @@ -374,7 +374,7 @@ public void hash(TaskInputs inputs) throws IOException { }); for (File file : inputs.getFiles()) { - try(Stream pathStream = Files.walk(file.toPath())) { + try (Stream pathStream = Files.walk(file.toPath())) { for (Path path : pathStream.filter(Files::isRegularFile).toList()) { debugLog(task, "Hashing task input file: " + path.toAbsolutePath()); hasher.putString(path.getFileName().toString()); @@ -403,11 +403,7 @@ private interface FileBasedLock extends AutoCloseable { private final class LockManager { public FileBasedLock createLock(Task task, File lockFile) { - if (WindowsFileBasedLock.isSupported()) { - return new WindowsFileBasedLock(task, lockFile); - } else { - return new IOControlledFileBasedLock(task, lockFile); - } + return new IOControlledFileBasedLock(task, lockFile); } } @@ -444,96 +440,6 @@ public void close() throws Exception { } } - private final class WindowsFileBasedLock extends HealthFileUsingFileBasedLock { - - private static boolean isSupported() { - return SystemUtils.IS_OS_WINDOWS; - } - - private final Task task; - private final File lockFile; - private final NioBasedFileLock nioBasedFileLock; - - private WindowsFileBasedLock(Task task, File lockFile) { - super(new File(lockFile.getParentFile(), HEALTHY_FILE_NAME)); - - if (!isSupported() || !NioBasedFileLock.isSupported()) { - throw new UnsupportedOperationException("Windows file locks are not supported on this platform, or NIO based locking is not supported!"); - } - - this.task = task; - this.lockFile = lockFile; - this.nioBasedFileLock = new NioBasedFileLock(task, lockFile); - } - - @Override - public void updateAccessTime() { - if (!lockFile.setLastModified(System.currentTimeMillis())) { - throw new RuntimeException("Failed to update access time for lock file: " + lockFile.getAbsolutePath()); - } - - debugLog(task, "Updated access time for lock file: " + lockFile.getAbsolutePath()); - } - - @Override - public void close() throws Exception { - //Close the super first, this ensures that the healthy file is created only if the lock was successful - super.close(); - this.nioBasedFileLock.close(); - debugLog(task, "Lock file closed: " + lockFile.getAbsolutePath()); - } - } - - private final class NioBasedFileLock implements AutoCloseable { - - public static boolean isSupported() { - return SystemUtils.IS_OS_WINDOWS; - } - - private static final Map FILE_LOCKS = new ConcurrentHashMap<>(); - - private final Task task; - private final File lockFile; - private final RandomAccessFile lockFileAccess; - private final FileChannel fileChannel; - private final FileLock fileLock; - - public NioBasedFileLock(Task task, File lockFile) { - if (!isSupported()) { - throw new UnsupportedOperationException("NIO file locks are not supported on this platform"); - } - - this.task = task; - this.lockFile = lockFile; - - try { - this.lockFileAccess = new RandomAccessFile(lockFile, "rw"); - this.fileChannel = this.lockFileAccess.getChannel(); - this.fileLock = this.fileChannel.lock(); - - final OwnerAwareReentrantLock lock = FILE_LOCKS.computeIfAbsent(lockFile.getAbsolutePath(), s1 -> new OwnerAwareReentrantLock()); - debugLog(task, "Created local thread lock for thread: " + Thread.currentThread().getId() + " - " + Thread.currentThread().getName()); - lock.lock(); - - debugLog(task, "Acquired lock on file: " + lockFile.getAbsolutePath()); - } catch (IOException e) { - throw new RuntimeException("Failed to acquire lock on file: " + lockFile.getAbsolutePath(), e); - } - } - - @Override - public void close() throws Exception { - fileLock.release(); - fileChannel.close(); - lockFileAccess.close(); - - final OwnerAwareReentrantLock lock = FILE_LOCKS.get(lockFile.getAbsolutePath()); - lock.unlock(); - - debugLog(task, "Released lock on file: " + lockFile.getAbsolutePath()); - } - } - private final class IOControlledFileBasedLock extends HealthFileUsingFileBasedLock { private final Task task;