Skip to content

Commit

Permalink
Mark timed out transaction as terminated.
Browse files Browse the repository at this point in the history
Mark transaction killed by TimeoutGuard as terminated.
Add new TransactionTimeOut status for that case.
  • Loading branch information
MishaDemianenko committed Sep 28, 2016
1 parent 912dbb8 commit bede360
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 44 deletions.
Expand Up @@ -144,6 +144,9 @@ enum Transaction implements Status
TransactionMarkedAsFailed( ClientError, TransactionMarkedAsFailed( ClientError,
"Transaction was marked as both successful and failed. Failure takes precedence and so this " + "Transaction was marked as both successful and failed. Failure takes precedence and so this " +
"transaction was rolled back although it may have looked like it was going to be committed" ), "transaction was rolled back although it may have looked like it was going to be committed" ),
TransactionTimedOut( ClientError,
"The transaction has not completed within the specified timeout. You may want to retry with a longer " +
"timeout." ),


// database errors // database errors
TransactionStartFailed( DatabaseError, TransactionStartFailed( DatabaseError,
Expand Down
Expand Up @@ -19,11 +19,14 @@
*/ */
package org.neo4j.kernel.guard; package org.neo4j.kernel.guard;


public class GuardException extends RuntimeException import org.neo4j.kernel.api.exceptions.Status;

public abstract class GuardException extends RuntimeException implements Status.HasStatus
{ {


protected GuardException( final String message ) protected GuardException( final String message )
{ {
super( message ); super( message );
} }

} }

This file was deleted.

Expand Up @@ -19,12 +19,14 @@
*/ */
package org.neo4j.kernel.guard; package org.neo4j.kernel.guard;


import org.neo4j.kernel.api.exceptions.Status;

public class GuardTimeoutException extends GuardException public class GuardTimeoutException extends GuardException
{ {


private final long overtime; private final long overtime;


public GuardTimeoutException(String message, final long overtime ) public GuardTimeoutException( String message, final long overtime )
{ {
super( message ); super( message );
this.overtime = overtime; this.overtime = overtime;
Expand All @@ -34,4 +36,10 @@ public long getOvertime()
{ {
return overtime; return overtime;
} }

@Override
public Status status()
{
return Status.Transaction.TransactionTimedOut;
}
} }
Expand Up @@ -21,6 +21,7 @@


import java.time.Clock; import java.time.Clock;


import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.impl.api.KernelStatement; import org.neo4j.kernel.impl.api.KernelStatement;
import org.neo4j.kernel.impl.api.KernelTransactionImplementation; import org.neo4j.kernel.impl.api.KernelTransactionImplementation;
import org.neo4j.logging.Log; import org.neo4j.logging.Log;
Expand All @@ -46,20 +47,22 @@ public void check( KernelStatement statement )
check( statement.getTransaction() ); check( statement.getTransaction() );
} }


private void check (KernelTransactionImplementation transaction) private void check( KernelTransactionImplementation transaction )
{ {
check( getMaxTransactionCompletionTime( transaction ), "Transaction timeout." ); check( transaction, "Transaction timeout." );
} }


private void check( long maxCompletionTime, String timeoutDescription ) private void check( KernelTransactionImplementation transaction, String timeoutDescription )
{ {
long now = clock.millis(); long now = clock.millis();
long maxCompletionTime = getMaxTransactionCompletionTime( transaction );
if ( maxCompletionTime < now ) if ( maxCompletionTime < now )
{ {
final long overtime = now - maxCompletionTime; long overtime = now - maxCompletionTime;
String message = timeoutDescription + " (Overtime: " + overtime + " ms)."; String message = timeoutDescription + " (Overtime: " + overtime + " ms).";
log.warn( message ); log.warn( message );
throw new GuardTimeoutException(message, overtime ); transaction.markForTermination( Status.Transaction.TransactionTimedOut );
throw new GuardTimeoutException( message, overtime );
} }
} }


Expand Down
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;


import org.neo4j.kernel.api.KernelTransaction; import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.api.security.AccessMode; import org.neo4j.kernel.api.security.AccessMode;
import org.neo4j.kernel.impl.api.KernelStatement; import org.neo4j.kernel.impl.api.KernelStatement;
import org.neo4j.kernel.impl.api.KernelTransactionImplementation; import org.neo4j.kernel.impl.api.KernelTransactionImplementation;
Expand All @@ -37,6 +38,8 @@
import org.neo4j.logging.Log; import org.neo4j.logging.Log;


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;


public class TimeoutGuardTest extends KernelTransactionTestBase public class TimeoutGuardTest extends KernelTransactionTestBase
Expand Down Expand Up @@ -64,6 +67,9 @@ public void detectTimedTransaction()


check( timeoutGuard, kernelStatement, overtime, message ); check( timeoutGuard, kernelStatement, overtime, message );


KernelTransactionImplementation transaction = kernelStatement.getTransaction();
assertSame( Status.Transaction.TransactionTimedOut, transaction.getReasonIfTerminated() );

logProvider.assertContainsMessageContaining( message ); logProvider.assertContainsMessageContaining( message );
} }


Expand All @@ -80,6 +86,9 @@ public void allowToProceedWhenTransactionTimeoutNotReached()


timeoutGuard.check( kernelStatement ); timeoutGuard.check( kernelStatement );


KernelTransactionImplementation transaction = kernelStatement.getTransaction();
assertNull( transaction.getReasonIfTerminated() );

logProvider.assertNoLoggingOccurred(); logProvider.assertNoLoggingOccurred();
} }


Expand Down

0 comments on commit bede360

Please sign in to comment.