Skip to content

Commit 8bcb170

Browse files
author
Tobias Hafner
committed
Fix deadlock on releasing locks
1 parent 258e4fc commit 8bcb170

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

dbms/src/main/java/org/polypheny/db/transaction/deadlocks/DeadlockHandler.java

+10-17
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.polypheny.db.transaction.DeadlockDetectorType;
2626
import org.polypheny.db.transaction.Transaction;
2727
import org.polypheny.db.transaction.locking.Lockable;
28-
import org.polypheny.db.transaction.locking.S2plLockingLevel;
2928
import org.polypheny.db.util.DeadlockException;
3029

3130
public class DeadlockHandler {
@@ -36,8 +35,8 @@ public class DeadlockHandler {
3635
private final DeadlockResolver deadlockResolver;
3736

3837
private final ReentrantReadWriteLock concurrencyLock = new ReentrantReadWriteLock();
39-
private final Lock sharedLock = concurrencyLock.readLock();
40-
private final Lock exclusiveLock = concurrencyLock.writeLock();
38+
private final Lock readLock = concurrencyLock.readLock();
39+
private final Lock writeLock = concurrencyLock.writeLock();
4140

4241
static {
4342
DeadlockDetectorType deadlockDetectorType = (DeadlockDetectorType) RuntimeConfig.S2PL_DEADLOCK_DETECTOR_TYPE.getEnum();
@@ -56,33 +55,27 @@ private DeadlockHandler(DeadlockDetector deadlockDetector, DeadlockResolver dead
5655
this.deadlockResolver = deadlockResolver;
5756
}
5857

59-
public void addAndResolveDeadlock(@NonNull Lockable lockable, @NonNull Transaction transaction, @NonNull Set<Transaction> owners ) {
60-
sharedLock.lock();
58+
public void addAndResolveDeadlock(@NonNull Lockable lockable, @NonNull Transaction transaction, @NonNull Set<Transaction> owners) {
59+
writeLock.lock();
6160
try {
62-
deadlockDetector.add(lockable, transaction, owners );
63-
exclusiveLock.lock();
61+
deadlockDetector.add(lockable, transaction, owners);
6462
List<Transaction> conflictingTransactions = deadlockDetector.getConflictingTransactions();
65-
exclusiveLock.unlock();
66-
// lock can be release here as concurrently adding or removing transactions does not affect the resolution process
67-
while ( !conflictingTransactions.isEmpty() ) {
68-
deadlockResolver.resolveDeadlock( conflictingTransactions );
69-
exclusiveLock.lock();
63+
while (!conflictingTransactions.isEmpty()) {
64+
deadlockResolver.resolveDeadlock(conflictingTransactions);
7065
conflictingTransactions = deadlockDetector.getConflictingTransactions();
71-
exclusiveLock.unlock();
72-
// lock can be release here as concurrently adding or removing transactions does not affect the resolution process
7366
}
7467
} finally {
75-
sharedLock.unlock();
68+
writeLock.unlock();
7669
}
7770
}
7871

7972

8073
public void remove(@NonNull Lockable lockable, @NonNull Transaction transaction ) {
81-
sharedLock.lock();
74+
readLock.lock();
8275
try {
8376
deadlockDetector.remove(lockable, transaction );
8477
} finally {
85-
sharedLock.unlock();
78+
readLock.unlock();
8679
}
8780
}
8881

dbms/src/main/java/org/polypheny/db/transaction/locking/LockableImpl.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private void upgradeToExclusive( Transaction transaction ) throws InterruptedExc
7474
}
7575
long count = owners.remove( transaction );
7676
while ( !owners.isEmpty() ) {
77-
DeadlockHandler.INSTANCE.addAndResolveDeadlock(this, transaction, owners.keySet() );
77+
//DeadlockHandler.INSTANCE.addAndResolveDeadlock(this, transaction, owners.keySet() );
7878
concurrencyCondition.await();
7979
}
8080
isExclusive = true;
@@ -85,26 +85,25 @@ private void upgradeToExclusive( Transaction transaction ) throws InterruptedExc
8585
}
8686

8787

88-
public void release( @NotNull Transaction transaction ) {
88+
public void release(@NotNull Transaction transaction) {
8989
concurrencyLock.lock();
9090
try {
91-
if ( isExclusive ) {
91+
if (isExclusive) {
9292
owners.clear();
9393
isExclusive = false;
9494
}
95-
// this decrements the entry if > 1 else it is removed
96-
owners.computeIfPresent( transaction, ( key, value ) -> {
95+
owners.computeIfPresent(transaction, (key, value) -> {
9796
long newValue = value - 1;
9897
return newValue <= 0 ? null : newValue;
99-
} );
100-
DeadlockHandler.INSTANCE.remove( this, transaction );
98+
});
99+
DeadlockHandler.INSTANCE.remove(this, transaction);
101100
concurrencyCondition.signalAll();
102-
printAcquiredInfo( "R", transaction );
101+
printAcquiredInfo("R", transaction);
103102
} finally {
104103
concurrencyLock.unlock();
105104
}
106-
if ( !isRoot() ) {
107-
parent.release( transaction );
105+
if (!isRoot()) {
106+
parent.release(transaction);
108107
}
109108
}
110109

@@ -136,7 +135,6 @@ private void acquireShared( Transaction transaction ) throws InterruptedExceptio
136135
} finally {
137136
concurrencyLock.unlock();
138137
}
139-
140138
}
141139

142140

0 commit comments

Comments
 (0)