Skip to content

Commit

Permalink
changed triple in Lockmanager to dedicated record
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Oct 24, 2024
1 parent 35e5e30 commit 7b4c449
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019-2024 The Polypheny Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.polypheny.db.transaction;

import org.polypheny.db.transaction.Lock.LockMode;

public record LockInformation( Thread thread, LockMode mode, PolyXid xid ) {

}
14 changes: 7 additions & 7 deletions dbms/src/main/java/org/polypheny/db/transaction/LockManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class LockManager {

private boolean isExclusive = false;
private final Set<Xid> owners = new HashSet<>();
private final ConcurrentLinkedQueue<Triple<Thread, LockMode, PolyXid>> waiters = new ConcurrentLinkedQueue<>();
private final ConcurrentLinkedQueue<LockInformation> waiters = new ConcurrentLinkedQueue<>();
private final ReentrantLock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();

Expand All @@ -65,7 +65,7 @@ public void lock( LockMode mode, @NonNull Transaction transaction ) throws Deadl
Thread thread = Thread.currentThread();

synchronized ( waiters ) {
if ( waiters.add( Triple.of( thread, mode, transaction.getXid() ) ) ) {
if ( waiters.add( new LockInformation( thread, mode, transaction.getXid() ) ) ) {
log.debug( "could not add" );
}
}
Expand All @@ -75,7 +75,7 @@ public void lock( LockMode mode, @NonNull Transaction transaction ) throws Deadl
lock.lock();
try {
//noinspection DataFlowIssue // else we have a general problem
while ( waiters.peek().left != thread ) {
while ( waiters.peek().thread() != thread ) {
log.debug( "wait {} ", transaction.getXid() );
boolean successful = condition.await( RuntimeConfig.LOCKING_MAX_TIMEOUT_SECONDS.getInteger(), TimeUnit.SECONDS );

Expand Down Expand Up @@ -108,7 +108,7 @@ public void lock( LockMode mode, @NonNull Transaction transaction ) throws Deadl
} else if ( owners.contains( transaction.getXid() ) && (mode == LockMode.EXCLUSIVE)
&& owners.size() <= waiters.size()
// trx is owner and wants to upgrade, other transaction has the same -> deadlock
&& waiters.stream().filter( w -> w.right != transaction.getXid() ).anyMatch( w -> owners.contains( w.right ) && w.middle == LockMode.EXCLUSIVE ) ) {
&& waiters.stream().filter( w -> w.xid() != transaction.getXid() ).anyMatch( w -> owners.contains( w.xid() ) && w.mode() == LockMode.EXCLUSIVE ) ) {
cleanupWaiters( thread );
// we have to interrupt one transaction, all want to upgrade
throw new DeadlockException( "Write-write conflict with multiple transactions." );
Expand All @@ -135,7 +135,7 @@ public void lock( LockMode mode, @NonNull Transaction transaction ) throws Deadl

private void cleanupWaiters( Thread thread ) {
synchronized ( waiters ) {
List<Triple<Thread, LockMode, PolyXid>> remove = waiters.stream().filter( w -> w.left == thread ).toList();
List<LockInformation> remove = waiters.stream().filter( w -> w.thread() == thread ).toList();
if ( remove.isEmpty() ) {
return;
}
Expand All @@ -147,12 +147,12 @@ private void cleanupWaiters( Thread thread ) {

private void cleanupWaiters( PolyXid xid ) {
synchronized ( waiters ) {
List<Triple<Thread, LockMode, PolyXid>> remove = waiters.stream().filter( w -> w.right == xid ).toList();
List<LockInformation> remove = waiters.stream().filter( w -> w.xid() == xid ).toList();
if ( remove.isEmpty() ) {
return;
}
assert remove.size() == 1;
assert remove.get( 0 ).left == Thread.currentThread();
assert remove.get( 0 ).thread() == Thread.currentThread();
waiters.remove( remove.get( 0 ) );
}
}
Expand Down

0 comments on commit 7b4c449

Please sign in to comment.