com.twitter.finagle.Address Java Examples

The following examples show how to use com.twitter.finagle.Address. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #2
Source File: TestRoutingService.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private List<Address> getAddresses(boolean weightedAddresses) {
    ArrayList<Address> addresses = new ArrayList<Address>();
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.1", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.2", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.3", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.4", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.5", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.6", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.7", 3181)));

    if (weightedAddresses) {
        ArrayList<Address> wAddresses = new ArrayList<Address>();
        for (Address address: addresses) {
            wAddresses.add(WeightedAddress.apply(address, 1.0));
        }
        return wAddresses;
    } else {
        return addresses;
    }
}
 
Example #3
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #4
Source File: TestRoutingService.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private List<Address> getAddresses(boolean weightedAddresses) {
    ArrayList<Address> addresses = new ArrayList<Address>();
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.1", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.2", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.3", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.4", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.5", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.6", 3181)));
    addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.7", 3181)));

    if (weightedAddresses) {
        ArrayList<Address> wAddresses = new ArrayList<Address>();
        for (Address address: addresses) {
            wAddresses.add(WeightedAddress.apply(address, 1.0));
        }
        return wAddresses;
    } else {
        return addresses;
    }
}
 
Example #5
Source File: TestConsistentHashRoutingService.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testBlackoutHost() throws Exception {
    TestName name = new TestName();
    RoutingService routingService = ConsistentHashRoutingService.newBuilder()
            .serverSet(new NameServerSet(name))
            .resolveFromName(true)
            .numReplicas(997)
            .blackoutSeconds(2)
            .build();

    InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", 3181);
    Address address = Addresses.newInetAddress(inetAddress);
    List<Address> addresses = new ArrayList<Address>(1);
    addresses.add(address);
    name.changeAddrs(addresses);

    routingService.startService();

    RoutingService.RoutingContext routingContext =
            RoutingService.RoutingContext.of(new DefaultRegionResolver());

    String streamName = "test-blackout-host";
    assertEquals(inetAddress, routingService.getHost(streamName, routingContext));
    routingService.removeHost(inetAddress, new ChannelWriteException(new IOException("test exception")));
    try {
        routingService.getHost(streamName, routingContext);
        fail("Should fail to get host since no brokers are available");
    } catch (NoBrokersAvailableException nbae) {
        // expected
    }

    TimeUnit.SECONDS.sleep(3);
    assertEquals(inetAddress, routingService.getHost(streamName, routingContext));

    routingService.stopService();
}
 
Example #6
Source File: TestConsistentHashRoutingService.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testBlackoutHost() throws Exception {
    TestName name = new TestName();
    RoutingService routingService = ConsistentHashRoutingService.newBuilder()
            .serverSet(new NameServerSet(name))
            .resolveFromName(true)
            .numReplicas(997)
            .blackoutSeconds(2)
            .build();

    InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", 3181);
    Address address = Addresses.newInetAddress(inetAddress);
    List<Address> addresses = new ArrayList<Address>(1);
    addresses.add(address);
    name.changeAddrs(addresses);

    routingService.startService();

    RoutingService.RoutingContext routingContext =
            RoutingService.RoutingContext.of(new DefaultRegionResolver());

    String streamName = "test-blackout-host";
    assertEquals(inetAddress, routingService.getHost(streamName, routingContext));
    routingService.removeHost(inetAddress, new ChannelWriteException(new IOException("test exception")));
    try {
        routingService.getHost(streamName, routingContext);
        fail("Should fail to get host since no brokers are available");
    } catch (NoBrokersAvailableException nbae) {
        // expected
    }

    TimeUnit.SECONDS.sleep(3);
    assertEquals(inetAddress, routingService.getHost(streamName, routingContext));

    routingService.stopService();
}
 
Example #7
Source File: ClientDestTracingFilterInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private String getRemote(Object[] objects) {
    if (objects == null || objects.length == 0) {
        return "";
    }
    if (objects[0] instanceof InetSocketAddress) {
        /*
         * Compatible with versions below 6.33.0
         * 6.33.0 and below use {@link java.net.SocketAddress} as parameter
         */
        return formatPeer((InetSocketAddress) objects[0]);
    } else if (objects[0] instanceof Address.Inet) {
        return formatPeer(((Address.Inet) objects[0]).addr());
    }
    return "";
}
 
Example #8
Source File: TestName.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
public void changeAddrs(List<Address> addresses) {
    if (null != callback) {
        LOG.info("Sending a callback {}", addresses);
        callback.apply(Addrs.newBoundAddr(addresses));
    }
}
 
Example #9
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private BoxedUnit respondToChanges(Addr addr) {
    ImmutableSet<ServiceInstance> oldHostSet = ImmutableSet.copyOf(hostSet);

    ImmutableSet<ServiceInstance> newHostSet = oldHostSet;

    if (addr instanceof Addr.Bound) {
        scala.collection.immutable.Set<Address> endpointAddresses = ((Addr.Bound) addr).addrs();
        scala.collection.Iterator<Address> endpointAddressesIterator = endpointAddresses.toIterator();
        HashSet<ServiceInstance> serviceInstances = new HashSet<ServiceInstance>();
        while (endpointAddressesIterator.hasNext()) {
            serviceInstances.add(endpointAddressToServiceInstance(endpointAddressesIterator.next()));
        }
        newHostSet = ImmutableSet.copyOf(serviceInstances);

    } else if (addr instanceof Addr.Failed) {
        logger.error("Name resolution failed", ((Addr.Failed) addr).cause());
        newHostSet = ImmutableSet.of();
    } else if (addr.toString().equals("Pending")) {
        logger.info("Name resolution pending");
        newHostSet = oldHostSet;
    } else if (addr.toString().equals("Neg")) {
        newHostSet = ImmutableSet.of();
    } else {
        logger.error("Invalid Addr type: {}", addr.getClass().getName());
        throw new UnsupportedOperationException("Invalid Addr type:" + addr.getClass().getName());
    }

    // Reference comparison is valid as the sets are immutable
    if (oldHostSet != newHostSet) {
        logger.info("NameServerSet updated: {} -> {}", hostSetToString(oldHostSet), hostSetToString(newHostSet));
        resolutionPending.set(false);
        hostSet = newHostSet;
        synchronized (watchers) {
            for (HostChangeMonitor<ServiceInstance> watcher: watchers) {
                watcher.onChange(newHostSet);
            }
        }

    }

    return BoxedUnit.UNIT;
}
 
Example #10
Source File: TestRoutingService.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private void testRoutingServiceHelper(boolean consistentHash,
                                      boolean weightedAddresses,
                                      boolean asyncResolution)
    throws Exception {
    ExecutorService executorService = null;
    final List<Address> addresses = getAddresses(weightedAddresses);
    final TestName name = new TestName();
    RoutingService routingService;
    if (consistentHash) {
        routingService = ConsistentHashRoutingService.newBuilder()
                .serverSet(new NameServerSet(name))
                .resolveFromName(true)
                .numReplicas(997)
                .build();
    } else {
        routingService = ServerSetRoutingService.newServerSetRoutingServiceBuilder()
                .serverSetWatcher(new TwitterServerSetWatcher(new NameServerSet(name), true)).build();
    }

    if (asyncResolution) {
        executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                name.changeAddrs(addresses);
            }
        });
    } else {
        name.changeAddrs(addresses);
    }
    routingService.startService();

    HashSet<SocketAddress> mapping = new HashSet<SocketAddress>();

    for (int i = 0; i < 1000; i++) {
        for (int j = 0; j < 5; j++) {
            String stream = "TestStream-" + i + "-" + j;
            mapping.add(routingService.getHost(stream,
                    RoutingService.RoutingContext.of(new DefaultRegionResolver())));
        }
    }

    assertEquals(mapping.size(), addresses.size());

    if (null != executorService) {
        executorService.shutdown();
    }

}
 
Example #11
Source File: TestName.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
public void changeAddrs(List<Address> addresses) {
    if (null != callback) {
        LOG.info("Sending a callback {}", addresses);
        callback.apply(Addrs.newBoundAddr(addresses));
    }
}
 
Example #12
Source File: NameServerSet.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private BoxedUnit respondToChanges(Addr addr) {
    ImmutableSet<ServiceInstance> oldHostSet = ImmutableSet.copyOf(hostSet);

    ImmutableSet<ServiceInstance> newHostSet = oldHostSet;

    if (addr instanceof Addr.Bound) {
        scala.collection.immutable.Set<Address> endpointAddresses = ((Addr.Bound)addr).addrs();
        scala.collection.Iterator<Address> endpointAddressesIterator = endpointAddresses.toIterator();
        HashSet<ServiceInstance> serviceInstances = new HashSet<ServiceInstance>();
        while (endpointAddressesIterator.hasNext()) {
            serviceInstances.add(endpointAddressToServiceInstance(endpointAddressesIterator.next()));
        }
        newHostSet = ImmutableSet.copyOf(serviceInstances);

    } else if (addr instanceof Addr.Failed) {
        logger.error("Name resolution failed", ((Addr.Failed)addr).cause());
        newHostSet = ImmutableSet.of();
    } else if (addr.toString().equals("Pending")) {
        logger.info("Name resolution pending");
        newHostSet = oldHostSet;
    } else if (addr.toString().equals("Neg")) {
        newHostSet = ImmutableSet.of();
    } else {
        logger.error("Invalid Addr type: {}", addr.getClass().getName());
        throw new UnsupportedOperationException("Invalid Addr type:" + addr.getClass().getName());
    }

    // Reference comparison is valid as the sets are immutable
    if (oldHostSet != newHostSet) {
        logger.info("NameServerSet updated: {} -> {}", hostSetToString(oldHostSet), hostSetToString(newHostSet));
        resolutionPending.set(false);
        hostSet = newHostSet;
        synchronized (watchers) {
            for (HostChangeMonitor<ServiceInstance> watcher: watchers) {
                watcher.onChange(newHostSet);
            }
        }

    }

    return BoxedUnit.UNIT;
}
 
Example #13
Source File: TestRoutingService.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private void testRoutingServiceHelper(boolean consistentHash, boolean weightedAddresses, boolean asyncResolution) throws Exception {
    ExecutorService executorService = null;
    final List<Address> addresses = getAddresses(weightedAddresses);
    final TestName name = new TestName();
    RoutingService routingService;
    if (consistentHash) {
        routingService = ConsistentHashRoutingService.newBuilder()
                .serverSet(new NameServerSet(name))
                .resolveFromName(true)
                .numReplicas(997)
                .build();
    } else {
        routingService = ServerSetRoutingService.newServerSetRoutingServiceBuilder()
                .serverSetWatcher(new TwitterServerSetWatcher(new NameServerSet(name), true)).build();
    }

    if (asyncResolution) {
        executorService = Executors.newSingleThreadExecutor();
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                name.changeAddrs(addresses);
            }
        });
    } else {
        name.changeAddrs(addresses);
    }
    routingService.startService();

    HashSet<SocketAddress> mapping = new HashSet<SocketAddress>();

    for (int i = 0; i < 1000; i++) {
        for (int j = 0; j < 5; j++) {
            String stream = "TestStream-" + i + "-" + j;
            mapping.add(routingService.getHost(stream,
                    RoutingService.RoutingContext.of(new DefaultRegionResolver())));
        }
    }

    assert(mapping.size() == addresses.size());

    if (null != executorService) {
        executorService.shutdown();
    }

}