Search in sources :

Example 31 with LoadBalancer

use of com.linkedin.d2.balancer.LoadBalancer 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<>();
    loadBalancerStrategyFactories.put("rr", new RandomLoadBalancerStrategyFactory());
    loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
    // define the clients that we support (http, etc)
    Map<String, TransportClientFactory> clientFactories = new HashMap<>();
    clientFactories.put("http", new HttpClientFactory.Builder().build());
    // 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<>();
    MockStore<ClusterProperties> clusterRegistry = new MockStore<>();
    MockStore<UriProperties> uriRegistry = new MockStore<>();
    SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriRegistry, clusterRegistry, serviceRegistry, clientFactories, loadBalancerStrategyFactories);
    // create the load balancer
    SimpleLoadBalancer loadBalancer = new SimpleLoadBalancer(state, executorService);
    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) 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)

Example 32 with LoadBalancer

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

the class TestScatterGather method testScatterGatherEntityLoadBalancerIntegration.

@Test(dataProvider = "requestBuilderDataProvider")
public static void testScatterGatherEntityLoadBalancerIntegration(RootBuilderWrapper<Long, Greeting> builders) throws Exception {
    SimpleLoadBalancer loadBalancer = MockLBFactory.createLoadBalancer();
    KeyMapper keyMapper = new ConsistentHashKeyMapper(loadBalancer, new TestPartitionInfoProvider());
    try {
        keyMapper.mapKeysV2(URI.create("http://badurischeme/"), new HashSet<String>());
        Assert.fail("keyMapper should reject non-D2 URI scheme");
    } catch (IllegalArgumentException e) {
    // expected
    }
    ScatterGatherBuilder<Greeting> sg = new ScatterGatherBuilder<>(keyMapper);
    final int NUM_IDS = 20;
    Long[] requestIds = generateIds(NUM_IDS);
    Collection<ScatterGatherBuilder.KVRequestInfo<Long, EntityResponse<Greeting>>> scatterGatherRequests = buildScatterGatherGetEntityRequests(sg, requestIds);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) HostToKeyMapper(com.linkedin.d2.balancer.util.HostToKeyMapper) KeyMapper(com.linkedin.d2.balancer.KeyMapper) ConsistentHashKeyMapper(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper) ConsistentHashKeyMapper(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper) Test(org.testng.annotations.Test) RestLiIntegrationTest(com.linkedin.restli.examples.RestLiIntegrationTest)

Aggregations

Test (org.testng.annotations.Test)22 None (com.linkedin.common.util.None)17 FutureCallback (com.linkedin.common.callback.FutureCallback)16 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)13 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)13 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)13 MockStore (com.linkedin.d2.discovery.stores.mock.MockStore)12 RequestContext (com.linkedin.r2.message.RequestContext)11 HashMap (java.util.HashMap)11 LoadBalancer (com.linkedin.d2.balancer.LoadBalancer)9 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)9 LoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory)9 RandomLoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory)9 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)9 ArrayList (java.util.ArrayList)9 DarkClusterConfigMap (com.linkedin.d2.DarkClusterConfigMap)8 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)8 URI (java.net.URI)8 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)8 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)7