You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by zh...@apache.org on 2025/06/27 01:58:09 UTC

(pulsar) branch branch-3.0 updated: [fix][ml] Enhance OpFindNewest to support skip non-recoverable data (#24441)

This is an automated email from the ASF dual-hosted git repository.

zhaocong pushed a commit to branch branch-3.0
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new bd5eed479a3 [fix][ml] Enhance OpFindNewest to support skip non-recoverable data (#24441)
bd5eed479a3 is described below

commit bd5eed479a32520cbc5b1030f0c51e9ba6eb59dd
Author: Cong Zhao <zh...@apache.org>
AuthorDate: Fri Jun 27 09:08:58 2025 +0800

    [fix][ml] Enhance OpFindNewest to support skip non-recoverable data (#24441)
    
    (cherry picked from commit 3e36632e2e49401cb042a690435c91bb808f4147)
---
 .../bookkeeper/mledger/impl/OpFindNewest.java      | 116 ++++++++++++++-
 .../service/PersistentMessageFinderTest.java       | 163 ++++++++++++++++++++-
 .../broker/service/SubscriptionSeekTest.java       |  86 +++++++++++
 3 files changed, 358 insertions(+), 7 deletions(-)

diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/OpFindNewest.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/OpFindNewest.java
index 900af9322c7..d34b199de8e 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/OpFindNewest.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/OpFindNewest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.bookkeeper.mledger.impl;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import com.google.common.collect.Range;
 import java.util.Optional;
 import java.util.function.Predicate;
 import lombok.extern.slf4j.Slf4j;
@@ -44,6 +46,7 @@ class OpFindNewest implements ReadEntryCallback {
     PositionImpl searchPosition;
     long min;
     long max;
+    long mid;
     Position lastMatchedPosition = null;
     State state;
 
@@ -58,6 +61,7 @@ class OpFindNewest implements ReadEntryCallback {
 
         this.min = 0;
         this.max = numberOfEntries;
+        this.mid = mid();
 
         this.searchPosition = startPosition;
         this.state = State.checkFirst;
@@ -113,7 +117,8 @@ class OpFindNewest implements ReadEntryCallback {
             } else {
                 // start binary search
                 state = State.searching;
-                searchPosition = ledger.getPositionAfterN(startPosition, mid(), PositionBound.startExcluded);
+                this.mid = mid();
+                searchPosition = ledger.getPositionAfterN(startPosition, this.mid, PositionBound.startExcluded);
                 find();
             }
             break;
@@ -121,26 +126,129 @@ class OpFindNewest implements ReadEntryCallback {
             if (condition.test(entry)) {
                 // mid - last
                 lastMatchedPosition = position;
-                min = mid();
+                min = mid;
             } else {
                 // start - mid
-                max = mid() - 1;
+                max = mid - 1;
             }
+            this.mid = mid();
 
             if (max <= min) {
                 callback.findEntryComplete(lastMatchedPosition, OpFindNewest.this.ctx);
                 return;
             }
-            searchPosition = ledger.getPositionAfterN(startPosition, mid(), PositionBound.startExcluded);
+            searchPosition = ledger.getPositionAfterN(startPosition, this.mid, PositionBound.startExcluded);
             find();
         }
     }
 
     @Override
     public void readEntryFailed(ManagedLedgerException exception, Object ctx) {
+        if (exception instanceof ManagedLedgerException.NonRecoverableLedgerException
+            && ledger.getConfig().isAutoSkipNonRecoverableData()) {
+            try {
+                log.info("[{}] Ledger {} is not recoverable, skip non-recoverable data, state:{}", ledger.getName(),
+                    searchPosition, state);
+                checkArgument(state == State.checkFirst || state == State.checkLast || state == State.searching);
+                if (state == State.checkFirst) {
+                    // If we failed to read the first entry, try next valid position
+                    PositionImpl nextPosition = findNextValidPosition(searchPosition, exception);
+                    if (nextPosition != null && nextPosition.getEntryId() != -1) {
+                        long numberOfEntries =
+                            ledger.getNumberOfEntries(Range.closedOpen(searchPosition, nextPosition));
+                        searchPosition = nextPosition;
+                        min += numberOfEntries;
+                        find();
+                        return;
+                    }
+                } else if (state == State.checkLast) {
+                    PositionImpl prevPosition = findPreviousValidPosition(searchPosition, exception);
+                    if (prevPosition != null && prevPosition.getEntryId() != -1) {
+                        long numberOfEntries =
+                            ledger.getNumberOfEntries(Range.openClosed(prevPosition, searchPosition));
+                        searchPosition = prevPosition;
+                        max -= numberOfEntries;
+                        find();
+                        return;
+                    }
+                } else if (state == State.searching) {
+                    // In searching state, if we failed to read the mid entry, try next valid position
+                    PositionImpl nextPosition = findNextValidPosition(searchPosition, exception);
+                    if (nextPosition != null && nextPosition.getEntryId() != -1) {
+                        searchPosition = nextPosition;
+                        find();
+                        return;
+                    } else {
+                        // If we can't find next valid position, try previous valid position
+                        PositionImpl prevPosition = findPreviousValidPosition(searchPosition, exception);
+                        if (prevPosition != null && prevPosition.getEntryId() != -1) {
+                            searchPosition = prevPosition;
+                            find();
+                            return;
+                        }
+                    }
+                }
+
+                // If don't find any entry, return the last matched position
+                log.warn("[{}] Failed to find next valid entry. Returning last matched position: {}", ledger.getName(),
+                    lastMatchedPosition);
+                callback.findEntryComplete(lastMatchedPosition, OpFindNewest.this.ctx);
+                return;
+            } catch (Exception e) {
+                callback.findEntryFailed(
+                    new ManagedLedgerException("Failed to skip non-recoverable data during search position", e),
+                    Optional.ofNullable(searchPosition), OpFindNewest.this.ctx);
+                return;
+            }
+        }
+
         callback.findEntryFailed(exception, Optional.ofNullable(searchPosition), OpFindNewest.this.ctx);
     }
 
+    private PositionImpl findPreviousValidPosition(PositionImpl searchPosition, ManagedLedgerException exception) {
+        PositionImpl prevPosition;
+        if (exception instanceof ManagedLedgerException.LedgerNotExistException) {
+            prevPosition =
+                ledger.getPreviousPosition(PositionImpl.get(searchPosition.getLedgerId(), -1L));
+        } else {
+            prevPosition = ledger.getPreviousPosition(searchPosition);
+        }
+        if (prevPosition.getEntryId() != -1) {
+            var minPosition = ledger.getPositionAfterN(startPosition, min, PositionBound.startExcluded);
+            if (minPosition.compareTo(prevPosition) > 0) {
+                // If the previous position is out of the min position, an invalid position is returned
+                prevPosition = null;
+            }
+        }
+        return prevPosition;
+    }
+
+    private PositionImpl findNextValidPosition(PositionImpl searchPosition, Exception exception) {
+        PositionImpl nextPosition = null;
+        if (exception instanceof ManagedLedgerException.LedgerNotExistException) {
+            Long nextLedgerId = ledger.getNextValidLedger(searchPosition.getLedgerId());
+            if (nextLedgerId != null) {
+                Boolean nonEmptyLedger = ledger.getOptionalLedgerInfo(nextLedgerId)
+                    .map(ledgerInfo -> ledgerInfo.getEntries() > 0)
+                    .orElse(false);
+                if (nonEmptyLedger) {
+                    nextPosition = PositionImpl.get(nextLedgerId, 0);
+                }
+            }
+        } else {
+            nextPosition = ledger.getNextValidPosition(searchPosition);
+        }
+
+        if (nextPosition != null) {
+            var maxPosition = ledger.getPositionAfterN(startPosition, max, PositionBound.startExcluded);
+            if (maxPosition.compareTo(nextPosition) < 0) {
+                // If the next position is out of the max position, an invalid position is returned
+                nextPosition = null;
+            }
+        }
+        return nextPosition;
+    }
+
     public void find() {
         if (cursor != null ? cursor.hasMoreEntries(searchPosition) : ledger.hasMoreEntries(searchPosition)) {
             ledger.asyncReadEntry(searchPosition, this, null);
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentMessageFinderTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentMessageFinderTest.java
index aee9225fc8a..7f12e8a4d4c 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentMessageFinderTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentMessageFinderTest.java
@@ -46,6 +46,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.core.MediaType;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.bookkeeper.mledger.AsyncCallbacks;
 import org.apache.bookkeeper.mledger.Entry;
 import org.apache.bookkeeper.mledger.ManagedCursor;
@@ -63,6 +64,9 @@ import org.apache.commons.lang3.tuple.Pair;
 import org.apache.pulsar.broker.service.persistent.PersistentMessageExpiryMonitor;
 import org.apache.pulsar.broker.service.persistent.PersistentMessageFinder;
 import org.apache.pulsar.broker.service.persistent.PersistentSubscription;
+import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.impl.MessageIdImpl;
+import org.apache.pulsar.client.impl.MessageImpl;
 import org.apache.pulsar.client.impl.ResetCursorData;
 import org.apache.pulsar.common.allocator.PulsarByteBufAllocator;
 import org.apache.pulsar.common.api.proto.BrokerEntryMetadata;
@@ -77,6 +81,7 @@ import org.testng.Assert;
 import org.testng.annotations.Test;
 
 @Test(groups = "broker")
+@Slf4j
 public class PersistentMessageFinderTest extends MockedBookKeeperTestCase {
 
     public static byte[] createMessageWrittenToLedger(String msg) {
@@ -140,7 +145,12 @@ public class PersistentMessageFinderTest extends MockedBookKeeperTestCase {
     }
 
     CompletableFuture<Void> findMessage(final Result result, final ManagedCursor c1, final long timestamp) {
-        PersistentMessageFinder messageFinder = new PersistentMessageFinder("topicname", c1, 0);
+        return findMessage(result, c1, timestamp, 0);
+    }
+
+    CompletableFuture<Void> findMessage(final Result result, final ManagedCursor c1, final long timestamp,
+                                        int ledgerCloseTimestampMaxClockSkewMillis) {
+        PersistentMessageFinder messageFinder = new PersistentMessageFinder("topicname", c1, ledgerCloseTimestampMaxClockSkewMillis);
 
         final CompletableFuture<Void> future = new CompletableFuture<>();
         messageFinder.findMessages(timestamp, new AsyncCallbacks.FindEntryCallback() {
@@ -404,7 +414,7 @@ public class PersistentMessageFinderTest extends MockedBookKeeperTestCase {
 
         List<LedgerInfo> ledgers = ledger.getLedgersInfoAsList();
         LedgerInfo lastLedgerInfo = ledgers.get(ledgers.size() - 1);
-        // The `lastLedgerInfo` should be newly opened, and it does not contain any entries. 
+        // The `lastLedgerInfo` should be newly opened, and it does not contain any entries.
         // Please refer to: https://github.com/apache/pulsar/pull/22034
         assertEquals(lastLedgerInfo.getEntries(), 0);
         assertEquals(ledgers.size(), totalEntries / entriesPerLedger + 1);
@@ -422,7 +432,7 @@ public class PersistentMessageFinderTest extends MockedBookKeeperTestCase {
             PositionImpl markDeletePosition = (PositionImpl) c1.getMarkDeletedPosition();
             // The markDeletePosition points to the last entry of the previous ledger in lastLedgerInfo.
             assertEquals(markDeletePosition.getLedgerId(), lastLedgerInfo.getLedgerId() - 1);
-            assertEquals(markDeletePosition.getEntryId(), entriesPerLedger - 1); 
+            assertEquals(markDeletePosition.getEntryId(), entriesPerLedger - 1);
         });
 
         c1.close();
@@ -431,6 +441,153 @@ public class PersistentMessageFinderTest extends MockedBookKeeperTestCase {
 
     }
 
+    public void testFindMessageWithTimestampAutoSkipNonRecoverable() throws Exception {
+
+        final String ledgerAndCursorName = "testFindMessageWithTimestampAutoSkipNonRecoverable";
+        final int entriesPerLedger = 5;
+        final int totalEntries = 50;
+
+        ManagedLedgerConfig config = new ManagedLedgerConfig();
+        config.setRetentionSizeInMB(10);
+        config.setMaxEntriesPerLedger(entriesPerLedger);
+        config.setRetentionTime(1, TimeUnit.HOURS);
+        config.setAutoSkipNonRecoverableData(true);
+        ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open(ledgerAndCursorName, config);
+
+        long initTimeMillis = System.currentTimeMillis();
+        for (int i = 0; i < totalEntries; i++) {
+            ledger.addEntry(createMessageWrittenToLedger("msg" + i, initTimeMillis + i));
+        }
+        // {0,1,2,3,4} (0) 3 x
+        // {5,6,7,8,9} (1) 4
+        // {10,11,12,13,14} (2) 5
+        // {15,16,17,18,19} (3) 6
+        // {20,21,22,23,24} (4) 7 x
+        // {25,26,27,28,29} (5) 8 x
+        // {30,31,32,33,34} (6) 9
+        // {35,36,37,38,39} (7) 10
+        // {40,41,42,43,44} (8) 11
+        // {45,46,47,48,49} (9) 12 x
+        Awaitility.await().untilAsserted(() ->
+                assertEquals(ledger.getState(), ManagedLedgerImpl.State.LedgerOpened));
+
+        List<LedgerInfo> ledgers = ledger.getLedgersInfoAsList();
+        LedgerInfo lastLedgerInfo = ledgers.get(ledgers.size() - 1);
+        // The `lastLedgerInfo` should be newly opened, and it does not contain any entries.
+        // Please refer to: https://github.com/apache/pulsar/pull/22034
+        assertEquals(lastLedgerInfo.getEntries(), 0);
+        assertEquals(ledgers.size(), totalEntries / entriesPerLedger + 1);
+
+        bkc.deleteLedger(ledgers.get(0).getLedgerId());
+        bkc.deleteLedger(ledgers.get(4).getLedgerId());
+        bkc.deleteLedger(ledgers.get(5).getLedgerId());
+        bkc.deleteLedger(ledgers.get(9).getLedgerId());
+
+        MessageId messageId = findMessageIdByPublishTime(initTimeMillis + 17, ledger).join();
+        log.info("messageId: {}", messageId);
+        assertEquals(messageId, new MessageIdImpl(ledgers.get(3).getLedgerId(), 2, -1));
+
+        messageId = findMessageIdByPublishTime(initTimeMillis + 27, ledger).join();
+        log.info("messageId: {}", messageId);
+        assertEquals(messageId, new MessageIdImpl(ledgers.get(4).getLedgerId(), 0, -1));
+
+        messageId = findMessageIdByPublishTime(initTimeMillis + 43, ledger).join();
+        log.info("messageId: {}", messageId);
+        assertEquals(messageId, new MessageIdImpl(ledgers.get(8).getLedgerId(), 3, -1));
+
+        messageId = findMessageIdByPublishTime(initTimeMillis + 48, ledger).join();
+        log.info("messageId: {}", messageId);
+        assertEquals(messageId, new MessageIdImpl(ledgers.get(9).getLedgerId(), 0, -1));
+
+        ledger.close();
+        factory.shutdown();
+    }
+
+    public void testFindMessageByCursorWithTimestampAutoSkipNonRecoverable() throws Exception {
+
+        final String ledgerAndCursorName = "testFindMessageByCursorWithTimestampAutoSkipNonRecoverable";
+        final int entriesPerLedger = 5;
+        final int totalEntries = 50;
+
+        ManagedLedgerConfig config = new ManagedLedgerConfig();
+        config.setRetentionSizeInMB(10);
+        config.setMaxEntriesPerLedger(entriesPerLedger);
+        config.setRetentionTime(1, TimeUnit.HOURS);
+        config.setAutoSkipNonRecoverableData(true);
+        ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open(ledgerAndCursorName, config);
+        ManagedCursorImpl cursor = (ManagedCursorImpl) ledger.openCursor(ledgerAndCursorName);
+
+        long initTimeMillis = System.currentTimeMillis();
+        for (int i = 0; i < totalEntries; i++) {
+            ledger.addEntry(createMessageWrittenToLedger("msg" + i, initTimeMillis + i));
+        }
+        // {0,1,2,3,4} (0) 3 x
+        // {5,6,7,8,9} (1) 4
+        // {10,11,12,13,14} (2) 5
+        // {15,16,17,18,19} (3) 6
+        // {20,21,22,23,24} (4) 7 x
+        // {25,26,27,28,29} (5) 8 x
+        // {30,31,32,33,34} (6) 9
+        // {35,36,37,38,39} (7) 10
+        // {40,41,42,43,44} (8) 11
+        // {45,46,47,48,49} (9) 12 x
+        Awaitility.await().untilAsserted(() ->
+                assertEquals(ledger.getState(), ManagedLedgerImpl.State.LedgerOpened));
+
+        List<LedgerInfo> ledgers = ledger.getLedgersInfoAsList();
+        LedgerInfo lastLedgerInfo = ledgers.get(ledgers.size() - 1);
+        // The `lastLedgerInfo` should be newly opened, and it does not contain any entries.
+        // Please refer to: https://github.com/apache/pulsar/pull/22034
+        assertEquals(lastLedgerInfo.getEntries(), 0);
+        assertEquals(ledgers.size(), totalEntries / entriesPerLedger + 1);
+
+        bkc.deleteLedger(ledgers.get(0).getLedgerId());
+        bkc.deleteLedger(ledgers.get(4).getLedgerId());
+        bkc.deleteLedger(ledgers.get(5).getLedgerId());
+        bkc.deleteLedger(ledgers.get(9).getLedgerId());
+        Result result = new Result();
+
+        findMessage(result, cursor, initTimeMillis + 17, -1).join();
+        log.info("position: {}", result.position);
+        assertNull(result.exception);
+        assertEquals(result.position, PositionImpl.get(ledgers.get(3).getLedgerId(), 1));
+
+        result = new Result();
+        findMessage(result, cursor, initTimeMillis + 27, -1).join();
+        log.info("position: {}", result.position);
+        assertNull(result.exception);
+        assertEquals(result.position, PositionImpl.get(ledgers.get(3).getLedgerId(), 4));
+
+        result = new Result();
+        findMessage(result, cursor, initTimeMillis + 43, -1).join();
+        log.info("position: {}", result.position);
+        assertNull(result.exception);
+        assertEquals(result.position, PositionImpl.get(ledgers.get(8).getLedgerId(), 2));
+
+        ledger.close();
+        factory.shutdown();
+    }
+
+    private CompletableFuture<MessageId> findMessageIdByPublishTime(long timestamp, ManagedLedger managedLedger) {
+        return managedLedger.asyncFindPosition(entry -> {
+            try {
+                long entryTimestamp = Commands.getEntryTimestamp(entry.getDataBuffer());
+                return MessageImpl.isEntryPublishedEarlierThan(entryTimestamp, timestamp);
+            } catch (Exception e) {
+                log.error("Error deserializing message for message position find", e);
+            } finally {
+                entry.release();
+            }
+            return false;
+        }).thenApply(position -> {
+            if (position == null) {
+                return null;
+            } else {
+                return new MessageIdImpl(position.getLedgerId(), position.getEntryId(), -1);
+            }
+        });
+    }
+
     @Test
     public void testIncorrectClientClock() throws Exception {
         final String ledgerAndCursorName = "testIncorrectClientClock";
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SubscriptionSeekTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SubscriptionSeekTest.java
index 2607c0022ea..fb3f0dc4302 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SubscriptionSeekTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/SubscriptionSeekTest.java
@@ -30,6 +30,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -87,6 +88,7 @@ public class SubscriptionSeekTest extends BrokerTestBase {
         conf.setManagedLedgerMaxEntriesPerLedger(10);
         conf.setDefaultRetentionSizeInMB(100);
         conf.setDefaultRetentionTimeInMinutes(100);
+        conf.setAutoSkipNonRecoverableData(true);
         super.baseSetup();
         conf.setAcknowledgmentAtBatchIndexLevelEnabled(true);
     }
@@ -563,6 +565,90 @@ public class SubscriptionSeekTest extends BrokerTestBase {
         }
     }
 
+    @Test(timeOut = 30_000)
+    public void testSeekByTimestampWithSkipNonRecoverableData() throws Exception {
+        String topicName = "persistent://prop/use/ns-abc/testSeekByTimestampWithSkipNonRecoverableData";
+        admin.topics().createNonPartitionedTopic(topicName);
+        admin.topics().createSubscription(topicName, "my-sub", MessageId.earliest);
+
+        @Cleanup
+        Producer<String> producer =
+            pulsarClient.newProducer(Schema.STRING).topic(topicName).enableBatching(false).create();
+        for (int i = 0; i < 55; i++) {
+            producer.send(("message-" + i));
+            Thread.sleep(10);
+        }
+
+        Map<Long, MessageIdImpl> timestampToMessageId = new HashMap<>();
+        List<Long> ledgerIds = new ArrayList<>();
+        @Cleanup
+        Reader<String> reader =
+            pulsarClient.newReader(Schema.STRING).topic(topicName).startMessageId(MessageId.earliest).create();
+        while (reader.hasMessageAvailable()) {
+            Message<String> message = reader.readNext();
+            log.info("message: {} ----- {}", message.getMessageId(), message.getPublishTime());
+            timestampToMessageId.put(message.getPublishTime(), (MessageIdImpl) message.getMessageId());
+            long ledgerId = ((MessageIdImpl) message.getMessageId()).getLedgerId();
+            if (!ledgerIds.contains(ledgerId)) {
+                ledgerIds.add(ledgerId);
+            }
+        }
+
+        Assert.assertEquals(timestampToMessageId.size(), 55);
+
+        LinkedHashSet<Long> deletedLedgerIds = new LinkedHashSet<>();
+        deletedLedgerIds.add(ledgerIds.get(0));
+        deletedLedgerIds.add(ledgerIds.get(ledgerIds.size() - 1));
+        int mid = ledgerIds.size() / 2;
+        deletedLedgerIds.add(ledgerIds.get(mid));
+
+        for (Long deletedLedgerId : deletedLedgerIds) {
+            pulsar.getBookKeeperClient().deleteLedger(deletedLedgerId);
+            log.info("delete ledger: {}", deletedLedgerId);
+        }
+
+        admin.topics().unload(topicName);
+
+        @Cleanup
+        org.apache.pulsar.client.api.Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING)
+            .receiverQueueSize(0)
+            .topic(topicName).subscriptionName("my-sub")
+            .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).subscribe();
+        long[] timestamps = timestampToMessageId.keySet().stream().mapToLong(Long::longValue).toArray();
+
+        Arrays.sort(timestamps);
+
+        timestampToMessageId.values().removeIf(messageId -> deletedLedgerIds.contains(messageId.getLedgerId()));
+
+        final int lastNonRecoverableEntryNums = 5;
+
+        for (int i = 0; i < timestamps.length - lastNonRecoverableEntryNums; i++) {
+            MessageIdImpl nextValidMessageId = timestampToMessageId.get(timestamps[i]);
+            int l = i;
+            while (nextValidMessageId == null) {
+                nextValidMessageId = timestampToMessageId.get(timestamps[l++]);
+            }
+
+            consumer.seek(timestamps[i]);
+            Message<String> receive = consumer.receive();
+
+            MessageIdImpl msgId = (MessageIdImpl) receive.getMessageId();
+            Assert.assertEquals(msgId.getLedgerId(), nextValidMessageId.getLedgerId());
+            Assert.assertEquals(msgId.getEntryId(), nextValidMessageId.getEntryId());
+        }
+
+        MessageIdImpl lastMessageId = (MessageIdImpl) producer.send(("message-last"));
+
+        for (int i = timestamps.length - lastNonRecoverableEntryNums; i < timestamps.length; i++) {
+            consumer.seek(timestamps[i]);
+            Message<String> receive = consumer.receive();
+
+            MessageIdImpl msgId = (MessageIdImpl) receive.getMessageId();
+            Assert.assertEquals(msgId.getLedgerId(), lastMessageId.getLedgerId());
+            Assert.assertEquals(msgId.getEntryId(), lastMessageId.getEntryId());
+        }
+    }
+
     @Test(timeOut = 30_000)
     public void testSeekByTimestampWithLedgerTrim() throws Exception {
         String topicName = "persistent://prop/use/ns-abc/testSeekByTimestampWithLedgerTrim";