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");
}
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;
}
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());
}
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);
}
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;
}
Aggregations