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

Shutdown ClusterTopologyRefreshTask properly #2985

Open
wants to merge 8 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,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;

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

private final EventExecutorGroup genericWorkerPool;

private static final ReentrantLock refreshLock = new ReentrantLock();

private static final Condition refreshComplete = refreshLock.newCondition();

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

Expand Down Expand Up @@ -112,6 +118,14 @@ public boolean isTopologyRefreshInProgress() {
return clusterTopologyRefreshTask.get();
}

public ReentrantLock getRefreshLock() {
return refreshLock;
}

public Condition getRefreshComplete() {
return refreshComplete;
}

@Override
public void run() {

Expand Down Expand Up @@ -345,7 +359,15 @@ void doRun() {
logger.warn("Cannot refresh Redis Cluster topology", throwable);
}

set(false);
refreshLock.lock();
try {
reloadTopologyAsync.get();

set(false);
refreshComplete.signalAll();
} finally {
refreshLock.unlock();
}
});
} catch (Exception e) {
logger.warn("Cannot refresh Redis Cluster topology", e);
Expand Down
11 changes: 9 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,8 @@
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
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 +1154,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();
try {
return super.shutdownAsync(quietPeriod, timeout, timeUnit);
} finally {
refreshLock.unlock();
}
}

// -------------------------------------------------------------------------
Expand Down
Loading