Search in sources :

Example 26 with StreamRequest

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

the class RetryClientTest method testStreamRetry.

@Test
public void testStreamRetry() throws Exception {
    SimpleLoadBalancer balancer = prepareLoadBalancer(Arrays.asList("http://test.linkedin.com/retry1", "http://test.linkedin.com/good"), HttpClientFactory.UNLIMITED_CLIENT_REQUEST_RETRY_RATIO);
    DynamicClient dynamicClient = new DynamicClient(balancer, null);
    RetryClient client = new RetryClient(dynamicClient, balancer, D2ClientConfig.DEFAULT_RETRY_LIMIT, RetryClient.DEFAULT_UPDATE_INTERVAL_MS, RetryClient.DEFAULT_AGGREGATED_INTERVAL_NUM, SystemClock.instance(), true, true);
    URI uri = URI.create("d2://retryService?arg1arg2");
    StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.newEntityStream(new ByteStringWriter(CONTENT)));
    DegraderTrackerClientTest.TestCallback<StreamResponse> restCallback = new DegraderTrackerClientTest.TestCallback<>();
    client.streamRequest(streamRequest, restCallback);
    assertNull(restCallback.e);
    assertNotNull(restCallback.t);
}
Also used : SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 27 with StreamRequest

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

the class TestBackupRequestsClient method testStreamRequestWithNoIsFullRequest.

@Test(invocationCount = 3, dataProvider = "isD2Async")
public void testStreamRequestWithNoIsFullRequest(boolean isD2Async) throws Exception {
    // 1s till response comes back
    int responseDelayNano = 100000000;
    // make backup request after 0.5 second
    int backupDelayNano = 50000000;
    Deque<URI> hostsReceivingRequest = new ConcurrentLinkedDeque<>();
    BackupRequestsClient client = createAlwaysBackupClientWithHosts(Arrays.asList("http://test1.com:123", "http://test2.com:123"), hostsReceivingRequest, responseDelayNano, backupDelayNano, isD2Async);
    URI uri = URI.create("d2://testService");
    // if there is no IS_FULL_REQUEST set, backup requests will not happen
    StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.newEntityStream(new ByteStringWriter(CONTENT)));
    RequestContext context = new RequestContext();
    context.putLocalAttr(R2Constants.OPERATION, "get");
    RequestContext context1 = context.clone();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<AssertionError> failure = new AtomicReference<>();
    client.streamRequest(streamRequest, context1, new Callback<StreamResponse>() {

        @Override
        public void onError(Throwable e) {
            failure.set(new AssertionError("Callback onError"));
            latch.countDown();
        }

        @Override
        public void onSuccess(StreamResponse result) {
            try {
                assertEquals(result.getStatus(), 200);
                assertEquals(result.getHeader("buffered"), "false");
                assertEquals(hostsReceivingRequest.size(), 1);
                assertEquals(new HashSet<>(hostsReceivingRequest).size(), 1);
                hostsReceivingRequest.clear();
            } catch (AssertionError e) {
                failure.set(e);
            }
            latch.countDown();
        }
    });
    latch.await(2, TimeUnit.SECONDS);
    if (failure.get() != null) {
        throw failure.get();
    }
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 28 with StreamRequest

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

the class TestBackupRequestsClient method testStreamRequestWithIsFullRequest.

@Test(invocationCount = 3, dataProvider = "isD2Async")
public void testStreamRequestWithIsFullRequest(boolean isD2Async) throws Exception {
    // 5s till response comes back
    int responseDelayNano = 500000000;
    // make backup request after 1 second
    int backupDelayNano = 100000000;
    Deque<URI> hostsReceivingRequest = new ConcurrentLinkedDeque<>();
    BackupRequestsClient client = createAlwaysBackupClientWithHosts(Arrays.asList("http://test1.com:123", "http://test2.com:123"), hostsReceivingRequest, responseDelayNano, backupDelayNano, isD2Async);
    URI uri = URI.create("d2://testService");
    // if there is IS_FULL_REQUEST set, backup requests will happen
    StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.newEntityStream(new ByteStringWriter(CONTENT)));
    RequestContext context = new RequestContext();
    context.putLocalAttr(R2Constants.OPERATION, "get");
    context.putLocalAttr(R2Constants.IS_FULL_REQUEST, true);
    RequestContext context1 = context.clone();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<AssertionError> failure = new AtomicReference<>();
    client.streamRequest(streamRequest, context1, new Callback<StreamResponse>() {

        @Override
        public void onError(Throwable e) {
            failure.set(new AssertionError("Callback onError"));
            latch.countDown();
        }

        @Override
        public void onSuccess(StreamResponse result) {
            try {
                assertEquals(result.getStatus(), 200);
                assertEquals(result.getHeader("buffered"), "true");
                assertEquals(hostsReceivingRequest.size(), 2);
                assertEquals(new HashSet<>(hostsReceivingRequest).size(), 2);
                hostsReceivingRequest.clear();
            } catch (AssertionError e) {
                failure.set(e);
            }
            latch.countDown();
        }
    });
    latch.await(6, TimeUnit.SECONDS);
    if (failure.get() != null) {
        throw failure.get();
    }
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 29 with StreamRequest

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

the class RewriteLoadBalancerClientTestStreamRequest method testEscapingHelper.

private void testEscapingHelper(String hostUri, String serviceName, String path) {
    URI uri = URI.create(hostUri);
    TestClient wrappedClient = new TestClient();
    RewriteLoadBalancerClient client = new RewriteLoadBalancerClient(serviceName, uri, wrappedClient);
    assertEquals(client.getUri(), uri);
    assertEquals(client.getServiceName(), serviceName);
    StreamRequest streamRequest = getRequest("d2://" + serviceName + path);
    Map<String, String> restWireAttrs = new HashMap<>();
    TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<>();
    client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
    assertFalse(restCallback.response.hasError());
    assertEquals(wrappedClient.streamRequest.getHeaders(), streamRequest.getHeaders());
    assertEquals(wrappedClient.streamRequest.getMethod(), streamRequest.getMethod());
    assertEquals(wrappedClient.streamRequest.getURI(), URI.create(hostUri + path));
}
Also used : HashMap(java.util.HashMap) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) TestTransportCallback(com.linkedin.d2.balancer.clients.DegraderTrackerClientTest.TestTransportCallback) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Example 30 with StreamRequest

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

the class RewriteLoadBalancerClientTestStreamRequest method testClient.

@Test(groups = { "small", "back-end" })
public void testClient() throws URISyntaxException {
    URI uri = URI.create("http://test.linkedin.com/test");
    String serviceName = "HistoryService";
    TestClient wrappedClient = new TestClient();
    RewriteLoadBalancerClient client = new RewriteLoadBalancerClient(serviceName, uri, wrappedClient);
    assertEquals(client.getUri(), uri);
    assertEquals(client.getServiceName(), serviceName);
    StreamRequest streamRequest = new StreamRequestBuilder(URI.create("d2://HistoryService/getCube")).build(EntityStreams.emptyStream());
    Map<String, String> restWireAttrs = new HashMap<>();
    TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<>();
    client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
    assertFalse(restCallback.response.hasError());
    assertEquals(wrappedClient.streamRequest.getHeaders(), streamRequest.getHeaders());
    assertEquals(wrappedClient.streamRequest.getMethod(), streamRequest.getMethod());
    // check the rewrite
    assertEquals(wrappedClient.streamRequest.getURI(), URI.create("http://test.linkedin.com/test/getCube"));
    assertEquals(wrappedClient.restWireAttrs, restWireAttrs);
}
Also used : HashMap(java.util.HashMap) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) TestTransportCallback(com.linkedin.d2.balancer.clients.DegraderTrackerClientTest.TestTransportCallback) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Aggregations

StreamRequest (com.linkedin.r2.message.stream.StreamRequest)173 Test (org.testng.annotations.Test)132 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)121 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)116 URI (java.net.URI)84 RequestContext (com.linkedin.r2.message.RequestContext)82 CountDownLatch (java.util.concurrent.CountDownLatch)51 ByteString (com.linkedin.data.ByteString)46 RestRequest (com.linkedin.r2.message.rest.RestRequest)45 Callback (com.linkedin.common.callback.Callback)38 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)34 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)32 HashMap (java.util.HashMap)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)29 BeforeTest (org.testng.annotations.BeforeTest)26 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)25 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)24 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)23 RestResponse (com.linkedin.r2.message.rest.RestResponse)23 AfterTest (org.testng.annotations.AfterTest)23