use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class RouteLookupClient method restRequest.
@Override
public void restRequest(final RestRequest request, final RequestContext requestContext, final Callback<RestResponse> callback, String routeKey) {
String originalServiceName = request.getURI().getAuthority();
Callback<String> routeLookupCallback = new Callback<String>() {
@Override
public void onError(Throwable e) {
callback.onError(e);
}
@Override
public void onSuccess(String resultServiceName) {
RestRequest resultRequest = createNewRequestWithNewServiceName(request, resultServiceName);
_client.restRequest(resultRequest, requestContext, callback);
}
};
_routeLookup.run(originalServiceName, _routingGroup, routeKey, routeLookupCallback);
}
use of com.linkedin.r2.message.rest.RestRequest 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.r2.message.rest.RestRequest in project rest.li by linkedin.
the class RouteLookupClient method restRequest.
@Override
public Future<RestResponse> restRequest(final RestRequest request, final RequestContext requestContext, String routekey) {
final FutureCallback<String> futureCallback = new FutureCallback<String>();
String originalServiceName = LoadBalancerUtil.getServiceNameFromUri(request.getURI());
String resultServiceName;
_routeLookup.run(originalServiceName, _routingGroup, routekey, futureCallback);
try {
resultServiceName = futureCallback.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
RestRequest resultRequest = createNewRequestWithNewServiceName(request, resultServiceName);
Future<RestResponse> resultFuture = _client.restRequest(resultRequest, requestContext);
return resultFuture;
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class RouteLookupClient method createNewRequestWithNewServiceName.
/**
* Creates a new request identical to the original request, but changes the d2 service name to
* the one passed in.
* @param origRequest original request to copy
* @param newServiceName the new service name to rewrite the request with
* @return a new RestRequest
*/
private static RestRequest createNewRequestWithNewServiceName(RestRequest origRequest, String newServiceName) {
RestRequestBuilder modifiedBuilder = origRequest.builder();
URI originalURI = modifiedBuilder.getURI();
if (!("d2".equals(originalURI.getScheme()))) {
throw new IllegalArgumentException("Unsupported scheme in URI: " + originalURI);
}
UriBuilder modifiedUriBuilder = UriBuilder.fromUri(originalURI);
modifiedUriBuilder.host(newServiceName);
URI resultUri = modifiedUriBuilder.build();
modifiedBuilder.setURI(resultUri);
RestRequest resultRequest = modifiedBuilder.build();
return resultRequest;
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class TrackerClientTest method testCallTrackingRestRequest.
@Test
public void testCallTrackingRestRequest() throws Exception {
URI uri = URI.create("http://test.qa.com:1234/foo");
SettableClock clock = new SettableClock();
AtomicInteger action = new AtomicInteger(0);
TransportClient tc = new TransportClient() {
@Override
public void restRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<RestResponse> callback) {
clock.addDuration(5);
switch(action.get()) {
// success
case 0:
callback.onResponse(TransportResponseImpl.success(RestResponse.NO_RESPONSE));
break;
// fail with rest exception
case 1:
callback.onResponse(TransportResponseImpl.error(RestException.forError(500, "rest exception")));
break;
// fail with timeout exception
case 2:
callback.onResponse(TransportResponseImpl.error(new RemoteInvocationException(new TimeoutException())));
break;
// fail with other exception
default:
callback.onResponse(TransportResponseImpl.error(new RuntimeException()));
break;
}
}
@Override
public void shutdown(Callback<None> callback) {
}
};
TrackerClient client = createTrackerClient(tc, clock, uri);
CallTracker callTracker = client.getCallTracker();
CallTracker.CallStats stats;
DegraderControl degraderControl = client.getDegraderControl(DefaultPartitionAccessor.DEFAULT_PARTITION_ID);
client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
clock.addDuration(5000);
stats = callTracker.getCallStats();
Assert.assertEquals(stats.getCallCount(), 1);
Assert.assertEquals(stats.getErrorCount(), 0);
Assert.assertEquals(stats.getCallCountTotal(), 1);
Assert.assertEquals(stats.getErrorCountTotal(), 0);
Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.0, 0.001);
action.set(1);
client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
clock.addDuration(5000);
stats = callTracker.getCallStats();
Assert.assertEquals(stats.getCallCount(), 1);
Assert.assertEquals(stats.getErrorCount(), 1);
Assert.assertEquals(stats.getCallCountTotal(), 2);
Assert.assertEquals(stats.getErrorCountTotal(), 1);
Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.2, 0.001);
action.set(2);
client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
clock.addDuration(5000);
stats = callTracker.getCallStats();
Assert.assertEquals(stats.getCallCount(), 1);
Assert.assertEquals(stats.getErrorCount(), 1);
Assert.assertEquals(stats.getCallCountTotal(), 3);
Assert.assertEquals(stats.getErrorCountTotal(), 2);
Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.4, 0.001);
action.set(3);
client.restRequest(new RestRequestBuilder(uri).build(), new RequestContext(), new HashMap<>(), new TestTransportCallback<>());
clock.addDuration(5000);
stats = callTracker.getCallStats();
Assert.assertEquals(stats.getCallCount(), 1);
Assert.assertEquals(stats.getErrorCount(), 1);
Assert.assertEquals(stats.getCallCountTotal(), 4);
Assert.assertEquals(stats.getErrorCountTotal(), 3);
Assert.assertEquals(degraderControl.getCurrentComputedDropRate(), 0.2, 0.001);
}
Aggregations