Search in sources :

Example 16 with Server

use of com.linkedin.r2.transport.common.Server in project rest.li by linkedin.

the class LoadBalancerEchoServer method startServer.

public void startServer() throws IOException, InterruptedException, URISyntaxException {
    final RestDispatcher restDispatcher = new RestDispatcher();
    final TransportDispatcherBuilder dispatcherBuilder = new TransportDispatcherBuilder();
    for (String validPath : _validPaths) {
        dispatcherBuilder.addRestHandler(URI.create(validPath), restDispatcher);
    }
    final TransportDispatcher dispatcher = dispatcherBuilder.build();
    // start the server
    if (_scheme.equals("http")) {
        _server = getHttpServer(dispatcher);
    }
    _server.start();
}
Also used : TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder)

Example 17 with Server

use of com.linkedin.r2.transport.common.Server 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 18 with Server

use of com.linkedin.r2.transport.common.Server 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 19 with Server

use of com.linkedin.r2.transport.common.Server in project rest.li by linkedin.

the class AbstractClient method restRequest.

@Override
public void restRequest(RestRequest request, RequestContext requestContext, Callback<RestResponse> callback) {
    StreamRequest streamRequest = Messages.toStreamRequest(request);
    // IS_FULL_REQUEST flag, if set true, would result in the request being sent without using chunked transfer encoding
    // This is needed as the legacy R2 server (before 2.8.0) does not support chunked transfer encoding.
    requestContext.putLocalAttr(R2Constants.IS_FULL_REQUEST, true);
    // here we add back the content-length header for the response because some client code depends on this header
    streamRequest(streamRequest, requestContext, Messages.toStreamCallback(callback, true));
}
Also used : StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Example 20 with Server

use of com.linkedin.r2.transport.common.Server in project rest.li by linkedin.

the class TestServerTimeoutAsyncEvent method testFilterThrowButShouldNotTimeout.

@Test
public void testFilterThrowButShouldNotTimeout() throws Exception {
    RestRequest request = new RestRequestBuilder(getHttpUri(BUGGY_FILTER_URI)).setEntity(new byte[10240]).build();
    _client.restRequest(request);
    Future<RestResponse> futureResponse = _client.restRequest(request);
    // if server times out, our second request would fail with TimeoutException because it's blocked by first one
    try {
        futureResponse.get(ASYNC_EVENT_TIMEOUT / 2, TimeUnit.MILLISECONDS);
        Assert.fail("Should fail with ExecutionException");
    } catch (ExecutionException ex) {
        Assert.assertTrue(ex.getCause() instanceof RestException);
        RestException restException = (RestException) ex.getCause();
        Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("Buggy filter throws."));
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Aggregations

Test (org.testng.annotations.Test)82 RestRequest (com.linkedin.r2.message.rest.RestRequest)52 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)50 RequestContext (com.linkedin.r2.message.RequestContext)49 URI (java.net.URI)43 RestResponse (com.linkedin.r2.message.rest.RestResponse)41 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)32 FutureCallback (com.linkedin.common.callback.FutureCallback)30 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)30 CountDownLatch (java.util.concurrent.CountDownLatch)24 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)23 RestException (com.linkedin.r2.message.rest.RestException)21 ExecutionException (java.util.concurrent.ExecutionException)21 None (com.linkedin.common.util.None)20 Server (org.eclipse.jetty.server.Server)20 ByteString (com.linkedin.data.ByteString)19 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)19 HashMap (java.util.HashMap)15 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)13 ArrayList (java.util.ArrayList)13