Search in sources :

Example 1 with MLPendingAckStore

use of org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore in project pulsar by apache.

the class TransactionTest method testSubscriptionRecreateTopic.

@Test
public void testSubscriptionRecreateTopic() throws PulsarAdminException, NoSuchFieldException, IllegalAccessException, PulsarClientException {
    String topic = "persistent://pulsar/system/testReCreateTopic";
    String subName = "sub_testReCreateTopic";
    int retentionSizeInMbSetTo = 5;
    int retentionSizeInMbSetTopic = 6;
    int retentionSizeInMinutesSetTo = 5;
    int retentionSizeInMinutesSetTopic = 6;
    admin.topics().createNonPartitionedTopic(topic);
    PulsarService pulsarService = super.getPulsarServiceList().get(0);
    pulsarService.getBrokerService().getTopics().clear();
    ManagedLedgerFactory managedLedgerFactory = pulsarService.getBrokerService().getManagedLedgerFactory();
    Field field = ManagedLedgerFactoryImpl.class.getDeclaredField("ledgers");
    field.setAccessible(true);
    ConcurrentHashMap<String, CompletableFuture<ManagedLedgerImpl>> ledgers = (ConcurrentHashMap<String, CompletableFuture<ManagedLedgerImpl>>) field.get(managedLedgerFactory);
    ledgers.remove(TopicName.get(topic).getPersistenceNamingEncoding());
    try {
        admin.topics().createNonPartitionedTopic(topic);
        Assert.fail();
    } catch (PulsarAdminException.ConflictException e) {
        log.info("Cann`t create topic again");
    }
    admin.topics().setRetention(topic, new RetentionPolicies(retentionSizeInMinutesSetTopic, retentionSizeInMbSetTopic));
    pulsarClient.newConsumer().topic(topic).subscriptionName(subName).subscribe();
    pulsarService.getBrokerService().getTopicIfExists(topic).thenAccept(option -> {
        if (!option.isPresent()) {
            log.error("Failed o get Topic named: {}", topic);
            Assert.fail();
        }
        PersistentTopic originPersistentTopic = (PersistentTopic) option.get();
        String pendingAckTopicName = MLPendingAckStore.getTransactionPendingAckStoreSuffix(originPersistentTopic.getName(), subName);
        try {
            admin.topics().setRetention(pendingAckTopicName, new RetentionPolicies(retentionSizeInMinutesSetTo, retentionSizeInMbSetTo));
        } catch (PulsarAdminException e) {
            log.error("Failed to get./setRetention of topic with Exception:" + e);
            Assert.fail();
        }
        PersistentSubscription subscription = originPersistentTopic.getSubscription(subName);
        subscription.getPendingAckManageLedger().thenAccept(managedLedger -> {
            long retentionSize = managedLedger.getConfig().getRetentionSizeInMB();
            if (!originPersistentTopic.getTopicPolicies().isPresent()) {
                log.error("Failed to getTopicPolicies of :" + originPersistentTopic);
                Assert.fail();
            }
            TopicPolicies topicPolicies = originPersistentTopic.getTopicPolicies().get();
            Assert.assertEquals(retentionSizeInMbSetTopic, retentionSize);
            MLPendingAckStoreProvider mlPendingAckStoreProvider = new MLPendingAckStoreProvider();
            CompletableFuture<PendingAckStore> future = mlPendingAckStoreProvider.newPendingAckStore(subscription);
            future.thenAccept(pendingAckStore -> {
                ((MLPendingAckStore) pendingAckStore).getManagedLedger().thenAccept(managedLedger1 -> {
                    Assert.assertEquals(managedLedger1.getConfig().getRetentionSizeInMB(), retentionSizeInMbSetTo);
                });
            });
        });
    });
}
Also used : RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) MLPendingAckStoreProvider(org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStoreProvider) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) Field(java.lang.reflect.Field) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarService(org.apache.pulsar.broker.PulsarService) MLPendingAckStore(org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore) PendingAckStore(org.apache.pulsar.broker.transaction.pendingack.PendingAckStore) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test)

Example 2 with MLPendingAckStore

use of org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore in project pulsar by apache.

the class TransactionTest method testEndTPRecoveringWhenManagerLedgerDisReadable.

@Test
public void testEndTPRecoveringWhenManagerLedgerDisReadable() throws Exception {
    String topic = NAMESPACE1 + "/testEndTPRecoveringWhenManagerLedgerDisReadable";
    admin.topics().createNonPartitionedTopic(topic);
    @Cleanup Producer<String> producer = pulsarClient.newProducer(Schema.STRING).producerName("test").enableBatching(false).sendTimeout(0, TimeUnit.SECONDS).topic(topic).create();
    producer.newMessage().send();
    PersistentTopic persistentTopic = (PersistentTopic) getPulsarServiceList().get(0).getBrokerService().getTopic(topic, false).get().get();
    persistentTopic.getManagedLedger().getConfig().setAutoSkipNonRecoverableData(true);
    PersistentSubscription persistentSubscription = (PersistentSubscription) persistentTopic.createSubscription("test", CommandSubscribe.InitialPosition.Earliest, false).get();
    ManagedCursorImpl managedCursor = mock(ManagedCursorImpl.class);
    doReturn(true).when(managedCursor).hasMoreEntries();
    doReturn(false).when(managedCursor).isClosed();
    doReturn(new PositionImpl(-1, -1)).when(managedCursor).getMarkDeletedPosition();
    doAnswer(invocation -> {
        AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(1);
        callback.readEntriesFailed(new ManagedLedgerException.NonRecoverableLedgerException("No ledger exist"), null);
        return null;
    }).when(managedCursor).asyncReadEntries(anyInt(), any(), any(), any());
    TransactionPendingAckStoreProvider pendingAckStoreProvider = mock(TransactionPendingAckStoreProvider.class);
    doReturn(CompletableFuture.completedFuture(new MLPendingAckStore(persistentTopic.getManagedLedger(), managedCursor, null))).when(pendingAckStoreProvider).newPendingAckStore(any());
    doReturn(CompletableFuture.completedFuture(true)).when(pendingAckStoreProvider).checkInitializedBefore(any());
    Class<PulsarService> pulsarServiceClass = PulsarService.class;
    Field field = pulsarServiceClass.getDeclaredField("transactionPendingAckStoreProvider");
    field.setAccessible(true);
    field.set(getPulsarServiceList().get(0), pendingAckStoreProvider);
    PendingAckHandleImpl pendingAckHandle1 = new PendingAckHandleImpl(persistentSubscription);
    Awaitility.await().untilAsserted(() -> assertEquals(pendingAckHandle1.getStats().state, "Ready"));
    doAnswer(invocation -> {
        AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(1);
        callback.readEntriesFailed(new ManagedLedgerException.ManagedLedgerFencedException(), null);
        return null;
    }).when(managedCursor).asyncReadEntries(anyInt(), any(), any(), any());
    PendingAckHandleImpl pendingAckHandle2 = new PendingAckHandleImpl(persistentSubscription);
    Awaitility.await().untilAsserted(() -> assertEquals(pendingAckHandle2.getStats().state, "Ready"));
}
Also used : ManagedCursorImpl(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl) PendingAckHandleImpl(org.apache.pulsar.broker.transaction.pendingack.impl.PendingAckHandleImpl) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) Cleanup(lombok.Cleanup) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) MLPendingAckStore(org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore) Field(java.lang.reflect.Field) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PulsarService(org.apache.pulsar.broker.PulsarService) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) TransactionPendingAckStoreProvider(org.apache.pulsar.broker.transaction.pendingack.TransactionPendingAckStoreProvider) Test(org.testng.annotations.Test)

Example 3 with MLPendingAckStore

use of org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore in project pulsar by yahoo.

the class TransactionTest method testEndTPRecoveringWhenManagerLedgerDisReadable.

@Test
public void testEndTPRecoveringWhenManagerLedgerDisReadable() throws Exception {
    String topic = NAMESPACE1 + "/testEndTPRecoveringWhenManagerLedgerDisReadable";
    admin.topics().createNonPartitionedTopic(topic);
    @Cleanup Producer<String> producer = pulsarClient.newProducer(Schema.STRING).producerName("test").enableBatching(false).sendTimeout(0, TimeUnit.SECONDS).topic(topic).create();
    producer.newMessage().send();
    PersistentTopic persistentTopic = (PersistentTopic) getPulsarServiceList().get(0).getBrokerService().getTopic(topic, false).get().get();
    persistentTopic.getManagedLedger().getConfig().setAutoSkipNonRecoverableData(true);
    PersistentSubscription persistentSubscription = (PersistentSubscription) persistentTopic.createSubscription("test", CommandSubscribe.InitialPosition.Earliest, false, null).get();
    ManagedCursorImpl managedCursor = mock(ManagedCursorImpl.class);
    doReturn(true).when(managedCursor).hasMoreEntries();
    doReturn(false).when(managedCursor).isClosed();
    doReturn(new PositionImpl(-1, -1)).when(managedCursor).getMarkDeletedPosition();
    doAnswer(invocation -> {
        AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(1);
        callback.readEntriesFailed(new ManagedLedgerException.NonRecoverableLedgerException("No ledger exist"), null);
        return null;
    }).when(managedCursor).asyncReadEntries(anyInt(), any(), any(), any());
    TransactionPendingAckStoreProvider pendingAckStoreProvider = mock(TransactionPendingAckStoreProvider.class);
    doReturn(CompletableFuture.completedFuture(new MLPendingAckStore(persistentTopic.getManagedLedger(), managedCursor, null, 500))).when(pendingAckStoreProvider).newPendingAckStore(any());
    doReturn(CompletableFuture.completedFuture(true)).when(pendingAckStoreProvider).checkInitializedBefore(any());
    Class<PulsarService> pulsarServiceClass = PulsarService.class;
    Field field = pulsarServiceClass.getDeclaredField("transactionPendingAckStoreProvider");
    field.setAccessible(true);
    field.set(getPulsarServiceList().get(0), pendingAckStoreProvider);
    PendingAckHandleImpl pendingAckHandle1 = new PendingAckHandleImpl(persistentSubscription);
    Awaitility.await().untilAsserted(() -> assertEquals(pendingAckHandle1.getStats().state, "Ready"));
    doAnswer(invocation -> {
        AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(1);
        callback.readEntriesFailed(new ManagedLedgerException.ManagedLedgerFencedException(), null);
        return null;
    }).when(managedCursor).asyncReadEntries(anyInt(), any(), any(), any());
    PendingAckHandleImpl pendingAckHandle2 = new PendingAckHandleImpl(persistentSubscription);
    Awaitility.await().untilAsserted(() -> assertEquals(pendingAckHandle2.getStats().state, "Ready"));
    doAnswer(invocation -> {
        AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(1);
        callback.readEntriesFailed(new ManagedLedgerException.CursorAlreadyClosedException("test"), null);
        return null;
    }).when(managedCursor).asyncReadEntries(anyInt(), any(), any(), any());
    PendingAckHandleImpl pendingAckHandle3 = new PendingAckHandleImpl(persistentSubscription);
    Awaitility.await().untilAsserted(() -> assertEquals(pendingAckHandle3.getStats().state, "Ready"));
}
Also used : ManagedCursorImpl(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl) PendingAckHandleImpl(org.apache.pulsar.broker.transaction.pendingack.impl.PendingAckHandleImpl) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) Cleanup(lombok.Cleanup) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) MLPendingAckStore(org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore) Field(java.lang.reflect.Field) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PulsarService(org.apache.pulsar.broker.PulsarService) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) TransactionPendingAckStoreProvider(org.apache.pulsar.broker.transaction.pendingack.TransactionPendingAckStoreProvider) Test(org.testng.annotations.Test)

Example 4 with MLPendingAckStore

use of org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore in project pulsar by yahoo.

the class PendingAckMetadataTest method testPendingAckManageLedgerWriteFailState.

@Test
public void testPendingAckManageLedgerWriteFailState() throws Exception {
    ManagedLedgerFactoryConfig factoryConf = new ManagedLedgerFactoryConfig();
    factoryConf.setMaxCacheSize(0);
    String pendingAckTopicName = MLPendingAckStore.getTransactionPendingAckStoreSuffix("test", "test");
    @Cleanup("shutdown") ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(metadataStore, bkc, factoryConf);
    CompletableFuture<ManagedLedger> completableFuture = new CompletableFuture<>();
    factory.asyncOpen(pendingAckTopicName, new AsyncCallbacks.OpenLedgerCallback() {

        @Override
        public void openLedgerComplete(ManagedLedger ledger, Object ctx) {
            completableFuture.complete(ledger);
        }

        @Override
        public void openLedgerFailed(ManagedLedgerException exception, Object ctx) {
        }
    }, null);
    ManagedCursor cursor = completableFuture.get().openCursor("test");
    ManagedCursor subCursor = completableFuture.get().openCursor("test");
    MLPendingAckStore pendingAckStore = new MLPendingAckStore(completableFuture.get(), cursor, subCursor, 500);
    Field field = MLPendingAckStore.class.getDeclaredField("managedLedger");
    field.setAccessible(true);
    ManagedLedgerImpl managedLedger = (ManagedLedgerImpl) field.get(pendingAckStore);
    field = ManagedLedgerImpl.class.getDeclaredField("STATE_UPDATER");
    field.setAccessible(true);
    AtomicReferenceFieldUpdater<ManagedLedgerImpl, ManagedLedgerImpl.State> state = (AtomicReferenceFieldUpdater<ManagedLedgerImpl, ManagedLedgerImpl.State>) field.get(managedLedger);
    state.set(managedLedger, WriteFailed);
    try {
        pendingAckStore.appendAbortMark(new TxnID(1, 1), CommandAck.AckType.Cumulative).get();
        fail();
    } catch (ExecutionException e) {
        assertTrue(e.getCause().getCause() instanceof ManagedLedgerException.ManagedLedgerAlreadyClosedException);
    }
    pendingAckStore.appendAbortMark(new TxnID(1, 1), CommandAck.AckType.Cumulative).get();
    completableFuture.get().close();
    cursor.close();
    subCursor.close();
}
Also used : ManagedLedgerFactoryConfig(org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Cleanup(lombok.Cleanup) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) AtomicReferenceFieldUpdater(java.util.concurrent.atomic.AtomicReferenceFieldUpdater) MLPendingAckStore(org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore) Field(java.lang.reflect.Field) ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) TxnID(org.apache.pulsar.client.api.transaction.TxnID) ManagedLedgerFactory(org.apache.bookkeeper.mledger.ManagedLedgerFactory) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) ExecutionException(java.util.concurrent.ExecutionException) ManagedLedgerFactoryImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl) Test(org.testng.annotations.Test)

Example 5 with MLPendingAckStore

use of org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore in project incubator-pulsar by apache.

the class TransactionTest method testEndTPRecoveringWhenManagerLedgerDisReadable.

@Test
public void testEndTPRecoveringWhenManagerLedgerDisReadable() throws Exception {
    String topic = NAMESPACE1 + "/testEndTPRecoveringWhenManagerLedgerDisReadable";
    admin.topics().createNonPartitionedTopic(topic);
    @Cleanup Producer<String> producer = pulsarClient.newProducer(Schema.STRING).producerName("test").enableBatching(false).sendTimeout(0, TimeUnit.SECONDS).topic(topic).create();
    producer.newMessage().send();
    PersistentTopic persistentTopic = (PersistentTopic) getPulsarServiceList().get(0).getBrokerService().getTopic(topic, false).get().get();
    persistentTopic.getManagedLedger().getConfig().setAutoSkipNonRecoverableData(true);
    PersistentSubscription persistentSubscription = (PersistentSubscription) persistentTopic.createSubscription("test", CommandSubscribe.InitialPosition.Earliest, false, null).get();
    ManagedCursorImpl managedCursor = mock(ManagedCursorImpl.class);
    doReturn(true).when(managedCursor).hasMoreEntries();
    doReturn(false).when(managedCursor).isClosed();
    doReturn(new PositionImpl(-1, -1)).when(managedCursor).getMarkDeletedPosition();
    doAnswer(invocation -> {
        AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(1);
        callback.readEntriesFailed(new ManagedLedgerException.NonRecoverableLedgerException("No ledger exist"), null);
        return null;
    }).when(managedCursor).asyncReadEntries(anyInt(), any(), any(), any());
    TransactionPendingAckStoreProvider pendingAckStoreProvider = mock(TransactionPendingAckStoreProvider.class);
    doReturn(CompletableFuture.completedFuture(new MLPendingAckStore(persistentTopic.getManagedLedger(), managedCursor, null, 500))).when(pendingAckStoreProvider).newPendingAckStore(any());
    doReturn(CompletableFuture.completedFuture(true)).when(pendingAckStoreProvider).checkInitializedBefore(any());
    Class<PulsarService> pulsarServiceClass = PulsarService.class;
    Field field = pulsarServiceClass.getDeclaredField("transactionPendingAckStoreProvider");
    field.setAccessible(true);
    field.set(getPulsarServiceList().get(0), pendingAckStoreProvider);
    PendingAckHandleImpl pendingAckHandle1 = new PendingAckHandleImpl(persistentSubscription);
    Awaitility.await().untilAsserted(() -> assertEquals(pendingAckHandle1.getStats().state, "Ready"));
    doAnswer(invocation -> {
        AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(1);
        callback.readEntriesFailed(new ManagedLedgerException.ManagedLedgerFencedException(), null);
        return null;
    }).when(managedCursor).asyncReadEntries(anyInt(), any(), any(), any());
    PendingAckHandleImpl pendingAckHandle2 = new PendingAckHandleImpl(persistentSubscription);
    Awaitility.await().untilAsserted(() -> assertEquals(pendingAckHandle2.getStats().state, "Ready"));
    doAnswer(invocation -> {
        AsyncCallbacks.ReadEntriesCallback callback = invocation.getArgument(1);
        callback.readEntriesFailed(new ManagedLedgerException.CursorAlreadyClosedException("test"), null);
        return null;
    }).when(managedCursor).asyncReadEntries(anyInt(), any(), any(), any());
    PendingAckHandleImpl pendingAckHandle3 = new PendingAckHandleImpl(persistentSubscription);
    Awaitility.await().untilAsserted(() -> assertEquals(pendingAckHandle3.getStats().state, "Ready"));
}
Also used : ManagedCursorImpl(org.apache.bookkeeper.mledger.impl.ManagedCursorImpl) PendingAckHandleImpl(org.apache.pulsar.broker.transaction.pendingack.impl.PendingAckHandleImpl) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) Cleanup(lombok.Cleanup) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) MLPendingAckStore(org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore) Field(java.lang.reflect.Field) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) PulsarService(org.apache.pulsar.broker.PulsarService) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) AsyncCallbacks(org.apache.bookkeeper.mledger.AsyncCallbacks) TransactionPendingAckStoreProvider(org.apache.pulsar.broker.transaction.pendingack.TransactionPendingAckStoreProvider) Test(org.testng.annotations.Test)

Aggregations

Field (java.lang.reflect.Field)8 MLPendingAckStore (org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStore)8 Test (org.testng.annotations.Test)8 PulsarService (org.apache.pulsar.broker.PulsarService)6 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)6 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)6 CompletableFuture (java.util.concurrent.CompletableFuture)5 Cleanup (lombok.Cleanup)5 AsyncCallbacks (org.apache.bookkeeper.mledger.AsyncCallbacks)5 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)5 ManagedLedgerFactory (org.apache.bookkeeper.mledger.ManagedLedgerFactory)5 ManagedLedgerImpl (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ManagedCursorImpl (org.apache.bookkeeper.mledger.impl.ManagedCursorImpl)3 PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)3 PendingAckStore (org.apache.pulsar.broker.transaction.pendingack.PendingAckStore)3 TransactionPendingAckStoreProvider (org.apache.pulsar.broker.transaction.pendingack.TransactionPendingAckStoreProvider)3 MLPendingAckStoreProvider (org.apache.pulsar.broker.transaction.pendingack.impl.MLPendingAckStoreProvider)3 PendingAckHandleImpl (org.apache.pulsar.broker.transaction.pendingack.impl.PendingAckHandleImpl)3 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)3