Search in sources :

Example 6 with LedgerEntry

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

the class ForceReadOnlyBookieTest method testBookieForceStartAsReadOnly.

/**
 * Check force start readonly bookie.
 */
@Test
public void testBookieForceStartAsReadOnly() throws Exception {
    // create ledger, add entries
    LedgerHandle ledger = bkc.createLedger(2, 2, DigestType.MAC, "".getBytes());
    for (int i = 0; i < 10; i++) {
        ledger.addEntry("data".getBytes());
    }
    ledger.close();
    LOG.info("successed prepare");
    // start bookie 1 as readonly
    bsConfs.get(1).setReadOnlyModeEnabled(true);
    bsConfs.get(1).setForceReadOnlyBookie(true);
    restartBookies();
    Bookie bookie = bs.get(1).getBookie();
    assertTrue("Bookie should be running and in readonly mode", bookie.isRunning() && bookie.isReadOnly());
    LOG.info("successed force start ReadOnlyBookie");
    // Check new bookie with readonly mode enabled.
    File[] ledgerDirs = bsConfs.get(1).getLedgerDirs();
    assertEquals("Only one ledger dir should be present", 1, ledgerDirs.length);
    // kill the writable bookie
    killBookie(0);
    // read entry from read only bookie
    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()));
    }
    LOG.info("successed read entry from ReadOnlyBookie");
    // test will not transfer to Writable mode.
    LedgerDirsManager ledgerDirsManager = bookie.getLedgerDirsManager();
    ledgerDirsManager.addToWritableDirs(new File(ledgerDirs[0], "current"), true);
    assertTrue("Bookie should be running and in readonly mode", bookie.isRunning() && bookie.isReadOnly());
    LOG.info("successed: bookie still readonly");
}
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) Test(org.junit.Test)

Example 7 with LedgerEntry

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

the class TestTLS method testClient.

private LedgerMetadata testClient(BookKeeper client, int clusterSize) throws Exception {
    byte[] passwd = "testPassword".getBytes();
    int numEntries = 100;
    long lid;
    byte[] testEntry = "testEntry".getBytes();
    try (LedgerHandle lh = client.createLedger(clusterSize, clusterSize, DigestType.CRC32, passwd)) {
        for (int i = 0; i <= numEntries; i++) {
            lh.addEntry(testEntry);
        }
        lid = lh.getId();
    }
    try (LedgerHandle lh = client.openLedger(lid, DigestType.CRC32, passwd)) {
        Enumeration<LedgerEntry> entries = lh.readEntries(0, numEntries);
        while (entries.hasMoreElements()) {
            LedgerEntry e = entries.nextElement();
            assertTrue("Entry contents incorrect", Arrays.equals(e.getEntry(), testEntry));
        }
        BookKeeperAdmin admin = new BookKeeperAdmin(client);
        return admin.getLedgerMetadata(lh);
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin)

Example 8 with LedgerEntry

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

the class TestAutoRecoveryAlongWithBookieServers method testAutoRecoveryAlongWithBookieServers.

/**
 * Tests that the auto recovery service along with Bookie servers itself.
 */
@Test
public void testAutoRecoveryAlongWithBookieServers() throws Exception {
    LedgerHandle lh = bkc.createLedger(3, 3, BookKeeper.DigestType.CRC32, "testpasswd".getBytes());
    byte[] testData = "testBuiltAutoRecovery".getBytes();
    for (int i = 0; i < 10; i++) {
        lh.addEntry(testData);
    }
    lh.close();
    BookieSocketAddress replicaToKill = LedgerHandleAdapter.getLedgerMetadata(lh).getEnsembles().get(0L).get(0);
    killBookie(replicaToKill);
    BookieSocketAddress newBkAddr = startNewBookieAndReturnAddress();
    while (ReplicationTestUtil.isLedgerInUnderReplication(zkc, lh.getId(), basePath)) {
        Thread.sleep(100);
    }
    // Killing all bookies except newly replicated bookie
    Set<Entry<Long, ArrayList<BookieSocketAddress>>> entrySet = LedgerHandleAdapter.getLedgerMetadata(lh).getEnsembles().entrySet();
    for (Entry<Long, ArrayList<BookieSocketAddress>> entry : entrySet) {
        ArrayList<BookieSocketAddress> bookies = entry.getValue();
        for (BookieSocketAddress bookie : bookies) {
            if (bookie.equals(newBkAddr)) {
                continue;
            }
            killBookie(bookie);
        }
    }
    // Should be able to read the entries from 0-9
    LedgerHandle lhs = bkc.openLedgerNoRecovery(lh.getId(), BookKeeper.DigestType.CRC32, "testpasswd".getBytes());
    Enumeration<LedgerEntry> entries = lhs.readEntries(0, 9);
    assertTrue("Should have the elements", entries.hasMoreElements());
    while (entries.hasMoreElements()) {
        LedgerEntry entry = entries.nextElement();
        assertEquals("testBuiltAutoRecovery", new String(entry.getEntry()));
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ArrayList(java.util.ArrayList) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Entry(java.util.Map.Entry) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) Test(org.junit.Test)

Example 9 with LedgerEntry

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

the class MD5DigestBookKeeperTest method entryCount.

/**
 * check if the entry exists. Restart the bookie to allow access
 */
private int entryCount(long ledgerId, ServerConfiguration bookieConf, ClientConfiguration clientConf) throws Exception {
    LOG.info("Counting entries in {}", ledgerId);
    for (ServerConfiguration conf : bsConfs) {
        bookieConf.setBookieAuthProviderFactoryClass(SASLBookieAuthProviderFactory.class.getName());
        bookieConf.setProperty(JAAS_CLIENT_ALLOWED_IDS, ".*hd.*");
    }
    clientConf.setClientAuthProviderFactoryClass(SASLClientProviderFactory.class.getName());
    restartBookies();
    try (BookKeeper bkc = new BookKeeper(clientConf, zkc);
        LedgerHandle lh = bkc.openLedger(ledgerId, DigestType.CRC32, PASSWD)) {
        if (lh.getLastAddConfirmed() < 0) {
            return 0;
        }
        Enumeration<LedgerEntry> e = lh.readEntries(0, lh.getLastAddConfirmed());
        int count = 0;
        while (e.hasMoreElements()) {
            count++;
            assertTrue("Should match what we wrote", Arrays.equals(e.nextElement().getEntry(), ENTRY));
        }
        return count;
    }
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) LedgerEntry(org.apache.bookkeeper.client.LedgerEntry) BookKeeper(org.apache.bookkeeper.client.BookKeeper)

Example 10 with LedgerEntry

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

the class ReadOnlyBookieTest method testBookieShouldServeAsReadOnly.

/**
 * Check readonly bookie.
 */
@Test
public void testBookieShouldServeAsReadOnly() 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());
    }
    // 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
    }
    assertTrue("Bookie should be running and 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) BKException(org.apache.bookkeeper.client.BKException) File(java.io.File) 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