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());
}
}
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());
}
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());
}
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));
}
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());
}
Aggregations