From 3e344bc8278d9e3a2e08a90aa23d5f4b5a3ee696 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Thu, 12 Dec 2013 22:37:16 +0000 Subject: [PATCH] throw a timeout exception if not possible to get the lock after the timeout git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1550560 13f79535-47bb-0310-9956-ffa450edef68 --- .../filelock/DefaultFileLockManager.java | 114 ++++++++++-------- .../common/filelock/FileLockManager.java | 4 +- .../filelock/DefaultFileLockManagerTest.java | 20 +-- 3 files changed, 76 insertions(+), 62 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/DefaultFileLockManager.java b/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/DefaultFileLockManager.java index b54889535..e55a56ad2 100644 --- a/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/DefaultFileLockManager.java +++ b/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/DefaultFileLockManager.java @@ -47,7 +47,7 @@ public class DefaultFileLockManager @Override public Lock readFileLock( File file ) - throws FileLockException, FileNotFoundException + throws FileLockException, FileLockTimeoutException { if ( skipLocking ) { @@ -57,43 +57,50 @@ public class DefaultFileLockManager StopWatch stopWatch = new StopWatch(); boolean acquired = false; - Lock lock = new Lock( file, false ); + try + { + Lock lock = new Lock( file, false ); - stopWatch.start(); + stopWatch.start(); - while ( !acquired ) - { - if ( timeout > 0 ) + while ( !acquired ) { - long delta = stopWatch.getTotalTimeMillis(); - if ( delta > timeout ) + if ( timeout > 0 ) { - log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file ); - // we could not get the lock within the timeout period, so return null - return null; + long delta = stopWatch.getTotalTimeMillis(); + if ( delta > timeout ) + { + log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file ); + // we could not get the lock within the timeout period, so throw FileLockTimeoutException + throw new FileLockTimeoutException(); + } + } + try + { + lock.openLock( false, timeout > 0 ); + acquired = true; + } + catch ( IOException e ) + { + throw new FileLockException( e.getMessage(), e ); + } + catch ( IllegalStateException e ) + { + log.debug( "openLock {}:{}", e.getClass(), e.getMessage() ); } } - try - { - lock.openLock( false, timeout > 0 ); - acquired = true; - } - catch ( IOException e ) - { - throw new FileLockException( e.getMessage(), e ); - } - catch ( IllegalStateException e ) - { - log.debug( "openLock {}:{}", e.getClass(), e.getMessage() ); - } + return lock; + } + catch ( FileNotFoundException e ) + { + throw new FileLockException( e.getMessage(), e ); } - return lock; } @Override public Lock writeFileLock( File file ) - throws FileLockException, FileNotFoundException + throws FileLockException, FileLockTimeoutException { if ( skipLocking ) { @@ -103,37 +110,44 @@ public class DefaultFileLockManager StopWatch stopWatch = new StopWatch(); boolean acquired = false; - Lock lock = new Lock( file, true ); + try + { + Lock lock = new Lock( file, true ); - stopWatch.start(); + stopWatch.start(); - while ( !acquired ) - { - if ( timeout > 0 ) + while ( !acquired ) { - long delta = stopWatch.getTotalTimeMillis(); - if ( delta > timeout ) + if ( timeout > 0 ) { - log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file ); - // we could not get the lock within the timeout period, so return null - return null; + long delta = stopWatch.getTotalTimeMillis(); + if ( delta > timeout ) + { + log.warn( "Cannot acquire read lock within {} millis. Will skip the file: {}", timeout, file ); + // we could not get the lock within the timeout period, so throw FileLockTimeoutException + throw new FileLockTimeoutException(); + } + } + try + { + lock.openLock( true, timeout > 0 ); + acquired = true; + } + catch ( IOException e ) + { + throw new FileLockException( e.getMessage(), e ); + } + catch ( IllegalStateException e ) + { + log.debug( "openLock {}:{}", e.getClass(), e.getMessage() ); } } - try - { - lock.openLock( true, timeout > 0 ); - acquired = true; - } - catch ( IOException e ) - { - throw new FileLockException( e.getMessage(), e ); - } - catch ( IllegalStateException e ) - { - log.debug( "openLock {}:{}", e.getClass(), e.getMessage() ); - } + return lock; + } + catch ( FileNotFoundException e ) + { + throw new FileLockException( e.getMessage(), e ); } - return lock; } diff --git a/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/FileLockManager.java b/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/FileLockManager.java index fa90933ea..6746f4e3b 100644 --- a/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/FileLockManager.java +++ b/archiva-modules/archiva-base/archiva-filelock/src/main/java/org/apache/archiva/common/filelock/FileLockManager.java @@ -28,10 +28,10 @@ import java.io.FileNotFoundException; public interface FileLockManager { Lock writeFileLock( File file ) - throws FileLockException, FileNotFoundException; + throws FileLockException, FileLockTimeoutException; Lock readFileLock( File file ) - throws FileLockException, FileNotFoundException; + throws FileLockException, FileLockTimeoutException; void release( Lock lock ) throws FileLockException, FileNotFoundException; diff --git a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java index 9f2e7ad10..6aa437248 100644 --- a/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java +++ b/archiva-modules/archiva-base/archiva-filelock/src/test/java/org/apache/archiva/common/filelock/DefaultFileLockManagerTest.java @@ -81,7 +81,7 @@ public class DefaultFileLockManagerTest } public void thread1() - throws FileLockException, IOException + throws FileLockException, FileLockTimeoutException, IOException { logger.info( "thread1" ); Lock lock = fileLockManager.writeFileLock( this.file ); @@ -99,7 +99,7 @@ public class DefaultFileLockManagerTest } public void thread2() - throws FileLockException, IOException + throws FileLockException, FileLockTimeoutException, IOException { logger.info( "thread2" ); Lock lock = fileLockManager.writeFileLock( this.file ); @@ -117,7 +117,7 @@ public class DefaultFileLockManagerTest } public void thread3() - throws FileLockException, IOException + throws FileLockException, FileLockTimeoutException, IOException { logger.info( "thread3" ); Lock lock = fileLockManager.readFileLock( this.file ); @@ -135,7 +135,7 @@ public class DefaultFileLockManagerTest } public void thread4() - throws FileLockException, IOException + throws FileLockException, FileLockTimeoutException, IOException { logger.info( "thread4" ); Lock lock = fileLockManager.writeFileLock( this.file ); @@ -153,7 +153,7 @@ public class DefaultFileLockManagerTest } public void thread5() - throws FileLockException, IOException + throws FileLockException, FileLockTimeoutException, IOException { logger.info( "thread5" ); Lock lock = fileLockManager.writeFileLock( this.file ); @@ -171,7 +171,7 @@ public class DefaultFileLockManagerTest } public void thread6() - throws FileLockException, IOException + throws FileLockException, FileLockTimeoutException, IOException { logger.info( "thread6" ); Lock lock = fileLockManager.readFileLock( this.file ); @@ -189,7 +189,7 @@ public class DefaultFileLockManagerTest } public void thread7() - throws FileLockException, IOException + throws FileLockException, FileLockTimeoutException, IOException { logger.info( "thread7" ); Lock lock = fileLockManager.writeFileLock( this.file ); @@ -207,7 +207,7 @@ public class DefaultFileLockManagerTest } public void thread8() - throws FileLockException, IOException + throws FileLockException, FileLockTimeoutException, IOException { logger.info( "thread8" ); Lock lock = fileLockManager.readFileLock( this.file ); @@ -225,7 +225,7 @@ public class DefaultFileLockManagerTest } public void thread9() - throws FileLockException, IOException + throws FileLockException, FileLockTimeoutException, IOException { logger.info( "thread7" ); Lock lock = fileLockManager.writeFileLock( this.file ); @@ -243,7 +243,7 @@ public class DefaultFileLockManagerTest } public void thread10() - throws FileLockException, IOException + throws FileLockException, FileLockTimeoutException, IOException { logger.info( "thread10" ); Lock lock = fileLockManager.readFileLock( this.file ); -- 2.39.5