Search in sources :

Example 21 with LedgerEntry

use of org.apache.bookkeeper.client.LedgerEntry in project distributedlog by twitter.

the class LedgerBatchReader method run.

@Override
public void run() {
    long lac = lh.getLastAddConfirmed();
    long entryId = 0L;
    while (entryId <= lac) {
        long startEntryId = entryId;
        long endEntryId = Math.min(startEntryId + batchSize - 1, lac);
        Enumeration<LedgerEntry> entries = null;
        while (null == entries) {
            try {
                entries = lh.readEntries(startEntryId, endEntryId);
            } catch (BKException bke) {
                logger.error("Encountered exceptions on reading [ {} - {} ] ", new Object[] { startEntryId, endEntryId, bke });
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                break;
            }
        }
        if (null == entries) {
            break;
        }
        while (entries.hasMoreElements()) {
            LedgerEntry entry = entries.nextElement();
            readEntryListener.onEntryComplete(BKException.Code.OK, lh, entry, null);
        }
        entryId = endEntryId + 1;
    }
}
Also used : LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException)

Example 22 with LedgerEntry

use of org.apache.bookkeeper.client.LedgerEntry in project distributedlog by twitter.

the class TestLedgerAllocator method testSuccessAllocatorShouldDeleteUnusedledger.

@Test(timeout = 60000)
public void testSuccessAllocatorShouldDeleteUnusedledger() throws Exception {
    String allocationPath = "/allocation-delete-unused-ledger";
    zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(allocationPath, false, stat);
    Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion()));
    SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator1.allocate();
    // wait until allocated
    ZKTransaction txn1 = newTxn();
    LedgerHandle lh1 = FutureUtils.result(allocator1.tryObtain(txn1, NULL_LISTENER));
    // Second allocator kicks in
    stat = new Stat();
    data = zkc.get().getData(allocationPath, false, stat);
    allocationData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion()));
    SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator2.allocate();
    // wait until allocated
    ZKTransaction txn2 = newTxn();
    LedgerHandle lh2 = FutureUtils.result(allocator2.tryObtain(txn2, NULL_LISTENER));
    // should fail to commit txn1 as version is changed by second allocator
    try {
        FutureUtils.result(txn1.execute());
        fail("Should fail commit obtaining ledger handle from first allocator as allocator is modified by second allocator.");
    } catch (ZKException ke) {
    // as expected
    }
    FutureUtils.result(txn2.execute());
    Utils.close(allocator1);
    Utils.close(allocator2);
    // ledger handle should be deleted
    try {
        lh1.close();
        fail("LedgerHandle allocated by allocator1 should be deleted.");
    } catch (BKException bke) {
    // as expected
    }
    try {
        bkc.get().openLedger(lh1.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
        fail("LedgerHandle allocated by allocator1 should be deleted.");
    } catch (BKException.BKNoSuchLedgerExistsException nslee) {
    // as expected
    }
    long eid = lh2.addEntry("hello world".getBytes());
    lh2.close();
    LedgerHandle readLh = bkc.get().openLedger(lh2.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
    Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
    int i = 0;
    while (entries.hasMoreElements()) {
        LedgerEntry entry = entries.nextElement();
        assertEquals("hello world", new String(entry.getEntry(), UTF_8));
        ++i;
    }
    assertEquals(1, i);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ZKTransaction(com.twitter.distributedlog.zk.ZKTransaction) ZKException(com.twitter.distributedlog.exceptions.ZKException) Stat(org.apache.zookeeper.data.Stat) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) ZkVersion(org.apache.bookkeeper.meta.ZkVersion) Test(org.junit.Test)

Example 23 with LedgerEntry

use of org.apache.bookkeeper.client.LedgerEntry in project pulsar by yahoo.

the class SimpleBookKeeperTest method simpleTest.

@Test
public void simpleTest() throws Exception {
    LedgerHandle ledger = bkc.createLedger(DigestType.MAC, SECRET.getBytes());
    long ledgerId = ledger.getId();
    log.info("Writing to ledger: {}", ledgerId);
    for (int i = 0; i < 10; i++) {
        String content = "entry-" + i;
        ledger.addEntry(content.getBytes(Encoding));
    }
    ledger.close();
    ledger = bkc.openLedger(ledgerId, DigestType.MAC, SECRET.getBytes());
    Enumeration<LedgerEntry> entries = ledger.readEntries(0, 9);
    while (entries.hasMoreElements()) {
        LedgerEntry entry = entries.nextElement();
        String content = new String(entry.getEntry(), Encoding);
        log.info("Entry {}  lenght={} content='{}'", entry.getEntryId(), entry.getLength(), content);
    }
    ledger.close();
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Test(org.testng.annotations.Test)

Example 24 with LedgerEntry

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

the class TestReplicationWorker method verifyRecoveredLedgers.

private void verifyRecoveredLedgers(LedgerHandle lh, long startEntryId, long endEntryId) throws BKException, InterruptedException {
    LedgerHandle lhs = bkc.openLedgerNoRecovery(lh.getId(), BookKeeper.DigestType.CRC32, TESTPASSWD);
    Enumeration<LedgerEntry> entries = lhs.readEntries(startEntryId, endEntryId);
    assertTrue("Should have the elements", entries.hasMoreElements());
    while (entries.hasMoreElements()) {
        LedgerEntry entry = entries.nextElement();
        assertEquals("TestReplicationWorker", new String(entry.getEntry()));
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry)

Example 25 with LedgerEntry

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

the class BookieJournalRollingTest method validLedgerEntries.

private void validLedgerEntries(long[] ledgerIds, int msgSize, int numMsgs) throws Exception {
    // Open the ledgers
    LedgerHandle[] lhs = new LedgerHandle[ledgerIds.length];
    for (int i = 0; i < lhs.length; i++) {
        lhs[i] = bkc.openLedger(ledgerIds[i], digestType, "".getBytes());
    }
    StringBuilder msgSB = new StringBuilder();
    for (int i = 0; i < msgSize; i++) {
        msgSB.append("a");
    }
    String msg = msgSB.toString();
    int numToRead = 10;
    // read all of the entries for all the ledgers
    for (int j = 0; j < lhs.length; j++) {
        int start = 0;
        int read = Math.min(numToRead, numMsgs - start);
        int end = start + read - 1;
        int entryId = 0;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Validating Entries of Ledger " + ledgerIds[j]);
        }
        while (start < numMsgs) {
            Enumeration<LedgerEntry> seq = lhs[j].readEntries(start, end);
            assertTrue("Enumeration of ledger entries has no element", seq.hasMoreElements());
            while (seq.hasMoreElements()) {
                LedgerEntry e = seq.nextElement();
                assertEquals(entryId, e.getEntryId());
                StringBuilder sb = new StringBuilder();
                sb.append(ledgerIds[j]).append('-').append(entryId).append('-').append(msg);
                assertArrayEquals(sb.toString().getBytes(), e.getEntry());
                entryId++;
            }
            assertEquals(entryId - 1, end);
            start = end + 1;
            read = Math.min(numToRead, numMsgs - start);
            end = start + read - 1;
        }
    }
    for (LedgerHandle lh : lhs) {
        lh.close();
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry)

Aggregations

LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)54 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)38 BKException (org.apache.bookkeeper.client.BKException)21 Test (org.junit.Test)20 BookKeeper (org.apache.bookkeeper.client.BookKeeper)10 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)9 IOException (java.io.IOException)8 ByteBuffer (java.nio.ByteBuffer)7 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)6 File (java.io.File)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Bookie (org.apache.bookkeeper.bookie.Bookie)4 LedgerDirsManager (org.apache.bookkeeper.bookie.LedgerDirsManager)4 MLDataFormats (org.apache.bookkeeper.mledger.proto.MLDataFormats)4 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)3 ManagedLedgerImpl.createManagedLedgerException (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.createManagedLedgerException)3 Versioned (org.apache.bookkeeper.versioning.Versioned)3