Search in sources :

Example 1 with DisruptFilter

use of com.linkedin.r2.disruptor.DisruptFilter in project rest.li by linkedin.

the class TestDisruptFilter method testExecutorRejectExecution.

@Test
public void testExecutorRejectExecution() throws Exception {
    final AtomicBoolean success = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    ExecutorService rejectedExecutor = EasyMock.createStrictMock(ExecutorService.class);
    rejectedExecutor.execute(EasyMock.anyObject(Runnable.class));
    EasyMock.expectLastCall().andAnswer(() -> {
        success.set(true);
        latch.countDown();
        throw new RejectedExecutionException();
    });
    EasyMock.replay(rejectedExecutor);
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.error(REQUEST_LATENCY));
    final DisruptFilter filter = new DisruptFilter(_scheduler, rejectedExecutor, REQUEST_TIMEOUT);
    final NextFilter<StreamRequest, StreamResponse> next = new NextFilter<StreamRequest, StreamResponse>() {

        @Override
        public void onRequest(StreamRequest restRequest, RequestContext requestContext, Map<String, String> wireAttrs) {
            success.set(false);
            latch.countDown();
        }

        @Override
        public void onResponse(StreamResponse restResponse, RequestContext requestContext, Map<String, String> wireAttrs) {
            success.set(false);
            latch.countDown();
        }

        @Override
        public void onError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs) {
            success.set(false);
            latch.countDown();
        }
    };
    filter.onStreamRequest(new StreamRequestBuilder(new URI(URI)).build(EntityStreams.emptyStream()), requestContext, Collections.emptyMap(), next);
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Missing NextFilter invocation");
    Assert.assertTrue(success.get(), "Unexpected method invocation");
    EasyMock.verify(rejectedExecutor);
    EasyMock.reset(rejectedExecutor);
}
Also used : NextFilter(com.linkedin.r2.filter.NextFilter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 2 with DisruptFilter

use of com.linkedin.r2.disruptor.DisruptFilter in project rest.li by linkedin.

the class TestDisruptFilter method testStreamErrorDisrupt.

@Test
public void testStreamErrorDisrupt() throws Exception {
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.error(REQUEST_LATENCY));
    final DisruptFilter filter = new DisruptFilter(_scheduler, _executor, REQUEST_TIMEOUT);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    final NextFilter<StreamRequest, StreamResponse> next = new NextFilter<StreamRequest, StreamResponse>() {

        @Override
        public void onRequest(StreamRequest restRequest, RequestContext requestContext, Map<String, String> wireAttrs) {
            latch.countDown();
        }

        @Override
        public void onResponse(StreamResponse restResponse, RequestContext requestContext, Map<String, String> wireAttrs) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs) {
            success.set(ex instanceof DisruptedException);
            latch.countDown();
        }
    };
    filter.onStreamRequest(new StreamRequestBuilder(new URI(URI)).build(EntityStreams.emptyStream()), requestContext, Collections.emptyMap(), next);
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Missing NextFilter invocation");
    Assert.assertTrue(success.get(), "Unexpected method invocation");
}
Also used : NextFilter(com.linkedin.r2.filter.NextFilter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 3 with DisruptFilter

use of com.linkedin.r2.disruptor.DisruptFilter in project rest.li by linkedin.

the class TestDisruptFilter method testRestErrorDisrupt.

@Test
public void testRestErrorDisrupt() throws Exception {
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.error(REQUEST_LATENCY));
    final DisruptFilter filter = new DisruptFilter(_scheduler, _executor, REQUEST_TIMEOUT);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    final NextFilter<RestRequest, RestResponse> next = new NextFilter<RestRequest, RestResponse>() {

        @Override
        public void onRequest(RestRequest restRequest, RequestContext requestContext, Map<String, String> wireAttrs) {
            latch.countDown();
        }

        @Override
        public void onResponse(RestResponse restResponse, RequestContext requestContext, Map<String, String> wireAttrs) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs) {
            success.set(ex instanceof DisruptedException);
            latch.countDown();
        }
    };
    filter.onRestRequest(new RestRequestBuilder(new URI(URI)).build(), requestContext, Collections.emptyMap(), next);
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Missing NextFilter invocation");
    Assert.assertTrue(success.get(), "Unexpected method invocation");
}
Also used : NextFilter(com.linkedin.r2.filter.NextFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 4 with DisruptFilter

use of com.linkedin.r2.disruptor.DisruptFilter in project rest.li by linkedin.

the class HttpClientFactory method getClient.

/**
   * Create a new {@link TransportClient} with the specified properties,
   * {@link SSLContext} and {@link SSLParameters}
   *
   * @param properties map of properties for the {@link TransportClient}
   * @param sslContext {@link SSLContext} to be used for requests over SSL/TLS.
   * @param sslParameters {@link SSLParameters} to configure secure connections.
   * @return an appropriate {@link TransportClient} instance, as specified by the properties.
   */
private TransportClient getClient(Map<String, ? extends Object> properties, SSLContext sslContext, SSLParameters sslParameters) {
    LOG.info("Getting a client with configuration {} and SSLContext {}", properties, sslContext);
    TransportClient client = getRawClient(properties, sslContext, sslParameters);
    List<String> httpRequestServerSupportedEncodings = ConfigValueExtractor.buildList(properties.remove(HTTP_REQUEST_CONTENT_ENCODINGS), LIST_SEPARATOR);
    // In the old model, responses were compressed according to the type of method, so clients would send
    // the Accept-Encoding header if the method was in HTTP_RESPONSE_COMPRESSION_OPERATIONS.
    // In the new model, responses are compressed according to its size, so clients send the Accept-Encoding header
    // if the server enabled response compression by setting HTTP_USE_RESPONSE_COMPRESSION to true.
    // Until all servers migrate to the new model, clients will understand both models,
    // and send the Accept-Encoding header if either the old or the new criterion is satisfied.
    List<String> httpResponseCompressionOperations = ConfigValueExtractor.buildList(properties.remove(HTTP_RESPONSE_COMPRESSION_OPERATIONS), LIST_SEPARATOR);
    String useResponseCompressionProperty = (String) properties.get(HTTP_USE_RESPONSE_COMPRESSION);
    if (useResponseCompressionProperty != null && Boolean.parseBoolean(useResponseCompressionProperty)) {
        httpResponseCompressionOperations.add(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR);
    }
    FilterChain filters = _filters;
    if (_useClientCompression) {
        List<String> responseEncodings = null;
        if (properties.containsKey(HTTP_RESPONSE_CONTENT_ENCODINGS)) {
            responseEncodings = ConfigValueExtractor.buildList(properties.remove(HTTP_RESPONSE_CONTENT_ENCODINGS), LIST_SEPARATOR);
        }
        String httpServiceName = (String) properties.get(HTTP_SERVICE_NAME);
        EncodingType restRequestContentEncoding = getRestRequestContentEncoding(httpRequestServerSupportedEncodings);
        StreamEncodingType streamRequestContentEncoding = getStreamRequestContentEncoding(httpRequestServerSupportedEncodings);
        if (restRequestContentEncoding != EncodingType.IDENTITY || !httpResponseCompressionOperations.isEmpty()) {
            filters = filters.addLastRest(new ClientCompressionFilter(restRequestContentEncoding, getRestRequestCompressionConfig(httpServiceName, restRequestContentEncoding), buildRestAcceptEncodingSchemaNames(responseEncodings), _responseCompressionConfigs.get(httpServiceName), httpResponseCompressionOperations));
        }
        if (streamRequestContentEncoding != StreamEncodingType.IDENTITY || !httpResponseCompressionOperations.isEmpty()) {
            CompressionConfig compressionConfig = getStreamRequestCompressionConfig(httpServiceName, streamRequestContentEncoding);
            filters = filters.addLast(new ClientStreamCompressionFilter(streamRequestContentEncoding, compressionConfig, buildStreamAcceptEncodingSchemas(responseEncodings), _responseCompressionConfigs.get(httpServiceName), httpResponseCompressionOperations, _compressionExecutor));
        }
    }
    Integer queryPostThreshold = chooseNewOverDefault(getIntValue(properties, HTTP_QUERY_POST_THRESHOLD), Integer.MAX_VALUE);
    ClientQueryTunnelFilter clientQueryTunnelFilter = new ClientQueryTunnelFilter(queryPostThreshold);
    filters = filters.addLastRest(clientQueryTunnelFilter);
    filters = filters.addLast(clientQueryTunnelFilter);
    // Add the disruptor filter to the end of the filter chain to get the most accurate simulation of disrupt
    Integer requestTimeout = chooseNewOverDefault(getIntValue(properties, HTTP_REQUEST_TIMEOUT), DEFAULT_REQUEST_TIMEOUT);
    DisruptFilter disruptFilter = new DisruptFilter(_executor, _eventLoopGroup, requestTimeout);
    filters = filters.addLastRest(disruptFilter);
    filters = filters.addLast(disruptFilter);
    client = new FilterChainClient(client, filters);
    client = new FactoryClient(client);
    synchronized (_mutex) {
        if (!_running) {
            throw new IllegalStateException("Factory is shutting down");
        }
        _clientsOutstanding++;
        return client;
    }
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) FilterChain(com.linkedin.r2.filter.FilterChain) StreamEncodingType(com.linkedin.r2.filter.compression.streaming.StreamEncodingType) EncodingType(com.linkedin.r2.filter.compression.EncodingType) DisruptFilter(com.linkedin.r2.disruptor.DisruptFilter) StreamEncodingType(com.linkedin.r2.filter.compression.streaming.StreamEncodingType) ClientStreamCompressionFilter(com.linkedin.r2.filter.compression.ClientStreamCompressionFilter) FilterChainClient(com.linkedin.r2.filter.transport.FilterChainClient) ClientQueryTunnelFilter(com.linkedin.r2.filter.transport.ClientQueryTunnelFilter) ClientCompressionFilter(com.linkedin.r2.filter.compression.ClientCompressionFilter) CompressionConfig(com.linkedin.r2.filter.CompressionConfig)

Example 5 with DisruptFilter

use of com.linkedin.r2.disruptor.DisruptFilter in project rest.li by linkedin.

the class TestDisruptFilter method testRestLatencyDisrupt.

@Test
public void testRestLatencyDisrupt() throws Exception {
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.delay(REQUEST_LATENCY));
    final DisruptFilter filter = new DisruptFilter(_scheduler, _executor, REQUEST_TIMEOUT);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    final NextFilter<RestRequest, RestResponse> next = new NextFilter<RestRequest, RestResponse>() {

        @Override
        public void onRequest(RestRequest restRequest, RequestContext requestContext, Map<String, String> wireAttrs) {
            success.set(true);
            latch.countDown();
        }

        @Override
        public void onResponse(RestResponse restResponse, RequestContext requestContext, Map<String, String> wireAttrs) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs) {
            latch.countDown();
        }
    };
    filter.onRestRequest(new RestRequestBuilder(new URI(URI)).build(), requestContext, Collections.emptyMap(), next);
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Missing NextFilter invocation");
    Assert.assertTrue(success.get(), "Unexpected method invocation");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestRequest(com.linkedin.r2.message.rest.RestRequest) NextFilter(com.linkedin.r2.filter.NextFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) CountDownLatch(java.util.concurrent.CountDownLatch) Map(java.util.Map) URI(java.net.URI) Test(org.testng.annotations.Test)

Aggregations

NextFilter (com.linkedin.r2.filter.NextFilter)8 RequestContext (com.linkedin.r2.message.RequestContext)8 URI (java.net.URI)8 Map (java.util.Map)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Test (org.testng.annotations.Test)8 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)5 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)5 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)5 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 RestResponse (com.linkedin.r2.message.rest.RestResponse)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 TimeoutException (java.util.concurrent.TimeoutException)2 DisruptFilter (com.linkedin.r2.disruptor.DisruptFilter)1 CompressionConfig (com.linkedin.r2.filter.CompressionConfig)1 FilterChain (com.linkedin.r2.filter.FilterChain)1 ClientCompressionFilter (com.linkedin.r2.filter.compression.ClientCompressionFilter)1