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
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 )
Expand All @@ -105,9 +108,12 @@ private boolean queueLock( ResourceType resourceType, long resourceId, Set<Resou
}

@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
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,
* 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
* 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}.
*/
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. */
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();

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

@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
public void acquireShared( Locks.ResourceType resourceType, long resourceId )
public void acquireShared( Locks.ResourceType resourceType, long... resourceIds )
{
stateHolder.incrementActiveClients( this );
try
{
PrimitiveLongObjectMap<LockResource> localLocks = localShared( resourceType );
LockResource resource = localLocks.get( resourceId );
if ( resource != null )
{
resource.acquireReference();
}
else
for ( long resourceId : resourceIds )
{
resource = new LockResource( resourceType, resourceId );
if ( manager.getReadLock( resource, lockTransaction ) )
LockResource resource = localLocks.get( resourceId );
if ( resource != null )
{
localLocks.put( resourceId, resource );
resource.acquireReference();
}
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
public void acquireExclusive( Locks.ResourceType resourceType, long resourceId )
public void acquireExclusive( Locks.ResourceType resourceType, long... resourceIds )
{
stateHolder.incrementActiveClients( this );
try
{
PrimitiveLongObjectMap<LockResource> localLocks = localExclusive( resourceType );
LockResource resource = localLocks.get( resourceId );
if ( resource != null )
{
resource.acquireReference();
}
else
for ( long resourceId : resourceIds )
{
resource = new LockResource( resourceType, resourceId );
if ( manager.getWriteLock( resource, lockTransaction ) )
LockResource resource = localLocks.get( resourceId );
if ( resource != null )
{
localLocks.put( resourceId, resource );
resource.acquireReference();
}
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
public void acquireExclusive( Locks.ResourceType resourceType, long resourceId )
public void acquireExclusive( Locks.ResourceType resourceType, long... resourceIds )
throws AcquireLockTimeoutException
{
assertEquals( ResourceTypes.RELATIONSHIP, resourceType );
relationshipLocksAcquired.add( resourceId );
for ( long resourceId : resourceIds )
{
relationshipLocksAcquired.add( resourceId );
}
}

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

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

Map<Long, AtomicInteger> lockMap = getLockMap( sharedLocks, resourceType );
AtomicInteger preExistingLock = lockMap.get( resourceId );
if ( preExistingLock != null )
for ( long resourceId : resourceIds )
{
// We already hold this lock, just increment the local reference count
preExistingLock.incrementAndGet();
}
else if ( getReadLockOnMaster( resourceType, resourceId ) )
{
if ( client.trySharedLock( resourceType, resourceId ) )
AtomicInteger preExistingLock = lockMap.get( resourceId );
if ( preExistingLock != null )
{
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
public void acquireExclusive( Locks.ResourceType resourceType, long resourceId ) throws
public void acquireExclusive( Locks.ResourceType resourceType, long... resourceIds ) throws
AcquireLockTimeoutException
{
assertNotStopped();

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

AtomicInteger preExistingLock = lockMap.get( resourceId );
if ( preExistingLock != null )
for ( long resourceId : resourceIds )
{
// We already hold this lock, just increment the local reference count
preExistingLock.incrementAndGet();
}
else if ( acquireExclusiveOnMaster( resourceType, resourceId ) )
{
if ( client.tryExclusiveLock( resourceType, resourceId ) )
AtomicInteger preExistingLock = lockMap.get( resourceId );
if ( preExistingLock != null )
{
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.