Skip to content

Shutdown ClusterTopologyRefreshTask properly #2985

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

import io.lettuce.core.ClientOptions;
Expand Down Expand Up @@ -64,6 +65,8 @@ class ClusterTopologyRefreshScheduler implements Runnable, ClusterEventListener

private final EventExecutorGroup genericWorkerPool;

private static final ReentrantLock refreshLock = new ReentrantLock();

ClusterTopologyRefreshScheduler(Supplier<ClusterClientOptions> clientOptions, Supplier<Partitions> partitions,
Supplier<CompletionStage<?>> refreshTopology, ClientResources clientResources) {

Expand Down Expand Up @@ -108,27 +111,38 @@ public void suspendTopologyRefresh() {
}
}

/**
* @return {@code true} if a topology refresh is in progress.
*/
public boolean isTopologyRefreshInProgress() {
return clusterTopologyRefreshTask.get();
}

/**
* @return the {@link ReentrantLock} that is used to synchronize topology refresh.
*/
public ReentrantLock getRefreshLock() {
return refreshLock;
}

@Override
public void run() {

logger.debug("ClusterTopologyRefreshScheduler.run()");

if (isEventLoopActive()) {

if (!clientOptions.get().isRefreshClusterView()) {
logger.debug("Periodic ClusterTopologyRefresh is disabled");
return;
}
} else {
if (!clientOptions.get().isRefreshClusterView()) {
logger.debug("Periodic ClusterTopologyRefresh is disabled");
return;
}

clientResources.eventExecutorGroup().submit(clusterTopologyRefreshTask);
if (refreshLock.tryLock() && isEventLoopActive()) {
clientResources.eventExecutorGroup().submit(clusterTopologyRefreshTask);
} else {
logger.debug("Event loop is shutting down, skip topology refresh");
if (refreshLock.isHeldByCurrentThread()) {
refreshLock.unlock();
}
}
}

@Override
Expand Down Expand Up @@ -338,18 +352,17 @@ void doRun() {
if (logger.isDebugEnabled()) {
logger.debug("ClusterTopologyRefreshTask requesting partitions");
}
try {
reloadTopologyAsync.get().whenComplete((ignore, throwable) -> {

if (throwable != null) {
logger.warn("Cannot refresh Redis Cluster topology", throwable);
}
reloadTopologyAsync.get().whenComplete((ignore, throwable) -> {

set(false);
});
} catch (Exception e) {
logger.warn("Cannot refresh Redis Cluster topology", e);
}
if (throwable != null) {
logger.warn("Cannot refresh Redis Cluster topology", throwable);
}

set(false);

refreshLock.unlock();
});
}

}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/lettuce/core/cluster/RedisClusterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -1152,8 +1153,13 @@ public void setPartitions(Partitions partitions) {
public CompletableFuture<Void> shutdownAsync(long quietPeriod, long timeout, TimeUnit timeUnit) {

suspendTopologyRefresh();

return super.shutdownAsync(quietPeriod, timeout, timeUnit);
ReentrantLock refreshLock = topologyRefreshScheduler.getRefreshLock();
refreshLock.lock();
return super.shutdownAsync(quietPeriod, timeout, timeUnit).whenComplete((ignore, ex) -> {
if (refreshLock.isHeldByCurrentThread()) {
refreshLock.unlock();
}
});
}

// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockito.Mockito.*;

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

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -37,11 +38,11 @@ class ClusterTopologyRefreshSchedulerUnitTests {

private ClusterTopologyRefreshScheduler sut;

private ClusterTopologyRefreshOptions immediateRefresh = ClusterTopologyRefreshOptions.builder()
private final ClusterTopologyRefreshOptions immediateRefresh = ClusterTopologyRefreshOptions.builder()
.enablePeriodicRefresh(1, TimeUnit.MILLISECONDS).enableAllAdaptiveRefreshTriggers().build();

private ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder().topologyRefreshOptions(immediateRefresh)
.build();
private final ClusterClientOptions clusterClientOptions = ClusterClientOptions.builder()
.topologyRefreshOptions(immediateRefresh).build();

@Mock
private ClientResources clientResources;
Expand Down Expand Up @@ -78,7 +79,7 @@ void runShouldSubmitRefreshShouldTrigger() {
void runnableShouldCallPartitionRefresh() {

when(clusterClient.getClusterClientOptions()).thenReturn(clusterClientOptions);

when(clusterClient.refreshPartitionsAsync()).thenReturn(new CompletableFuture<>());
when(eventExecutors.submit(any(Runnable.class))).then(invocation -> {
((Runnable) invocation.getArguments()[0]).run();
return null;
Expand All @@ -93,6 +94,7 @@ void runnableShouldCallPartitionRefresh() {
void shouldNotSubmitIfExecutorIsShuttingDown() {

when(eventExecutors.isShuttingDown()).thenReturn(true);
when(clusterClient.getClusterClientOptions()).thenReturn(clusterClientOptions);

sut.run();
verify(eventExecutors, never()).submit(any(Runnable.class));
Expand All @@ -102,6 +104,7 @@ void shouldNotSubmitIfExecutorIsShuttingDown() {
void shouldNotSubmitIfExecutorIsShutdown() {

when(eventExecutors.isShutdown()).thenReturn(true);
when(clusterClient.getClusterClientOptions()).thenReturn(clusterClientOptions);

sut.run();
verify(eventExecutors, never()).submit(any(Runnable.class));
Expand All @@ -111,6 +114,7 @@ void shouldNotSubmitIfExecutorIsShutdown() {
void shouldNotSubmitIfExecutorIsTerminated() {

when(eventExecutors.isTerminated()).thenReturn(true);
when(clusterClient.getClusterClientOptions()).thenReturn(clusterClientOptions);

sut.run();
verify(eventExecutors, never()).submit(any(Runnable.class));
Expand Down
Loading