Search in sources :

Example 11 with Task

use of com.linkedin.parseq.Task in project rest.li by linkedin.

the class ParseqTraceDebugRequestHandler method handleRequest.

@Override
public void handleRequest(final RestRequest request, final RequestContext context, final ResourceDebugRequestHandler resourceDebugRequestHandler, final Callback<RestResponse> callback) {
    // Find out the path coming after the "__debug" path segment
    String fullPath = request.getURI().getPath();
    int debugSegmentIndex = fullPath.indexOf(DelegatingDebugRequestHandler.DEBUG_PATH_SEGMENT);
    final String debugHandlerPath = fullPath.substring(debugSegmentIndex + DelegatingDebugRequestHandler.DEBUG_PATH_SEGMENT.length() + 1);
    assert (debugHandlerPath.startsWith(HANDLER_ID));
    // Decide whether this is a user issued debug request or a follow up static content request for tracevis html.
    if (debugHandlerPath.equals(TRACEVIS_PATH) || debugHandlerPath.equals(RAW_PATH)) {
        if (context.getLocalAttr(RestLiMethodInvoker.ATTRIBUTE_PROMISE_LISTENER) != null) {
            callback.onError(new Exception("Unexpected PromiseListener in local attributes: " + context.getLocalAttr(RestLiMethodInvoker.ATTRIBUTE_PROMISE_LISTENER)));
        }
        // This listener is registered to the resource execution task in RestLiMethodInvoker. Upon resolution of the task,
        // this listener gets the trace for the task and send it through the callback.
        context.putLocalAttr(RestLiMethodInvoker.ATTRIBUTE_PROMISE_LISTENER, new PromiseListener<Object>() {

            @Override
            public void onResolved(Promise<Object> promise) {
                try {
                    sendDebugResponse(callback, ((Task<?>) promise).getTrace(), debugHandlerPath);
                } catch (Throwable e) {
                    callback.onError(e);
                }
            }
        });
        resourceDebugRequestHandler.handleRequest(request, context, new Callback<RestResponse>() {

            @Override
            public void onError(Throwable e) {
            // No-op. We only care about the ParSeq trace but not the execution result, be it successful or not.
            }

            @Override
            public void onSuccess(RestResponse result) {
            // No-op. We only care about the ParSeq trace but not the execution result, be it successful or not.
            }
        });
    } else {
        // We know that the request is a static content request. So here we figure out the internal path for the
        // JAR resource from the request path. A request uri such as "/__debug/parseqtrace/trace.html" translates to
        // "/tracevis/trace.html" for the resource path.
        String resourcePath = debugHandlerPath.replaceFirst(HANDLER_ID, ENTRY_PATH_SEGMENT_TRACEVIS);
        ClassLoader currentClassLoader = getClass().getClassLoader();
        InputStream resourceStream = currentClassLoader.getResourceAsStream(resourcePath);
        String mediaType = null;
        if (resourceStream != null) {
            mediaType = determineMediaType(resourcePath);
        }
        // If the requested file type is not supported by this debug request handler, return 404.
        if (mediaType == null) {
            callback.onError(new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
        try {
            sendByteArrayAsResponse(callback, IOUtils.toByteArray(resourceStream), mediaType);
        } catch (IOException exception) {
            callback.onError(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, exception));
        }
    }
}
Also used : Task(com.linkedin.parseq.Task) RestResponse(com.linkedin.r2.message.rest.RestResponse) InputStream(java.io.InputStream) ByteString(com.linkedin.data.ByteString) IOException(java.io.IOException) IOException(java.io.IOException)

Example 12 with Task

use of com.linkedin.parseq.Task in project rest.li by linkedin.

the class MockBatchingStrategy method testAddToGroupAndExecute_MultipleGroup.

@Test
public void testAddToGroupAndExecute_MultipleGroup() throws Exception {
    // Task will be called twice in two groups
    Task mockTask = mock(Task.class);
    ExecutionGroup eg2 = new ExecutionGroup(_engine);
    eg.addTaskByFluentClient(client1, mockTask);
    eg2.addTaskByFluentClient(client1, mockTask);
    eg.execute();
    eg2.execute();
    CountDownLatch waitLatch = new CountDownLatch(2);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            waitLatch.countDown();
            return null;
        }
    }).when(mockTask).contextRun(any(), any(), any());
    waitLatch.await(1000, TimeUnit.MILLISECONDS);
    verify(mockTask, times(2)).contextRun(any(), any(), any());
}
Also used : Answer(org.mockito.stubbing.Answer) Task(com.linkedin.parseq.Task) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 13 with Task

use of com.linkedin.parseq.Task in project rest.li by linkedin.

the class TestParseqTraceDebugRequestHandler method executeRequestThroughParseqDebugHandler.

private void executeRequestThroughParseqDebugHandler(URI uri, Callback<RestResponse> callback) {
    ParseqTraceDebugRequestHandler requestHandler = new ParseqTraceDebugRequestHandler();
    RestRequestBuilder requestBuilder = new RestRequestBuilder(uri);
    RestRequest request = requestBuilder.build();
    RequestContext requestContext = new RequestContext();
    requestHandler.handleRequest(request, requestContext, new RestLiDebugRequestHandler.ResourceDebugRequestHandler() {

        @Override
        @SuppressWarnings("unchecked")
        public void handleRequest(RestRequest request, RequestContext requestContext, Callback<RestResponse> callback) {
            RestResponse response = EasyMock.createMock(RestResponse.class);
            JsonTraceCodec jsonTraceCodec = new JsonTraceCodec();
            Trace t = null;
            try {
                t = jsonTraceCodec.decode(TEST_TRACE);
            } catch (IOException exc) {
            // test will fail later
            }
            Task<Object> task = EasyMock.createMock(Task.class);
            EasyMock.expect(task.getTrace()).andReturn(t);
            EasyMock.replay(task);
            PromiseListener<Object> promiseListener = (PromiseListener<Object>) requestContext.getLocalAttr(RestLiMethodInvoker.ATTRIBUTE_PROMISE_LISTENER);
            promiseListener.onResolved(task);
            callback.onSuccess(response);
        }
    }, callback);
}
Also used : Task(com.linkedin.parseq.Task) PromiseListener(com.linkedin.parseq.promise.PromiseListener) RestResponse(com.linkedin.r2.message.rest.RestResponse) IOException(java.io.IOException) Trace(com.linkedin.parseq.trace.Trace) RestRequest(com.linkedin.r2.message.rest.RestRequest) JsonTraceCodec(com.linkedin.parseq.trace.codec.json.JsonTraceCodec) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext)

Aggregations

Task (com.linkedin.parseq.Task)13 BaseTask (com.linkedin.parseq.BaseTask)4 SettablePromise (com.linkedin.parseq.promise.SettablePromise)4 BaseEngineTest (com.linkedin.parseq.BaseEngineTest)3 Promises (com.linkedin.parseq.promise.Promises)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Function (java.util.function.Function)3 Test (org.testng.annotations.Test)3 ByteString (com.linkedin.data.ByteString)2 Engine (com.linkedin.parseq.Engine)2 Task.withRetryPolicy (com.linkedin.parseq.Task.withRetryPolicy)2 Failure (com.linkedin.parseq.function.Failure)2 Success (com.linkedin.parseq.function.Success)2 Promise (com.linkedin.parseq.promise.Promise)2 TerminationPolicy (com.linkedin.parseq.retry.termination.TerminationPolicy)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 InterfaceType (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor.InterfaceType)2 IOException (java.io.IOException)2