use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.
the class TestRouteLookupClient method testRouteLookupClientCallback.
@Test
public void testRouteLookupClientCallback() throws InterruptedException, ExecutionException, TimeoutException {
RouteLookup routeLookup = new SimpleTestRouteLookup();
final D2Client d2Client = new D2ClientBuilder().setZkHosts("localhost:2121").build();
d2Client.start(new FutureCallback<None>());
RouteLookupClient routeLookupClient = new RouteLookupClient(d2Client, routeLookup, "WestCoast");
RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("d2://simple_uri")).build();
FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
routeLookupClient.restRequest(dummyRestRequest, futureCallback, "5555");
try {
RestResponse response = futureCallback.get(10, TimeUnit.SECONDS);
Assert.fail("Unexpected success, request should have thrown a ServiceUnavailableException");
} catch (Exception e) {
String message = e.getMessage();
if (!message.contains("_serviceName=simple_uriWestCoast5555Foo")) {
Assert.fail("request was not rewritten to point at the d2 service simple_uriWestCoast5555Foo");
}
}
}
use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.
the class ConsistentHashKeyMapper method doMapKeys.
private <K> MapKeyResult<URI, K> doMapKeys(Ring<URI> ring, Iterable<K> keys) throws ServiceUnavailableException {
String[] keyTokens = new String[1];
List<MapKeyResult.UnmappedKey<K>> unmappedKeys = new ArrayList<MapKeyResult.UnmappedKey<K>>();
Map<URI, Collection<K>> result = new HashMap<URI, Collection<K>>();
for (K key : keys) {
keyTokens[0] = key.toString();
int hashCode = _hashFunction.hash(keyTokens);
URI uri = ring.get(hashCode);
if (uri == null) {
unmappedKeys.add(new MapKeyResult.UnmappedKey<K>(key, MapKeyResult.ErrorType.NO_HOST_AVAILABLE_IN_PARTITION));
continue;
}
Collection<K> collection = result.get(uri);
if (collection == null) {
collection = new ArrayList<K>();
result.put(uri, collection);
}
collection.add(key);
}
return new MapKeyResult<URI, K>(result, unmappedKeys);
}
use of com.linkedin.d2.balancer.ServiceUnavailableException 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();
}
use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.
the class SimpleLoadBalancerTest method testLoadBalancerSmoke.
@Test(groups = { "small", "back-end" })
public void testLoadBalancerSmoke() throws URISyntaxException, ServiceUnavailableException, InterruptedException, ExecutionException {
for (int tryAgain = 0; tryAgain < 1000; ++tryAgain) {
Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
List<String> prioritizedSchemes = new ArrayList<String>();
MockStore<ServiceProperties> serviceRegistry = new MockStore<ServiceProperties>();
MockStore<ClusterProperties> clusterRegistry = new MockStore<ClusterProperties>();
MockStore<UriProperties> uriRegistry = new MockStore<UriProperties>();
ScheduledExecutorService executorService = new SynchronousExecutorService();
//loadBalancerStrategyFactories.put("rr", new RandomLoadBalancerStrategyFactory());
loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
// PrpcClientFactory();
// new
clientFactories.put("http", new DoNothingClientFactory());
// HttpClientFactory();
SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriRegistry, clusterRegistry, serviceRegistry, clientFactories, loadBalancerStrategyFactories);
SimpleLoadBalancer loadBalancer = new SimpleLoadBalancer(state, 5, TimeUnit.SECONDS);
FutureCallback<None> balancerCallback = new FutureCallback<None>();
loadBalancer.start(balancerCallback);
balancerCallback.get();
URI uri1 = URI.create("http://test.qa1.com:1234");
URI uri2 = URI.create("http://test.qa2.com:2345");
URI uri3 = URI.create("http://test.qa3.com:6789");
Map<Integer, PartitionData> partitionData = new HashMap<Integer, PartitionData>(1);
partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1d));
Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<URI, Map<Integer, PartitionData>>(3);
uriData.put(uri1, partitionData);
uriData.put(uri2, partitionData);
uriData.put(uri3, partitionData);
prioritizedSchemes.add("http");
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 expectedUri1 = URI.create("http://test.qa1.com:1234/foo");
URI expectedUri2 = URI.create("http://test.qa2.com:2345/foo");
URI expectedUri3 = URI.create("http://test.qa3.com:6789/foo");
Set<URI> expectedUris = new HashSet<URI>();
expectedUris.add(expectedUri1);
expectedUris.add(expectedUri2);
expectedUris.add(expectedUri3);
for (int i = 0; i < 100; ++i) {
RewriteClient client = (RewriteClient) loadBalancer.getClient(new URIRequest("d2://foo/52"), new RequestContext());
assertTrue(expectedUris.contains(client.getUri()));
assertEquals(client.getUri().getScheme(), "http");
}
final CountDownLatch latch = new CountDownLatch(1);
PropertyEventShutdownCallback callback = new PropertyEventShutdownCallback() {
@Override
public void done() {
latch.countDown();
}
};
state.shutdown(callback);
if (!latch.await(60, TimeUnit.SECONDS)) {
fail("unable to shutdown state");
}
executorService.shutdownNow();
assertTrue(executorService.isShutdown(), "ExecutorService should have shut down!");
}
}
use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.
the class SimpleLoadBalancerTest method testLoadBalancerSimulationDegrader.
@Test(groups = { "medium", "back-end" })
public void testLoadBalancerSimulationDegrader() throws URISyntaxException, IOException, ServiceUnavailableException, InterruptedException {
SimpleLoadBalancerSimulation simulator = new SimpleLoadBalancerSimulation(new DegraderLoadBalancerStrategyFactoryV3());
simulator.simulateMultithreaded(1, 1000, 20);
simulator.reset();
simulator.simulateMultithreaded(50, 10000, 20);
simulator.reset();
}
Aggregations