Skip to content

Commit

Permalink
LockResult clean up
Browse files Browse the repository at this point in the history
* Make the API more generic - it handles more lock results than
  just deadlocks
* Don't default to deadlock, have the status injected
  • Loading branch information
Mark Needham committed Nov 18, 2015
1 parent 6032ca7 commit ea0ee32
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Expand Up @@ -311,7 +311,7 @@ public Response<LockResult> acquireExclusiveLock( RequestContext context, Locks.
} }
catch ( NoSuchEntryException | ConcurrentAccessException e) catch ( NoSuchEntryException | ConcurrentAccessException e)
{ {
return spi.packTransactionObligationResponse( context, new LockResult( "Unable to acquire exclusive lock: " + e.getMessage() ) ); return spi.packTransactionObligationResponse( context, new LockResult( LockStatus.NOT_LOCKED, "Unable to acquire exclusive lock: " + e.getMessage() ) );
} }
try try
{ {
Expand All @@ -323,7 +323,7 @@ public Response<LockResult> acquireExclusiveLock( RequestContext context, Locks.
} }
catch ( DeadlockDetectedException e ) catch ( DeadlockDetectedException e )
{ {
return spi.packTransactionObligationResponse( context, new LockResult( "Can't acquire exclusive lock, because it would have caused a deadlock: " + e.getMessage() ) ); return spi.packTransactionObligationResponse( context, new LockResult( LockStatus.DEAD_LOCKED,"Can't acquire exclusive lock, because it would have caused a deadlock: " + e.getMessage() ) );
} }
catch ( IllegalResourceException e ) catch ( IllegalResourceException e )
{ {
Expand All @@ -347,7 +347,7 @@ public Response<LockResult> acquireSharedLock( RequestContext context, Locks.Res
} }
catch ( NoSuchEntryException | ConcurrentAccessException e) catch ( NoSuchEntryException | ConcurrentAccessException e)
{ {
return spi.packTransactionObligationResponse( context, new LockResult( "Unable to acquire shared lock: " + e.getMessage() ) ); return spi.packTransactionObligationResponse( context, new LockResult( LockStatus.NOT_LOCKED, "Unable to acquire shared lock: " + e.getMessage() ) );
} }
try try
{ {
Expand All @@ -360,7 +360,7 @@ public Response<LockResult> acquireSharedLock( RequestContext context, Locks.Res
} }
catch ( DeadlockDetectedException e ) catch ( DeadlockDetectedException e )
{ {
return spi.packTransactionObligationResponse( context, new LockResult( e.getMessage() ) ); return spi.packTransactionObligationResponse( context, new LockResult( LockStatus.DEAD_LOCKED, e.getMessage() ) );
} }
catch ( IllegalResourceException e ) catch ( IllegalResourceException e )
{ {
Expand Down
Expand Up @@ -57,7 +57,7 @@ public void write( LockResult responseObject, ChannelBuffer result ) throws IOEx
result.writeByte( responseObject.getStatus().ordinal() ); result.writeByte( responseObject.getStatus().ordinal() );
if ( responseObject.getStatus().hasMessage() ) if ( responseObject.getStatus().hasMessage() )
{ {
writeString( result, responseObject.getDeadlockMessage() ); writeString( result, responseObject.getMessage() );
} }
} }
}; };
Expand All @@ -79,7 +79,7 @@ public LockResult read( ChannelBuffer buffer, ByteBuffer temporaryBuffer ) throw
throw Exceptions.withMessage( e, format( "%s | read invalid ordinal %d. First %db of this channel buffer is:%n%s", throw Exceptions.withMessage( e, format( "%s | read invalid ordinal %d. First %db of this channel buffer is:%n%s",
e.getMessage(), statusOrdinal, maxBytesToPrint, beginningOfBufferAsHexString( buffer, maxBytesToPrint ) ) ); e.getMessage(), statusOrdinal, maxBytesToPrint, beginningOfBufferAsHexString( buffer, maxBytesToPrint ) ) );
} }
return status.hasMessage() ? new LockResult( readString( buffer ) ) : new LockResult( status ); return status.hasMessage() ? new LockResult( LockStatus.DEAD_LOCKED, readString( buffer ) ) : new LockResult( status );
} }


private String beginningOfBufferAsHexString( ChannelBuffer buffer, int maxBytesToPrint ) private String beginningOfBufferAsHexString( ChannelBuffer buffer, int maxBytesToPrint )
Expand Down
Expand Up @@ -22,33 +22,33 @@
public class LockResult public class LockResult
{ {
private final LockStatus status; private final LockStatus status;
private final String deadlockMessage; private final String message;

public LockResult( LockStatus status ) public LockResult( LockStatus status )
{ {
this.status = status; this.status = status;
this.deadlockMessage = null; this.message = null;
} }

public LockResult( String deadlockMessage ) public LockResult( LockStatus status, String message )
{ {
this.status = LockStatus.DEAD_LOCKED; this.status = status;
this.deadlockMessage = deadlockMessage; this.message = message;
} }


public LockStatus getStatus() public LockStatus getStatus()
{ {
return status; return status;
} }


public String getDeadlockMessage() public String getMessage()
{ {
return deadlockMessage; return message;
} }

@Override @Override
public String toString() public String toString()
{ {
return "LockResult[" + status + ", " + deadlockMessage + "]"; return "LockResult[" + status + ", " + message + "]";
} }
} }
Expand Up @@ -258,7 +258,7 @@ private boolean receiveLockResponse( Response<LockResult> response )
switch ( result.getStatus() ) switch ( result.getStatus() )
{ {
case DEAD_LOCKED: case DEAD_LOCKED:
throw new DeadlockDetectedException( result.getDeadlockMessage() ); throw new DeadlockDetectedException( result.getMessage() );
case NOT_LOCKED: case NOT_LOCKED:
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
case OK_LOCKED: case OK_LOCKED:
Expand Down

0 comments on commit ea0ee32

Please sign in to comment.