use of com.linkedin.d2.balancer.strategies.LoadBalancerStrategy in project rest.li by linkedin.
the class ConsistentHashKeyMapperTest method getConsistentHashKeyMapper.
private ConsistentHashKeyMapper getConsistentHashKeyMapper(RingFactory<URI> ringFactory) throws URISyntaxException {
String serviceName = "articles";
String clusterName = "cluster";
String path = "path";
String strategyName = "degrader";
//setup partition
Map<URI, Map<Integer, PartitionData>> partitionDescriptions = new HashMap<URI, Map<Integer, PartitionData>>();
final URI foo1 = new URI("http://foo1.com");
Map<Integer, PartitionData> foo1Data = new HashMap<Integer, PartitionData>();
foo1Data.put(0, new PartitionData(1.0));
partitionDescriptions.put(foo1, foo1Data);
final URI foo2 = new URI("http://foo2.com");
Map<Integer, PartitionData> foo2Data = new HashMap<Integer, PartitionData>();
foo2Data.put(3, new PartitionData(1.0));
foo2Data.put(4, new PartitionData(1.0));
partitionDescriptions.put(foo2, foo2Data);
final URI foo3 = new URI("http://foo3.com");
Map<Integer, PartitionData> foo3Data = new HashMap<Integer, PartitionData>();
foo3Data.put(0, new PartitionData(1.0));
partitionDescriptions.put(foo3, foo3Data);
final URI foo4 = new URI("http://foo4.com");
Map<Integer, PartitionData> foo4Data = new HashMap<Integer, PartitionData>();
foo4Data.put(1, new PartitionData(1.0));
partitionDescriptions.put(foo4, foo4Data);
final URI foo5 = new URI("http://foo5.com");
Map<Integer, PartitionData> foo5Data = new HashMap<Integer, PartitionData>();
foo5Data.put(1, new PartitionData(1.0));
partitionDescriptions.put(foo5, foo5Data);
final URI foo6 = new URI("http://foo6.com");
Map<Integer, PartitionData> foo6Data = new HashMap<Integer, PartitionData>();
foo6Data.put(1, new PartitionData(1.0));
partitionDescriptions.put(foo6, foo6Data);
//setup strategy which involves tweaking the hash ring to get partitionId -> URI host
List<LoadBalancerState.SchemeStrategyPair> orderedStrategies = new ArrayList<LoadBalancerState.SchemeStrategyPair>();
LoadBalancerStrategy strategy = new TestLoadBalancerStrategy(partitionDescriptions, ringFactory);
orderedStrategies.add(new LoadBalancerState.SchemeStrategyPair("http", strategy));
//setup the partition accessor which is used to get partitionId -> keys
PartitionAccessor accessor = new TestPartitionAccessor();
URI serviceURI = new URI("d2://" + serviceName);
SimpleLoadBalancer balancer = new SimpleLoadBalancer(new PartitionedLoadBalancerTestState(clusterName, serviceName, path, strategyName, partitionDescriptions, orderedStrategies, accessor));
ConsistentHashKeyMapper mapper = new ConsistentHashKeyMapper(balancer, balancer);
return mapper;
}
use of com.linkedin.d2.balancer.strategies.LoadBalancerStrategy in project rest.li by linkedin.
the class ZKFSLoadBalancerWithFacilitiesFactory method createDefaultLoadBalancerStrategyFactories.
private Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> createDefaultLoadBalancerStrategyFactories(HealthCheckOperations healthCheckOperations, ScheduledExecutorService executorService) {
final Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
final RandomLoadBalancerStrategyFactory randomStrategyFactory = new RandomLoadBalancerStrategyFactory();
final DegraderLoadBalancerStrategyFactoryV3 degraderStrategyFactoryV3 = new DegraderLoadBalancerStrategyFactoryV3(healthCheckOperations, executorService);
loadBalancerStrategyFactories.put("random", randomStrategyFactory);
loadBalancerStrategyFactories.put("degrader", degraderStrategyFactoryV3);
loadBalancerStrategyFactories.put("degraderV2", degraderStrategyFactoryV3);
loadBalancerStrategyFactories.put("degraderV3", degraderStrategyFactoryV3);
loadBalancerStrategyFactories.put("degraderV2_1", degraderStrategyFactoryV3);
return loadBalancerStrategyFactories;
}
use of com.linkedin.d2.balancer.strategies.LoadBalancerStrategy in project rest.li by linkedin.
the class SimpleLoadBalancerStateTest method reset.
public void reset(boolean useSSL) {
_executorService = new SynchronousExecutorService();
_uriRegistry = new MockStore<UriProperties>();
_clusterRegistry = new MockStore<ClusterProperties>();
_serviceRegistry = new MockStore<ServiceProperties>();
_clientFactories = new HashMap<String, TransportClientFactory>();
_loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
_loadBalancerStrategyFactories.put("random", new RandomLoadBalancerStrategyFactory());
_loadBalancerStrategyFactories.put("degraderV3", new DegraderLoadBalancerStrategyFactoryV3());
try {
_sslContext = SSLContext.getDefault();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
_sslParameters = new SSLParameters();
if (useSSL) {
_clientFactories.put("https", new SimpleLoadBalancerTest.DoNothingClientFactory());
_state = new SimpleLoadBalancerState(_executorService, _uriRegistry, _clusterRegistry, _serviceRegistry, _clientFactories, _loadBalancerStrategyFactories, _sslContext, _sslParameters, true);
} else {
_clientFactories.put("http", new SimpleLoadBalancerTest.DoNothingClientFactory());
_state = new SimpleLoadBalancerState(_executorService, _uriRegistry, _clusterRegistry, _serviceRegistry, _clientFactories, _loadBalancerStrategyFactories);
}
FutureCallback<None> callback = new FutureCallback<None>();
_state.start(callback);
try {
callback.get();
} catch (Exception e) {
Assert.fail("State start failed", e);
}
}
use of com.linkedin.d2.balancer.strategies.LoadBalancerStrategy 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<String>();
URI uri = URI.create("http://cluster-1/test");
List<String> schemes = new ArrayList<String>();
Map<URI, Double> weights = new HashMap<URI, Double>();
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 degrader 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("degrader");
_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);
}
use of com.linkedin.d2.balancer.strategies.LoadBalancerStrategy 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<String>();
Map<URI, Double> weights = new HashMap<URI, Double>();
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);
}
Aggregations