diff --git a/build.properties b/build.properties index 10e74e4..d5a5530 100644 --- a/build.properties +++ b/build.properties @@ -16,4 +16,4 @@ javac.source=1.5 -lib.version=0.1.6 +lib.version=0.1.8 diff --git a/src/main/com/deftlabs/lock/mongo/impl/LockImpl.java b/src/main/com/deftlabs/lock/mongo/impl/LockImpl.java index eb63fcd..84c4b8e 100644 --- a/src/main/com/deftlabs/lock/mongo/impl/LockImpl.java +++ b/src/main/com/deftlabs/lock/mongo/impl/LockImpl.java @@ -179,10 +179,13 @@ private boolean tryDistributedLock() { } @Override public void unlock() { - LockDao.unlock(_mongo, _name, _svcOptions, _lockOptions, _lockId); _locked.set(false); - _lockId = null; - LockSupport.unpark(_waitingThreads.peek()); + try { + LockDao.unlock(_mongo, _name, _svcOptions, _lockOptions, _lockId); + } finally { + _lockId = null; + LockSupport.unpark(_waitingThreads.peek()); + } } /** diff --git a/src/main/com/deftlabs/lock/mongo/impl/Monitor.java b/src/main/com/deftlabs/lock/mongo/impl/Monitor.java index 590e146..6d97d41 100644 --- a/src/main/com/deftlabs/lock/mongo/impl/Monitor.java +++ b/src/main/com/deftlabs/lock/mongo/impl/Monitor.java @@ -53,7 +53,7 @@ static class LockHeartbeat extends MonitorThread { } @Override - boolean monitor() throws InterruptedException { + void monitor() { for (final String lockName : _locks.keySet()) { final DistributedLock lock = _locks.get(lockName); @@ -63,8 +63,11 @@ boolean monitor() throws InterruptedException { LockDao.heartbeat(_mongo, lockName, lockId, lock.getOptions(), _svcOptions); } + } - return _shutdown.await(_svcOptions.getHeartbeatFrequency(), TimeUnit.MILLISECONDS); + @Override + long awaitMillis() { + return _svcOptions.getHeartbeatFrequency(); } } @@ -80,9 +83,13 @@ static class LockTimeout extends MonitorThread { } @Override - boolean monitor() throws InterruptedException { + void monitor() { LockDao.expireInactiveLocks(_mongo, _svcOptions); - return _shutdown.await(_svcOptions.getTimeoutFrequency(), TimeUnit.MILLISECONDS); + } + + @Override + long awaitMillis() { + return _svcOptions.getTimeoutFrequency(); } } @@ -100,7 +107,7 @@ static class LockUnlocked extends MonitorThread { } @Override - boolean monitor() throws InterruptedException { + void monitor() { for (final String lockName : _locks.keySet()) { final DistributedLock lock = _locks.get(lockName); @@ -112,8 +119,11 @@ boolean monitor() throws InterruptedException { // The lock is not locked, wakeup any blocking threads. lock.wakeupBlocked(); } + } - return _shutdown.await(_svcOptions.getLockUnlockedFrequency(), TimeUnit.MILLISECONDS); + @Override + long awaitMillis() { + return _svcOptions.getLockUnlockedFrequency(); } } @@ -140,13 +150,18 @@ private static abstract class MonitorThread extends Thread { } @Override public void run() { - boolean shutdown = false; try { - while (!shutdown) { - try { shutdown = monitor(); - } catch (final InterruptedException ie) { break; - } catch (final Throwable t) { LOG.log(Level.SEVERE, t.getMessage(), t); } - } + // do-while to eagerly try at startup for any initial cleanup. + do { + try { + monitor(); + } catch (final Throwable t) { + LOG.log(Level.SEVERE, t.getMessage(), t); + } + } while (!_shutdown.await(awaitMillis(), TimeUnit.MILLISECONDS)); + } catch (final InterruptedException ignored) { + // Safe exit. + Thread.currentThread().interrupt(); } finally { _exited.countDown(); } @@ -156,7 +171,9 @@ private static abstract class MonitorThread extends Thread { * Performs check and awaits shutdown signal for configured amount of milliseconds * @return true if shutdown() was called, false otherwise. */ - abstract boolean monitor() throws InterruptedException; + abstract void monitor(); + + abstract long awaitMillis(); void shutdown() throws InterruptedException { _shutdown.countDown(); @@ -168,8 +185,8 @@ void shutdown() throws InterruptedException { final Mongo _mongo; final DistributedLockSvcOptions _svcOptions; final Map _locks; - final CountDownLatch _shutdown; - final CountDownLatch _exited; + private final CountDownLatch _shutdown; + private final CountDownLatch _exited; } private static final Logger LOG = Logger.getLogger("com.deftlabs.lock.mongo.Monitor");