Search in sources :

Example 56 with LedgerHandle

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

the class ReadOnlyBookieTest method testBookieShutdownIfReadOnlyModeNotEnabled.

/**
 * check readOnlyModeEnabled=false.
 */
@Test
public void testBookieShutdownIfReadOnlyModeNotEnabled() throws Exception {
    killBookie(1);
    baseConf.setReadOnlyModeEnabled(false);
    startNewBookie();
    File[] ledgerDirs = bsConfs.get(1).getLedgerDirs();
    assertEquals("Only one ledger dir should be present", 1, ledgerDirs.length);
    Bookie bookie = bs.get(1).getBookie();
    LedgerHandle ledger = bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
    LedgerDirsManager ledgerDirsManager = bookie.getLedgerDirsManager();
    for (int i = 0; i < 10; i++) {
        ledger.addEntry("data".getBytes());
    }
    // Now add the current ledger dir to filled dirs list
    ledgerDirsManager.addToFilledDirs(new File(ledgerDirs[0], "current"));
    try {
        ledger.addEntry("data".getBytes());
        fail("Should fail to add entry since there isn't enough bookies alive.");
    } catch (BKException.BKNotEnoughBookiesException e) {
    // Expected
    }
    // wait for up to 10 seconds for bookie to shut down
    for (int i = 0; i < 10 && bookie.isAlive(); i++) {
        Thread.sleep(1000);
    }
    assertFalse("Bookie should shutdown if readOnlyMode not enabled", bookie.isAlive());
}
Also used : LedgerDirsManager(org.apache.bookkeeper.bookie.LedgerDirsManager) Bookie(org.apache.bookkeeper.bookie.Bookie) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BKException(org.apache.bookkeeper.client.BKException) File(java.io.File) Test(org.junit.Test)

Example 57 with LedgerHandle

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

the class ReadOnlyBookieTest method testBookieContinueWritingIfMultipleLedgersPresent.

/**
 * Check multiple ledger dirs.
 */
@Test
public void testBookieContinueWritingIfMultipleLedgersPresent() throws Exception {
    startNewBookieWithMultipleLedgerDirs(2);
    File[] ledgerDirs = bsConfs.get(1).getLedgerDirs();
    assertEquals("Only one ledger dir should be present", 2, ledgerDirs.length);
    Bookie bookie = bs.get(1).getBookie();
    LedgerHandle ledger = bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
    LedgerDirsManager ledgerDirsManager = bookie.getLedgerDirsManager();
    for (int i = 0; i < 10; i++) {
        ledger.addEntry("data".getBytes());
    }
    // Now add the current ledger dir to filled dirs list
    ledgerDirsManager.addToFilledDirs(new File(ledgerDirs[0], "current"));
    for (int i = 0; i < 10; i++) {
        ledger.addEntry("data".getBytes());
    }
    assertEquals("writable dirs should have one dir", 1, ledgerDirsManager.getWritableLedgerDirs().size());
    assertTrue("Bookie should shutdown if readOnlyMode not enabled", bookie.isAlive());
}
Also used : LedgerDirsManager(org.apache.bookkeeper.bookie.LedgerDirsManager) Bookie(org.apache.bookkeeper.bookie.Bookie) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) File(java.io.File) Test(org.junit.Test)

Example 58 with LedgerHandle

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

the class BookieFailureTest method testLedgerNoRecoveryOpenAfterBKCrashed.

@Test
public void testLedgerNoRecoveryOpenAfterBKCrashed() throws Exception {
    // Create a ledger
    LedgerHandle beforelh = bkc.createLedger(numBookies, numBookies, digestType, "".getBytes());
    int numEntries = 10;
    String tmp = "BookKeeper is cool!";
    for (int i = 0; i < numEntries; i++) {
        beforelh.addEntry(tmp.getBytes());
    }
    // shutdown first bookie server
    killBookie(0);
    // try to open ledger no recovery
    LedgerHandle afterlh = bkc.openLedgerNoRecovery(beforelh.getId(), digestType, "".getBytes());
    assertEquals(numEntries - 2, afterlh.getLastAddConfirmed());
    startNewBookie();
    LedgerHandle beforelh2 = bkc.createLedger(numBookies, 1, digestType, "".getBytes());
    for (int i = 0; i < numEntries; i++) {
        beforelh2.addEntry(tmp.getBytes());
    }
    // shutdown first bookie server
    killBookie(0);
    // try to open ledger no recovery
    try {
        bkc.openLedgerNoRecovery(beforelh2.getId(), digestType, "".getBytes());
        fail("Should have thrown exception");
    } catch (BKException.BKReadException e) {
    // correct behaviour
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BKException(org.apache.bookkeeper.client.BKException) Test(org.junit.Test)

Example 59 with LedgerHandle

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

the class BookieFailureTest method testReadLastConfirmedOp.

/**
 * Verify read last confirmed op, it shouldn't cause any deadlock as new
 * bookie connection is being established and returning the connection
 * notification in the same caller thread. It would be simulated by delaying
 * the future.addlistener() in PerChannelBookieClient after the connection
 * establishment. Now the future.addlistener() will notify back in the same
 * thread and simultaneously invoke the pendingOp.operationComplete() event.
 *
 * <p>BOOKKEEPER-326
 */
@Test
public void testReadLastConfirmedOp() throws Exception {
    startNewBookie();
    startNewBookie();
    // Create a ledger
    LedgerHandle beforelh = bkc.createLedger(numBookies + 2, numBookies + 2, digestType, "".getBytes());
    int numEntries = 10;
    String tmp = "BookKeeper is cool!";
    for (int i = 0; i < numEntries; i++) {
        beforelh.addEntry(tmp.getBytes());
    }
    // shutdown first bookie server
    killBookie(0);
    startNewBookie();
    // create new bookie client, and forcing to establish new
    // PerChannelBookieClient connections for recovery flows.
    BookKeeperTestClient bkc1 = new BookKeeperTestClient(baseClientConf);
    // try to open ledger with recovery
    LedgerHandle afterlh = bkc1.openLedger(beforelh.getId(), digestType, "".getBytes());
    assertEquals("Entries got missed", beforelh.getLastAddPushed(), afterlh.getLastAddConfirmed());
    bkc1.close();
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeperTestClient(org.apache.bookkeeper.client.BookKeeperTestClient) Test(org.junit.Test)

Example 60 with LedgerHandle

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

the class BookieReadWriteTest method testReadFromOpenLedgerZeroAndOne.

@Test
public void testReadFromOpenLedgerZeroAndOne() 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);
        /*
             * We haven't written anything, so it should be empty.
             */
        LOG.debug("Checking that it is empty");
        long readLastConfirmed = lhOpen.readLastConfirmed();
        assertTrue("Last confirmed has the wrong value", readLastConfirmed == LedgerHandle.INVALID_ENTRY_ID);
        /*
             * Writing one entry.
             */
        LOG.debug("Going to write one entry");
        ByteBuffer entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        entries.add(entry.array());
        entriesSize.add(entry.array().length);
        lh.addEntry(entry.array());
        /*
             * The hint should still indicate that there is no confirmed
             * add.
             */
        LOG.debug("Checking that it is still empty even after writing one entry");
        readLastConfirmed = lhOpen.readLastConfirmed();
        assertTrue(readLastConfirmed == LedgerHandle.INVALID_ENTRY_ID);
        /*
             * Adding one more, and this time we should expect to
             * see one entry.
             */
        entry = ByteBuffer.allocate(4);
        entry.putInt(rng.nextInt(maxInt));
        entry.position(0);
        entries.add(entry.array());
        entriesSize.add(entry.array().length);
        lh.addEntry(entry.array());
        LOG.info("Checking that it has an entry");
        readLastConfirmed = lhOpen.readLastConfirmed();
        assertTrue(readLastConfirmed == 0L);
        // 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) BKException(org.apache.bookkeeper.client.BKException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)238 Test (org.junit.Test)140 CountDownLatch (java.util.concurrent.CountDownLatch)54 BookKeeper (org.apache.bookkeeper.client.BookKeeper)52 LedgerEntry (org.apache.bookkeeper.client.LedgerEntry)49 BKException (org.apache.bookkeeper.client.BKException)39 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)28 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)28 File (java.io.File)27 BookieSocketAddress (org.apache.bookkeeper.net.BookieSocketAddress)23 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)22 List (java.util.List)18 CompletableFuture (java.util.concurrent.CompletableFuture)18 BookieServer (org.apache.bookkeeper.proto.BookieServer)18 Test (org.testng.annotations.Test)17 Entry (org.apache.bookkeeper.mledger.Entry)16 ZKTransaction (com.twitter.distributedlog.zk.ZKTransaction)13 ByteBuffer (java.nio.ByteBuffer)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13