Search in sources :

Example 1 with Address

use of com.twitter.finagle.Address in project distributedlog by twitter.

the class TestConsistentHashRoutingService method testBlackoutHost.

@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();
}
Also used : ChannelWriteException(com.twitter.finagle.ChannelWriteException) DLSocketAddress(com.twitter.distributedlog.service.DLSocketAddress) SocketAddress(java.net.SocketAddress) Address(com.twitter.finagle.Address) InetSocketAddress(java.net.InetSocketAddress) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) NoBrokersAvailableException(com.twitter.finagle.NoBrokersAvailableException) IOException(java.io.IOException) DefaultRegionResolver(com.twitter.distributedlog.client.resolver.DefaultRegionResolver) Test(org.junit.Test)

Example 2 with Address

use of com.twitter.finagle.Address in project distributedlog by twitter.

the class TestRoutingService method testRoutingServiceHelper.

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();
    }
}
Also used : SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Address(com.twitter.finagle.Address) WeightedAddress(com.twitter.finagle.addr.WeightedAddress) ExecutorService(java.util.concurrent.ExecutorService) DefaultRegionResolver(com.twitter.distributedlog.client.resolver.DefaultRegionResolver) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) HashSet(java.util.HashSet)

Example 3 with Address

use of com.twitter.finagle.Address in project distributedlog by twitter.

the class TestConsistentHashRoutingService method testPerformServerSetChangeOnName.

@Test(timeout = 60000)
public void testPerformServerSetChangeOnName() throws Exception {
    TestName name = new TestName();
    ConsistentHashRoutingService routingService = (ConsistentHashRoutingService) ConsistentHashRoutingService.newBuilder().serverSet(new NameServerSet(name)).resolveFromName(true).numReplicas(997).build();
    int basePort = 3180;
    int numHosts = 4;
    List<Address> addresses1 = Lists.newArrayListWithExpectedSize(4);
    List<Address> addresses2 = Lists.newArrayListWithExpectedSize(4);
    List<Address> addresses3 = Lists.newArrayListWithExpectedSize(4);
    // fill up the addresses1
    for (int i = 0; i < numHosts; i++) {
        InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", basePort + i);
        Address address = Addresses.newInetAddress(inetAddress);
        addresses1.add(address);
    }
    // fill up the addresses2 - overlap with addresses1
    for (int i = 0; i < numHosts; i++) {
        InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", basePort + 2 + i);
        Address address = Addresses.newInetAddress(inetAddress);
        addresses2.add(address);
    }
    // fill up the addresses3 - not overlap with addresses2
    for (int i = 0; i < numHosts; i++) {
        InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", basePort + 10 + i);
        Address address = Addresses.newInetAddress(inetAddress);
        addresses3.add(address);
    }
    final List<SocketAddress> leftAddresses = Lists.newArrayList();
    final List<SocketAddress> joinAddresses = Lists.newArrayList();
    RoutingService.RoutingListener routingListener = new RoutingService.RoutingListener() {

        @Override
        public void onServerLeft(SocketAddress address) {
            synchronized (leftAddresses) {
                leftAddresses.add(address);
                leftAddresses.notifyAll();
            }
        }

        @Override
        public void onServerJoin(SocketAddress address) {
            synchronized (joinAddresses) {
                joinAddresses.add(address);
                joinAddresses.notifyAll();
            }
        }
    };
    routingService.registerListener(routingListener);
    name.changeAddrs(addresses1);
    routingService.startService();
    synchronized (joinAddresses) {
        while (joinAddresses.size() < numHosts) {
            joinAddresses.wait();
        }
    }
    // validate 4 nodes joined
    synchronized (joinAddresses) {
        assertEquals(numHosts, joinAddresses.size());
    }
    synchronized (leftAddresses) {
        assertEquals(0, leftAddresses.size());
    }
    assertEquals(numHosts, routingService.shardId2Address.size());
    assertEquals(numHosts, routingService.address2ShardId.size());
    for (int i = 0; i < numHosts; i++) {
        InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", basePort + i);
        assertTrue(routingService.address2ShardId.containsKey(inetAddress));
        int shardId = routingService.address2ShardId.get(inetAddress);
        SocketAddress sa = routingService.shardId2Address.get(shardId);
        assertNotNull(sa);
        assertEquals(inetAddress, sa);
    }
    // update addresses2 - 2 new hosts joined, 2 old hosts left
    name.changeAddrs(addresses2);
    synchronized (joinAddresses) {
        while (joinAddresses.size() < numHosts + 2) {
            joinAddresses.wait();
        }
    }
    synchronized (leftAddresses) {
        while (leftAddresses.size() < numHosts - 2) {
            leftAddresses.wait();
        }
    }
    assertEquals(numHosts, routingService.shardId2Address.size());
    assertEquals(numHosts, routingService.address2ShardId.size());
    // first 2 shards should leave
    for (int i = 0; i < 2; i++) {
        InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", basePort + i);
        assertFalse(routingService.address2ShardId.containsKey(inetAddress));
    }
    for (int i = 0; i < numHosts; i++) {
        InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", basePort + 2 + i);
        assertTrue(routingService.address2ShardId.containsKey(inetAddress));
        int shardId = routingService.address2ShardId.get(inetAddress);
        SocketAddress sa = routingService.shardId2Address.get(shardId);
        assertNotNull(sa);
        assertEquals(inetAddress, sa);
    }
    // update addresses3 - 2 new hosts joined, 2 old hosts left
    name.changeAddrs(addresses3);
    synchronized (joinAddresses) {
        while (joinAddresses.size() < numHosts + 2 + numHosts) {
            joinAddresses.wait();
        }
    }
    synchronized (leftAddresses) {
        while (leftAddresses.size() < numHosts - 2 + numHosts) {
            leftAddresses.wait();
        }
    }
    assertEquals(numHosts, routingService.shardId2Address.size());
    assertEquals(numHosts, routingService.address2ShardId.size());
    // first 6 shards should leave
    for (int i = 0; i < 2 + numHosts; i++) {
        InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", basePort + i);
        assertFalse(routingService.address2ShardId.containsKey(inetAddress));
    }
    // new 4 shards should exist
    for (int i = 0; i < numHosts; i++) {
        InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", basePort + 10 + i);
        assertTrue(routingService.address2ShardId.containsKey(inetAddress));
        int shardId = routingService.address2ShardId.get(inetAddress);
        SocketAddress sa = routingService.shardId2Address.get(shardId);
        assertNotNull(sa);
        assertEquals(inetAddress, sa);
    }
}
Also used : DLSocketAddress(com.twitter.distributedlog.service.DLSocketAddress) SocketAddress(java.net.SocketAddress) Address(com.twitter.finagle.Address) InetSocketAddress(java.net.InetSocketAddress) InetSocketAddress(java.net.InetSocketAddress) DLSocketAddress(com.twitter.distributedlog.service.DLSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 4 with Address

use of com.twitter.finagle.Address in project distributedlog by twitter.

the class TestRoutingService method getAddresses.

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;
    }
}
Also used : SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Address(com.twitter.finagle.Address) WeightedAddress(com.twitter.finagle.addr.WeightedAddress) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList)

Aggregations

Address (com.twitter.finagle.Address)4 InetSocketAddress (java.net.InetSocketAddress)4 SocketAddress (java.net.SocketAddress)4 DefaultRegionResolver (com.twitter.distributedlog.client.resolver.DefaultRegionResolver)2 DLSocketAddress (com.twitter.distributedlog.service.DLSocketAddress)2 WeightedAddress (com.twitter.finagle.addr.WeightedAddress)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ChannelWriteException (com.twitter.finagle.ChannelWriteException)1 NoBrokersAvailableException (com.twitter.finagle.NoBrokersAvailableException)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1