Search in sources :

Example 11 with QuotaConfig

use of com.github.ambry.config.QuotaConfig in project ambry by linkedin.

the class AmbryQuotaManagerTest method buildAmbryQuotaManagerWithAmbryCUEnforcer.

/**
 * Build {@link AmbryQuotaManager} object with {@link AmbryCUQuotaEnforcer} as one of the enforcers.
 * @return AmbryQuotaManager object.
 * @throws ReflectiveOperationException in case the {@link AmbryQuotaManager} object could not be created.
 */
private AmbryQuotaManager buildAmbryQuotaManagerWithAmbryCUEnforcer() throws ReflectiveOperationException {
    Properties properties = new Properties();
    properties.setProperty(QuotaConfig.RESOURCE_CU_QUOTA_IN_JSON, String.format("{\n" + "    \"%s\": {\n" + "        \"wcu\": %d,\n" + "        \"rcu\": %d\n" + "    },\n" + "    \"%s\": {\n" + "        \"wcu\": %d,\n" + "        \"rcu\": %d\n" + "    }\n" + "}", String.valueOf(ACCOUNT.getId()), 10, 10, String.valueOf(ANOTHER_ACCOUNT.getId()), 10, 10));
    properties.setProperty(QuotaConfig.FRONTEND_CU_CAPACITY_IN_JSON, String.format("{\n" + "  \"wcu\": %d,\n" + "  \"rcu\": %d\n" + "}", 100, 100));
    quotaConfig = new QuotaConfig(new VerifiableProperties(properties));
    quotaConfig.requestQuotaEnforcerSourcePairInfoJson = buildDefaultQuotaEnforcerSourceInfoPairJson().toString();
    QuotaMetrics quotaMetrics = new QuotaMetrics(new MetricRegistry());
    AmbryQuotaManager ambryQuotaManager = createAmbryQuotaManager(quotaConfig, quotaMetrics);
    Assert.assertEquals(2, ambryQuotaManager.quotaEnforcers.size());
    List<QuotaEnforcer> quotaEnforcers = new ArrayList<>(ambryQuotaManager.quotaEnforcers);
    quotaEnforcers.sort(Comparator.comparing(o -> o.getClass().getSimpleName()));
    Assert.assertTrue(quotaEnforcers.get(0) instanceof AmbryCUQuotaEnforcer);
    Assert.assertTrue(quotaEnforcers.get(1) instanceof TestCUQuotaEnforcerFactory.TestAmbryCUQuotaEnforcer);
    testAmbryCUQuotaEnforcer = (TestCUQuotaEnforcerFactory.TestAmbryCUQuotaEnforcer) quotaEnforcers.get(1);
    return ambryQuotaManager;
}
Also used : Arrays(java.util.Arrays) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) RestMethod(com.github.ambry.rest.RestMethod) VerifiableProperties(com.github.ambry.config.VerifiableProperties) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) QuotaConfig(com.github.ambry.config.QuotaConfig) List(java.util.List) JSONObject(org.json.JSONObject) Map(java.util.Map) Account(com.github.ambry.account.Account) AmbryCUQuotaEnforcer(com.github.ambry.quota.capacityunit.AmbryCUQuotaEnforcer) Comparator(java.util.Comparator) Assert(org.junit.Assert) RestRequest(com.github.ambry.rest.RestRequest) Collections(java.util.Collections) InMemAccountService(com.github.ambry.account.InMemAccountService) JSONArray(org.json.JSONArray) Parameterized(org.junit.runners.Parameterized) VerifiableProperties(com.github.ambry.config.VerifiableProperties) MetricRegistry(com.codahale.metrics.MetricRegistry) ArrayList(java.util.ArrayList) AmbryCUQuotaEnforcer(com.github.ambry.quota.capacityunit.AmbryCUQuotaEnforcer) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) QuotaConfig(com.github.ambry.config.QuotaConfig) AmbryCUQuotaEnforcer(com.github.ambry.quota.capacityunit.AmbryCUQuotaEnforcer)

Example 12 with QuotaConfig

use of com.github.ambry.config.QuotaConfig in project ambry by linkedin.

the class AmbryQuotaManagerUpdateNotificationTest method ambryQuotaManagerUpdateNotificationTest.

@Test
public void ambryQuotaManagerUpdateNotificationTest() throws Exception {
    VerifiableProperties verifiableProperties = new VerifiableProperties(new Properties());
    MetricRegistry metricRegistry = new MetricRegistry();
    QuotaConfig quotaConfig = new QuotaConfig(verifiableProperties);
    QuotaRecommendationMergePolicy quotaRecommendationMergePolicy = new SimpleQuotaRecommendationMergePolicy(quotaConfig);
    AccountServiceForConsumerTest accountService = new AccountServiceForConsumerTest(new AccountServiceConfig(verifiableProperties), new AccountServiceMetrics(metricRegistry), null);
    AmbryQuotaManager ambryQuotaManager = new AmbryQuotaManager(quotaConfig, quotaRecommendationMergePolicy, accountService, null, new QuotaMetrics(metricRegistry));
    AmbryCUQuotaSource quotaSource = (AmbryCUQuotaSource) getQuotaSourceMember(ambryQuotaManager);
    assertTrue("updated accounts should be empty", quotaSource.getAllQuota().isEmpty());
    Set<Short> accountIdSet = new HashSet<>();
    accountIdSet.add((short) 1);
    accountIdSet.add((short) 2);
    Map<Short, Account> idToRefAccountMap = new HashMap<>();
    AccountTestUtils.generateRefAccounts(idToRefAccountMap, new HashMap<>(), accountIdSet, 2, 3);
    accountService.notifyAccountUpdateConsumers(idToRefAccountMap.values());
    assertEquals("Invalid size of updated accounts", quotaSource.getAllQuota().size(), 2);
}
Also used : Account(com.github.ambry.account.Account) VerifiableProperties(com.github.ambry.config.VerifiableProperties) HashMap(java.util.HashMap) MetricRegistry(com.codahale.metrics.MetricRegistry) AccountServiceConfig(com.github.ambry.config.AccountServiceConfig) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) AccountServiceMetrics(com.github.ambry.account.AccountServiceMetrics) QuotaConfig(com.github.ambry.config.QuotaConfig) AmbryCUQuotaSource(com.github.ambry.quota.capacityunit.AmbryCUQuotaSource) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with QuotaConfig

use of com.github.ambry.config.QuotaConfig in project ambry by linkedin.

the class AmbryCUQuotaEnforcerFactoryTest method testGetRequestQuotaEnforcer.

@Test
public void testGetRequestQuotaEnforcer() throws IOException {
    QuotaConfig quotaConfig = new QuotaConfig(new VerifiableProperties(new Properties()));
    QuotaSource quotaSource = new AmbryCUQuotaSource(quotaConfig, Mockito.mock(AccountService.class));
    AccountStatsStore mockAccountStatsStore = Mockito.mock(AccountStatsStore.class);
    AmbryCUQuotaEnforcerFactory ambryCUQuotaEnforcerFactory = new AmbryCUQuotaEnforcerFactory(quotaConfig, quotaSource, mockAccountStatsStore);
    Assert.assertEquals(AmbryCUQuotaEnforcer.class, ambryCUQuotaEnforcerFactory.getQuotaEnforcer().getClass());
}
Also used : QuotaSource(com.github.ambry.quota.QuotaSource) VerifiableProperties(com.github.ambry.config.VerifiableProperties) QuotaConfig(com.github.ambry.config.QuotaConfig) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) AccountService(com.github.ambry.account.AccountService) AccountStatsStore(com.github.ambry.accountstats.AccountStatsStore) Test(org.junit.Test)

Example 14 with QuotaConfig

use of com.github.ambry.config.QuotaConfig in project ambry by linkedin.

the class NonBlockingRouterQuotaCallbackTest method testRouterWithDefaultQuotaCallback.

/**
 * Test default {@link QuotaChargeCallback} doesn't charge anything and doesn't error out when throttling is disabled.
 */
@Test
public void testRouterWithDefaultQuotaCallback() throws Exception {
    try {
        setRouter();
        assertExpectedThreadCounts(2, 1);
        AtomicInteger listenerCalledCount = new AtomicInteger(0);
        QuotaConfig quotaConfig = new QuotaConfig(new VerifiableProperties(new Properties()));
        QuotaManager quotaManager = new ChargeTesterQuotaManager(quotaConfig, new SimpleQuotaRecommendationMergePolicy(quotaConfig), accountService, null, new QuotaMetrics(new MetricRegistry()), listenerCalledCount);
        QuotaChargeCallback quotaChargeCallback = QuotaUtils.buildQuotaChargeCallback(null, quotaManager, true);
        int blobSize = 3000;
        setOperationParams(blobSize, TTL_SECS);
        String compositeBlobId = router.putBlob(putBlobProperties, putUserMetadata, putChannel, PutBlobOptions.DEFAULT, null, quotaChargeCallback).get();
        assertEquals(0, listenerCalledCount.get());
        RetainingAsyncWritableChannel retainingAsyncWritableChannel = new RetainingAsyncWritableChannel();
        router.getBlob(compositeBlobId, new GetBlobOptionsBuilder().build(), null, quotaChargeCallback).get().getBlobDataChannel().readInto(retainingAsyncWritableChannel, null).get();
        // read out all the chunks.
        retainingAsyncWritableChannel.consumeContentAsInputStream().close();
        assertEquals(0, listenerCalledCount.get());
    } finally {
        router.close();
        assertExpectedThreadCounts(0, 0);
        // submission after closing should return a future that is already done.
        assertClosed();
    }
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) RetainingAsyncWritableChannel(com.github.ambry.commons.RetainingAsyncWritableChannel) MetricRegistry(com.codahale.metrics.MetricRegistry) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) QuotaMetrics(com.github.ambry.quota.QuotaMetrics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QuotaConfig(com.github.ambry.config.QuotaConfig) SimpleQuotaRecommendationMergePolicy(com.github.ambry.quota.SimpleQuotaRecommendationMergePolicy) QuotaChargeCallback(com.github.ambry.quota.QuotaChargeCallback) QuotaManager(com.github.ambry.quota.QuotaManager) AmbryQuotaManager(com.github.ambry.quota.AmbryQuotaManager) Test(org.junit.Test)

Aggregations

QuotaConfig (com.github.ambry.config.QuotaConfig)14 VerifiableProperties (com.github.ambry.config.VerifiableProperties)13 Properties (java.util.Properties)13 Test (org.junit.Test)7 MetricRegistry (com.codahale.metrics.MetricRegistry)5 ArrayList (java.util.ArrayList)3 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 Account (com.github.ambry.account.Account)2 AccountService (com.github.ambry.account.AccountService)2 InMemAccountService (com.github.ambry.account.InMemAccountService)2 AccountStatsStore (com.github.ambry.accountstats.AccountStatsStore)2 QuotaManager (com.github.ambry.quota.QuotaManager)2 QuotaSource (com.github.ambry.quota.QuotaSource)2 SimpleQuotaRecommendationMergePolicy (com.github.ambry.quota.SimpleQuotaRecommendationMergePolicy)2 AmbryCUQuotaEnforcer (com.github.ambry.quota.capacityunit.AmbryCUQuotaEnforcer)2 RestRequest (com.github.ambry.rest.RestRequest)2 Before (org.junit.Before)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1