Search in sources :

Example 16 with ReadableStreamChannel

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

the class FrontendTestUrlSigningServiceFactory method submitResponseTest.

/**
 * Tests
 * {@link FrontendRestRequestService#submitResponse(RestRequest, RestResponseChannel, ReadableStreamChannel, Exception)}.
 * @throws JSONException
 * @throws UnsupportedEncodingException
 * @throws URISyntaxException
 */
@Test
public void submitResponseTest() throws JSONException, UnsupportedEncodingException, URISyntaxException {
    String exceptionMsg = TestUtils.getRandomString(10);
    responseHandler.shutdown();
    // handleResponse of FrontendTestResponseHandler throws exception because it has been shutdown.
    try {
        // there is an exception already.
        RestRequest restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        assertTrue("RestRequest channel is not open", restRequest.isOpen());
        MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, new RuntimeException(exceptionMsg));
        assertEquals("Unexpected exception message", exceptionMsg, restResponseChannel.getException().getMessage());
        // there is no exception and exception thrown when the response is submitted.
        restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        assertTrue("RestRequest channel is not open", restRequest.isOpen());
        restResponseChannel = new MockRestResponseChannel();
        ReadableStreamChannel response = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
        assertTrue("Response channel is not open", response.isOpen());
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, response, null);
        assertNotNull("There is no cause of failure", restResponseChannel.getException());
        // resources should have been cleaned up.
        assertFalse("Response channel is not cleaned up", response.isOpen());
    } finally {
        frontendRestRequestService.setupResponseHandler(responseHandler);
        responseHandler.start();
    }
    // verify tracking infos are attached accordingly.
    RestRequest restRequest;
    MockRestResponseChannel restResponseChannel;
    for (String header : RestUtils.TrackingHeaders.TRACKING_HEADERS) {
        restRequest = createRestRequest(RestMethod.GET, "/", null, null);
        restResponseChannel = new MockRestResponseChannel();
        frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
        assertTrue("Response header should not contain tracking info", restResponseChannel.getHeader(header) == null);
    }
    restRequest = createRestRequest(RestMethod.GET, "/", null, null);
    restRequest.setArg(RestUtils.InternalKeys.SEND_TRACKING_INFO, new Boolean(true));
    restResponseChannel = new MockRestResponseChannel();
    frontendRestRequestService.submitResponse(restRequest, restResponseChannel, null, null);
    assertEquals("Unexpected or missing tracking info", datacenterName, restResponseChannel.getHeader(RestUtils.TrackingHeaders.DATACENTER_NAME));
    assertEquals("Unexpected or missing tracking info", hostname, restResponseChannel.getHeader(RestUtils.TrackingHeaders.FRONTEND_NAME));
}
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 17 with ReadableStreamChannel

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

the class FrontendTestUrlSigningServiceFactory method verifyResponsePathAccountAndContainerInjection.

/**
 * Test response path account and container injection for V1 blob IDs.
 * @param serviceId the service ID for the blob.
 * @param isPrivate {@code true} if the blob is private.
 * @param expectedAccount the expected {@link Account} to verify its presence in {@link RestRequest}.
 * @param expectedContainer the expected {@link Container} to verify its presence in {@link RestRequest}.
 * @throws Exception
 */
private void verifyResponsePathAccountAndContainerInjection(String serviceId, boolean isPrivate, Account expectedAccount, Container expectedContainer) throws Exception {
    BlobProperties blobProperties = new BlobProperties(0, serviceId, "owner", "image/gif", isPrivate, Utils.Infinite_Time, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, false, null, null, null);
    ReadableStreamChannel content = new ByteBufferReadableStreamChannel(ByteBuffer.allocate(0));
    String blobId = router.putBlobWithIdVersion(blobProperties, new byte[0], content, BlobId.BLOB_ID_V1).get();
    verifyAccountAndContainerFromBlobId(blobId, expectedAccount, expectedContainer, null);
}
Also used : ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) ByteBufferReadableStreamChannel(com.github.ambry.commons.ByteBufferReadableStreamChannel) BlobProperties(com.github.ambry.messageformat.BlobProperties)

Example 18 with ReadableStreamChannel

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

the class GetAccountsHandlerTest method validRequestsTest.

/**
 * Test valid request cases.
 * @throws Exception
 */
@Test
public void validRequestsTest() throws Exception {
    Account account = accountService.createAndAddRandomAccount();
    ThrowingBiConsumer<RestRequest, Collection<Account>> testAction = (request, expectedAccounts) -> {
        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("Accounts do not match", new HashSet<>(expectedAccounts), new HashSet<>(AccountCollectionSerde.accountsFromInputStreamInJson(asyncWritableChannel.consumeContentAsInputStream())));
    };
    testAction.accept(createRestRequest(null, null, null, Operations.ACCOUNTS), accountService.getAllAccounts());
    testAction.accept(createRestRequest(account.getName(), null, null, Operations.ACCOUNTS), Collections.singleton(account));
    testAction.accept(createRestRequest(null, Short.toString(account.getId()), null, Operations.ACCOUNTS), Collections.singleton(account));
}
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) 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) Collection(java.util.Collection) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with ReadableStreamChannel

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

the class TailoredPeersClusterMap method handleGoodCaseTest.

/**
 * Tests for the good cases where the datanodes are correct. The peers obtained from the response is compared
 * against the ground truth.
 * @throws Exception
 */
@Test
public void handleGoodCaseTest() throws Exception {
    for (String datanode : TailoredPeersClusterMap.DATANODE_NAMES) {
        RestRequest restRequest = getRestRequest(datanode);
        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)));
        Set<String> expectedPeers = clusterMap.getPeers(datanode);
        Set<String> peersFromResponse = getPeersFromResponse(RestTestUtils.getJsonizedResponseBody(channel));
        assertEquals("Peer list returned does not match expected for " + datanode, expectedPeers, peersFromResponse);
    }
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) 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 20 with ReadableStreamChannel

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

the class TailoredPeersClusterMap method sendRequestGetResponse.

/**
 * Sends the given {@link RestRequest} to the {@link GetPeersHandler} and waits for the response and returns it.
 * @param restRequest the {@link RestRequest} to send.
 * @param restResponseChannel the {@link RestResponseChannel} where headers will be set.
 * @return the response body as a {@link ReadableStreamChannel}.
 * @throws Exception
 */
private ReadableStreamChannel sendRequestGetResponse(RestRequest restRequest, RestResponseChannel restResponseChannel) throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
    final AtomicReference<ReadableStreamChannel> channelRef = new AtomicReference<>();
    getPeersHandler.handle(restRequest, restResponseChannel, new Callback<ReadableStreamChannel>() {

        @Override
        public void onCompletion(ReadableStreamChannel result, Exception exception) {
            channelRef.set(result);
            exceptionRef.set(exception);
            latch.countDown();
        }
    });
    assertTrue("Latch did not count down in time", latch.await(1, TimeUnit.SECONDS));
    if (exceptionRef.get() != null) {
        throw exceptionRef.get();
    }
    return channelRef.get();
}
Also used : ReadableStreamChannel(com.github.ambry.router.ReadableStreamChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) RestServiceException(com.github.ambry.rest.RestServiceException)

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