Search in sources :

Example 46 with TransportCallback

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

the class TransportHealthCheck method checkHealth.

@Override
public void checkHealth(Callback<None> callback) {
    final long startTime = _clock.currentTimeMillis();
    TransportCallback<RestResponse> transportCallback = response -> {
        long delay = _clock.currentTimeMillis() - startTime;
        if (response.hasError()) {
            _log.debug("checkHealth: error response for request ({}): {}", _restRequest.getURI(), response.getError());
            callback.onError(new Exception("Error from " + _restRequest.getURI() + " : " + response.getError()));
        } else if (delay > _responseTimeThreshold) {
            _log.debug("checkHealth: return delay ({}ms) longer than threshold for request {}", delay, _restRequest.getURI());
            callback.onError(new TimeoutException("HealthCheck Timeout: " + delay + "ms for " + _restRequest.getURI()));
        } else if (!_healthCheckResponseValidator.validateResponse(response.getResponse())) {
            _log.error("checkHealth: response validating error for request ({}): {}", _restRequest.getURI(), response);
            callback.onError(new Throwable("HealthCheck Response Error"));
        } else {
            _log.debug("checkHealth successful for client {}", _clientToCheck);
            callback.onSuccess(None.none());
        }
    };
    _clientToCheck.restRequest(_restRequest, _requestContext, _wireAttrs, transportCallback);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) Logger(org.slf4j.Logger) RequestContext(com.linkedin.r2.message.RequestContext) None(com.linkedin.common.util.None) Clock(com.linkedin.util.clock.Clock) Map(java.util.Map) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) Callback(com.linkedin.common.callback.Callback) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponse(com.linkedin.r2.message.rest.RestResponse) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 47 with TransportCallback

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

the class TestHttpBridge method testStreamToHttpErrorMessage.

@Test
public void testStreamToHttpErrorMessage() throws TimeoutException, InterruptedException {
    URI uri = URI.create("http://some.host/thisShouldAppearInTheErrorMessage");
    RestRequest r = new RestRequestBuilder(uri).build();
    FutureCallback<StreamResponse> futureCallback = new FutureCallback<StreamResponse>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(futureCallback);
    TransportCallback<StreamResponse> bridgeCallback = HttpBridge.streamToHttpCallback(callback, Messages.toStreamRequest(r));
    bridgeCallback.onResponse(TransportResponseImpl.<StreamResponse>error(new Exception()));
    try {
        futureCallback.get(30, TimeUnit.SECONDS);
        Assert.fail("get should have thrown exception");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause().getMessage().contains(uri.toString()));
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestRequest(com.linkedin.r2.message.rest.RestRequest) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) Test(org.testng.annotations.Test)

Example 48 with TransportCallback

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

the class TestHttpBridge method testRestToHttpErrorMessage.

@Test
public void testRestToHttpErrorMessage() throws TimeoutException, InterruptedException {
    URI uri = URI.create("http://some.host/thisShouldAppearInTheErrorMessage");
    RestRequest r = new RestRequestBuilder(uri).build();
    FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
    TransportCallback<RestResponse> callback = new TransportCallbackAdapter<RestResponse>(futureCallback);
    TransportCallback<RestResponse> bridgeCallback = HttpBridge.restToHttpCallback(callback, r);
    bridgeCallback.onResponse(TransportResponseImpl.<RestResponse>error(new Exception()));
    try {
        futureCallback.get(30, TimeUnit.SECONDS);
        Assert.fail("get should have thrown exception");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause().getMessage().contains(uri.toString()));
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) Test(org.testng.annotations.Test)

Example 49 with TransportCallback

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

the class TestMessages method testToRestTransportCallbackSuccess.

@Test
public void testToRestTransportCallbackSuccess() {
    TransportCallback<StreamResponse> streamCallback = response -> {
        Assert.assertFalse(response.hasError());
        Assert.assertNotNull(response.getResponse());
        response.getResponse().getEntityStream().setReader(ENTITY_VERIFIER);
        Assert.assertNotNull(response.getWireAttributes());
        Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
    };
    TransportCallback<RestResponse> restCallback = Messages.toRestTransportCallback(streamCallback);
    RestResponseBuilder builder = new RestResponseBuilder();
    builder.setEntity(DATA);
    RestResponse restResponse = builder.build();
    restCallback.onResponse(TransportResponseImpl.success(restResponse, WIRE_ATTR));
}
Also used : ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Callback(com.linkedin.common.callback.Callback) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) Messages(com.linkedin.r2.message.Messages) RestResponse(com.linkedin.r2.message.rest.RestResponse) EntityStreams(com.linkedin.r2.message.stream.entitystream.EntityStreams) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) Assert(org.testng.Assert) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Map(java.util.Map) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) Test(org.testng.annotations.Test)

Example 50 with TransportCallback

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

the class TestMessages method testToRestTransportCallbackRestException.

@Test
public void testToRestTransportCallbackRestException() {
    TransportCallback<StreamResponse> streamCallback = response -> {
        Assert.assertTrue(response.hasError());
        Assert.assertNotNull(response.getError());
        Assert.assertTrue(response.getError() instanceof StreamException);
        Assert.assertNotNull(response.getWireAttributes());
        Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
    };
    TransportCallback<RestResponse> restCallback = Messages.toRestTransportCallback(streamCallback);
    RestResponseBuilder builder = new RestResponseBuilder();
    builder.setEntity(DATA);
    RestResponse restResponse = builder.build();
    restCallback.onResponse(TransportResponseImpl.error(new RestException(restResponse, new IllegalStateException()), WIRE_ATTR));
}
Also used : ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Callback(com.linkedin.common.callback.Callback) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) Messages(com.linkedin.r2.message.Messages) RestResponse(com.linkedin.r2.message.rest.RestResponse) EntityStreams(com.linkedin.r2.message.stream.entitystream.EntityStreams) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) Assert(org.testng.Assert) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Map(java.util.Map) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException) StreamException(com.linkedin.r2.message.stream.StreamException) Test(org.testng.annotations.Test)

Aggregations

RestRequest (com.linkedin.r2.message.rest.RestRequest)32 RequestContext (com.linkedin.r2.message.RequestContext)30 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)29 RestResponse (com.linkedin.r2.message.rest.RestResponse)26 FutureCallback (com.linkedin.common.callback.FutureCallback)22 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)22 Test (org.testng.annotations.Test)22 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)21 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)19 ByteString (com.linkedin.data.ByteString)16 ExecutionException (java.util.concurrent.ExecutionException)16 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)14 TimeoutException (java.util.concurrent.TimeoutException)14 HashMap (java.util.HashMap)13 URI (java.net.URI)12 Map (java.util.Map)12 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)11 RestException (com.linkedin.r2.message.rest.RestException)10 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)10 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)9