Search in sources :

Example 26 with LedgerEntry

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

the class ReadOnlyBookieTest method testBookieShouldTurnWritableFromReadOnly.

@Test
public void testBookieShouldTurnWritableFromReadOnly() throws Exception {
    killBookie(0);
    baseConf.setReadOnlyModeEnabled(true);
    startNewBookie();
    LedgerHandle ledger = bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
    // Check new bookie with readonly mode enabled.
    File[] ledgerDirs = bsConfs.get(1).getLedgerDirs();
    assertEquals("Only one ledger dir should be present", 1, ledgerDirs.length);
    Bookie bookie = bs.get(1).getBookie();
    LedgerDirsManager ledgerDirsManager = bookie.getLedgerDirsManager();
    for (int i = 0; i < 10; i++) {
        ledger.addEntry("data".getBytes());
    }
    File testDir = new File(ledgerDirs[0], "current");
    // Now add the current ledger dir to filled dirs list
    ledgerDirsManager.addToFilledDirs(testDir);
    try {
        ledger.addEntry("data".getBytes());
        fail("Should fail to add entry since there isn't enough bookies alive.");
    } catch (BKException.BKNotEnoughBookiesException e) {
    // Expected
    }
    bkc.waitForReadOnlyBookie(Bookie.getBookieAddress(bsConfs.get(1))).get(30, TimeUnit.SECONDS);
    LOG.info("bookie is running {}, readonly {}.", bookie.isRunning(), bookie.isReadOnly());
    assertTrue("Bookie should be running and converted to readonly mode", bookie.isRunning() && bookie.isReadOnly());
    // should fail to create ledger
    try {
        bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
        fail("Should fail to create a ledger since there isn't enough bookies alive.");
    } catch (BKException.BKNotEnoughBookiesException bke) {
    // Expected.
    }
    // Now add the current ledger dir back to writable dirs list
    ledgerDirsManager.addToWritableDirs(testDir, true);
    bkc.waitForWritableBookie(Bookie.getBookieAddress(bsConfs.get(1))).get(30, TimeUnit.SECONDS);
    LOG.info("bookie is running {}, readonly {}.", bookie.isRunning(), bookie.isReadOnly());
    assertTrue("Bookie should be running and converted back to writable mode", bookie.isRunning() && !bookie.isReadOnly());
    LedgerHandle newLedger = bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
    for (int i = 0; i < 10; i++) {
        newLedger.addEntry("data".getBytes());
    }
    Enumeration<LedgerEntry> readEntries = newLedger.readEntries(0, 9);
    while (readEntries.hasMoreElements()) {
        LedgerEntry entry = readEntries.nextElement();
        assertEquals("Entry should contain correct data", "data", new String(entry.getEntry()));
    }
}
Also used : LedgerDirsManager(org.apache.bookkeeper.bookie.LedgerDirsManager) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) Bookie(org.apache.bookkeeper.bookie.Bookie) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BKException(org.apache.bookkeeper.client.BKException) File(java.io.File) Test(org.junit.Test)

Example 27 with LedgerEntry

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

the class ReadOnlyBookieTest method testReadFromReadOnlyBookieShouldBeSuccess.

/**
 * Try to read closed ledger from restarted ReadOnlyBookie.
 */
public void testReadFromReadOnlyBookieShouldBeSuccess() throws Exception {
    LedgerHandle ledger = bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
    for (int i = 0; i < 10; i++) {
        ledger.addEntry("data".getBytes());
    }
    ledger.close();
    bsConfs.get(1).setReadOnlyModeEnabled(true);
    bsConfs.get(1).setDiskCheckInterval(500);
    restartBookies();
    // Check new bookie with readonly mode enabled.
    File[] ledgerDirs = bsConfs.get(1).getLedgerDirs();
    assertEquals("Only one ledger dir should be present", 1, ledgerDirs.length);
    Bookie bookie = bs.get(1).getBookie();
    LedgerDirsManager ledgerDirsManager = bookie.getLedgerDirsManager();
    // Now add the current ledger dir to filled dirs list
    ledgerDirsManager.addToFilledDirs(new File(ledgerDirs[0], "current"));
    // Wait till Bookie converts to ReadOnly mode.
    Thread.sleep(1000);
    assertTrue("Bookie should be converted to readonly mode", bookie.isRunning() && bookie.isReadOnly());
    // Now kill the other bookie and read entries from the readonly bookie
    killBookie(0);
    Enumeration<LedgerEntry> readEntries = ledger.readEntries(0, 9);
    while (readEntries.hasMoreElements()) {
        LedgerEntry entry = readEntries.nextElement();
        assertEquals("Entry should contain correct data", "data", new String(entry.getEntry()));
    }
}
Also used : LedgerDirsManager(org.apache.bookkeeper.bookie.LedgerDirsManager) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) Bookie(org.apache.bookkeeper.bookie.Bookie) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) File(java.io.File)

Example 28 with LedgerEntry

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

the class BookieFailureTest method testBookieRecovery.

@Test
public void testBookieRecovery() throws Exception {
    // Shutdown all but 1 bookie
    bs.get(0).shutdown();
    bs.get(1).shutdown();
    bs.get(2).shutdown();
    byte[] passwd = "blah".getBytes();
    LedgerHandle lh = bkc.createLedger(1, 1, digestType, passwd);
    int numEntries = 100;
    for (int i = 0; i < numEntries; i++) {
        byte[] data = ("" + i).getBytes();
        lh.addEntry(data);
    }
    bs.get(3).shutdown();
    BookieServer server = new BookieServer(bsConfs.get(3));
    server.start();
    bs.set(3, server);
    assertEquals(numEntries - 1, lh.getLastAddConfirmed());
    Enumeration<LedgerEntry> entries = lh.readEntries(0, lh.getLastAddConfirmed());
    int numScanned = 0;
    while (entries.hasMoreElements()) {
        assertEquals(("" + numScanned), new String(entries.nextElement().getEntry()));
        numScanned++;
    }
    assertEquals(numEntries, numScanned);
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookieServer(org.apache.bookkeeper.proto.BookieServer) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Test(org.junit.Test)

Example 29 with LedgerEntry

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

the class BookieReadWriteTest method testReadFromOpenLedgerOpenOnce.

@Test
public void testReadFromOpenLedgerOpenOnce() throws Exception {
    try {
        // Create a ledger
        lh = bkc.createLedger(digestType, ledgerPassword);
        // bkc.initMessageDigest("SHA1");
        ledgerId = lh.getId();
        LOG.info("Ledger ID: " + lh.getId());
        LedgerHandle lhOpen = bkc.openLedgerNoRecovery(ledgerId, digestType, ledgerPassword);
        writeNEntriesLastWriteSync(lh, numEntriesToWrite / 2);
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        // no recovery opened ledger 's last confirmed entry id is
        // less than written
        // and it just can read until (i-1)
        int toRead = numEntriesToWrite / 2 - 2;
        long readLastConfirmed = lhOpen.readLastConfirmed();
        assertTrue(readLastConfirmed != 0);
        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(toRead), e.getEntry());
        // should not written to a read only ledger
        try {
            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");
        }
        writeNEntriesLastWriteSync(lh, numEntriesToWrite / 2);
        long last = lh.readLastConfirmed();
        assertTrue("Last confirmed add: " + last, last == (numEntriesToWrite - 2));
        LOG.debug("*** WRITE COMPLETE ***");
        // close ledger
        lh.close();
        // close read only ledger should not change metadata
        lhOpen.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 30 with LedgerEntry

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

the class BookieReadWriteTest method testReadWriteSyncSingleClient.

@Test
public void testReadWriteSyncSingleClient() throws IOException {
    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());
            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 - 1));
        Enumeration<LedgerEntry> ls = lh.readEntries(0, numEntriesToWrite - 1);
        int i = 0;
        while (ls.hasMoreElements()) {
            ByteBuffer origbb = ByteBuffer.wrap(entries.get(i++));
            Integer origEntry = origbb.getInt();
            ByteBuffer result = ByteBuffer.wrap(ls.nextElement().getEntry());
            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));
        }
        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) 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