Search in sources :

Example 1 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 2 with ReadableStreamChannel

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

the class FrontendTestUrlSigningServiceFactory method releaseResourcesTest.

/**
 * Tests releasing of resources if response submission fails.
 * @throws JSONException
 * @throws UnsupportedEncodingException
 * @throws URISyntaxException
 */
@Test
public void releaseResourcesTest() throws JSONException, UnsupportedEncodingException, URISyntaxException {
    responseHandler.shutdown();
    // handleResponse of FrontendTestResponseHandler throws exception because it has been shutdown.
    try {
        RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        ReadableStreamChannel channel = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
        assertTrue("RestRequest channel not open", restRequest.isOpen());
        assertTrue("ReadableStreamChannel not open", channel.isOpen());
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, channel, null);
        assertFalse("ReadableStreamChannel is still open", channel.isOpen());
        // null ReadableStreamChannel
        restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        restResponseChannel = new MockRestResponseChannel();
        assertTrue("RestRequest channel not open", restRequest.isOpen());
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
        // bad RestRequest (close() throws IOException)
        channel = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
        restResponseChannel = new MockRestResponseChannel();
        assertTrue("ReadableStreamChannel not open", channel.isOpen());
        frontendRestRequestService.submitResponse(new BadRestRequest(), restResponseChannel, channel, null);
        // bad ReadableStreamChannel (close() throws IOException)
        restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        restResponseChannel = new MockRestResponseChannel();
        assertTrue("RestRequest channel not open", restRequest.isOpen());
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, new BadRSC(), null);
    } finally {
        frontendRestRequestService.setupResponseHandler(responseHandler);
        responseHandler.start();
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 3 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 4 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 5 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)

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