use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.
the class SimpleLoadBalancer method getClient.
/**
* Given a Request, returns a TransportClient that can handle requests for the Request.
*
*
* @param request
* A request whose URI is a URL of the format "d2://>servicename</optional/path".
* @param requestContext context for this request
* @return A client that can be called to retrieve data for the URN.
* @throws ServiceUnavailableException
* If the load balancer can't figure out how to reach a service for the given
* URN, an ServiceUnavailableException will be thrown.
*/
@Override
public TransportClient getClient(Request request, RequestContext requestContext) throws ServiceUnavailableException {
TransportClient client;
URI uri = request.getURI();
debug(_log, "get client for uri: ", uri);
ServiceProperties service = listenToServiceAndCluster(uri);
String serviceName = service.getServiceName();
String clusterName = service.getClusterName();
ClusterProperties cluster = getClusterProperties(serviceName, clusterName);
// Check if we want to override the service URL and bypass choosing among the existing
// tracker clients. This is useful when the service we want is not announcing itself to
// the cluster, ie a private service for a set of clients.
URI targetService = LoadBalancerUtil.TargetHints.getRequestContextTargetService(requestContext);
if (targetService == null) {
LoadBalancerStateItem<UriProperties> uriItem = getUriItem(serviceName, clusterName, cluster);
UriProperties uris = uriItem.getProperty();
List<LoadBalancerState.SchemeStrategyPair> orderedStrategies = _state.getStrategiesForService(serviceName, service.getPrioritizedSchemes());
TrackerClient trackerClient = chooseTrackerClient(request, requestContext, serviceName, clusterName, cluster, uriItem, uris, orderedStrategies, service);
String clusterAndServiceUriString = trackerClient.getUri() + service.getPath();
client = new RewriteClient(serviceName, URI.create(clusterAndServiceUriString), trackerClient);
_serviceAvailableStats.inc();
} else {
_log.debug("service hint found, using generic client for target: {}", targetService);
TransportClient transportClient = _state.getClient(serviceName, targetService.getScheme());
client = new RewriteClient(serviceName, targetService, transportClient);
}
return client;
}
use of com.linkedin.r2.message.RequestContext 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.RequestContext 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.RequestContext in project rest.li by linkedin.
the class RewriteClientTestStreamRequest method testWithEverything.
@Test
public void testWithEverything() {
URI uri = URI.create("http://username:password@test.linkedin.com:9876/test");
String serviceName = "HistoryService";
TestClient wrappedClient = new TestClient();
RewriteClient client = new RewriteClient(serviceName, uri, wrappedClient);
assertEquals(client.getUri(), uri);
assertEquals(client.getServiceName(), serviceName);
assertEquals(client.getWrappedClient(), wrappedClient);
StreamRequest streamRequest = getRequest("d2://HistoryService/getCube?bar=baz#fragId");
Map<String, String> restWireAttrs = new HashMap<String, String>();
TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<StreamResponse>();
client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
assertFalse(restCallback.response.hasError());
assertEquals(wrappedClient.streamRequest.getHeaders(), streamRequest.getHeaders());
assertEquals(wrappedClient.streamRequest.getMethod(), streamRequest.getMethod());
assertEquals(wrappedClient.streamRequest.getURI(), URI.create("http://username:password@test.linkedin.com:9876/test/getCube?bar=baz#fragId"));
}
use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.
the class RewriteClientTestStreamRequest method testEscapingHelper.
private void testEscapingHelper(String hostUri, String serviceName, String path) {
URI uri = URI.create(hostUri);
TestClient wrappedClient = new TestClient();
RewriteClient client = new RewriteClient(serviceName, uri, wrappedClient);
assertEquals(client.getUri(), uri);
assertEquals(client.getServiceName(), serviceName);
assertEquals(client.getWrappedClient(), wrappedClient);
StreamRequest streamRequest = getRequest("d2://" + serviceName + path);
Map<String, String> restWireAttrs = new HashMap<String, String>();
TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<StreamResponse>();
client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
assertFalse(restCallback.response.hasError());
assertEquals(wrappedClient.streamRequest.getHeaders(), streamRequest.getHeaders());
assertEquals(wrappedClient.streamRequest.getMethod(), streamRequest.getMethod());
assertEquals(wrappedClient.streamRequest.getURI(), URI.create(hostUri + path));
}
Aggregations