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 ); IndexDescriptor indexDescriptor = new IndexDescriptor( labelId, propertyKeyId );
assertIndexOnline( state, indexDescriptor ); assertIndexOnline( state, indexDescriptor );
state.locks().explicit().acquireExclusive( INDEX_ENTRY, state.locks().pessimistic().acquireExclusive( INDEX_ENTRY,
indexEntryResourceId( labelId, propertyKeyId, ObjectUtil.toString( value ) ) ); indexEntryResourceId( labelId, propertyKeyId, ObjectUtil.toString( value ) ) );


long existing = entityReadOperations.nodeGetFromUniqueIndexSeek( state, indexDescriptor, 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. // 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 ); long indexEntryId = indexEntryResourceId( labelId, propertyKeyId, stringVal );


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


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


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


// Commit the transaction // Commit the transaction
commitProcess.commit( transactionRepresentation, lockGroup, commitEvent, INTERNAL ); commitProcess.commit( transactionRepresentation, lockGroup, commitEvent, INTERNAL );
Expand Down Expand Up @@ -1236,6 +1236,6 @@ public void registerCloseListener( CloseListener listener )
@Override @Override
public String toString() 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(); PrimitiveLongIterator iterator = nodeIds.iterator();
while ( iterator.hasNext() ) 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(); PrimitiveLongIterator iterator = nodeIds.iterator();
while ( iterator.hasNext() ) while ( iterator.hasNext() )
{ {
state.locks().implicit().releaseExclusive( ResourceTypes.NODE, iterator.next() ); state.locks().optimistic().releaseExclusive( ResourceTypes.NODE, iterator.next() );
} }
nodeIds.clear(); nodeIds.clear();
break; break;
Expand Down
Expand Up @@ -20,8 +20,8 @@
package org.neo4j.kernel.impl.locking; package org.neo4j.kernel.impl.locking;


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


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


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


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


/** /**
* Component used by {@link KernelStatement} to acquire {@link #implicit() implicit} and {@link #explicit() explicit} * Component used by {@link KernelStatement} to acquire {@link #pessimistic() pessimistic} and
* locks. * {@link #optimistic() optimistic} locks.
*/ */
public interface StatementLocks extends AutoCloseable public interface StatementLocks extends AutoCloseable
{ {
/** /**
* Get {@link Locks.Client} responsible for explicit locks. Such locks are explicitly grabbed by the user via * Get {@link Locks.Client} responsible for pessimistic locks. Such locks will be grabbed right away.
* {@link Transaction#acquireReadLock(PropertyContainer)} and
* {@link Transaction#acquireWriteLock(PropertyContainer)}.
* *
* @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 * Get {@link Locks.Client} responsible for optimistic locks. Such locks could potentially be grabbed later at
* provide consistency guarantees. * commit time.
* *
* @return the locks client to serve implicit locks. * @return the locks client to serve implicit locks.
*/ */
Locks.Client implicit(); Locks.Client optimistic();


/** /**
* Prepare the underlying {@link Locks.Client client}(s) for commit. * 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; 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}. * locks using {@link DeferringLockClient}.
*/ */
public class DeferringStatementLocks implements StatementLocks public class DeferringStatementLocks implements StatementLocks
Expand All @@ -38,13 +38,13 @@ public DeferringStatementLocks( Locks.Client explicit )
} }


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


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


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


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


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


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


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


Expand Down

0 comments on commit 0b26c70

Please sign in to comment.