|
| 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.service.persistent; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | +import java.util.concurrent.RejectedExecutionException; |
| 26 | +import org.apache.bookkeeper.mledger.ManagedCursor; |
| 27 | +import org.apache.bookkeeper.mledger.ManagedLedger; |
| 28 | +import org.apache.pulsar.broker.testcontext.PulsarTestContext; |
| 29 | +import org.testng.annotations.AfterMethod; |
| 30 | +import org.testng.annotations.BeforeMethod; |
| 31 | +import org.testng.annotations.Test; |
| 32 | + |
| 33 | +@Test(groups = "broker") |
| 34 | +public class PersistentTopicMessageExpiryTest { |
| 35 | + |
| 36 | + private static final int TTL_SECONDS = 60; |
| 37 | + |
| 38 | + private PulsarTestContext context; |
| 39 | + private PersistentTopic topic; |
| 40 | + |
| 41 | + @BeforeMethod(alwaysRun = true) |
| 42 | + public void setUp() throws Exception { |
| 43 | + context = PulsarTestContext.builderForNonStartableContext().build(); |
| 44 | + topic = new PersistentTopic( |
| 45 | + "persistent://public/default/message-expiry", |
| 46 | + context.getBrokerService(), |
| 47 | + mock(ManagedLedger.class), |
| 48 | + mock(MessageDeduplication.class)); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterMethod(alwaysRun = true) |
| 52 | + public void tearDown() throws Exception { |
| 53 | + if (context != null) { |
| 54 | + context.close(); |
| 55 | + context = null; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testSubscriptionFailureDoesNotStopOtherSubscriptions() { |
| 61 | + PersistentSubscription first = mockDurableSubscription("sub-1"); |
| 62 | + PersistentSubscription second = mockDurableSubscription("sub-2"); |
| 63 | + var failure = new RejectedExecutionException("Topic executor is shut down"); |
| 64 | + |
| 65 | + when(first.expireMessagesAsync(TTL_SECONDS)).thenThrow(failure); |
| 66 | + when(second.expireMessagesAsync(TTL_SECONDS)).thenThrow(failure); |
| 67 | + topic.getSubscriptions().put("sub-1", first); |
| 68 | + topic.getSubscriptions().put("sub-2", second); |
| 69 | + |
| 70 | + assertThatCode(() -> topic.checkMessageExpiryWithoutSharedPosition(TTL_SECONDS)) |
| 71 | + .as("Subscription failures should not escape the expiry check") |
| 72 | + .doesNotThrowAnyException(); |
| 73 | + |
| 74 | + verify(first).expireMessagesAsync(TTL_SECONDS); |
| 75 | + verify(second).expireMessagesAsync(TTL_SECONDS); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testReplicatorFailureDoesNotStopOtherReplicators() { |
| 80 | + PersistentReplicator first = mock(PersistentReplicator.class); |
| 81 | + PersistentReplicator second = mock(PersistentReplicator.class); |
| 82 | + var failure = new RejectedExecutionException("Topic executor is shut down"); |
| 83 | + |
| 84 | + when(first.expireMessagesAsync(TTL_SECONDS)).thenThrow(failure); |
| 85 | + when(second.expireMessagesAsync(TTL_SECONDS)).thenThrow(failure); |
| 86 | + topic.getReplicators().put("cluster-1", first); |
| 87 | + topic.getReplicators().put("cluster-2", second); |
| 88 | + |
| 89 | + assertThatCode(() -> topic.checkMessageExpiryWithoutSharedPosition(TTL_SECONDS)) |
| 90 | + .as("Replicator failures should not escape the expiry check") |
| 91 | + .doesNotThrowAnyException(); |
| 92 | + |
| 93 | + verify(first).expireMessagesAsync(TTL_SECONDS); |
| 94 | + verify(second).expireMessagesAsync(TTL_SECONDS); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testShadowReplicatorFailureDoesNotStopOtherShadowReplicators() { |
| 99 | + ShadowReplicator first = mock(ShadowReplicator.class); |
| 100 | + ShadowReplicator second = mock(ShadowReplicator.class); |
| 101 | + var failure = new RejectedExecutionException("Topic executor is shut down"); |
| 102 | + |
| 103 | + when(first.expireMessagesAsync(TTL_SECONDS)).thenThrow(failure); |
| 104 | + when(second.expireMessagesAsync(TTL_SECONDS)).thenThrow(failure); |
| 105 | + topic.getShadowReplicators().put("persistent://public/default/shadow-1", first); |
| 106 | + topic.getShadowReplicators().put("persistent://public/default/shadow-2", second); |
| 107 | + |
| 108 | + assertThatCode(() -> topic.checkMessageExpiryWithoutSharedPosition(TTL_SECONDS)) |
| 109 | + .as("Shadow replicator failures should not escape the expiry check") |
| 110 | + .doesNotThrowAnyException(); |
| 111 | + |
| 112 | + verify(first).expireMessagesAsync(TTL_SECONDS); |
| 113 | + verify(second).expireMessagesAsync(TTL_SECONDS); |
| 114 | + } |
| 115 | + |
| 116 | + private PersistentSubscription mockDurableSubscription(String name) { |
| 117 | + ManagedCursor cursor = mock(ManagedCursor.class); |
| 118 | + when(cursor.isDurable()).thenReturn(true); |
| 119 | + |
| 120 | + PersistentSubscription subscription = mock(PersistentSubscription.class); |
| 121 | + when(subscription.getName()).thenReturn(name); |
| 122 | + when(subscription.getCursor()).thenReturn(cursor); |
| 123 | + return subscription; |
| 124 | + } |
| 125 | +} |