Search in sources :

Example 1 with SimpleCounter

use of org.apache.zookeeper.server.metric.SimpleCounter in project zookeeper by apache.

the class NettyServerCnxnFactoryTest method testOutstandingHandshakeLimit.

/*
     * In this test we are flooding the server with SSL connections, and expecting that not
     * all the connection will succeed at once. Some of the connections should be closed,
     * as there is a maximum number of parallel SSL handshake the server is willing to do
     * for security reasons.
     */
@Test
public void testOutstandingHandshakeLimit() throws Exception {
    // setting up SSL params, but disable some debug logs
    x509Util = SSLAuthTest.setUpSecure();
    System.clearProperty("javax.net.debug");
    // starting a single server (it will be closed in the tearDown)
    setUpWithServerId(1);
    // initializing the statistics
    SimpleCounter tlsHandshakeExceeded = (SimpleCounter) ServerMetrics.getMetrics().TLS_HANDSHAKE_EXCEEDED;
    tlsHandshakeExceeded.reset();
    assertEquals(tlsHandshakeExceeded.get(), 0);
    // setting the HandshakeLimit to 3, so only 3 SSL handshakes can happen in parallel
    NettyServerCnxnFactory factory = (NettyServerCnxnFactory) serverFactory;
    factory.setSecure(true);
    factory.setOutstandingHandshakeLimit(3);
    // starting the threads that will try to connect to the server
    // we will have 3 threads, each of them establishing 3 connections
    int threadNum = 3;
    int cnxnPerThread = 3;
    int cnxnLimit = threadNum * cnxnPerThread;
    AtomicInteger cnxnCreated = new AtomicInteger(0);
    CountDownLatch latch = new CountDownLatch(1);
    Thread[] cnxnWorker = new Thread[threadNum];
    for (int i = 0; i < cnxnWorker.length; i++) {
        cnxnWorker[i] = new ClientConnectionGenerator(i, cnxnPerThread, cnxnCreated, cnxnLimit, latch, zooKeeperClients);
        cnxnWorker[i].start();
    }
    // we might need to wait potentially for a longer time for all the connection to get established,
    // as the ZooKeeper Server will close some of the connections and the clients will have to re-try
    boolean allConnectionsCreatedInTime = latch.await(30, TimeUnit.SECONDS);
    int actualConnections = cnxnCreated.get();
    LOG.info("created {} connections", actualConnections);
    if (!allConnectionsCreatedInTime) {
        fail(String.format("Only %d out of %d connections created!", actualConnections, cnxnLimit));
    }
    // Assert the server refused some of the connections because the handshake limit was reached
    // (throttling should be greater than 0)
    long handshakeThrottledNum = tlsHandshakeExceeded.get();
    LOG.info("TLS_HANDSHAKE_EXCEEDED: {}", handshakeThrottledNum);
    assertThat("The number of handshake throttled should be " + "greater than 0", handshakeThrottledNum, Matchers.greaterThan(0L));
    // Assert there is no outstanding handshake anymore, all the clients connected in the end
    int outstandingHandshakeNum = factory.getOutstandingHandshakeNum();
    LOG.info("outstanding handshake is {}", outstandingHandshakeNum);
    assertThat("The outstanding handshake number should be 0 " + "after all cnxns established", outstandingHandshakeNum, Matchers.is(0));
}
Also used : SimpleCounter(org.apache.zookeeper.server.metric.SimpleCounter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) SSLAuthTest(org.apache.zookeeper.test.SSLAuthTest) Test(org.junit.jupiter.api.Test)

Example 2 with SimpleCounter

use of org.apache.zookeeper.server.metric.SimpleCounter in project zookeeper by apache.

the class TxnLogDigestTest method digestFromTxnLogsMatchesTree.

/**
 * Check that the digest stored in the txn matches the digest calculated
 * from DataTree.
 */
@Test
public void digestFromTxnLogsMatchesTree() throws Exception {
    // reset the mismatch metrics
    SimpleCounter digestMistachesCount = (SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT;
    digestMistachesCount.reset();
    // trigger some write ops
    performOperations(createClient(), "/digestFromTxnLogsMatchesTree");
    // make sure there is no digest mismatch
    assertEquals(0, digestMistachesCount.get());
    // verify that the digest is wrote to disk with txn
    TxnDigest lastDigest = getLastTxnLogDigest();
    assertNotNull(lastDigest);
    assertEquals(server.getZKDatabase().getDataTree().getTreeDigest(), lastDigest.getTreeDigest());
}
Also used : SimpleCounter(org.apache.zookeeper.server.metric.SimpleCounter) TxnDigest(org.apache.zookeeper.txn.TxnDigest) QuorumPeerMainTest(org.apache.zookeeper.server.quorum.QuorumPeerMainTest) Test(org.junit.jupiter.api.Test)

Example 3 with SimpleCounter

use of org.apache.zookeeper.server.metric.SimpleCounter in project zookeeper by apache.

the class ServerMetricsTest method testSimpleCounter.

@Test
public void testSimpleCounter() {
    SimpleCounter metric = new SimpleCounter("test");
    testSimpleCounter(metric, 0);
    testSimpleCounter(metric, 1);
    for (int i = 0; i < RANDOM_TRIALS; ++i) {
        testSimpleCounter(metric, RANDOM_SIZE);
    }
}
Also used : SimpleCounter(org.apache.zookeeper.server.metric.SimpleCounter) Test(org.junit.jupiter.api.Test)

Example 4 with SimpleCounter

use of org.apache.zookeeper.server.metric.SimpleCounter in project zookeeper by apache.

the class TxnLogDigestTest method testTxnMissing.

/**
 * Simulate the scenario where txn is missing, and make sure the
 * digest code can catch this issue.
 */
@Test
public void testTxnMissing() throws Exception {
    // updated MockedFileTxnLog to skip append txn on specific txn
    MockedFileTxnLog.skipAppendZxid = 3;
    // trigger some write operations
    performOperations(createClient(), "/testTxnMissing");
    // restart server to load the corrupted txn file
    SimpleCounter digestMistachesCount = (SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT;
    digestMistachesCount.reset();
    restartServerWithDigestFlag(true);
    // check that digest mismatch is reported
    assertThat("mismtach should be reported", digestMistachesCount.get(), greaterThan(0L));
    // restart server with digest disabled
    digestMistachesCount.reset();
    restartServerWithDigestFlag(false);
    // check that no digest mismatch is reported
    assertEquals(0, digestMistachesCount.get());
}
Also used : SimpleCounter(org.apache.zookeeper.server.metric.SimpleCounter) QuorumPeerMainTest(org.apache.zookeeper.server.quorum.QuorumPeerMainTest) Test(org.junit.jupiter.api.Test)

Example 5 with SimpleCounter

use of org.apache.zookeeper.server.metric.SimpleCounter in project zookeeper by apache.

the class TxnLogDigestTest method checkTxnCompatibleWithAndWithoutDigest.

/**
 * Test the compatible when enable/disable digest:
 *
 * * check that txns which were written with digest can be read when
 *   digest is disabled
 * * check that txns which were written without digest can be read
 *   when digest is enabled.
 */
@Test
public void checkTxnCompatibleWithAndWithoutDigest() throws Exception {
    // 1. start server with digest disabled
    restartServerWithDigestFlag(false);
    // trigger some write ops
    Map<String, String> expectedNodes = performOperations(createClient(), "/p1");
    // reset the mismatch metrics
    SimpleCounter digestMistachesCount = (SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT;
    digestMistachesCount.reset();
    // 2. restart server with digest enabled
    restartServerWithDigestFlag(true);
    // make sure the data wrote when digest was disabled can be
    // successfully read
    checkNodes(expectedNodes);
    Map<String, String> expectedNodes1 = performOperations(createClient(), "/p2");
    // make sure there is no digest mismatch
    assertEquals(0, digestMistachesCount.get());
    // 3. disable the digest again and make sure everything is fine
    restartServerWithDigestFlag(false);
    checkNodes(expectedNodes);
    checkNodes(expectedNodes1);
}
Also used : SimpleCounter(org.apache.zookeeper.server.metric.SimpleCounter) QuorumPeerMainTest(org.apache.zookeeper.server.quorum.QuorumPeerMainTest) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleCounter (org.apache.zookeeper.server.metric.SimpleCounter)5 Test (org.junit.jupiter.api.Test)5 QuorumPeerMainTest (org.apache.zookeeper.server.quorum.QuorumPeerMainTest)3 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SSLAuthTest (org.apache.zookeeper.test.SSLAuthTest)1 TxnDigest (org.apache.zookeeper.txn.TxnDigest)1