Search in sources :

Example 6 with QuotaException

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

the class AmbryCUQuotaEnforcer method charge.

@Override
public QuotaRecommendation charge(RestRequest restRequest, Map<QuotaName, Double> requestCostMap) throws QuotaException {
    final QuotaName quotaName = QuotaUtils.getCUQuotaName(restRequest);
    if (requestCostMap.isEmpty() || !requestCostMap.containsKey(quotaName)) {
        String errorMessage = String.format("No %s cost provided for request %s. Nothing to charge", quotaName.name(), RestUtils.convertToStr(restRequest));
        throw new QuotaException(errorMessage, true);
    }
    final QuotaResource quotaResource = QuotaResource.fromRestRequest(restRequest);
    doAndHandleException(() -> {
        quotaSource.chargeUsage(quotaResource, quotaName, requestCostMap.get(quotaName));
        return null;
    }, String.format("Could not charge for request %s due to", restRequest));
    return recommend(restRequest);
}
Also used : QuotaName(com.github.ambry.quota.QuotaName) QuotaException(com.github.ambry.quota.QuotaException) QuotaResource(com.github.ambry.quota.QuotaResource)

Example 7 with QuotaException

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

the class AmbryCUQuotaSourceTest method testCreation.

@Test
public void testCreation() throws QuotaException {
    Assert.assertEquals(4, ambryCUQuotaSource.getAllQuota().size());
    Assert.assertEquals(1024000000, (long) ambryCUQuotaSource.getQuota(new QuotaResource("101_1", QuotaResourceType.CONTAINER), QuotaName.READ_CAPACITY_UNIT).getQuotaValue());
    Assert.assertEquals(1024000000, (long) ambryCUQuotaSource.getQuota(new QuotaResource("101_1", QuotaResourceType.CONTAINER), QuotaName.WRITE_CAPACITY_UNIT).getQuotaValue());
    Assert.assertEquals(258438456, (long) ambryCUQuotaSource.getQuota(new QuotaResource("101_2", QuotaResourceType.CONTAINER), QuotaName.READ_CAPACITY_UNIT).getQuotaValue());
    Assert.assertEquals(258438456, (long) ambryCUQuotaSource.getQuota(new QuotaResource("101_2", QuotaResourceType.CONTAINER), QuotaName.WRITE_CAPACITY_UNIT).getQuotaValue());
    Assert.assertEquals(1024000000, (long) ambryCUQuotaSource.getQuota(new QuotaResource("102_1", QuotaResourceType.CONTAINER), QuotaName.READ_CAPACITY_UNIT).getQuotaValue());
    Assert.assertEquals(1024000000, (long) ambryCUQuotaSource.getQuota(new QuotaResource("102_1", QuotaResourceType.CONTAINER), QuotaName.WRITE_CAPACITY_UNIT).getQuotaValue());
    try {
        ambryCUQuotaSource.getQuota(new QuotaResource("101", QuotaResourceType.ACCOUNT), QuotaName.WRITE_CAPACITY_UNIT);
        Assert.fail("If quota is not present, it should throw an exception");
    } catch (QuotaException quotaException) {
    }
    try {
        ambryCUQuotaSource.getQuota(new QuotaResource("102", QuotaResourceType.ACCOUNT), QuotaName.WRITE_CAPACITY_UNIT);
        Assert.fail("If quota is not present, it should throw an exception");
    } catch (QuotaException quotaException) {
    }
    Assert.assertEquals(10737418240L, (long) ambryCUQuotaSource.getQuota(new QuotaResource("103", QuotaResourceType.ACCOUNT), QuotaName.READ_CAPACITY_UNIT).getQuotaValue());
    Assert.assertEquals(10737418240L, (long) ambryCUQuotaSource.getQuota(new QuotaResource("103", QuotaResourceType.ACCOUNT), QuotaName.WRITE_CAPACITY_UNIT).getQuotaValue());
    Assert.assertEquals(0, ambryCUQuotaSource.getSystemResourceUsage(QuotaName.READ_CAPACITY_UNIT), 0.01);
    Assert.assertEquals(0, ambryCUQuotaSource.getSystemResourceUsage(QuotaName.READ_CAPACITY_UNIT), 0.01);
    Assert.assertEquals(4, ambryCUQuotaSource.getAllQuotaUsage().size());
    Assert.assertEquals(0, (long) ambryCUQuotaSource.getUsage(new QuotaResource("101_1", QuotaResourceType.CONTAINER), QuotaName.READ_CAPACITY_UNIT));
    Assert.assertEquals(0, (long) ambryCUQuotaSource.getUsage(new QuotaResource("101_1", QuotaResourceType.CONTAINER), QuotaName.WRITE_CAPACITY_UNIT));
    Assert.assertEquals(0, (long) ambryCUQuotaSource.getUsage(new QuotaResource("101_2", QuotaResourceType.CONTAINER), QuotaName.READ_CAPACITY_UNIT));
    Assert.assertEquals(0, (long) ambryCUQuotaSource.getUsage(new QuotaResource("101_2", QuotaResourceType.CONTAINER), QuotaName.WRITE_CAPACITY_UNIT));
    Assert.assertEquals(0, (long) ambryCUQuotaSource.getUsage(new QuotaResource("102_1", QuotaResourceType.CONTAINER), QuotaName.READ_CAPACITY_UNIT));
    Assert.assertEquals(0, (long) ambryCUQuotaSource.getUsage(new QuotaResource("102_1", QuotaResourceType.CONTAINER), QuotaName.WRITE_CAPACITY_UNIT));
}
Also used : QuotaException(com.github.ambry.quota.QuotaException) QuotaResource(com.github.ambry.quota.QuotaResource) Test(org.junit.Test)

Example 8 with QuotaException

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

the class StorageQuotaEnforcer method getQuotaValueForResource.

/**
 * Return the storage quota value (in bytes) for given {@link QuotaResource}.
 * @param resource The {@link QuotaResource} to fetch quota value.
 * @return The storage quota value (in bytes)
 */
protected long getQuotaValueForResource(QuotaResource resource) {
    long quotaValue = -1;
    try {
        Quota quota = quotaSource.getQuota(resource, QuotaName.STORAGE_IN_GB);
        quotaValue = (long) quota.getQuotaValue() * BYTES_IN_GB;
    } catch (QuotaException quotaException) {
        logger.warn("Quota not found for resource id: {}, type: {}", resource.getResourceId(), resource.getQuotaResourceType());
    }
    return quotaValue;
}
Also used : Quota(com.github.ambry.quota.Quota) QuotaException(com.github.ambry.quota.QuotaException)

Example 9 with QuotaException

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

the class GetBlobInfoOperation method checkAndMaybeComplete.

/**
 * Check whether the operation can be completed, if so complete it.
 */
private void checkAndMaybeComplete() {
    if (progressTracker.isDone()) {
        if (progressTracker.hasSucceeded()) {
            operationException.set(null);
        } else if (operationTracker.hasFailedOnNotFound()) {
            operationException.set(new RouterException("GetBlobInfoOperation failed because of BlobNotFound", RouterErrorCode.BlobDoesNotExist));
        }
        operationCompleted = true;
    }
    if (operationCompleted && operationCallbackInvoked.compareAndSet(false, true)) {
        if (quotaChargeCallback != null) {
            try {
                quotaChargeCallback.charge();
            } catch (QuotaException quotaException) {
                // No exception should be thrown when doing quota charge for blobinfo operation.
                logger.trace("Unexpected exception {} thrown on handling quota event for {}", quotaException, blobId);
            }
        }
        Exception e = operationException.get();
        if (operationResult == null && e == null) {
            e = new RouterException("Operation failed, but exception was not set", RouterErrorCode.UnexpectedInternalError);
            routerMetrics.operationFailureWithUnsetExceptionCount.inc();
        }
        if (e != null) {
            operationResult = null;
            routerMetrics.onGetBlobError(e, options, isEncrypted);
        }
        long operationLatencyMs = time.milliseconds() - submissionTimeMs;
        if (isEncrypted) {
            routerMetrics.getEncryptedBlobInfoOperationLatencyMs.update(operationLatencyMs);
        } else {
            routerMetrics.getBlobInfoOperationLatencyMs.update(operationLatencyMs);
        }
        NonBlockingRouter.completeOperation(null, getOperationCallback, operationResult, e);
    }
}
Also used : QuotaException(com.github.ambry.quota.QuotaException) IOException(java.io.IOException) MessageFormatException(com.github.ambry.messageformat.MessageFormatException) QuotaException(com.github.ambry.quota.QuotaException)

Example 10 with QuotaException

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

the class OperationQuotaChargerTest method testGetQuotaResource.

@Test
public void testGetQuotaResource() throws Exception {
    // getQuotaResource should return null if quotaChargeCallback is null.
    OperationQuotaCharger operationQuotaCharger = new OperationQuotaCharger(null, BLOBID, "GetOperation");
    Assert.assertNull("getQuotaResource should return null if quotaChargeCallback is null.", operationQuotaCharger.getQuotaResource());
    QuotaChargeCallback quotaChargeCallback = Mockito.mock(QuotaChargeCallback.class);
    operationQuotaCharger = new OperationQuotaCharger(quotaChargeCallback, BLOBID, "GetOperation");
    // getQuotaResource should return what quotaChargeCallback.getQuotaResource returns.
    QuotaResource quotaResource = new QuotaResource("test", QuotaResourceType.ACCOUNT);
    Mockito.when(quotaChargeCallback.getQuotaResource()).thenReturn(quotaResource);
    Assert.assertEquals("getQuotaResource should return what quotaChargeCallback.getQuotaResource returns.", quotaResource, quotaChargeCallback.getQuotaResource());
    // getQuotaResource should return null if quotaChargeCallback throws exception.
    Mockito.when(quotaChargeCallback.getQuotaResource()).thenThrow(new QuotaException("", new RestServiceException("", RestServiceErrorCode.InternalServerError), false));
    Assert.assertNull("getQuotaResource should return null if quotaChargeCallback is null.", operationQuotaCharger.getQuotaResource());
}
Also used : RestServiceException(com.github.ambry.rest.RestServiceException) QuotaChargeCallback(com.github.ambry.quota.QuotaChargeCallback) QuotaException(com.github.ambry.quota.QuotaException) QuotaResource(com.github.ambry.quota.QuotaResource) Test(org.junit.Test)

Aggregations

QuotaException (com.github.ambry.quota.QuotaException)11 Test (org.junit.Test)7 QuotaResource (com.github.ambry.quota.QuotaResource)5 QuotaChargeCallback (com.github.ambry.quota.QuotaChargeCallback)4 QuotaName (com.github.ambry.quota.QuotaName)3 Account (com.github.ambry.account.Account)2 QuotaMethod (com.github.ambry.quota.QuotaMethod)2 QuotaRecommendation (com.github.ambry.quota.QuotaRecommendation)2 RestRequest (com.github.ambry.rest.RestRequest)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 AccountService (com.github.ambry.account.AccountService)1 Container (com.github.ambry.account.Container)1 AccountStatsStore (com.github.ambry.accountstats.AccountStatsStore)1 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)1 RetainingAsyncWritableChannel (com.github.ambry.commons.RetainingAsyncWritableChannel)1 QuotaConfig (com.github.ambry.config.QuotaConfig)1 VerifiableProperties (com.github.ambry.config.VerifiableProperties)1 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)1 MessageFormatRecord (com.github.ambry.messageformat.MessageFormatRecord)1 GetOption (com.github.ambry.protocol.GetOption)1