Search in sources :

Example 31 with Response

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

the class TestDarkClusterVerifierManager method testVerifierErrorHandling.

@Test
void testVerifierErrorHandling() throws InterruptedException {
    setup(true);
    RestRequest req = new RestRequestBuilder(URI.create("foo")).build();
    _verifierManager.onDarkError(req, new Throwable(), DARK_CLUSTER1_NAME);
    _verifierManager.onDarkError(req, new Throwable(), DARK_CLUSTER1_NAME);
    _verifierManager.onError(req, new Throwable());
    waitForLatch();
    Assert.assertEquals(_verifier.onResponseCount, 1, "expected on response count of 1");
    Assert.assertEquals(_verifier.onDarkResponseCount, 2, "expected on dark response count of 2");
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 32 with Response

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

the class BaseDarkClusterDispatcherImpl method sendRequest.

public boolean sendRequest(RestRequest originalRequest, RestRequest darkRequest, RequestContext originalRequestContext, int numRequestDuplicates) {
    boolean requestSent = false;
    Callback<RestResponse> callback = new Callback<RestResponse>() {

        @Override
        public void onSuccess(RestResponse result) {
            _successCount.incrementAndGet();
            // Result of request is discarded if verifier is not enabled
            _verifierManager.onDarkResponse(originalRequest, result, _darkClusterName);
        }

        @Override
        public void onError(Throwable e) {
            _exceptionCount.incrementAndGet();
            _notifier.notify(() -> new RuntimeException("Got error response for:  " + darkRequest.getURI() + " from source host " + originalRequest.getURI(), e));
            String exceptionName = e.getClass().getSimpleName();
            if (e.getCause() != null) {
                exceptionName += "/" + e.getCause().getClass().getSimpleName();
            }
            AtomicInteger oldCount = _exceptionCountMap.putIfAbsent(exceptionName, new AtomicInteger(1));
            if (oldCount != null) {
                oldCount.incrementAndGet();
            }
            _verifierManager.onDarkError(originalRequest, e, _darkClusterName);
        }
    };
    for (int i = 0; i < numRequestDuplicates; i++) {
        _requestCount.incrementAndGet();
        if (_dispatcher.sendRequest(originalRequest, darkRequest, originalRequestContext, _darkClusterName, callback)) {
            requestSent = true;
        }
    }
    return requestSent;
}
Also used : Callback(com.linkedin.common.callback.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RestResponse(com.linkedin.r2.message.rest.RestResponse)

Example 33 with Response

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

the class TestMIMEChainingSinglePart method testSinglePartDataSource.

// Verifies that a single part mime reader can be used as a data source to the writer.
// To make the test easier to write, we simply chain back to the client in the form of simulating a response.
@Test(dataProvider = "chunkSizes")
public void testSinglePartDataSource(final int chunkSize) throws Exception {
    final List<MultiPartMIMEDataSourceWriter> dataSources = generateInputStreamDataSources(chunkSize, _scheduledExecutorService);
    final MultiPartMIMEWriter writer = new MultiPartMIMEWriter.Builder().appendDataSources(dataSources).build();
    final StreamRequest streamRequest = mock(StreamRequest.class);
    when(streamRequest.getEntityStream()).thenReturn(writer.getEntityStream());
    final String contentTypeHeader = "multipart/mixed; boundary=" + writer.getBoundary();
    when(streamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER)).thenReturn(contentTypeHeader);
    // Client side preparation to read the part back on the callback
    // Note the chunks size will carry over since the client is controlling how much data he gets back
    // based on the chunk size he writes.
    MIMETestUtils.MultiPartMIMEFullReaderCallback clientReceiver = new MIMETestUtils.MultiPartMIMEFullReaderCallback();
    Callback<StreamResponse> callback = generateSuccessChainCallback(clientReceiver);
    // Server side start
    MultiPartMIMEReader reader = MultiPartMIMEReader.createAndAcquireStream(streamRequest);
    final CountDownLatch latch = new CountDownLatch(1);
    ServerMultiPartMIMEReaderSinglePartSenderCallback serverSender = new ServerMultiPartMIMEReaderSinglePartSenderCallback(latch, callback);
    reader.registerReaderCallback(serverSender);
    latch.await(_testTimeout, TimeUnit.MILLISECONDS);
    // Verify client
    Assert.assertEquals(clientReceiver.getSinglePartMIMEReaderCallbacks().size(), 1);
    Assert.assertEquals(clientReceiver.getSinglePartMIMEReaderCallbacks().get(0).getFinishedData(), BODY_A.getPartData());
    Assert.assertEquals(clientReceiver.getSinglePartMIMEReaderCallbacks().get(0).getHeaders(), BODY_A.getPartHeaders());
    // Verify server
    List<MIMETestUtils.SinglePartMIMEFullReaderCallback> singlePartMIMEReaderCallbacks = serverSender.getSinglePartMIMEReaderCallbacks();
    Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 3);
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(0).getFinishedData(), BODY_B.getPartData());
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(0).getHeaders(), BODY_B.getPartHeaders());
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(1).getFinishedData(), BODY_C.getPartData());
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(1).getHeaders(), BODY_C.getPartHeaders());
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(2).getFinishedData(), BODY_D.getPartData());
    Assert.assertEquals(singlePartMIMEReaderCallbacks.get(2).getHeaders(), BODY_D.getPartHeaders());
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) MIMETestUtils(com.linkedin.multipart.utils.MIMETestUtils) Test(org.testng.annotations.Test)

Example 34 with Response

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

the class ClientRetryFilter method processError.

private <REQ extends Request, RES extends Response> void processError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<REQ, RES> nextFilter) {
    String retryAttr = wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY);
    if (retryAttr != null) {
        if (ex instanceof RestException) {
            ex = new RestException(((RestException) ex).getResponse(), new RetriableRequestException(retryAttr, ex.getCause()));
        } else if (ex instanceof StreamException) {
            ex = new StreamException(((StreamException) ex).getResponse(), new RetriableRequestException(retryAttr, ex.getCause()));
        } else {
            ex = new RetriableRequestException(retryAttr, ex);
        }
    }
    nextFilter.onError(ex, requestContext, wireAttrs);
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestException(com.linkedin.r2.message.rest.RestException) StreamException(com.linkedin.r2.message.stream.StreamException)

Example 35 with Response

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

the class DirectoryDbSource method loadDb.

private Map<RestRequest, RestResponse> loadDb(File dir, MessageSerializer serializer) throws IOException {
    final Map<RestRequest, RestResponse> db = new HashMap<>();
    final String[] ids = DirectoryDbUtil.listRequestIds(dir);
    Arrays.sort(ids);
    for (String id : ids) {
        InputStream reqIn = null;
        InputStream resIn = null;
        try {
            reqIn = new FileInputStream(DirectoryDbUtil.requestFileName(dir, id));
            final RestRequest req = serializer.readRestRequest(reqIn);
            resIn = new FileInputStream(DirectoryDbUtil.responseFileName(dir, id));
            final RestResponse res = serializer.readRestResponse(resIn);
            db.put(canonicalize(req), res);
        } catch (IOException e) {
            _log.warn("Failed to parse request or response for: " + id, e);
        } finally {
            closeSilently(reqIn);
            closeSilently(resIn);
        }
    }
    return db;
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

Test (org.testng.annotations.Test)155 RestResponse (com.linkedin.r2.message.rest.RestResponse)142 RestRequest (com.linkedin.r2.message.rest.RestRequest)101 RequestContext (com.linkedin.r2.message.RequestContext)95 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)87 URI (java.net.URI)77 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)66 ByteString (com.linkedin.data.ByteString)58 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)41 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)40 HashMap (java.util.HashMap)39 FutureCallback (com.linkedin.common.callback.FutureCallback)36 RestException (com.linkedin.r2.message.rest.RestException)36 CountDownLatch (java.util.concurrent.CountDownLatch)35 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)34 ExecutionException (java.util.concurrent.ExecutionException)32 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)27 Callback (com.linkedin.common.callback.Callback)25 IOException (java.io.IOException)24 Map (java.util.Map)24