|
| 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.testng.Assert.assertEquals; |
| 22 | +import static org.testng.Assert.assertTrue; |
| 23 | +import static org.testng.Assert.fail; |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 26 | +import java.util.concurrent.atomic.AtomicInteger; |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
| 28 | +import org.apache.bookkeeper.mledger.Position; |
| 29 | +import org.apache.pulsar.broker.BrokerTestUtil; |
| 30 | +import org.apache.pulsar.client.admin.PulsarAdminException; |
| 31 | +import org.apache.pulsar.client.api.MessageId; |
| 32 | +import org.apache.pulsar.client.api.ProducerConsumerBase; |
| 33 | +import org.apache.pulsar.client.api.Schema; |
| 34 | +import org.apache.pulsar.common.naming.TopicName; |
| 35 | +import org.apache.pulsar.common.util.FutureUtil; |
| 36 | +import org.apache.pulsar.compaction.Compactor; |
| 37 | +import org.apache.zookeeper.MockZooKeeper; |
| 38 | +import org.awaitility.Awaitility; |
| 39 | +import org.awaitility.reflect.WhiteboxImpl; |
| 40 | +import org.testng.annotations.AfterClass; |
| 41 | +import org.testng.annotations.BeforeClass; |
| 42 | +import org.testng.annotations.Test; |
| 43 | + |
| 44 | +@Test(groups = "broker") |
| 45 | +public class CompactionConcurrencyTest extends ProducerConsumerBase { |
| 46 | + // don't make this over 2000ms, otherwise the test will be flaky due to ZKSessionWatcher |
| 47 | + static final int DELETE_OPERATION_DELAY_MS = 1900; |
| 48 | + |
| 49 | + @BeforeClass |
| 50 | + @Override |
| 51 | + protected void setup() throws Exception { |
| 52 | + super.internalSetup(); |
| 53 | + super.producerBaseSetup(); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterClass |
| 57 | + @Override |
| 58 | + protected void cleanup() throws Exception { |
| 59 | + super.internalCleanup(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void doInitConf() throws Exception { |
| 64 | + super.doInitConf(); |
| 65 | + // Disable the scheduled task: compaction. |
| 66 | + conf.setBrokerServiceCompactionMonitorIntervalInSeconds(Integer.MAX_VALUE); |
| 67 | + // Disable the scheduled task: retention. |
| 68 | + conf.setRetentionCheckIntervalInSeconds(Integer.MAX_VALUE); |
| 69 | + } |
| 70 | + |
| 71 | + private void triggerCompactionAndWait(String topicName) throws Exception { |
| 72 | + PersistentTopic persistentTopic = |
| 73 | + (PersistentTopic) pulsar.getBrokerService().getTopic(topicName, false).get().get(); |
| 74 | + persistentTopic.triggerCompaction(); |
| 75 | + Awaitility.await().untilAsserted(() -> { |
| 76 | + Position lastConfirmPos = persistentTopic.getManagedLedger().getLastConfirmedEntry(); |
| 77 | + Position markDeletePos = persistentTopic |
| 78 | + .getSubscription(Compactor.COMPACTION_SUBSCRIPTION).getCursor().getMarkDeletedPosition(); |
| 79 | + assertEquals(markDeletePos.getLedgerId(), lastConfirmPos.getLedgerId()); |
| 80 | + assertEquals(markDeletePos.getEntryId(), lastConfirmPos.getEntryId()); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testDisableCompactionConcurrently() throws Exception { |
| 86 | + String topicName = "persistent://public/default/" + BrokerTestUtil.newUniqueName("tp"); |
| 87 | + admin.topics().createNonPartitionedTopic(topicName); |
| 88 | + admin.topicPolicies().setCompactionThreshold(topicName, 1); |
| 89 | + admin.topics().createSubscription(topicName, "s1", MessageId.earliest); |
| 90 | + var producer = pulsarClient.newProducer(Schema.STRING).topic(topicName).enableBatching(false).create(); |
| 91 | + producer.newMessage().key("k0").value("v0").send(); |
| 92 | + triggerCompactionAndWait(topicName); |
| 93 | + admin.topics().deleteSubscription(topicName, "s1"); |
| 94 | + PersistentTopic persistentTopic = |
| 95 | + (PersistentTopic) pulsar.getBrokerService().getTopic(topicName, false).get().get(); |
| 96 | + AtomicBoolean disablingCompaction = persistentTopic.disablingCompaction; |
| 97 | + |
| 98 | + // Disable compaction. |
| 99 | + // Inject a delay when the first time of deleting cursor. |
| 100 | + AtomicInteger times = new AtomicInteger(); |
| 101 | + String cursorPath = String.format("/managed-ledgers/%s/__compaction", |
| 102 | + TopicName.get(topicName).getPersistenceNamingEncoding()); |
| 103 | + admin.topicPolicies().removeCompactionThreshold(topicName); |
| 104 | + mockZooKeeper.delay(DELETE_OPERATION_DELAY_MS, (op, path) -> { |
| 105 | + return op == MockZooKeeper.Op.DELETE && cursorPath.equals(path) && times.incrementAndGet() == 1; |
| 106 | + }); |
| 107 | + mockZooKeeperGlobal.delay(DELETE_OPERATION_DELAY_MS, (op, path) -> { |
| 108 | + return op == MockZooKeeper.Op.DELETE && cursorPath.equals(path) && times.incrementAndGet() == 1; |
| 109 | + }); |
| 110 | + AtomicReference<CompletableFuture<Void>> f1 = new AtomicReference<CompletableFuture<Void>>(); |
| 111 | + AtomicReference<CompletableFuture<Void>> f2 = new AtomicReference<CompletableFuture<Void>>(); |
| 112 | + new Thread(() -> { |
| 113 | + f1.set(admin.topics().deleteSubscriptionAsync(topicName, "__compaction")); |
| 114 | + }).start(); |
| 115 | + new Thread(() -> { |
| 116 | + f2.set(admin.topics().deleteSubscriptionAsync(topicName, "__compaction")); |
| 117 | + }).start(); |
| 118 | + |
| 119 | + // Verify: the next compaction will be skipped. |
| 120 | + Awaitility.await().untilAsserted(() -> { |
| 121 | + assertTrue(disablingCompaction.get()); |
| 122 | + }); |
| 123 | + producer.newMessage().key("k1").value("v1").send(); |
| 124 | + producer.newMessage().key("k2").value("v2").send(); |
| 125 | + CompletableFuture<Long> currentCompaction1 = persistentTopic.currentCompaction; |
| 126 | + WhiteboxImpl.getInternalState(persistentTopic, "currentCompaction"); |
| 127 | + persistentTopic.triggerCompaction(); |
| 128 | + CompletableFuture<Long> currentCompaction2 = persistentTopic.currentCompaction; |
| 129 | + assertTrue(currentCompaction1 == currentCompaction2); |
| 130 | + |
| 131 | + // Verify: one of the requests should fail. |
| 132 | + Awaitility.await().untilAsserted(() -> { |
| 133 | + assertTrue(f1.get() != null); |
| 134 | + assertTrue(f2.get() != null); |
| 135 | + assertTrue(f1.get().isDone()); |
| 136 | + assertTrue(f2.get().isDone()); |
| 137 | + assertTrue(f1.get().isCompletedExceptionally() || f2.get().isCompletedExceptionally()); |
| 138 | + assertTrue(!f1.get().isCompletedExceptionally() || !f2.get().isCompletedExceptionally()); |
| 139 | + }); |
| 140 | + try { |
| 141 | + f1.get().join(); |
| 142 | + f2.get().join(); |
| 143 | + fail("Should fail"); |
| 144 | + } catch (Exception ex) { |
| 145 | + Throwable actEx = FutureUtil.unwrapCompletionException(ex); |
| 146 | + assertTrue(actEx instanceof PulsarAdminException.PreconditionFailedException); |
| 147 | + } |
| 148 | + |
| 149 | + // cleanup. |
| 150 | + producer.close(); |
| 151 | + admin.topics().delete(topicName, false); |
| 152 | + } |
| 153 | +} |
0 commit comments