Search in sources :

Example 11 with ClientX509Util

use of org.apache.zookeeper.common.ClientX509Util in project zookeeper by apache.

the class NettyServerCnxnTest method runEnableDisableThrottling.

private void runEnableDisableThrottling(boolean secure, boolean randomDisableEnable) throws Exception {
    ClientX509Util x509Util = null;
    if (secure) {
        x509Util = SSLAuthTest.setUpSecure();
    }
    try {
        NettyServerCnxnFactory factory = (NettyServerCnxnFactory) serverFactory;
        factory.setAdvancedFlowControlEnabled(true);
        if (secure) {
            factory.setSecure(true);
        }
        final String path = "/testEnableDisableThrottling";
        try (ZooKeeper zk = createClient()) {
            zk.create(path, new byte[1], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            // meanwhile start another thread to enable and disable recv
            AtomicBoolean stopped = new AtomicBoolean(false);
            Random random = new Random();
            Thread enableDisableThread = null;
            if (randomDisableEnable) {
                enableDisableThread = new Thread() {

                    @Override
                    public void run() {
                        while (!stopped.get()) {
                            for (final ServerCnxn cnxn : serverFactory.cnxns) {
                                boolean shouldDisableEnable = random.nextBoolean();
                                if (shouldDisableEnable) {
                                    cnxn.disableRecv();
                                } else {
                                    cnxn.enableRecv();
                                }
                            }
                            try {
                                Thread.sleep(10);
                            } catch (InterruptedException e) {
                            /* ignore */
                            }
                        }
                        // always enable the recv at end
                        for (final ServerCnxn cnxn : serverFactory.cnxns) {
                            cnxn.enableRecv();
                        }
                    }
                };
            } else {
                enableDisableThread = new Thread() {

                    @Override
                    public void run() {
                        while (!stopped.get()) {
                            for (final ServerCnxn cnxn : serverFactory.cnxns) {
                                try {
                                    cnxn.disableRecv();
                                    Thread.sleep(10);
                                    cnxn.enableRecv();
                                    Thread.sleep(10);
                                } catch (InterruptedException e) {
                                /* ignore */
                                }
                            }
                        }
                    }
                };
            }
            enableDisableThread.start();
            LOG.info("started thread to enable and disable recv");
            // start a thread to keep sending requests
            int totalRequestsNum = 100000;
            AtomicInteger successResponse = new AtomicInteger();
            CountDownLatch responseReceivedLatch = new CountDownLatch(totalRequestsNum);
            Thread clientThread = new Thread() {

                @Override
                public void run() {
                    int requestIssued = 0;
                    while (requestIssued++ < totalRequestsNum) {
                        zk.getData(path, null, new DataCallback() {

                            @Override
                            public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                                if (rc == KeeperException.Code.OK.intValue()) {
                                    successResponse.addAndGet(1);
                                } else {
                                    LOG.info("failed response is {}", rc);
                                }
                                responseReceivedLatch.countDown();
                            }
                        }, null);
                    }
                }
            };
            clientThread.start();
            LOG.info("started thread to issue {} async requests", totalRequestsNum);
            // and verify the response received is same as what we issued
            assertTrue(responseReceivedLatch.await(60, TimeUnit.SECONDS));
            LOG.info("received all {} responses", totalRequestsNum);
            stopped.set(true);
            enableDisableThread.join();
            LOG.info("enable and disable recv thread exited");
            // wait another second for the left requests to finish
            LOG.info("waiting another 1s for the requests to go through");
            Thread.sleep(1000);
            assertEquals(successResponse.get(), totalRequestsNum);
        }
    } finally {
        if (secure) {
            SSLAuthTest.clearSecureSetting(x509Util);
        }
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) DataCallback(org.apache.zookeeper.AsyncCallback.DataCallback) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientX509Util(org.apache.zookeeper.common.ClientX509Util) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 12 with ClientX509Util

use of org.apache.zookeeper.common.ClientX509Util in project zookeeper by apache.

the class SaslDigestAuthOverSSLTest method setUpSSLWithNoAuth.

public ClientX509Util setUpSSLWithNoAuth() {
    String testDataPath = System.getProperty("test.data.dir", "src/test/resources/data");
    System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory");
    System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty");
    System.setProperty(ZKClientConfig.SECURE_CLIENT, "true");
    System.setProperty("zookeeper.ssl.clientAuth", "none");
    System.setProperty("zookeeper.ssl.quorum.clientAuth", "none");
    ClientX509Util x509Util = new ClientX509Util();
    System.setProperty(x509Util.getSslTruststoreLocationProperty(), testDataPath + "/ssl/testTrustStore.jks");
    System.setProperty(x509Util.getSslTruststorePasswdProperty(), "testpass");
    System.setProperty(x509Util.getSslKeystoreLocationProperty(), testDataPath + "/ssl/testKeyStore.jks");
    System.setProperty(x509Util.getSslKeystorePasswdProperty(), "testpass");
    return x509Util;
}
Also used : ClientX509Util(org.apache.zookeeper.common.ClientX509Util)

Aggregations

ClientX509Util (org.apache.zookeeper.common.ClientX509Util)12 InetSocketAddress (java.net.InetSocketAddress)4 IOException (java.io.IOException)2 Socket (java.net.Socket)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 X509Util (org.apache.zookeeper.common.X509Util)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 ServerSocket (java.net.ServerSocket)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Properties (java.util.Properties)1 Random (java.util.Random)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SSLContext (javax.net.ssl.SSLContext)1 SSLSocket (javax.net.ssl.SSLSocket)1