From 0f135ad14bf8052c1af30712e570bbae4b6b8ab9 Mon Sep 17 00:00:00 2001 From: gavingaozhangmin Date: Wed, 8 Feb 2023 09:54:42 +0800 Subject: [PATCH] Fix getLastEntryInLedgerInternal --- .../bookkeeper/bookie/storage/ldb/EntryLocationIndex.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/EntryLocationIndex.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/EntryLocationIndex.java index 3f6d1ae55b2..124ced92345 100644 --- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/EntryLocationIndex.java +++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/EntryLocationIndex.java @@ -114,6 +114,14 @@ public long getLastEntryInLedger(long ledgerId) throws IOException { } private long getLastEntryInLedgerInternal(long ledgerId) throws IOException { + // Search the last entry in storage would be time consume in the case of empty ledger. + // check if ledge is empty first. + long entryLocation = getLocation(ledgerId, 0); + if (entryLocation == 0) { + log.warn("Tried to get last entry from an empty ledger: {}", ledgerId); + throw new Bookie.NoEntryException(ledgerId, -1); + } + LongPairWrapper maxEntryId = LongPairWrapper.get(ledgerId, Long.MAX_VALUE); // Search the last entry in storage