Skip to content

Commit 3fea440

Browse files
#4574: fix sanity check asyncBatchReadEntries (#4579)
* fix sanity check batchReadEntries
1 parent c7d7fc5 commit 3fea440

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

Diff for: bookkeeper-server/src/main/java/org/apache/bookkeeper/client/LedgerHandle.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -754,10 +754,10 @@ public void asyncReadEntries(long firstEntry, long lastEntry, ReadCallback cb, O
754754
*/
755755
public void asyncBatchReadEntries(long startEntry, int maxCount, long maxSize, ReadCallback cb, Object ctx) {
756756
// Little sanity check
757-
if (startEntry > lastAddConfirmed) {
758-
LOG.error("ReadEntries exception on ledgerId:{} firstEntry:{} lastAddConfirmed:{}",
757+
if (startEntry < 0 || startEntry > lastAddConfirmed) {
758+
LOG.error("IncorrectParameterException on ledgerId:{} startEntry:{} lastAddConfirmed:{}",
759759
ledgerId, startEntry, lastAddConfirmed);
760-
cb.readComplete(BKException.Code.ReadException, this, null, ctx);
760+
cb.readComplete(BKException.Code.IncorrectParameterException, this, null, ctx);
761761
return;
762762
}
763763
if (notSupportBatchRead()) {

Diff for: bookkeeper-server/src/test/java/org/apache/bookkeeper/client/BookKeeperTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,44 @@ public void testBatchReadFailBackToSingleRead2() throws Exception {
715715
}
716716
}
717717

718+
@Test
719+
public void testSanityCheckBatchReadEntriesV2() {
720+
ClientConfiguration conf = new ClientConfiguration().setUseV2WireProtocol(true);
721+
conf.setBatchReadEnabled(true);
722+
conf.setMetadataServiceUri(zkUtil.getMetadataServiceUri());
723+
int numEntries = 100;
724+
byte[] data = "foobar".getBytes();
725+
try (BookKeeper bkc = new BookKeeper(conf)) {
726+
long ledgerId;
727+
try (LedgerHandle lh = bkc.createLedger(2, 2, digestType, "testPasswd".getBytes())) {
728+
ledgerId = lh.getId();
729+
for (int i = 0; i < numEntries; i++) {
730+
lh.addEntry(data);
731+
}
732+
} catch (BKException | InterruptedException e) {
733+
fail("LedgerHandle inti failed: " + e.getMessage());
734+
return;
735+
}
736+
737+
// startEntry < 0
738+
try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
739+
assertEquals(numEntries - 1, lh.readLastConfirmed());
740+
Enumeration<LedgerEntry> entries = lh.batchReadEntries(-1, numEntries, 5 * 1024 * 1024);
741+
} catch (BKException | InterruptedException e) {
742+
LOG.info(e.getMessage(), e); // It should raise IncorrectParameterException
743+
}
744+
745+
// startEntry > lastAddConfirmed
746+
try (LedgerHandle lh = bkc.openLedger(ledgerId, digestType, "testPasswd".getBytes())) {
747+
Enumeration<LedgerEntry> entries = lh.batchReadEntries(numEntries, numEntries, 5 * 1024 * 1024);
748+
} catch (BKException | InterruptedException e) {
749+
LOG.info(e.getMessage(), e); // It should raise IncorrectParameterException
750+
}
751+
} catch (BKException | InterruptedException | IOException e) {
752+
fail("BookKeeper client init failed: " + e.getMessage());
753+
}
754+
}
755+
718756
@Test
719757
public void testBatchReadWithV2Protocol() throws Exception {
720758
ClientConfiguration conf = new ClientConfiguration().setUseV2WireProtocol(true);

0 commit comments

Comments
 (0)