Search in sources :

Example 6 with ReadableStreamChannel

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

the class UndeleteHandlerTest method setupBlob.

/**
 * Setup a blob and delete it so later we can undelete it.
 * @throws Exception
 */
public void setupBlob() throws Exception {
    ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.wrap(BLOB_DATA));
    blobId = router.putBlob(BLOB_PROPERTIES, new byte[0], channel, new PutBlobOptionsBuilder().build()).get(1, TimeUnit.SECONDS);
    idConverterFactory.translation = blobId;
    router.deleteBlob(blobId, SERVICE_ID).get(1, TimeUnit.SECONDS);
}
Also used : ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) PutBlobOptionsBuilder(com.github.ambry.router.PutBlobOptionsBuilder)

Example 7 with ReadableStreamChannel

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

the class GetClusterMapSnapshotHandlerTest method handleGoodCaseTest.

/**
 * Handles the case where everything works as expected
 * @throws Exception
 */
@Test
public void handleGoodCaseTest() throws Exception {
    RestRequest restRequest = createRestRequest();
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    ReadableStreamChannel channel = sendRequestGetResponse(restRequest, restResponseChannel);
    assertNotNull("There should be a response", channel);
    Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
    assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
    assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
    JSONObject expected = clusterMap.getSnapshot();
    JSONObject actual = RestTestUtils.getJsonizedResponseBody(channel);
    // remove timestamps because they may differ
    expected.remove(ClusterMapSnapshotConstants.TIMESTAMP_MS);
    actual.remove(ClusterMapSnapshotConstants.TIMESTAMP_MS);
    assertEquals("Snapshot does not match expected", expected.toString(), actual.toString());
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test)

Example 8 with ReadableStreamChannel

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

the class GetAccountsHandlerTest method getSingleContainerSuccessTest.

/**
 * Test success case of getting single container.
 * @throws Exception
 */
@Test
public void getSingleContainerSuccessTest() throws Exception {
    Account existingAccount = accountService.createAndAddRandomAccount();
    Container existingContainer = existingAccount.getAllContainers().iterator().next();
    ThrowingBiConsumer<RestRequest, Container> testAction = (request, expectedContainer) -> {
        RestResponseChannel restResponseChannel = new MockRestResponseChannel();
        ReadableStreamChannel channel = sendRequestGetResponse(request, restResponseChannel);
        assertNotNull("There should be a response", channel);
        Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
        assertEquals("Content-type is not as expected", RestUtils.JSON_CONTENT_TYPE, restResponseChannel.getHeader(RestUtils.Headers.CONTENT_TYPE));
        assertEquals("Content-length is not as expected", channel.getSize(), Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
        RetainingAsyncWritableChannel asyncWritableChannel = new RetainingAsyncWritableChannel((int) channel.getSize());
        channel.readInto(asyncWritableChannel, null).get();
        assertEquals("Container does not match", Collections.singletonList(expectedContainer), AccountCollectionSerde.containersFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream(), existingAccount.getId()));
    };
    testAction.accept(createRestRequest(existingAccount.getName(), null, existingContainer.getName(), Operations.ACCOUNTS_CONTAINERS), existingContainer);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FutureResult(com.github.ambry.router.FutureResult) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) AccountCollectionSerde(com.github.ambry.account.AccountCollectionSerde) RequestPath(com.github.ambry.rest.RequestPath) HashSet(java.util.HashSet) JSONObject(org.json.JSONObject) TestUtils(com.github.ambry.utils.TestUtils) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) Container(com.github.ambry.account.Container) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) RestMethod(com.github.ambry.rest.RestMethod) Collection(java.util.Collection) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Test(org.junit.Test) ThrowingBiConsumer(com.github.ambry.utils.ThrowingBiConsumer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Account(com.github.ambry.account.Account) RestUtils(com.github.ambry.rest.RestUtils) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test)

Example 9 with ReadableStreamChannel

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

the class BadRestRequest method multipleResponsesInFlightTest.

/**
 * Tests various functions when multiple responses are in flight.
 * @throws Exception
 */
@Test
public void multipleResponsesInFlightTest() throws Exception {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    AsyncRequestResponseHandler responseHandler = getAsyncRequestResponseHandler(0);
    responseHandler.start();
    try {
        final int EXPECTED_QUEUE_SIZE = 5;
        // test for duplicate request submission
        // submit a few responses that halt on read
        CountDownLatch releaseRead = new CountDownLatch(1);
        byte[] data = null;
        RestRequest restRequest = null;
        MockRestResponseChannel restResponseChannel = null;
        ReadableStreamChannel response = null;
        for (int i = 0; i < EXPECTED_QUEUE_SIZE; i++) {
            data = TestUtils.getRandomBytes(32);
            response = new HaltingRSC(ByteBuffer.wrap(data), releaseRead, executorService);
            restRequest = createRestRequest(RestMethod.GET, "/", null, null);
            restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
            restResponseChannel = new MockRestResponseChannel(restRequest);
            responseHandler.handleResponse(restRequest, restResponseChannel, response, null);
        }
        assertEquals("Response set size unexpected", EXPECTED_QUEUE_SIZE, responseHandler.getResponseSetSize());
        // attach a listener to the last MockRestResponseChannel
        EventMonitor<MockRestResponseChannel.Event> eventMonitor = new EventMonitor<MockRestResponseChannel.Event>(MockRestResponseChannel.Event.OnRequestComplete);
        restResponseChannel.addListener(eventMonitor);
        // we try to re submit the last response. This should fail.
        try {
            responseHandler.handleResponse(restRequest, restResponseChannel, response, null);
            fail("Trying to resubmit a response that is already in flight should have failed");
        } catch (RestServiceException e) {
            assertEquals("Unexpected error code", RestServiceErrorCode.RequestResponseQueuingFailure, e.getErrorCode());
        }
        assertEquals("Response set size unexpected", EXPECTED_QUEUE_SIZE, responseHandler.getResponseSetSize());
        releaseRead.countDown();
        if (!eventMonitor.awaitEvent(1, TimeUnit.SECONDS)) {
            fail("awaitResponse took too long. There might be a problem or the timeout may need to be increased");
        }
        // compare the output. We care only about the last one because we want to make sure the duplicate submission
        // did not mess with the original
        assertArrayEquals("Unexpected data in the response", data, restResponseChannel.getResponseBody());
        // test for shutdown when responses are still in progress
        releaseRead = new CountDownLatch(1);
        List<MockRestResponseChannel> responseChannels = new ArrayList<MockRestResponseChannel>(EXPECTED_QUEUE_SIZE);
        for (int i = 0; i < EXPECTED_QUEUE_SIZE; i++) {
            response = new HaltingRSC(ByteBuffer.allocate(0), releaseRead, executorService);
            restRequest = createRestRequest(RestMethod.GET, "/", null, null);
            restRequest.getMetricsTracker().scalingMetricsTracker.markRequestReceived();
            restResponseChannel = new MockRestResponseChannel(restRequest);
            responseHandler.handleResponse(restRequest, restResponseChannel, response, null);
            responseChannels.add(restResponseChannel);
        }
        assertEquals("Response set size unexpected", EXPECTED_QUEUE_SIZE, responseHandler.getResponseSetSize());
        responseHandler.shutdown();
        assertEquals("Response set size unexpected", 0, responseHandler.getResponseSetSize());
        releaseRead.countDown();
        // all of the responses should have exceptions
        for (MockRestResponseChannel channel : responseChannels) {
            assertNotNull("There should be an exception", channel.getException());
            if (channel.getException() instanceof RestServiceException) {
                RestServiceException rse = (RestServiceException) channel.getException();
                assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.ServiceUnavailable, rse.getErrorCode());
            } else {
                throw channel.getException();
            }
        }
    } finally {
        responseHandler.shutdown();
        executorService.shutdown();
    }
}
Also used : ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 10 with ReadableStreamChannel

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

the class MockHeadCallback method onCompletion.

/**
 * If there was no exception, sets headers and submits response.
 * @param result The result of the request - a {@link GetBlobResult} object with the {@link BlobInfo} containing the
 *               blob properties and other headers of the blob, and the {@link ReadableStreamChannel} of blob data.
 *               This is non null if the request executed successfully.
 * @param exception The exception that was reported on execution of the request (if any).
 */
@Override
public void onCompletion(GetBlobResult result, Exception exception) {
    try {
        restResponseChannel.setHeader(RestUtils.Headers.DATE, new GregorianCalendar().getTime());
        if (exception == null && result != null) {
            setResponseHeaders(result.getBlobInfo());
        } else if (exception != null && exception instanceof RouterException) {
            exception = new RestServiceException(exception, RestServiceErrorCode.getRestServiceErrorCode(((RouterException) exception).getErrorCode()));
        }
    } catch (Exception e) {
        exception = exception == null ? e : exception;
    } finally {
        ReadableStreamChannel channel = result != null ? result.getBlobDataChannel() : null;
        mockRestRequestService.handleResponse(restRequest, restResponseChannel, channel, exception);
    }
}
Also used : RouterException(com.github.ambry.router.RouterException) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) GregorianCalendar(java.util.GregorianCalendar) IOException(java.io.IOException) RouterException(com.github.ambry.router.RouterException)

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