Skip to content

Commit f6631be

Browse files
authored
[fix][broker]Fix failed consumption after loaded up a terminated topic (#24063)
1 parent a412a75 commit f6631be

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java

+3
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ public void operationComplete(ManagedLedgerInfo mlInfo, Stat stat) {
429429
log.debug("[{}] Opened ledger {}: {}", name, id, BKException.getMessage(rc));
430430
}
431431
if (rc == BKException.Code.OK) {
432+
if (State.Terminated.equals(state)) {
433+
currentLedger = lh;
434+
}
432435
LedgerInfo info = LedgerInfo.newBuilder().setLedgerId(id)
433436
.setEntries(lh.getLastAddConfirmed() + 1).setSize(lh.getLength())
434437
.setTimestamp(clock.millis()).build();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)