Search in sources :

Example 1 with LedgerEntry

use of org.apache.bookkeeper.client.api.LedgerEntry in project herddb by diennea.

the class BookkeeperCommitLogManager method scanRawLedger.

public static void scanRawLedger(long ledgerId, long fromId, long toId, herddb.client.ClientConfiguration clientConfiguration, ZookeeperMetadataStorageManager metadataStorageManager, Consumer<LogEntryWithSequenceNumber> consumer) throws Exception {
    ClientConfiguration config = new ClientConfiguration();
    config.setZkServers(metadataStorageManager.getZkAddress());
    config.setZkTimeout(metadataStorageManager.getZkSessionTimeout());
    config.setZkLedgersRootPath(clientConfiguration.getString(ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_PATH, ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_PATH_DEFAULT));
    config.setEnableParallelRecoveryRead(true);
    config.setEnableDigestTypeAutodetection(true);
    try (org.apache.bookkeeper.client.api.BookKeeper bookKeeper = org.apache.bookkeeper.client.api.BookKeeper.newBuilder(config).build()) {
        try (ReadHandle lh = bookKeeper.newOpenLedgerOp().withRecovery(false).withLedgerId(ledgerId).withPassword(BookkeeperCommitLog.SHARED_SECRET.getBytes(StandardCharsets.UTF_8)).execute().get()) {
            long lastAddConfirmed = lh.readLastAddConfirmed();
            if (toId < 0) {
                toId = lastAddConfirmed;
            }
            LOG.log(Level.INFO, "Scanning Ledger {0} from {1} to {2} LAC {3}", new Object[] { ledgerId, fromId, toId, lastAddConfirmed });
            for (long id = fromId; id <= toId; id++) {
                try (LedgerEntries entries = lh.readUnconfirmed(id, id)) {
                    LedgerEntry entry = entries.getEntry(id);
                    LogEntry lEntry = LogEntry.deserialize(entry.getEntryBytes());
                    LogEntryWithSequenceNumber e = new LogEntryWithSequenceNumber(new LogSequenceNumber(ledgerId, id), lEntry);
                    consumer.accept(e);
                }
            }
        }
    }
}
Also used : ReadHandle(org.apache.bookkeeper.client.api.ReadHandle) LedgerEntries(org.apache.bookkeeper.client.api.LedgerEntries) LedgerEntry(org.apache.bookkeeper.client.api.LedgerEntry) LogSequenceNumber(herddb.log.LogSequenceNumber) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) LogEntry(herddb.log.LogEntry)

Example 2 with LedgerEntry

use of org.apache.bookkeeper.client.api.LedgerEntry in project bookkeeper by apache.

the class TestParallelRead method testNormalParallelRead.

@Test
public void testNormalParallelRead() throws Exception {
    int numEntries = 10;
    long id = getLedgerToRead(5, 2, 2, numEntries);
    LedgerHandle lh = bkc.openLedger(id, digestType, passwd);
    // read single entry
    for (int i = 0; i < numEntries; i++) {
        PendingReadOp readOp = new PendingReadOp(lh, lh.bk.scheduler, i, i);
        readOp.parallelRead(true).submit();
        Iterator<LedgerEntry> entries = readOp.future().get().iterator();
        assertTrue(entries.hasNext());
        LedgerEntry entry = entries.next();
        assertNotNull(entry);
        assertEquals(i, Integer.parseInt(new String(entry.getEntryBytes())));
        entry.close();
        assertFalse(entries.hasNext());
    }
    // read multiple entries
    PendingReadOp readOp = new PendingReadOp(lh, lh.bk.scheduler, 0, numEntries - 1);
    readOp.parallelRead(true).submit();
    Iterator<LedgerEntry> iterator = readOp.future().get().iterator();
    int numReads = 0;
    while (iterator.hasNext()) {
        LedgerEntry entry = iterator.next();
        assertNotNull(entry);
        assertEquals(numReads, Integer.parseInt(new String(entry.getEntryBytes())));
        entry.close();
        ++numReads;
    }
    assertEquals(numEntries, numReads);
    lh.close();
}
Also used : LedgerEntry(org.apache.bookkeeper.client.api.LedgerEntry) Test(org.junit.Test)

Example 3 with LedgerEntry

use of org.apache.bookkeeper.client.api.LedgerEntry in project bookkeeper by apache.

the class TestParallelRead method testParallelReadWithFailedBookies.

@Test
public void testParallelReadWithFailedBookies() throws Exception {
    int numEntries = 10;
    long id = getLedgerToRead(5, 3, 3, numEntries);
    ClientConfiguration newConf = new ClientConfiguration().setReadEntryTimeout(30000);
    newConf.setZkServers(zkUtil.getZooKeeperConnectString());
    BookKeeper newBk = new BookKeeper(newConf);
    LedgerHandle lh = bkc.openLedger(id, digestType, passwd);
    ArrayList<BookieSocketAddress> ensemble = lh.getLedgerMetadata().getEnsemble(5);
    // kill two bookies
    killBookie(ensemble.get(0));
    killBookie(ensemble.get(1));
    // read multiple entries
    PendingReadOp readOp = new PendingReadOp(lh, lh.bk.scheduler, 0, numEntries - 1);
    readOp.parallelRead(true).submit();
    Iterator<LedgerEntry> entries = readOp.future().get().iterator();
    int numReads = 0;
    while (entries.hasNext()) {
        LedgerEntry entry = entries.next();
        assertNotNull(entry);
        assertEquals(numReads, Integer.parseInt(new String(entry.getEntryBytes())));
        ++numReads;
    }
    assertEquals(numEntries, numReads);
    lh.close();
    newBk.close();
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) LedgerEntry(org.apache.bookkeeper.client.api.LedgerEntry) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Example 4 with LedgerEntry

use of org.apache.bookkeeper.client.api.LedgerEntry in project bookkeeper by apache.

the class LedgerEntriesImplTest method testGetEntry.

@Test
public void testGetEntry() {
    for (int i = 0; i < entryNumber; i++) {
        LedgerEntry entry = ledgerEntriesImpl.getEntry(entryId + i);
        assertEquals(entryList.get(i).getLedgerId(), entry.getLedgerId());
        assertEquals(entryList.get(i).getEntryId(), entry.getEntryId());
        assertEquals(entryList.get(i).getLength(), entry.getLength());
        ByteBuf buf = entry.getEntryBuffer();
        byte[] content = new byte[buf.readableBytes()];
        buf.readBytes(content);
        assertArrayEquals(dataBytes, content);
        assertEquals(1, entry.getEntryBuffer().refCnt());
    }
    try {
        LedgerEntry entry = ledgerEntriesImpl.getEntry(entryId - 1);
        fail("Should get IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    // expected behavior
    }
    try {
        LedgerEntry entry = ledgerEntriesImpl.getEntry(entryId + entryNumber);
        fail("Should get IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    // expected behavior
    }
}
Also used : LedgerEntry(org.apache.bookkeeper.client.api.LedgerEntry) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

LedgerEntry (org.apache.bookkeeper.client.api.LedgerEntry)4 Test (org.junit.Test)3 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)2 LogEntry (herddb.log.LogEntry)1 LogSequenceNumber (herddb.log.LogSequenceNumber)1 ByteBuf (io.netty.buffer.ByteBuf)1 LedgerEntries (org.apache.bookkeeper.client.api.LedgerEntries)1 ReadHandle (org.apache.bookkeeper.client.api.ReadHandle)1 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)1