Search in sources :

Example 16 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 17 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)

Example 18 with ServiceUnavailableException

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

the class TestRouteLookupClient method testRouteLookupClientFuture.

@Test
public void testRouteLookupClientFuture() throws ExecutionException, InterruptedException {
    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();
    Future<RestResponse> future = routeLookupClient.restRequest(dummyRestRequest, "5436");
    try {
        future.get();
        // the request shouldn't succeed because we haven't set up a server or any service -> cluster -> uri
        // mapping; we want it to fail because we can get the service name we tried to get at from the
        // ServiceUnavailableException that is thrown.
        Assert.fail("Unexpected success, request should have thrown a ServiceUnavailableException");
    } catch (Exception e) {
        String message = e.getMessage();
        if (!message.contains("_serviceName=simple_uriWestCoast5436Foo")) {
            Assert.fail("request was not rewritten to point at the d2 service simple_uriWestCoast5436Foo");
        }
    }
}
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) Test(org.testng.annotations.Test)

Example 19 with ServiceUnavailableException

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

the class DynamicClient method getMetadata.

@Override
public Map<String, Object> getMetadata(URI uri) {
    if (_balancer != null) {
        try {
            String serviceName = LoadBalancerUtil.getServiceNameFromUri(uri);
            ServiceProperties serviceProperties = _balancer.getLoadBalancedServiceProperties(serviceName);
            if (serviceProperties != null) {
                return Collections.unmodifiableMap(serviceProperties.getServiceMetadataProperties());
            }
        } catch (ServiceUnavailableException e) {
            error(_log, e);
        }
    }
    return Collections.emptyMap();
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException)

Example 20 with ServiceUnavailableException

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

the class SimpleLoadBalancerStrawMan method main.

public static void main(String[] args) throws URISyntaxException, ServiceUnavailableException {
    // define the load balancing strategies that we support (round robin, etc)
    Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
    loadBalancerStrategyFactories.put("rr", new RandomLoadBalancerStrategyFactory());
    loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
    // define the clients that we support (http, etc)
    Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
    clientFactories.put("http", new HttpClientFactory());
    // listen for service updates (could be a glu discovery client, zk discovery client,
    // config discovery client, etc)
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    MockStore<ServiceProperties> serviceRegistry = new MockStore<ServiceProperties>();
    MockStore<ClusterProperties> clusterRegistry = new MockStore<ClusterProperties>();
    MockStore<UriProperties> uriRegistry = new MockStore<UriProperties>();
    SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriRegistry, clusterRegistry, serviceRegistry, clientFactories, loadBalancerStrategyFactories);
    // create the load balancer
    SimpleLoadBalancer loadBalancer = new SimpleLoadBalancer(state);
    final TransportClient tc = loadBalancer.getClient(new URIRequest("d2://browsemaps/52"), new RequestContext());
    final Client c = new TransportClientAdapter(tc, true);
    c.restRequest(null);
}
Also used : HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) MockStore(com.linkedin.d2.discovery.stores.mock.MockStore) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RequestContext(com.linkedin.r2.message.RequestContext) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) Client(com.linkedin.r2.transport.common.Client) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) URIRequest(com.linkedin.d2.balancer.util.URIRequest) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties)

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