Search in sources :

Example 6 with Endpoint

use of com.twitter.thrift.Endpoint in project distributedlog by twitter.

the class TestInetNameResolution method testInetNameResolution.

@Test(timeout = 10000)
public void testInetNameResolution() throws Exception {
    String nameStr = "inet!127.0.0.1:3181";
    final CountDownLatch resolved = new CountDownLatch(1);
    final AtomicBoolean validationFailed = new AtomicBoolean(false);
    NameServerSet serverSet = new NameServerSet(nameStr);
    serverSet.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {

        @Override
        public void onChange(ImmutableSet<ServiceInstance> hostSet) {
            if (hostSet.size() > 1) {
                logger.error("HostSet has more elements than expected {}", hostSet);
                validationFailed.set(true);
                resolved.countDown();
            } else if (hostSet.size() == 1) {
                ServiceInstance serviceInstance = hostSet.iterator().next();
                Endpoint endpoint = serviceInstance.getAdditionalEndpoints().get("thrift");
                InetSocketAddress address = new InetSocketAddress(endpoint.getHost(), endpoint.getPort());
                if (endpoint.getPort() != 3181) {
                    logger.error("Port does not match the expected port {}", endpoint.getPort());
                    validationFailed.set(true);
                } else if (!address.getAddress().getHostAddress().equals("127.0.0.1")) {
                    logger.error("Host address does not match the expected address {}", address.getAddress().getHostAddress());
                    validationFailed.set(true);
                }
                resolved.countDown();
            }
        }
    });
    resolved.await();
    Assert.assertEquals(false, validationFailed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Endpoint(com.twitter.thrift.Endpoint) InetSocketAddress(java.net.InetSocketAddress) ServiceInstance(com.twitter.thrift.ServiceInstance) CountDownLatch(java.util.concurrent.CountDownLatch) DynamicHostSet(com.twitter.common.net.pool.DynamicHostSet) Test(org.junit.Test)

Example 7 with Endpoint

use of com.twitter.thrift.Endpoint in project distributedlog by twitter.

the class NameServerSet method endpointAddressToServiceInstance.

private ServiceInstance endpointAddressToServiceInstance(Address endpointAddress) {
    if (endpointAddress instanceof Address.Inet) {
        InetSocketAddress inetSocketAddress = ((Address.Inet) endpointAddress).addr();
        Endpoint endpoint = new Endpoint(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
        HashMap<String, Endpoint> map = new HashMap<String, Endpoint>();
        map.put("thrift", endpoint);
        return new ServiceInstance(endpoint, map, Status.ALIVE);
    } else {
        logger.error("We expect InetSocketAddress while the resolved address {} was {}", endpointAddress, endpointAddress.getClass());
        throw new UnsupportedOperationException("invalid endpoint address: " + endpointAddress);
    }
}
Also used : Endpoint(com.twitter.thrift.Endpoint) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) ServiceInstance(com.twitter.thrift.ServiceInstance)

Example 8 with Endpoint

use of com.twitter.thrift.Endpoint in project distributedlog by twitter.

the class NameServerSet method hostSetToString.

private String hostSetToString(ImmutableSet<ServiceInstance> hostSet) {
    StringBuilder result = new StringBuilder();
    result.append("(");
    for (ServiceInstance serviceInstance : hostSet) {
        Endpoint endpoint = serviceInstance.getServiceEndpoint();
        result.append(String.format(" %s:%d", endpoint.getHost(), endpoint.getPort()));
    }
    result.append(" )");
    return result.toString();
}
Also used : Endpoint(com.twitter.thrift.Endpoint) ServiceInstance(com.twitter.thrift.ServiceInstance)

Aggregations

Endpoint (com.twitter.thrift.Endpoint)8 ServiceInstance (com.twitter.thrift.ServiceInstance)8 Test (org.junit.Test)6 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)4 InetSocketAddress (java.net.InetSocketAddress)4 EndpointStatus (com.twitter.common.zookeeper.ServerSet.EndpointStatus)2 GsonBuilder (com.google.gson.GsonBuilder)1 DynamicHostSet (com.twitter.common.net.pool.DynamicHostSet)1 Status (com.twitter.thrift.Status)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1