Skip to content

Commit

Permalink
Fix the backoff strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfurmanski committed Feb 16, 2016
1 parent 1ce1ebc commit 53c0589
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 28 deletions.
Expand Up @@ -23,22 +23,31 @@


public class ExpontentialBackoffStrategy implements RetryStrategy public class ExpontentialBackoffStrategy implements RetryStrategy
{ {
private long timeout; private final long initialBackoffTimeMillis;


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


@Override @Override
public long get() public Timeout newTimeout()
{ {
return timeout; return new Timeout()
} {
private long backoffTimeMillis = initialBackoffTimeMillis;


@Override @Override
public void increaseTimeout() public long getMillis()
{ {
timeout = 2 * timeout; return backoffTimeMillis;
}

@Override
public void increment()
{
backoffTimeMillis = backoffTimeMillis * 2;
}
};
} }
} }
Expand Up @@ -79,6 +79,7 @@ public long commit( final TransactionToApply tx,
throw new TransactionFailureException( "Could not create immutable transaction for replication", e ); throw new TransactionFailureException( "Could not create immutable transaction for replication", e );
} }


RetryStrategy.Timeout timeout = retryStrategy.newTimeout();
boolean hasNeverReplicated = true; boolean hasNeverReplicated = true;
boolean interrupted = false; boolean interrupted = false;
try ( CommittingTransaction futureTxId = txFutures.register( operationContext.localOperationId() ) ) try ( CommittingTransaction futureTxId = txFutures.register( operationContext.localOperationId() ) )
Expand All @@ -104,8 +105,7 @@ public long commit( final TransactionToApply tx,


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


return txId; return txId;
Expand All @@ -119,7 +119,8 @@ 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.get(), TimeUnit.MILLISECONDS ); operationContext, timeout.getMillis(), TimeUnit.MILLISECONDS );
timeout.increment();
txRetryMonitor.retry(); txRetryMonitor.retry();
} }
} }
Expand Down
Expand Up @@ -21,6 +21,11 @@


public interface RetryStrategy public interface RetryStrategy
{ {
long get(); Timeout newTimeout();
void increaseTimeout();
interface Timeout
{
long getMillis();
void increment();
}
} }
Expand Up @@ -23,21 +23,30 @@


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


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


@Override constantTimeout = new Timeout()
public long get() {
{ @Override
return timeout; public long getMillis()
{
return backoffTimeMillis;
}

@Override
public void increment()
{
}
};
} }


@Override @Override
public void increaseTimeout() public Timeout newTimeout()
{ {
return constantTimeout;
} }
} }
Expand Up @@ -34,30 +34,32 @@ public void shouldDoubleEachTime() throws Exception
{ {
// given // given
ExpontentialBackoffStrategy strategy = new ExpontentialBackoffStrategy( 1, MILLISECONDS ); ExpontentialBackoffStrategy strategy = new ExpontentialBackoffStrategy( 1, MILLISECONDS );
RetryStrategy.Timeout timeout = strategy.newTimeout();


// when // when
for ( int i = 0; i < NUMBER_OF_ACCESSES; i++ ) for ( int i = 0; i < NUMBER_OF_ACCESSES; i++ )
{ {
strategy.increaseTimeout(); timeout.increment();
} }


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


@Test @Test
public void shouldProvidePreviousTimeout() throws Exception public void shouldProvidePreviousTimeout() throws Exception
{ {
// given // given
ExpontentialBackoffStrategy strategy = new ExpontentialBackoffStrategy( 1, MILLISECONDS ); ExpontentialBackoffStrategy strategy = new ExpontentialBackoffStrategy( 1, MILLISECONDS );
RetryStrategy.Timeout timeout = strategy.newTimeout();


// when // when
for ( int i = 0; i <= NUMBER_OF_ACCESSES; i++ ) for ( int i = 0; i <= NUMBER_OF_ACCESSES; i++ )
{ {
strategy.increaseTimeout(); timeout.increment();
} }


// then // then
assertEquals( 2 << NUMBER_OF_ACCESSES, strategy.get() ); assertEquals( 2 << NUMBER_OF_ACCESSES, timeout.getMillis() );
} }
} }

0 comments on commit 53c0589

Please sign in to comment.