Search in sources :

Example 1 with ThrottlingRecommendation

use of com.github.ambry.quota.ThrottlingRecommendation in project ambry by linkedin.

the class AmbrySecurityService method processRequestCharges.

@Override
public void processRequestCharges(RestRequest restRequest, RestResponseChannel responseChannel, BlobInfo blobInfo) {
    Map<String, Double> requestCost = ambryCostModelPolicy.calculateRequestCost(restRequest, responseChannel, blobInfo);
    setRequestCostHeader(requestCost, responseChannel);
    if (QuotaUtils.isRequestResourceQuotaManaged(restRequest) && quotaManager != null) {
        ThrottlingRecommendation throttlingRecommendation = null;
        try {
            throttlingRecommendation = quotaManager.recommend(restRequest);
        } catch (QuotaException quotaException) {
            LOGGER.warn("Exception {} in while processing request charges.", quotaException);
        }
        if (throttlingRecommendation != null) {
            RestUtils.buildUserQuotaHeadersMap(throttlingRecommendation).entrySet().stream().forEach(headerEntry -> responseChannel.setHeader(headerEntry.getKey(), headerEntry.getValue()));
        }
    }
}
Also used : ThrottlingRecommendation(com.github.ambry.quota.ThrottlingRecommendation) QuotaException(com.github.ambry.quota.QuotaException)

Example 2 with ThrottlingRecommendation

use of com.github.ambry.quota.ThrottlingRecommendation in project ambry by linkedin.

the class AmbrySecurityService method postProcessRequest.

@Override
public void postProcessRequest(RestRequest restRequest, Callback<Void> callback) {
    if (restRequest == null || callback == null) {
        throw new IllegalArgumentException("RestRequest or Callback is null");
    }
    Exception exception = null;
    frontendMetrics.securityServicePostProcessRequestRate.mark();
    long startTimeMs = System.currentTimeMillis();
    try {
        if (!isOpen) {
            exception = new RestServiceException("SecurityService is closed", RestServiceErrorCode.ServiceUnavailable);
        } else if (hostLevelThrottler.shouldThrottle(restRequest)) {
            exception = new RestServiceException("Too many requests", RestServiceErrorCode.TooManyRequests);
        } else {
            if (QuotaUtils.isRequestResourceQuotaManaged(restRequest) && quotaManager != null) {
                ThrottlingRecommendation throttlingRecommendation = quotaManager.recommend(restRequest);
                if (throttlingRecommendation != null && throttlingRecommendation.shouldThrottle() && quotaManager.getQuotaMode() == QuotaMode.THROTTLING) {
                    Map<String, String> quotaHeaderMap = RestUtils.buildUserQuotaHeadersMap(throttlingRecommendation);
                    throw new RestServiceException("User Quota Exceeded", RestServiceErrorCode.TooManyRequests, true, true, quotaHeaderMap);
                }
            }
            if (restRequest.getRestMethod() == RestMethod.DELETE || restRequest.getRestMethod() == RestMethod.PUT) {
                accountAndContainerNamePreconditionCheck(restRequest);
            } else if (restRequest.getRestMethod() == RestMethod.GET) {
                RequestPath requestPath = getRequestPath(restRequest);
                String operationOrBlobId = requestPath.getOperationOrBlobId(true);
                // ensure that secure path validation is only performed when getting blobs rather than other operations.
                if (!operationOrBlobId.isEmpty() && !OPERATIONS.contains(operationOrBlobId)) {
                    validateSecurePathIfRequired(restRequest, requestPath.getPrefix(), frontendConfig.securePathPrefix);
                }
            }
        }
    } catch (Exception e) {
        exception = e;
    } finally {
        frontendMetrics.securityServicePostProcessRequestTimeInMs.update(System.currentTimeMillis() - startTimeMs);
        callback.onCompletion(null, exception);
    }
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) RequestPath(com.github.ambry.rest.RequestPath) ThrottlingRecommendation(com.github.ambry.quota.ThrottlingRecommendation) Map(java.util.Map) QuotaException(com.github.ambry.quota.QuotaException) RestServiceException(com.github.ambry.rest.RestServiceException)

Aggregations

QuotaException (com.github.ambry.quota.QuotaException)2 ThrottlingRecommendation (com.github.ambry.quota.ThrottlingRecommendation)2 RequestPath (com.github.ambry.rest.RequestPath)1 RestServiceException (com.github.ambry.rest.RestServiceException)1 Map (java.util.Map)1