Skip to content

Commit

Permalink
Remove volatile terminated flag to only use volatile termination reason
Browse files Browse the repository at this point in the history
  • Loading branch information
burqen committed Jun 30, 2016
1 parent f0e6805 commit 4df6637
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 29 deletions.
Expand Up @@ -113,16 +113,16 @@ interface CloseListener
boolean isOpen(); boolean isOpen();


/** /**
* @return {@code true} if {@link #markForTermination(Status)} has been invoked, otherwise {@code false}. * @return {@link Status} if {@link #markForTermination(Status)} has been invoked, otherwise {@code null}.
*/ */
Status shouldBeTerminated(); Status terminationReason();


/** /**
* Marks this transaction for termination, such that it cannot commit successfully and will try to be * Marks this transaction for termination, such that it cannot commit successfully and will try to be
* terminated by having other methods throw a specific termination exception, as to sooner reach the assumed * terminated by having other methods throw a specific termination exception, as to sooner reach the assumed
* point where {@link #close()} will be invoked. * point where {@link #close()} will be invoked.
*/ */
void markForTermination( Status typeOfReason ); void markForTermination( Status reason );


/** /**
* Register a {@link CloseListener} to be invoked after commit, but before transaction events "after" hooks * Register a {@link CloseListener} to be invoked after commit, but before transaction events "after" hooks
Expand Down
Expand Up @@ -128,7 +128,7 @@ void assertOpen()
throw new NotInTransactionException( "The statement has been closed." ); throw new NotInTransactionException( "The statement has been closed." );
} }


Status terminationReason = transaction.shouldBeTerminated(); Status terminationReason = transaction.terminationReason();
if ( terminationReason != null ) if ( terminationReason != null )
{ {
throw new TransactionTerminatedException( terminationReason ); throw new TransactionTerminatedException( terminationReason );
Expand Down
Expand Up @@ -172,8 +172,7 @@ TransactionType upgradeToSchemaTransaction() throws InvalidTransactionTypeKernel
private StoreStatement storeStatement; private StoreStatement storeStatement;
private boolean closing, closed; private boolean closing, closed;
private boolean failure, success; private boolean failure, success;
private volatile boolean terminated; private volatile Status terminationReason;
private Status terminationReason;
// Some header information // Some header information
private long startTimeMillis; private long startTimeMillis;
private long lastTransactionIdWhenStarted; private long lastTransactionIdWhenStarted;
Expand Down Expand Up @@ -255,7 +254,7 @@ public KernelTransactionImplementation initialize( long lastCommittedTx, long la
{ {
this.locks = locksManager.newClient(); this.locks = locksManager.newClient();
this.terminationReason = null; this.terminationReason = null;
this.closing = closed = failure = success = terminated = false; this.closing = closed = failure = success = false;
this.transactionType = TransactionType.ANY; this.transactionType = TransactionType.ANY;
this.beforeHookInvoked = false; this.beforeHookInvoked = false;
this.recordState.initialize( lastCommittedTx ); this.recordState.initialize( lastCommittedTx );
Expand Down Expand Up @@ -287,9 +286,9 @@ public void failure()
} }


@Override @Override
public Status shouldBeTerminated() public Status terminationReason()
{ {
return terminated ? terminationReason : null; return terminationReason;
} }


/** /**
Expand Down Expand Up @@ -317,7 +316,6 @@ public void markForTermination( Status reason )
if ( stillSameTransaction && canBeTerminated() ) if ( stillSameTransaction && canBeTerminated() )
{ {
failure = true; failure = true;
terminated = true;
terminationReason = reason; terminationReason = reason;
if ( txTerminationAwareLocks && locks != null ) if ( txTerminationAwareLocks && locks != null )
{ {
Expand Down Expand Up @@ -490,7 +488,7 @@ public void close() throws TransactionFailureException
closing = true; closing = true;
try try
{ {
if ( failure || !success || terminated ) if ( failure || !success || isTerminated() )
{ {
rollback(); rollback();
failOnNonExplicitRollbackIfNeeded( ); failOnNonExplicitRollbackIfNeeded( );
Expand Down Expand Up @@ -542,7 +540,7 @@ public void close() throws TransactionFailureException
*/ */
private void failOnNonExplicitRollbackIfNeeded() throws TransactionFailureException private void failOnNonExplicitRollbackIfNeeded() throws TransactionFailureException
{ {
if ( success && terminated ) if ( success && isTerminated() )
{ {
throw new TransactionTerminatedException( terminationReason ); throw new TransactionTerminatedException( terminationReason );
} }
Expand Down Expand Up @@ -754,7 +752,6 @@ private void release()
{ {
locks.close(); locks.close();
locks = null; locks = null;
terminated = false;
terminationReason = null; terminationReason = null;
pool.release( this ); pool.release( this );
if ( storeStatement != null ) if ( storeStatement != null )
Expand All @@ -776,7 +773,12 @@ private void release()
*/ */
private boolean canBeTerminated() private boolean canBeTerminated()
{ {
return !closed && !terminated; return !closed && !isTerminated();
}

private boolean isTerminated()
{
return terminationReason != null;
} }


public long lastTransactionTimestampWhenStarted() public long lastTransactionTimestampWhenStarted()
Expand Down
Expand Up @@ -71,7 +71,7 @@ private void assertInUnterminatedTransaction( TopLevelTransaction transaction )
{ {
throw new NotInTransactionException(); throw new NotInTransactionException();
} }
Status terminationReason = transaction.getTransaction().shouldBeTerminated(); Status terminationReason = transaction.getTransaction().terminationReason();
if ( terminationReason != null ) if ( terminationReason != null )
{ {
throw new TransactionTerminatedException( terminationReason ); throw new TransactionTerminatedException( terminationReason );
Expand Down
Expand Up @@ -70,7 +70,7 @@ public void shouldCloseOpenedLabelScanReader() throws Exception
public void shouldThrowTerminateExceptionWhenTransactionTerminated() throws Exception public void shouldThrowTerminateExceptionWhenTransactionTerminated() throws Exception
{ {
KernelTransactionImplementation transaction = mock( KernelTransactionImplementation.class ); KernelTransactionImplementation transaction = mock( KernelTransactionImplementation.class );
when( transaction.shouldBeTerminated() ).thenReturn( Status.General.UnknownFailure ); when( transaction.terminationReason() ).thenReturn( Status.General.UnknownFailure );


KernelStatement statement = new KernelStatement( KernelStatement statement = new KernelStatement(
transaction, mock( IndexReaderFactory.class ), transaction, mock( IndexReaderFactory.class ),
Expand Down
Expand Up @@ -59,6 +59,7 @@
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
Expand Down Expand Up @@ -176,7 +177,7 @@ public void shouldRollbackOnClosingSuccessfulButTerminatedTransaction() throws E
{ {
// WHEN // WHEN
transaction.markForTermination( Status.General.UnknownFailure ); transaction.markForTermination( Status.General.UnknownFailure );
assertEquals( Status.General.UnknownFailure, transaction.shouldBeTerminated() ); assertEquals( Status.General.UnknownFailure, transaction.terminationReason() );
} }


// THEN // THEN
Expand All @@ -193,7 +194,7 @@ public void shouldRollbackOnClosingTerminatedButSuccessfulTransaction() throws E
transaction.markForTermination( Status.General.UnknownFailure ); transaction.markForTermination( Status.General.UnknownFailure );
transaction.success(); transaction.success();


assertEquals( Status.General.UnknownFailure, transaction.shouldBeTerminated() ); assertEquals( Status.General.UnknownFailure, transaction.terminationReason() );


try try
{ {
Expand All @@ -219,7 +220,7 @@ public void shouldNotDowngradeFailureState() throws Exception
// WHEN // WHEN
transaction.markForTermination( Status.General.UnknownFailure ); transaction.markForTermination( Status.General.UnknownFailure );
transaction.failure(); transaction.failure();
assertEquals( Status.General.UnknownFailure, transaction.shouldBeTerminated() ); assertEquals( Status.General.UnknownFailure, transaction.terminationReason() );
} }


// THEN // THEN
Expand Down Expand Up @@ -433,7 +434,7 @@ public void markForTerminationNotInitializedTransaction()


transaction.markForTermination( Status.General.UnknownFailure ); transaction.markForTermination( Status.General.UnknownFailure );


assertEquals( Status.General.UnknownFailure, transaction.shouldBeTerminated() ); assertEquals( Status.General.UnknownFailure, transaction.terminationReason() );
} }


@Test @Test
Expand All @@ -447,7 +448,7 @@ public void markForTerminationInitializedTransaction()


transaction.markForTermination( Status.General.UnknownFailure ); transaction.markForTermination( Status.General.UnknownFailure );


assertEquals( Status.General.UnknownFailure, transaction.shouldBeTerminated() ); assertEquals( Status.General.UnknownFailure, transaction.terminationReason() );
verify( client ).stop(); verify( client ).stop();
} }


Expand All @@ -464,7 +465,7 @@ public void markForTerminationTerminatedTransaction()
transaction.markForTermination( Status.General.UnknownFailure ); transaction.markForTermination( Status.General.UnknownFailure );
transaction.markForTermination( Status.General.UnknownFailure ); transaction.markForTermination( Status.General.UnknownFailure );


assertEquals( Status.General.UnknownFailure, transaction.shouldBeTerminated() ); assertEquals( Status.General.UnknownFailure, transaction.terminationReason() );
verify( client ).stop(); verify( client ).stop();
verify( transactionMonitor ).transactionTerminated(); verify( transactionMonitor ).transactionTerminated();
} }
Expand Down Expand Up @@ -567,6 +568,31 @@ public void txMarkedForBothSuccessAndFailureThrowsOnClose()
} }
} }


@Test
public void initializedTransactionShouldHaveNoTerminationReason() throws Exception
{
KernelTransactionImplementation tx = newInitializedTransaction();
assertNull( tx.terminationReason() );
}

@Test
public void shouldReportCorrectTerminationReason() throws Exception
{
Status status = Status.Transaction.Terminated;
KernelTransactionImplementation tx = newInitializedTransaction();
tx.markForTermination( status );
assertSame( status, tx.terminationReason() );
}

@Test
public void closedTransactionShouldHaveNoTerminationReason() throws Exception
{
KernelTransactionImplementation tx = newInitializedTransaction();
tx.markForTermination( Status.Transaction.Terminated );
tx.close();
assertNull( tx.terminationReason() );
}

private final NeoStores neoStores = mock( NeoStores.class ); private final NeoStores neoStores = mock( NeoStores.class );
private final MetaDataStore metaDataStore = mock( MetaDataStore.class ); private final MetaDataStore metaDataStore = mock( MetaDataStore.class );
private final TransactionHooks hooks = new TransactionHooks(); private final TransactionHooks hooks = new TransactionHooks();
Expand Down
Expand Up @@ -84,7 +84,7 @@ public void accept( TestKernelTransaction tx )
public void accept( TestKernelTransaction tx ) public void accept( TestKernelTransaction tx )
{ {
close( tx ); close( tx );
assertNull( tx.shouldBeTerminated() ); assertNull( tx.terminationReason() );
tx.initialize(); tx.initialize();
} }
} }
Expand Down Expand Up @@ -422,13 +422,13 @@ void assertRolledBack()


void assertTerminated() void assertTerminated()
{ {
assertEquals( Status.Transaction.MarkedAsFailed, shouldBeTerminated() ); assertEquals( Status.Transaction.MarkedAsFailed, terminationReason() );
assertTrue( monitor.terminated ); assertTrue( monitor.terminated );
} }


void assertNotTerminated() void assertNotTerminated()
{ {
assertNull( shouldBeTerminated() ); assertNull( terminationReason() );
assertFalse( monitor.terminated ); assertFalse( monitor.terminated );
} }
} }
Expand Down
Expand Up @@ -112,7 +112,7 @@ public void shouldDisposeTransactionsWhenAsked() throws Exception
assertThat( postDispose, not( equalTo( first ) ) ); assertThat( postDispose, not( equalTo( first ) ) );
assertThat( postDispose, not( equalTo( second ) ) ); assertThat( postDispose, not( equalTo( second ) ) );


assertTrue( leftOpen.shouldBeTerminated() != null ); assertTrue( leftOpen.terminationReason() != null );
} }


@Test @Test
Expand Down
Expand Up @@ -79,7 +79,7 @@ public LockingStatementOperationsTest()
entityReadOps, entityWriteOps, schemaReadOps, schemaWriteOps, schemaStateOps entityReadOps, entityWriteOps, schemaReadOps, schemaWriteOps, schemaStateOps
); );


when( transaction.shouldBeTerminated() ).thenReturn( null ); when( transaction.terminationReason() ).thenReturn( null );
} }


@Test @Test
Expand Down
Expand Up @@ -50,7 +50,6 @@
import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;

import static org.neo4j.kernel.impl.api.StatementOperationsTestHelper.mockedParts; import static org.neo4j.kernel.impl.api.StatementOperationsTestHelper.mockedParts;
import static org.neo4j.kernel.impl.api.StatementOperationsTestHelper.mockedState; import static org.neo4j.kernel.impl.api.StatementOperationsTestHelper.mockedState;
import static org.neo4j.kernel.impl.store.SchemaStorage.IndexRuleKind.CONSTRAINT; import static org.neo4j.kernel.impl.store.SchemaStorage.IndexRuleKind.CONSTRAINT;
Expand Down Expand Up @@ -199,7 +198,7 @@ public boolean isOpen()
} }


@Override @Override
public Status shouldBeTerminated() public Status terminationReason()
{ {
return null; return null;
} }
Expand Down

0 comments on commit 4df6637

Please sign in to comment.