diff --git a/community/common/src/main/java/org/neo4j/kernel/api/exceptions/Status.java b/community/common/src/main/java/org/neo4j/kernel/api/exceptions/Status.java index b79c9d993046..bced28f26873 100644 --- a/community/common/src/main/java/org/neo4j/kernel/api/exceptions/Status.java +++ b/community/common/src/main/java/org/neo4j/kernel/api/exceptions/Status.java @@ -185,7 +185,9 @@ enum Transaction implements Status LockClientStopped( TransientError, "Transaction terminated, no more locks can be acquired." ), Terminated( TransientError, - "Explicitly terminated by the user." ); + "Explicitly terminated by the user." ), + Interrupted( TransientError, + "Interrupted while waiting." ); private final Code code; diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/util/concurrent/LockWaitStrategies.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/util/concurrent/LockWaitStrategies.java index 3c9d6264491c..55fc71953260 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/util/concurrent/LockWaitStrategies.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/util/concurrent/LockWaitStrategies.java @@ -22,6 +22,8 @@ import org.neo4j.storageengine.api.lock.AcquireLockTimeoutException; import org.neo4j.storageengine.api.lock.WaitStrategy; +import static org.neo4j.kernel.api.exceptions.Status.Transaction.Interrupted; + public enum LockWaitStrategies implements WaitStrategy { SPIN @@ -73,7 +75,7 @@ public void apply( long iteration ) throws AcquireLockTimeoutException catch(InterruptedException e) { Thread.interrupted(); - throw new AcquireLockTimeoutException( e , "Interrupted while waiting."); + throw new AcquireLockTimeoutException( e , "Interrupted while waiting.", Interrupted ); } } }; diff --git a/community/kernel/src/main/java/org/neo4j/storageengine/api/lock/AcquireLockTimeoutException.java b/community/kernel/src/main/java/org/neo4j/storageengine/api/lock/AcquireLockTimeoutException.java index 82f486d39b85..78a6d9c41474 100644 --- a/community/kernel/src/main/java/org/neo4j/storageengine/api/lock/AcquireLockTimeoutException.java +++ b/community/kernel/src/main/java/org/neo4j/storageengine/api/lock/AcquireLockTimeoutException.java @@ -19,19 +19,31 @@ */ package org.neo4j.storageengine.api.lock; +import org.neo4j.kernel.api.exceptions.Status; + /** * Acquiring a lock failed. This is a runtime exception now to ease the transition from the old lock interface, but * it should be made into a {@link org.neo4j.kernel.api.exceptions.KernelException} asap. */ -public class AcquireLockTimeoutException extends RuntimeException +public class AcquireLockTimeoutException extends RuntimeException implements Status.HasStatus { - public AcquireLockTimeoutException( Throwable cause, String message, Object... parameters ) + private final Status statusCode; + + public AcquireLockTimeoutException( Throwable cause, String message, Status statusCode ) { - super( String.format(message, parameters), cause ); + super( message, cause ); + this.statusCode = statusCode; } - public AcquireLockTimeoutException( String message ) + public AcquireLockTimeoutException( String message, Status statusCode ) { super( message ); + this.statusCode = statusCode; + } + + @Override + public Status status() + { + return statusCode; } } diff --git a/enterprise/core-edge/src/main/java/org/neo4j/coreedge/core/state/machines/locks/LeaderOnlyLockManager.java b/enterprise/core-edge/src/main/java/org/neo4j/coreedge/core/state/machines/locks/LeaderOnlyLockManager.java index 225cd38ca9fd..094c68c0baa8 100644 --- a/enterprise/core-edge/src/main/java/org/neo4j/coreedge/core/state/machines/locks/LeaderOnlyLockManager.java +++ b/enterprise/core-edge/src/main/java/org/neo4j/coreedge/core/state/machines/locks/LeaderOnlyLockManager.java @@ -21,18 +21,20 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import java.util.concurrent.TimeoutException; import org.neo4j.coreedge.core.consensus.LeaderLocator; import org.neo4j.coreedge.core.consensus.NoLeaderFoundException; import org.neo4j.coreedge.core.replication.Replicator; import org.neo4j.coreedge.core.state.machines.tx.ReplicatedTransactionStateMachine; import org.neo4j.coreedge.identity.MemberId; +import org.neo4j.kernel.api.exceptions.Status; import org.neo4j.kernel.impl.locking.Locks; import org.neo4j.storageengine.api.lock.AcquireLockTimeoutException; import org.neo4j.storageengine.api.lock.ResourceType; -import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static org.neo4j.kernel.api.exceptions.Status.Cluster.NoLeaderAvailable; +import static org.neo4j.kernel.api.exceptions.Status.Cluster.NotALeader; +import static org.neo4j.kernel.api.exceptions.Status.Transaction.Interrupted; /** * Each member of the cluster uses its own {@link LeaderOnlyLockManager} which wraps a local {@link Locks} manager. @@ -109,7 +111,7 @@ private synchronized int acquireTokenOrThrow() } catch ( InterruptedException e ) { - throw new AcquireLockTimeoutException( e, "Interrupted acquiring token." ); + throw new AcquireLockTimeoutException( e, "Interrupted acquiring token.", Interrupted ); } try @@ -121,17 +123,18 @@ private synchronized int acquireTokenOrThrow() } else { - throw new AcquireLockTimeoutException( "Failed to acquire lock token. Was taken by another candidate." ); + throw new AcquireLockTimeoutException( "Failed to acquire lock token. Was taken by another candidate.", + NotALeader); } } catch ( ExecutionException e ) { - throw new AcquireLockTimeoutException( e, "Failed to acquire lock token." ); + throw new AcquireLockTimeoutException( e, "Failed to acquire lock token.", NotALeader ); } catch ( InterruptedException e ) { Thread.currentThread().interrupt(); - throw new AcquireLockTimeoutException( e, "Failed to acquire lock token." ); + throw new AcquireLockTimeoutException( e, "Failed to acquire lock token.", Interrupted ); } } @@ -145,12 +148,12 @@ private void ensureLeader() } catch ( NoLeaderFoundException e ) { - throw new AcquireLockTimeoutException( e, "Could not acquire lock token." ); + throw new AcquireLockTimeoutException( e, "Could not acquire lock token.", NoLeaderAvailable ); } if ( !leader.equals( myself ) ) { - throw new AcquireLockTimeoutException( LOCK_NOT_ON_LEADER_ERROR_MESSAGE ); + throw new AcquireLockTimeoutException( LOCK_NOT_ON_LEADER_ERROR_MESSAGE, NotALeader ); } } @@ -195,7 +198,7 @@ private void ensureHoldingToken() } else if ( lockTokenId != lockTokenStateMachine.currentToken().id() ) { - throw new AcquireLockTimeoutException( "Local instance lost lock token." ); + throw new AcquireLockTimeoutException( "Local instance lost lock token.", NotALeader ); } }