Search in sources :

Example 31 with LedgerEntry

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

the class BookieReadWriteTest method testReadWriteAsyncSingleClient.

private void testReadWriteAsyncSingleClient(int numEntries) throws IOException {
    SyncObj sync = new SyncObj();
    try {
        // Create a ledger
        lh = bkc.createLedger(digestType, ledgerPassword);
        // bkc.initMessageDigest("SHA1");
        ledgerId = lh.getId();
        LOG.info("Ledger ID: " + lh.getId());
        for (int i = 0; i < numEntriesToWrite; i++) {
            ByteBuffer entry = ByteBuffer.allocate(4);
            entry.putInt(rng.nextInt(maxInt));
            entry.position(0);
            entries.add(entry.array());
            entriesSize.add(entry.array().length);
            lh.asyncAddEntry(entry.array(), this, sync);
        }
        // wait for all entries to be acknowledged
        synchronized (sync) {
            while (sync.counter < numEntriesToWrite) {
                LOG.debug("Entries counter = " + sync.counter);
                sync.wait();
            }
            assertEquals("Error adding", BKException.Code.OK, sync.getReturnCode());
        }
        LOG.debug("*** WRITE COMPLETE ***");
        // close ledger
        lh.close();
        // *** WRITING PART COMPLETE // READ PART BEGINS ***
        // open ledger
        lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);
        LOG.debug("Number of entries written: " + (lh.getLastAddConfirmed() + 1));
        assertTrue("Verifying number of entries written", lh.getLastAddConfirmed() == (numEntriesToWrite - 1));
        // read entries
        lh.asyncReadEntries(0, numEntriesToWrite - 1, this, sync);
        synchronized (sync) {
            while (!sync.value) {
                sync.wait();
            }
            assertEquals("Error reading", BKException.Code.OK, sync.getReturnCode());
        }
        LOG.debug("*** READ COMPLETE ***");
        // at this point, Enumeration<LedgerEntry> ls is filled with the returned
        // values
        int i = 0;
        Enumeration<LedgerEntry> ls = sync.getLedgerEntries();
        while (ls.hasMoreElements()) {
            ByteBuffer origbb = ByteBuffer.wrap(entries.get(i));
            Integer origEntry = origbb.getInt();
            byte[] entry = ls.nextElement().getEntry();
            ByteBuffer result = ByteBuffer.wrap(entry);
            LOG.debug("Length of result: " + result.capacity());
            LOG.debug("Original entry: " + origEntry);
            Integer retrEntry = result.getInt();
            LOG.debug("Retrieved entry: " + retrEntry);
            assertTrue("Checking entry " + i + " for equality", origEntry.equals(retrEntry));
            assertTrue("Checking entry " + i + " for size", entry.length == entriesSize.get(i).intValue());
            i++;
        }
        assertTrue("Checking number of read entries", i == numEntriesToWrite);
        lh.close();
    } catch (BKException e) {
        LOG.error("Test failed", e);
        fail("Test failed due to BookKeeper exception");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        LOG.error("Test failed", e);
        fail("Test failed due to interruption");
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) ByteBuffer(java.nio.ByteBuffer)

Example 32 with LedgerEntry

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

the class BookieReadWriteTest method testReadWriteZero.

@Test
public void testReadWriteZero() throws IOException {
    try {
        // Create a ledger
        lh = bkc.createLedger(digestType, ledgerPassword);
        // bkc.initMessageDigest("SHA1");
        ledgerId = lh.getId();
        LOG.info("Ledger ID: " + lh.getId());
        final CountDownLatch completeLatch = new CountDownLatch(numEntriesToWrite);
        final AtomicInteger rc = new AtomicInteger(BKException.Code.OK);
        for (int i = 0; i < numEntriesToWrite; i++) {
            lh.asyncAddEntry(new byte[0], new AddCallback() {

                public void addComplete(int rccb, LedgerHandle lh, long entryId, Object ctx) {
                    rc.compareAndSet(BKException.Code.OK, rccb);
                    completeLatch.countDown();
                }
            }, null);
        }
        completeLatch.await();
        if (rc.get() != BKException.Code.OK) {
            throw BKException.create(rc.get());
        }
        /*
             * Write a non-zero entry
             */
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        entries.add(entry.array());
        lh.addEntry(entry.array());
        lh.close();
        lh = bkc.openLedger(ledgerId, digestType, ledgerPassword);
        LOG.debug("Number of entries written: " + lh.getLastAddConfirmed());
        assertTrue("Verifying number of entries written", lh.getLastAddConfirmed() == numEntriesToWrite);
        Enumeration<LedgerEntry> ls = lh.readEntries(0, numEntriesToWrite - 1);
        int i = 0;
        while (ls.hasMoreElements()) {
            ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
            LOG.debug("Length of result: " + result.capacity());
            assertTrue("Checking if entry " + i + " has zero bytes", result.capacity() == 0);
        }
        lh.close();
    } catch (BKException e) {
        LOG.error("Test failed", e);
        fail("Test failed due to BookKeeper exception");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        LOG.error("Test failed", e);
        fail("Test failed due to interruption");
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AddCallback(org.apache.bookkeeper.client.AsyncCallback.AddCallback) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 33 with LedgerEntry

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

the class BookieReadWriteTest method testReadFromOpenLedger.

@Test
public void testReadFromOpenLedger() throws Exception {
    try {
        // Create a ledger
        lh = bkc.createLedger(digestType, ledgerPassword);
        // bkc.initMessageDigest("SHA1");
        ledgerId = lh.getId();
        LOG.info("Ledger ID: " + lh.getId());
        long lac = writeNEntriesLastWriteSync(lh, numEntriesToWrite);
        LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
        // no recovery opened ledger 's last confirmed entry id is less than written
        // and it just can read until (i-1)
        long toRead = lac - 1;
        Enumeration<LedgerEntry> readEntry = lhOpen.readEntries(toRead, toRead);
        assertTrue("Enumeration of ledger entries has no element", readEntry.hasMoreElements());
        LedgerEntry e = readEntry.nextElement();
        assertEquals(toRead, e.getEntryId());
        assertArrayEquals(entries.get((int) toRead), e.getEntry());
        // should not written to a read only ledger
        try {
            ByteBuffer entry = ByteBuffer.allocate(4);
            entry.putInt(rng.nextInt(maxInt));
            entry.position(0);
            lhOpen.addEntry(entry.array());
            fail("Should have thrown an exception here");
        } catch (BKException.BKIllegalOpException bkioe) {
        // this is the correct response
        } catch (Exception ex) {
            LOG.error("Unexpected exception", ex);
            fail("Unexpected exception");
        }
        // close read only ledger should not change metadata
        lhOpen.close();
        lac = writeNEntriesLastWriteSync(lh, numEntriesToWrite);
        assertEquals("Last confirmed add: ", lac, (numEntriesToWrite * 2) - 1);
        LOG.debug("*** WRITE COMPLETE ***");
        // close ledger
        lh.close();
        /*
             * Asynchronous call to read last confirmed entry
             */
        lh = bkc.createLedger(digestType, ledgerPassword);
        // bkc.initMessageDigest("SHA1");
        ledgerId = lh.getId();
        writeNEntriesLastWriteSync(lh, numEntriesToWrite);
        SyncObj sync = new SyncObj();
        lh.asyncReadLastConfirmed(this, sync);
        // Wait for for last confirmed
        synchronized (sync) {
            while (sync.lastConfirmed == -1) {
                LOG.debug("Counter = " + sync.lastConfirmed);
                sync.wait();
            }
            assertEquals("Error reading", BKException.Code.OK, sync.getReturnCode());
        }
        assertEquals("Last confirmed add", sync.lastConfirmed, (numEntriesToWrite - 2));
        LOG.debug("*** WRITE COMPLETE ***");
        // close ledger
        lh.close();
    } catch (BKException e) {
        LOG.error("Test failed", e);
        fail("Test failed due to BookKeeper exception");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        LOG.error("Test failed", e);
        fail("Test failed due to interruption");
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BKIllegalOpException(org.apache.bookkeeper.client.BKException.BKIllegalOpException) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) ByteBuffer(java.nio.ByteBuffer) BKIllegalOpException(org.apache.bookkeeper.client.BKException.BKIllegalOpException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) BKException(org.apache.bookkeeper.client.BKException) Test(org.junit.Test)

Example 34 with LedgerEntry

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

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 LongVersion(stat.getVersion()));
    SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator1.allocate();
    // wait until allocated
    ZKTransaction txn1 = newTxn();
    LedgerHandle lh1 = Utils.ioResult(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 LongVersion(stat.getVersion()));
    SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator2.allocate();
    // wait until allocated
    ZKTransaction txn2 = newTxn();
    LedgerHandle lh2 = Utils.ioResult(allocator2.tryObtain(txn2, NULL_LISTENER));
    // should fail to commit txn1 as version is changed by second allocator
    try {
        Utils.ioResult(txn1.execute());
        fail("Should fail commit obtaining ledger handle from first allocator" + " as allocator is modified by second allocator.");
    } catch (ZKException ke) {
    // as expected
    }
    Utils.ioResult(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(org.apache.distributedlog.zk.ZKTransaction) ZKException(org.apache.distributedlog.exceptions.ZKException) Stat(org.apache.zookeeper.data.Stat) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) Test(org.junit.Test)

Example 35 with LedgerEntry

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

the class TestLedgerAllocator method testBadVersionOnTwoAllocators.

@Test(timeout = 60000)
public void testBadVersionOnTwoAllocators() throws Exception {
    String allocationPath = "/allocation-bad-version";
    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 LongVersion(stat.getVersion()));
    SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator1.allocate();
    // wait until allocated
    ZKTransaction txn1 = newTxn();
    LedgerHandle lh = Utils.ioResult(allocator1.tryObtain(txn1, NULL_LISTENER));
    allocator2.allocate();
    ZKTransaction txn2 = newTxn();
    try {
        Utils.ioResult(allocator2.tryObtain(txn2, NULL_LISTENER));
        fail("Should fail allocating on second allocator as allocator1 is starting allocating something.");
    } catch (ZKException ke) {
        assertEquals(KeeperException.Code.BADVERSION, ke.getKeeperExceptionCode());
    }
    Utils.ioResult(txn1.execute());
    Utils.close(allocator1);
    Utils.close(allocator2);
    long eid = lh.addEntry("hello world".getBytes());
    lh.close();
    LedgerHandle readLh = bkc.get().openLedger(lh.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(org.apache.distributedlog.zk.ZKTransaction) ZKException(org.apache.distributedlog.exceptions.ZKException) Stat(org.apache.zookeeper.data.Stat) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Test(org.junit.Test)

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