Search in sources :

Example 11 with RestRequest

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();
}
Also used : RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder)

Example 12 with RestRequest

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));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) TrackerClient(com.linkedin.d2.balancer.clients.TrackerClient) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test) TrackerClientTest(com.linkedin.d2.balancer.clients.TrackerClientTest)

Example 13 with RestRequest

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));
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) DynamicClient(com.linkedin.d2.balancer.clients.DynamicClient) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI)

Example 14 with RestRequest

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"));
                }
            });
        }
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) Random(java.util.Random) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Map(java.util.Map)

Example 15 with RestRequest

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"));
                    }
                });
            }
        }
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Map(java.util.Map)

Aggregations

RestRequest (com.linkedin.r2.message.rest.RestRequest)293 Test (org.testng.annotations.Test)243 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)174 RequestContext (com.linkedin.r2.message.RequestContext)154 RestResponse (com.linkedin.r2.message.rest.RestResponse)147 URI (java.net.URI)124 ByteString (com.linkedin.data.ByteString)65 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)62 FutureCallback (com.linkedin.common.callback.FutureCallback)50 RestException (com.linkedin.r2.message.rest.RestException)49 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)44 ExecutionException (java.util.concurrent.ExecutionException)43 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)38 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)36 HashMap (java.util.HashMap)35 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)33 Map (java.util.Map)28 Callback (com.linkedin.common.callback.Callback)27 URISyntaxException (java.net.URISyntaxException)25 ResourceContext (com.linkedin.restli.server.ResourceContext)24