Search in sources :

Example 6 with ServiceUnavailableException

use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.

the class TestRouteLookupClient method testRouteLookupClientCallback.

@Test
public void testRouteLookupClientCallback() throws InterruptedException, ExecutionException, TimeoutException {
    RouteLookup routeLookup = new SimpleTestRouteLookup();
    final D2Client d2Client = new D2ClientBuilder().setZkHosts("localhost:2121").build();
    d2Client.start(new FutureCallback<None>());
    RouteLookupClient routeLookupClient = new RouteLookupClient(d2Client, routeLookup, "WestCoast");
    RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("d2://simple_uri")).build();
    FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
    routeLookupClient.restRequest(dummyRestRequest, futureCallback, "5555");
    try {
        RestResponse response = futureCallback.get(10, TimeUnit.SECONDS);
        Assert.fail("Unexpected success, request should have thrown a ServiceUnavailableException");
    } catch (Exception e) {
        String message = e.getMessage();
        if (!message.contains("_serviceName=simple_uriWestCoast5555Foo")) {
            Assert.fail("request was not rewritten to point at the d2 service simple_uriWestCoast5555Foo");
        }
    }
}
Also used : D2Client(com.linkedin.d2.balancer.D2Client) RestResponse(com.linkedin.r2.message.rest.RestResponse) D2ClientBuilder(com.linkedin.d2.balancer.D2ClientBuilder) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 7 with ServiceUnavailableException

use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.

the class ConsistentHashKeyMapper method doMapKeys.

private <K> MapKeyResult<URI, K> doMapKeys(Ring<URI> ring, Iterable<K> keys) throws ServiceUnavailableException {
    String[] keyTokens = new String[1];
    List<MapKeyResult.UnmappedKey<K>> unmappedKeys = new ArrayList<MapKeyResult.UnmappedKey<K>>();
    Map<URI, Collection<K>> result = new HashMap<URI, Collection<K>>();
    for (K key : keys) {
        keyTokens[0] = key.toString();
        int hashCode = _hashFunction.hash(keyTokens);
        URI uri = ring.get(hashCode);
        if (uri == null) {
            unmappedKeys.add(new MapKeyResult.UnmappedKey<K>(key, MapKeyResult.ErrorType.NO_HOST_AVAILABLE_IN_PARTITION));
            continue;
        }
        Collection<K> collection = result.get(uri);
        if (collection == null) {
            collection = new ArrayList<K>();
            result.put(uri, collection);
        }
        collection.add(key);
    }
    return new MapKeyResult<URI, K>(result, unmappedKeys);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) Collection(java.util.Collection) MapKeyResult(com.linkedin.d2.balancer.util.MapKeyResult)

Example 8 with ServiceUnavailableException

use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.

the class SimpleLoadBalancerTest method testLoadBalancerSimulationRandomLarge.

@Test(enabled = false, groups = { "large", "back-end" })
public void testLoadBalancerSimulationRandomLarge() throws URISyntaxException, IOException, ServiceUnavailableException, InterruptedException {
    SimpleLoadBalancerSimulation simulator = new SimpleLoadBalancerSimulation(new RandomLoadBalancerStrategyFactory());
    simulator.simulateMultithreaded(1, 1000, 20);
    simulator.reset();
    simulator.simulateMultithreaded(1, 10000, 20);
    simulator.reset();
    simulator.simulateMultithreaded(8, 10000, 750);
    simulator.reset();
    simulator.simulateMultithreaded(50, 10000, 100);
    simulator.reset();
    simulator.simulateMultithreaded(50, 10000, 100);
    simulator.reset();
}
Also used : RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) SimpleLoadBalancerSimulation(com.linkedin.d2.balancer.simulator.SimpleLoadBalancerSimulation) Test(org.testng.annotations.Test)

Example 9 with ServiceUnavailableException

use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.

the class SimpleLoadBalancerTest method testLoadBalancerSmoke.

@Test(groups = { "small", "back-end" })
public void testLoadBalancerSmoke() throws URISyntaxException, ServiceUnavailableException, InterruptedException, ExecutionException {
    for (int tryAgain = 0; tryAgain < 1000; ++tryAgain) {
        Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
        Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
        List<String> prioritizedSchemes = new ArrayList<String>();
        MockStore<ServiceProperties> serviceRegistry = new MockStore<ServiceProperties>();
        MockStore<ClusterProperties> clusterRegistry = new MockStore<ClusterProperties>();
        MockStore<UriProperties> uriRegistry = new MockStore<UriProperties>();
        ScheduledExecutorService executorService = new SynchronousExecutorService();
        //loadBalancerStrategyFactories.put("rr", new RandomLoadBalancerStrategyFactory());
        loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
        // PrpcClientFactory();
        // new
        clientFactories.put("http", new DoNothingClientFactory());
        // HttpClientFactory();
        SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriRegistry, clusterRegistry, serviceRegistry, clientFactories, loadBalancerStrategyFactories);
        SimpleLoadBalancer loadBalancer = new SimpleLoadBalancer(state, 5, TimeUnit.SECONDS);
        FutureCallback<None> balancerCallback = new FutureCallback<None>();
        loadBalancer.start(balancerCallback);
        balancerCallback.get();
        URI uri1 = URI.create("http://test.qa1.com:1234");
        URI uri2 = URI.create("http://test.qa2.com:2345");
        URI uri3 = URI.create("http://test.qa3.com:6789");
        Map<Integer, PartitionData> partitionData = new HashMap<Integer, PartitionData>(1);
        partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
        Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<URI, Map<Integer, PartitionData>>(3);
        uriData.put(uri1, partitionData);
        uriData.put(uri2, partitionData);
        uriData.put(uri3, partitionData);
        prioritizedSchemes.add("http");
        clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
        serviceRegistry.put("foo", new ServiceProperties("foo", "cluster-1", "/foo", Arrays.asList("degrader"), Collections.<String, Object>emptyMap(), null, null, prioritizedSchemes, null));
        uriRegistry.put("cluster-1", new UriProperties("cluster-1", uriData));
        URI expectedUri1 = URI.create("http://test.qa1.com:1234/foo");
        URI expectedUri2 = URI.create("http://test.qa2.com:2345/foo");
        URI expectedUri3 = URI.create("http://test.qa3.com:6789/foo");
        Set<URI> expectedUris = new HashSet<URI>();
        expectedUris.add(expectedUri1);
        expectedUris.add(expectedUri2);
        expectedUris.add(expectedUri3);
        for (int i = 0; i < 100; ++i) {
            RewriteClient client = (RewriteClient) loadBalancer.getClient(new URIRequest("d2://foo/52"), new RequestContext());
            assertTrue(expectedUris.contains(client.getUri()));
            assertEquals(client.getUri().getScheme(), "http");
        }
        final CountDownLatch latch = new CountDownLatch(1);
        PropertyEventShutdownCallback callback = new PropertyEventShutdownCallback() {

            @Override
            public void done() {
                latch.countDown();
            }
        };
        state.shutdown(callback);
        if (!latch.await(60, TimeUnit.SECONDS)) {
            fail("unable to shutdown state");
        }
        executorService.shutdownNow();
        assertTrue(executorService.isShutdown(), "ExecutorService should have shut down!");
    }
}
Also used : PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) ArrayList(java.util.ArrayList) MockStore(com.linkedin.d2.discovery.stores.mock.MockStore) URI(java.net.URI) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) RequestContext(com.linkedin.r2.message.RequestContext) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) FutureCallback(com.linkedin.common.callback.FutureCallback) HashSet(java.util.HashSet) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SynchronousExecutorService(com.linkedin.d2.discovery.event.SynchronousExecutorService) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) URIRequest(com.linkedin.d2.balancer.util.URIRequest) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) CountDownLatch(java.util.concurrent.CountDownLatch) RewriteClient(com.linkedin.d2.balancer.clients.RewriteClient) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) None(com.linkedin.common.util.None) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 10 with ServiceUnavailableException

use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.

the class SimpleLoadBalancerTest method testLoadBalancerSimulationDegrader.

@Test(groups = { "medium", "back-end" })
public void testLoadBalancerSimulationDegrader() throws URISyntaxException, IOException, ServiceUnavailableException, InterruptedException {
    SimpleLoadBalancerSimulation simulator = new SimpleLoadBalancerSimulation(new DegraderLoadBalancerStrategyFactoryV3());
    simulator.simulateMultithreaded(1, 1000, 20);
    simulator.reset();
    simulator.simulateMultithreaded(50, 10000, 20);
    simulator.reset();
}
Also used : DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) SimpleLoadBalancerSimulation(com.linkedin.d2.balancer.simulator.SimpleLoadBalancerSimulation) Test(org.testng.annotations.Test)

Aggregations

URI (java.net.URI)16 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)15 Test (org.testng.annotations.Test)14 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)12 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)12 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)11 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)9 RequestContext (com.linkedin.r2.message.RequestContext)9 URIRequest (com.linkedin.d2.balancer.util.URIRequest)7 PropertyEventShutdownCallback (com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback)7 Map (java.util.Map)7 None (com.linkedin.common.util.None)6 SimpleLoadBalancerSimulation (com.linkedin.d2.balancer.simulator.SimpleLoadBalancerSimulation)6 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)6 RandomLoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory)6 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)6 HashSet (java.util.HashSet)6