Skip to content

Commit

Permalink
Changed the API for an explicit side-effect free get and explicit mut…
Browse files Browse the repository at this point in the history
…ation.
  • Loading branch information
jimwebber committed Feb 15, 2016
1 parent 46e1249 commit af33450
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
Expand Up @@ -24,24 +24,21 @@
public class ExpontentialBackoffStrategy implements RetryStrategy public class ExpontentialBackoffStrategy implements RetryStrategy
{ {
private long timeout; private long timeout;
private long previous;


public ExpontentialBackoffStrategy( long initialTimeout, TimeUnit timeUnit ) public ExpontentialBackoffStrategy( long initialTimeout, TimeUnit timeUnit )
{ {
this.timeout = timeUnit.toMillis( initialTimeout ); this.timeout = timeUnit.toMillis( initialTimeout );
} }


@Override @Override
public long nextTimeout() public long get()
{ {
previous = timeout;
timeout = 2 * timeout;
return timeout; return timeout;
} }


@Override @Override
public long previousTimeout() public void increaseTimeout()
{ {
return previous; timeout = 2 * timeout;
} }
} }
Expand Up @@ -104,7 +104,8 @@ public long commit( final TransactionToApply tx,


try try
{ {
Long txId = futureTxId.waitUntilCommitted( retryStrategy.nextTimeout(), TimeUnit.MILLISECONDS ); Long txId = futureTxId.waitUntilCommitted( retryStrategy.get(), TimeUnit.MILLISECONDS );
retryStrategy.increaseTimeout();
sessionPool.releaseSession( operationContext ); sessionPool.releaseSession( operationContext );


return txId; return txId;
Expand All @@ -118,7 +119,7 @@ public long commit( final TransactionToApply tx,
catch ( TimeoutException e ) catch ( TimeoutException e )
{ {
log.info( "Replication of %s timed out after %d %s; retrying.", log.info( "Replication of %s timed out after %d %s; retrying.",
operationContext, retryStrategy.previousTimeout(), TimeUnit.MILLISECONDS ); operationContext, retryStrategy.get(), TimeUnit.MILLISECONDS );
txRetryMonitor.retry(); txRetryMonitor.retry();
} }
} }
Expand Down
Expand Up @@ -21,6 +21,6 @@


public interface RetryStrategy public interface RetryStrategy
{ {
long nextTimeout(); long get();
long previousTimeout(); void increaseTimeout();
} }
Expand Up @@ -23,22 +23,21 @@


public class ConstantTimeRetryStrategy implements RetryStrategy public class ConstantTimeRetryStrategy implements RetryStrategy
{ {
private final long timeout; private long timeout;


public ConstantTimeRetryStrategy( long backoffTime, TimeUnit timeUnit ) public ConstantTimeRetryStrategy( long backoffTime, TimeUnit timeUnit )
{ {
this.timeout = timeUnit.toMillis( backoffTime ); this.timeout = timeUnit.toMillis( backoffTime );
} }


@Override @Override
public long nextTimeout() public long get()
{ {
return timeout; return timeout;
} }


@Override @Override
public long previousTimeout() public void increaseTimeout()
{ {
return timeout;
} }
} }
Expand Up @@ -38,11 +38,11 @@ public void shouldDoubleEachTime() throws Exception
// when // when
for ( int i = 0; i < NUMBER_OF_ACCESSES; i++ ) for ( int i = 0; i < NUMBER_OF_ACCESSES; i++ )
{ {
strategy.nextTimeout(); strategy.increaseTimeout();
} }


// then // then
assertEquals( 2 << NUMBER_OF_ACCESSES, strategy.nextTimeout() ); assertEquals( 2 << NUMBER_OF_ACCESSES - 1, strategy.get() );
} }


@Test @Test
Expand All @@ -54,10 +54,10 @@ public void shouldProvidePreviousTimeout() throws Exception
// when // when
for ( int i = 0; i <= NUMBER_OF_ACCESSES; i++ ) for ( int i = 0; i <= NUMBER_OF_ACCESSES; i++ )
{ {
strategy.nextTimeout(); strategy.increaseTimeout();
} }


// then // then
assertEquals( 2 << NUMBER_OF_ACCESSES - 1, strategy.previousTimeout() ); assertEquals( 2 << NUMBER_OF_ACCESSES, strategy.get() );
} }
} }

0 comments on commit af33450

Please sign in to comment.