Search in sources :

Example 11 with SharedLimiterKey

use of org.apache.gobblin.util.limiter.broker.SharedLimiterKey in project incubator-gobblin by apache.

the class RestliLimiterFactoryTest method testFactory.

@Test
public void testFactory() throws Exception {
    SharedResourcesBroker<SimpleScopeType> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(ConfigFactory.empty(), SimpleScopeType.GLOBAL.defaultScopeInstance());
    MyRequestSender requestSender = new MyRequestSender();
    broker.bindSharedResourceAtScope(new RedirectAwareRestClientRequestSender.Factory<>(), new SharedRestClientKey(RestliLimiterFactory.RESTLI_SERVICE_NAME), SimpleScopeType.GLOBAL, requestSender);
    RestliServiceBasedLimiter limiter = broker.getSharedResource(new RestliLimiterFactory<>(), new SharedLimiterKey("my/resource"));
    Assert.assertNotNull(limiter.acquirePermits(10));
    Assert.assertEquals(requestSender.requestList.size(), 1);
    broker.close();
}
Also used : SharedRestClientKey(org.apache.gobblin.restli.SharedRestClientKey) SimpleScopeType(org.apache.gobblin.broker.SimpleScopeType) SharedLimiterKey(org.apache.gobblin.util.limiter.broker.SharedLimiterKey) Test(org.testng.annotations.Test)

Example 12 with SharedLimiterKey

use of org.apache.gobblin.util.limiter.broker.SharedLimiterKey in project incubator-gobblin by apache.

the class ConfigStoreBasedPolicyTest method test.

@Test
public void test() throws Exception {
    URL prefix = getClass().getResource("/configStore");
    Config config = ConfigFactory.parseMap(ImmutableMap.of(ConfigClientBasedPolicyFactory.CONFIG_KEY_URI_PREFIX_KEY, "simple-" + prefix.toString()));
    SharedResourcesBroker<ThrottlingServerScopes> broker = SharedResourcesBrokerFactory.createDefaultTopLevelBroker(ConfigFactory.empty(), ThrottlingServerScopes.GLOBAL.defaultScopeInstance());
    ConfigClientBasedPolicyFactory policyFactory = new ConfigClientBasedPolicyFactory();
    ThrottlingPolicy policy = policyFactory.createPolicy(new SharedLimiterKey("ConfigBasedPolicyTest/resource1"), broker, config);
    Assert.assertEquals(policy.getClass(), QPSPolicy.class);
    Assert.assertEquals(((QPSPolicy) policy).getQps(), 100);
    policy = policyFactory.createPolicy(new SharedLimiterKey("ConfigBasedPolicyTest/resource2"), broker, config);
    Assert.assertEquals(policy.getClass(), CountBasedPolicy.class);
    Assert.assertEquals(((CountBasedPolicy) policy).getCount(), 50);
}
Also used : Config(com.typesafe.config.Config) SharedLimiterKey(org.apache.gobblin.util.limiter.broker.SharedLimiterKey) URL(java.net.URL) Test(org.testng.annotations.Test)

Example 13 with SharedLimiterKey

use of org.apache.gobblin.util.limiter.broker.SharedLimiterKey in project incubator-gobblin by apache.

the class LimiterServerResourceTest method testLimitedRequests.

@Test
public void testLimitedRequests() {
    ThrottlingPolicyFactory factory = new ThrottlingPolicyFactory();
    SharedLimiterKey res1key = new SharedLimiterKey("res1");
    SharedLimiterKey res2key = new SharedLimiterKey("res2");
    Map<String, String> configMap = ImmutableMap.<String, String>builder().put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, ThrottlingPolicyFactory.POLICY_KEY), CountBasedPolicy.FACTORY_ALIAS).put(BrokerConfigurationKeyGenerator.generateKey(factory, res1key, null, CountBasedPolicy.COUNT_KEY), "100").put(BrokerConfigurationKeyGenerator.generateKey(factory, res2key, null, ThrottlingPolicyFactory.POLICY_KEY), CountBasedPolicy.FACTORY_ALIAS).put(BrokerConfigurationKeyGenerator.generateKey(factory, res2key, null, CountBasedPolicy.COUNT_KEY), "50").build();
    ThrottlingGuiceServletConfig guiceServletConfig = new ThrottlingGuiceServletConfig();
    guiceServletConfig.initialize(ConfigFactory.parseMap(configMap));
    Injector injector = guiceServletConfig.getInjector();
    LimiterServerResource limiterServer = injector.getInstance(LimiterServerResource.class);
    PermitRequest res1request = new PermitRequest();
    res1request.setPermits(20);
    res1request.setResource(res1key.getResourceLimitedPath());
    PermitRequest res2request = new PermitRequest();
    res2request.setPermits(20);
    res2request.setResource(res2key.getResourceLimitedPath());
    PermitRequest res3request = new PermitRequest();
    res3request.setPermits(100000);
    res3request.setResource("res3");
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits(), new Long(20));
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits(), new Long(20));
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits(), new Long(20));
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits(), new Long(20));
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits(), new Long(20));
    try {
        // out of permits
        limiterServer.getSync(new ComplexResourceKey<>(res1request, new EmptyRecord())).getPermits();
        Assert.fail();
    } catch (RestLiServiceException exc) {
        Assert.assertEquals(exc.getStatus(), HttpStatus.S_403_FORBIDDEN);
    }
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res2request, new EmptyRecord())).getPermits(), new Long(20));
    Assert.assertEquals(limiterServer.getSync(new ComplexResourceKey<>(res2request, new EmptyRecord())).getPermits(), new Long(20));
    // out of permits
    try {
        // out of permits
        limiterServer.getSync(new ComplexResourceKey<>(res2request, new EmptyRecord())).getPermits();
        Assert.fail();
    } catch (RestLiServiceException exc) {
        Assert.assertEquals(exc.getStatus(), HttpStatus.S_403_FORBIDDEN);
    }
    // No limit
    Assert.assertTrue(limiterServer.getSync(new ComplexResourceKey<>(res3request, new EmptyRecord())).getPermits() >= res3request.getPermits());
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Injector(com.google.inject.Injector) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) SharedLimiterKey(org.apache.gobblin.util.limiter.broker.SharedLimiterKey) Test(org.testng.annotations.Test)

Aggregations

SharedLimiterKey (org.apache.gobblin.util.limiter.broker.SharedLimiterKey)13 Test (org.testng.annotations.Test)9 SimpleScopeType (org.apache.gobblin.broker.SimpleScopeType)6 Injector (com.google.inject.Injector)4 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)3 NotConfiguredException (org.apache.gobblin.broker.iface.NotConfiguredException)3 EmbeddedRestliServer (org.apache.gobblin.restli.EmbeddedRestliServer)3 SharedRestClientKey (org.apache.gobblin.restli.SharedRestClientKey)3 Meter (com.codahale.metrics.Meter)2 Config (com.typesafe.config.Config)2 MetricContext (org.apache.gobblin.metrics.MetricContext)2 MetricContextFactory (org.apache.gobblin.metrics.broker.MetricContextFactory)2 SubTaggedMetricContextKey (org.apache.gobblin.metrics.broker.SubTaggedMetricContextKey)2 ThrottlingGuiceServletConfig (org.apache.gobblin.restli.throttling.ThrottlingGuiceServletConfig)2 Timer (com.codahale.metrics.Timer)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Closer (com.google.common.io.Closer)1 DataMap (com.linkedin.data.DataMap)1 StringMap (com.linkedin.data.template.StringMap)1 Client (com.linkedin.r2.transport.common.Client)1