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