Search in sources :

Example 56 with ServiceProperties

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

the class SimpleLoadBalancerStateTest method testListenToService.

@Test(groups = { "small", "back-end" })
public void testListenToService() throws InterruptedException {
    reset();
    assertFalse(_state.isListeningToService("service-1"));
    assertNull(_state.getServiceProperties("service-1"));
    final CountDownLatch latch = new CountDownLatch(1);
    LoadBalancerStateListenerCallback callback = new LoadBalancerStateListenerCallback() {

        @Override
        public void done(int type, String name) {
            latch.countDown();
        }
    };
    _state.listenToService("service-1", callback);
    if (!latch.await(5, TimeUnit.SECONDS)) {
        fail("didn't get callback when listenToService was called");
    }
    assertTrue(_state.isListeningToService("service-1"));
    assertNotNull(_state.getServiceProperties("service-1"));
    assertNull(_state.getServiceProperties("service-1").getProperty());
    ServiceProperties property = new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"));
    _serviceRegistry.put("service-1", property);
    assertTrue(_state.isListeningToService("service-1"));
    assertNotNull(_state.getServiceProperties("service-1"));
    assertEquals(_state.getServiceProperties("service-1").getProperty(), property);
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) LoadBalancerStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.LoadBalancerStateListenerCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 57 with ServiceProperties

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

the class SimpleLoadBalancerStateTest method testRefreshWithConcurrentGetTC.

// This test is to verify a fix for a specific bug, where the d2 client receives a zookeeper
// update and concurrent getTrackerClient requests. In that case, all but the first concurrent
// requests got a null tracker client because the degraderLoadBalancerState was not fully initialized
// (hashring was empty), and this continued until the first request had atomically swamped a
// fully initialized state for other requests to use. This test failed on pre-fix code, it now
// succeeds.
@Test(groups = { "small", "back-end" })
public void testRefreshWithConcurrentGetTC() throws URISyntaxException, InterruptedException {
    reset();
    LinkedList<String> strategyList = new LinkedList<String>();
    URI uri = URI.create("http://cluster-1/test");
    final List<String> schemes = new ArrayList<String>();
    schemes.add("http");
    strategyList.add("degraderV3");
    // set up state
    _state.listenToService("service-1", new NullStateListenerCallback());
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    assertNull(_state.getStrategy("service-1", "http"));
    // Use the _clusterRegistry.put to populate the _state.clusterProperties, used by
    // _state.refreshServiceStrategies
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", strategyList, Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), Collections.<String, String>emptyMap(), schemes, Collections.<URI>emptySet()));
    LoadBalancerStrategy strategy = _state.getStrategy("service-1", "http");
    assertNotNull(strategy, "got null strategy in setup");
    // test serial to make sure things are working before concurrent test
    TransportClient resultTC = _state.getClient("service-1", "http");
    assertNotNull(resultTC, "got null tracker client in non-concurrent env");
    ExecutorService myExecutor = Executors.newCachedThreadPool();
    ArrayList<TcCallable> cArray = new ArrayList<TcCallable>();
    List<TrackerClient> clients = new ArrayList<TrackerClient>();
    Map<Integer, PartitionData> partitionDataMap = new HashMap<Integer, PartitionData>(2);
    partitionDataMap.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
    clients.add(new TrackerClient(uri, partitionDataMap, new DegraderLoadBalancerTest.TestLoadBalancerClient(uri), SystemClock.instance(), null));
    for (int i = 0; i < 20; i++) {
        cArray.add(i, new TcCallable(clients, _state));
    }
    Runnable refreshTask = new Runnable() {

        @Override
        public void run() {
            while (true) {
                List<String> myStrategyList = new LinkedList<String>();
                myStrategyList.add("degraderV3");
                _state.refreshServiceStrategies(new ServiceProperties("service-1", "cluster-1", "/test", myStrategyList, Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), Collections.<String, String>emptyMap(), schemes, Collections.<URI>emptySet()));
                if (Thread.interrupted()) {
                    return;
                }
            }
        }
    };
    myExecutor.execute(refreshTask);
    Integer badResults = 0;
    ArrayList<Future<Integer>> myList = new ArrayList<Future<Integer>>();
    for (int i = 0; i < cArray.size(); i++) {
        @SuppressWarnings("unchecked") Callable<Integer> c = (Callable) cArray.get(i);
        myList.add(i, myExecutor.submit(c));
    }
    try {
        for (int i = 0; i < cArray.size(); i++) {
            badResults += myList.get(i).get();
        }
    } catch (ExecutionException e) {
        Assert.assertFalse(true, "got ExecutionException");
    } finally {
        try {
            // call shutdownNow() to send an interrupt to the refreshTask
            myExecutor.shutdownNow();
            boolean status = myExecutor.awaitTermination(5, TimeUnit.SECONDS);
            if (status == false) {
                Assert.assertFalse(true, "failed to shutdown threads correctly");
            }
        } catch (InterruptedException ie) {
            // this thread was interrupted
            myExecutor.shutdownNow();
        }
    }
    Assert.assertTrue(badResults == 0, "getTrackerClients returned null");
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(java.net.URI) Callable(java.util.concurrent.Callable) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) ExecutionException(java.util.concurrent.ExecutionException) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) RandomLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy) LinkedList(java.util.LinkedList) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) SynchronousExecutorService(com.linkedin.d2.discovery.event.SynchronousExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Future(java.util.concurrent.Future) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 58 with ServiceProperties

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

the class MockLBFactory method createLoadBalancer.

static SimpleLoadBalancer createLoadBalancer() {
    // 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("degrader", new DegraderLoadBalancerStrategyFactoryV3());
    Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
    clientFactories.put("http", new HttpClientFactory());
    SynchronousExecutorService executorService = new SynchronousExecutorService();
    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);
    state.listenToService("greetings", new LoadBalancerState.NullStateListenerCallback());
    state.listenToService("groups", new LoadBalancerState.NullStateListenerCallback());
    state.listenToCluster("testcluster", new LoadBalancerState.NullStateListenerCallback());
    state.listenToCluster("badcluster", new LoadBalancerState.NullStateListenerCallback());
    List<String> schemes = new ArrayList<String>();
    schemes.add("http");
    Map<String, Object> metadataProperties = new HashMap<String, Object>();
    metadataProperties.put(RestConstants.RESTLI_PROTOCOL_VERSION_PROPERTY, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString());
    serviceRegistry.put("greetings", new ServiceProperties("greetings", "testcluster", "/greetings", Arrays.asList("degrader"), Collections.<String, Object>emptyMap(), null, null, schemes, null, metadataProperties));
    serviceRegistry.put("groups", new ServiceProperties("groups", "badcluster", "/groups", Arrays.asList("degrader"), Collections.<String, Object>emptyMap(), null, null, schemes, null, metadataProperties));
    clusterRegistry.put("testcluster", new ClusterProperties("testcluster"));
    clusterRegistry.put("badcluster", new ClusterProperties("badcluster"));
    uriRegistry.put("testcluster", new UriProperties("testcluster", createUriData("http://localhost:1338")));
    uriRegistry.put("badcluster", new UriProperties("badcluster", createUriData("http://localhost:1337")));
    // create the load balancer
    return new SimpleLoadBalancer(state);
}
Also used : HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) ArrayList(java.util.ArrayList) SimpleLoadBalancerState(com.linkedin.d2.balancer.simple.SimpleLoadBalancerState) LoadBalancerState(com.linkedin.d2.balancer.LoadBalancerState) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) SynchronousExecutorService(com.linkedin.d2.discovery.event.SynchronousExecutorService) SimpleLoadBalancerState(com.linkedin.d2.balancer.simple.SimpleLoadBalancerState) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties)

Aggregations

ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)53 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)37 HashMap (java.util.HashMap)36 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)33 URI (java.net.URI)30 Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)28 Map (java.util.Map)22 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)21 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)19 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)19 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)17 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)15 ServicePropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)14 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)14 HashSet (java.util.HashSet)12 UriPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer)11 None (com.linkedin.common.util.None)10 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)10 FutureCallback (com.linkedin.common.callback.FutureCallback)9