Permalink
Browse files

Changed GroupLock to normal locking instead of optimistic locking

  • Loading branch information...
orfjackal committed Dec 13, 2008
1 parent fe5a0ee commit 7c55e97c89dd7185c98f5ee2f27ec0133c756674
@@ -31,63 +31,64 @@
package net.orfjackal.dimdwarf.db.inmemory;
-import net.orfjackal.dimdwarf.db.OptimisticLockException;
-
import javax.annotation.CheckReturnValue;
import javax.annotation.concurrent.ThreadSafe;
import java.util.*;
-import java.util.concurrent.locks.ReentrantLock;
+import java.util.concurrent.locks.*;
/**
* @author Esko Luontola
* @since 19.11.2008
*/
@ThreadSafe
-public class GroupLock<T> {
+public class GroupLock<T extends Comparable<T>> {
+
+ private final SortedSet<T> lockedKeys = new TreeSet<T>();
- private final Set<T> lockedKeys = new HashSet<T>();
- private final ReentrantLock lock = new ReentrantLock();
+ private final ReentrantLock myLock = new ReentrantLock();
+ private final Condition someKeyWasUnlocked = myLock.newCondition();
@CheckReturnValue
- public LockHandle tryLock(T... keys) throws OptimisticLockException {
- return tryLock(Arrays.asList(keys));
+ public LockHandle lockAll(T... keys) {
+ return lockAll(Arrays.asList(keys));
}
@CheckReturnValue
- public LockHandle tryLock(Collection<T> keys) throws OptimisticLockException {
- lock.lock();
+ public LockHandle lockAll(Collection<T> keys) {
+ myLock.lock();
try {
- checkNoneIsLocked(keys);
- lockedKeys.addAll(keys);
- return new MyLockHandle(keys);
+ SortedSet<T> sortedKeys = new TreeSet<T>(keys);
+ for (T key : sortedKeys) {
+ awaitAndLock(key);
+ }
+ return new MyLockHandle(sortedKeys);
} finally {
- lock.unlock();
+ myLock.unlock();
}
}
- private void checkNoneIsLocked(Collection<T> keys) throws OptimisticLockException {
- for (T key : keys) {
- if (isLocked(key)) {
- throw new OptimisticLockException("Key is already locked: " + key);
- }
+ private void awaitAndLock(T key) {
+ while (isLocked(key)) {
+ someKeyWasUnlocked.awaitUninterruptibly();
}
+ lockedKeys.add(key);
}
public boolean isLocked(T key) {
- lock.lock();
+ myLock.lock();
try {
return lockedKeys.contains(key);
} finally {
- lock.unlock();
+ myLock.unlock();
}
}
public int getLockCount() {
- lock.lock();
+ myLock.lock();
try {
return lockedKeys.size();
} finally {
- lock.unlock();
+ myLock.unlock();
}
}
@@ -98,19 +99,20 @@ public int getLockCount() {
private Collection<T> keys;
public MyLockHandle(Collection<T> keys) {
- this.keys = Collections.unmodifiableCollection(new ArrayList<T>(keys));
+ this.keys = keys;
}
public void unlock() {
- lock.lock();
+ myLock.lock();
try {
if (keys == null) {
throw new IllegalStateException("Keys have already been unlocked: " + keys);
}
lockedKeys.removeAll(keys);
keys = null;
+ someKeyWasUnlocked.signalAll();
} finally {
- lock.unlock();
+ myLock.unlock();
}
}
}
@@ -88,7 +88,7 @@ public DbTableCommitHandle(Map<Blob, Blob> updates, RevisionHandle handle) {
}
private LockHandle prepare() {
- LockHandle lock = keysLockedForCommit.tryLock(updates.keySet());
+ LockHandle lock = keysLockedForCommit.lockAll(updates.keySet());
try {
checkForConflicts();
} catch (OptimisticLockException e) {
@@ -36,9 +36,12 @@
import net.orfjackal.dimdwarf.db.*;
import static net.orfjackal.dimdwarf.db.Blob.EMPTY_BLOB;
import net.orfjackal.dimdwarf.tx.*;
+import net.orfjackal.dimdwarf.util.ThrowingRunnable;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
+import java.util.concurrent.CyclicBarrier;
+
/**
* @author Esko Luontola
* @since 18.8.2008
@@ -84,15 +87,28 @@ private void updateInNewTransaction(Blob key, Blob value) {
tx.prepareAndCommit();
}
- private void tx1PreparesBeforeTx2() {
- tx1.prepare();
+ private void tx1PreparesBeforeTx2() throws Exception {
+ final CyclicBarrier sync = new CyclicBarrier(2);
+
+ Thread t1 = new Thread(new ThrowingRunnable() {
+ public void doRun() throws Throwable {
+ tx1.prepare();
+ sync.await();
+ Thread.sleep(1);
+ tx1.commit();
+ }
+ });
+ t1.start();
+
+ sync.await();
specify(new Block() {
public void run() throws Throwable {
tx2.prepare();
}
}, should.raise(TransactionException.class));
- tx1.commit();
tx2.rollback();
+
+ t1.join();
}
private void tx1PreparesAndCommitsBeforeTx2() {
@@ -230,7 +246,7 @@ public void create() {
table2.update(key, value2);
}
- public void onlyTheFirstToPrepareWillSucceed() {
+ public void onlyTheFirstToPrepareWillSucceed() throws Exception {
tx1PreparesBeforeTx2();
specify(readInNewTransaction(key), should.equal(value1));
}
@@ -251,7 +267,7 @@ public void create() {
table2.update(key, value2);
}
- public void onlyTheFirstToPrepareWillSucceed() {
+ public void onlyTheFirstToPrepareWillSucceed() throws Exception {
tx1PreparesBeforeTx2();
specify(readInNewTransaction(key), should.equal(value1));
}
@@ -279,7 +295,7 @@ public void create() {
table2.delete(key);
}
- public void onlyTheFirstToPrepareWillSucceed() {
+ public void onlyTheFirstToPrepareWillSucceed() throws Exception {
tx1PreparesBeforeTx2();
specify(readInNewTransaction(key), should.equal(Blob.EMPTY_BLOB));
}
@@ -33,10 +33,9 @@
import jdave.*;
import jdave.junit4.JDaveRunner;
-import net.orfjackal.dimdwarf.db.OptimisticLockException;
import org.junit.runner.RunWith;
-import java.util.Arrays;
+import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author Esko Luontola
@@ -52,6 +51,17 @@ public void create() throws Exception {
lock = new GroupLock<String>();
}
+ private static void unlockInNewThread(final LockHandle handle, final AtomicBoolean wasUnlockedFirst) {
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ wasUnlockedFirst.set(true);
+ handle.unlock();
+ }
+ });
+ t.setPriority(Thread.MIN_PRIORITY);
+ t.start();
+ }
+
public class WhenNothingIsLocked {
@@ -62,40 +72,40 @@ public void noKeysAreLocked() {
}
public class WhenOneKeyIsLocked {
+ private LockHandle handle;
public void create() {
- lock.tryLock("A");
+ handle = lock.lockAll("A");
}
public void oneKeyIsLocked() {
specify(lock.isLocked("A"));
specify(lock.getLockCount(), should.equal(1));
}
- public void otherKeysAreNotLocked() {
- specify(lock.isLocked("B"), should.equal(false));
+ public void theLockedKeyCanNotBeRelockedUntilItIsFirstUnlocked() {
+ AtomicBoolean wasUnlocked = new AtomicBoolean(false);
+ unlockInNewThread(handle, wasUnlocked);
+ lock.lockAll("A");
+ specify(wasUnlocked.get());
}
- public void theSameKeyCanNotBeLockedTwise() {
- specify(new Block() {
- public void run() throws Throwable {
- lock.tryLock("A");
- }
- }, should.raise(OptimisticLockException.class));
- specify(lock.getLockCount(), should.equal(1));
+ public void otherKeysAreNotLocked() {
+ specify(lock.isLocked("B"), should.equal(false));
}
public void otherKeysMayBeLocked() {
- lock.tryLock("B");
+ lock.lockAll("B");
specify(lock.isLocked("B"));
specify(lock.getLockCount(), should.equal(2));
}
}
public class WhenManyKeysAreLocked {
+ private LockHandle handle;
public void create() {
- lock.tryLock(Arrays.asList("A", "B"));
+ handle = lock.lockAll("A", "B");
}
public void thoseKeysAreLocked() {
@@ -104,17 +114,16 @@ public void thoseKeysAreLocked() {
specify(lock.getLockCount(), should.equal(2));
}
- public void anOverlappingSetOfKeysCanNotBeLocked() {
- specify(new Block() {
- public void run() throws Throwable {
- lock.tryLock(Arrays.asList("B", "C"));
- }
- }, should.raise(OptimisticLockException.class));
+ public void anOverlappingSetOfKeysCanNotBeRelockedUntilTheyAreFirstUnlocked() {
+ AtomicBoolean wasUnlocked = new AtomicBoolean(false);
+ unlockInNewThread(handle, wasUnlocked);
+ lock.lockAll("B", "C");
+ specify(wasUnlocked.get());
specify(lock.getLockCount(), should.equal(2));
}
public void aDistinctSetOfKeysMayBeLocked() {
- lock.tryLock(Arrays.asList("C", "D"));
+ lock.lockAll("C", "D");
specify(lock.getLockCount(), should.equal(4));
}
}
@@ -123,8 +132,8 @@ public void aDistinctSetOfKeysMayBeLocked() {
private LockHandle handleA;
public void create() {
- handleA = lock.tryLock("A");
- lock.tryLock("B");
+ handleA = lock.lockAll("A");
+ lock.lockAll("B");
handleA.unlock();
}
@@ -149,7 +158,7 @@ public void run() throws Throwable {
public void aUsedHandleCanNotUnlockAKeyWhichIsRelocked() {
specify(lock.getLockCount(), should.equal(1));
final LockHandle oldHandleA = handleA;
- final LockHandle newHandleA = lock.tryLock("A");
+ final LockHandle newHandleA = lock.lockAll("A");
specify(lock.getLockCount(), should.equal(2));
specify(new Block() {
public void run() throws Throwable {
@@ -0,0 +1,49 @@
+/*
+ * This file is part of Dimdwarf Application Server <http://dimdwarf.sourceforge.net/>
+ *
+ * Copyright (c) 2008, Esko Luontola. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * * Neither the name of the copyright holder nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package net.orfjackal.dimdwarf.util;
+
+/**
+ * @author Esko Luontola
+ * @since 13.12.2008
+ */
+public abstract class ThrowingRunnable implements Runnable {
+
+ public final void run() {
+ try {
+ doRun();
+ } catch (Throwable t) {
+ throw new RuntimeException(t);
+ }
+ }
+
+ public abstract void doRun() throws Throwable;
+}

0 comments on commit 7c55e97

Please sign in to comment.