Search in sources :

Example 1 with BufferedContentChannel

use of com.yahoo.jdisc.handler.BufferedContentChannel in project vespa by vespa-engine.

the class ThreadedRequestHandler method handleRequest.

/**
 * Handles a request by assigning a worker thread to it.
 *
 * @throws OverloadException if thread pool has no available thread
 */
@Override
public final ContentChannel handleRequest(Request request, ResponseHandler responseHandler) {
    metric.add("handled.requests", 1, contextFor(request.getBindingMatch()));
    if (request.getTimeout(TimeUnit.SECONDS) == null) {
        Duration timeout = getTimeout();
        if (timeout != null) {
            request.setTimeout(timeout.getSeconds(), TimeUnit.SECONDS);
        }
    }
    BufferedContentChannel content = new BufferedContentChannel();
    final RequestTask command = new RequestTask(request, content, responseHandler);
    try {
        executor.execute(command);
    } catch (RejectedExecutionException e) {
        command.failOnOverload();
        throw new OverloadException("No available threads for " + getClass().getSimpleName(), e);
    } finally {
        logRejectedRequests();
    }
    return content;
}
Also used : OverloadException(com.yahoo.jdisc.handler.OverloadException) Duration(java.time.Duration) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 2 with BufferedContentChannel

use of com.yahoo.jdisc.handler.BufferedContentChannel in project vespa by vespa-engine.

the class StateHandlerTest method requestAsString.

private String requestAsString(String requestUri) throws Exception {
    final BufferedContentChannel content = new BufferedContentChannel();
    Response response = driver.dispatchRequest(requestUri, new ResponseHandler() {

        @Override
        public ContentChannel handleResponse(Response response) {
            return content;
        }
    }).get(60, TimeUnit.SECONDS);
    assertNotNull(response);
    assertEquals(Response.Status.OK, response.getStatus());
    StringBuilder str = new StringBuilder();
    Reader in = new InputStreamReader(content.toStream(), StandardCharsets.UTF_8);
    for (int c; (c = in.read()) != -1; ) {
        str.append((char) c);
    }
    return str.toString();
}
Also used : Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel)

Example 3 with BufferedContentChannel

use of com.yahoo.jdisc.handler.BufferedContentChannel in project vespa by vespa-engine.

the class LoggingRequestHandlerTestCase method checkStartIsNotZeroWithoutTimingInstance.

@Test
public final void checkStartIsNotZeroWithoutTimingInstance() throws InterruptedException {
    Long startTime;
    MockResponseHandler responseHandler = new MockResponseHandler();
    com.yahoo.jdisc.http.HttpRequest request = createRequest();
    BufferedContentChannel requestContent = new BufferedContentChannel();
    requestContent.close(null);
    handler.handleRequest(request, requestContent, responseHandler);
    startTime = accessLogging.starts.poll(5, TimeUnit.MINUTES);
    if (startTime == null) {
    // test timed out, ignoring
    } else {
        assertFalse("Start time was 0,  that should never happen after the first millisecond of 1970.", startTime.longValue() == 0L);
    }
}
Also used : BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel) Test(org.junit.Test)

Example 4 with BufferedContentChannel

use of com.yahoo.jdisc.handler.BufferedContentChannel in project vespa by vespa-engine.

the class VipStatusHandlerTestCase method testFileFound.

@Test
public final void testFileFound() throws IOException {
    final File statusFile = File.createTempFile("VipStatusHandlerTestCase", null);
    try {
        final FileWriter writer = new FileWriter(statusFile);
        final String OK = "OK\n";
        writer.write(OK);
        writer.close();
        final VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(true).statusfile(statusFile.getAbsolutePath()).noSearchBackendsImpliesOutOfService(false));
        final VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config, metric);
        final MockResponseHandler responseHandler = new MockResponseHandler();
        final HttpRequest request = createRequest();
        final BufferedContentChannel requestContent = createChannel();
        handler.handleRequest(request, requestContent, responseHandler);
        final ByteBuffer b = responseHandler.channel.read();
        final byte[] asBytes = new byte[b.remaining()];
        b.get(asBytes);
        assertEquals(OK, Utf8.toString(asBytes));
    } finally {
        statusFile.delete();
    }
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) FileWriter(java.io.FileWriter) VipStatusConfig(com.yahoo.container.core.VipStatusConfig) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with BufferedContentChannel

use of com.yahoo.jdisc.handler.BufferedContentChannel in project vespa by vespa-engine.

the class VipStatusHandlerTestCase method testProgrammaticallyRemovedFromRotation.

@Test
public final void testProgrammaticallyRemovedFromRotation() throws IOException {
    VipStatus vipStatus = new VipStatus();
    final VipStatusConfig config = new VipStatusConfig(new VipStatusConfig.Builder().accessdisk(false).noSearchBackendsImpliesOutOfService(true));
    final VipStatusHandler handler = new VipStatusHandler(Executors.newCachedThreadPool(), config, metric, vipStatus);
    vipStatus.removeFromRotation(this);
    {
        final MockResponseHandler responseHandler = new MockResponseHandler();
        final HttpRequest request = createRequest();
        final BufferedContentChannel requestContent = createChannel();
        handler.handleRequest(request, requestContent, responseHandler);
        final ByteBuffer b = responseHandler.channel.read();
        final byte[] asBytes = new byte[b.remaining()];
        b.get(asBytes);
        assertEquals(VipStatusHandler.StatusResponse.NO_SEARCH_BACKENDS, Utf8.toString(asBytes));
    }
    vipStatus.addToRotation(this);
    {
        final MockResponseHandler responseHandler = new MockResponseHandler();
        final HttpRequest request = createRequest();
        final BufferedContentChannel requestContent = createChannel();
        handler.handleRequest(request, requestContent, responseHandler);
        final ByteBuffer b = responseHandler.channel.read();
        final byte[] asBytes = new byte[b.remaining()];
        b.get(asBytes);
        assertEquals(VipStatusHandler.OK_MESSAGE, Utf8.toString(asBytes));
    }
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) VipStatusConfig(com.yahoo.container.core.VipStatusConfig) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

BufferedContentChannel (com.yahoo.jdisc.handler.BufferedContentChannel)8 Test (org.junit.Test)5 VipStatusConfig (com.yahoo.container.core.VipStatusConfig)4 HttpRequest (com.yahoo.jdisc.http.HttpRequest)4 ByteBuffer (java.nio.ByteBuffer)4 Response (com.yahoo.jdisc.Response)1 OverloadException (com.yahoo.jdisc.handler.OverloadException)1 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Duration (java.time.Duration)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1