Search in sources :

Example 6 with MetadataCache

use of org.apache.pulsar.metadata.api.MetadataCache in project pulsar by yahoo.

the class MetadataCacheTest method testCloneInReadModifyUpdateOrCreate.

@Test(dataProvider = "impl")
public void testCloneInReadModifyUpdateOrCreate(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 = "/testCloneInReadModifyUpdateOrCreate-policies";
    // init cache
    Policies policies = new Policies();
    policies.max_unacked_messages_per_consumer = 100;
    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.readModifyUpdateOrCreate(path, (policies1) -> {
        Policies policiesRef = policies1.get();
        assertNotSame(policiesRef, tempPolicies);
        reference.set(policiesRef);
        policiesRef.max_unacked_messages_per_consumer = 200;
        return policiesRef;
    }).get();
    objCache.readModifyUpdateOrCreate(path, (policies1) -> {
        Policies policiesRef = policies1.get();
        assertNotSame(policiesRef, tempPolicies);
        reference2.set(policiesRef);
        policiesRef.max_unacked_messages_per_consumer = 300;
        return policiesRef;
    }).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);
}
Also used : MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) DataProvider(org.testng.annotations.DataProvider) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) Assert.assertEquals(org.testng.Assert.assertEquals) Cleanup(lombok.Cleanup) MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) CacheGetResult(org.apache.pulsar.metadata.api.CacheGetResult) MetadataCacheImpl(org.apache.pulsar.metadata.cache.impl.MetadataCacheImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) MetadataCache(org.apache.pulsar.metadata.api.MetadataCache) Assert.assertNotSame(org.testng.Assert.assertNotSame) MetadataSerde(org.apache.pulsar.metadata.api.MetadataSerde) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Assert.assertSame(org.testng.Assert.assertSame) Stat(org.apache.pulsar.metadata.api.Stat) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) Assert.fail(org.testng.Assert.fail) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) MetadataStoreConfig(org.apache.pulsar.metadata.api.MetadataStoreConfig) StandardCharsets(java.nio.charset.StandardCharsets) ContentDeserializationException(org.apache.pulsar.metadata.api.MetadataStoreException.ContentDeserializationException) Policies(org.apache.pulsar.common.policies.data.Policies) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TreeMap(java.util.TreeMap) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) NotificationType(org.apache.pulsar.metadata.api.NotificationType) Data(lombok.Data) Assert.assertTrue(org.testng.Assert.assertTrue) Optional(java.util.Optional) AllArgsConstructor(lombok.AllArgsConstructor) Awaitility(org.awaitility.Awaitility) NoArgsConstructor(lombok.NoArgsConstructor) MetadataStoreFactory(org.apache.pulsar.metadata.api.MetadataStoreFactory) Policies(org.apache.pulsar.common.policies.data.Policies) AtomicReference(java.util.concurrent.atomic.AtomicReference) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Example 7 with MetadataCache

use of org.apache.pulsar.metadata.api.MetadataCache in project pulsar by yahoo.

the class MetadataCacheTest method crossStoreAddDelete.

@Test(dataProvider = "zk")
public void crossStoreAddDelete(String provider, Supplier<String> urlSupplier) throws Exception {
    @Cleanup MetadataStore store1 = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
    @Cleanup MetadataStore store2 = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
    @Cleanup MetadataStore store3 = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
    MetadataCache<MyClass> objCache1 = store1.getMetadataCache(MyClass.class);
    MetadataCache<MyClass> objCache2 = store2.getMetadataCache(MyClass.class);
    MetadataCache<MyClass> objCache3 = store3.getMetadataCache(MyClass.class);
    List<MetadataCache<MyClass>> allCaches = new ArrayList<>();
    allCaches.add(objCache1);
    allCaches.add(objCache2);
    allCaches.add(objCache3);
    // Add on one cache and remove from another
    multiStoreAddDelete(allCaches, 0, 1, "add cache0 del cache1");
    // retry same order to rule out any stale state
    multiStoreAddDelete(allCaches, 0, 1, "add cache0 del cache1");
    // Reverse the operations
    multiStoreAddDelete(allCaches, 1, 0, "add cache1 del cache0");
    // Ensure that working on same cache continues to work.
    multiStoreAddDelete(allCaches, 1, 1, "add cache1 del cache1");
}
Also used : MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) ArrayList(java.util.ArrayList) MetadataCache(org.apache.pulsar.metadata.api.MetadataCache) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Example 8 with MetadataCache

use of org.apache.pulsar.metadata.api.MetadataCache in project incubator-pulsar by apache.

the class MetadataCacheTest method crossStoreAddDelete.

@Test(dataProvider = "zk")
public void crossStoreAddDelete(String provider, Supplier<String> urlSupplier) throws Exception {
    @Cleanup MetadataStore store1 = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
    @Cleanup MetadataStore store2 = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
    @Cleanup MetadataStore store3 = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
    MetadataCache<MyClass> objCache1 = store1.getMetadataCache(MyClass.class);
    MetadataCache<MyClass> objCache2 = store2.getMetadataCache(MyClass.class);
    MetadataCache<MyClass> objCache3 = store3.getMetadataCache(MyClass.class);
    List<MetadataCache<MyClass>> allCaches = new ArrayList<>();
    allCaches.add(objCache1);
    allCaches.add(objCache2);
    allCaches.add(objCache3);
    // Add on one cache and remove from another
    multiStoreAddDelete(allCaches, 0, 1, "add cache0 del cache1");
    // retry same order to rule out any stale state
    multiStoreAddDelete(allCaches, 0, 1, "add cache0 del cache1");
    // Reverse the operations
    multiStoreAddDelete(allCaches, 1, 0, "add cache1 del cache0");
    // Ensure that working on same cache continues to work.
    multiStoreAddDelete(allCaches, 1, 1, "add cache1 del cache1");
}
Also used : MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) ArrayList(java.util.ArrayList) MetadataCache(org.apache.pulsar.metadata.api.MetadataCache) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Example 9 with MetadataCache

use of org.apache.pulsar.metadata.api.MetadataCache in project incubator-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);
}
Also used : MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) DataProvider(org.testng.annotations.DataProvider) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) Assert.assertEquals(org.testng.Assert.assertEquals) Cleanup(lombok.Cleanup) MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) CacheGetResult(org.apache.pulsar.metadata.api.CacheGetResult) MetadataCacheImpl(org.apache.pulsar.metadata.cache.impl.MetadataCacheImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) MetadataCache(org.apache.pulsar.metadata.api.MetadataCache) Assert.assertNotSame(org.testng.Assert.assertNotSame) MetadataSerde(org.apache.pulsar.metadata.api.MetadataSerde) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Assert.assertSame(org.testng.Assert.assertSame) Stat(org.apache.pulsar.metadata.api.Stat) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) Assert.fail(org.testng.Assert.fail) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) MetadataStoreConfig(org.apache.pulsar.metadata.api.MetadataStoreConfig) StandardCharsets(java.nio.charset.StandardCharsets) ContentDeserializationException(org.apache.pulsar.metadata.api.MetadataStoreException.ContentDeserializationException) Policies(org.apache.pulsar.common.policies.data.Policies) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TreeMap(java.util.TreeMap) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) NotificationType(org.apache.pulsar.metadata.api.NotificationType) Data(lombok.Data) Assert.assertTrue(org.testng.Assert.assertTrue) Optional(java.util.Optional) AllArgsConstructor(lombok.AllArgsConstructor) Awaitility(org.awaitility.Awaitility) NoArgsConstructor(lombok.NoArgsConstructor) MetadataStoreFactory(org.apache.pulsar.metadata.api.MetadataStoreFactory) Policies(org.apache.pulsar.common.policies.data.Policies) AtomicReference(java.util.concurrent.atomic.AtomicReference) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Example 10 with MetadataCache

use of org.apache.pulsar.metadata.api.MetadataCache in project incubator-pulsar by apache.

the class MetadataCacheTest method testCloneInReadModifyUpdateOrCreate.

@Test(dataProvider = "impl")
public void testCloneInReadModifyUpdateOrCreate(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 = "/testCloneInReadModifyUpdateOrCreate-policies";
    // init cache
    Policies policies = new Policies();
    policies.max_unacked_messages_per_consumer = 100;
    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.readModifyUpdateOrCreate(path, (policies1) -> {
        Policies policiesRef = policies1.get();
        assertNotSame(policiesRef, tempPolicies);
        reference.set(policiesRef);
        policiesRef.max_unacked_messages_per_consumer = 200;
        return policiesRef;
    }).get();
    objCache.readModifyUpdateOrCreate(path, (policies1) -> {
        Policies policiesRef = policies1.get();
        assertNotSame(policiesRef, tempPolicies);
        reference2.set(policiesRef);
        policiesRef.max_unacked_messages_per_consumer = 300;
        return policiesRef;
    }).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);
}
Also used : MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) DataProvider(org.testng.annotations.DataProvider) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) Assert.assertEquals(org.testng.Assert.assertEquals) Cleanup(lombok.Cleanup) MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.testng.annotations.Test) CacheGetResult(org.apache.pulsar.metadata.api.CacheGetResult) MetadataCacheImpl(org.apache.pulsar.metadata.cache.impl.MetadataCacheImpl) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) MetadataCache(org.apache.pulsar.metadata.api.MetadataCache) Assert.assertNotSame(org.testng.Assert.assertNotSame) MetadataSerde(org.apache.pulsar.metadata.api.MetadataSerde) Map(java.util.Map) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Assert.assertSame(org.testng.Assert.assertSame) Stat(org.apache.pulsar.metadata.api.Stat) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) Assert.fail(org.testng.Assert.fail) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) MetadataStoreConfig(org.apache.pulsar.metadata.api.MetadataStoreConfig) StandardCharsets(java.nio.charset.StandardCharsets) ContentDeserializationException(org.apache.pulsar.metadata.api.MetadataStoreException.ContentDeserializationException) Policies(org.apache.pulsar.common.policies.data.Policies) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TreeMap(java.util.TreeMap) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) NotificationType(org.apache.pulsar.metadata.api.NotificationType) Data(lombok.Data) Assert.assertTrue(org.testng.Assert.assertTrue) Optional(java.util.Optional) AllArgsConstructor(lombok.AllArgsConstructor) Awaitility(org.awaitility.Awaitility) NoArgsConstructor(lombok.NoArgsConstructor) MetadataStoreFactory(org.apache.pulsar.metadata.api.MetadataStoreFactory) Policies(org.apache.pulsar.common.policies.data.Policies) AtomicReference(java.util.concurrent.atomic.AtomicReference) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Aggregations

ArrayList (java.util.ArrayList)15 Cleanup (lombok.Cleanup)15 MetadataCache (org.apache.pulsar.metadata.api.MetadataCache)15 MetadataStore (org.apache.pulsar.metadata.api.MetadataStore)15 Test (org.testng.annotations.Test)15 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)12 TypeReference (com.fasterxml.jackson.core.type.TypeReference)12 IOException (java.io.IOException)12 StandardCharsets (java.nio.charset.StandardCharsets)12 List (java.util.List)12 Map (java.util.Map)12 Optional (java.util.Optional)12 TreeMap (java.util.TreeMap)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 CompletionException (java.util.concurrent.CompletionException)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Supplier (java.util.function.Supplier)12 AllArgsConstructor (lombok.AllArgsConstructor)12 Data (lombok.Data)12 NoArgsConstructor (lombok.NoArgsConstructor)12