Search in sources :

Example 11 with RestResponseChannel

use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.

the class GetSignedUrlHandlerTest method verifySigningUrl.

// handleGoodCaseTest() helpers
/**
 * Verifies that a singed URL is returned and it matches what is expected
 * @param restRequest the {@link RestRequest} to get a signed URL.
 * @param urlExpected the URL that should be returned.
 * @param expectedAccount the {@link Account} that should be populated in {@link RestRequest}.
 * @param expectedContainer the {@link Container} that should be populated in {@link RestRequest}.
 * @throws Exception
 */
private void verifySigningUrl(RestRequest restRequest, String urlExpected, Account expectedAccount, Container expectedContainer) throws Exception {
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    sendRequestGetResponse(restRequest, restResponseChannel);
    Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
    assertEquals("Content-length is not as expected", 0, Integer.parseInt((String) restResponseChannel.getHeader(RestUtils.Headers.CONTENT_LENGTH)));
    assertEquals("Signed URL is not as expected", urlExpected, restResponseChannel.getHeader(RestUtils.Headers.SIGNED_URL));
    assertEquals("Account not as expected", expectedAccount, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_ACCOUNT_KEY));
    assertEquals("Container not as expected", expectedContainer, restRequest.getArgs().get(RestUtils.InternalKeys.TARGET_CONTAINER_KEY));
}
Also used : RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel)

Example 12 with RestResponseChannel

use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.

the class SimpleAmbryCostModelPolicyTest method testCalculateRequestCost.

@Test
public void testCalculateRequestCost() throws Exception {
    SimpleAmbryCostModelPolicy requestCostPolicy = new SimpleAmbryCostModelPolicy();
    RestResponseChannel restResponseChannel = mock(RestResponseChannel.class);
    when(restResponseChannel.getHeader(anyString())).thenReturn(0);
    String blobUri = "/AAYIAQSSAAgAAQAAAAAAABpFymbGwe7sRBWYa5OPlkcNHQ.bin";
    // test for a 4 MB GET request.
    BlobInfo blobInfo = getBlobInfo(4 * MB);
    RestRequest restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
    Map<String, Double> costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyReadCost(costMap, 1);
    // test for a small GET request (fractional CU).
    blobInfo = getBlobInfo(6 * MB);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyReadCost(costMap, 2);
    // test for a GET request of blob of size 0.
    blobInfo = getBlobInfo(0);
    restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyReadCost(costMap, 1);
    // test for a small POST request (fractional storage cost).
    blobInfo = getBlobInfo(8 * MB);
    restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, 8 * MB);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyWriteCost(costMap, 2, 8 * 1024 * 1024 / (double) QuotaUtils.BYTES_IN_GB);
    // test for a large POST request.
    blobInfo = getBlobInfo(4 * GB);
    restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, 4 * GB);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyWriteCost(costMap, 1024, 4);
    // test for a POST request of blob of size 0.
    blobInfo = getBlobInfo(0);
    restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, 0);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyWriteCost(costMap, 1, 0);
    // test for a HEAD request.
    restRequest = createMockRequestWithMethod(RestMethod.HEAD, blobUri, -1);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyReadCost(costMap, 1);
    // test for a DELETE request.
    restRequest = createMockRequestWithMethod(RestMethod.DELETE, blobUri, -1);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyWriteCost(costMap, 1, 0.0);
    // test for a PUT request.
    restRequest = createMockRequestWithMethod(RestMethod.PUT, blobUri, -1);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyWriteCost(costMap, 1, 0.0);
    // test for PUT with null blob info.
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, null);
    verifyWriteCost(costMap, 1, 0.0);
    // test BlobInfo and UserMetadata GET requests
    blobInfo = getBlobInfo(40 * GB);
    restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri + "/BlobInfo", -1);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyReadCost(costMap, 1);
    restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri + "/UserMetadata", -1);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyReadCost(costMap, 1);
    // Plain GET should use blob size
    restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
    costMap = requestCostPolicy.calculateRequestCost(restRequest, restResponseChannel, blobInfo);
    verifyReadCost(costMap, 10240);
// TODO add a range request case with large range
}
Also used : RestRequest(com.github.ambry.rest.RestRequest) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) BlobInfo(com.github.ambry.messageformat.BlobInfo) Test(org.junit.Test)

Example 13 with RestResponseChannel

use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.

the class SimpleRequestQuotaCostPolicyTest method testCalculateRequestQuotaCharge.

@Test
public void testCalculateRequestQuotaCharge() throws Exception {
    QuotaConfig quotaConfig = new QuotaConfig(new VerifiableProperties(new Properties()));
    SimpleRequestQuotaCostPolicy quotaRequestCostPolicy = new SimpleRequestQuotaCostPolicy(quotaConfig);
    RestResponseChannel restResponseChannel = mock(RestResponseChannel.class);
    when(restResponseChannel.getHeader(anyString())).thenReturn(0);
    String blobUri = "/AAYIAQSSAAgAAQAAAAAAABpFymbGwe7sRBWYa5OPlkcNHQ.bin";
    // test for a 4 MB GET request.
    long blobSize = 4 * MB;
    RestRequest restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
    Map<String, Double> costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, blobSize);
    verifyReadCost(costMap, Math.ceil(blobSize / (double) quotaConfig.quotaAccountingUnit));
    // test for a small GET request (fractional CU).
    blobSize = 6 * MB;
    costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, blobSize);
    verifyReadCost(costMap, Math.ceil(blobSize / (double) quotaConfig.quotaAccountingUnit));
    // test for a GET request of blob of size 0.
    restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
    costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, 0);
    verifyReadCost(costMap, 1);
    // test for a GET request of blob of size 512.
    blobSize = 512;
    restRequest = createMockRequestWithMethod(RestMethod.GET, blobUri, -1);
    costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, blobSize);
    verifyReadCost(costMap, 1);
    // test for a small POST request (fractional storage cost).
    blobSize = 8 * MB;
    restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, blobSize);
    costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, blobSize);
    verifyWriteCost(costMap, Math.ceil(blobSize / (double) quotaConfig.quotaAccountingUnit), 8 * 1024 * 1024 / (double) QuotaUtils.BYTES_IN_GB);
    // test for a large POST request.
    blobSize = 4 * QuotaUtils.BYTES_IN_GB;
    restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, blobSize);
    costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, blobSize);
    verifyWriteCost(costMap, Math.ceil(blobSize / (double) quotaConfig.quotaAccountingUnit), 4);
    // test for a POST request of blob of size 0.
    restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, 0);
    costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, 0);
    verifyWriteCost(costMap, 1, 0);
    // test for a POST request of blob of size 512.
    restRequest = createMockRequestWithMethod(RestMethod.POST, blobUri, 0);
    costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, 512);
    verifyWriteCost(costMap, 1, 0);
    // test for a HEAD request.
    restRequest = createMockRequestWithMethod(RestMethod.HEAD, blobUri, -1);
    costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, quotaConfig.quotaAccountingUnit);
    verifyReadCost(costMap, 1);
    // test for a DELETE request.
    restRequest = createMockRequestWithMethod(RestMethod.DELETE, blobUri, -1);
    costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, quotaConfig.quotaAccountingUnit);
    verifyWriteCost(costMap, 1, 0.0);
    // test for a PUT request.
    restRequest = createMockRequestWithMethod(RestMethod.PUT, blobUri, -1);
    costMap = quotaRequestCostPolicy.calculateRequestQuotaCharge(restRequest, quotaConfig.quotaAccountingUnit);
    verifyWriteCost(costMap, 1, 0.0);
}
Also used : RestRequest(com.github.ambry.rest.RestRequest) VerifiableProperties(com.github.ambry.config.VerifiableProperties) QuotaConfig(com.github.ambry.config.QuotaConfig) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Test(org.junit.Test)

Example 14 with RestResponseChannel

use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method doConditionalDeleteTest.

/**
 * Tests blob conditional DELETE operations on the given {@code container}.
 * @param toPostAccount the {@link Account} to use in post headers. Can be {@code null} if only using service ID.
 * @param toPostContainer the {@link Container} to use in post headers. Can be {@code null} if only using service ID.
 * @param serviceId the serviceId to use for the POST
 * @param isPrivate the isPrivate flag to pass as part of the POST
 * @param expectedAccount the {@link Account} details that are eventually expected to be populated.
 * @param expectedContainer the {@link Container} details that are eventually expected to be populated.
 * @throws Exception
 */
private void doConditionalDeleteTest(Account toPostAccount, Container toPostContainer, String serviceId, boolean isPrivate, Account expectedAccount, Container expectedContainer) throws Exception {
    final int CONTENT_LENGTH = 1024;
    ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(CONTENT_LENGTH));
    String contentType = "application/octet-stream";
    String ownerId = "postGetHeadDeleteOwnerID";
    JSONObject headers = new JSONObject();
    String accountNameInPost = toPostAccount != null ? toPostAccount.getName() : null;
    String containerNameInPost = toPostContainer != null ? toPostContainer.getName() : null;
    setAmbryHeadersForPut(headers, 7200, isPrivate, serviceId, contentType, ownerId, accountNameInPost, containerNameInPost);
    Map<String, String> userMetadata = new HashMap<>();
    userMetadata.put(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key1", "value1");
    userMetadata.put(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key2", "value2");
    RestUtilsTest.setUserMetadataHeaders(headers, userMetadata);
    // perform POST, GET, HEAD successfully before DELETE
    String blobId = postBlobAndVerify(headers, content, expectedAccount, expectedContainer);
    headers.put(RestUtils.Headers.BLOB_SIZE, (long) CONTENT_LENGTH);
    getBlobAndVerify(blobId, null, null, headers, content, expectedAccount, expectedContainer);
    getHeadAndVerify(blobId, null, null, headers, expectedAccount, expectedContainer);
    // test Conditional Delete failure because only container name is set
    RestResponseChannel restResponseChannel = new MockRestResponseChannel();
    JSONObject headers2 = new JSONObject();
    setAccountAndContainerHeaders(headers2, null, containerNameInPost);
    RestRequest restRequest = createRestRequest(RestMethod.DELETE, blobId, headers2, null);
    try {
        doOperation(restRequest, restResponseChannel);
        fail("Operation should have failed because only container name is set");
    } catch (RestServiceException e) {
        assertEquals("AmbryBlobStorageService should have thrown a BadRequest exception", RestServiceErrorCode.BadRequest, e.getErrorCode());
    }
    // test Conditional Delete failure because of incorrect account name
    restResponseChannel = new MockRestResponseChannel();
    setAccountAndContainerHeaders(headers, "INCORRECT_ACCOUNT_NAME", containerNameInPost);
    restRequest = createRestRequest(RestMethod.DELETE, blobId, headers, null);
    try {
        doOperation(restRequest, restResponseChannel);
        fail("Operation should have failed because incorrect account name");
    } catch (RestServiceException e) {
        assertEquals("AmbryBlobStorageService should have thrown a PreconditionFailed exception", RestServiceErrorCode.PreconditionFailed, e.getErrorCode());
    }
    // test Conditional Delete failure because of incorrect container name
    restResponseChannel = new MockRestResponseChannel();
    setAccountAndContainerHeaders(headers, accountNameInPost, "INCORRECT_CONTAINER_NAME");
    restRequest = createRestRequest(RestMethod.DELETE, blobId, headers, null);
    try {
        doOperation(restRequest, restResponseChannel);
        fail("Operation should have failed because incorrect container name");
    } catch (RestServiceException e) {
        assertEquals("AmbryBlobStorageService should have thrown a PreconditionFailed exception", RestServiceErrorCode.PreconditionFailed, e.getErrorCode());
    }
    // test Conditional Delete succeeds
    setAccountAndContainerHeaders(headers, accountNameInPost, containerNameInPost);
    restRequest = createRestRequest(RestMethod.DELETE, blobId, headers, null);
    verifyDeleteAccepted(restRequest);
    // check GET, HEAD and DELETE after delete.
    verifyOperationsAfterDelete(blobId, headers, content, expectedAccount, expectedContainer);
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) ByteBuffer(java.nio.ByteBuffer)

Example 15 with RestResponseChannel

use of com.github.ambry.rest.RestResponseChannel in project ambry by linkedin.

the class AmbrySecurityServiceTest method testExceptionCasesProcessResponse.

/**
 * Tests exception cases for
 * {@link SecurityService#processResponse(RestRequest, RestResponseChannel, BlobInfo, Callback)}
 * @param restMethod the {@link RestMethod} of the request to be made
 * @param restResponseChannel the {@link RestResponseChannel} to write responses over.
 * @param blobInfo the {@link BlobInfo} to be used for the {@link RestRequest}
 * @param expectedErrorCode the {@link RestServiceErrorCode} expected in the exception returned.
 * @throws Exception
 */
private void testExceptionCasesProcessResponse(RestMethod restMethod, RestResponseChannel restResponseChannel, BlobInfo blobInfo, RestServiceErrorCode expectedErrorCode) throws Exception {
    ThrowingConsumer<ExecutionException> errorAction = e -> {
        Assert.assertTrue("Exception should have been an instance of RestServiceException", e.getCause() instanceof RestServiceException);
        RestServiceException re = (RestServiceException) e.getCause();
        Assert.assertEquals("Unexpected RestServerErrorCode (Future)", expectedErrorCode, re.getErrorCode());
    };
    RestRequest restRequest = createRestRequest(restMethod, "/", null);
    TestUtils.assertException(ExecutionException.class, () -> securityService.processResponse(restRequest, restResponseChannel, blobInfo).get(), errorAction);
}
Also used : MockRestRequest(com.github.ambry.rest.MockRestRequest) FrontendConfig(com.github.ambry.config.FrontendConfig) BlobProperties(com.github.ambry.messageformat.BlobProperties) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ResponseStatus(com.github.ambry.rest.ResponseStatus) ByteBuffer(java.nio.ByteBuffer) ThrowingConsumer(com.github.ambry.utils.ThrowingConsumer) Future(java.util.concurrent.Future) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) HostThrottleConfig(com.github.ambry.config.HostThrottleConfig) TestUtils(com.github.ambry.utils.TestUtils) Locale(java.util.Locale) Map(java.util.Map) Container(com.github.ambry.account.Container) TimeZone(java.util.TimeZone) RestResponseChannel(com.github.ambry.rest.RestResponseChannel) RestServiceErrorCode(com.github.ambry.rest.RestServiceErrorCode) Utils(com.github.ambry.utils.Utils) BlobInfo(com.github.ambry.messageformat.BlobInfo) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestResponseChannel(com.github.ambry.rest.MockRestResponseChannel) QuotaTestUtils(com.github.ambry.quota.QuotaTestUtils) Account(com.github.ambry.account.Account) Callback(com.github.ambry.commons.Callback) RestUtils(com.github.ambry.rest.RestUtils) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InMemAccountService(com.github.ambry.account.InMemAccountService) ByteRanges(com.github.ambry.router.ByteRanges) SimpleDateFormat(java.text.SimpleDateFormat) AccountService(com.github.ambry.account.AccountService) HashMap(java.util.HashMap) RestTestUtils(com.github.ambry.rest.RestTestUtils) QuotaManager(com.github.ambry.quota.QuotaManager) QuotaConfig(com.github.ambry.config.QuotaConfig) RequestPath(com.github.ambry.rest.RequestPath) QuotaMode(com.github.ambry.quota.QuotaMode) HostLevelThrottler(com.github.ambry.commons.HostLevelThrottler) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) Pair(com.github.ambry.utils.Pair) RestMethod(com.github.ambry.rest.RestMethod) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ByteRange(com.github.ambry.router.ByteRange) IOException(java.io.IOException) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) InMemAccountServiceFactory(com.github.ambry.account.InMemAccountServiceFactory) AmbryQuotaManager(com.github.ambry.quota.AmbryQuotaManager) Mockito(org.mockito.Mockito) QuotaMetrics(com.github.ambry.quota.QuotaMetrics) SimpleQuotaRecommendationMergePolicy(com.github.ambry.quota.SimpleQuotaRecommendationMergePolicy) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) RestServiceException(com.github.ambry.rest.RestServiceException) MockRestRequest(com.github.ambry.rest.MockRestRequest) RestRequest(com.github.ambry.rest.RestRequest) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

RestResponseChannel (com.github.ambry.rest.RestResponseChannel)26 RestRequest (com.github.ambry.rest.RestRequest)23 MockRestResponseChannel (com.github.ambry.rest.MockRestResponseChannel)21 JSONObject (org.json.JSONObject)15 RestServiceException (com.github.ambry.rest.RestServiceException)14 Test (org.junit.Test)13 MockRestRequest (com.github.ambry.rest.MockRestRequest)12 RestMethod (com.github.ambry.rest.RestMethod)12 RequestPath (com.github.ambry.rest.RequestPath)11 RestServiceErrorCode (com.github.ambry.rest.RestServiceErrorCode)11 RestUtils (com.github.ambry.rest.RestUtils)11 ThrowingConsumer (com.github.ambry.utils.ThrowingConsumer)11 ExecutionException (java.util.concurrent.ExecutionException)11 FutureResult (com.github.ambry.router.FutureResult)10 ReadableStreamChannel (com.github.ambry.router.ReadableStreamChannel)10 ByteBuffer (java.nio.ByteBuffer)10 FrontendConfig (com.github.ambry.config.FrontendConfig)9 MetricRegistry (com.codahale.metrics.MetricRegistry)8 Account (com.github.ambry.account.Account)8 InMemAccountService (com.github.ambry.account.InMemAccountService)8