Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/java/org/apache/cassandra/config/RetrySpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@

import javax.annotation.Nullable;

import accord.utils.RandomSource;
import org.apache.cassandra.config.DurationSpec.LongMillisecondsBound;
import org.apache.cassandra.repair.SharedContext;
import org.apache.cassandra.service.RetryStrategy;
import org.apache.cassandra.service.TimeoutStrategy.LatencySourceFactory;
import org.apache.cassandra.service.WaitStrategy;

import static org.apache.cassandra.service.RetryStrategy.randomizers;

public class RetrySpec
{
public static class MaxAttempt
Expand Down Expand Up @@ -161,7 +164,9 @@ public static WaitStrategy toStrategy(SharedContext ctx, RetrySpec spec)
{
if (!spec.isEnabled())
return WaitStrategy.None.INSTANCE;
return RetryStrategy.parse(spec.baseSleepTime.toMilliseconds() + "ms * 2^attempts <= " + spec.maxSleepTime.toMilliseconds() + "ms,retries=" + (spec.maxAttempts.value - 1), LatencySourceFactory.none());
RandomSource randomSource = RandomSource.wrap(ctx.random().get());
RetryStrategy.WaitRandomizer randomizer = randomizers(randomSource).uniform();
return RetryStrategy.parse("0ms ... " + spec.baseSleepTime.toMilliseconds() + "ms * attempts <= " + spec.maxSleepTime.toMilliseconds() + "ms,retries=" + (spec.maxAttempts.value - 1), LatencySourceFactory.none(), randomizer);
}

@Override
Expand Down
34 changes: 34 additions & 0 deletions test/unit/org/apache/cassandra/service/RetryStrategyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@

package org.apache.cassandra.service;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.function.IntFunction;
import java.util.function.Supplier;

import org.junit.Test;

import accord.utils.Gen;
import accord.utils.RandomTestRunner;
import org.apache.cassandra.config.DurationSpec;
import org.apache.cassandra.config.RetrySpec;
import org.apache.cassandra.repair.SharedContext;
import org.assertj.core.api.Assertions;

public class RetryStrategyTest
{
Expand Down Expand Up @@ -64,6 +71,33 @@ public void fuzzParser()
});
}

@Test
public void testSeededWaitRandomizer()
{
RetrySpec spec = new RetrySpec(new RetrySpec.MaxAttempt(10),
new DurationSpec.LongMillisecondsBound("200ms"),
new DurationSpec.LongMillisecondsBound("1000ms"));
long wait1 = RetrySpec.toStrategy(sharedContext(100), spec).computeWait(1, TimeUnit.MILLISECONDS);
long wait2 = RetrySpec.toStrategy(sharedContext(100), spec).computeWait(1, TimeUnit.MILLISECONDS);
long wait3 = RetrySpec.toStrategy(sharedContext(200), spec).computeWait(1, TimeUnit.MILLISECONDS);
Assertions.assertThat(wait1).isEqualTo(wait2);
Assertions.assertThat(wait1).isNotEqualTo(wait3);
}

private static SharedContext sharedContext(long seed)
{
return new SharedContext.ForwardingSharedContext(SharedContext.Global.instance)
{
private final Random seededRandom = new Random(seed);

@Override
public Supplier<Random> random()
{
return () -> seededRandom;
}
};
}

private static class TestLatencySourceFactory implements TimeoutStrategy.LatencySourceFactory
{

Expand Down