use of com.linkedin.r2.RemoteInvocationException in project rest.li by linkedin.
the class TestExceptionsResource2 method testNonRestException.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testNonRestException(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) {
Response<Greeting> response = null;
RestClient brokenClient = new RestClient(getDefaultTransportClient(), "http://localhost:8888/");
try {
final Request<Greeting> req = builders.get().id(1L).build();
ResponseFuture<Greeting> future;
if (explicit) {
future = brokenClient.sendRequest(req, errorHandlingBehavior);
} else {
future = brokenClient.sendRequest(req);
}
response = future.getResponse();
Assert.fail("expected exception");
} catch (RemoteInvocationException e) {
Assert.assertEquals(e.getClass(), RemoteInvocationException.class);
}
}
use of com.linkedin.r2.RemoteInvocationException in project rest.li by linkedin.
the class RestLiStreamCallbackAdapter method onError.
@Override
public void onError(Throwable e) {
Callback<Response<T>> callback = new TimingCallback.Builder<>(_wrappedCallback, _requestContext).addEndTimingKey(FrameworkTimingKeys.CLIENT_RESPONSE_RESTLI_ERROR_DESERIALIZATION.key()).build();
TimingContextUtil.beginTiming(_requestContext, FrameworkTimingKeys.CLIENT_RESPONSE_RESTLI_ERROR_DESERIALIZATION.key());
// exception handling system in rest.li client will change to move to StreamException.
if (e instanceof StreamException) {
Messages.toRestException((StreamException) e, new Callback<RestException>() {
@Override
public void onError(Throwable e) {
// Should never happen.
callback.onError(e);
}
@Override
public void onSuccess(RestException result) {
callback.onError(ExceptionUtil.exceptionForThrowable(result, _decoder));
}
});
return;
}
if (e instanceof RemoteInvocationException) {
callback.onError(e);
return;
}
callback.onError(new RemoteInvocationException(e));
}
use of com.linkedin.r2.RemoteInvocationException in project rest.li by linkedin.
the class LatencyInstrumentationResource method create.
/**
* This is the "upstream endpoint" which is queried directly by the integration test.
* This endpoint makes a call to {@link #batchPartialUpdate(BatchPatchRequest)} (the "downstream endpoint"),
* then packs all the client-side timing data into the original server-side request context.
*/
@ReturnEntity
@RestMethod.Create
public CreateKVResponse<Long, InstrumentationControl> create(InstrumentationControl control) {
final boolean forceException = control.isForceException();
final boolean useScatterGather = control.isUseScatterGather();
final String uriPrefix = control.getServiceUriPrefix();
// Build the downstream request
final BatchPartialUpdateEntityRequestBuilder<Long, InstrumentationControl> builder = new LatencyInstrumentationBuilders().batchPartialUpdateAndGet();
final PatchRequest<InstrumentationControl> patch = PatchGenerator.diffEmpty(control);
for (long i = 0; i < DOWNSTREAM_BATCH_SIZE; i++) {
builder.input(i, patch);
}
final BatchPartialUpdateEntityRequest<Long, InstrumentationControl> request = builder.build();
// Set up the Rest.li client config
final RestLiClientConfig clientConfig = new RestLiClientConfig();
clientConfig.setUseStreaming(control.isUseStreaming());
if (useScatterGather) {
clientConfig.setScatterGatherStrategy(new DefaultScatterGatherStrategy(new DummyUriMapper()));
}
final TransportClient transportClient = new HttpClientFactory.Builder().build().getClient(Collections.emptyMap());
final RestClient restClient = new ForceScatterGatherRestClient(new TransportClientAdapter(transportClient), uriPrefix, clientConfig);
final RequestContext serverRequestContext = getContext().getRawRequestContext();
final RequestContext clientRequestContext = new RequestContext();
// Load the timing importance threshold from the server context into the client context
clientRequestContext.putLocalAttr(TimingContextUtil.TIMING_IMPORTANCE_THRESHOLD_KEY_NAME, serverRequestContext.getLocalAttr(TimingContextUtil.TIMING_IMPORTANCE_THRESHOLD_KEY_NAME));
try {
// Make the request, then assert that the returned errors (if any) are as expected
BatchKVResponse<Long, UpdateEntityStatus<InstrumentationControl>> response = restClient.sendRequest(request, clientRequestContext).getResponseEntity();
final Map<Long, ErrorResponse> errors = response.getErrors();
if (forceException && errors.isEmpty()) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Expected failures for the downstream batch request, but found none.");
}
if (!forceException && !errors.isEmpty()) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Expected no failures for the downstream batch request, but found some.");
}
for (ErrorResponse errorResponse : errors.values()) {
if (!DOWNSTREAM_ERROR_CODE.equals(errorResponse.getCode())) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Encountered a downstream failure with an unexpected or missing error code.");
}
}
} catch (RemoteInvocationException e) {
throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Downstream failures should be batch entry failures, but encountered a top-level request failure.", e);
}
Map<TimingKey, TimingContextUtil.TimingContext> clientTimingsMap = TimingContextUtil.getTimingsMap(clientRequestContext);
Map<TimingKey, TimingContextUtil.TimingContext> serverTimingsMap = TimingContextUtil.getTimingsMap(serverRequestContext);
// Load all client timings into the server timings map
serverTimingsMap.putAll(clientTimingsMap);
getContext().setResponseHeader(HAS_CLIENT_TIMINGS_HEADER, Boolean.TRUE.toString());
if (forceException) {
throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "You wanted me to fail, so I failed.").setCode(UPSTREAM_ERROR_CODE);
}
return new CreateKVResponse<>(1L, control);
}
use of com.linkedin.r2.RemoteInvocationException in project rest.li by linkedin.
the class TestGeneralEchoServiceTest method testThrowingEchoService.
@Test
public void testThrowingEchoService() throws Exception {
final EchoService client = getEchoClient(_client, Bootstrap.getThrowingEchoURI());
final String msg = "This is a simple echo message";
final FutureCallback<String> callback = new FutureCallback<>();
client.echo(msg, callback);
try {
callback.get();
Assert.fail("Should have thrown an exception");
} catch (ExecutionException e) {
Assert.assertTrue(e.getCause() instanceof RemoteInvocationException);
}
}
use of com.linkedin.r2.RemoteInvocationException in project rest.li by linkedin.
the class TestResponseCompression method testResponseCompression.
// Often fails in CI without a retry
@Test(dataProvider = "requestData", retryAnalyzer = SingleRetry.class)
public void testResponseCompression(Boolean useResponseCompression, CompressionConfig responseCompressionConfig, RestliRequestOptions restliRequestOptions, int idCount, String expectedAcceptEncoding, String expectedCompressionThreshold, boolean responseShouldBeCompressed) throws RemoteInvocationException, CloneNotSupportedException {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
Map<String, CompressionConfig> responseCompressionConfigs = new HashMap<>();
if (responseCompressionConfig != null) {
responseCompressionConfigs.put(SERVICE_NAME, responseCompressionConfig);
}
HttpClientFactory httpClientFactory = new HttpClientFactory.Builder().setEventLoopGroup(new NioEventLoopGroup(0, /* use default settings */
new NamedThreadFactory("R2 Nio Event Loop"))).setShutDownFactory(true).setScheduleExecutorService(executor).setShutdownScheduledExecutorService(true).setCallbackExecutor(executor).setShutdownCallbackExecutor(false).setJmxManager(AbstractJmxManager.NULL_JMX_MANAGER).setRequestCompressionThresholdDefault(Integer.MAX_VALUE).setRequestCompressionConfigs(Collections.<String, CompressionConfig>emptyMap()).setResponseCompressionConfigs(responseCompressionConfigs).setUseClientCompression(true).build();
Map<String, Object> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
if (useResponseCompression != null) {
properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, String.valueOf(useResponseCompression));
}
TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
Long[] ids = new Long[idCount];
for (int i = 0; i < ids.length; i++) {
ids[i] = (long) i;
}
BatchGetRequestBuilder<Long, Greeting> builder = new GreetingsBuilders(restliRequestOptions).batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding);
if (expectedCompressionThreshold != null) {
builder.setHeader(EXPECTED_COMPRESSION_THRESHOLD, expectedCompressionThreshold);
}
Request<BatchResponse<Greeting>> request = builder.build();
Response<BatchResponse<Greeting>> response = client.sendRequest(request).getResponse();
if (responseShouldBeCompressed) {
Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), EncodingType.GZIP.getHttpName());
} else {
Assert.assertNull(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED));
}
}
Aggregations