Search in sources :

Example 1 with QuotaConfig

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

the class QuotaTestUtils method createQuotaConfig.

/**
 * Create {@link QuotaConfig} object with {@link QuotaEnforcer} and corresponding {@link QuotaSource} map.
 * @param enforcerSourcemap {@link Map} for {@link QuotaEnforcer} and corresponding {@link QuotaSource} object.
 * @param isRequestThrottlingEnabled boolean flag indicating if request quota throttling should be enabled.
 * @param quotaMode {@link QuotaMode} for quota enforcement.
 * @return QuotaConfig object.
 */
public static QuotaConfig createQuotaConfig(Map<String, String> enforcerSourcemap, boolean isRequestThrottlingEnabled, QuotaMode quotaMode) {
    Properties properties = new Properties();
    properties.setProperty(QuotaConfig.REQUEST_THROTTLING_ENABLED, "" + isRequestThrottlingEnabled);
    properties.setProperty(QuotaConfig.THROTTLING_MODE, quotaMode.name());
    JSONArray jsonArray = new JSONArray();
    for (String enforcerFactoryClass : enforcerSourcemap.keySet()) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(QuotaConfig.ENFORCER_STR, enforcerFactoryClass);
        jsonObject.put(QuotaConfig.SOURCE_STR, enforcerSourcemap.get(enforcerFactoryClass));
        jsonArray.put(jsonObject);
    }
    properties.setProperty(QuotaConfig.REQUEST_QUOTA_ENFORCER_SOURCE_PAIR_INFO_JSON, new JSONObject().put(QuotaConfig.QUOTA_ENFORCER_SOURCE_PAIR_INFO_STR, jsonArray).toString());
    return new QuotaConfig(new VerifiableProperties(properties));
}
Also used : JSONObject(org.json.JSONObject) VerifiableProperties(com.github.ambry.config.VerifiableProperties) JSONArray(org.json.JSONArray) QuotaConfig(com.github.ambry.config.QuotaConfig) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties)

Example 2 with QuotaConfig

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

the class AmbryCUQuotaSourceTest method testInit.

public void testInit() throws Exception {
    QuotaSource quotaSource = new AmbryCUQuotaSourceFactory(new QuotaConfig(new VerifiableProperties(new Properties())), inMemAccountService).getQuotaSource();
    Assert.assertFalse(quotaSource.isReady());
    quotaSource.init();
    Assert.assertTrue(quotaSource.isReady());
    quotaSource.shutdown();
    Assert.assertFalse(quotaSource.isReady());
}
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)

Example 3 with QuotaConfig

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

the class AmbryCUQuotaSourceTest method setup.

/**
 * Create test setup by creating the {@link AmbryCUQuotaSource} object and updating account service.
 * @throws IOException
 * @throws AccountServiceException
 */
@Before
public void setup() throws IOException, AccountServiceException {
    Properties properties = new Properties();
    properties.setProperty(QuotaConfig.RESOURCE_CU_QUOTA_IN_JSON, DEFAULT_CU_QUOTA_IN_JSON);
    properties.setProperty(QuotaConfig.FRONTEND_CU_CAPACITY_IN_JSON, DEFAULT_FRONTEND_CAPACITY_JSON);
    quotaConfig = new QuotaConfig(new VerifiableProperties(properties));
    inMemAccountService = new InMemAccountService(false, true);
    ObjectMapper objectMapper = new ObjectMapper();
    testQuotas = objectMapper.readValue(quotaConfig.resourceCUQuotaInJson, new TypeReference<Map<String, JsonCUQuotaDataProviderUtil.MapOrQuota>>() {
    });
    for (String s : testQuotas.keySet()) {
        inMemAccountService.updateAccounts(Collections.singletonList(createAccountForQuota(testQuotas.get(s), s)));
    }
    ambryCUQuotaSource = (AmbryCUQuotaSource) new AmbryCUQuotaSourceFactory(quotaConfig, inMemAccountService).getQuotaSource();
    ambryCUQuotaSource.init();
}
Also used : InMemAccountService(com.github.ambry.account.InMemAccountService) VerifiableProperties(com.github.ambry.config.VerifiableProperties) QuotaConfig(com.github.ambry.config.QuotaConfig) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 4 with QuotaConfig

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

the class AmbryCUQuotaEnforcerTest method setup.

@Before
public void setup() throws IOException {
    ACCOUNT = ACCOUNT_SERVICE.createAndAddRandomAccount(QuotaResourceType.ACCOUNT);
    Properties properties = new Properties();
    properties.setProperty(QuotaConfig.RESOURCE_CU_QUOTA_IN_JSON, String.format("{\n" + "  \"%s\": {\n" + "    \"wcu\": %d,\n" + "    \"rcu\": %d\n" + "  }\n" + "}", String.valueOf(ACCOUNT.getId()), WCU, RCU));
    properties.setProperty(QuotaConfig.FRONTEND_CU_CAPACITY_IN_JSON, String.format("{\n" + "  \"wcu\": %d,\n" + "  \"rcu\": %d\n" + "}", FE_WCU, FE_RCU));
    QuotaConfig quotaConfig = new QuotaConfig(new VerifiableProperties(properties));
    QUOTA_SOURCE = new ExceptionQuotaSource(quotaConfig, ACCOUNT_SERVICE);
    QUOTA_SOURCE.init();
    AMBRY_QUOTA_ENFORCER = new AmbryCUQuotaEnforcer(QUOTA_SOURCE, quotaConfig);
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) QuotaConfig(com.github.ambry.config.QuotaConfig) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) Before(org.junit.Before)

Example 5 with QuotaConfig

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

the class AmbryQuotaManagerTest method buildAmbryQuotaManager.

/**
 * Build {@link AmbryQuotaManager} object with two test enforcers.
 * @return AmbryQuotaManager object.
 * @throws ReflectiveOperationException in case the {@link AmbryQuotaManager} object could not be created.
 */
private AmbryQuotaManager buildAmbryQuotaManager() throws ReflectiveOperationException {
    quotaConfig = new QuotaConfig(new VerifiableProperties(new Properties()));
    quotaConfig.requestQuotaEnforcerSourcePairInfoJson = buildTestQuotaEnforcerSourceInfoPairJson().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);
    Assert.assertTrue(quotaEnforcers.get(0) instanceof TestCUQuotaEnforcerFactory.TestAmbryCUQuotaEnforcer);
    Assert.assertTrue(quotaEnforcers.get(1) instanceof TestCUQuotaEnforcerFactory.TestAmbryCUQuotaEnforcer);
    testAmbryCUQuotaEnforcer = (TestCUQuotaEnforcerFactory.TestAmbryCUQuotaEnforcer) quotaEnforcers.get(1);
    return ambryQuotaManager;
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) MetricRegistry(com.codahale.metrics.MetricRegistry) QuotaConfig(com.github.ambry.config.QuotaConfig) ArrayList(java.util.ArrayList) AmbryCUQuotaEnforcer(com.github.ambry.quota.capacityunit.AmbryCUQuotaEnforcer) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties)

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