|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.transaction.buffer.impl; |
| 20 | + |
| 21 | +import static org.mockito.Mockito.doAnswer; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | +import static org.testng.Assert.assertFalse; |
| 25 | +import static org.testng.Assert.assertTrue; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Optional; |
| 28 | +import java.util.concurrent.CompletableFuture; |
| 29 | +import java.util.concurrent.CountDownLatch; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import lombok.SneakyThrows; |
| 32 | +import lombok.extern.slf4j.Slf4j; |
| 33 | +import org.apache.bookkeeper.mledger.ManagedLedger; |
| 34 | +import org.apache.pulsar.broker.BrokerTestUtil; |
| 35 | +import org.apache.pulsar.broker.service.BrokerService; |
| 36 | +import org.apache.pulsar.broker.service.Topic; |
| 37 | +import org.apache.pulsar.broker.service.TopicFactory; |
| 38 | +import org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopic; |
| 39 | +import org.apache.pulsar.broker.service.persistent.PersistentTopic; |
| 40 | +import org.apache.pulsar.broker.transaction.buffer.AbortedTxnProcessor; |
| 41 | +import org.apache.pulsar.broker.transaction.buffer.TransactionBufferProvider; |
| 42 | +import org.apache.pulsar.client.api.ProducerConsumerBase; |
| 43 | +import org.awaitility.Awaitility; |
| 44 | +import org.testng.annotations.AfterClass; |
| 45 | +import org.testng.annotations.BeforeClass; |
| 46 | +import org.testng.annotations.Test; |
| 47 | + |
| 48 | +@Slf4j |
| 49 | +@Test(groups = "broker") |
| 50 | +public class TransactionPersistentTopicTest extends ProducerConsumerBase { |
| 51 | + |
| 52 | + private static CountDownLatch topicInitSuccessSignal = new CountDownLatch(1); |
| 53 | + |
| 54 | + @BeforeClass(alwaysRun = true) |
| 55 | + @Override |
| 56 | + protected void setup() throws Exception { |
| 57 | + // Intercept when the `topicFuture` is about to complete and wait until the topic close operation finishes. |
| 58 | + conf.setTopicFactoryClassName(MyTopicFactory.class.getName()); |
| 59 | + conf.setTransactionCoordinatorEnabled(true); |
| 60 | + conf.setBrokerDeduplicationEnabled(false); |
| 61 | + super.internalSetup(); |
| 62 | + super.producerBaseSetup(); |
| 63 | + } |
| 64 | + |
| 65 | + @AfterClass(alwaysRun = true) |
| 66 | + @Override |
| 67 | + protected void cleanup() throws Exception { |
| 68 | + super.internalCleanup(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testNoOrphanClosedTopicIfTxnInternalFailed() { |
| 73 | + String tpName = BrokerTestUtil.newUniqueName("persistent://public/default/tp2"); |
| 74 | + |
| 75 | + BrokerService brokerService = pulsar.getBrokerService(); |
| 76 | + |
| 77 | + // 1. Mock close topic when create transactionBuffer |
| 78 | + TransactionBufferProvider mockTransactionBufferProvider = originTopic -> { |
| 79 | + AbortedTxnProcessor abortedTxnProcessor = mock(AbortedTxnProcessor.class); |
| 80 | + doAnswer(invocation -> { |
| 81 | + topicInitSuccessSignal.await(); |
| 82 | + return CompletableFuture.failedFuture(new RuntimeException("Mock recovery failed")); |
| 83 | + }).when(abortedTxnProcessor).recoverFromSnapshot(); |
| 84 | + when(abortedTxnProcessor.closeAsync()).thenReturn(CompletableFuture.completedFuture(null)); |
| 85 | + return new TopicTransactionBuffer( |
| 86 | + (PersistentTopic) originTopic, abortedTxnProcessor, AbortedTxnProcessor.SnapshotType.Single); |
| 87 | + }; |
| 88 | + TransactionBufferProvider originalTransactionBufferProvider = pulsar.getTransactionBufferProvider(); |
| 89 | + pulsar.setTransactionBufferProvider(mockTransactionBufferProvider); |
| 90 | + |
| 91 | + // 2. Trigger create topic and assert topic load success. |
| 92 | + CompletableFuture<Optional<Topic>> firstLoad = brokerService.getTopic(tpName, true); |
| 93 | + Awaitility.await().ignoreExceptions().atMost(10, TimeUnit.SECONDS) |
| 94 | + .pollInterval(200, TimeUnit.MILLISECONDS) |
| 95 | + .untilAsserted(() -> { |
| 96 | + assertTrue(firstLoad.isDone()); |
| 97 | + assertFalse(firstLoad.isCompletedExceptionally()); |
| 98 | + }); |
| 99 | + |
| 100 | + // 3. Assert topic removed from cache |
| 101 | + Awaitility.await().ignoreExceptions().atMost(10, TimeUnit.SECONDS) |
| 102 | + .pollInterval(500, TimeUnit.MILLISECONDS) |
| 103 | + .untilAsserted(() -> { |
| 104 | + assertFalse(brokerService.getTopics().containsKey(tpName)); |
| 105 | + }); |
| 106 | + |
| 107 | + // 4. Set txn provider to back |
| 108 | + pulsar.setTransactionBufferProvider(originalTransactionBufferProvider); |
| 109 | + } |
| 110 | + |
| 111 | + public static class MyTopicFactory implements TopicFactory { |
| 112 | + @Override |
| 113 | + public <T extends Topic> T create(String topic, ManagedLedger ledger, BrokerService brokerService, |
| 114 | + Class<T> topicClazz) { |
| 115 | + try { |
| 116 | + if (topicClazz == NonPersistentTopic.class) { |
| 117 | + return (T) new NonPersistentTopic(topic, brokerService); |
| 118 | + } else { |
| 119 | + return (T) new MyPersistentTopic(topic, ledger, brokerService); |
| 120 | + } |
| 121 | + } catch (Exception e) { |
| 122 | + throw new IllegalStateException(e); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void close() throws IOException { |
| 128 | + // No-op |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public static class MyPersistentTopic extends PersistentTopic { |
| 133 | + |
| 134 | + public MyPersistentTopic(String topic, ManagedLedger ledger, BrokerService brokerService) { |
| 135 | + super(topic, ledger, brokerService); |
| 136 | + } |
| 137 | + |
| 138 | + @SneakyThrows |
| 139 | + @Override |
| 140 | + public CompletableFuture<Void> checkDeduplicationStatus() { |
| 141 | + topicInitSuccessSignal.countDown(); |
| 142 | + // Sleep 1s pending txn buffer recover failed and close topic |
| 143 | + Thread.sleep(1000); |
| 144 | + return CompletableFuture.completedFuture(null); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments