Skip to content
Permalink
Browse files
Fix read empty ledger exception using bookkeeper admin
### Motivation
Fix #3222 

The root cause is that BookKeeperAdmin use `asyncReadEntriesInternal` to read entries. However, the `asyncReadEntriesInternal` doesn't check the lastAddConfirm, which will lead to the exception list in issue 3222

### Changes

Add lastAddConfirm check before call `asyncReadEntriesInternal`

Reviewers: Yong Zhang <zhangyong1025.zy@gmail.com>, Andrey Yegorov <None>

This closes #3240 from hangc0276/chenhang/fix_BKBookieHandleNotAvailableException_for_empty_ledger
  • Loading branch information
hangc0276 committed Apr 25, 2022
1 parent 49bb636 commit 083e2e94d18edcd85a1d25deaf77ecad0e3de61d
Showing 2 changed files with 19 additions and 3 deletions.
@@ -432,12 +432,11 @@ public boolean hasNext() {
if (currentEntry != null) {
return true;
}
if (lastEntryId == -1 || nextEntryId <= lastEntryId) {
if ((lastEntryId == -1 || nextEntryId <= lastEntryId) && nextEntryId <= handle.getLastAddConfirmed()) {
try {
CompletableFuture<Enumeration<LedgerEntry>> result = new CompletableFuture<>();

handle.asyncReadEntriesInternal(nextEntryId, nextEntryId,
new SyncReadCallback(result), null, false);
new SyncReadCallback(result), null, false);

currentEntry = SyncCallbackUtils.waitForResult(result).nextElement();

@@ -490,6 +490,23 @@ public void testGetListOfEntriesOfLedger(boolean isLedgerClosed) throws Exceptio
bkc.close();
}

@Test
public void testGetEntriesFromEmptyLedger() throws Exception {
ClientConfiguration conf = new ClientConfiguration();
conf.setMetadataServiceUri(zkUtil.getMetadataServiceUri());
BookKeeper bkc = new BookKeeper(conf);
LedgerHandle lh = bkc.createLedger(numOfBookies, numOfBookies, digestType, "testPasswd".getBytes(UTF_8));
lh.close();
long ledgerId = lh.getId();

try (BookKeeperAdmin bkAdmin = new BookKeeperAdmin(zkUtil.getZooKeeperConnectString())) {
Iterator<LedgerEntry> iter = bkAdmin.readEntries(ledgerId, 0, 0).iterator();
assertFalse(iter.hasNext());
}

bkc.close();
}

@Test
public void testGetListOfEntriesOfLedgerWithJustOneBookieInWriteQuorum() throws Exception {
ClientConfiguration conf = new ClientConfiguration();

0 comments on commit 083e2e9

Please sign in to comment.