Search in sources :

Example 21 with GetResult

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

the class PulsarLedgerUnderreplicationManager method getCheckAllLedgersCTime.

@Override
public long getCheckAllLedgersCTime() throws ReplicationException.UnavailableException {
    if (log.isDebugEnabled()) {
        log.debug("setCheckAllLedgersCTime");
    }
    try {
        Optional<GetResult> optRes = store.get(checkAllLedgersCtimePath).get();
        if (!optRes.isPresent()) {
            log.warn("checkAllLedgersCtimeZnode is not yet available");
            return -1;
        }
        byte[] data = optRes.get().getValue();
        CheckAllLedgersFormat checkAllLedgersFormat = CheckAllLedgersFormat.parseFrom(data);
        return checkAllLedgersFormat.hasCheckAllLedgersCTime() ? checkAllLedgersFormat.getCheckAllLedgersCTime() : -1;
    } catch (ExecutionException ee) {
        throw new ReplicationException.UnavailableException("Error contacting zookeeper", ee);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new ReplicationException.UnavailableException("Interrupted while contacting zookeeper", ie);
    } catch (InvalidProtocolBufferException ipbe) {
        throw new ReplicationException.UnavailableException("Error while parsing ZK protobuf binary data", ipbe);
    }
}
Also used : CheckAllLedgersFormat(org.apache.bookkeeper.proto.DataFormats.CheckAllLedgersFormat) GetResult(org.apache.pulsar.metadata.api.GetResult) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ReplicationException(org.apache.bookkeeper.replication.ReplicationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 22 with GetResult

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

the class PulsarLedgerUnderreplicationManager method getPlacementPolicyCheckCTime.

@Override
public long getPlacementPolicyCheckCTime() throws ReplicationException.UnavailableException {
    if (log.isDebugEnabled()) {
        log.debug("getPlacementPolicyCheckCTime");
    }
    try {
        Optional<GetResult> optRes = store.get(placementPolicyCheckCtimePath).get();
        if (!optRes.isPresent()) {
            log.warn("placementPolicyCheckCtimeZnode is not yet available");
            return -1;
        }
        byte[] data = optRes.get().getValue();
        PlacementPolicyCheckFormat placementPolicyCheckFormat = PlacementPolicyCheckFormat.parseFrom(data);
        return placementPolicyCheckFormat.hasPlacementPolicyCheckCTime() ? placementPolicyCheckFormat.getPlacementPolicyCheckCTime() : -1;
    } catch (ExecutionException ee) {
        throw new ReplicationException.UnavailableException("Error contacting zookeeper", ee);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new ReplicationException.UnavailableException("Interrupted while contacting zookeeper", ie);
    } catch (InvalidProtocolBufferException ipbe) {
        throw new ReplicationException.UnavailableException("Error while parsing ZK protobuf binary data", ipbe);
    }
}
Also used : GetResult(org.apache.pulsar.metadata.api.GetResult) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PlacementPolicyCheckFormat(org.apache.bookkeeper.proto.DataFormats.PlacementPolicyCheckFormat) ReplicationException(org.apache.bookkeeper.replication.ReplicationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 23 with GetResult

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

the class PulsarRegistrationManager method readCookie.

@Override
public Versioned<byte[]> readCookie(BookieId bookieId) throws BookieException {
    String path = this.cookiePath + "/" + bookieId;
    try {
        Optional<GetResult> res = store.get(path).get();
        if (!res.isPresent()) {
            throw new BookieException.CookieNotFoundException(bookieId.toString());
        }
        // sets stat version from MetadataStore
        LongVersion version = new LongVersion(res.get().getStat().getVersion());
        return new Versioned<>(res.get().getValue(), version);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new BookieException.MetadataStoreException(ie);
    } catch (ExecutionException e) {
        throw new BookieException.MetadataStoreException(e);
    }
}
Also used : GetResult(org.apache.pulsar.metadata.api.GetResult) Versioned(org.apache.bookkeeper.versioning.Versioned) LongVersion(org.apache.bookkeeper.versioning.LongVersion) ExecutionException(java.util.concurrent.ExecutionException) BookieException(org.apache.bookkeeper.bookie.BookieException)

Example 24 with GetResult

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

the class NamespaceServiceTest method testUnloadNamespaceBundleFailure.

@Test
public void testUnloadNamespaceBundleFailure() throws Exception {
    final String topicName = "persistent://my-property/use/my-ns/my-topic1";
    pulsarClient.newConsumer().topic(topicName).subscriptionName("my-subscriber-name").subscribe();
    ConcurrentOpenHashMap<String, CompletableFuture<Optional<Topic>>> topics = pulsar.getBrokerService().getTopics();
    Topic spyTopic = spy(topics.get(topicName).get().get());
    topics.clear();
    CompletableFuture<Optional<Topic>> topicFuture = CompletableFuture.completedFuture(Optional.of(spyTopic));
    // add mock topic
    topics.put(topicName, topicFuture);
    doAnswer((Answer<CompletableFuture<Void>>) invocation -> {
        CompletableFuture<Void> result = new CompletableFuture<>();
        result.completeExceptionally(new RuntimeException("first time failed"));
        return result;
    }).when(spyTopic).close(false);
    NamespaceBundle bundle = pulsar.getNamespaceService().getBundle(TopicName.get(topicName));
    pulsar.getNamespaceService().unloadNamespaceBundle(bundle).join();
    Optional<GetResult> res = this.pulsar.getLocalMetadataStore().get(ServiceUnitUtils.path(bundle)).join();
    assertFalse(res.isPresent());
}
Also used : BundleData(org.apache.pulsar.policies.data.loadbalancer.BundleData) Topic(org.apache.pulsar.broker.service.Topic) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) LoggerFactory(org.slf4j.LoggerFactory) Cleanup(lombok.Cleanup) LoadManager(org.apache.pulsar.broker.loadbalance.LoadManager) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) NamespaceBundleSplitAlgorithm(org.apache.pulsar.common.naming.NamespaceBundleSplitAlgorithm) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) MetadataCache(org.apache.pulsar.metadata.api.MetadataCache) Pair(org.apache.commons.lang3.tuple.Pair) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) URI(java.net.URI) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertFalse(org.testng.Assert.assertFalse) Method(java.lang.reflect.Method) AdvertisedListener(org.apache.pulsar.policies.data.loadbalancer.AdvertisedListener) EnumSet(java.util.EnumSet) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) LoadData(org.apache.pulsar.broker.loadbalance.LoadData) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) Assert.assertNotNull(org.testng.Assert.assertNotNull) LoadReport(org.apache.pulsar.policies.data.loadbalancer.LoadReport) BrokerTestBase(org.apache.pulsar.broker.service.BrokerTestBase) Consumer(org.apache.pulsar.client.api.Consumer) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ServiceUnitId(org.apache.pulsar.common.naming.ServiceUnitId) ConcurrentOpenHashMap(org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap) ModularLoadManagerWrapper(org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerWrapper) Optional(java.util.Optional) NamespaceBundleStats(org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats) Awaitility(org.awaitility.Awaitility) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TopicName(org.apache.pulsar.common.naming.TopicName) Assert.assertNull(org.testng.Assert.assertNull) Assert.assertEquals(org.testng.Assert.assertEquals) ModularLoadManagerImpl(org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl) CompletableFuture(java.util.concurrent.CompletableFuture) ResourceUnit(org.apache.pulsar.broker.loadbalance.ResourceUnit) Hashing(com.google.common.hash.Hashing) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) Lists(com.google.common.collect.Lists) GetResult(org.apache.pulsar.metadata.api.GetResult) Assert(org.testng.Assert) CollectionUtils(org.apache.commons.collections.CollectionUtils) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) SimpleResourceUnit(org.apache.pulsar.broker.loadbalance.impl.SimpleResourceUnit) PulsarClient(org.apache.pulsar.client.api.PulsarClient) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Logger(org.slf4j.Logger) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) Assert.fail(org.testng.Assert.fail) CreateOption(org.apache.pulsar.metadata.api.extended.CreateOption) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) Policies(org.apache.pulsar.common.policies.data.Policies) AsyncLoadingCache(com.github.benmanes.caffeine.cache.AsyncLoadingCache) LookupResult(org.apache.pulsar.broker.lookup.LookupResult) Assert.assertTrue(org.testng.Assert.assertTrue) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) CompletableFuture(java.util.concurrent.CompletableFuture) Optional(java.util.Optional) GetResult(org.apache.pulsar.metadata.api.GetResult) Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Test(org.testng.annotations.Test)

Example 25 with GetResult

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

the class MetadataStoreTest method insertionTestWithExpectedVersion.

@Test(dataProvider = "impl")
public void insertionTestWithExpectedVersion(String provider, Supplier<String> urlSupplier) throws Exception {
    @Cleanup MetadataStore store = MetadataStoreFactory.create(urlSupplier.get(), MetadataStoreConfig.builder().build());
    String key1 = newKey();
    try {
        store.put(key1, "value-1".getBytes(), Optional.of(0L)).join();
        fail("Should have failed");
    } catch (CompletionException e) {
        assertException(e, BadVersionException.class);
    }
    try {
        store.put(key1, "value-1".getBytes(), Optional.of(1L)).join();
        fail("Should have failed");
    } catch (CompletionException e) {
        assertException(e, BadVersionException.class);
    }
    store.put(key1, "value-1".getBytes(), Optional.of(-1L)).join();
    assertTrue(store.exists(key1).join());
    Optional<GetResult> optRes = store.get(key1).join();
    assertTrue(optRes.isPresent());
    assertEquals(optRes.get().getValue(), "value-1".getBytes());
    assertEquals(optRes.get().getStat().getVersion(), 0);
    try {
        store.put(key1, "value-2".getBytes(), Optional.of(-1L)).join();
        fail("Should have failed");
    } catch (CompletionException e) {
        assertException(e, BadVersionException.class);
    }
    try {
        store.put(key1, "value-2".getBytes(), Optional.of(1L)).join();
        fail("Should have failed");
    } catch (CompletionException e) {
        assertException(e, BadVersionException.class);
    }
    assertTrue(store.exists(key1).join());
    optRes = store.get(key1).join();
    assertTrue(optRes.isPresent());
    assertEquals(optRes.get().getValue(), "value-1".getBytes());
    assertEquals(optRes.get().getStat().getVersion(), 0);
    store.put(key1, "value-2".getBytes(), Optional.of(0L)).join();
    assertTrue(store.exists(key1).join());
    optRes = store.get(key1).join();
    assertTrue(optRes.isPresent());
    assertEquals(optRes.get().getValue(), "value-2".getBytes());
    assertEquals(optRes.get().getStat().getVersion(), 1);
}
Also used : MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) GetResult(org.apache.pulsar.metadata.api.GetResult) CompletionException(java.util.concurrent.CompletionException) BadVersionException(org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Aggregations

GetResult (org.apache.pulsar.metadata.api.GetResult)62 ExecutionException (java.util.concurrent.ExecutionException)24 Test (org.testng.annotations.Test)24 Optional (java.util.Optional)18 MetadataStore (org.apache.pulsar.metadata.api.MetadataStore)18 CompletableFuture (java.util.concurrent.CompletableFuture)17 ReplicationException (org.apache.bookkeeper.replication.ReplicationException)15 Cleanup (lombok.Cleanup)13 EnumSet (java.util.EnumSet)12 MetadataStoreException (org.apache.pulsar.metadata.api.MetadataStoreException)12 Stat (org.apache.pulsar.metadata.api.Stat)12 CreateOption (org.apache.pulsar.metadata.api.extended.CreateOption)12 List (java.util.List)11 TimeUnit (java.util.concurrent.TimeUnit)11 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)9 Set (java.util.Set)9 CompletionException (java.util.concurrent.CompletionException)9 BeforeMethod (org.testng.annotations.BeforeMethod)9 Lists (com.google.common.collect.Lists)8 ArrayList (java.util.ArrayList)8