Search in sources :

Example 1 with UnhandledErrorListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.UnhandledErrorListener in project coprhd-controller by CoprHD.

the class ZkConnection method build.

/**
 * Builds zk connector. Note that this method does not initiate a connection. {@link ZkConnection#connect()} must be called to connect
 * to cluster.
 * <p/>
 * This separation is provided so that callbacks can be setup separately prior to connection to cluster.
 */
public void build() {
    try {
        _zkConnection = CuratorFrameworkFactory.builder().connectString(_connectString).connectionTimeoutMs(DEFAULT_CONN_TIMEOUT).canBeReadOnly(true).sessionTimeoutMs(_timeoutMs).retryPolicy(new RetryUntilElapsed(_timeoutMs, RETRY_INTERVAL_MS)).build();
        _zkConnection.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {

            @Override
            public void unhandledError(String message, Throwable e) {
                _logger.warn("Unknown exception in curator stack", e);
            }
        });
        _zkConnection.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                _logger.info("Current connection state {}", newState);
            }
        });
        if (FileUtils.exists(siteIdFile)) {
            siteId = new String(FileUtils.readDataFromFile(siteIdFile));
            siteId = siteId.trim();
            _logger.info("Current site id is {}", siteId);
        }
    } catch (Exception e) {
        throw CoordinatorException.fatals.failedToBuildZKConnector(e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryUntilElapsed(org.apache.curator.retry.RetryUntilElapsed) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) IOException(java.io.IOException) CoordinatorException(com.emc.storageos.coordinator.exceptions.CoordinatorException)

Example 2 with UnhandledErrorListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.UnhandledErrorListener in project xian by happyyangyuan.

the class TestFrameworkBackground method testErrorListener.

@Test
public void testErrorListener() throws Exception {
    ACLProvider badAclProvider = new ACLProvider() {

        @Override
        public List<ACL> getDefaultAcl() {
            throw new UnsupportedOperationException();
        }

        @Override
        public List<ACL> getAclForPath(String path) {
            throw new UnsupportedOperationException();
        }
    };
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).aclProvider(badAclProvider).build();
    try {
        client.start();
        final CountDownLatch errorLatch = new CountDownLatch(1);
        UnhandledErrorListener listener = new UnhandledErrorListener() {

            @Override
            public void unhandledError(String message, Throwable e) {
                if (e instanceof UnsupportedOperationException) {
                    errorLatch.countDown();
                }
            }
        };
        client.create().inBackground().withUnhandledErrorListener(listener).forPath("/foo");
        Assert.assertTrue(new Timing().awaitLatch(errorLatch));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ACL(org.apache.zookeeper.data.ACL) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 3 with UnhandledErrorListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.UnhandledErrorListener in project xian by happyyangyuan.

the class TestFrameworkBackground method testShutdown.

/**
 * CURATOR-126
 * Shutdown the Curator client while there are still background operations running.
 */
@Test
public void testShutdown() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1)).maxCloseWaitMs(timing.forWaiting().milliseconds()).build();
    try {
        final AtomicBoolean hadIllegalStateException = new AtomicBoolean(false);
        ((CuratorFrameworkImpl) client).debugUnhandledErrorListener = new UnhandledErrorListener() {

            @Override
            public void unhandledError(String message, Throwable e) {
                if (e instanceof IllegalStateException) {
                    hadIllegalStateException.set(true);
                }
            }
        };
        client.start();
        final CountDownLatch operationReadyLatch = new CountDownLatch(1);
        ((CuratorFrameworkImpl) client).debugListener = new CuratorFrameworkImpl.DebugBackgroundListener() {

            @Override
            public void listen(OperationAndData<?> data) {
                try {
                    operationReadyLatch.await();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        };
        // queue a background operation that will block due to the debugListener
        client.create().inBackground().forPath("/hey");
        timing.sleepABit();
        // close the client while the background is still blocked
        client.close();
        // unblock the background
        operationReadyLatch.countDown();
        timing.sleepABit();
        // should not generate an exception
        Assert.assertFalse(hadIllegalStateException.get());
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) Test(org.testng.annotations.Test)

Example 4 with UnhandledErrorListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.UnhandledErrorListener in project xian by happyyangyuan.

the class TestLeaderAcls method testAclErrorWithLeader.

@Test(description = "Validation test for CURATOR-365")
public void testAclErrorWithLeader() throws Exception {
    ACLProvider provider = new ACLProvider() {

        @Override
        public List<ACL> getDefaultAcl() {
            return ZooDefs.Ids.OPEN_ACL_UNSAFE;
        }

        @Override
        public List<ACL> getAclForPath(String path) {
            if (path.equals("/base")) {
                try {
                    String testDigest = DigestAuthenticationProvider.generateDigest("test:test");
                    return Collections.singletonList(new ACL(ZooDefs.Perms.ALL, new Id("digest", testDigest)));
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
            }
            return getDefaultAcl();
        }
    };
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(timing.milliseconds(), 3);
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(retryPolicy).aclProvider(provider).authorization("digest", "test:test".getBytes());
    CuratorFramework client = builder.build();
    LeaderLatch latch = null;
    try {
        client.start();
        latch = new LeaderLatch(client, "/base");
        latch.start();
        Assert.assertTrue(latch.await(timing.forWaiting().seconds(), TimeUnit.SECONDS));
        latch.close();
        latch = null;
        CuratorFramework noAuthClient = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy);
        try {
            noAuthClient.start();
            final CountDownLatch noAuthLatch = new CountDownLatch(1);
            UnhandledErrorListener listener = new UnhandledErrorListener() {

                @Override
                public void unhandledError(String message, Throwable e) {
                    if (e instanceof KeeperException.NoAuthException) {
                        noAuthLatch.countDown();
                    }
                }
            };
            noAuthClient.getUnhandledErrorListenable().addListener(listener);
            // use a path below "base" as noAuthClient is not authorized to create nodes in "/base"
            // but also making sure that the code goes through the backgroundCreateParentsThenNode() codepath
            latch = new LeaderLatch(noAuthClient, "/base/second");
            latch.start();
            Assert.assertTrue(timing.awaitLatch(noAuthLatch));
        } finally {
            CloseableUtils.closeQuietly(noAuthClient);
        }
    } finally {
        CloseableUtils.closeQuietly(latch);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ACL(org.apache.zookeeper.data.ACL) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) Id(org.apache.zookeeper.data.Id) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) RetryPolicy(org.apache.curator.RetryPolicy) Test(org.testng.annotations.Test)

Example 5 with UnhandledErrorListener

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.api.UnhandledErrorListener in project xian by happyyangyuan.

the class TestNodeCache method testDeleteThenCreate.

@Test
public void testDeleteThenCreate() throws Exception {
    NodeCache cache = null;
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    client.start();
    try {
        client.create().creatingParentsIfNeeded().forPath("/test/foo", "one".getBytes());
        final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
        client.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {

            @Override
            public void unhandledError(String message, Throwable e) {
                error.set(e);
            }
        });
        final Semaphore semaphore = new Semaphore(0);
        cache = new NodeCache(client, "/test/foo");
        cache.getListenable().addListener(new NodeCacheListener() {

            @Override
            public void nodeChanged() throws Exception {
                semaphore.release();
            }
        });
        cache.start(true);
        Assert.assertEquals(cache.getCurrentData().getData(), "one".getBytes());
        client.delete().forPath("/test/foo");
        Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS));
        client.create().forPath("/test/foo", "two".getBytes());
        Assert.assertTrue(semaphore.tryAcquire(1, 10, TimeUnit.SECONDS));
        Throwable t = error.get();
        if (t != null) {
            Assert.fail("Assert", t);
        }
        Assert.assertEquals(cache.getCurrentData().getData(), "two".getBytes());
        cache.close();
    } finally {
        CloseableUtils.closeQuietly(cache);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) AtomicReference(java.util.concurrent.atomic.AtomicReference) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) Semaphore(java.util.concurrent.Semaphore) Test(org.testng.annotations.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)8 UnhandledErrorListener (org.apache.curator.framework.api.UnhandledErrorListener)8 Test (org.testng.annotations.Test)6 RetryOneTime (org.apache.curator.retry.RetryOneTime)4 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Timing (org.apache.curator.test.Timing)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ACLProvider (org.apache.curator.framework.api.ACLProvider)2 ACL (org.apache.zookeeper.data.ACL)2 CoordinatorException (com.emc.storageos.coordinator.exceptions.CoordinatorException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1