use of com.linkedin.restli.examples.instrumentation.api.InstrumentationControl 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.restli.examples.instrumentation.api.InstrumentationControl in project rest.li by linkedin.
the class LatencyInstrumentationResource method batchPartialUpdate.
/**
* This is the "downstream endpoint", queried by {@link #create(InstrumentationControl)} (the "upstream endpoint").
*/
@ReturnEntity
@RestMethod.BatchPartialUpdate
public BatchUpdateEntityResult<Long, InstrumentationControl> batchPartialUpdate(BatchPatchRequest<Long, InstrumentationControl> batchPatchRequest) throws DataProcessingException {
final Map<Long, UpdateEntityResponse<InstrumentationControl>> results = new HashMap<>();
final Map<Long, RestLiServiceException> errors = new HashMap<>();
for (Map.Entry<Long, PatchRequest<InstrumentationControl>> entry : batchPatchRequest.getData().entrySet()) {
// Render each patch into a normal record so we know whether or not to force a failure
InstrumentationControl control = new InstrumentationControl();
PatchApplier.applyPatch(control, entry.getValue());
if (control.isForceException()) {
RestLiServiceException error = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "You wanted me to fail, so I failed.").setCode(DOWNSTREAM_ERROR_CODE);
errors.put(entry.getKey(), error);
} else {
results.put(entry.getKey(), new UpdateEntityResponse<>(HttpStatus.S_200_OK, control));
}
}
return new BatchUpdateEntityResult<>(results, errors);
}
use of com.linkedin.restli.examples.instrumentation.api.InstrumentationControl in project rest.li by linkedin.
the class TestLatencyInstrumentation method makeUpstreamRequest.
/**
* Make the "upstream" request (as opposed to the "downstream" request made from the resource method) using a set of
* test parameters. Waits for the timing keys to be recorded by the {@link InstrumentationTrackingFilter} before
* returning.
* @param useStreaming parameter from the test method
* @param forceException parameter from the test method
*/
private void makeUpstreamRequest(boolean useStreaming, boolean forceException, boolean useScatterGather) throws RemoteInvocationException, InterruptedException {
InstrumentationControl instrumentationControl = new InstrumentationControl().setServiceUriPrefix(FILTERS_URI_PREFIX).setUseStreaming(useStreaming).setForceException(forceException).setUseScatterGather(useScatterGather);
CreateIdEntityRequest<Long, InstrumentationControl> createRequest = new LatencyInstrumentationBuilders().createAndGet().input(instrumentationControl).build();
ResponseFuture<IdEntityResponse<Long, InstrumentationControl>> response = getClient().sendRequest(createRequest);
try {
response.getResponseEntity();
if (forceException) {
Assert.fail("Forcing exception, should've failed.");
}
} catch (RestLiResponseException e) {
if (e.getStatus() != 400) {
Assert.fail("Server responded with a non-400 error: " + e.getServiceErrorStackTrace());
}
if (!forceException) {
Assert.fail("Not forcing exception, didn't expect failure.");
}
}
// Wait for the server to send the response and save the timings
final boolean success = _countDownLatch.await(10, TimeUnit.SECONDS);
if (!success) {
Assert.fail("Request timed out!");
}
}
Aggregations