Search in sources :

Example 1 with DefaultRegionResolver

use of com.twitter.distributedlog.client.resolver.DefaultRegionResolver in project distributedlog by twitter.

the class TestProxyClientManager method createProxyClientManager.

private static ProxyClientManager createProxyClientManager(ProxyClient.Builder builder, HostProvider hostProvider, long periodicHandshakeIntervalMs) {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.setPeriodicHandshakeIntervalMs(periodicHandshakeIntervalMs);
    clientConfig.setPeriodicOwnershipSyncIntervalMs(-1);
    HashedWheelTimer dlTimer = new HashedWheelTimer(new ThreadFactoryBuilder().setNameFormat("TestProxyClientManager-timer-%d").build(), clientConfig.getRedirectBackoffStartMs(), TimeUnit.MILLISECONDS);
    return new ProxyClientManager(clientConfig, builder, dlTimer, hostProvider, new ClientStats(NullStatsReceiver.get(), false, new DefaultRegionResolver()));
}
Also used : ClientStats(com.twitter.distributedlog.client.stats.ClientStats) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HashedWheelTimer(org.jboss.netty.util.HashedWheelTimer) DefaultRegionResolver(com.twitter.distributedlog.client.resolver.DefaultRegionResolver) ClientConfig(com.twitter.distributedlog.client.ClientConfig)

Example 2 with DefaultRegionResolver

use of com.twitter.distributedlog.client.resolver.DefaultRegionResolver 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 3 with DefaultRegionResolver

use of com.twitter.distributedlog.client.resolver.DefaultRegionResolver in project distributedlog by twitter.

the class TestRegionsRoutingService method testRoutingListener.

@Test(timeout = 60000)
public void testRoutingListener() throws Exception {
    int numRoutingServices = 5;
    RoutingService.Builder[] routingServiceBuilders = new RoutingService.Builder[numRoutingServices];
    Set<SocketAddress> hosts = new HashSet<SocketAddress>();
    Map<SocketAddress, String> regionMap = new HashMap<SocketAddress, String>();
    for (int i = 0; i < numRoutingServices; i++) {
        String finagleNameStr = "inet!127.0.0.1:" + (3181 + i);
        routingServiceBuilders[i] = RoutingUtils.buildRoutingService(finagleNameStr);
        SocketAddress address = new InetSocketAddress("127.0.0.1", 3181 + i);
        hosts.add(address);
        regionMap.put(address, "region-" + i);
    }
    final CountDownLatch doneLatch = new CountDownLatch(numRoutingServices);
    final AtomicInteger numHostsLeft = new AtomicInteger(0);
    final Set<SocketAddress> jointHosts = new HashSet<SocketAddress>();
    RegionsRoutingService regionsRoutingService = RegionsRoutingService.newBuilder().routingServiceBuilders(routingServiceBuilders).resolver(new DefaultRegionResolver(regionMap)).build();
    regionsRoutingService.registerListener(new RoutingService.RoutingListener() {

        @Override
        public void onServerLeft(SocketAddress address) {
            numHostsLeft.incrementAndGet();
        }

        @Override
        public void onServerJoin(SocketAddress address) {
            jointHosts.add(address);
            doneLatch.countDown();
        }
    });
    regionsRoutingService.startService();
    doneLatch.await();
    assertEquals(numRoutingServices, jointHosts.size());
    assertEquals(0, numHostsLeft.get());
    assertTrue(Sets.difference(hosts, jointHosts).immutableCopy().isEmpty());
}
Also used : HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultRegionResolver(com.twitter.distributedlog.client.resolver.DefaultRegionResolver) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with DefaultRegionResolver

use of com.twitter.distributedlog.client.resolver.DefaultRegionResolver 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 5 with DefaultRegionResolver

use of com.twitter.distributedlog.client.resolver.DefaultRegionResolver in project distributedlog by twitter.

the class TestRegionsRoutingService method testGetHost.

@Test(timeout = 60000)
public void testGetHost() throws Exception {
    int numRoutingServices = 3;
    RoutingService.Builder[] routingServiceBuilders = new RoutingService.Builder[numRoutingServices];
    Map<SocketAddress, String> regionMap = new HashMap<SocketAddress, String>();
    for (int i = 0; i < numRoutingServices; i++) {
        String finagleNameStr = "inet!127.0.0.1:" + (3181 + i);
        routingServiceBuilders[i] = RoutingUtils.buildRoutingService(finagleNameStr);
        SocketAddress address = new InetSocketAddress("127.0.0.1", 3181 + i);
        regionMap.put(address, "region-" + i);
    }
    RegionsRoutingService regionsRoutingService = RegionsRoutingService.newBuilder().resolver(new DefaultRegionResolver(regionMap)).routingServiceBuilders(routingServiceBuilders).build();
    regionsRoutingService.startService();
    RoutingService.RoutingContext routingContext = RoutingService.RoutingContext.of(new DefaultRegionResolver()).addTriedHost(new InetSocketAddress("127.0.0.1", 3183), StatusCode.WRITE_EXCEPTION);
    assertEquals(new InetSocketAddress("127.0.0.1", 3181), regionsRoutingService.getHost("any", routingContext));
    routingContext = RoutingService.RoutingContext.of(new DefaultRegionResolver()).addTriedHost(new InetSocketAddress("127.0.0.1", 3181), StatusCode.WRITE_EXCEPTION);
    assertEquals(new InetSocketAddress("127.0.0.1", 3182), regionsRoutingService.getHost("any", routingContext));
    // add 3182 to routing context as tried host
    routingContext.addTriedHost(new InetSocketAddress("127.0.0.1", 3182), StatusCode.WRITE_EXCEPTION);
    assertEquals(new InetSocketAddress("127.0.0.1", 3183), regionsRoutingService.getHost("any", routingContext));
    // add 3183 to routing context as tried host
    routingContext.addTriedHost(new InetSocketAddress("127.0.0.1", 3183), StatusCode.WRITE_EXCEPTION);
    try {
        regionsRoutingService.getHost("any", routingContext);
        fail("Should fail to get host since all regions are tried.");
    } catch (NoBrokersAvailableException nbae) {
    // expected
    }
}
Also used : HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) NoBrokersAvailableException(com.twitter.finagle.NoBrokersAvailableException) DefaultRegionResolver(com.twitter.distributedlog.client.resolver.DefaultRegionResolver) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Aggregations

DefaultRegionResolver (com.twitter.distributedlog.client.resolver.DefaultRegionResolver)5 InetSocketAddress (java.net.InetSocketAddress)4 SocketAddress (java.net.SocketAddress)4 Test (org.junit.Test)3 Address (com.twitter.finagle.Address)2 NoBrokersAvailableException (com.twitter.finagle.NoBrokersAvailableException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ClientConfig (com.twitter.distributedlog.client.ClientConfig)1 ClientStats (com.twitter.distributedlog.client.stats.ClientStats)1 DLSocketAddress (com.twitter.distributedlog.service.DLSocketAddress)1 ChannelWriteException (com.twitter.finagle.ChannelWriteException)1 WeightedAddress (com.twitter.finagle.addr.WeightedAddress)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1