Search in sources :

Example 11 with DrainReader

use of com.linkedin.r2.message.stream.entitystream.DrainReader in project rest.li by linkedin.

the class TestServerTimeout method getTransportDispatcher.

@Override
protected TransportDispatcher getTransportDispatcher() {
    final Map<URI, StreamRequestHandler> handlers = new HashMap<>();
    handlers.put(BUGGY_SERVER_URI, new BuggyRequestHandler());
    handlers.put(THROW_BUT_SHOULD_NOT_TIMEOUT_URI, new ThrowHandler());
    handlers.put(BUGGY_FILTER_URI, new NormalHandler());
    return new TransportDispatcher() {

        @Override
        public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
            throw new UnsupportedOperationException("This dispatcher only supports stream");
        }

        @Override
        public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
            StreamRequestHandler handler = handlers.get(req.getURI());
            if (handler != null) {
                handler.handleRequest(req, requestContext, new TransportCallbackAdapter<>(callback));
            } else {
                req.getEntityStream().setReader(new DrainReader());
                callback.onResponse(TransportResponseImpl.<StreamResponse>error(new IllegalStateException("Handler not found for URI " + req.getURI())));
            }
        }
    };
}
Also used : TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) HashMap(java.util.HashMap) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) URI(java.net.URI) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) StreamRequestHandler(com.linkedin.r2.transport.common.StreamRequestHandler) RestRequest(com.linkedin.r2.message.rest.RestRequest) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with DrainReader

use of com.linkedin.r2.message.stream.entitystream.DrainReader in project rest.li by linkedin.

the class TestJetty404 method setup.

@BeforeClass
public void setup() throws IOException {
    _clientFactory = new HttpClientFactory.Builder().build();
    _client = new TransportClientAdapter(_clientFactory.getClient(Collections.<String, String>emptyMap()), true);
    _server = new HttpServerFactory().createH2cServer(PORT, "/correct-path", 50, new TransportDispatcher() {

        @Override
        public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
            callback.onResponse(TransportResponseImpl.success(new RestResponseBuilder().build()));
        }

        @Override
        public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
            req.getEntityStream().setReader(new DrainReader());
            callback.onResponse(TransportResponseImpl.success(new StreamResponseBuilder().build(EntityStreams.emptyStream())));
        }
    }, true);
    _server.start();
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) BeforeClass(org.testng.annotations.BeforeClass)

Example 13 with DrainReader

use of com.linkedin.r2.message.stream.entitystream.DrainReader in project rest.li by linkedin.

the class TestJetty404 method testJetty404.

// make sure jetty's default behavior will read all the request bytes in case of 404
// Known to be flaky in CI
@Test(retryAnalyzer = SingleRetry.class)
public void testJetty404() throws Exception {
    BytesWriter writer = new BytesWriter(200 * 1024, (byte) 100);
    final AtomicReference<Throwable> exRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    _client.streamRequest(new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, URI.create("/wrong-path"))).build(EntityStreams.newEntityStream(writer)), new Callback<StreamResponse>() {

        @Override
        public void onError(Throwable e) {
            exRef.set(e);
            latch.countDown();
        }

        @Override
        public void onSuccess(StreamResponse result) {
            latch.countDown();
        }
    });
    latch.await(5000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(writer.isDone());
    Throwable ex = exRef.get();
    Assert.assertTrue(ex instanceof StreamException, "Expected StreamException but found: " + ex);
    StreamResponse response = ((StreamException) ex).getResponse();
    Assert.assertEquals(response.getStatus(), RestStatus.NOT_FOUND);
    response.getEntityStream().setReader(new DrainReader());
}
Also used : BytesWriter(test.r2.integ.helper.BytesWriter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamException(com.linkedin.r2.message.stream.StreamException) Test(org.testng.annotations.Test)

Example 14 with DrainReader

use of com.linkedin.r2.message.stream.entitystream.DrainReader in project rest.li by linkedin.

the class TestChannelPoolStreamHandler method getChannel.

private static EmbeddedChannel getChannel() {
    EmbeddedChannel ch = new EmbeddedChannel(new RAPStreamResponseDecoder(1000), new RAPStreamResponseHandler(), new ChannelPoolStreamHandler());
    ch.attr(RAPStreamResponseDecoder.TIMEOUT_ATTR_KEY).set(new Timeout<>(Executors.newSingleThreadScheduledExecutor(), 1000, TimeUnit.MILLISECONDS, None.none()));
    ch.attr(RAPStreamResponseHandler.CALLBACK_ATTR_KEY).set(response -> {
        StreamResponse streamResponse = response.getResponse();
        streamResponse.getEntityStream().setReader(new DrainReader());
    });
    return ch;
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader)

Example 15 with DrainReader

use of com.linkedin.r2.message.stream.entitystream.DrainReader in project rest.li by linkedin.

the class TestBackupRequestsClient method createAlwaysBackupClientWithHosts.

private BackupRequestsClient createAlwaysBackupClientWithHosts(List<String> uris, Deque<URI> hostsReceivingRequestList, int responseDelayNano, int backupDelayNano, boolean isD2Async) throws IOException {
    Map<URI, Map<Integer, PartitionData>> partitionDescriptions = new HashMap<>();
    uris.forEach(uri -> partitionDescriptions.put(URI.create(uri), Collections.singletonMap(0, new PartitionData(1))));
    StaticLoadBalancerState LbState = new StaticLoadBalancerState() {

        @Override
        public TrackerClient getClient(String serviceName, URI uri) {
            return new DegraderTrackerClientImpl(uri, partitionDescriptions.get(uri), null, SystemClock.instance(), null) {

                @Override
                public void restRequest(RestRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<RestResponse> callback) {
                    // whenever a trackerClient is used to make request, record down it's hostname
                    hostsReceivingRequestList.add(uri);
                    // delay response to allow backup request to happen
                    _executor.schedule(() -> callback.onResponse(TransportResponseImpl.success(new RestResponseBuilder().build())), responseDelayNano, TimeUnit.NANOSECONDS);
                }

                @Override
                public void streamRequest(StreamRequest request, RequestContext requestContext, Map<String, String> wireAttrs, TransportCallback<StreamResponse> callback) {
                    // whenever a trackerClient is used to make request, record down it's hostname
                    hostsReceivingRequestList.add(uri);
                    if (null != requestContext.getLocalAttr(R2Constants.BACKUP_REQUEST_BUFFERED_BODY)) {
                        callback.onResponse(TransportResponseImpl.success(new StreamResponseBuilder().setHeader(BUFFERED_HEADER, String.valueOf(requestContext.getLocalAttr(R2Constants.BACKUP_REQUEST_BUFFERED_BODY) != null)).build(EntityStreams.emptyStream())));
                        return;
                    }
                    request.getEntityStream().setReader(new DrainReader() {

                        public void onDone() {
                            // delay response to allow backup request to happen
                            _executor.schedule(() -> callback.onResponse(TransportResponseImpl.success(new StreamResponseBuilder().setHeader(BUFFERED_HEADER, String.valueOf(requestContext.getLocalAttr(R2Constants.BACKUP_REQUEST_BUFFERED_BODY) != null)).build(EntityStreams.emptyStream()))), responseDelayNano, TimeUnit.NANOSECONDS);
                        }
                    });
                }
            };
        }
    };
    LbState.TEST_URIS_PARTITIONDESCRIPTIONS.putAll(partitionDescriptions);
    LbState.TEST_SERVICE_BACKUP_REQUEST_PROPERTIES.add(createBackupRequestsConfiguration(5, "get"));
    LbState.refreshDefaultProperties();
    LoadBalancer loadBalancer = new SimpleLoadBalancer(LbState, _executor);
    DynamicClient dynamicClient = new DynamicClient(loadBalancer, null);
    return new BackupRequestsClient(dynamicClient, loadBalancer, _executor, null, 10, TimeUnit.SECONDS, isD2Async) {

        @Override
        Optional<TrackingBackupRequestsStrategy> getStrategyAfterUpdate(final String serviceName, final String operation) {
            // constantly enable backup request after backupDelayNano time.
            BackupRequestsStrategy alwaysBackup = new TestTrackingBackupRequestsStrategy.MockBackupRequestsStrategy(() -> Optional.of((long) backupDelayNano), () -> true);
            return Optional.of(new TrackingBackupRequestsStrategy(alwaysBackup));
        }
    };
}
Also used : TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) HashMap(java.util.HashMap) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) LoadBalancer(com.linkedin.d2.balancer.LoadBalancer) ByteString(com.linkedin.data.ByteString) TrackingBackupRequestsStrategy(com.linkedin.d2.backuprequests.TrackingBackupRequestsStrategy) TestTrackingBackupRequestsStrategy(com.linkedin.d2.backuprequests.TestTrackingBackupRequestsStrategy) URI(java.net.URI) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) BackupRequestsStrategy(com.linkedin.d2.backuprequests.BackupRequestsStrategy) TrackingBackupRequestsStrategy(com.linkedin.d2.backuprequests.TrackingBackupRequestsStrategy) TestTrackingBackupRequestsStrategy(com.linkedin.d2.backuprequests.TestTrackingBackupRequestsStrategy) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) StaticLoadBalancerState(com.linkedin.d2.balancer.StaticLoadBalancerState) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

DrainReader (com.linkedin.r2.message.stream.entitystream.DrainReader)15 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)11 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)10 RequestContext (com.linkedin.r2.message.RequestContext)8 URI (java.net.URI)8 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)7 RestRequest (com.linkedin.r2.message.rest.RestRequest)6 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)6 Map (java.util.Map)6 Test (org.testng.annotations.Test)6 StreamRequestHandler (com.linkedin.r2.transport.common.StreamRequestHandler)5 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)5 HashMap (java.util.HashMap)5 ByteString (com.linkedin.data.ByteString)3 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)3 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 BeforeClass (org.testng.annotations.BeforeClass)3 FilterChain (com.linkedin.r2.filter.FilterChain)2