Skip to content

Commit

Permalink
Fix LockRegLeaderIn for interrupted Thread.sleep (#2455)
Browse files Browse the repository at this point in the history
* Fix LockRegLeaderIn for interrupted Thread.sleep

https://build.spring.io/browse/INT-FATS5IC-517

If current thread is interrupted, the `Thread.sleep()` interrupts
immediately.
In the catch block of the main loop in the `LockRegistryLeaderInitiator`
we have such a dangerous `sleep()` and don't restart election in this
candidate any more

* Move `Thread.sleep()` to else after checking the current thread for
interrupted state
* Remove `LongRunningIntegrationTest` rule from the
`RedisLockRegistryLeaderInitiatorTests` since it now works much faster
after proper `busy-wait` handling

**Cherry-pick to master**

* Ignore interruption on the sleep i catch and move on with loop
  • Loading branch information
artembilan authored and garyrussell committed May 30, 2018
1 parent 0285a99 commit 7f898a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,6 @@ else if (acquired) {
}
// The lock was broken and we are no longer leader
handleRevoked();
if (isRunning()) {
// Give it a chance to elect some other leader.
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
}
}

if (e instanceof InterruptedException || Thread.currentThread().isInterrupted()) {
Expand All @@ -417,9 +413,21 @@ else if (acquired) {
}
return null;
}
else if (logger.isDebugEnabled()) {
logger.debug("Error acquiring the lock for " + this.context +
". " + (isRunning() ? "Retrying..." : ""), e);
else {
if (isRunning()) {
// Give it a chance to elect some other leader.
try {
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
}
catch (InterruptedException e1) {
// Ignore interruption and let it to be caught on the next cycle.
Thread.currentThread().interrupt();
}
}
if (logger.isDebugEnabled()) {
logger.debug("Error acquiring the lock for " + this.context +
". " + (isRunning() ? "Retrying..." : ""), e);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.integration.support.leader.LockRegistryLeaderInitiator;
import org.springframework.integration.test.rule.Log4j2LevelAdjuster;
import org.springframework.integration.test.support.LongRunningIntegrationTest;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

/**
Expand All @@ -49,9 +48,6 @@
*/
public class RedisLockRegistryLeaderInitiatorTests extends RedisAvailableTests {

@Rule
public LongRunningIntegrationTest longTests = new LongRunningIntegrationTest();

@Rule
public Log4j2LevelAdjuster adjuster =
Log4j2LevelAdjuster.trace()
Expand Down

0 comments on commit 7f898a5

Please sign in to comment.