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



##########
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:
       I cannot do it, because with this sleep all of the messages will be 
considered "expired"
   
   AKAIF there is no framework/tool in Pulsar Codebase to simulate wall clock 
time, if we want to do something better we will have to introduce it.
   Probably it is overkill for this patch.
   
   In this case we are not "waiting" for something, we just have to let the 
time pass (so Awaitility won't help).
   

##########
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:
       because I want to see that the Consumer pre-fetched one message, and so 
it can "see" an expired message,
   this is what happens and I want to test it in order to prevent behaviour 
changes in the future.

##########
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:
       it is only a copy/paste from the test below. I will drop this line, it 
does not matter

##########
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:
       it is the same thing that happens with "expired" messages.
   
   If you have all of the messages "expired" the condition evaluates to true 
for every message ("message is expired"), and in that case findNewestMatching 
must return the last message in the topic.
    

##########
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:
       I am not sure I understand, getLastMessageId is returning a non-null 
MessageId here




----------------------------------------------------------------
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