sijie commented on a change in pull request #9083:
URL: https://github.com/apache/pulsar/pull/9083#discussion_r549781036



##########
File path: 
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/OpFindNewest.java
##########
@@ -92,10 +94,14 @@ public void readEntryComplete(Entry entry, Object ctx) {
                 return;
             } else {
                 lastMatchedPosition = position;
-
                 // check last entry
                 state = State.checkLast;
+                PositionImpl lastPosition = ledger.getLastPosition();
                 searchPosition = ledger.getPositionAfterN(searchPosition, max, 
PositionBound.startExcluded);
+                if (lastPosition.compareTo(searchPosition) < 0) {
+                    log.debug("first position {} matches, last should be {}, 
but moving to lastPos {}", position, searchPosition, lastPosition);

Review comment:
       add this to a `if (log.isDebugEnabled())` block.

##########
File path: 
managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
##########
@@ -2140,6 +2141,28 @@ void testIndividuallyDeletedMessages3(boolean 
useOpenRangeSet) throws Exception
         assertTrue(c1.isIndividuallyDeletedEntriesEmpty());
     }
 
+    @Test(timeOut = 20000)
+    void testFindNewestMatchingAfterLedgerRollover() throws Exception {
+        ManagedLedgerImpl ledger = (ManagedLedgerImpl) 
factory.open("my_test_ledger");
+        ManagedCursorImpl c1 = (ManagedCursorImpl) ledger.openCursor("c1");
+        ledger.addEntry("expired".getBytes(Encoding));
+        ledger.addEntry("expired".getBytes(Encoding));
+        ledger.addEntry("expired".getBytes(Encoding));
+        ledger.addEntry("expired".getBytes(Encoding));
+        Position last = ledger.addEntry("expired".getBytes(Encoding));
+
+        // roll a new ledger
+        int numLedgersBefore = ledger.getLedgersInfo().size();
+        ledger.getConfig().setMaxEntriesPerLedger(1);
+        ledger.rollCurrentLedgerIfFull();
+        Awaitility.await().atMost(20, TimeUnit.SECONDS)
+                .until(() -> ledger.getLedgersInfo().size() > 
numLedgersBefore);
+
+        assertEquals(last,
+                c1.findNewestMatching(entry -> 
Arrays.equals(entry.getDataAndRelease(), "expired".getBytes(Encoding))));

Review comment:
       This test doesn't make any sense to me. You are adding 5 entries with 
the same content. 

##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MessageTTLTest.java
##########
@@ -0,0 +1,147 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.service;
+
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.awaitility.Awaitility;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class MessageTTLTest extends BrokerTestBase {
+
+    private static final Logger log = 
LoggerFactory.getLogger(MessageTTLTest.class);
+    @BeforeClass
+    @Override
+    protected void setup() throws Exception {
+        this.conf.setTtlDurationDefaultInSeconds(1);
+        this.conf.setBrokerDeleteInactiveTopicsEnabled(false);
+        super.baseSetup();
+    }
+
+    @AfterClass(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    @Test
+    public void testMessageExpiryAfterTopicUnload() throws Exception {
+        int numMsgs = 50;
+        final String topicName = "persistent://prop/ns-abc/testttl";
+        final String subscriptionName = "ttl-sub-1";
+        
+        pulsarClient.newConsumer()
+                .topic(topicName).subscriptionName(subscriptionName)
+                .receiverQueueSize(1) // this makes the test easier and 
predictable
+                .subscribe()
+                .close();
+        
+        Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
+                .enableBatching(false) // this makes the test easier and 
predictable
+                .create();
+       
+        List<CompletableFuture<MessageId>> sendFutureList = 
Lists.newArrayList();
+        for (int i = 0; i < numMsgs; i++) {
+            byte[] message = ("my-message-" + i).getBytes();
+            sendFutureList.add(producer.sendAsync(message));
+        }
+        FutureUtil.waitForAll(sendFutureList).get();
+        producer.close();
+        
+        // unload a reload the topic
+        // this action created a new ledger
+        // having a managed ledger with more than one
+        // ledger should not impact message expiration
+        admin.topics().unload(topicName);        
+        admin.topics().getStats(topicName);
+       
+        AbstractTopic topic = (AbstractTopic) 
pulsar.getBrokerService().getTopicReference(topicName).get();
+        Thread.sleep(this.conf.getTtlDurationDefaultInSeconds() * 2000);
+        log.info("***** run message expiry now");
+        this.runMessageExpiryCheck();
+        
+        Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topicName)
+                .subscriptionName(subscriptionName)
+                .receiverQueueSize(1) // this makes the test easier and 
predictable
+                .subscribe();
+        Message<byte[]> msg = consumer.receive(10, 
java.util.concurrent.TimeUnit.SECONDS);

Review comment:
       This doesn't make any sense to me. It will result in the test waiting 
for 10 seconds. 
   
   A deterministic approach is using `getLastMessageId` to check if the 
messages are expired.

##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MessageTTLTest.java
##########
@@ -0,0 +1,147 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.service;
+
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.awaitility.Awaitility;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class MessageTTLTest extends BrokerTestBase {
+
+    private static final Logger log = 
LoggerFactory.getLogger(MessageTTLTest.class);
+    @BeforeClass
+    @Override
+    protected void setup() throws Exception {
+        this.conf.setTtlDurationDefaultInSeconds(1);
+        this.conf.setBrokerDeleteInactiveTopicsEnabled(false);
+        super.baseSetup();
+    }
+
+    @AfterClass(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    @Test
+    public void testMessageExpiryAfterTopicUnload() throws Exception {
+        int numMsgs = 50;
+        final String topicName = "persistent://prop/ns-abc/testttl";
+        final String subscriptionName = "ttl-sub-1";
+        
+        pulsarClient.newConsumer()
+                .topic(topicName).subscriptionName(subscriptionName)
+                .receiverQueueSize(1) // this makes the test easier and 
predictable
+                .subscribe()
+                .close();
+        
+        Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
+                .enableBatching(false) // this makes the test easier and 
predictable
+                .create();
+       
+        List<CompletableFuture<MessageId>> sendFutureList = 
Lists.newArrayList();
+        for (int i = 0; i < numMsgs; i++) {
+            byte[] message = ("my-message-" + i).getBytes();
+            sendFutureList.add(producer.sendAsync(message));
+        }
+        FutureUtil.waitForAll(sendFutureList).get();
+        producer.close();
+        
+        // unload a reload the topic
+        // this action created a new ledger
+        // having a managed ledger with more than one
+        // ledger should not impact message expiration
+        admin.topics().unload(topicName);        
+        admin.topics().getStats(topicName);
+       
+        AbstractTopic topic = (AbstractTopic) 
pulsar.getBrokerService().getTopicReference(topicName).get();
+        Thread.sleep(this.conf.getTtlDurationDefaultInSeconds() * 2000);
+        log.info("***** run message expiry now");
+        this.runMessageExpiryCheck();
+        
+        Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topicName)
+                .subscriptionName(subscriptionName)
+                .receiverQueueSize(1) // this makes the test easier and 
predictable

Review comment:
       How does this make the test easier and predictable?

##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MessageTTLTest.java
##########
@@ -0,0 +1,147 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.service;
+
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.awaitility.Awaitility;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class MessageTTLTest extends BrokerTestBase {
+
+    private static final Logger log = 
LoggerFactory.getLogger(MessageTTLTest.class);
+    @BeforeClass
+    @Override
+    protected void setup() throws Exception {
+        this.conf.setTtlDurationDefaultInSeconds(1);
+        this.conf.setBrokerDeleteInactiveTopicsEnabled(false);
+        super.baseSetup();
+    }
+
+    @AfterClass(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    @Test
+    public void testMessageExpiryAfterTopicUnload() throws Exception {
+        int numMsgs = 50;
+        final String topicName = "persistent://prop/ns-abc/testttl";
+        final String subscriptionName = "ttl-sub-1";
+        
+        pulsarClient.newConsumer()
+                .topic(topicName).subscriptionName(subscriptionName)
+                .receiverQueueSize(1) // this makes the test easier and 
predictable
+                .subscribe()
+                .close();
+        
+        Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
+                .enableBatching(false) // this makes the test easier and 
predictable
+                .create();
+       
+        List<CompletableFuture<MessageId>> sendFutureList = 
Lists.newArrayList();
+        for (int i = 0; i < numMsgs; i++) {
+            byte[] message = ("my-message-" + i).getBytes();
+            sendFutureList.add(producer.sendAsync(message));
+        }
+        FutureUtil.waitForAll(sendFutureList).get();
+        producer.close();
+        
+        // unload a reload the topic
+        // this action created a new ledger
+        // having a managed ledger with more than one
+        // ledger should not impact message expiration
+        admin.topics().unload(topicName);        
+        admin.topics().getStats(topicName);
+       
+        AbstractTopic topic = (AbstractTopic) 
pulsar.getBrokerService().getTopicReference(topicName).get();
+        Thread.sleep(this.conf.getTtlDurationDefaultInSeconds() * 2000);

Review comment:
       Can we avoid using `Thread.sleep`?

##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MessageTTLTest.java
##########
@@ -0,0 +1,147 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.service;
+
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.awaitility.Awaitility;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class MessageTTLTest extends BrokerTestBase {
+
+    private static final Logger log = 
LoggerFactory.getLogger(MessageTTLTest.class);
+    @BeforeClass
+    @Override
+    protected void setup() throws Exception {
+        this.conf.setTtlDurationDefaultInSeconds(1);
+        this.conf.setBrokerDeleteInactiveTopicsEnabled(false);
+        super.baseSetup();
+    }
+
+    @AfterClass(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    @Test
+    public void testMessageExpiryAfterTopicUnload() throws Exception {
+        int numMsgs = 50;
+        final String topicName = "persistent://prop/ns-abc/testttl";
+        final String subscriptionName = "ttl-sub-1";
+        
+        pulsarClient.newConsumer()
+                .topic(topicName).subscriptionName(subscriptionName)
+                .receiverQueueSize(1) // this makes the test easier and 
predictable

Review comment:
       You just use this consumer to create a subscription. Why setting 
`receiverQueueSize` makes the test easier and predictable?

##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/MessageTTLTest.java
##########
@@ -0,0 +1,147 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.service;
+
+
+import com.google.common.collect.Lists;
+
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import org.apache.pulsar.client.api.Consumer;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.awaitility.Awaitility;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+public class MessageTTLTest extends BrokerTestBase {
+
+    private static final Logger log = 
LoggerFactory.getLogger(MessageTTLTest.class);
+    @BeforeClass
+    @Override
+    protected void setup() throws Exception {
+        this.conf.setTtlDurationDefaultInSeconds(1);
+        this.conf.setBrokerDeleteInactiveTopicsEnabled(false);
+        super.baseSetup();
+    }
+
+    @AfterClass(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    @Test
+    public void testMessageExpiryAfterTopicUnload() throws Exception {
+        int numMsgs = 50;
+        final String topicName = "persistent://prop/ns-abc/testttl";
+        final String subscriptionName = "ttl-sub-1";
+        
+        pulsarClient.newConsumer()
+                .topic(topicName).subscriptionName(subscriptionName)
+                .receiverQueueSize(1) // this makes the test easier and 
predictable
+                .subscribe()
+                .close();
+        
+        Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
+                .enableBatching(false) // this makes the test easier and 
predictable
+                .create();
+       
+        List<CompletableFuture<MessageId>> sendFutureList = 
Lists.newArrayList();
+        for (int i = 0; i < numMsgs; i++) {
+            byte[] message = ("my-message-" + i).getBytes();
+            sendFutureList.add(producer.sendAsync(message));
+        }
+        FutureUtil.waitForAll(sendFutureList).get();
+        producer.close();
+        
+        // unload a reload the topic
+        // this action created a new ledger
+        // having a managed ledger with more than one
+        // ledger should not impact message expiration
+        admin.topics().unload(topicName);        
+        admin.topics().getStats(topicName);
+       
+        AbstractTopic topic = (AbstractTopic) 
pulsar.getBrokerService().getTopicReference(topicName).get();
+        Thread.sleep(this.conf.getTtlDurationDefaultInSeconds() * 2000);
+        log.info("***** run message expiry now");
+        this.runMessageExpiryCheck();
+        
+        Consumer<byte[]> consumer = pulsarClient.newConsumer()
+                .topic(topicName)
+                .subscriptionName(subscriptionName)
+                .receiverQueueSize(1) // this makes the test easier and 
predictable
+                .subscribe();
+        Message<byte[]> msg = consumer.receive(10, 
java.util.concurrent.TimeUnit.SECONDS);
+        assertNull(msg);
+        consumer.close();
+    }
+
+    
+    @Test
+    public void testStandardMessageExpiry() throws Exception {
+        int numMsgs = 50;
+        final String topicName = "persistent://prop/ns-abc/testttl";
+        final String subscriptionName = "ttl-sub-1";
+        Consumer<byte[]> consumer = 
pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName)
+                .subscriptionType(SubscriptionType.Key_Shared) // this has not 
effect, Exclusive mode works as well
+                .receiverQueueSize(1) // this makes the test easier and 
predictable
+                .subscribe();
+        
+        Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName)
+                .enableBatching(false) // this makes the test easier and 
predictable
+                .create();
+       
+        List<CompletableFuture<MessageId>> sendFutureList = 
Lists.newArrayList();
+        for (int i = 0; i < numMsgs; i++) {
+            byte[] message = ("my-message-" + i).getBytes();
+            sendFutureList.add(producer.sendAsync(message));
+        }
+        FutureUtil.waitForAll(sendFutureList).get();
+        producer.close();
+        
+        Message<byte[]> msg = consumer.receive(10, 
java.util.concurrent.TimeUnit.SECONDS);
+        assertNotNull(msg);
+        consumer.acknowledge(msg);
+       
+        AbstractTopic topic = (AbstractTopic) 
pulsar.getBrokerService().getTopicReference(topicName).get();
+        Thread.sleep(this.conf.getTtlDurationDefaultInSeconds() * 2000);
+        this.runMessageExpiryCheck();
+        
+        Message<byte[]> msg2 = consumer.receive(1, 
java.util.concurrent.TimeUnit.SECONDS);
+        // the consumer prefetched a message (or a batch of messages in case 
of enableBatching(true))
+        assertNotNull(msg);
+        consumer.acknowledge(msg2);
+        // all messages expired, so we expect to see a null here
+        Message<byte[]> msg3 = consumer.receive(1, 
java.util.concurrent.TimeUnit.SECONDS);

Review comment:
       Same comment as above.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to