use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class HealthCheckRequestFactory method buildRestRequest.
public RestRequest buildRestRequest(String method, URI uri) {
RestRequestBuilder requestBuilder = new RestRequestBuilder(uri);
requestBuilder.setMethod(method);
requestBuilder.setHeader("X-RestLi-Protocol-Version", "2.0.0");
return requestBuilder.build();
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class DegraderLoadBalancerTest method testTargetHostHeaderBinding.
@Test
public void testTargetHostHeaderBinding() {
final int NUM_SERVERS = 10;
DegraderLoadBalancerStrategyV3 strategy = getStrategy();
List<TrackerClient> clients = new ArrayList<TrackerClient>(NUM_SERVERS);
for (int ii = 0; ii < NUM_SERVERS; ++ii) {
clients.add(getClient(URI.create("http://server" + ii + ".testing:9876/foobar")));
}
Map<TrackerClient, Integer> serverCounts = new HashMap<TrackerClient, Integer>();
RestRequestBuilder builder = new RestRequestBuilder(URI.create("d2://fooservice"));
final int NUM_REQUESTS = 100;
for (int ii = 0; ii < NUM_REQUESTS; ++ii) {
TrackerClient client = getTrackerClient(strategy, builder.build(), new RequestContext(), 0, clients);
Integer count = serverCounts.get(client);
if (count == null) {
count = 0;
}
serverCounts.put(client, count + 1);
}
//First, check that requests are normally evenly distributed.
Assert.assertEquals(serverCounts.size(), NUM_SERVERS);
serverCounts.clear();
RestRequest request = builder.build();
RequestContext context = new RequestContext();
KeyMapper.TargetHostHints.setRequestContextTargetHost(context, clients.get(0).getUri());
for (int ii = 0; ii < NUM_REQUESTS; ++ii) {
TrackerClient client = getTrackerClient(strategy, request, context, 0, clients);
Integer count = serverCounts.get(client);
if (count == null) {
count = 0;
}
serverCounts.put(client, count + 1);
}
Assert.assertEquals(serverCounts.size(), 1);
Assert.assertEquals(serverCounts.keySet().iterator().next(), clients.get(0));
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class LoadBalancerEchoClient method startClient.
public void startClient() throws URISyntaxException, InterruptedException, ExecutionException, IOException, PropertyStoreException {
DynamicClient client = new DynamicClient(getLoadBalancer(_hostPort), null);
for (; ; ) {
int index = 0;
if (_services.length > 1) {
index = _random.nextInt(_services.length);
}
String service = _services[index];
URI uri = URI.create("d2://" + service);
RestRequest req = new RestRequestBuilder(uri).setEntity("hi there".getBytes("UTF-8")).build();
try {
Future<RestResponse> response = client.restRequest(req);
String responseString = response.get().getEntity().asString("UTF-8");
System.err.println(uri + " response: " + responseString);
} catch (ExecutionException e) {
System.err.println("future.get() failed for " + uri + ": " + e);
}
Thread.sleep(_random.nextInt(1000));
}
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class EmailClientExample method sendTraffic.
private static void sendTraffic(Map<String, Long> trafficProportion, D2Client d2Client) throws Exception {
for (Map.Entry<String, Long> proportion : trafficProportion.entrySet()) {
long queryPerSecond = proportion.getValue();
String serviceName = proportion.getKey();
Random random = new Random();
for (int i = 0; i < queryPerSecond; i++) {
final URI uri = new URI("d2://" + serviceName + "?user=" + random.nextInt());
RestRequestBuilder requestBuilder = new RestRequestBuilder(uri).setMethod("get");
RestRequest request = requestBuilder.build();
//we don't care about the result from the server after all,
//you can see the traffic hits the echo server from stdout
d2Client.restRequest(request, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
System.err.println("URI = " + uri.toString() + " didn't get any response");
}
@Override
public void onSuccess(RestResponse result) {
System.out.println("URI = " + uri.toString() + " was served by " + result.getEntity().asString("UTF-8"));
}
});
}
}
}
use of com.linkedin.r2.message.rest.RestRequest in project rest.li by linkedin.
the class ProfileClientExample method sendTraffic.
private static void sendTraffic(Map<String, Map<String, Long>> trafficProportion, D2Client d2Client, Long delay) throws Exception {
for (Map.Entry<String, Map<String, Long>> d2Service : trafficProportion.entrySet()) {
for (Map.Entry<String, Long> partition : d2Service.getValue().entrySet()) {
final URI uri = new URI("d2://" + d2Service.getKey() + "?partitionId=" + partition.getKey());
RestRequestBuilder requestBuilder = new RestRequestBuilder(uri).setMethod("get");
if (delay != null) {
requestBuilder.setHeader("delay", delay.toString());
}
RestRequest request = requestBuilder.build();
Long queryPerSecond = partition.getValue();
for (int i = 0; i < queryPerSecond; i++) {
//we don't care about the result from the server after all,
//you can see the traffic hits the echo server from stdout
d2Client.restRequest(request, new Callback<RestResponse>() {
@Override
public void onError(Throwable e) {
System.err.println("URI = " + uri.toString() + " didn't get any response");
}
@Override
public void onSuccess(RestResponse result) {
System.out.println("URI = " + uri.toString() + " was served by " + result.getEntity().asString("UTF-8"));
}
});
}
}
}
}
Aggregations