Skip to content

EntryMemTable.getLastEntry reports incorrect last entry when ledger metadata is present #4895

Open

Description

@djsweet

BUG REPORT

Describe the bug

EntryMemTable.getLastEntry reports an incorrect last entry if ledger metadata is being used.

This is caused by a long overflow in KeyComparator: if any ledger metadata is present within the EntryMemTable (with a negative entry ID), then the Long.MAX_VALUE passed in through getLastEntry causes a comparison subtraction to overflow.

To Reproduce

The following unit test, added to EntryMemTableTest, will fail.

public void testFindingLastEntryWithMetadata() throws IOException {
    final long testLedgerCount = 8 * 1024;
    final byte[] payload = new byte[0];

    for (long i = 0; i < testLedgerCount; i++) {
        memTable.addEntry(i, BookieImpl.METAENTRY_ID_LEDGER_KEY, ByteBuffer.wrap(payload), this);
        memTable.addEntry(i, 0, ByteBuffer.wrap(payload), this);
    }

    for (long i = 0; i < testLedgerCount; i++) {
        final EntryKeyValue ekv = memTable.getLastEntry(i);
        assertNotNull(String.format("Null entry for ledger %d", i), ekv);
        assertEquals(String.format("Wrong ledger ID at ledger %d", i), i, ekv.getLedgerId());
        assertEquals(String.format("Wrong last entry ID at ledger %d", i), 0, ekv.getEntryId());
    }
}

This test will fail with the message "Null entry for ledger 0".

Expected behavior

The posted test should pass: having ledger metadata present in the EntryMemTable should preserve expected ordering semantics within the table.

Additional context

This method is used in SortedLedgerStorage, for determining whether the ledger exists before the entry has been flushed into InterleavedLedgerStorage, and for retrieving the LastAddConfirmed entry if requested by the Bookie protocol. This bug results in incorrectly reporting that ledgers do not exist, and incorrectly reporting the LastAddConfirmed entry, when SortedLedgerStorage is in use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions