Skip to content

Commit

Permalink
More descriptive naming for StatementLocks
Browse files Browse the repository at this point in the history
explicit -> pessimistic because they are grabbed immediately
implicit -> optimistic because they can be deferred
  • Loading branch information
burqen authored and lutovich committed Jul 21, 2016
1 parent cbe5df7 commit 0b26c70
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 80 deletions.
Expand Up @@ -156,7 +156,7 @@ private void validateNoExistingNodeWithLabelAndProperty( KernelStatement state,
{
IndexDescriptor indexDescriptor = new IndexDescriptor( labelId, propertyKeyId );
assertIndexOnline( state, indexDescriptor );
state.locks().explicit().acquireExclusive( INDEX_ENTRY,
state.locks().pessimistic().acquireExclusive( INDEX_ENTRY,
indexEntryResourceId( labelId, propertyKeyId, ObjectUtil.toString( value ) ) );

long existing = entityReadOperations.nodeGetFromUniqueIndexSeek( state, indexDescriptor, value );
Expand Down Expand Up @@ -328,7 +328,7 @@ public long nodeGetFromUniqueIndexSeek(
}

// If we find the node - hold a shared lock. If we don't find a node - hold an exclusive lock.
Locks.Client locks = state.locks().explicit();
Locks.Client locks = state.locks().pessimistic();
long indexEntryId = indexEntryResourceId( labelId, propertyKeyId, stringVal );

locks.acquireShared( INDEX_ENTRY, indexEntryId );
Expand Down
Expand Up @@ -602,7 +602,7 @@ private void commit() throws TransactionFailureException
}

statementLocks.prepareForCommit();
context.init( statementLocks.explicit() );
context.init( statementLocks.pessimistic() );
prepareRecordChangesFromTransactionState();
}

Expand Down Expand Up @@ -636,7 +636,7 @@ private void commit() throws TransactionFailureException
headerInformation.getMasterId(),
headerInformation.getAuthorId(),
startTimeMillis, lastTransactionIdWhenStarted, clock.currentTimeMillis(),
statementLocks.explicit().getLockSessionId() );
statementLocks.pessimistic().getLockSessionId() );

// Commit the transaction
commitProcess.commit( transactionRepresentation, lockGroup, commitEvent, INTERNAL );
Expand Down Expand Up @@ -1236,6 +1236,6 @@ public void registerCloseListener( CloseListener listener )
@Override
public String toString()
{
return "KernelTransaction[" + this.statementLocks.explicit().getLockSessionId() + "]";
return "KernelTransaction[" + this.statementLocks.pessimistic().getLockSessionId() + "]";
}
}

Large diffs are not rendered by default.

Expand Up @@ -105,7 +105,7 @@ void lockAllNodesAndConsumeRelationships( long nodeId, final KernelStatement sta
PrimitiveLongIterator iterator = nodeIds.iterator();
while ( iterator.hasNext() )
{
state.locks().implicit().acquireExclusive( ResourceTypes.NODE, iterator.next() );
state.locks().optimistic().acquireExclusive( ResourceTypes.NODE, iterator.next() );
}
}

Expand All @@ -121,7 +121,7 @@ void lockAllNodesAndConsumeRelationships( long nodeId, final KernelStatement sta
PrimitiveLongIterator iterator = nodeIds.iterator();
while ( iterator.hasNext() )
{
state.locks().implicit().releaseExclusive( ResourceTypes.NODE, iterator.next() );
state.locks().optimistic().releaseExclusive( ResourceTypes.NODE, iterator.next() );
}
nodeIds.clear();
break;
Expand Down
Expand Up @@ -20,8 +20,8 @@
package org.neo4j.kernel.impl.locking;

/**
* A {@link StatementLocks} implementation that uses given {@link Locks.Client} for both {@link #implicit() implicit}
* and {@link #explicit() explicit} locks.
* A {@link StatementLocks} implementation that uses given {@link Locks.Client} for both
* {@link #optimistic() optimistic} and {@link #pessimistic() pessimistic} locks.
*/
public class SimpleStatementLocks implements StatementLocks
{
Expand All @@ -33,13 +33,13 @@ public SimpleStatementLocks( Locks.Client client )
}

@Override
public Locks.Client explicit()
public Locks.Client pessimistic()
{
return client;
}

@Override
public Locks.Client implicit()
public Locks.Client optimistic()
{
return client;
}
Expand Down
Expand Up @@ -19,32 +19,28 @@
*/
package org.neo4j.kernel.impl.locking;

import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.impl.api.KernelStatement;

/**
* Component used by {@link KernelStatement} to acquire {@link #implicit() implicit} and {@link #explicit() explicit}
* locks.
* Component used by {@link KernelStatement} to acquire {@link #pessimistic() pessimistic} and
* {@link #optimistic() optimistic} locks.
*/
public interface StatementLocks extends AutoCloseable
{
/**
* Get {@link Locks.Client} responsible for explicit locks. Such locks are explicitly grabbed by the user via
* {@link Transaction#acquireReadLock(PropertyContainer)} and
* {@link Transaction#acquireWriteLock(PropertyContainer)}.
* Get {@link Locks.Client} responsible for pessimistic locks. Such locks will be grabbed right away.
*
* @return the locks client to serve explicit locks.
* @return the locks client to serve pessimistic locks.
*/
Locks.Client explicit();
Locks.Client pessimistic();

/**
* Get {@link Locks.Client} responsible for implicit locks. Such locks are grabbed by the database itself to
* provide consistency guarantees.
* Get {@link Locks.Client} responsible for optimistic locks. Such locks could potentially be grabbed later at
* commit time.
*
* @return the locks client to serve implicit locks.
*/
Locks.Client implicit();
Locks.Client optimistic();

/**
* Prepare the underlying {@link Locks.Client client}(s) for commit.
Expand Down
Expand Up @@ -23,7 +23,7 @@
import org.neo4j.kernel.impl.locking.StatementLocks;

/**
* A {@link StatementLocks} implementation that defers {@link #implicit() implicit}
* A {@link StatementLocks} implementation that defers {@link #optimistic() optimistic}
* locks using {@link DeferringLockClient}.
*/
public class DeferringStatementLocks implements StatementLocks
Expand All @@ -38,13 +38,13 @@ public DeferringStatementLocks( Locks.Client explicit )
}

@Override
public Locks.Client explicit()
public Locks.Client pessimistic()
{
return explicit;
}

@Override
public Locks.Client implicit()
public Locks.Client optimistic()
{
return implicit;
}
Expand Down
Expand Up @@ -36,8 +36,8 @@ public void shouldUseSameClientForImplicitAndExplicit() throws Exception
final SimpleStatementLocks statementLocks = new SimpleStatementLocks( client );

// THEN
assertSame( client, statementLocks.explicit() );
assertSame( client, statementLocks.implicit() );
assertSame( client, statementLocks.pessimistic() );
assertSame( client, statementLocks.optimistic() );
}

@Test
Expand Down
Expand Up @@ -44,8 +44,8 @@ public void shouldUseCorrectClientForImplicitAndExplicit() throws Exception
final DeferringStatementLocks statementLocks = new DeferringStatementLocks( client );

// THEN
assertSame( client, statementLocks.explicit() );
assertThat( statementLocks.implicit(), instanceOf( DeferringLockClient.class ) );
assertSame( client, statementLocks.pessimistic() );
assertThat( statementLocks.optimistic(), instanceOf( DeferringLockClient.class ) );
}

@Test
Expand All @@ -70,8 +70,8 @@ public void shouldPrepareExplicitForCommitWhenLocksAcquire() throws Exception
final DeferringStatementLocks statementLocks = new DeferringStatementLocks( client );

// WHEN
statementLocks.implicit().acquireExclusive( ResourceTypes.NODE, 1 );
statementLocks.implicit().acquireExclusive( ResourceTypes.RELATIONSHIP, 42 );
statementLocks.optimistic().acquireExclusive( ResourceTypes.NODE, 1 );
statementLocks.optimistic().acquireExclusive( ResourceTypes.RELATIONSHIP, 42 );
verify( client, never() ).acquireExclusive( any( Locks.ResourceType.class ), anyLong() );
statementLocks.prepareForCommit();

Expand Down

0 comments on commit 0b26c70

Please sign in to comment.