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

[Issue 13004] Fix race condition in ResourceLockImpl#revalidate #13006

Merged
merged 1 commit into from
Nov 29, 2021
Merged
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 @@ -44,6 +44,7 @@ public class ResourceLockImpl<T> implements ResourceLock<T> {
private long version;
private final CompletableFuture<Void> expiredFuture;
private boolean revalidateAfterReconnection = false;
private CompletableFuture<Void> revalidateFuture;

private enum State {
Init,
Expand Down Expand Up @@ -144,6 +145,9 @@ synchronized CompletableFuture<Void> acquire(T newValue) {

// Simple operation of acquiring the lock with no retries, or checking for the lock content
private CompletableFuture<Void> acquireWithNoRevalidation(T newValue) {
if (log.isDebugEnabled()) {
log.debug("acquireWithNoRevalidation,newValue={},version={}", newValue, version);
}
byte[] payload;
try {
payload = serde.serialize(path, newValue);
Expand Down Expand Up @@ -212,6 +216,30 @@ synchronized CompletableFuture<Void> revalidateIfNeededAfterReconnection() {
}

synchronized CompletableFuture<Void> revalidate(T newValue) {
if (revalidateFuture == null || revalidateFuture.isDone()) {
revalidateFuture = doRevalidate(newValue);
} else {
if (log.isDebugEnabled()) {
log.debug("Previous revalidating is not finished while revalidate newValue={}, value={}, version={}",
newValue, value, version);
}
CompletableFuture<Void> newFuture = new CompletableFuture<>();
revalidateFuture.whenComplete((unused, throwable) -> {
doRevalidate(newValue).thenRun(() -> newFuture.complete(null))
.exceptionally(throwable1 -> {
newFuture.completeExceptionally(throwable1);
return null;
});
});
revalidateFuture = newFuture;
}
return revalidateFuture;
}

private synchronized CompletableFuture<Void> doRevalidate(T newValue) {
if (log.isDebugEnabled()) {
log.debug("doRevalidate with newValue={}, version={}", newValue, version);
}
return store.get(path)
.thenCompose(optGetResult -> {
if (!optGetResult.isPresent()) {
Expand Down