Search in sources :

Example 91 with LedgerHandle

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

the class TestAuth 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) {
        conf.setBookieAuthProviderFactoryClass(AlwaysSucceedBookieAuthProviderFactory.class.getName());
    }
    clientConf.setClientAuthProviderFactoryClass(SendUntilCompleteClientAuthProviderFactory.class.getName());
    restartBookies();
    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 92 with LedgerHandle

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

the class TestAuth method connectAndWriteToBookie.

// we pass in ledgerId because the method may throw exceptions
private void connectAndWriteToBookie(ClientConfiguration conf, AtomicLong ledgerWritten) throws Exception {
    LOG.info("Connecting to bookie");
    BookKeeper bkc = new BookKeeper(conf, zkc);
    LedgerHandle l = bkc.createLedger(1, 1, DigestType.CRC32, PASSWD);
    ledgerWritten.set(l.getId());
    l.addEntry(ENTRY);
    l.close();
    bkc.close();
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeper(org.apache.bookkeeper.client.BookKeeper)

Example 93 with LedgerHandle

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

the class EnableZkSecurityBasicTest method testCreateLedgerAddEntryOnSecureZooKeepeer.

@Test
public void testCreateLedgerAddEntryOnSecureZooKeepeer() throws Exception {
    startNewBookie();
    ClientConfiguration conf = new ClientConfiguration();
    conf.setZkServers(zkUtil.getZooKeeperConnectString());
    conf.setZkTimeout(20000);
    conf.setZkEnableSecurity(true);
    try (BookKeeper bkc = new BookKeeper(conf)) {
        try (LedgerHandle lh = bkc.createLedger(1, 1, 1, BookKeeper.DigestType.CRC32, "testPasswd".getBytes())) {
            lh.addEntry("foo".getBytes(StandardCharsets.UTF_8));
        }
    }
    checkAllAcls();
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) BookKeeper(org.apache.bookkeeper.client.BookKeeper) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Example 94 with LedgerHandle

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

the class LedgerStorageCheckpointTest method testIfEntryLogPerLedgerEnabledCheckpointFlushesAllLogs.

/*
     * in this method it checks if entryLogPerLedger is enabled, then
     * InterLeavedLedgerStorage.checkpoint flushes current activelog and flushes
     * all rotatedlogs and closes them.
     *
     */
@Test
public void testIfEntryLogPerLedgerEnabledCheckpointFlushesAllLogs() throws Exception {
    File tmpDir = createTempDir("DiskCheck", "test");
    final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(zkUtil.getZooKeeperConnectString()).setZkTimeout(5000).setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new String[] { tmpDir.getPath() }).setAutoRecoveryDaemonEnabled(false).setFlushInterval(3000).setBookiePort(PortManager.nextFreePort()).setEntryLogPerLedgerEnabled(true).setLedgerStorageClass(InterleavedLedgerStorage.class.getName()).setFlushIntervalInBytes(10000000);
    Assert.assertEquals("Number of JournalDirs", 1, conf.getJournalDirs().length);
    // we know there is only one ledgerDir
    File ledgerDir = Bookie.getCurrentDirectories(conf.getLedgerDirs())[0];
    BookieServer server = new BookieServer(conf);
    server.start();
    ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setZkServers(zkUtil.getZooKeeperConnectString());
    BookKeeper bkClient = new BookKeeper(clientConf);
    InterleavedLedgerStorage ledgerStorage = (InterleavedLedgerStorage) server.getBookie().ledgerStorage;
    EntryLogger entryLogger = ledgerStorage.entryLogger;
    int numOfEntries = 5;
    byte[] dataBytes = "data".getBytes();
    long ledgerId = 10;
    LedgerHandle handle = bkClient.createLedgerAdv(ledgerId, 1, 1, 1, DigestType.CRC32, "passwd".getBytes(), null);
    for (int j = 0; j < numOfEntries; j++) {
        handle.addEntry(j, dataBytes);
    }
    handle.close();
    // simulate rolling entrylog
    ledgerStorage.entryLogger.rollLog();
    ledgerId = 20;
    handle = bkClient.createLedgerAdv(ledgerId, 1, 1, 1, DigestType.CRC32, "passwd".getBytes(), null);
    for (int j = 0; j < numOfEntries; j++) {
        handle.addEntry(j, dataBytes);
    }
    handle.close();
    // simulate rolling entrylog
    ledgerStorage.entryLogger.rollLog();
    ledgerId = 30;
    handle = bkClient.createLedgerAdv(ledgerId, 1, 1, 1, DigestType.CRC32, "passwd".getBytes(), null);
    for (int j = 0; j < numOfEntries; j++) {
        handle.addEntry(j, dataBytes);
    }
    handle.close();
    Assert.assertNotEquals("bytesWrittenSinceLastFlush shouldn't be zero", 0, entryLogger.logChannel.getUnpersistedBytes());
    Assert.assertNotEquals("There should be logChannelsToFlush", 0, entryLogger.logChannelsToFlush.size());
    /*
         * wait for atleast flushInterval period, so that checkpoint can happen.
         */
    executorController.advance(Duration.ofMillis(conf.getFlushInterval()));
    /*
         * since checkpoint happenend, there shouldn't be any logChannelsToFlush
         * and bytesWrittenSinceLastFlush should be zero.
         */
    Assert.assertTrue("There shouldn't be logChannelsToFlush", ((entryLogger.logChannelsToFlush == null) || (entryLogger.logChannelsToFlush.size() == 0)));
    Assert.assertEquals("bytesWrittenSinceLastFlush should be zero", 0, entryLogger.logChannel.getUnpersistedBytes());
}
Also used : LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) BookieServer(org.apache.bookkeeper.proto.BookieServer) BookKeeper(org.apache.bookkeeper.client.BookKeeper) File(java.io.File) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 95 with LedgerHandle

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

the class LedgerStorageCheckpointTest method testCheckpointofILSWhenEntryLogIsRotated.

public void testCheckpointofILSWhenEntryLogIsRotated(boolean entryLogPerLedgerEnabled) throws Exception {
    File tmpDir = createTempDir("DiskCheck", "test");
    final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(zkUtil.getZooKeeperConnectString()).setZkTimeout(5000).setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new String[] { tmpDir.getPath() }).setAutoRecoveryDaemonEnabled(false).setFlushInterval(30000).setBookiePort(PortManager.nextFreePort()).setEntryLogPerLedgerEnabled(entryLogPerLedgerEnabled).setLedgerStorageClass(InterleavedLedgerStorage.class.getName());
    Assert.assertEquals("Number of JournalDirs", 1, conf.getJournalDirs().length);
    // we know there is only one ledgerDir
    File ledgerDir = Bookie.getCurrentDirectories(conf.getLedgerDirs())[0];
    BookieServer server = new BookieServer(conf);
    server.start();
    ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setZkServers(zkUtil.getZooKeeperConnectString());
    BookKeeper bkClient = new BookKeeper(clientConf);
    InterleavedLedgerStorage ledgerStorage = (InterleavedLedgerStorage) server.getBookie().ledgerStorage;
    int numOfEntries = 5;
    byte[] dataBytes = "data".getBytes();
    long ledgerId = 10;
    LedgerHandle handle = bkClient.createLedgerAdv(ledgerId, 1, 1, 1, DigestType.CRC32, "passwd".getBytes(), null);
    for (int j = 0; j < numOfEntries; j++) {
        handle.addEntry(j, dataBytes);
    }
    handle.close();
    // simulate rolling entrylog
    ledgerStorage.entryLogger.rollLog();
    // sleep for a bit for checkpoint to do its task
    executorController.advance(Duration.ofMillis(500));
    File lastMarkFile = new File(ledgerDir, "lastMark");
    LogMark rolledLogMark = readLastMarkFile(lastMarkFile);
    if (entryLogPerLedgerEnabled) {
        Assert.assertEquals("rolledLogMark should be zero, since checkpoint" + "shouldn't have happened when entryLog is rotated", 0, rolledLogMark.compare(new LogMark()));
    } else {
        Assert.assertNotEquals("rolledLogMark shouldn't be zero, since checkpoint" + "should have happened when entryLog is rotated", 0, rolledLogMark.compare(new LogMark()));
    }
    bkClient.close();
    server.shutdown();
}
Also used : LastLogMark(org.apache.bookkeeper.bookie.Journal.LastLogMark) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) BookieServer(org.apache.bookkeeper.proto.BookieServer) BookKeeper(org.apache.bookkeeper.client.BookKeeper) File(java.io.File) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration)

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