Search in sources :

Example 1 with TestStatsProvider

use of org.apache.bookkeeper.test.TestStatsProvider in project bookkeeper by apache.

the class TestTLS method testTLSChannelCounters.

/**
 * Verify TLS and non-TLS channel counters.
 */
@Test
public void testTLSChannelCounters() throws Exception {
    ClientConfiguration tlsClientconf = new ClientConfiguration(baseClientConf).setNumChannelsPerBookie(1);
    ClientConfiguration nonTlsClientconf = new ClientConfiguration(baseClientConf).setNumChannelsPerBookie(1).setTLSProviderFactoryClass(null);
    TestStatsProvider tlsStatsProvider = new TestStatsProvider();
    TestStatsProvider nonTlsStatsProvider = new TestStatsProvider();
    BookKeeperTestClient tlsClient = new BookKeeperTestClient(tlsClientconf, tlsStatsProvider);
    BookKeeperTestClient nonTlsClient = new BookKeeperTestClient(nonTlsClientconf, nonTlsStatsProvider);
    // IO load from clients
    testClient(tlsClient, numBookies);
    testClient(nonTlsClient, numBookies);
    // verify stats
    for (int i = 0; i < numBookies; i++) {
        BookieServer bookie = bs.get(i);
        InetSocketAddress addr = bookie.getLocalAddress().getSocketAddress();
        StringBuilder nameBuilder = new StringBuilder(BookKeeperClientStats.CHANNEL_SCOPE).append(".").append(addr.getAddress().getHostAddress().replace('.', '_').replace('-', '_')).append("_").append(addr.getPort()).append(".");
        // check stats on TLS enabled client
        assertEquals("Mismatch TLS channel count", 1, tlsClient.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.ACTIVE_TLS_CHANNEL_COUNTER).get().longValue());
        assertEquals("TLS handshake failure unexpected", 0, tlsClient.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.FAILED_TLS_HANDSHAKE_COUNTER).get().longValue());
        assertEquals("Mismatch non-TLS channel count", 0, tlsClient.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.ACTIVE_NON_TLS_CHANNEL_COUNTER).get().longValue());
        assertEquals("Connection failures unexpected", 0, tlsClient.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.FAILED_CONNECTION_COUNTER).get().longValue());
        // check stats on non-TLS enabled client
        assertEquals("Mismatch TLS channel count", 0, nonTlsClient.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.ACTIVE_TLS_CHANNEL_COUNTER).get().longValue());
        assertEquals("TLS handshake failure unexpected", 0, nonTlsClient.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.FAILED_TLS_HANDSHAKE_COUNTER).get().longValue());
        assertEquals("Mismatch non-TLS channel count", 1, nonTlsClient.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.ACTIVE_NON_TLS_CHANNEL_COUNTER).get().longValue());
        assertEquals("Connection failures unexpected", 0, nonTlsClient.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.FAILED_CONNECTION_COUNTER).get().longValue());
        bookie.shutdown();
        assertEquals("Mismatch TLS channel count", 0, tlsClient.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.ACTIVE_TLS_CHANNEL_COUNTER).get().longValue());
        assertEquals("Mismatch non-TLS channel count", 0, nonTlsClient.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.ACTIVE_NON_TLS_CHANNEL_COUNTER).get().longValue());
    }
}
Also used : TestStatsProvider(org.apache.bookkeeper.test.TestStatsProvider) InetSocketAddress(java.net.InetSocketAddress) BookieServer(org.apache.bookkeeper.proto.BookieServer) BookKeeperTestClient(org.apache.bookkeeper.client.BookKeeperTestClient) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Example 2 with TestStatsProvider

use of org.apache.bookkeeper.test.TestStatsProvider in project bookkeeper by apache.

the class TestTLS method testHandshakeFailure.

/**
 * Verify handshake failure due to missing entry in trust store.
 */
@Test
public void testHandshakeFailure() throws Exception {
    ClientConfiguration clientConf = new ClientConfiguration(baseClientConf).setNumChannelsPerBookie(1);
    // restart a bookie with wrong trust store
    int restartBookieIdx = 0;
    ServerConfiguration badBookieConf = bsConfs.get(restartBookieIdx);
    switch(serverTrustStoreFormat) {
        case PEM:
            badBookieConf.setTLSTrustStore(getResourcePath("server-cert.pem"));
            break;
        case JKS:
            badBookieConf.setTLSTrustStore(getResourcePath("server-key.jks")).setTLSTrustStorePasswordPath(getResourcePath("keyStoreServerPassword.txt"));
            break;
        case PKCS12:
            badBookieConf.setTLSTrustStore(getResourcePath("server-key.p12")).setTLSTrustStorePasswordPath(getResourcePath("keyStoreServerPassword.txt"));
            break;
        default:
            throw new Exception("Unrecognized trust store format: " + serverTrustStoreFormat);
    }
    killBookie(restartBookieIdx);
    LOG.info("Sleeping for 1s before restarting bookie with bad cert");
    Thread.sleep(1000);
    BookieServer bookie = startBookie(badBookieConf);
    bs.add(bookie);
    bsConfs.add(badBookieConf);
    // Create ledger and write entries
    TestStatsProvider testStatsProvider = new TestStatsProvider();
    BookKeeperTestClient client = new BookKeeperTestClient(clientConf, testStatsProvider);
    byte[] passwd = "testPassword".getBytes();
    int numEntries = 2;
    byte[] testEntry = "testEntry".getBytes();
    // should fail to write entries whey WQ == AQ == 3
    try (LedgerHandle lh = client.createLedger(numBookies, numBookies, numBookies, DigestType.CRC32, passwd)) {
        for (int i = 0; i <= numEntries; i++) {
            lh.addEntry(testEntry);
        }
        fail("Should have failed with not enough bookies to write");
    } catch (BKException.BKNotEnoughBookiesException bke) {
    // expected
    }
    // check failed handshake counter
    InetSocketAddress addr = bookie.getLocalAddress().getSocketAddress();
    StringBuilder nameBuilder = new StringBuilder(BookKeeperClientStats.CHANNEL_SCOPE).append(".").append(addr.getAddress().getHostAddress().replace('.', '_').replace('-', '_')).append("_").append(addr.getPort()).append(".");
    assertEquals("TLS handshake failure expected", 1, client.getTestStatsProvider().getCounter(nameBuilder.toString() + BookKeeperClientStats.FAILED_TLS_HANDSHAKE_COUNTER).get().longValue());
}
Also used : TestStatsProvider(org.apache.bookkeeper.test.TestStatsProvider) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) InetSocketAddress(java.net.InetSocketAddress) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) BookieServer(org.apache.bookkeeper.proto.BookieServer) BookKeeperTestClient(org.apache.bookkeeper.client.BookKeeperTestClient) BKException(org.apache.bookkeeper.client.BKException) IOException(java.io.IOException) BKException(org.apache.bookkeeper.client.BKException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Example 3 with TestStatsProvider

use of org.apache.bookkeeper.test.TestStatsProvider in project bookkeeper by apache.

the class GcLedgersTest method testGarbageCollectLedgers.

@Test
public void testGarbageCollectLedgers() throws Exception {
    int numLedgers = 100;
    int numRemovedLedgers = 10;
    final Set<Long> createdLedgers = new HashSet<Long>();
    final Set<Long> removedLedgers = new HashSet<Long>();
    // create 100 ledgers
    createLedgers(numLedgers, createdLedgers);
    Random r = new Random(System.currentTimeMillis());
    final List<Long> tmpList = new ArrayList<Long>();
    tmpList.addAll(createdLedgers);
    Collections.shuffle(tmpList, r);
    // random remove several ledgers
    for (int i = 0; i < numRemovedLedgers; i++) {
        long ledgerId = tmpList.get(i);
        synchronized (removedLedgers) {
            getLedgerManager().removeLedgerMetadata(ledgerId, Version.ANY, new GenericCallback<Void>() {

                @Override
                public void operationComplete(int rc, Void result) {
                    synchronized (removedLedgers) {
                        removedLedgers.notify();
                    }
                }
            });
            removedLedgers.wait();
        }
        removedLedgers.add(ledgerId);
        createdLedgers.remove(ledgerId);
    }
    final CountDownLatch inGcProgress = new CountDownLatch(1);
    final CountDownLatch createLatch = new CountDownLatch(1);
    final CountDownLatch endLatch = new CountDownLatch(2);
    final CompactableLedgerStorage mockLedgerStorage = new MockLedgerStorage();
    TestStatsProvider stats = new TestStatsProvider();
    final GarbageCollector garbageCollector = new ScanAndCompareGarbageCollector(getLedgerManager(), mockLedgerStorage, baseConf, stats.getStatsLogger("gc"));
    Thread gcThread = new Thread() {

        @Override
        public void run() {
            garbageCollector.gc(new GarbageCollector.GarbageCleaner() {

                boolean paused = false;

                @Override
                public void clean(long ledgerId) {
                    try {
                        mockLedgerStorage.deleteLedger(ledgerId);
                    } catch (IOException e) {
                        e.printStackTrace();
                        return;
                    }
                    if (!paused) {
                        inGcProgress.countDown();
                        try {
                            createLatch.await();
                        } catch (InterruptedException ie) {
                            Thread.currentThread().interrupt();
                        }
                        paused = true;
                    }
                    LOG.info("Garbage Collected ledger {}", ledgerId);
                }
            });
            LOG.info("Gc Thread quits.");
            endLatch.countDown();
        }
    };
    Thread createThread = new Thread() {

        @Override
        public void run() {
            try {
                inGcProgress.await();
                // create 10 more ledgers
                createLedgers(10, createdLedgers);
                LOG.info("Finished creating 10 more ledgers.");
                createLatch.countDown();
            } catch (Exception e) {
            }
            LOG.info("Create Thread quits.");
            endLatch.countDown();
        }
    };
    createThread.start();
    gcThread.start();
    endLatch.await();
    // test ledgers
    for (Long ledger : removedLedgers) {
        assertFalse(activeLedgers.containsKey(ledger));
    }
    for (Long ledger : createdLedgers) {
        assertTrue(activeLedgers.containsKey(ledger));
    }
    assertTrue("Wrong DELETED_LEDGER_COUNT", stats.getCounter("gc." + DELETED_LEDGER_COUNT).get() == removedLedgers.size());
    assertTrue("Wrong ACTIVE_LEDGER_COUNT", stats.getGauge("gc." + ACTIVE_LEDGER_COUNT).getSample().intValue() == createdLedgers.size());
}
Also used : TestStatsProvider(org.apache.bookkeeper.test.TestStatsProvider) ArrayList(java.util.ArrayList) ScanAndCompareGarbageCollector(org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Checkpoint(org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint) BookieException(org.apache.bookkeeper.bookie.BookieException) BKException(org.apache.bookkeeper.client.BKException) IOException(java.io.IOException) Random(java.util.Random) CompactableLedgerStorage(org.apache.bookkeeper.bookie.CompactableLedgerStorage) HashSet(java.util.HashSet) ScanAndCompareGarbageCollector(org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector) GarbageCollector(org.apache.bookkeeper.bookie.GarbageCollector) Test(org.junit.Test)

Example 4 with TestStatsProvider

use of org.apache.bookkeeper.test.TestStatsProvider in project bookkeeper by apache.

the class GcLedgersTest method testGcLedgersOutsideRange.

@Test
public void testGcLedgersOutsideRange() throws Exception {
    final SortedSet<Long> createdLedgers = Collections.synchronizedSortedSet(new TreeSet<Long>());
    final Queue<Long> cleaned = new LinkedList<Long>();
    int numLedgers = 100;
    createLedgers(numLedgers, createdLedgers);
    MockLedgerStorage mockLedgerStorage = new MockLedgerStorage();
    TestStatsProvider stats = new TestStatsProvider();
    final GarbageCollector garbageCollector = new ScanAndCompareGarbageCollector(getLedgerManager(), mockLedgerStorage, baseConf, stats.getStatsLogger("gc"));
    GarbageCollector.GarbageCleaner cleaner = new GarbageCollector.GarbageCleaner() {

        @Override
        public void clean(long ledgerId) {
            LOG.info("Cleaned {}", ledgerId);
            cleaned.add(ledgerId);
            try {
                mockLedgerStorage.deleteLedger(ledgerId);
            } catch (IOException e) {
                e.printStackTrace();
                fail("Exception from deleteLedger");
            }
        }
    };
    garbageCollector.gc(cleaner);
    assertNull("Should have cleaned nothing", cleaned.poll());
    assertTrue("Wrong DELETED_LEDGER_COUNT", stats.getCounter("gc." + DELETED_LEDGER_COUNT).get() == 0);
    assertTrue("Wrong ACTIVE_LEDGER_COUNT", stats.getGauge("gc." + ACTIVE_LEDGER_COUNT).getSample().intValue() == numLedgers);
    long last = createdLedgers.last();
    removeLedger(last);
    garbageCollector.gc(cleaner);
    assertNotNull("Should have cleaned something", cleaned.peek());
    assertEquals("Should have cleaned last ledger" + last, (long) last, (long) cleaned.poll());
    assertTrue("Wrong DELETED_LEDGER_COUNT", stats.getCounter("gc." + DELETED_LEDGER_COUNT).get() == 1);
    long first = createdLedgers.first();
    removeLedger(first);
    garbageCollector.gc(cleaner);
    assertNotNull("Should have cleaned something", cleaned.peek());
    assertEquals("Should have cleaned first ledger" + first, (long) first, (long) cleaned.poll());
    assertTrue("Wrong DELETED_LEDGER_COUNT", stats.getCounter("gc." + DELETED_LEDGER_COUNT).get() == 2);
    garbageCollector.gc(cleaner);
    assertTrue("Wrong ACTIVE_LEDGER_COUNT", stats.getGauge("gc." + ACTIVE_LEDGER_COUNT).getSample().intValue() == (numLedgers - 2));
}
Also used : TestStatsProvider(org.apache.bookkeeper.test.TestStatsProvider) ScanAndCompareGarbageCollector(org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Checkpoint(org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint) ScanAndCompareGarbageCollector(org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector) GarbageCollector(org.apache.bookkeeper.bookie.GarbageCollector) Test(org.junit.Test)

Example 5 with TestStatsProvider

use of org.apache.bookkeeper.test.TestStatsProvider in project bookkeeper by apache.

the class TestSpeculativeRead method createClient.

@SuppressWarnings("deprecation")
BookKeeperTestClient createClient(int specTimeout) throws Exception {
    ClientConfiguration conf = new ClientConfiguration().setSpeculativeReadTimeout(specTimeout).setReadTimeout(30000).setReorderReadSequenceEnabled(true).setEnsemblePlacementPolicySlowBookies(true);
    conf.setZkServers(zkUtil.getZooKeeperConnectString());
    return new BookKeeperTestClient(conf, new TestStatsProvider());
}
Also used : TestStatsProvider(org.apache.bookkeeper.test.TestStatsProvider) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration)

Aggregations

TestStatsProvider (org.apache.bookkeeper.test.TestStatsProvider)8 Test (org.junit.Test)5 File (java.io.File)3 IOException (java.io.IOException)3 ClientConfiguration (org.apache.bookkeeper.conf.ClientConfiguration)3 InetSocketAddress (java.net.InetSocketAddress)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Checkpoint (org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint)2 GarbageCollector (org.apache.bookkeeper.bookie.GarbageCollector)2 ScanAndCompareGarbageCollector (org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector)2 BKException (org.apache.bookkeeper.client.BKException)2 BookKeeperTestClient (org.apache.bookkeeper.client.BookKeeperTestClient)2 ServerConfiguration (org.apache.bookkeeper.conf.ServerConfiguration)2 BookieServer (org.apache.bookkeeper.proto.BookieServer)2 DiskChecker (org.apache.bookkeeper.util.DiskChecker)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1