Search in sources :

Example 11 with Response

use of com.yahoo.processing.Response in project vespa by vespa-engine.

the class AsyncExecutionTestCase method testAsyncExecution.

/**
 * Execute a processing chain which forks off into multiple threads
 */
@Test
public void testAsyncExecution() {
    // Create a chain
    Chain<Processor> chain = new Chain<>(new CombineData(), new BlockingSplitter(2), new Get6DataItems(), new DataSource());
    // Execute it
    Request request = new Request();
    request.properties().set("appendage", 1);
    Response response = Execution.createRoot(chain, 0, Execution.Environment.createEmpty()).process(request);
    // Verify the result
    assertEquals(6 * 2 - 1, response.data().asList().size());
    assertEquals("first.2, third.2", response.data().get(0).toString());
    assertEquals("second.2", response.data().get(1).toString());
    assertEquals("first.3", response.data().get(2).toString());
    assertEquals("second.3", response.data().get(3).toString());
    assertEquals("third.3", response.data().get(4).toString());
    // from the parallel execution
    assertEquals("first.2", response.data().get(5).toString());
    assertEquals("second.2", response.data().get(6).toString());
    assertEquals("third.2", response.data().get(7).toString());
    assertEquals("first.3", response.data().get(8).toString());
    assertEquals("second.3", response.data().get(9).toString());
    assertEquals("third.3", response.data().get(10).toString());
}
Also used : Response(com.yahoo.processing.Response) Chain(com.yahoo.component.chain.Chain) Processor(com.yahoo.processing.Processor) Request(com.yahoo.processing.Request) Test(org.junit.Test)

Example 12 with Response

use of com.yahoo.processing.Response in project vespa by vespa-engine.

the class JDiscContainerProcessingTest method requireThatBasicProcessingWorks.

@Test
public void requireThatBasicProcessingWorks() {
    try (JDisc container = getContainerWithRot13()) {
        Processing processing = container.processing();
        Request req = new Request();
        req.properties().set("title", "Good day!");
        Response response = processing.process(ComponentSpecification.fromString("foo"), req);
        assertThat(response.data().get(0).toString(), equalTo("Tbbq qnl!"));
    }
}
Also used : Response(com.yahoo.processing.Response) Request(com.yahoo.processing.Request) Test(org.junit.Test)

Example 13 with Response

use of com.yahoo.processing.Response in project vespa by vespa-engine.

the class AsynchronousSectionedRendererTest method render.

@SuppressWarnings("unchecked")
public String render(Renderer renderer, DataList data) throws InterruptedException, IOException {
    TestContentChannel contentChannel = new TestContentChannel();
    Execution execution = Execution.createRoot(new NoopProcessor(), 0, null);
    final ContentChannelOutputStream stream = new ContentChannelOutputStream(contentChannel);
    ListenableFuture result = renderer.render(stream, new Response(data), execution, null);
    int waitCounter = 1000;
    while (!result.isDone()) {
        Thread.sleep(60);
        --waitCounter;
        if (waitCounter < 0) {
            throw new IllegalStateException();
        }
    }
    stream.close();
    contentChannel.close(null);
    String str = "";
    for (ByteBuffer buf : contentChannel.getBuffers()) {
        str += Utf8.toString(buf);
    }
    return str;
}
Also used : Response(com.yahoo.processing.Response) Execution(com.yahoo.processing.execution.Execution) ContentChannelOutputStream(com.yahoo.container.jdisc.ContentChannelOutputStream) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ByteBuffer(java.nio.ByteBuffer)

Example 14 with Response

use of com.yahoo.processing.Response in project vespa by vespa-engine.

the class AbstractProcessingHandler method handle.

@Override
@SuppressWarnings("unchecked")
public HttpResponse handle(HttpRequest request, ContentChannel channel) {
    com.yahoo.processing.Request processingRequest = new com.yahoo.processing.Request();
    populate("", request.propertyMap(), processingRequest.properties());
    populate("context", request.getJDiscRequest().context(), processingRequest.properties());
    processingRequest.properties().set(Request.JDISC_REQUEST, request);
    FreezeListener freezeListener = new FreezeListener(processingRequest, renderers, defaultRenderer, channel, renderingExecutor);
    processingRequest.properties().set(freezeListenerKey, freezeListener);
    Chain<COMPONENT> chain = chainRegistry.getComponent(resolveChainId(processingRequest.properties()));
    if (chain == null)
        throw new IllegalArgumentException("Chain '" + processingRequest.properties().get("chain") + "' not found");
    Execution execution = createExecution(chain, processingRequest);
    freezeListener.setExecution(execution);
    Response processingResponse = execution.process(processingRequest);
    return freezeListener.getHttpResponse(processingResponse);
}
Also used : Response(com.yahoo.processing.Response) HttpResponse(com.yahoo.container.jdisc.HttpResponse) Execution(com.yahoo.processing.execution.Execution) Request(com.yahoo.processing.Request) HttpRequest(com.yahoo.container.jdisc.HttpRequest) Request(com.yahoo.processing.Request)

Example 15 with Response

use of com.yahoo.processing.Response in project vespa by vespa-engine.

the class AsyncExecution method waitForAll.

/*
     * Waits for all futures until the given timeout. If a FutureResult isn't
     * done when the timeout expires, it will be cancelled, and it will return a
     * response. All unfinished Futures will be cancelled.
     *
     * @return the list of responses in the same order as returned from the task collection
     */
// Note that this may also be achieved using guava Futures. Not sure if this should be deprecated because of it.
public static List<Response> waitForAll(final Collection<FutureResponse> tasks, final long timeout) {
    // Copy the list in case it is modified while we are waiting
    final List<FutureResponse> workingTasks = new ArrayList<>(tasks);
    @SuppressWarnings({ "rawtypes", "unchecked" }) final Future task = getFuture(new Callable() {

        @Override
        public List<Future> call() {
            for (final FutureResponse task : workingTasks) {
                task.get();
            }
            return null;
        }
    });
    try {
        task.get(timeout, TimeUnit.MILLISECONDS);
    } catch (final TimeoutException | InterruptedException | ExecutionException e) {
    // Handle timeouts below
    }
    final List<Response> responses = new ArrayList<>(tasks.size());
    for (final FutureResponse future : workingTasks) responses.add(getTaskResponse(future));
    return responses;
}
Also used : ArrayList(java.util.ArrayList) FutureResponse(com.yahoo.processing.response.FutureResponse) Response(com.yahoo.processing.Response) FutureResponse(com.yahoo.processing.response.FutureResponse) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Response (com.yahoo.processing.Response)20 Request (com.yahoo.processing.Request)12 Processor (com.yahoo.processing.Processor)11 Test (org.junit.Test)11 Chain (com.yahoo.component.chain.Chain)10 Execution (com.yahoo.processing.execution.Execution)4 DataList (com.yahoo.processing.response.DataList)3 FutureResponse (com.yahoo.processing.response.FutureResponse)2 ProcessorLibrary (com.yahoo.processing.test.ProcessorLibrary)2 ArrayList (java.util.ArrayList)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ContentChannelOutputStream (com.yahoo.container.jdisc.ContentChannelOutputStream)1 HttpRequest (com.yahoo.container.jdisc.HttpRequest)1 HttpResponse (com.yahoo.container.jdisc.HttpResponse)1 HttpRequest (com.yahoo.jdisc.http.HttpRequest)1 AsyncExecution (com.yahoo.processing.execution.AsyncExecution)1 ArrayDataList (com.yahoo.processing.response.ArrayDataList)1 IncomingData (com.yahoo.processing.response.IncomingData)1 AsyncDataProcessingInitiator (com.yahoo.processing.test.documentation.AsyncDataProcessingInitiator)1 AsyncDataProducer (com.yahoo.processing.test.documentation.AsyncDataProducer)1