Search in sources :

Example 1 with Quota

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

the class JSONStringStorageQuotaSource method getQuota.

@Override
public Quota getQuota(QuotaResource quotaResource, QuotaName quotaName) {
    if (quotaName != QuotaName.STORAGE_IN_GB) {
        return null;
    }
    QuotaResourceType resourceType = quotaResource.getQuotaResourceType();
    if (resourceType != QuotaResourceType.ACCOUNT && resourceType != QuotaResourceType.CONTAINER) {
        throw new IllegalArgumentException("Unsupported quota resource type: " + resourceType);
    }
    String resourceId = quotaResource.getResourceId();
    if (storageQuota.containsKey(resourceId)) {
        long quotaValue = storageQuota.get(resourceId);
        return new Quota(quotaName, quotaValue, quotaResource);
    }
    return null;
}
Also used : QuotaResourceType(com.github.ambry.quota.QuotaResourceType) Quota(com.github.ambry.quota.Quota)

Example 2 with Quota

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

the class JSONStringStorageQuotaSourceTest method testJSONStringStorageQuotaSource.

@Test
public void testJSONStringStorageQuotaSource() throws Exception {
    // Trick to create a string literal without escape.
    String json = "{`10`: {`1`: 1000, `2`: 3000}, `20`: {`4`: 2000, `5`: 1000}, `30`: 4000, `40`: 5000, `50`: {`6`: 6000}}".replace("`", "\"");
    Properties properties = new Properties();
    properties.setProperty(StorageQuotaConfig.STORAGE_QUOTA_IN_JSON, json);
    StorageQuotaConfig config = new StorageQuotaConfig(new VerifiableProperties(properties));
    // Setting up accounts and account service
    InMemAccountService accountService = new InMemAccountService(false, false);
    Account account = new AccountBuilder((short) 10, "10", Account.AccountStatus.ACTIVE, QuotaResourceType.CONTAINER).addOrUpdateContainer(new ContainerBuilder((short) 1, "1", Container.ContainerStatus.ACTIVE, "", (short) 10).build()).addOrUpdateContainer(new ContainerBuilder((short) 2, "2", Container.ContainerStatus.ACTIVE, "", (short) 10).build()).build();
    accountService.updateAccounts(Collections.singleton(account));
    account = new AccountBuilder((short) 20, "20", Account.AccountStatus.ACTIVE, QuotaResourceType.CONTAINER).addOrUpdateContainer(new ContainerBuilder((short) 4, "4", Container.ContainerStatus.ACTIVE, "", (short) 20).build()).addOrUpdateContainer(new ContainerBuilder((short) 5, "5", Container.ContainerStatus.ACTIVE, "", (short) 20).build()).build();
    accountService.updateAccounts(Collections.singleton(account));
    account = new AccountBuilder((short) 30, "30", Account.AccountStatus.ACTIVE, QuotaResourceType.ACCOUNT).addOrUpdateContainer(new ContainerBuilder((short) 4, "4", Container.ContainerStatus.ACTIVE, "", (short) 30).build()).build();
    accountService.updateAccounts(Collections.singleton(account));
    account = new AccountBuilder((short) 40, "40", Account.AccountStatus.ACTIVE, QuotaResourceType.ACCOUNT).addOrUpdateContainer(new ContainerBuilder((short) 4, "4", Container.ContainerStatus.ACTIVE, "", (short) 40).build()).build();
    accountService.updateAccounts(Collections.singleton(account));
    account = new AccountBuilder((short) 50, "50", Account.AccountStatus.ACTIVE, QuotaResourceType.CONTAINER).addOrUpdateContainer(new ContainerBuilder((short) 6, "6", Container.ContainerStatus.ACTIVE, "", (short) 50).build()).build();
    accountService.updateAccounts(Collections.singleton(account));
    JSONStringStorageQuotaSource source = new JSONStringStorageQuotaSource(config, accountService);
    QuotaResourceType resourceType = QuotaResourceType.CONTAINER;
    Quota quota = source.getQuota(new QuotaResource("1000_1", resourceType), QuotaName.STORAGE_IN_GB);
    assertNull(quota);
    quota = source.getQuota(new QuotaResource("10_1", resourceType), QuotaName.STORAGE_IN_GB);
    assertEquals(1000L, (long) quota.getQuotaValue());
    quota = source.getQuota(new QuotaResource("10_2", resourceType), QuotaName.STORAGE_IN_GB);
    assertEquals(3000L, (long) quota.getQuotaValue());
    quota = source.getQuota(new QuotaResource("20_4", resourceType), QuotaName.STORAGE_IN_GB);
    assertEquals(2000L, (long) quota.getQuotaValue());
    quota = source.getQuota(new QuotaResource("20_5", resourceType), QuotaName.STORAGE_IN_GB);
    assertEquals(1000L, (long) quota.getQuotaValue());
    quota = source.getQuota(new QuotaResource("30", QuotaResourceType.ACCOUNT), QuotaName.STORAGE_IN_GB);
    assertEquals(4000L, (long) quota.getQuotaValue());
    quota = source.getQuota(new QuotaResource("40", QuotaResourceType.ACCOUNT), QuotaName.STORAGE_IN_GB);
    assertEquals(5000L, (long) quota.getQuotaValue());
    quota = source.getQuota(new QuotaResource("50_6", resourceType), QuotaName.STORAGE_IN_GB);
    assertEquals(6000L, (long) quota.getQuotaValue());
}
Also used : Account(com.github.ambry.account.Account) InMemAccountService(com.github.ambry.account.InMemAccountService) ContainerBuilder(com.github.ambry.account.ContainerBuilder) QuotaResourceType(com.github.ambry.quota.QuotaResourceType) Quota(com.github.ambry.quota.Quota) VerifiableProperties(com.github.ambry.config.VerifiableProperties) AccountBuilder(com.github.ambry.account.AccountBuilder) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) StorageQuotaConfig(com.github.ambry.config.StorageQuotaConfig) QuotaResource(com.github.ambry.quota.QuotaResource) Test(org.junit.Test)

Example 3 with Quota

use of com.github.ambry.quota.Quota 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)

Aggregations

Quota (com.github.ambry.quota.Quota)3 QuotaResourceType (com.github.ambry.quota.QuotaResourceType)2 Account (com.github.ambry.account.Account)1 AccountBuilder (com.github.ambry.account.AccountBuilder)1 ContainerBuilder (com.github.ambry.account.ContainerBuilder)1 InMemAccountService (com.github.ambry.account.InMemAccountService)1 StorageQuotaConfig (com.github.ambry.config.StorageQuotaConfig)1 VerifiableProperties (com.github.ambry.config.VerifiableProperties)1 QuotaException (com.github.ambry.quota.QuotaException)1 QuotaResource (com.github.ambry.quota.QuotaResource)1 Properties (java.util.Properties)1 Test (org.junit.Test)1