|
| 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.client.api; |
| 20 | + |
| 21 | +import static org.testng.Assert.assertEquals; |
| 22 | +import static org.testng.Assert.assertNotNull; |
| 23 | +import static org.testng.Assert.assertTrue; |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import lombok.extern.slf4j.Slf4j; |
| 27 | +import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl; |
| 28 | +import org.apache.pulsar.broker.BrokerTestUtil; |
| 29 | +import org.apache.pulsar.broker.service.persistent.PersistentTopic; |
| 30 | +import org.awaitility.Awaitility; |
| 31 | +import org.testng.annotations.AfterClass; |
| 32 | +import org.testng.annotations.BeforeClass; |
| 33 | +import org.testng.annotations.Test; |
| 34 | + |
| 35 | +@Test(groups = "broker-api") |
| 36 | +@Slf4j |
| 37 | +public class PersistentTopicTerminateTest extends ProducerConsumerBase { |
| 38 | + |
| 39 | + |
| 40 | + @BeforeClass |
| 41 | + @Override |
| 42 | + protected void setup() throws Exception { |
| 43 | + super.internalSetup(); |
| 44 | + super.producerBaseSetup(); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterClass(alwaysRun = true) |
| 48 | + @Override |
| 49 | + protected void cleanup() throws Exception { |
| 50 | + super.internalCleanup(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testRecoverAfterTerminate() throws Exception { |
| 55 | + final String topicName = BrokerTestUtil.newUniqueName("persistent://public/default/tp"); |
| 56 | + final String subscriptionName = "s1"; |
| 57 | + admin.topics().createNonPartitionedTopic(topicName); |
| 58 | + admin.topics().createSubscription(topicName, subscriptionName, MessageId.earliest); |
| 59 | + |
| 60 | + // Trigger 2 ledgers creation. |
| 61 | + Producer<String> producer = pulsarClient.newProducer(Schema.STRING).topic(topicName).create(); |
| 62 | + producer.send("1"); |
| 63 | + admin.topics().unload(topicName); |
| 64 | + producer.send("2"); |
| 65 | + |
| 66 | + // Terminate topic. |
| 67 | + producer.close(); |
| 68 | + admin.topics().terminateTopic(topicName); |
| 69 | + admin.topics().unload(topicName); |
| 70 | + |
| 71 | + // Verify: consume 2 msgs. |
| 72 | + Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING).topic(topicName) |
| 73 | + .subscriptionName(subscriptionName).subscribe(); |
| 74 | + |
| 75 | + Message<String> msg1 = consumer.receive(2, TimeUnit.SECONDS); |
| 76 | + assertNotNull(msg1); |
| 77 | + assertEquals(msg1.getValue(), "1"); |
| 78 | + Message<String> msg2 = consumer.receive(2, TimeUnit.SECONDS); |
| 79 | + assertNotNull(msg2); |
| 80 | + assertEquals(msg2.getValue(), "2"); |
| 81 | + |
| 82 | + // Verify: the ledgers acked will be cleaned up. |
| 83 | + admin.topics().skipAllMessages(topicName, subscriptionName); |
| 84 | + Awaitility.await().untilAsserted(() -> { |
| 85 | + PersistentTopic persistentTopic = |
| 86 | + (PersistentTopic) pulsar.getBrokerService().getTopic(topicName, false).join().get(); |
| 87 | + ManagedLedgerImpl ml = (ManagedLedgerImpl) persistentTopic.getManagedLedger(); |
| 88 | + CompletableFuture<Void> trimLedgersFuture = new CompletableFuture<>(); |
| 89 | + ml.trimConsumedLedgersInBackground(trimLedgersFuture); |
| 90 | + trimLedgersFuture.join(); |
| 91 | + assertTrue(ml.getLedgersInfo().size() <= 1); |
| 92 | + }); |
| 93 | + |
| 94 | + // Cleanup. |
| 95 | + consumer.close(); |
| 96 | + admin.topics().delete(topicName, false); |
| 97 | + } |
| 98 | +} |
0 commit comments