Search in sources :

Example 1 with RandomLoadBalancerStrategyFactory

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

the class D2ClientBuilder method createDefaultLoadBalancerStrategyFactories.

/**
 * Adds the default load balancer strategy factories only if they are not present in the provided factories
 * during the transition period.
 *
 * @return Default mapping of the load balancer strategy names and the strategies
 */
private Map<String, LoadBalancerStrategyFactory<?>> createDefaultLoadBalancerStrategyFactories() {
    final Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<>(_config.loadBalancerStrategyFactories);
    final RandomLoadBalancerStrategyFactory randomStrategyFactory = new RandomLoadBalancerStrategyFactory();
    loadBalancerStrategyFactories.putIfAbsent("random", randomStrategyFactory);
    final DegraderLoadBalancerStrategyFactoryV3 degraderStrategyFactoryV3 = new DegraderLoadBalancerStrategyFactoryV3(_config.healthCheckOperations, _config._executorService, _config.eventEmitter, Collections.emptyList());
    loadBalancerStrategyFactories.putIfAbsent("degrader", degraderStrategyFactoryV3);
    loadBalancerStrategyFactories.putIfAbsent("degraderV2", degraderStrategyFactoryV3);
    loadBalancerStrategyFactories.putIfAbsent("degraderV3", degraderStrategyFactoryV3);
    loadBalancerStrategyFactories.putIfAbsent("degraderV2_1", degraderStrategyFactoryV3);
    if (_config.enableRelativeLoadBalancer) {
        final RelativeLoadBalancerStrategyFactory relativeLoadBalancerStrategyFactory = new RelativeLoadBalancerStrategyFactory(_config._executorService, _config.healthCheckOperations, Collections.emptyList(), _config.eventEmitter, SystemClock.instance());
        loadBalancerStrategyFactories.putIfAbsent(RelativeLoadBalancerStrategy.RELATIVE_LOAD_BALANCER_STRATEGY_NAME, relativeLoadBalancerStrategyFactory);
    }
    return loadBalancerStrategyFactories;
}
Also used : RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) RelativeLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategyFactory) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) RelativeLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategyFactory) RelativeLoadBalancerStrategy(com.linkedin.d2.balancer.strategies.relative.RelativeLoadBalancerStrategy) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)

Example 2 with RandomLoadBalancerStrategyFactory

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

the class LoadBalancerClientCli method getZKFSLoadBalancer.

public ZKFSLoadBalancer getZKFSLoadBalancer(String zkConnectString, String d2path, String d2ServicePath) throws Exception {
    _tmpDir = createTempDirectory(_tmpdirName);
    ZKFSComponentFactory componentFactory = new ZKFSComponentFactory();
    if (d2ServicePath == null || d2ServicePath.isEmpty()) {
        d2ServicePath = "services";
    }
    Map<String, TransportClientFactory> clientFactories = new HashMap<>();
    clientFactories.put("http", new HttpClientFactory.Builder().build());
    Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<>();
    loadBalancerStrategyFactories.put("random", new RandomLoadBalancerStrategyFactory());
    loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
    loadBalancerStrategyFactories.put("degraderV2", new DegraderLoadBalancerStrategyFactoryV3());
    loadBalancerStrategyFactories.put("degraderV3", new DegraderLoadBalancerStrategyFactoryV3());
    loadBalancerStrategyFactories.put("degraderV2_1", new DegraderLoadBalancerStrategyFactoryV3());
    ZKFSTogglingLoadBalancerFactoryImpl factory = new ZKFSTogglingLoadBalancerFactoryImpl(componentFactory, TIMEOUT, TimeUnit.MILLISECONDS, d2path, _tmpDir.getAbsolutePath(), clientFactories, loadBalancerStrategyFactories, d2ServicePath, null, null, false);
    return new ZKFSLoadBalancer(zkConnectString, SESSION_TIMEOUT, (int) TIMEOUT, factory, null, d2path);
}
Also used : ZKFSTogglingLoadBalancerFactoryImpl(com.linkedin.d2.balancer.zkfs.ZKFSTogglingLoadBalancerFactoryImpl) HashMap(java.util.HashMap) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) RandomLoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) ZKFSComponentFactory(com.linkedin.d2.balancer.zkfs.ZKFSComponentFactory) ZKFSLoadBalancer(com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory)

Example 3 with RandomLoadBalancerStrategyFactory

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

the class SimpleLoadBalancerTest method testGetClient.

/**
 * This tests getClient(). When TargetHints and scheme does not match, throw ServiceUnavailableException
 * @throws Exception
 */
@Test(expectedExceptions = ServiceUnavailableException.class)
@SuppressWarnings("deprecation")
public void testGetClient() throws Exception {
    Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<>();
    Map<String, TransportClientFactory> clientFactories = new HashMap<>();
    List<String> prioritizedSchemes = new ArrayList<>();
    MockStore<ServiceProperties> serviceRegistry = new MockStore<>();
    MockStore<ClusterProperties> clusterRegistry = new MockStore<>();
    MockStore<UriProperties> uriRegistry = new MockStore<>();
    ScheduledExecutorService executorService = new SynchronousExecutorService();
    // loadBalancerStrategyFactories.put("rr", new RandomLoadBalancerStrategyFactory());
    loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
    // PrpcClientFactory();
    // new
    clientFactories.put(PropertyKeys.HTTPS_SCHEME, new DoNothingClientFactory());
    // HttpClientFactory();
    SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriRegistry, clusterRegistry, serviceRegistry, clientFactories, loadBalancerStrategyFactories);
    SimpleLoadBalancer loadBalancer = new SimpleLoadBalancer(state, 5, TimeUnit.SECONDS, _d2Executor);
    FutureCallback<None> balancerCallback = new FutureCallback<>();
    loadBalancer.start(balancerCallback);
    balancerCallback.get(5, TimeUnit.SECONDS);
    Map<Integer, PartitionData> partitionData = new HashMap<>(1);
    partitionData.put(DEFAULT_PARTITION_ID, new PartitionData(1d));
    Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<>(3);
    prioritizedSchemes.add(PropertyKeys.HTTPS_SCHEME);
    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 uri = URI.create("http://test.qd.com:1234/foo");
    RequestContext requestContextWithHint = new RequestContext();
    LoadBalancerUtil.TargetHints.setRequestContextTargetService(requestContextWithHint, uri);
    URIRequest uriRequest = new URIRequest("d2://foo");
    loadBalancer.getClient(uriRequest, requestContextWithHint);
}
Also used : HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) ArrayList(java.util.ArrayList) MockStore(com.linkedin.d2.discovery.stores.mock.MockStore) URI(java.net.URI) 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) 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) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) None(com.linkedin.common.util.None) Map(java.util.Map) DarkClusterConfigMap(com.linkedin.d2.DarkClusterConfigMap) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 4 with RandomLoadBalancerStrategyFactory

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

the class SimpleLoadBalancerTest method testLoadBalancerSimulationRandomLarge.

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

use of com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory 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)

Aggregations

RandomLoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory)12 HashMap (java.util.HashMap)10 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)9 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)9 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)9 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)7 FutureCallback (com.linkedin.common.callback.FutureCallback)5 None (com.linkedin.common.util.None)5 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)5 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)5 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)5 SynchronousExecutorService (com.linkedin.d2.discovery.event.SynchronousExecutorService)5 RequestContext (com.linkedin.r2.message.RequestContext)5 Test (org.testng.annotations.Test)5 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)4 URIRequest (com.linkedin.d2.balancer.util.URIRequest)4 MockStore (com.linkedin.d2.discovery.stores.mock.MockStore)4 URI (java.net.URI)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 DarkClusterConfigMap (com.linkedin.d2.DarkClusterConfigMap)3