Search in sources :

Example 11 with ReadableStreamChannel

use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.

the class MockRouter method getBlob.

@Override
public Future<GetBlobResult> getBlob(String blobId, GetBlobOptions options, Callback<GetBlobResult> callback, QuotaChargeCallback quotaChargeCallback) {
    lock.lock();
    try {
        BlobInfoAndData blob = allBlobs.get(blobId);
        FutureResult<GetBlobResult> future = new FutureResult<>();
        if (blob == null) {
            Exception e = new RouterException("NotFound", RouterErrorCode.BlobDoesNotExist);
            future.done(null, e);
            if (callback != null) {
                callback.onCompletion(null, e);
            }
            return future;
        }
        // Discard the options and only return the BlobAll.
        ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(blob.getData()));
        GetBlobResult result = new GetBlobResult(blob.getBlobInfo(), channel);
        future.done(result, null);
        if (callback != null) {
            callback.onCompletion(result, null);
        }
        return future;
    } finally {
        lock.unlock();
    }
}
Also used : RouterException(com.github.ambry.router.RouterException) GetBlobResult(com.github.ambry.router.GetBlobResult) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) FutureResult(com.github.ambry.router.FutureResult) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException)

Example 12 with ReadableStreamChannel

use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.

the class MockHeadCallback method shouldProceed.

/**
 * Handles argument pre-checks and examines the URL to see if any custom operations need to be performed (which might
 * involve throwing exceptions).
 * <p/>
 * Also blocks if required.
 * @param restRequest the {@link RestRequest} that needs to be handled.
 * @param restResponseChannel the {@link RestResponseChannel} that can be used to set headers.
 * @return {@code true} if the pre-checks decided it is OK to continue. Otherwise {@code false}.
 */
private boolean shouldProceed(RestRequest restRequest, RestResponseChannel restResponseChannel) {
    if (blocking) {
        try {
            blockLatch.await();
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }
    boolean shouldProceed = canHonorRequest(restRequest, restResponseChannel);
    if (shouldProceed) {
        String uri = restRequest.getUri();
        ReadableStreamChannel response = null;
        Exception exception = null;
        if (uri.startsWith(ECHO_REST_METHOD)) {
            String responseStr = restRequest.getRestMethod().toString() + uri.substring(ECHO_REST_METHOD.length());
            ByteBuffer buffer = ByteBuffer.wrap(responseStr.getBytes());
            response = new ByteBufferRSC(buffer);
            shouldProceed = false;
        } else if (THROW_RUNTIME_EXCEPTION.equals(uri)) {
            throw new RuntimeException(THROW_RUNTIME_EXCEPTION);
        } else if (SEND_RESPONSE_RUNTIME_EXCEPTION.equals(uri)) {
            shouldProceed = false;
            exception = new RuntimeException(SEND_RESPONSE_RUNTIME_EXCEPTION);
        } else if (SEND_RESPONSE_REST_SERVICE_EXCEPTION.equals(uri)) {
            shouldProceed = false;
            RestServiceErrorCode errorCode = RestServiceErrorCode.InternalServerError;
            try {
                errorCode = RestServiceErrorCode.valueOf(verifiableProperties.getString(REST_ERROR_CODE));
            } catch (IllegalArgumentException e) {
            // it's alright.
            }
            exception = new RestServiceException(SEND_RESPONSE_REST_SERVICE_EXCEPTION, errorCode);
        }
        if (!shouldProceed) {
            try {
                if (exception == null) {
                    restResponseChannel.setStatus(ResponseStatus.Ok);
                }
            } catch (RestServiceException e) {
                exception = e;
            } finally {
                handleResponse(restRequest, restResponseChannel, response, exception);
            }
        }
    }
    return shouldProceed;
}
Also used : ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException) ByteBufferRSC(com.github.ambry.router.ByteBufferRSC)

Example 13 with ReadableStreamChannel

use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.

the class NoSizeRSC method readPartByPartTest.

/**
 * Tests reading {@link ReadableStreamChannelInputStream} part by part.
 * @param in the data that the {@link ReadableStreamChannelInputStream} should contain.
 * @throws Exception
 */
private void readPartByPartTest(byte[] in) throws Exception {
    // channel with size and one piece of content.
    ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
    InputStream stream = new ReadableStreamChannelInputStream(channel);
    doReadPartByPartTest(stream, in);
    stream.close();
    // channel with no size but one piece of content.
    channel = new NoSizeRSC(ByteBuffer.wrap(in));
    stream = new ReadableStreamChannelInputStream(channel);
    doReadPartByPartTest(stream, in);
    stream.close();
    // channel with no size and multiple pieces of content.
    List<ByteBuffer> contents = splitContent(in, CONTENT_SPLIT_PART_COUNT);
    contents.add(null);
    channel = new MockRestRequest(MockRestRequest.DUMMY_DATA, contents);
    stream = new ReadableStreamChannelInputStream(channel);
    doReadPartByPartTest(stream, in);
    stream.close();
}
Also used : ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) InputStream(java.io.InputStream) MockRestRequest(com.github.ambry.rest.MockRestRequest) ByteBuffer(java.nio.ByteBuffer)

Example 14 with ReadableStreamChannel

use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.

the class NoSizeRSC method readErrorCasesTest.

/**
 * Tests cases where read() methods get incorrect input.
 * @throws IOException
 */
@Test
public void readErrorCasesTest() throws IOException {
    byte[] in = new byte[1024];
    new Random().nextBytes(in);
    ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(in));
    InputStream dstInputStream = new ReadableStreamChannelInputStream(channel);
    try {
        dstInputStream.read(null, 0, in.length);
        fail("The read should have failed");
    } catch (NullPointerException e) {
    // expected. nothing to do.
    }
    byte[] out = new byte[in.length];
    try {
        dstInputStream.read(out, -1, out.length);
        fail("The read should have failed");
    } catch (IndexOutOfBoundsException e) {
    // expected. nothing to do.
    }
    try {
        dstInputStream.read(out, 0, -1);
        fail("The read should have failed");
    } catch (IndexOutOfBoundsException e) {
    // expected. nothing to do.
    }
    try {
        dstInputStream.read(out, 0, out.length + 1);
        fail("The read should have failed");
    } catch (IndexOutOfBoundsException e) {
    // expected. nothing to do.
    }
    assertEquals("Bytes read should have been 0 because passed len was 0", 0, dstInputStream.read(out, 0, 0));
}
Also used : Random(java.util.Random) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 15 with ReadableStreamChannel

use of com.github.ambry.router.ReadableStreamChannel in project ambry by linkedin.

the class NoSizeRSC method incompleteReadsTest.

/**
 * Tests for the case when reads are incomplete either because exceptions were thrown or the read simply did not
 * complete.
 * @throws IOException
 */
@Test
public void incompleteReadsTest() throws IOException {
    // Exception during read
    String exceptionMsg = "@@randomMsg@@";
    Exception exceptionToThrow = new Exception(exceptionMsg);
    ReadableStreamChannel channel = new IncompleteReadReadableStreamChannel(exceptionToThrow);
    InputStream inputStream = new ReadableStreamChannelInputStream(channel);
    try {
        inputStream.read();
        fail("The read should have failed");
    } catch (Exception e) {
        while (e.getCause() != null) {
            e = (Exception) e.getCause();
        }
        assertEquals("Exception messages do not match", exceptionMsg, e.getMessage());
    }
    // incomplete read
    channel = new IncompleteReadReadableStreamChannel(null);
    inputStream = new ReadableStreamChannelInputStream(channel);
    try {
        inputStream.read();
        fail("The read should have failed");
    } catch (IllegalStateException e) {
    // expected. Nothing to do.
    }
}
Also used : ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) InputStream(java.io.InputStream) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)29 Test (org.junit.Test)14 MockRestRequest (com.github.ambry.rest.MockRestRequest)13 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)11 RestRequest (com.github.ambry.rest.RestRequest)10 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)9 IOException (java.io.IOException)8 ByteBuffer (java.nio.ByteBuffer)8 RestResponseChannel (com.github.ambry.rest.RestResponseChannel)7 InputStream (java.io.InputStream)7 RestServiceException (com.github.ambry.rest.RestServiceException)6 JSONObject (org.json.JSONObject)6 FutureResult (com.github.ambry.router.FutureResult)5 RetainingAsyncWritableChannel (com.github.ambry.commons.RetainingAsyncWritableChannel)4 RequestPath (com.github.ambry.rest.RequestPath)4 RestMethod (com.github.ambry.rest.RestMethod)4 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)4 RestUtils (com.github.ambry.rest.RestUtils)4 RouterException (com.github.ambry.router.RouterException)4 ThrowingConsumer (com.github.ambry.utils.ThrowingConsumer)4