Search in sources :

Example 56 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestBackupRequestsClient method testStatsConsumerLatencyUpdate.

// @Test - Disabled due to flakiness. See SI-3077 to track and resolve this.
public void testStatsConsumerLatencyUpdate() throws Exception {
    AtomicReference<ServiceProperties> serviceProperties = new AtomicReference<>();
    TestBackupRequestsStrategyStatsConsumer statsConsumer = new TestBackupRequestsStrategyStatsConsumer();
    serviceProperties.set(createServiceProperties(null));
    BackupRequestsClient client = createClient(serviceProperties::get, statsConsumer, new ConstantResponseTimeDistribution(1, TimeUnit.NANOSECONDS), false);
    URI uri = URI.create("d2://testService");
    RestRequest restRequest = new RestRequestBuilder(uri).setEntity(CONTENT).build();
    RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(R2Constants.OPERATION, "get");
    Future<RestResponse> response = client.restRequest(restRequest, requestContext);
    assertEquals(response.get().getStatus(), 200);
    List<StatsConsumerEvent> events = statsConsumer.getEvents();
    assertEquals(events.size(), 0);
    for (int i = 0; i < Short.MAX_VALUE * 4; i++) {
        requestContext = new RequestContext();
        requestContext.putLocalAttr(R2Constants.OPERATION, "get");
        response = client.restRequest(restRequest, requestContext);
        assertEquals(response.get().getStatus(), 200);
    }
    assertEquals(statsConsumer.getLatencyWithBackup().size(), 0);
    assertEquals(statsConsumer.getLatencyWithoutBackup().size(), 0);
    serviceProperties.set(createServiceProperties(Arrays.asList(createBackupRequestsConfiguration(5, "get"))));
    while (statsConsumer.getLatencyWithoutBackup().size() < 1) {
        requestContext = new RequestContext();
        requestContext.putLocalAttr(R2Constants.OPERATION, "get");
        response = client.restRequest(restRequest, requestContext);
        assertEquals(response.get().getStatus(), 200);
    }
    assertEquals(statsConsumer.getLatencyWithoutBackup().size(), 1);
    assertEquals(statsConsumer.getLatencyWithBackup().size(), 1);
    // allowing 1% imprecision
    long expected = statsConsumer.getLatencyWithoutBackup().get(0).getTotalCount();
    long actual = statsConsumer.getLatencyWithBackup().get(0).getTotalCount();
    assertTrue(actual > expected * .99 && actual < expected * 1.01, "Expected: " + expected + "+-" + (expected * .01) + ", but actual: " + actual);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) ConstantResponseTimeDistribution(com.linkedin.d2.backuprequests.ConstantResponseTimeDistribution) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext)

Example 57 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder 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)

Example 58 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class ExampleD2Client method sendTraffic.

private static void sendTraffic(Map<String, Long> trafficProportion, D2Client d2Client) throws URISyntaxException {
    for (Map.Entry<String, Long> entry : trafficProportion.entrySet()) {
        URI uri = new URI("d2://" + entry.getKey());
        RestRequest request = new RestRequestBuilder(uri).setMethod("get").build();
        for (long i = 0; i < entry.getValue(); 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);
        }
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Map(java.util.Map) URI(java.net.URI)

Example 59 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder 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 60 with RestRequestBuilder

use of com.linkedin.r2.message.rest.RestRequestBuilder in project rest.li by linkedin.

the class TestChannelPoolBehavior method testChannelReuse.

@Test
public void testChannelReuse() throws Exception {
    _client2.streamRequest(new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, NOT_FOUND_URI)).build(EntityStreams.newEntityStream(new SlowWriter())), new Callback<StreamResponse>() {

        @Override
        public void onError(Throwable e) {
            if (e instanceof StreamException) {
                StreamException streamException = (StreamException) e;
                streamException.getResponse().getEntityStream().setReader(new CancelingReader());
            }
            throw new RuntimeException(e);
        }

        @Override
        public void onSuccess(StreamResponse result) {
            result.getEntityStream().setReader(new DrainReader());
        }
    });
    Future<RestResponse> responseFuture = _client2.restRequest(new RestRequestBuilder(Bootstrap.createHttpURI(PORT, NORMAL_URI)).build());
    RestResponse response = responseFuture.get(WRITER_DELAY * 1000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(response.getStatus(), RestStatus.OK);
}
Also used : CancelingReader(com.linkedin.r2.message.stream.entitystream.CancelingReader) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamException(com.linkedin.r2.message.stream.StreamException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Aggregations

RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)336 RestRequest (com.linkedin.r2.message.rest.RestRequest)290 Test (org.testng.annotations.Test)267 URI (java.net.URI)220 RestResponse (com.linkedin.r2.message.rest.RestResponse)192 RequestContext (com.linkedin.r2.message.RequestContext)155 ExecutionException (java.util.concurrent.ExecutionException)55 ByteString (com.linkedin.data.ByteString)46 FutureCallback (com.linkedin.common.callback.FutureCallback)43 RestException (com.linkedin.r2.message.rest.RestException)42 HashMap (java.util.HashMap)36 TimeoutException (java.util.concurrent.TimeoutException)29 AfterTest (org.testng.annotations.AfterTest)26 BeforeTest (org.testng.annotations.BeforeTest)26 Callback (com.linkedin.common.callback.Callback)25 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)25 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)21 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)20