Search in sources :

Example 31 with ConnectionStateListener

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

the class TestInterProcessSemaphore method testAcquireAfterLostServer.

@Test
public void testAcquireAfterLostServer() throws Exception {
    // CURATOR-335
    final String SEMAPHORE_PATH = "/test";
    final int MAX_SEMAPHORES = 1;
    final int NUM_CLIENTS = 10;
    ExecutorService executor = Executors.newFixedThreadPool(NUM_CLIENTS);
    final Timing timing = new Timing();
    // long session time on purpose
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.forWaiting().milliseconds(), timing.connection(), new RetryOneTime(1));
    try {
        client.start();
        // cause one of the semaphores to create its node and then wait
        InterProcessSemaphoreV2.debugAcquireLatch = new CountDownLatch(1);
        // semaphore will notify when getChildren() fails
        InterProcessSemaphoreV2.debugFailedGetChildrenLatch = new CountDownLatch(1);
        final CountDownLatch isReadyLatch = new CountDownLatch(NUM_CLIENTS);
        final BlockingQueue<Boolean> acquiredQueue = Queues.newLinkedBlockingQueue();
        Runnable runner = new Runnable() {

            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, SEMAPHORE_PATH, MAX_SEMAPHORES);
                    Lease lease = null;
                    try {
                        isReadyLatch.countDown();
                        lease = semaphore.acquire();
                        acquiredQueue.add(true);
                        timing.sleepABit();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        break;
                    } catch (KeeperException e) {
                        try {
                            timing.sleepABit();
                        } catch (InterruptedException e2) {
                            Thread.currentThread().interrupt();
                            break;
                        }
                    } catch (Exception ignore) {
                    // ignore
                    } finally {
                        if (lease != null) {
                            semaphore.returnLease(lease);
                        }
                    }
                }
            }
        };
        for (int i = 0; i < NUM_CLIENTS; ++i) {
            executor.execute(runner);
        }
        Assert.assertTrue(timing.awaitLatch(isReadyLatch));
        timing.sleepABit();
        final CountDownLatch lostLatch = new CountDownLatch(1);
        final CountDownLatch restartedLatch = new CountDownLatch(1);
        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    lostLatch.countDown();
                } else if (newState == ConnectionState.RECONNECTED) {
                    restartedLatch.countDown();
                }
            }
        });
        timing.sleepABit();
        server.stop();
        Assert.assertTrue(timing.awaitLatch(lostLatch));
        // the waiting semaphore proceeds to getChildren - which should fail
        InterProcessSemaphoreV2.debugAcquireLatch.countDown();
        // wait until getChildren fails
        Assert.assertTrue(timing.awaitLatch(InterProcessSemaphoreV2.debugFailedGetChildrenLatch));
        server.restart();
        Assert.assertTrue(timing.awaitLatch(restartedLatch));
        for (int i = 0; i < NUM_CLIENTS; ++i) {
            // acquires should continue as normal after server restart
            Boolean polled = acquiredQueue.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
            if ((polled == null) || !polled) {
                Assert.fail("Semaphores not reacquired after restart");
            }
        }
    } finally {
        executor.shutdownNow();
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExecutorService(java.util.concurrent.ExecutorService) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) KeeperException(org.apache.zookeeper.KeeperException) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 32 with ConnectionStateListener

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

the class TestInterProcessSemaphoreCluster method testCluster.

@Test
public void testCluster() throws Exception {
    final int QTY = 20;
    final int OPERATION_TIME_MS = 1000;
    final String PATH = "/foo/bar/lock";
    ExecutorService executorService = Executors.newFixedThreadPool(QTY);
    ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
    final Timing timing = new Timing();
    TestingCluster cluster = new TestingCluster(3);
    List<SemaphoreClient> semaphoreClients = Lists.newArrayList();
    try {
        cluster.start();
        final AtomicInteger opCount = new AtomicInteger(0);
        for (int i = 0; i < QTY; ++i) {
            SemaphoreClient semaphoreClient = new SemaphoreClient(cluster.getConnectString(), PATH, new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    opCount.incrementAndGet();
                    Thread.sleep(OPERATION_TIME_MS);
                    return null;
                }
            });
            completionService.submit(semaphoreClient);
            semaphoreClients.add(semaphoreClient);
        }
        timing.forWaiting().sleepABit();
        Assert.assertNotNull(SemaphoreClient.getActiveClient());
        final CountDownLatch latch = new CountDownLatch(1);
        CuratorFramework client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    latch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        client.start();
        try {
            client.getZookeeperClient().blockUntilConnectedOrTimedOut();
            cluster.stop();
            latch.await();
        } finally {
            CloseableUtils.closeQuietly(client);
        }
        long startTicks = System.currentTimeMillis();
        for (; ; ) {
            int thisOpCount = opCount.get();
            Thread.sleep(2 * OPERATION_TIME_MS);
            if (thisOpCount == opCount.get()) {
                // checking that the op count isn't increasing
                break;
            }
            Assert.assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
        }
        int thisOpCount = opCount.get();
        Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
        cluster = new TestingCluster(iterator.next(), iterator.next());
        cluster.start();
        timing.forWaiting().sleepABit();
        startTicks = System.currentTimeMillis();
        for (; ; ) {
            Thread.sleep(2 * OPERATION_TIME_MS);
            if (opCount.get() > thisOpCount) {
                // checking that semaphore has started working again
                break;
            }
            Assert.assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
        }
    } finally {
        for (SemaphoreClient semaphoreClient : semaphoreClients) {
            CloseableUtils.closeQuietly(semaphoreClient);
        }
        CloseableUtils.closeQuietly(cluster);
        executorService.shutdownNow();
    }
}
Also used : InstanceSpec(org.apache.curator.test.InstanceSpec) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) 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)

Example 33 with ConnectionStateListener

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

the class TestSharedCount method testDisconnectEventOnWatcherDoesNotRetry.

@Test
public void testDisconnectEventOnWatcherDoesNotRetry() throws Exception {
    final CountDownLatch gotSuspendEvent = new CountDownLatch(1);
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryNTimes(10, 1000));
    curatorFramework.start();
    curatorFramework.blockUntilConnected();
    SharedCount sharedCount = new SharedCount(curatorFramework, "/count", 10);
    sharedCount.start();
    curatorFramework.getConnectionStateListenable().addListener(new ConnectionStateListener() {

        @Override
        public void stateChanged(CuratorFramework client, ConnectionState newState) {
            if (newState == ConnectionState.SUSPENDED) {
                gotSuspendEvent.countDown();
            }
        }
    });
    try {
        server.stop();
        // if watcher goes into 10second retry loop we won't get timely notification
        Assert.assertTrue(gotSuspendEvent.await(5, TimeUnit.SECONDS));
    } finally {
        CloseableUtils.closeQuietly(sharedCount);
        CloseableUtils.closeQuietly(curatorFramework);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) 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 34 with ConnectionStateListener

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

the class TestPathChildrenCache method testWithBadConnect.

@Test
public void testWithBadConnect() throws Exception {
    final int serverPort = server.getPort();
    server.close();
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 1000, 1000, new RetryOneTime(1));
    try {
        client.start();
        final CountDownLatch ensurePathLatch = new CountDownLatch(1);
        PathChildrenCache cache = new PathChildrenCache(client, "/", true) {

            @Override
            protected void ensurePath() throws Exception {
                try {
                    super.ensurePath();
                } catch (Exception e) {
                    ensurePathLatch.countDown();
                    throw e;
                }
            }
        };
        final CountDownLatch addedLatch = new CountDownLatch(1);
        PathChildrenCacheListener listener = new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED && event.getData().getPath().equals("/baz")) {
                    addedLatch.countDown();
                }
            }
        };
        cache.getListenable().addListener(listener);
        cache.start();
        Assert.assertTrue(timing.awaitLatch(ensurePathLatch));
        final CountDownLatch connectedLatch = new CountDownLatch(1);
        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.CONNECTED) {
                    connectedLatch.countDown();
                }
            }
        });
        server = new TestingServer(serverPort, true);
        Assert.assertTrue(timing.awaitLatch(connectedLatch));
        client.create().creatingParentContainersIfNeeded().forPath("/baz", new byte[] { 1, 2, 3 });
        assertNotNull("/baz does not exist", client.checkExists().forPath("/baz"));
        Assert.assertTrue(timing.awaitLatch(addedLatch));
        assertNotNull("cache doesn't see /baz", cache.getCurrentData("/baz"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) RetryOneTime(org.apache.curator.retry.RetryOneTime) KeeperException(org.apache.zookeeper.KeeperException) 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)

Example 35 with ConnectionStateListener

use of org.apache.curator.framework.state.ConnectionStateListener in project fabric8 by jboss-fuse.

the class CuratorFrameworkImpl method start.

@Override
public void start() {
    log.info("Starting");
    if (!state.compareAndSet(CuratorFrameworkState.LATENT, CuratorFrameworkState.STARTED)) {
        throw new IllegalStateException("Cannot be started more than once");
    }
    try {
        // ordering dependency - must be called before client.start()
        connectionStateManager.start();
        final ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (ConnectionState.CONNECTED == newState || ConnectionState.RECONNECTED == newState) {
                    logAsErrorConnectionErrors.set(true);
                }
            }
        };
        this.getConnectionStateListenable().addListener(listener);
        client.start();
        executorService = Executors.newSingleThreadScheduledExecutor(threadFactory);
        executorService.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                backgroundOperationsLoop();
                return null;
            }
        });
    } catch (Exception e) {
        ThreadUtils.checkInterrupted(e);
        handleBackgroundOperationException(null, e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) KeeperException(org.apache.zookeeper.KeeperException) CuratorConnectionLossException(org.apache.curator.CuratorConnectionLossException)

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