Search in sources :

Example 41 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.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 42 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.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 43 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.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 44 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project incubator-dubbo-ops by apache.

the class ConfigCenterTest method setup.

@Before
public void setup() throws Exception {
    int zkServerPort = NetUtils.getAvailablePort();
    zkAddress = "zookeeper://127.0.0.1:" + zkServerPort;
    zkServer = new TestingServer(zkServerPort, true);
    zkClient = CuratorFrameworkFactory.builder().connectString(zkServer.getConnectString()).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
    zkClient.start();
}
Also used : TestingServer(org.apache.curator.test.TestingServer) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Before(org.junit.Before)

Example 45 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project incubator-gobblin by apache.

the class ZookeeperBasedJobLock method initializeCuratorFramework.

private static synchronized void initializeCuratorFramework(Properties properties) {
    if (curatorFrameworkShutdownHook == null) {
        curatorFrameworkShutdownHook = new CuratorFrameworkShutdownHook();
        Runtime.getRuntime().addShutdownHook(curatorFrameworkShutdownHook);
    }
    if (curatorFramework == null) {
        CuratorFramework newCuratorFramework = CuratorFrameworkFactory.builder().connectString(properties.getProperty(CONNECTION_STRING, CONNECTION_STRING_DEFAULT)).connectionTimeoutMs(getMilliseconds(properties, CONNECTION_TIMEOUT_SECONDS, CONNECTION_TIMEOUT_SECONDS_DEFAULT)).sessionTimeoutMs(getMilliseconds(properties, SESSION_TIMEOUT_SECONDS, SESSION_TIMEOUT_SECONDS_DEFAULT)).retryPolicy(new ExponentialBackoffRetry(getMilliseconds(properties, RETRY_BACKOFF_SECONDS, RETRY_BACKOFF_SECONDS_DEFAULT), getInt(properties, MAX_RETRY_COUNT, MAX_RETRY_COUNT_DEFAULT))).build();
        newCuratorFramework.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
                switch(connectionState) {
                    case LOST:
                        log.warn("Lost connection with zookeeper");
                        for (Map.Entry<String, JobLockEventListener> lockEventListener : lockEventListeners.entrySet()) {
                            log.warn("Informing job {} that lock was lost", lockEventListener.getKey());
                            lockEventListener.getValue().onLost();
                        }
                        break;
                    case SUSPENDED:
                        log.warn("Suspended connection with zookeeper");
                        for (Map.Entry<String, JobLockEventListener> lockEventListener : lockEventListeners.entrySet()) {
                            log.warn("Informing job {} that lock was suspended", lockEventListener.getKey());
                            lockEventListener.getValue().onLost();
                        }
                        break;
                    case CONNECTED:
                        log.info("Connected with zookeeper");
                        break;
                    case RECONNECTED:
                        log.warn("Regained connection with zookeeper");
                        break;
                    case READ_ONLY:
                        log.warn("Zookeeper connection went into read-only mode");
                        break;
                }
            }
        });
        newCuratorFramework.start();
        try {
            if (!newCuratorFramework.blockUntilConnected(getInt(properties, CONNECTION_TIMEOUT_SECONDS, CONNECTION_TIMEOUT_SECONDS_DEFAULT), TimeUnit.SECONDS)) {
                throw new RuntimeException("Time out while waiting to connect to zookeeper");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while waiting to connect to zookeeper");
        }
        curatorFramework = newCuratorFramework;
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener)

Aggregations

ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)189 CuratorFramework (org.apache.curator.framework.CuratorFramework)113 RetryPolicy (org.apache.curator.RetryPolicy)46 Before (org.junit.Before)31 TestingCluster (org.apache.curator.test.TestingCluster)28 Test (org.testng.annotations.Test)23 TestingServer (org.apache.curator.test.TestingServer)19 IOException (java.io.IOException)18 Timing (org.apache.curator.test.Timing)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 ArrayList (java.util.ArrayList)14 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)12 ACLProvider (org.apache.curator.framework.api.ACLProvider)12 Test (org.junit.Test)12 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 HashMap (java.util.HashMap)8 KeeperException (org.apache.zookeeper.KeeperException)8