Search in sources :

Example 1 with MetadataCache

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);
    }
}
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) Predicate(java.util.function.Predicate) 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) CompletionException(java.util.concurrent.CompletionException) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Example 2 with MetadataCache

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();
}
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) Predicate(java.util.function.Predicate) 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) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Example 3 with MetadataCache

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);
}
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) Predicate(java.util.function.Predicate) 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 4 with MetadataCache

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);
}
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 5 with MetadataCache

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);
    }
}
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) CompletionException(java.util.concurrent.CompletionException) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) 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