Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

javac.source=1.5

lib.version=0.1.6
lib.version=0.1.8
9 changes: 6 additions & 3 deletions src/main/com/deftlabs/lock/mongo/impl/LockImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

/**
Expand Down
47 changes: 32 additions & 15 deletions src/main/com/deftlabs/lock/mongo/impl/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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);

Expand All @@ -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();
}
}

Expand All @@ -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();
}
Expand All @@ -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();
Expand All @@ -168,8 +185,8 @@ void shutdown() throws InterruptedException {
final Mongo _mongo;
final DistributedLockSvcOptions _svcOptions;
final Map<String, DistributedLock> _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");
Expand Down