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

[fix][metadata] Cleanup state when lock revalidation gets LockBusyException #17700

Merged
merged 17 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -125,7 +125,7 @@ private void handleSessionEvent(SessionEvent se) {
if (se == SessionEvent.SessionReestablished) {
log.info("Metadata store session has been re-established. Revalidating all the existing locks.");
for (ResourceLockImpl<T> lock : locks.values()) {
futures.add(lock.revalidate(lock.getValue()));
futures.add(lock.revalidate(lock.getValue(), true));
}

} else if (se == SessionEvent.Reconnected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.common.concurrent.FutureUtils;
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.metadata.api.GetResult;
import org.apache.pulsar.metadata.api.MetadataSerde;
import org.apache.pulsar.metadata.api.MetadataStoreException;
Expand Down Expand Up @@ -127,7 +128,7 @@ synchronized CompletableFuture<Void> acquire(T newValue) {
.thenRun(() -> result.complete(null))
.exceptionally(ex -> {
if (ex.getCause() instanceof LockBusyException) {
revalidate(newValue)
revalidate(newValue, false)
.thenAccept(__ -> result.complete(null))
.exceptionally(ex1 -> {
result.completeExceptionally(ex1);
Expand Down Expand Up @@ -184,38 +185,21 @@ synchronized void lockWasInvalidated() {
}

log.info("Lock on resource {} was invalidated", path);
revalidate(value)
.thenRun(() -> log.info("Successfully revalidated the lock on {}", path))
.exceptionally(ex -> {
synchronized (ResourceLockImpl.this) {
if (ex.getCause() instanceof BadVersionException) {
log.warn("Failed to revalidate the lock at {}. Marked as expired", path);
state = State.Released;
expiredFuture.complete(null);
} else {
// We failed to revalidate the lock due to connectivity issue
// Continue assuming we hold the lock, until we can revalidate it, either
// on Reconnected or SessionReestablished events.
revalidateAfterReconnection = true;
log.warn("Failed to revalidate the lock at {}. Retrying later on reconnection {}", path,
ex.getCause().getMessage());
}
}
return null;
});
revalidate(value, true)
.thenRun(() -> log.info("Successfully revalidated the lock on {}", path));
}

synchronized CompletableFuture<Void> revalidateIfNeededAfterReconnection() {
if (revalidateAfterReconnection) {
revalidateAfterReconnection = false;
log.warn("Revalidate lock at {} after reconnection", path);
return revalidate(value);
return revalidate(value, true);
} else {
return CompletableFuture.completedFuture(null);
}
}

synchronized CompletableFuture<Void> revalidate(T newValue) {
synchronized CompletableFuture<Void> revalidate(T newValue, boolean retryWhenConnectionLost) {
mattisonchao marked this conversation as resolved.
Show resolved Hide resolved
if (revalidateFuture == null || revalidateFuture.isDone()) {
revalidateFuture = doRevalidate(newValue);
} else {
Expand All @@ -233,6 +217,30 @@ synchronized CompletableFuture<Void> revalidate(T newValue) {
});
revalidateFuture = newFuture;
}
revalidateFuture.exceptionally(ex -> {
mattisonchao marked this conversation as resolved.
Show resolved Hide resolved
synchronized (ResourceLockImpl.this) {
if (!retryWhenConnectionLost) {
mattisonchao marked this conversation as resolved.
Show resolved Hide resolved
log.warn("Failed to revalidate the lock at {}. Marked as expired", path);
state = State.Released;
expiredFuture.complete(null);
return null;
}
Throwable realCause = FutureUtil.unwrapCompletionException(ex);
if (realCause instanceof BadVersionException || realCause instanceof LockBusyException) {
log.warn("Failed to revalidate the lock at {}. Marked as expired", path);
mattisonchao marked this conversation as resolved.
Show resolved Hide resolved
state = State.Released;
expiredFuture.complete(null);
} else {
// We failed to revalidate the lock due to connectivity issue
// Continue assuming we hold the lock, until we can revalidate it, either
// on Reconnected or SessionReestablished events.
revalidateAfterReconnection = true;
log.warn("Failed to revalidate the lock at {}. Retrying later on reconnection {}", path,
realCause.getMessage());
}
}
return null;
});
return revalidateFuture;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;
import org.apache.pulsar.metadata.coordination.impl.CoordinationServiceImpl;
import org.awaitility.Awaitility;
import org.testng.Assert;
import org.testng.annotations.Test;

public class LockManagerTest extends BaseMetadataStoreTest {
Expand Down Expand Up @@ -293,4 +294,35 @@ public void revalidateLockOnDifferentSession(String provider, Supplier<String> u
assertEquals(new String(store1.get(path2).join().get().getValue()), "\"value-1\"");
});
}

@Test(dataProvider = "impl")
public void testCleanUpStateWhenRevalidationGotLockBusy(String provider, Supplier<String> urlSupplier)
throws Exception {

if (provider.equals("Memory") || provider.equals("RocksDB")) {
// Local memory provider doesn't really have the concept of multiple sessions
return;
}

@Cleanup
MetadataStoreExtended store = MetadataStoreExtended.create(urlSupplier.get(),
MetadataStoreConfig.builder().build());
@Cleanup
CoordinationService cs1 = new CoordinationServiceImpl(store);
@Cleanup
LockManager<String> lm1 = cs1.getLockManager(String.class);
final String path = newKey();
ResourceLock<String> lock = lm1.acquireLock(path, "value-1").join();

assertFalse(lock.getLockExpiredFuture().isDone());
store.delete(path, Optional.empty()).join();

ResourceLock<String> lock2 = lm1.acquireLock(path, "value-2").join();

assertFalse(lock2.getLockExpiredFuture().isDone());

Awaitility.await().untilAsserted(()-> {
Assert.assertTrue(lock.getLockExpiredFuture().isDone());
});
}
}