Search in sources :

Example 31 with ServiceUnavailableException

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

the class SimpleLoadBalancer method listenToServiceAndCluster.

private ServiceProperties listenToServiceAndCluster(URI uri) throws ServiceUnavailableException {
    if (!D2_SCHEME_NAME.equalsIgnoreCase(uri.getScheme())) {
        throw new IllegalArgumentException("Unsupported scheme in URI " + uri);
    }
    // get the service for this uri
    String serviceName = LoadBalancerUtil.getServiceNameFromUri(uri);
    ServiceProperties service = getLoadBalancedServiceProperties(serviceName);
    String clusterName = service.getClusterName();
    listenToCluster(serviceName, clusterName);
    return service;
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties)

Example 32 with ServiceUnavailableException

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

the class SimpleLoadBalancer method getPartitionAccessor.

@Override
public PartitionAccessor getPartitionAccessor(URI serviceUri) throws ServiceUnavailableException {
    ServiceProperties service = listenToServiceAndCluster(serviceUri);
    String serviceName = service.getServiceName();
    String clusterName = service.getClusterName();
    return getPartitionAccessor(serviceName, clusterName);
}
Also used : ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties)

Example 33 with ServiceUnavailableException

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

the class ConsistentHashKeyMapperTest method createRunnables.

/**
   * Create tasks for the deadlock test
   */
private List<Runnable> createRunnables(int num, final ConsistentHashKeyMapper mapper, String serviceName, final CountDownLatch latch) throws URISyntaxException {
    final URI serviceURI = new URI("d2://" + serviceName);
    List<Runnable> runnables = new ArrayList<Runnable>();
    for (int i = 0; i < num; i++) {
        // since i < numPartitions, the keys will be distributed to different partitions
        final List<String> keys = generateKeys(i);
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                // wait until all jobs submitted
                latch.countDown();
                try {
                    latch.await();
                    mapper.mapKeysV3(serviceURI, keys, 1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ServiceUnavailableException e) {
                    e.printStackTrace();
                }
            }
        };
        runnables.add(runnable);
    }
    return runnables;
}
Also used : ArrayList(java.util.ArrayList) ServiceUnavailableException(com.linkedin.d2.balancer.ServiceUnavailableException) URI(java.net.URI)

Example 34 with ServiceUnavailableException

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

the class TestAllPartitionsRequestBuilder method testSendAllPartitionsRequests.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "restliRequestOptions")
public void testSendAllPartitionsRequests(RestliRequestOptions options, RingFactory<URI> ringFactory) throws ServiceUnavailableException, URISyntaxException, RestException, InterruptedException {
    final int PARTITION_NUM = 5;
    List<URI> expectedUris = new ArrayList<URI>();
    ConsistentHashKeyMapper mapper = getKeyToHostMapper(PARTITION_NUM, expectedUris, ringFactory);
    AllPartitionsRequestBuilder<Greeting> searchRB = new AllPartitionsRequestBuilder<Greeting>(mapper);
    ActionRequestBuilder<Long, Greeting> builder = new ActionRequestBuilder<Long, Greeting>(TEST_URI, Greeting.class, _COLL_SPEC, options);
    ActionRequest<Greeting> request = builder.name("updateTone").id(1L).setParam(new FieldDef<Tone>("newTone", Tone.class, DataTemplateUtil.getSchema(Tone.class)), Tone.FRIENDLY).build();
    final Map<String, Greeting> results = new ConcurrentHashMap<String, Greeting>();
    final CountDownLatch latch = new CountDownLatch(PARTITION_NUM);
    final List<Throwable> errors = new ArrayList<Throwable>();
    final List<Greeting> responses = new ArrayList<Greeting>();
    Callback<Response<Greeting>> cb = new Callback<Response<Greeting>>() {

        @Override
        public void onError(Throwable e) {
            synchronized (errors) {
                errors.add(e);
            }
            latch.countDown();
        }

        @Override
        public void onSuccess(Response<Greeting> response) {
            results.put(response.getEntity().toString(), response.getEntity());
            synchronized (responses) {
                responses.add(response.getEntity());
            }
            latch.countDown();
        }
    };
    HostSet hostsResult = searchRB.sendRequests(getClient(), request, new RequestContext(), cb);
    List<URI> uris = hostsResult.getAllHosts();
    Assert.assertTrue(uris.containsAll(expectedUris));
    Assert.assertTrue(expectedUris.containsAll(uris));
    latch.await();
    if (!errors.isEmpty()) {
        Assert.fail("I knew it: " + errors.toString());
    }
    Assert.assertEquals(PARTITION_NUM, responses.size());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ArrayList(java.util.ArrayList) URI(java.net.URI) HostSet(com.linkedin.d2.balancer.util.HostSet) AllPartitionsRequestBuilder(com.linkedin.restli.client.AllPartitionsRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Response(com.linkedin.restli.client.Response) FieldDef(com.linkedin.data.template.FieldDef) ActionRequestBuilder(com.linkedin.restli.client.ActionRequestBuilder) Callback(com.linkedin.common.callback.Callback) Tone(com.linkedin.restli.examples.greetings.api.Tone) ConsistentHashKeyMapper(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper) ConsistentHashKeyMapperTest(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapperTest) Test(org.testng.annotations.Test)

Example 35 with ServiceUnavailableException

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

the class TestScatterGather method testBuildSGKVRequests.

public static void testBuildSGKVRequests(int endPointsNum, int partitionNum, RootBuilderWrapper<Long, Greeting> builders) throws URISyntaxException, RestException, ServiceUnavailableException {
    final int NUM_ENDPOINTS = endPointsNum;
    ConsistentHashKeyMapper mapper;
    if (partitionNum > 0) {
        mapper = getKeyToHostMapper(endPointsNum, partitionNum);
    } else {
        mapper = getKeyToHostMapper(endPointsNum);
    }
    ScatterGatherBuilder<Greeting> sg = new ScatterGatherBuilder<Greeting>(mapper);
    final int NUM_IDS = 100;
    Long[] ids = generateIds(NUM_IDS);
    Map<Long, Greeting> updates = generateUpdates(ids);
    testBuildSGGetKVRequests(NUM_ENDPOINTS, sg, ids);
    testBuildSGDeleteRequests(NUM_ENDPOINTS, sg, ids, builders);
    testBuildSGUpdateRequests(NUM_ENDPOINTS, sg, updates, builders);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ConsistentHashKeyMapper(com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper)

Aggregations

URI (java.net.URI)16 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)15 Test (org.testng.annotations.Test)14 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)12 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)12 ServiceUnavailableException (com.linkedin.d2.balancer.ServiceUnavailableException)11 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 DegraderLoadBalancerStrategyFactoryV3 (com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3)9 RequestContext (com.linkedin.r2.message.RequestContext)9 URIRequest (com.linkedin.d2.balancer.util.URIRequest)7 PropertyEventShutdownCallback (com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback)7 Map (java.util.Map)7 None (com.linkedin.common.util.None)6 SimpleLoadBalancerSimulation (com.linkedin.d2.balancer.simulator.SimpleLoadBalancerSimulation)6 LoadBalancerStrategy (com.linkedin.d2.balancer.strategies.LoadBalancerStrategy)6 RandomLoadBalancerStrategyFactory (com.linkedin.d2.balancer.strategies.random.RandomLoadBalancerStrategyFactory)6 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)6 HashSet (java.util.HashSet)6