Fixed according to comments

Signed-off-by: Vorotyntsev <vorotyntsevdanila@gmail.com>
pull/3364/head
Vorotyntsev 4 years ago
parent 2c24a344a3
commit 9de51f42bd

@ -352,8 +352,8 @@ public class Redisson implements RedissonClient {
} }
@Override @Override
public RLock getSpinLock(String name, LockOptions.BackOffOptions backOffOptions) { public RLock getSpinLock(String name, LockOptions.BackOff backOff) {
return new RedissonSpinLock(connectionManager.getCommandExecutor(), name, backOffOptions); return new RedissonSpinLock(connectionManager.getCommandExecutor(), name, backOff);
} }
@Override @Override

@ -133,8 +133,8 @@ public class RedissonReactive implements RedissonReactiveClient {
} }
@Override @Override
public RLockReactive getSpinLock(String name, LockOptions.BackOffOptions backOffOptions) { public RLockReactive getSpinLock(String name, LockOptions.BackOff backOff) {
RedissonSpinLock spinLock = new RedissonSpinLock(commandExecutor, name, backOffOptions); RedissonSpinLock spinLock = new RedissonSpinLock(commandExecutor, name, backOff);
return ReactiveProxyBuilder.create(commandExecutor, spinLock, RLockReactive.class); return ReactiveProxyBuilder.create(commandExecutor, spinLock, RLockReactive.class);
} }

@ -121,8 +121,8 @@ public class RedissonRx implements RedissonRxClient {
} }
@Override @Override
public RLockRx getSpinLock(String name, LockOptions.BackOffOptions backOffOptions) { public RLockRx getSpinLock(String name, LockOptions.BackOff backOff) {
RedissonSpinLock spinLock = new RedissonSpinLock(commandExecutor, name, backOffOptions); RedissonSpinLock spinLock = new RedissonSpinLock(commandExecutor, name, backOff);
return RxProxyBuilder.create(commandExecutor, spinLock, RLockRx.class); return RxProxyBuilder.create(commandExecutor, spinLock, RLockRx.class);
} }

@ -44,16 +44,16 @@ public class RedissonSpinLock extends RedissonBaseLock {
protected long internalLockLeaseTime; protected long internalLockLeaseTime;
protected final LockOptions.BackOffOptions backOffOptions; protected final LockOptions.BackOff backOff;
final CommandAsyncExecutor commandExecutor; final CommandAsyncExecutor commandExecutor;
public RedissonSpinLock(CommandAsyncExecutor commandExecutor, String name, public RedissonSpinLock(CommandAsyncExecutor commandExecutor, String name,
LockOptions.BackOffOptions backOffOptions) { LockOptions.BackOff backOff) {
super(commandExecutor, name); super(commandExecutor, name);
this.commandExecutor = commandExecutor; this.commandExecutor = commandExecutor;
this.internalLockLeaseTime = commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(); this.internalLockLeaseTime = commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout();
this.backOffOptions = backOffOptions; this.backOff = backOff;
} }
@Override @Override
@ -87,7 +87,7 @@ public class RedissonSpinLock extends RedissonBaseLock {
if (ttl == null) { if (ttl == null) {
return; return;
} }
LockOptions.BackOffPolicy backOffPolicy = backOffOptions.create(); LockOptions.BackOffPolicy backOffPolicy = backOff.create();
while (ttl != null) { while (ttl != null) {
long nextSleepPeriod = backOffPolicy.getNextSleepPeriod(); long nextSleepPeriod = backOffPolicy.getNextSleepPeriod();
Thread.sleep(nextSleepPeriod); Thread.sleep(nextSleepPeriod);
@ -158,7 +158,7 @@ public class RedissonSpinLock extends RedissonBaseLock {
return false; return false;
} }
LockOptions.BackOffPolicy backOffPolicy = backOffOptions.create(); LockOptions.BackOffPolicy backOffPolicy = backOff.create();
while (ttl != null) { while (ttl != null) {
current = System.currentTimeMillis(); current = System.currentTimeMillis();
Thread.sleep(backOffPolicy.getNextSleepPeriod()); Thread.sleep(backOffPolicy.getNextSleepPeriod());
@ -244,7 +244,7 @@ public class RedissonSpinLock extends RedissonBaseLock {
@Override @Override
public RFuture<Void> lockAsync(long leaseTime, TimeUnit unit, long currentThreadId) { public RFuture<Void> lockAsync(long leaseTime, TimeUnit unit, long currentThreadId) {
RPromise<Void> result = new RedissonPromise<>(); RPromise<Void> result = new RedissonPromise<>();
LockOptions.BackOffPolicy backOffPolicy = backOffOptions.create(); LockOptions.BackOffPolicy backOffPolicy = backOff.create();
lockAsync(leaseTime, unit, currentThreadId, result, backOffPolicy); lockAsync(leaseTime, unit, currentThreadId, result, backOffPolicy);
return result; return result;
@ -313,7 +313,7 @@ public class RedissonSpinLock extends RedissonBaseLock {
RPromise<Boolean> result = new RedissonPromise<>(); RPromise<Boolean> result = new RedissonPromise<>();
AtomicLong time = new AtomicLong(unit.toMillis(waitTime)); AtomicLong time = new AtomicLong(unit.toMillis(waitTime));
LockOptions.BackOffPolicy backOffPolicy = backOffOptions.create(); LockOptions.BackOffPolicy backOffPolicy = backOff.create();
tryLock(leaseTime, unit, currentThreadId, result, time, backOffPolicy); tryLock(leaseTime, unit, currentThreadId, result, time, backOffPolicy);
return result; return result;

@ -27,7 +27,7 @@ public class LockOptions {
/** /**
* Factory for {@linkplain BackOffPolicy} class. * Factory for {@linkplain BackOffPolicy} class.
*/ */
public interface BackOffOptions { public interface BackOff {
BackOffPolicy create(); BackOffPolicy create();
} }
@ -48,7 +48,7 @@ public class LockOptions {
* Back off algorithm, where sleep period starts with {@linkplain #initialDelay}, each time increases * Back off algorithm, where sleep period starts with {@linkplain #initialDelay}, each time increases
* {@linkplain #multiplier} times but doesn't exceed {@linkplain #maxDelay} * {@linkplain #multiplier} times but doesn't exceed {@linkplain #maxDelay}
*/ */
public static class ExponentialBackOffOptions implements BackOffOptions { public static class ExponentialBackOff implements BackOff {
private long maxDelay = 128; private long maxDelay = 128;
private long initialDelay = 1; private long initialDelay = 1;
private int multiplier = 2; private int multiplier = 2;
@ -66,7 +66,7 @@ public class LockOptions {
* @param maxDelay - max sleep period. Has to be positive * @param maxDelay - max sleep period. Has to be positive
* @return ExponentialBackOffOptions instance * @return ExponentialBackOffOptions instance
*/ */
public ExponentialBackOffOptions maxDelay(long maxDelay) { public ExponentialBackOff maxDelay(long maxDelay) {
if (maxDelay <= 0) { if (maxDelay <= 0) {
throw new IllegalArgumentException("maxDelay should be positive"); throw new IllegalArgumentException("maxDelay should be positive");
} }
@ -86,7 +86,7 @@ public class LockOptions {
* @param initialDelay - initial sleep period. Has to be positive * @param initialDelay - initial sleep period. Has to be positive
* @return ExponentialBackOffOptions instance * @return ExponentialBackOffOptions instance
*/ */
public ExponentialBackOffOptions initialDelay(long initialDelay) { public ExponentialBackOff initialDelay(long initialDelay) {
if (initialDelay <= 0) { if (initialDelay <= 0) {
throw new IllegalArgumentException("initialDelay should be positive"); throw new IllegalArgumentException("initialDelay should be positive");
} }
@ -106,7 +106,7 @@ public class LockOptions {
* @param multiplier - sleep period multiplier. Has to be positive * @param multiplier - sleep period multiplier. Has to be positive
* @return ExponentialBackOffOptions instance * @return ExponentialBackOffOptions instance
*/ */
public ExponentialBackOffOptions multiplier(int multiplier) { public ExponentialBackOff multiplier(int multiplier) {
if (multiplier <= 0) { if (multiplier <= 0) {
throw new IllegalArgumentException("multiplier should be positive"); throw new IllegalArgumentException("multiplier should be positive");
} }
@ -152,7 +152,7 @@ public class LockOptions {
* To reduce possible negative effects of many threads simultaneously sending requests, a small random value is * To reduce possible negative effects of many threads simultaneously sending requests, a small random value is
* added to all sleep periods. * added to all sleep periods.
*/ */
public static class ConstantBackOffOptions implements BackOffOptions { public static class ConstantBackOff implements BackOff {
private long delay = 64; private long delay = 64;
@Override @Override
@ -168,7 +168,7 @@ public class LockOptions {
* @param delay - sleep period value. Has to be positive * @param delay - sleep period value. Has to be positive
* @return ConstantBackOffOptions instance * @return ConstantBackOffOptions instance
*/ */
public ConstantBackOffOptions delay(long delay) { public ConstantBackOff delay(long delay) {
if (delay <= 0) { if (delay <= 0) {
throw new IllegalArgumentException("delay should be positive"); throw new IllegalArgumentException("delay should be positive");
} }
@ -203,7 +203,7 @@ public class LockOptions {
* *
* @return BackOffOptions instance * @return BackOffOptions instance
*/ */
public static BackOffOptions defaults() { public static BackOff defaults() {
return new ExponentialBackOffOptions(); return new ExponentialBackOff();
} }
} }

@ -497,7 +497,7 @@ public interface RedissonClient {
* @param name - name of object * @param name - name of object
* @return Lock object * @return Lock object
*/ */
RLock getSpinLock(String name, LockOptions.BackOffOptions backOffOptions); RLock getSpinLock(String name, LockOptions.BackOff backOff);
/** /**
* Returns MultiLock instance associated with specified <code>locks</code> * Returns MultiLock instance associated with specified <code>locks</code>

@ -187,7 +187,7 @@ public interface RedissonReactiveClient {
* @param name - name of object * @param name - name of object
* @return Lock object * @return Lock object
*/ */
RLockReactive getSpinLock(String name, LockOptions.BackOffOptions backOffOptions); RLockReactive getSpinLock(String name, LockOptions.BackOff backOff);
/** /**
* Returns MultiLock instance associated with specified <code>locks</code> * Returns MultiLock instance associated with specified <code>locks</code>

@ -186,7 +186,7 @@ public interface RedissonRxClient {
* @param name - name of object * @param name - name of object
* @return Lock object * @return Lock object
*/ */
RLockRx getSpinLock(String name, LockOptions.BackOffOptions backOffOptions); RLockRx getSpinLock(String name, LockOptions.BackOff backOff);
/** /**
* Returns MultiLock instance associated with specified <code>locks</code> * Returns MultiLock instance associated with specified <code>locks</code>

@ -9,12 +9,12 @@ public class LockOptionsTest {
@Test @Test
public void testExponentialBackoff() { public void testExponentialBackoff() {
LockOptions.BackOffOptions backOffOptions = new LockOptions.ExponentialBackOffOptions() LockOptions.BackOff backOff = new LockOptions.ExponentialBackOff()
.initialDelay(10) .initialDelay(10)
.maxDelay(100) .maxDelay(100)
.multiplier(3); .multiplier(3);
LockOptions.BackOffPolicy backOffPolicy = backOffOptions.create(); LockOptions.BackOffPolicy backOffPolicy = backOff.create();
assertThat(backOffPolicy.getNextSleepPeriod()).isBetween(10L, 10L); assertThat(backOffPolicy.getNextSleepPeriod()).isBetween(10L, 10L);
@ -26,7 +26,7 @@ public class LockOptionsTest {
@Test @Test
public void testConstantBackoff() { public void testConstantBackoff() {
LockOptions.ConstantBackOffOptions backOffOptions = new LockOptions.ConstantBackOffOptions() LockOptions.ConstantBackOff backOffOptions = new LockOptions.ConstantBackOff()
.delay(30); .delay(30);
LockOptions.BackOffPolicy backOffPolicy = backOffOptions.create(); LockOptions.BackOffPolicy backOffPolicy = backOffOptions.create();

Loading…
Cancel
Save