Search in sources :

Example 1 with RandomLoadBalancerStrategy

use of com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testRegister.

@Test(groups = { "small", "back-end" })
public void testRegister() {
    reset();
    TestListener listener = new TestListener();
    List<String> schemes = new ArrayList<>();
    schemes.add("http");
    _state.register(listener);
    assertNull(listener.scheme);
    assertNull(listener.strategy);
    assertNull(listener.serviceName);
    // trigger a strategy add
    // first add a cluster
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
    // then add a service
    _state.listenToService("service-1", new NullStateListenerCallback());
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), null, null, schemes, null));
    // this should trigger a refresh
    assertEquals(listener.scheme, "http");
    assertTrue(listener.strategy instanceof RandomLoadBalancerStrategy);
    assertEquals(listener.serviceName, "service-1");
    // then update the cluster
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
    // this triggered a second refresh, but also an onStrategyRemoved. The onStrategyRemoved should
    // be done first, and then the onStrategyAdd, so we should still see a valid strategy.
    assertEquals(listener.scheme, "http");
    assertTrue(listener.strategy instanceof RandomLoadBalancerStrategy);
    assertEquals(listener.serviceName, "service-1");
    _state.listenToCluster("partition-cluster-1", new NullStateListenerCallback());
    _clusterRegistry.put("partition-cluster-1", new ClusterProperties("partition-cluster-1", null, new HashMap<>(), new HashSet<>(), new RangeBasedPartitionProperties("id=(\\d+)", 0, 100, 2)));
    _state.listenToService("partition-service-1", new NullStateListenerCallback());
    _serviceRegistry.put("partition-service-1", new ServiceProperties("partition-service-1", "partition-cluster-1", "/partition-test", Arrays.asList("degraderV3"), Collections.<String, Object>emptyMap(), null, null, schemes, null));
    assertEquals(listener.scheme, "http");
    assertTrue(listener.strategy instanceof DegraderLoadBalancerStrategyV3);
}
Also used : RandomLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DegraderLoadBalancerStrategyV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyV3) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) RangeBasedPartitionProperties(com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 2 with RandomLoadBalancerStrategy

use of com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testGetStrategy.

@Test(groups = { "small", "back-end" })
public void testGetStrategy() throws URISyntaxException {
    reset();
    URI uri = URI.create("http://cluster-1/test");
    List<String> schemes = new ArrayList<>();
    Map<URI, Double> weights = new HashMap<>();
    weights.put(uri, 1d);
    schemes.add("http");
    assertNull(_state.getStrategy("service-1", "http"));
    // set up state
    _state.listenToService("service-1", new NullStateListenerCallback());
    assertNull(_state.getStrategy("service-1", "http"));
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), null, null, schemes, null));
    LoadBalancerStrategy strategy = _state.getStrategy("service-1", "http");
    assertNotNull(strategy);
    assertTrue(strategy instanceof RandomLoadBalancerStrategy);
}
Also used : NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) HashMap(java.util.HashMap) RandomLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy) ArrayList(java.util.ArrayList) RelativeLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) RandomLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy) URI(java.net.URI) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 3 with RandomLoadBalancerStrategy

use of com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testServiceStrategyList.

@Test(groups = { "small", "back-end" })
public void testServiceStrategyList() throws URISyntaxException, InterruptedException {
    reset();
    LinkedList<String> strategyList = new LinkedList<>();
    URI uri = URI.create("http://cluster-1/test");
    List<String> schemes = new ArrayList<>();
    Map<URI, Double> weights = new HashMap<>();
    weights.put(uri, 1d);
    schemes.add("http");
    assertNull(_state.getStrategy("service-1", "http"));
    // set up state
    _state.listenToService("service-1", new NullStateListenerCallback());
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    assertNull(_state.getStrategy("service-1", "http"));
    // Put degraderV2_1 into the strategyList, it it not one of the supported strategies in
    // this strategyFactory, so we should not get a strategy back for http.
    strategyList.add("degraderV2_1");
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", strategyList, Collections.<String, Object>emptyMap(), null, null, schemes, null));
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
    assertNull(_state.getStrategy("service-1", "http"));
    // put the random strategy into the Strategy list, it is one of the supported strategies in the
    // strategyFactory for this unit test
    strategyList.clear();
    strategyList.add("random");
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", strategyList, Collections.<String, Object>emptyMap(), null, null, schemes, null));
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
    LoadBalancerStrategy strategy = _state.getStrategy("service-1", "http");
    assertNotNull(strategy);
    assertTrue(strategy instanceof RandomLoadBalancerStrategy);
    // now add the degraderV3 strategy into the Strategy list
    strategyList.addFirst("degraderV3");
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", strategyList, Collections.<String, Object>emptyMap(), null, null, schemes, null));
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
    strategy = _state.getStrategy("service-1", "http");
    assertNotNull(strategy);
    assertTrue(strategy instanceof DegraderLoadBalancerStrategyV3);
}
Also used : HashMap(java.util.HashMap) RandomLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy) ArrayList(java.util.ArrayList) RelativeLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) RandomLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy) URI(java.net.URI) LinkedList(java.util.LinkedList) DegraderLoadBalancerStrategyV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyV3) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 4 with RandomLoadBalancerStrategy

use of com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testRefreshServiceStrategies.

@Test(groups = { "small", "back-end" })
public void testRefreshServiceStrategies() throws URISyntaxException, InterruptedException {
    reset();
    URI uri = URI.create("http://cluster-1/test");
    List<String> schemes = new ArrayList<>();
    Map<URI, Double> weights = new HashMap<>();
    weights.put(uri, 1d);
    schemes.add("http");
    assertNull(_state.getStrategy("service-1", "http"));
    // set up state
    _state.listenToService("service-1", new NullStateListenerCallback());
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    assertNull(_state.getStrategy("service-1", "http"));
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), null, null, schemes, null));
    assertNotNull(_state.getStrategy("service-1", "http"));
    LoadBalancerStrategy strategy = _state.getStrategy("service-1", "http");
    assertNotNull(strategy);
    assertTrue(strategy instanceof RandomLoadBalancerStrategy);
    // check that we're getting the exact same strategy (by pointer) every time
    assertTrue(strategy == _state.getStrategy("service-1", "http"));
    assertTrue(strategy == _state.getStrategy("service-1", "http"));
    // now make sure adding a cluster property won't change the strategy
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
    // we should still have the same strategy now
    LoadBalancerStrategy newStrategy = _state.getStrategy("service-1", "http");
    assertNotNull(newStrategy);
    assertTrue(strategy == newStrategy);
    assertTrue(newStrategy instanceof RandomLoadBalancerStrategy);
    strategy = newStrategy;
    // refresh by adding service
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), null, null, schemes, null));
    // we should have a different strategy
    newStrategy = _state.getStrategy("service-1", "http");
    assertNotNull(newStrategy);
    assertFalse(strategy == newStrategy);
    assertTrue(newStrategy instanceof RandomLoadBalancerStrategy);
    TestShutdownCallback callback = new TestShutdownCallback();
    _state.shutdown(callback);
    assertTrue(callback.await(10, TimeUnit.SECONDS), "Failed to shut down state");
}
Also used : HashMap(java.util.HashMap) RandomLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy) ArrayList(java.util.ArrayList) RelativeLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) RandomLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy) URI(java.net.URI) NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Example 5 with RandomLoadBalancerStrategy

use of com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy in project rest.li by linkedin.

the class SimpleLoadBalancerStateTest method testUnregister.

@Test(groups = { "small", "back-end" })
public void testUnregister() {
    reset();
    TestListener listener = new TestListener();
    List<String> schemes = new ArrayList<>();
    schemes.add("http");
    _state.register(listener);
    assertNull(listener.scheme);
    assertNull(listener.strategy);
    assertNull(listener.serviceName);
    // trigger a strategy add
    // first add a cluster
    _state.listenToCluster("cluster-1", new NullStateListenerCallback());
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
    // then add a service
    _state.listenToService("service-1", new NullStateListenerCallback());
    _serviceRegistry.put("service-1", new ServiceProperties("service-1", "cluster-1", "/test", Arrays.asList("random"), Collections.<String, Object>emptyMap(), null, null, schemes, null));
    // this should trigger a refresh
    assertEquals(listener.scheme, "http");
    assertTrue(listener.strategy instanceof RandomLoadBalancerStrategy);
    assertEquals(listener.serviceName, "service-1");
    _state.unregister(listener);
    // then update the cluster
    _clusterRegistry.put("cluster-1", new ClusterProperties("cluster-1"));
    // there should be no update here, since we unregistered
    assertEquals(listener.scheme, "http");
    assertTrue(listener.strategy instanceof RandomLoadBalancerStrategy);
    assertEquals(listener.serviceName, "service-1");
}
Also used : NullStateListenerCallback(com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) RandomLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy) ArrayList(java.util.ArrayList) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) Test(org.testng.annotations.Test) DegraderLoadBalancerTest(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)

Aggregations

Test (org.testng.annotations.Test)6 NullStateListenerCallback (com.linkedin.d2.balancer.LoadBalancerState.NullStateListenerCallback)5 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)5 DegraderLoadBalancerTest (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerTest)5 RandomLoadBalancerStrategy (com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategy)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)4 URI (java.net.URI)4 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)3 RelativeLoadBalancerStrategy (com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy)3 DegraderLoadBalancerStrategyV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyV3)2 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)1 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)1 RangeBasedPartitionProperties (com.linkedin.d2.balancer.properties.RangeBasedPartitionProperties)1 RequestContext (com.linkedin.r2.message.RequestContext)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1