Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -24,19 +24,23 @@
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.annotations.VisibleForTesting;
import io.netty.util.concurrent.DefaultThreadFactory;
import java.io.IOException;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.common.concurrent.FutureUtils;
import org.apache.pulsar.common.util.Backoff;
import org.apache.pulsar.metadata.api.CacheGetResult;
import org.apache.pulsar.metadata.api.GetResult;
import org.apache.pulsar.metadata.api.MetadataCache;
Expand All @@ -58,6 +62,8 @@ public class MetadataCacheImpl<T> implements MetadataCache<T>, Consumer<Notifica
private final MetadataStore store;
private final MetadataStoreExtended storeExtended;
private final MetadataSerde<T> serde;
private final ScheduledExecutorService backoffExecutor =
Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("metadata-cache-backoff"));

private final AsyncLoadingCache<String, Optional<CacheGetResult<T>>> objCache;

Expand Down Expand Up @@ -321,22 +327,25 @@ public void accept(Notification t) {
}
}

private CompletableFuture<T> executeWithRetry(Supplier<CompletableFuture<T>> op, String key) {
CompletableFuture<T> result = new CompletableFuture<>();
private void execute(Supplier<CompletableFuture<T>> op, String key, CompletableFuture<T> result, Backoff backoff) {
op.get().thenAccept(result::complete).exceptionally((ex) -> {
if (ex.getCause() instanceof BadVersionException) {
// if resource is updated by other than metadata-cache then metadata-cache will get bad-version
// exception. so, try to invalidate the cache and try one more time.
objCache.synchronous().invalidate(key);
op.get().thenAccept(result::complete).exceptionally((ex1) -> {
result.completeExceptionally(ex1.getCause());
return null;
});
backoffExecutor.schedule(() -> execute(op, key, result, backoff), backoff.next(),
TimeUnit.MILLISECONDS);
return null;
}
result.completeExceptionally(ex.getCause());
return null;
});
}

private CompletableFuture<T> executeWithRetry(Supplier<CompletableFuture<T>> op, String key) {
final var backoff = new Backoff(100, TimeUnit.MILLISECONDS, 1, TimeUnit.MINUTES, 0, TimeUnit.MILLISECONDS);
CompletableFuture<T> result = new CompletableFuture<>();
execute(op, key, result, backoff);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.common.policies.data.Policies;
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.apache.pulsar.metadata.api.CacheGetResult;
import org.apache.pulsar.metadata.api.MetadataCache;
Expand Down Expand Up @@ -488,32 +489,29 @@ public void readModifyUpdate(String provider, Supplier<String> urlSupplier) thro
public void readModifyUpdateBadVersionRetry() throws Exception {
String url = zks.getConnectionString();
@Cleanup
MetadataStore sourceStore1 = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
@Cleanup
MetadataStore sourceStore2 = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataStore store = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());

MetadataCache<MyClass> objCache1 = sourceStore1.getMetadataCache(MyClass.class);
MetadataCache<MyClass> objCache2 = sourceStore2.getMetadataCache(MyClass.class);
MetadataCache<MyClass> cache = store.getMetadataCache(MyClass.class);

String key1 = newKey();

MyClass value1 = new MyClass("a", 1);
objCache1.create(key1, value1).join();
assertEquals(objCache1.get(key1).join().get().b, 1);

CompletableFuture<MyClass> future1 = objCache1.readModifyUpdate(key1, v -> {
return new MyClass(v.a, v.b + 1);
});
cache.create(key1, value1).join();
assertEquals(cache.get(key1).join().get().b, 1);

CompletableFuture<MyClass> future2 = objCache2.readModifyUpdate(key1, v -> {
return new MyClass(v.a, v.b + 1);
});

MyClass myClass1 = future1.join();
assertEquals(myClass1.b, 2);
final var futures = new ArrayList<CompletableFuture<MyClass>>();
final var sourceStores = new ArrayList<MetadataStore>();

MyClass myClass2 = future2.join();
assertEquals(myClass2.b, 3);
for (int i = 0; i < 20; i++) {
final var sourceStore = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
sourceStores.add(sourceStore);
final var objCache = sourceStore.getMetadataCache(MyClass.class);
futures.add(objCache.readModifyUpdate(key1, v -> new MyClass(v.a, v.b + 1)));
}
FutureUtil.waitForAll(futures).join();
for (var sourceStore : sourceStores) {
sourceStore.close();
}
}

@Test(dataProvider = "impl")
Expand Down
Loading