Search in sources :

Example 86 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project BRFS by zhangnianli.

the class DiscoveryExample method main.

public static void main(String[] args) throws Exception {
    // This method is scaffolding to get the example up and running
    CuratorFramework client = null;
    ServiceDiscovery<InstanceDetails> serviceDiscovery = null;
    Map<String, ServiceProvider<InstanceDetails>> providers = Maps.newHashMap();
    try {
        client = CuratorFrameworkFactory.newClient("192.168.101.86:2181", new ExponentialBackoffRetry(1000, 3));
        client.start();
        JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class);
        serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(PATH).serializer(serializer).build();
        serviceDiscovery.start();
        processCommands(serviceDiscovery, providers, client);
    } finally {
        for (ServiceProvider<InstanceDetails> cache : providers.values()) {
            CloseableUtils.closeQuietly(cache);
        }
        CloseableUtils.closeQuietly(serviceDiscovery);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) JsonInstanceSerializer(org.apache.curator.x.discovery.details.JsonInstanceSerializer) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ServiceProvider(org.apache.curator.x.discovery.ServiceProvider)

Example 87 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project BRFS by zhangnianli.

the class TestServiceManager method main.

public static void main(String[] args) throws Exception {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient(zk_address, 5 * 1000, 30 * 1000, retryPolicy);
    client.start();
    client.blockUntilConnected();
    ServiceManager sm = new DefaultServiceManager(client.usingNamespace("test_c"));
    sm.start();
    sm.addServiceStateListener("ss_g", new ServiceStateListener() {

        @Override
        public void serviceRemoved(Service service) {
        // TODO Auto-generated method stub
        }

        @Override
        public void serviceAdded(Service service) {
            System.out.println("##########################################before add " + service);
        }
    });
    sm.registerService(new Service("ss_1", "ss_g", "localhost", 999));
    Thread.sleep(1000);
    sm.addServiceStateListener("ss_g", new ServiceStateListener() {

        @Override
        public void serviceRemoved(Service service) {
        // TODO Auto-generated method stub
        }

        @Override
        public void serviceAdded(Service service) {
            System.out.println("##########################################after add " + service);
        }
    });
    sm.addServiceStateListener("ss_g", new ServiceStateListener() {

        @Override
        public void serviceRemoved(Service service) {
        // TODO Auto-generated method stub
        }

        @Override
        public void serviceAdded(Service service) {
            System.out.println("##########################################after_2 add " + service);
        }
    });
    sm.registerService(new Service("ss_2", "ss_g", "localhost", 1222));
    synchronized (sm) {
        sm.wait();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) DefaultServiceManager(com.bonree.brfs.common.service.impl.DefaultServiceManager) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) DefaultServiceManager(com.bonree.brfs.common.service.impl.DefaultServiceManager) RetryPolicy(org.apache.curator.RetryPolicy)

Example 88 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project BRFS by zhangnianli.

the class TestService method main.

public static void main(String[] args) throws Exception {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.101.86:2181", 3000, 3000, retryPolicy);
    client.start();
    client.blockUntilConnected();
    ServiceManager sm = new DefaultServiceManager(client.usingNamespace("test"));
    sm.start();
    Service s = new Service();
    s.setServiceGroup("group");
    s.setServiceId("ser_" + ID);
    s.setHost("local");
    sm.registerService(s);
    Service tmp = sm.getServiceById(s.getServiceGroup(), s.getServiceId());
    System.out.println(tmp);
    sm.addServiceStateListener("group", new ServiceStateListener() {

        @Override
        public void serviceRemoved(Service service) {
            System.out.println("remove--" + service.getServiceId());
        }

        @Override
        public void serviceAdded(Service service) {
            System.out.println("add--" + service.getServiceId());
        }
    });
    System.out.println(sm.getServiceById(s.getServiceGroup(), s.getServiceId()));
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) DefaultServiceManager(com.bonree.brfs.common.service.impl.DefaultServiceManager) ServiceStateListener(com.bonree.brfs.common.service.ServiceStateListener) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) DefaultServiceManager(com.bonree.brfs.common.service.impl.DefaultServiceManager) ServiceManager(com.bonree.brfs.common.service.ServiceManager) Service(com.bonree.brfs.common.service.Service) RetryPolicy(org.apache.curator.RetryPolicy)

Example 89 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project pravega by pravega.

the class ClusterZKTest method deregisterNode.

@Test(timeout = TEST_TIMEOUT)
public void deregisterNode() throws Exception {
    LinkedBlockingQueue<String> nodeAddedQueue = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<String> nodeRemovedQueue = new LinkedBlockingQueue<>();
    LinkedBlockingQueue<Exception> exceptionsQueue = new LinkedBlockingQueue<>();
    @Cleanup CuratorFramework client2 = CuratorFrameworkFactory.builder().connectString(zkUrl).retryPolicy(new ExponentialBackoffRetry(RETRY_SLEEP_MS, MAX_RETRY)).namespace(CLUSTER_NAME_2).build();
    @Cleanup Cluster clusterListener = new ClusterZKImpl(client2, ClusterType.HOST);
    clusterListener.addListener((eventType, host) -> {
        switch(eventType) {
            case HOST_ADDED:
                nodeAddedQueue.offer(host.getIpAddr());
                break;
            case HOST_REMOVED:
                nodeRemovedQueue.offer(host.getIpAddr());
                break;
            case ERROR:
                exceptionsQueue.offer(new RuntimeException("Encountered error"));
                break;
            default:
                exceptionsQueue.offer(new RuntimeException("Unhandled case"));
                break;
        }
    });
    @Cleanup CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkUrl).retryPolicy(new ExponentialBackoffRetry(RETRY_SLEEP_MS, MAX_RETRY)).namespace(CLUSTER_NAME_2).build();
    // Create Add a node to the cluster.
    @Cleanup Cluster clusterZKInstance1 = new ClusterZKImpl(client, ClusterType.HOST);
    clusterZKInstance1.registerHost(new Host(HOST_1, PORT, null));
    assertEquals(HOST_1, nodeAddedQueue.poll(5, TimeUnit.SECONDS));
    clusterZKInstance1.deregisterHost(new Host(HOST_1, PORT, null));
    assertEquals(HOST_1, nodeRemovedQueue.poll(5, TimeUnit.SECONDS));
    Exception exception = exceptionsQueue.poll();
    if (exception != null) {
        throw exception;
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Cluster(io.pravega.common.cluster.Cluster) Host(io.pravega.common.cluster.Host) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Cleanup(lombok.Cleanup) IOException(java.io.IOException) Test(org.junit.Test)

Example 90 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project pravega by pravega.

the class ZKSegmentContainerMonitorTest method startClient.

private CuratorFramework startClient() {
    val client = CuratorFrameworkFactory.newClient(zkUrl, new ExponentialBackoffRetry(RETRY_SLEEP_MS, MAX_RETRY));
    client.start();
    return client;
}
Also used : lombok.val(lombok.val) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry)

Aggregations

ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)181 CuratorFramework (org.apache.curator.framework.CuratorFramework)107 RetryPolicy (org.apache.curator.RetryPolicy)46 Before (org.junit.Before)26 TestingCluster (org.apache.curator.test.TestingCluster)23 Test (org.testng.annotations.Test)23 IOException (java.io.IOException)18 TestingServer (org.apache.curator.test.TestingServer)18 Timing (org.apache.curator.test.Timing)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)12 ACLProvider (org.apache.curator.framework.api.ACLProvider)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)11 ConnectionState (org.apache.curator.framework.state.ConnectionState)11 ExecutorService (java.util.concurrent.ExecutorService)10 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)10 TestingServerStarter (io.pravega.test.common.TestingServerStarter)9 KeeperException (org.apache.zookeeper.KeeperException)8 HashMap (java.util.HashMap)7