Skip to content

Commit

Permalink
Preparation for resourceIds...
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint authored and lutovich committed Jul 21, 2016
1 parent 8722fd3 commit 2574153
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 150 deletions.
Expand Up @@ -89,9 +89,12 @@ public DeferringLockClient( Client clientDelegate )
} }


@Override @Override
public void acquireShared( ResourceType resourceType, long resourceId ) throws AcquireLockTimeoutException public void acquireShared( ResourceType resourceType, long... resourceIds ) throws AcquireLockTimeoutException
{ {
queueLock( resourceType, resourceId, shared ); for ( long resourceId : resourceIds )
{
queueLock( resourceType, resourceId, shared );
}
} }


private boolean queueLock( ResourceType resourceType, long resourceId, Set<Resource> lockSet ) private boolean queueLock( ResourceType resourceType, long resourceId, Set<Resource> lockSet )
Expand All @@ -105,9 +108,12 @@ private boolean queueLock( ResourceType resourceType, long resourceId, Set<Resou
} }


@Override @Override
public void acquireExclusive( ResourceType resourceType, long resourceId ) throws AcquireLockTimeoutException public void acquireExclusive( ResourceType resourceType, long... resourceIds ) throws AcquireLockTimeoutException
{ {
queueLock( resourceType, resourceId, exclusive ); for ( long resourceId : resourceIds )
{
queueLock( resourceType, resourceId, exclusive );
}
} }


@Override @Override
Expand Down
Expand Up @@ -78,14 +78,14 @@ interface Client extends AutoCloseable
* Can be grabbed when there are no locks or only share locks on a resource. If the lock cannot be acquired, * Can be grabbed when there are no locks or only share locks on a resource. If the lock cannot be acquired,
* behavior is specified by the {@link WaitStrategy} for the given {@link ResourceType}. * behavior is specified by the {@link WaitStrategy} for the given {@link ResourceType}.
*/ */
void acquireShared( ResourceType resourceType, long resourceId ) throws AcquireLockTimeoutException; void acquireShared( ResourceType resourceType, long... resourceIds ) throws AcquireLockTimeoutException;


/** /**
* Can be grabbed when no other client holds locks on the relevant resources. No other clients can hold locks * Can be grabbed when no other client holds locks on the relevant resources. No other clients can hold locks
* while one client holds an exclusive lock. If the lock cannot be acquired, * while one client holds an exclusive lock. If the lock cannot be acquired,
* behavior is specified by the {@link WaitStrategy} for the given {@link ResourceType}. * behavior is specified by the {@link WaitStrategy} for the given {@link ResourceType}.
*/ */
void acquireExclusive( ResourceType resourceType, long resourceId ) throws AcquireLockTimeoutException; void acquireExclusive( ResourceType resourceType, long... resourceIds ) throws AcquireLockTimeoutException;


/** Try grabbing exclusive lock, not waiting and returning a boolean indicating if we got the lock. */ /** Try grabbing exclusive lock, not waiting and returning a boolean indicating if we got the lock. */
boolean tryExclusiveLock( ResourceType resourceType, long resourceId ); boolean tryExclusiveLock( ResourceType resourceType, long resourceId );
Expand Down
Expand Up @@ -24,12 +24,12 @@ public class NoOpClient implements Locks.Client
public static final Locks.Client NO_LOCKS = new NoOpClient(); public static final Locks.Client NO_LOCKS = new NoOpClient();


@Override @Override
public void acquireShared( Locks.ResourceType resourceType, long resourceId ) throws AcquireLockTimeoutException public void acquireShared( Locks.ResourceType resourceType, long... resourceIds ) throws AcquireLockTimeoutException
{ {
} }


@Override @Override
public void acquireExclusive( Locks.ResourceType resourceType, long resourceId ) throws AcquireLockTimeoutException public void acquireExclusive( Locks.ResourceType resourceType, long... resourceIds ) throws AcquireLockTimeoutException
{ {
} }


Expand Down
Expand Up @@ -60,27 +60,30 @@ public CommunityLockClient( LockManagerImpl manager )
} }


@Override @Override
public void acquireShared( Locks.ResourceType resourceType, long resourceId ) public void acquireShared( Locks.ResourceType resourceType, long... resourceIds )
{ {
stateHolder.incrementActiveClients( this ); stateHolder.incrementActiveClients( this );
try try
{ {
PrimitiveLongObjectMap<LockResource> localLocks = localShared( resourceType ); PrimitiveLongObjectMap<LockResource> localLocks = localShared( resourceType );
LockResource resource = localLocks.get( resourceId ); for ( long resourceId : resourceIds )
if ( resource != null )
{
resource.acquireReference();
}
else
{ {
resource = new LockResource( resourceType, resourceId ); LockResource resource = localLocks.get( resourceId );
if ( manager.getReadLock( resource, lockTransaction ) ) if ( resource != null )
{ {
localLocks.put( resourceId, resource ); resource.acquireReference();
} }
else else
{ {
throw new LockClientStoppedException( this ); resource = new LockResource( resourceType, resourceId );
if ( manager.getReadLock( resource, lockTransaction ) )
{
localLocks.put( resourceId, resource );
}
else
{
throw new LockClientStoppedException( this );
}
} }
} }
} }
Expand All @@ -93,27 +96,30 @@ public void acquireShared( Locks.ResourceType resourceType, long resourceId )




@Override @Override
public void acquireExclusive( Locks.ResourceType resourceType, long resourceId ) public void acquireExclusive( Locks.ResourceType resourceType, long... resourceIds )
{ {
stateHolder.incrementActiveClients( this ); stateHolder.incrementActiveClients( this );
try try
{ {
PrimitiveLongObjectMap<LockResource> localLocks = localExclusive( resourceType ); PrimitiveLongObjectMap<LockResource> localLocks = localExclusive( resourceType );
LockResource resource = localLocks.get( resourceId ); for ( long resourceId : resourceIds )
if ( resource != null )
{
resource.acquireReference();
}
else
{ {
resource = new LockResource( resourceType, resourceId ); LockResource resource = localLocks.get( resourceId );
if ( manager.getWriteLock( resource, lockTransaction ) ) if ( resource != null )
{ {
localLocks.put( resourceId, resource ); resource.acquireReference();
} }
else else
{ {
throw new LockClientStoppedException( this ); resource = new LockResource( resourceType, resourceId );
if ( manager.getWriteLock( resource, lockTransaction ) )
{
localLocks.put( resourceId, resource );
}
else
{
throw new LockClientStoppedException( this );
}
} }
} }
} }
Expand Down
Expand Up @@ -115,11 +115,14 @@ public Tracker( NeoStores neoStores )
} }


@Override @Override
public void acquireExclusive( Locks.ResourceType resourceType, long resourceId ) public void acquireExclusive( Locks.ResourceType resourceType, long... resourceIds )
throws AcquireLockTimeoutException throws AcquireLockTimeoutException
{ {
assertEquals( ResourceTypes.RELATIONSHIP, resourceType ); assertEquals( ResourceTypes.RELATIONSHIP, resourceType );
relationshipLocksAcquired.add( resourceId ); for ( long resourceId : resourceIds )
{
relationshipLocksAcquired.add( resourceId );
}
} }


protected void changingRelationship( long relId ) protected void changingRelationship( long relId )
Expand Down
Expand Up @@ -100,54 +100,60 @@ private Map<Long, AtomicInteger> getLockMap(
} }


@Override @Override
public void acquireShared( Locks.ResourceType resourceType, long resourceId ) throws AcquireLockTimeoutException public void acquireShared( Locks.ResourceType resourceType, long... resourceIds ) throws AcquireLockTimeoutException
{ {
assertNotStopped(); assertNotStopped();


Map<Long, AtomicInteger> lockMap = getLockMap( sharedLocks, resourceType ); Map<Long, AtomicInteger> lockMap = getLockMap( sharedLocks, resourceType );
AtomicInteger preExistingLock = lockMap.get( resourceId ); for ( long resourceId : resourceIds )
if ( preExistingLock != null )
{ {
// We already hold this lock, just increment the local reference count AtomicInteger preExistingLock = lockMap.get( resourceId );
preExistingLock.incrementAndGet(); if ( preExistingLock != null )
}
else if ( getReadLockOnMaster( resourceType, resourceId ) )
{
if ( client.trySharedLock( resourceType, resourceId ) )
{ {
lockMap.put( resourceId, new AtomicInteger( 1 ) ); // We already hold this lock, just increment the local reference count
preExistingLock.incrementAndGet();
} }
else else if ( getReadLockOnMaster( resourceType, resourceId ) )
{ {
throw new LocalDeadlockDetectedException( client, localLockManager, resourceType, resourceId, READ ); if ( client.trySharedLock( resourceType, resourceId ) )
{
lockMap.put( resourceId, new AtomicInteger( 1 ) );
}
else
{
throw new LocalDeadlockDetectedException( client, localLockManager, resourceType, resourceId, READ );


}
} }
} }
} }


@Override @Override
public void acquireExclusive( Locks.ResourceType resourceType, long resourceId ) throws public void acquireExclusive( Locks.ResourceType resourceType, long... resourceIds ) throws
AcquireLockTimeoutException AcquireLockTimeoutException
{ {
assertNotStopped(); assertNotStopped();


Map<Long, AtomicInteger> lockMap = getLockMap( exclusiveLocks, resourceType ); Map<Long, AtomicInteger> lockMap = getLockMap( exclusiveLocks, resourceType );


AtomicInteger preExistingLock = lockMap.get( resourceId ); for ( long resourceId : resourceIds )
if ( preExistingLock != null )
{ {
// We already hold this lock, just increment the local reference count AtomicInteger preExistingLock = lockMap.get( resourceId );
preExistingLock.incrementAndGet(); if ( preExistingLock != null )
}
else if ( acquireExclusiveOnMaster( resourceType, resourceId ) )
{
if ( client.tryExclusiveLock( resourceType, resourceId ) )
{ {
lockMap.put( resourceId, new AtomicInteger( 1 ) ); // We already hold this lock, just increment the local reference count
preExistingLock.incrementAndGet();
} }
else else if ( acquireExclusiveOnMaster( resourceType, resourceId ) )
{ {
throw new LocalDeadlockDetectedException( client, localLockManager, resourceType, resourceId, WRITE ); if ( client.tryExclusiveLock( resourceType, resourceId ) )
{
lockMap.put( resourceId, new AtomicInteger( 1 ) );
}
else
{
throw new LocalDeadlockDetectedException( client, localLockManager, resourceType, resourceId, WRITE );
}
} }
} }
} }
Expand Down

0 comments on commit 2574153

Please sign in to comment.