Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-388: add non blocking sleeper #440

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public BackOffPolicyBuilder random(boolean random) {

/**
* The {@link Sleeper} instance to be used to back off. Policies default to
* {@link ThreadWaitSleeper}.
* {@link NonBlockingSleeper}.
* @param sleeper the {@link Sleeper} instance
* @return this
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class ExponentialBackOffPolicy implements SleepingBackOffPolicy<Exponenti
*/
private Supplier<Double> multiplierSupplier;

private Sleeper sleeper = new ThreadWaitSleeper();
private Sleeper sleeper = new NonBlockingSleeper();

/**
* Public setter for the {@link Sleeper} strategy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class FixedBackOffPolicy extends StatelessBackOffPolicy implements Sleepi
*/
private Supplier<Long> backOffPeriod = () -> DEFAULT_BACK_OFF_PERIOD;

private Sleeper sleeper = new ThreadWaitSleeper();
private Sleeper sleeper = new NonBlockingSleeper();

public FixedBackOffPolicy withSleeper(Sleeper sleeper) {
FixedBackOffPolicy res = new FixedBackOffPolicy();
Expand All @@ -56,7 +56,7 @@ public FixedBackOffPolicy withSleeper(Sleeper sleeper) {

/**
* Public setter for the {@link Sleeper} strategy.
* @param sleeper the sleeper to set defaults to {@link ThreadWaitSleeper}.
* @param sleeper the sleeper to set defaults to {@link NonBlockingSleeper}.
*/
public void setSleeper(Sleeper sleeper) {
this.sleeper = sleeper;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.retry.backoff;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
* Non-blocking {@link Sleeper} implementation that just sleep with specified
* backOffPeriod.
*
* @author Alireza Hakimrabet
*/
@SuppressWarnings("serial")
public class NonBlockingSleeper implements Sleeper {

@Override
public void sleep(long backOffPeriod) throws InterruptedException {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try {
TimeUnit.MILLISECONDS.sleep(backOffPeriod);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new BackOffInterruptedException("Thread interrupted while sleeping", e);
}
});
future.join();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry what difference does it make against regular Thread.sleep() in the thread which calls this Sleeper?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for reading and comment my code.
when backoff execute from a scheduler and the thread that run the scheduler, going to sleep in the (sleeper.java).
the main idea of that is to release the thread that run scheduler, because the time scheduler and back off time going to Disorder the scheduler or preventing other scheduler from running.
am i right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a confirmation with tests that it works as you explain ?
I just believe that this join() is blocking the thread which calls it.

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
* Simple {@link Sleeper} implementation that just blocks the current Thread with sleep
* period.
*
* @deprecated in favor of {@link NonBlockingSleeper}
* @author Artem Bilan
* @since 1.1
*/
@Deprecated
@SuppressWarnings("serial")
public class ThreadWaitSleeper implements Sleeper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class UniformRandomBackOffPolicy extends StatelessBackOffPolicy

private final Random random = new Random(System.currentTimeMillis());

private Sleeper sleeper = new ThreadWaitSleeper();
private Sleeper sleeper = new NonBlockingSleeper();

public UniformRandomBackOffPolicy withSleeper(Sleeper sleeper) {
UniformRandomBackOffPolicy res = new UniformRandomBackOffPolicy();
Expand All @@ -65,7 +65,7 @@ public UniformRandomBackOffPolicy withSleeper(Sleeper sleeper) {

/**
* Public setter for the {@link Sleeper} strategy.
* @param sleeper the sleeper to set defaults to {@link ThreadWaitSleeper}.
* @param sleeper the sleeper to set defaults to {@link NonBlockingSleeper}.
*/
public void setSleeper(Sleeper sleeper) {
this.sleeper = sleeper;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.retry.backoff;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Alireza Hakimrabet
*/
public class NonBlockingSleeperTests {

@Test
public void testSingleBackOff() throws Exception {
long backOffPeriod = 50;
NonBlockingSleeper strategy = new NonBlockingSleeper();
long before = System.currentTimeMillis();
strategy.sleep(backOffPeriod);
long after = System.currentTimeMillis();
assertEqualsApprox(backOffPeriod, after - before, 25);
}

private void assertEqualsApprox(long desired, long actual, long variance) {
long lower = desired - variance;
long upper = desired + 2 * variance;
assertThat(lower).describedAs("Expected value to be between '%d' and '%d' but was '%d'", lower, upper, actual)
.isLessThanOrEqualTo(actual);
}

}