code.onehippo.org is currently readonly. We are migrating to code.bloomreach.com, please continue working there on Monday 14/12. See: https://docs.bloomreach.com/display/engineering/GitLab

Commit e209d242 authored by Ard Schrijvers's avatar Ard Schrijvers

REPO-1811 Implement new LockResource getters

parent bb1f233b
......@@ -77,35 +77,48 @@ public abstract class AbstractLockManager implements InternalLockManager {
final MutableLock lock = localLocks.get(key);
if (lock == null) {
getLogger().debug("Create lock '{}' for thread '{}'", key, Thread.currentThread().getName());
localLocks.put(key, createLock(key, Thread.currentThread().getName()));
return new LockResourceImpl(key);
MutableLock newLock = createLock(key, Thread.currentThread().getName());
localLocks.put(key, newLock);
return new LockResourceImpl(newLock);
}
final Thread lockThread = lock.getThread().get();
if (lockThread == null || !lockThread.isAlive()) {
getLogger().warn("Thread '{}' that created lock for '{}' has stopped without releasing the lock. Thread '{}' " +
"now gets the lock", lock.getLockThread(), key, Thread.currentThread().getName());
unlock(key);
localLocks.put(key, createLock(key, Thread.currentThread().getName()));
return new LockResourceImpl(key);
MutableLock newLock = createLock(key, Thread.currentThread().getName());
localLocks.put(key, newLock);
return new LockResourceImpl(newLock);
}
if (lockThread == Thread.currentThread()) {
getLogger().debug("Thread '{}' already contains lock '{}', increase hold count", Thread.currentThread().getName(), key);
lock.increment();
return new LockResourceImpl(key);
return new LockResourceImpl(lock);
}
throw new LockException(String.format("This thread '%s' cannot lock '%s' : already locked by thread '%s'",
Thread.currentThread().getName(), key, lockThread.getName()));
}
private class LockResourceImpl implements LockResource {
private String key;
LockResourceImpl(final String key) {
this.key = key;
private MutableLock lock;
LockResourceImpl(final MutableLock lock) {
this.lock = lock;
}
@Override
public void close() {
unlock(key);
unlock(lock.getLockKey());
}
@Override
public Lock getLock() {
return lock;
}
@Override
public Thread getHolder() {
return lock.getThread().get();
}
}
......
......@@ -33,6 +33,7 @@ import org.onehippo.testutils.log4j.Log4jInterceptor;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
......@@ -92,6 +93,9 @@ public class LockManagerBasicTest extends AbstractLockManagerTest {
try (LockResource lock = lockManager.lock(key)) {
try (LockResource lock2 = lockManager.lock(key)) {
dbRowAssertion(key, "RUNNING");
assertFalse(lock == lock2);
assertTrue(lock.getLock() == lock2.getLock());
assertTrue(lock.getHolder() == lock2.getHolder());
}
dbRowAssertion(key, "RUNNING");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment