Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in LockTable #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
199 changes: 107 additions & 92 deletions src/main/java/org/vanilladb/core/storage/tx/concurrency/LockTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void run() {
}
}

private Map<Object, Lockers> lockerMap = new HashMap<Object, Lockers>();
private Map<Object, Lockers> lockerMap = new ConcurrentHashMap<>();
private Map<Long, Set<Object>> lockByMap = new ConcurrentHashMap<Long, Set<Object>>();
private Set<Long> txnsToBeAborted = Collections.synchronizedSet(new HashSet<Long>());
private Map<Long, Object> txWaitMap = new ConcurrentHashMap<Long, Object>();
Expand Down Expand Up @@ -173,30 +173,33 @@ private void toBeAbortedAndNotified(long txNum) {
void sLock(Object obj, long txNum) {
Object anchor = getAnchor(obj);
txWaitMap.put(txNum, anchor);
synchronized (anchor) {
Lockers lks = prepareLockers(obj);
try {
synchronized (anchor) {
Lockers lks = prepareLockers(obj);

if (hasSLock(lks, txNum))
return;
if (hasSLock(lks, txNum))
return;

try {
long timestamp = System.currentTimeMillis();
while (!sLockable(lks, txNum) && !waitingTooLong(timestamp)) {
avoidDeadlock(lks, txNum, S_LOCK);
lks.requestSet.add(txNum);
try {
long timestamp = System.currentTimeMillis();
while (!sLockable(lks, txNum) && !waitingTooLong(timestamp)) {
avoidDeadlock(lks, txNum, S_LOCK);
lks.requestSet.add(txNum);

anchor.wait(MAX_TIME);
lks.requestSet.remove(txNum);
anchor.wait(MAX_TIME);
lks.requestSet.remove(txNum);
}
if (!sLockable(lks, txNum))
throw new LockAbortException();
lks.sLockers.add(txNum);
getObjectSet(txNum).add(obj);
} catch (InterruptedException e) {
throw new LockAbortException("abort tx." + txNum + " by interrupted");
}
if (!sLockable(lks, txNum))
throw new LockAbortException();
lks.sLockers.add(txNum);
getObjectSet(txNum).add(obj);
} catch (InterruptedException e) {
throw new LockAbortException("abort tx." + txNum + " by interrupted");
}
} finally {
txWaitMap.remove(txNum);
}
txWaitMap.remove(txNum);
}

/**
Expand All @@ -212,30 +215,33 @@ void sLock(Object obj, long txNum) {
void xLock(Object obj, long txNum) {
Object anchor = getAnchor(obj);
txWaitMap.put(txNum, anchor);
synchronized (anchor) {
Lockers lks = prepareLockers(obj);
try {
synchronized (anchor) {
Lockers lks = prepareLockers(obj);

if (hasXLock(lks, txNum))
return;
if (hasXLock(lks, txNum))
return;

try {
long timestamp = System.currentTimeMillis();
while (!xLockable(lks, txNum) && !waitingTooLong(timestamp)) {
avoidDeadlock(lks, txNum, X_LOCK);
lks.requestSet.add(txNum);
try {
long timestamp = System.currentTimeMillis();
while (!xLockable(lks, txNum) && !waitingTooLong(timestamp)) {
avoidDeadlock(lks, txNum, X_LOCK);
lks.requestSet.add(txNum);

anchor.wait(MAX_TIME);
lks.requestSet.remove(txNum);
}
if (!xLockable(lks, txNum))
anchor.wait(MAX_TIME);
lks.requestSet.remove(txNum);
}
if (!xLockable(lks, txNum))
throw new LockAbortException();
lks.xLocker = txNum;
getObjectSet(txNum).add(obj);
} catch (InterruptedException e) {
throw new LockAbortException();
lks.xLocker = txNum;
getObjectSet(txNum).add(obj);
} catch (InterruptedException e) {
throw new LockAbortException();
}
}
} finally {
txWaitMap.remove(txNum);
}
txWaitMap.remove(txNum);
}

/**
Expand All @@ -251,30 +257,33 @@ void xLock(Object obj, long txNum) {
void sixLock(Object obj, long txNum) {
Object anchor = getAnchor(obj);
txWaitMap.put(txNum, anchor);
synchronized (anchor) {
Lockers lks = prepareLockers(obj);
try {
synchronized (anchor) {
Lockers lks = prepareLockers(obj);

if (hasSixLock(lks, txNum))
return;
if (hasSixLock(lks, txNum))
return;

try {
long timestamp = System.currentTimeMillis();
while (!sixLockable(lks, txNum) && !waitingTooLong(timestamp)) {
avoidDeadlock(lks, txNum, SIX_LOCK);
lks.requestSet.add(txNum);
try {
long timestamp = System.currentTimeMillis();
while (!sixLockable(lks, txNum) && !waitingTooLong(timestamp)) {
avoidDeadlock(lks, txNum, SIX_LOCK);
lks.requestSet.add(txNum);

anchor.wait(MAX_TIME);
lks.requestSet.remove(txNum);
}
if (!sixLockable(lks, txNum))
anchor.wait(MAX_TIME);
lks.requestSet.remove(txNum);
}
if (!sixLockable(lks, txNum))
throw new LockAbortException();
lks.sixLocker = txNum;
getObjectSet(txNum).add(obj);
} catch (InterruptedException e) {
throw new LockAbortException();
lks.sixLocker = txNum;
getObjectSet(txNum).add(obj);
} catch (InterruptedException e) {
throw new LockAbortException();
}
}
} finally {
txWaitMap.remove(txNum);
}
txWaitMap.remove(txNum);
}

/**
Expand All @@ -289,28 +298,31 @@ void sixLock(Object obj, long txNum) {
void isLock(Object obj, long txNum) {
Object anchor = getAnchor(obj);
txWaitMap.put(txNum, anchor);
synchronized (anchor) {
Lockers lks = prepareLockers(obj);
if (hasIsLock(lks, txNum))
return;
try {
long timestamp = System.currentTimeMillis();
while (!isLockable(lks, txNum) && !waitingTooLong(timestamp)) {
avoidDeadlock(lks, txNum, IS_LOCK);
lks.requestSet.add(txNum);

anchor.wait(MAX_TIME);
lks.requestSet.remove(txNum);
}
if (!isLockable(lks, txNum))
try {
synchronized (anchor) {
Lockers lks = prepareLockers(obj);
if (hasIsLock(lks, txNum))
return;
try {
long timestamp = System.currentTimeMillis();
while (!isLockable(lks, txNum) && !waitingTooLong(timestamp)) {
avoidDeadlock(lks, txNum, IS_LOCK);
lks.requestSet.add(txNum);

anchor.wait(MAX_TIME);
lks.requestSet.remove(txNum);
}
if (!isLockable(lks, txNum))
throw new LockAbortException();
lks.isLockers.add(txNum);
getObjectSet(txNum).add(obj);
} catch (InterruptedException e) {
throw new LockAbortException();
lks.isLockers.add(txNum);
getObjectSet(txNum).add(obj);
} catch (InterruptedException e) {
throw new LockAbortException();
}
}
} finally {
txWaitMap.remove(txNum);
}
txWaitMap.remove(txNum);
}

/**
Expand All @@ -325,30 +337,33 @@ void isLock(Object obj, long txNum) {
void ixLock(Object obj, long txNum) {
Object anchor = getAnchor(obj);
txWaitMap.put(txNum, anchor);
synchronized (anchor) {
Lockers lks = prepareLockers(obj);
try {
synchronized (anchor) {
Lockers lks = prepareLockers(obj);

if (hasIxLock(lks, txNum))
return;
if (hasIxLock(lks, txNum))
return;

try {
long timestamp = System.currentTimeMillis();
while (!ixLockable(lks, txNum) && !waitingTooLong(timestamp)) {
avoidDeadlock(lks, txNum, IX_LOCK);
lks.requestSet.add(txNum);
try {
long timestamp = System.currentTimeMillis();
while (!ixLockable(lks, txNum) && !waitingTooLong(timestamp)) {
avoidDeadlock(lks, txNum, IX_LOCK);
lks.requestSet.add(txNum);

anchor.wait(MAX_TIME);
lks.requestSet.remove(txNum);
}
if (!ixLockable(lks, txNum))
anchor.wait(MAX_TIME);
lks.requestSet.remove(txNum);
}
if (!ixLockable(lks, txNum))
throw new LockAbortException();
lks.ixLockers.add(txNum);
getObjectSet(txNum).add(obj);
} catch (InterruptedException e) {
throw new LockAbortException();
lks.ixLockers.add(txNum);
getObjectSet(txNum).add(obj);
} catch (InterruptedException e) {
throw new LockAbortException();
}
}
} finally {
txWaitMap.remove(txNum);
}
txWaitMap.remove(txNum);
}

/**
Expand Down