use of org.apache.pulsar.metadata.api.MetadataCache in project pulsar by apache.
the class MetadataCacheTest method readModifyUpdate.
@Test(dataProvider = "impl")
public void readModifyUpdate(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStore store = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache = store.getMetadataCache(MyClass.class);
String key1 = newKey();
MyClass value1 = new MyClass("a", 1);
objCache.create(key1, value1).join();
assertEquals(objCache.readModifyUpdate(key1, v -> new MyClass(v.a, v.b + 1)).join(), new MyClass("a", 2));
Optional<MyClass> newValue1 = objCache.get(key1).join();
assertTrue(newValue1.isPresent());
assertEquals(newValue1.get().a, "a");
assertEquals(newValue1.get().b, 2);
// Should fail if the key does not exist
try {
objCache.readModifyUpdate(newKey(), v -> {
return new MyClass(v.a, v.b + 1);
}).join();
} catch (CompletionException e) {
assertEquals(e.getCause().getClass(), NotFoundException.class);
}
}
use of org.apache.pulsar.metadata.api.MetadataCache in project pulsar by apache.
the class MetadataCacheTest method readModifyUpdateBadVersionRetry.
/**
* This test validates that metadata-cache can handle BadVersion failure if other cache/metadata-source updates the
* data with different version.
*
* @throws Exception
*/
@Test
public void readModifyUpdateBadVersionRetry() throws Exception {
String url = zks.getConnectionString();
@Cleanup MetadataStore sourceStore1 = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataStore sourceStore2 = MetadataStoreFactory.create(url, MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache1 = sourceStore1.getMetadataCache(MyClass.class);
MetadataCache<MyClass> objCache2 = sourceStore2.getMetadataCache(MyClass.class);
String key1 = newKey();
MyClass value1 = new MyClass("a", 1);
objCache1.create(key1, value1).join();
objCache1.get(key1).join();
objCache2.readModifyUpdate(key1, v -> {
return new MyClass(v.a, v.b + 1);
}).join();
objCache1.readModifyUpdate(key1, v -> {
return new MyClass(v.a, v.b + 1);
}).join();
}
use of org.apache.pulsar.metadata.api.MetadataCache in project pulsar by apache.
the class MetadataCacheTest method testReadCloned.
@Test(dataProvider = "impl")
public void testReadCloned(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStore store = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
MetadataCache<Policies> objCache = store.getMetadataCache(Policies.class);
String path = "/testReadCloned-policies";
// init cache
Policies policies = new Policies();
policies.max_unacked_messages_per_consumer = 100;
policies.replication_clusters.add("1");
objCache.create(path, policies).get();
Policies tempPolicies = objCache.get(path).get().get();
assertSame(tempPolicies, objCache.get(path).get().get());
AtomicReference<Policies> reference = new AtomicReference<>(new Policies());
AtomicReference<Policies> reference2 = new AtomicReference<>(new Policies());
objCache.readModifyUpdate(path, (policies1) -> {
assertNotSame(policies1, tempPolicies);
reference.set(policies1);
policies1.max_unacked_messages_per_consumer = 200;
return policies1;
}).get();
objCache.readModifyUpdate(path, (policies1) -> {
assertNotSame(policies1, tempPolicies);
reference2.set(policies1);
policies1.max_unacked_messages_per_consumer = 300;
return policies1;
}).get();
// The original object should not be modified
assertEquals(tempPolicies.max_unacked_messages_per_consumer.intValue(), 100);
assertNotSame(reference.get(), reference2.get());
assertNotEquals(reference.get().max_unacked_messages_per_consumer, reference2.get().max_unacked_messages_per_consumer);
}
use of org.apache.pulsar.metadata.api.MetadataCache in project pulsar by yahoo.
the class MetadataCacheTest method testReadCloned.
@Test(dataProvider = "impl")
public void testReadCloned(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStore store = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
MetadataCache<Policies> objCache = store.getMetadataCache(Policies.class);
String path = "/testReadCloned-policies";
// init cache
Policies policies = new Policies();
policies.max_unacked_messages_per_consumer = 100;
policies.replication_clusters.add("1");
objCache.create(path, policies).get();
Policies tempPolicies = objCache.get(path).get().get();
assertSame(tempPolicies, objCache.get(path).get().get());
AtomicReference<Policies> reference = new AtomicReference<>(new Policies());
AtomicReference<Policies> reference2 = new AtomicReference<>(new Policies());
objCache.readModifyUpdate(path, (policies1) -> {
assertNotSame(policies1, tempPolicies);
reference.set(policies1);
policies1.max_unacked_messages_per_consumer = 200;
return policies1;
}).get();
objCache.readModifyUpdate(path, (policies1) -> {
assertNotSame(policies1, tempPolicies);
reference2.set(policies1);
policies1.max_unacked_messages_per_consumer = 300;
return policies1;
}).get();
// The original object should not be modified
assertEquals(tempPolicies.max_unacked_messages_per_consumer.intValue(), 100);
assertNotSame(reference.get(), reference2.get());
assertNotEquals(reference.get().max_unacked_messages_per_consumer, reference2.get().max_unacked_messages_per_consumer);
}
use of org.apache.pulsar.metadata.api.MetadataCache in project pulsar by yahoo.
the class MetadataCacheTest method readModifyUpdate.
@Test(dataProvider = "impl")
public void readModifyUpdate(String provider, Supplier<String> urlSupplier) throws Exception {
@Cleanup MetadataStore store = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
MetadataCache<MyClass> objCache = store.getMetadataCache(MyClass.class);
String key1 = newKey();
MyClass value1 = new MyClass("a", 1);
objCache.create(key1, value1).join();
assertEquals(objCache.readModifyUpdate(key1, v -> new MyClass(v.a, v.b + 1)).join(), new MyClass("a", 2));
Optional<MyClass> newValue1 = objCache.get(key1).join();
assertTrue(newValue1.isPresent());
assertEquals(newValue1.get().a, "a");
assertEquals(newValue1.get().b, 2);
// Should fail if the key does not exist
try {
objCache.readModifyUpdate(newKey(), v -> {
return new MyClass(v.a, v.b + 1);
}).join();
} catch (CompletionException e) {
assertEquals(e.getCause().getClass(), NotFoundException.class);
}
}
Aggregations