Search in sources :

Example 11 with ServiceInstance

use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.

the class TestServiceDiscovery method testUnregisterService.

// CURATOR-164
@Test
public void testUnregisterService() throws Exception {
    final String name = "name";
    final CountDownLatch restartLatch = new CountDownLatch(1);
    List<Closeable> closeables = Lists.newArrayList();
    InstanceSerializer<String> slowSerializer = new JsonInstanceSerializer<String>(String.class) {

        private boolean first = true;

        @Override
        public byte[] serialize(ServiceInstance<String> instance) throws Exception {
            if (first) {
                System.out.println("Serializer first registration.");
                first = false;
            } else {
                System.out.println("Waiting for reconnect to finish.");
                // Simulate the serialize method being slow.
                // This could just be a timed wait, but that's kind of non-deterministic.
                restartLatch.await();
            }
            return super.serialize(instance);
        }
    };
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name(name).port(10064).build();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).serializer(slowSerializer).watchInstances(true).build();
        closeables.add(discovery);
        discovery.start();
        Assert.assertFalse(discovery.queryForInstances(name).isEmpty(), "Service should start registered.");
        server.stop();
        server.restart();
        discovery.unregisterService(instance);
        restartLatch.countDown();
        // Wait for the rest of registration to finish.
        new Timing().sleepABit();
        Assert.assertTrue(discovery.queryForInstances(name).isEmpty(), "Service should have unregistered.");
    } finally {
        Collections.reverse(closeables);
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) ServiceInstance(org.apache.curator.x.discovery.ServiceInstance) Timing(org.apache.curator.test.Timing) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 12 with ServiceInstance

use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.

the class TestServiceProvider method testBasic.

@Test
public void testBasic() throws Exception {
    List<Closeable> closeables = Lists.newArrayList();
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).build();
        closeables.add(discovery);
        discovery.start();
        ServiceProvider<String> provider = discovery.serviceProviderBuilder().serviceName("test").build();
        closeables.add(provider);
        provider.start();
        Assert.assertEquals(provider.getInstance(), instance);
        List<ServiceInstance<String>> list = Lists.newArrayList();
        list.add(instance);
        Assert.assertEquals(provider.getAllInstances(), list);
    } finally {
        Collections.reverse(closeables);
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) ServiceInstance(org.apache.curator.x.discovery.ServiceInstance) Test(org.testng.annotations.Test)

Example 13 with ServiceInstance

use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.

the class TestWatchedInstances method testWatchedInstances.

@Test
public void testWatchedInstances() throws Exception {
    Timing timing = new Timing();
    List<Closeable> closeables = Lists.newArrayList();
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
        closeables.add(client);
        client.start();
        ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
        ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).watchInstances(true).build();
        closeables.add(discovery);
        discovery.start();
        Assert.assertEquals(discovery.queryForNames(), Arrays.asList("test"));
        List<ServiceInstance<String>> list = Lists.newArrayList();
        list.add(instance);
        Assert.assertEquals(discovery.queryForInstances("test"), list);
        ServiceDiscoveryImpl<String> discoveryImpl = (ServiceDiscoveryImpl<String>) discovery;
        ServiceInstance<String> changedInstance = ServiceInstance.<String>builder().id(instance.getId()).address(instance.getAddress()).payload("different").name(instance.getName()).port(instance.getPort()).build();
        String path = discoveryImpl.pathForInstance("test", instance.getId());
        byte[] bytes = discoveryImpl.getSerializer().serialize(changedInstance);
        client.setData().forPath(path, bytes);
        timing.sleepABit();
        ServiceInstance<String> registeredService = discoveryImpl.getRegisteredService(instance.getId());
        Assert.assertNotNull(registeredService);
        Assert.assertEquals(registeredService.getPayload(), "different");
    } finally {
        Collections.reverse(closeables);
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) Closeable(java.io.Closeable) ServiceInstance(org.apache.curator.x.discovery.ServiceInstance) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 14 with ServiceInstance

use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.

the class TestFastjsonTypeReference method test.

@Test
public void test() {
    final NodeStatus status = new NodeStatus();
    status.setActiveCount(1);
    ServiceInstance<NodeStatus> instance = JSON.parseObject(new JSONObject() {

        {
            put("payload", status);
        }
    }.toJSONString(), new TypeReference<ServiceInstance<NodeStatus>>() {
    });
    Assert.assertEquals(instance.getPayload().getActiveCount(), 1);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) ServiceInstance(org.apache.curator.x.discovery.ServiceInstance) NodeStatus(info.xiancloud.core.distribution.NodeStatus) Test(org.junit.Test)

Example 15 with ServiceInstance

use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.

the class JsonInstanceSerializer method deserialize.

@SuppressWarnings({ "unchecked" })
@Override
public ServiceInstance<T> deserialize(byte[] bytes) throws Exception {
    ServiceInstance rawServiceInstance = mapper.readValue(bytes, type);
    // just to verify that it's the correct type
    payloadClass.cast(rawServiceInstance.getPayload());
    return (ServiceInstance<T>) rawServiceInstance;
}
Also used : ServiceInstance(org.apache.curator.x.discovery.ServiceInstance)

Aggregations

ServiceInstance (org.apache.curator.x.discovery.ServiceInstance)19 CuratorFramework (org.apache.curator.framework.CuratorFramework)9 RetryOneTime (org.apache.curator.retry.RetryOneTime)9 Test (org.testng.annotations.Test)9 Closeable (java.io.Closeable)8 IOException (java.io.IOException)6 HashSet (java.util.HashSet)4 Timing (org.apache.curator.test.Timing)4 DrillbitEndpoint (org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint)4 ArrayList (java.util.ArrayList)2 Semaphore (java.util.concurrent.Semaphore)2 MultiKey (org.apache.commons.collections.keyvalue.MultiKey)2 JSONObject (com.alibaba.fastjson.JSONObject)1 NodeStatus (info.xiancloud.core.distribution.NodeStatus)1 BufferedReader (java.io.BufferedReader)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 EnsurePath (org.apache.curator.utils.EnsurePath)1 SimpleTimestampedMessageParser (org.apache.oozie.util.SimpleTimestampedMessageParser)1