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);
}
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));
}
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;
}
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);
}
}
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());
}
Aggregations