Search in sources :

Example 11 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.

the class TestFailedDeleteManager method testLostSession.

@Test
public void testLostSession() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
    try {
        client.start();
        client.create().forPath("/test-me");
        final CountDownLatch latch = new CountDownLatch(1);
        final Semaphore semaphore = new Semaphore(0);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if ((newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)) {
                    semaphore.release();
                } else if (newState == ConnectionState.RECONNECTED) {
                    latch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        server.stop();
        Assert.assertTrue(timing.acquireSemaphore(semaphore));
        try {
            client.delete().guaranteed().forPath("/test-me");
            Assert.fail();
        } catch (KeeperException.ConnectionLossException e) {
        // expected
        }
        Assert.assertTrue(timing.acquireSemaphore(semaphore));
        timing.sleepABit();
        server.restart();
        Assert.assertTrue(timing.awaitLatch(latch));
        timing.sleepABit();
        Assert.assertNull(client.checkExists().forPath("/test-me"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Timing(org.apache.curator.test.Timing) Semaphore(java.util.concurrent.Semaphore) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 12 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.

the class TestWithCluster method testSplitBrain.

@Test
public void testSplitBrain() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = null;
    TestingCluster cluster = new TestingCluster(3);
    cluster.start();
    try {
        // make sure all instances are up
        for (InstanceSpec instanceSpec : cluster.getInstances()) {
            client = CuratorFrameworkFactory.newClient(instanceSpec.getConnectString(), new RetryOneTime(1));
            client.start();
            client.checkExists().forPath("/");
            client.close();
            client = null;
        }
        client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        client.start();
        final CountDownLatch latch = new CountDownLatch(2);
        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
                    latch.countDown();
                }
            }
        });
        client.checkExists().forPath("/");
        for (InstanceSpec instanceSpec : cluster.getInstances()) {
            if (!instanceSpec.equals(cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper()))) {
                Assert.assertTrue(cluster.killServer(instanceSpec));
            }
        }
        Assert.assertTrue(timing.awaitLatch(latch));
    } finally {
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) InstanceSpec(org.apache.curator.test.InstanceSpec) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 13 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.

the class TestLeaderLatch method testLostConnection.

@Test
public void testLostConnection() throws Exception {
    final int PARTICIPANT_QTY = 10;
    List<LeaderLatch> latches = Lists.newArrayList();
    final Timing timing = new Timing();
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    countDownLatch.countDown();
                }
            }
        });
        for (int i = 0; i < PARTICIPANT_QTY; ++i) {
            LeaderLatch latch = new LeaderLatch(client, PATH_NAME);
            latch.start();
            latches.add(latch);
        }
        waitForALeader(latches, timing);
        server.stop();
        Assert.assertTrue(timing.awaitLatch(countDownLatch));
        timing.forWaiting().sleepABit();
        Assert.assertEquals(getLeaders(latches).size(), 0);
        server.restart();
        // should reconnect
        Assert.assertEquals(waitForALeader(latches, timing).size(), 1);
    } finally {
        for (LeaderLatch latch : latches) {
            CloseableUtils.closeQuietly(latch);
        }
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 14 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.

the class TestLeaderSelector method testLeaderNodeDeleteOnInterrupt.

@Test
public void testLeaderNodeDeleteOnInterrupt() throws Exception {
    Timing timing = new Timing();
    LeaderSelector selector = null;
    CuratorFramework client = null;
    try {
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        final CountDownLatch reconnectedLatch = new CountDownLatch(1);
        ConnectionStateListener connectionStateListener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.RECONNECTED) {
                    reconnectedLatch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(connectionStateListener);
        client.start();
        final BlockingQueue<Thread> queue = new ArrayBlockingQueue<Thread>(1);
        LeaderSelectorListener listener = new LeaderSelectorListener() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                queue.add(Thread.currentThread());
                try {
                    Thread.currentThread().join();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        selector = new LeaderSelector(client, "/leader", listener);
        selector.start();
        Thread leaderThread = queue.take();
        server.stop();
        leaderThread.interrupt();
        server.restart();
        Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
        timing.sleepABit();
        Assert.assertEquals(client.getChildren().forPath("/leader").size(), 0);
    } finally {
        CloseableUtils.closeQuietly(selector);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 15 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.

the class TestInterProcessMutexBase method testWaitingProcessKilledServer.

@Test
public void testWaitingProcessKilledServer() throws Exception {
    final Timing timing = new Timing();
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
    try {
        client.start();
        final CountDownLatch latch = new CountDownLatch(1);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    latch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        final AtomicBoolean isFirst = new AtomicBoolean(true);
        ExecutorCompletionService<Object> service = new ExecutorCompletionService<Object>(Executors.newFixedThreadPool(2));
        for (int i = 0; i < 2; ++i) {
            service.submit(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    InterProcessLock lock = makeLock(client);
                    lock.acquire();
                    try {
                        if (isFirst.compareAndSet(true, false)) {
                            timing.sleepABit();
                            server.stop();
                            Assert.assertTrue(timing.awaitLatch(latch));
                            server.restart();
                        }
                    } finally {
                        try {
                            lock.release();
                        } catch (Exception e) {
                        // ignore
                        }
                    }
                    return null;
                }
            });
        }
        for (int i = 0; i < 2; ++i) {
            service.take().get(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
        }
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)37 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)37 ConnectionState (org.apache.curator.framework.state.ConnectionState)36 Test (org.testng.annotations.Test)29 Timing (org.apache.curator.test.Timing)24 CountDownLatch (java.util.concurrent.CountDownLatch)23 RetryOneTime (org.apache.curator.retry.RetryOneTime)20 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)9 KeeperException (org.apache.zookeeper.KeeperException)9 TestingCluster (org.apache.curator.test.TestingCluster)5 TestingServer (org.apache.curator.test.TestingServer)5 IOException (java.io.IOException)4 Semaphore (java.util.concurrent.Semaphore)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 InstanceSpec (org.apache.curator.test.InstanceSpec)4 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)3 ExecutorService (java.util.concurrent.ExecutorService)3 RetryNTimes (org.apache.curator.retry.RetryNTimes)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2