Search in sources :

Example 11 with ServiceUnavailableException

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

the class SimpleLoadBalancerTest method testLoadBalancerDropRate.

/**
   * This test simulates dropping requests by playing with OverrideDropRate in config
   *
   */
@Test(groups = { "small", "back-end" })
public void testLoadBalancerDropRate() throws ServiceUnavailableException, ExecutionException, InterruptedException {
    final int RETRY = 10;
    for (int tryAgain = 0; tryAgain < RETRY; ++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);
        Random random = new Random();
        for (int i = 0; i < 100; ++i) {
            try {
                RewriteClient client = (RewriteClient) loadBalancer.getClient(new URIRequest("d2://foo/52"), new RequestContext());
                TrackerClient tClient = (TrackerClient) client.getWrappedClient();
                DegraderImpl degrader = (DegraderImpl) tClient.getDegrader(DefaultPartitionAccessor.DEFAULT_PARTITION_ID);
                DegraderImpl.Config cfg = new DegraderImpl.Config(degrader.getConfig());
                // Change DropRate to 0.0 at the rate of 1/3
                cfg.setOverrideDropRate((random.nextInt(2) == 0) ? 1.0 : 0.0);
                degrader.setConfig(cfg);
                assertTrue(expectedUris.contains(client.getUri()));
                assertEquals(client.getUri().getScheme(), "http");
            } catch (ServiceUnavailableException e) {
                assertTrue(e.toString().contains("in a bad state (high latency/high error)"));
            }
        }
        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) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) URI(java.net.URI) Random(java.util.Random) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) 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) DegraderImpl(com.linkedin.util.degrader.DegraderImpl) 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 12 with ServiceUnavailableException

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

the class SimpleLoadBalancerTest method testLoadBalancerSimulationRandom.

@Test(groups = { "medium", "back-end" }, enabled = false)
public void testLoadBalancerSimulationRandom() throws URISyntaxException, IOException, ServiceUnavailableException, InterruptedException {
    SimpleLoadBalancerSimulation simulator = new SimpleLoadBalancerSimulation(new RandomLoadBalancerStrategyFactory());
    simulator.simulateMultithreaded(1, 1000, 20);
    simulator.reset();
    simulator.simulateMultithreaded(50, 10000, 20);
    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 13 with ServiceUnavailableException

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

the class LoadBalancerSimulator method getPoints.

/**
   * Given a serviceName and partition number, return the hashring points for each URI
   * @param serviceName
   * @param partition
   * @return
   * @throws ServiceUnavailableException
   */
public Map<URI, Integer> getPoints(String serviceName, int partition) throws ServiceUnavailableException {
    URI serviceUri = URI.create("d2://" + serviceName);
    Ring<URI> ring = _loadBalancer.getRings(serviceUri).get(partition);
    Map<URI, Integer> pointsMap = new HashMap<>();
    Iterator<URI> iter = ring.getIterator(0);
    iter.forEachRemaining(uri -> pointsMap.compute(uri, (k, v) -> v == null ? 1 : v + 1));
    return pointsMap;
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) LoggerFactory(org.slf4j.LoggerFactory) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) TimeoutException(java.util.concurrent.TimeoutException) RewriteClient(com.linkedin.d2.balancer.clients.RewriteClient) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) PriorityBlockingQueue(java.util.concurrent.PriorityBlockingQueue) Future(java.util.concurrent.Future) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) Map(java.util.Map) URI(java.net.URI) Delayed(java.util.concurrent.Delayed) Assert.assertFalse(org.testng.Assert.assertFalse) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) SynchronousExecutorService(com.linkedin.d2.discovery.event.SynchronousExecutorService) Collection(java.util.Collection) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) MockStore(com.linkedin.d2.discovery.stores.mock.MockStore) PropertyKeys(com.linkedin.d2.balancer.properties.PropertyKeys) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) None(com.linkedin.common.util.None) PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) DegraderLoadBalancerStrategyConfig(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyConfig) FutureCallback(com.linkedin.common.callback.FutureCallback) FutureTask(java.util.concurrent.FutureTask) Callback(com.linkedin.common.callback.Callback) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) URIRequest(com.linkedin.d2.balancer.util.URIRequest) Assert(org.testng.Assert) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Ring(com.linkedin.d2.balancer.util.hashing.Ring) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) ExecutorService(java.util.concurrent.ExecutorService) RunnableFuture(java.util.concurrent.RunnableFuture) RestRequest(com.linkedin.r2.message.rest.RestRequest) Logger(org.slf4j.Logger) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) Iterator(java.util.Iterator) Clock(com.linkedin.util.clock.Clock) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) RequestContext(com.linkedin.r2.message.RequestContext) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) Collections(java.util.Collections) HashMap(java.util.HashMap) URI(java.net.URI)

Example 14 with ServiceUnavailableException

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

the class SimpleLoadBalancerSimulation method simulateMultithreaded.

/**
   * Run the simulation from a play-back text file. The format of the text file is the
   * same as the way the random messages are generated in getRandomMessage().
   *
   * @param threads
   *          Number of threads to run.
   * @param simulationStepSize
   *          Number of operations to perform during each step.
   * @param playbackStream
   *          The input stream of messages to play back.
   * @throws IOException
   * @throws ServiceUnavailableException
   */
public void simulateMultithreaded(int threads, int simulationStepSize, InputStream playbackStream) throws IOException, ServiceUnavailableException {
    BufferedReader br = new BufferedReader(new InputStreamReader(playbackStream));
    String line = "";
    initQueues(threads);
    int lineCount = 0;
    while (line != null) {
        // load simulation steps into queue
        for (int i = 0; i < simulationStepSize && line != null; ++i) {
            line = br.readLine();
            if (line != null) {
                lineCount++;
                String[] message = line.split(" ");
                _queues[getThreadId(message, threads)].add(message);
            }
        }
        // simulate
        runSimulation(threads);
        // now validate that state is as we expect
        verifyState();
        reset();
    }
    try {
        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");
        }
    } catch (InterruptedException e) {
        fail("unable to shutdown");
    }
}
Also used : PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 15 with ServiceUnavailableException

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

the class SimpleLoadBalancerSimulation method simulateMultithreaded.

/**
   * Run the simulation using randomly generated messages.
   *
   * @param threads
   *          Number of threads to run.
   * @param simulationStepSize
   *          Number of operations to perform during each step.
   * @param totalSteps
   *          The number of steps.
   * @throws ServiceUnavailableException
   */
public void simulateMultithreaded(int threads, int simulationStepSize, int totalSteps, String messageLogPath) throws ServiceUnavailableException {
    initQueues(threads);
    initMessageLog(messageLogPath);
    for (int step = 0; step < totalSteps; ++step) {
        try {
            // load simulation steps into queue
            for (int i = 0; i < simulationStepSize; ++i) {
                String[] message = getRandomMessage();
                if (_messageLog != null) {
                    _messageLog.write((LoadBalancerUtil.join(Arrays.asList(message), " ") + "\n").getBytes("UTF-8"));
                }
                _queues[getThreadId(message, threads)].add(message);
                ++_totalMessages;
            }
        } catch (UnsupportedEncodingException e) {
            fail("unable to encode message properly");
        } catch (IOException e) {
            fail("unable to write to file");
        }
        // simulate
        long start = System.currentTimeMillis();
        runSimulation(threads);
        System.err.println(simulationStepSize + " in " + (System.currentTimeMillis() - start) + "ms (" + _totalMessages + " total)");
        // now validate that state is as we expect
        verifyState();
        reset();
    }
    try {
        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");
        }
    } catch (InterruptedException e) {
        fail("interrupted during state and thread shutdown");
    }
}
Also used : PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

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