|
| 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 java.nio.charset.StandardCharsets.UTF_8; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import io.netty.buffer.ByteBuf; |
| 24 | +import io.netty.buffer.Unpooled; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.CompletableFuture; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import org.apache.bookkeeper.mledger.Position; |
| 32 | +import org.apache.bookkeeper.mledger.PositionFactory; |
| 33 | +import org.apache.pulsar.broker.service.SharedPulsarBaseTest; |
| 34 | +import org.apache.pulsar.broker.service.Topic; |
| 35 | +import org.apache.pulsar.broker.service.persistent.PersistentTopic; |
| 36 | +import org.apache.pulsar.common.api.proto.MessageMetadata; |
| 37 | +import org.apache.pulsar.common.protocol.Commands; |
| 38 | +import org.assertj.core.api.SoftAssertions; |
| 39 | +import org.testng.annotations.DataProvider; |
| 40 | +import org.testng.annotations.Test; |
| 41 | + |
| 42 | +@Test(groups = "broker-api") |
| 43 | +public class ChunkedMessageDeduplicationRecoveryTest extends SharedPulsarBaseTest { |
| 44 | + |
| 45 | + @DataProvider |
| 46 | + public Object[][] reloadBetweenChunks() { |
| 47 | + return new Object[][] { |
| 48 | + // Control case for the live publish path; no reload is performed. |
| 49 | + {false, -1, new String[] {"hello-", "world"}}, |
| 50 | + {true, 0, new String[] {"hello-", "world"}}, |
| 51 | + {true, 1, new String[] {"hello-", "middle-", "world"}} |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + @Test(dataProvider = "reloadBetweenChunks", timeOut = 30000) |
| 56 | + public void testIncompleteChunkedMessageSurvivesDeduplicationRecovery( |
| 57 | + boolean reload, int reloadAfterChunk, String[] chunkValues) throws Exception { |
| 58 | + String topic = newTopicName(); |
| 59 | + String producerName = "chunked-producer"; |
| 60 | + long sequenceId = 41; |
| 61 | + int numChunks = chunkValues.length; |
| 62 | + String completeMessage = String.join("", chunkValues); |
| 63 | + admin.namespaces().setDeduplicationStatus(getNamespace(), true); |
| 64 | + admin.topics().createNonPartitionedTopic(topic); |
| 65 | + admin.topics().createSubscription(topic, "test-sub", MessageId.earliest); |
| 66 | + PersistentTopic persistentTopic = (PersistentTopic) getTopic(topic, false).get(10, TimeUnit.SECONDS) |
| 67 | + .orElseThrow(); |
| 68 | + assertThat(persistentTopic.getMessageDeduplication().isEnabled()).isTrue(); |
| 69 | + |
| 70 | + SoftAssertions softly = new SoftAssertions(); |
| 71 | + // Publish through the broker's real persistence path, pausing between protocol chunks. |
| 72 | + for (int chunkId = 0; chunkId < numChunks; chunkId++) { |
| 73 | + if (reload && chunkId == reloadAfterChunk + 1) { |
| 74 | + admin.topics().unload(topic); |
| 75 | + persistentTopic = (PersistentTopic) getTopic(topic, false).get(10, TimeUnit.SECONDS).orElseThrow(); |
| 76 | + softly.assertThat(persistentTopic.getLastPublishedSequenceId(producerName)) |
| 77 | + .as("Recovery must not advance the watermark past an incomplete chunked message") |
| 78 | + .isEqualTo(-1); |
| 79 | + } |
| 80 | + |
| 81 | + Position chunk = publishChunk(persistentTopic, producerName, sequenceId, chunkId, numChunks, |
| 82 | + chunkValues[chunkId], completeMessage); |
| 83 | + softly.assertThat(chunk.getLedgerId()).isGreaterThanOrEqualTo(0); |
| 84 | + if (chunkId < numChunks - 1) { |
| 85 | + softly.assertThat(persistentTopic.getLastPublishedSequenceId(producerName)) |
| 86 | + .as("A persisted prefix does not complete the logical message") |
| 87 | + .isEqualTo(-1); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + try (Producer<String> producer = pulsarClient.newProducer(Schema.STRING).topic(topic) |
| 92 | + .enableBatching(false).create(); |
| 93 | + Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING).topic(topic) |
| 94 | + .subscriptionName("test-sub").subscribe()) { |
| 95 | + producer.send("probe"); |
| 96 | + List<String> received = new ArrayList<>(); |
| 97 | + for (int i = 0; i < 2; i++) { |
| 98 | + Message<String> message = consumer.receive(2, TimeUnit.SECONDS); |
| 99 | + if (message == null) { |
| 100 | + break; |
| 101 | + } |
| 102 | + received.add(message.getValue()); |
| 103 | + } |
| 104 | + softly.assertThat(received) |
| 105 | + .as("The chunked message must remain deliverable when later normal messages are delivered") |
| 106 | + .containsExactly(completeMessage, "probe"); |
| 107 | + } |
| 108 | + softly.assertAll(); |
| 109 | + } |
| 110 | + |
| 111 | + private Position publishChunk(PersistentTopic topic, String producerName, long sequenceId, |
| 112 | + int chunkId, int numChunks, String value, String completeMessage) throws Exception { |
| 113 | + MessageMetadata metadata = new MessageMetadata() |
| 114 | + .setProducerName(producerName) |
| 115 | + .setSequenceId(sequenceId) |
| 116 | + .setPublishTime(System.currentTimeMillis()) |
| 117 | + .setUuid(producerName + "-" + sequenceId) |
| 118 | + .setChunkId(chunkId) |
| 119 | + .setNumChunksFromMsg(numChunks) |
| 120 | + .setTotalChunkMsgSize(completeMessage.getBytes(UTF_8).length); |
| 121 | + ByteBuf payload = Unpooled.copiedBuffer(value, UTF_8); |
| 122 | + ByteBuf data; |
| 123 | + try { |
| 124 | + data = Commands.serializeMetadataAndPayload(Commands.ChecksumType.Crc32c, metadata, payload); |
| 125 | + } finally { |
| 126 | + payload.release(); |
| 127 | + } |
| 128 | + CompletableFuture<Position> persisted = new CompletableFuture<>(); |
| 129 | + try { |
| 130 | + topic.publishMessage(data, new Topic.PublishContext() { |
| 131 | + private final Map<String, Object> properties = new HashMap<>(); |
| 132 | + |
| 133 | + @Override |
| 134 | + public String getProducerName() { |
| 135 | + return producerName; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public long getSequenceId() { |
| 140 | + return sequenceId; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean isChunked() { |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void setProperty(String propertyName, Object value) { |
| 150 | + properties.put(propertyName, value); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public Object getProperty(String propertyName) { |
| 155 | + return properties.get(propertyName); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void completed(Exception exception, long ledgerId, long entryId) { |
| 160 | + if (exception != null) { |
| 161 | + persisted.completeExceptionally(exception); |
| 162 | + } else { |
| 163 | + persisted.complete(PositionFactory.create(ledgerId, entryId)); |
| 164 | + } |
| 165 | + } |
| 166 | + }); |
| 167 | + return persisted.get(10, TimeUnit.SECONDS); |
| 168 | + } finally { |
| 169 | + data.release(); |
| 170 | + } |
| 171 | + } |
| 172 | +} |