use of org.apache.gobblin.util.limiter.MultiLimiter in project incubator-gobblin by apache.
the class MRStressTest method createLimiter.
static Limiter createLimiter(Configuration configuration, SharedResourcesBroker<SimpleScopeType> broker) {
try {
Limiter limiter = new NoopLimiter();
long localQps = configuration.getLong(LOCALLY_ENFORCED_QPS, 0);
if (localQps > 0) {
log.info("Setting up local qps " + localQps);
limiter = new MultiLimiter(limiter, new RateBasedLimiter(localQps));
}
if (configuration.getBoolean(USE_THROTTLING_SERVER, false)) {
log.info("Setting up remote throttling.");
String resourceId = configuration.get(RESOURCE_ID);
Limiter globalLimiter = broker.getSharedResource(new RestliLimiterFactory<SimpleScopeType>(), new SharedLimiterKey(resourceId));
limiter = new MultiLimiter(limiter, globalLimiter);
}
return limiter;
} catch (NotConfiguredException nce) {
throw new RuntimeException(nce);
}
}
use of org.apache.gobblin.util.limiter.MultiLimiter in project incubator-gobblin by apache.
the class StreamThrottler method doThrottleInputStream.
/**
* Throttles an {@link InputStream} if throttling is configured.
* @param inputStream {@link InputStream} to throttle.
* @param sourceURI used for selecting the throttling policy.
* @param targetURI used for selecting the throttling policy.
*/
@Builder(builderMethodName = "throttleInputStream", builderClassName = "InputStreamThrottler")
private ThrottledInputStream doThrottleInputStream(InputStream inputStream, URI sourceURI, URI targetURI) {
Preconditions.checkNotNull(inputStream, "InputStream cannot be null.");
Limiter limiter = new NoopLimiter();
if (sourceURI != null && targetURI != null) {
StreamCopierSharedLimiterKey key = new StreamCopierSharedLimiterKey(sourceURI, targetURI);
try {
limiter = new MultiLimiter(limiter, this.broker.getSharedResource(new SharedLimiterFactory<S>(), key));
} catch (NotConfiguredException nce) {
log.warn("Could not create a Limiter for key " + key, nce);
}
} else {
log.info("Not throttling input stream because source or target URIs are not defined.");
}
Optional<MeteredInputStream> meteredStream = MeteredInputStream.findWrappedMeteredInputStream(inputStream);
if (!meteredStream.isPresent()) {
meteredStream = Optional.of(MeteredInputStream.builder().in(inputStream).build());
inputStream = meteredStream.get();
}
return new ThrottledInputStream(inputStream, limiter, meteredStream.get());
}
use of org.apache.gobblin.util.limiter.MultiLimiter in project incubator-gobblin by apache.
the class SharedLimiterFactoryTest method testMultiLevelLimiter.
@Test
public void testMultiLevelLimiter() throws Exception {
SharedResourcesBrokerImpl<SimpleScopeType> broker = getBrokerForConfigMap(ImmutableMap.of(JOINER.join(BrokerConstants.GOBBLIN_BROKER_CONFIG_PREFIX, SharedLimiterFactory.NAME, SimpleScopeType.GLOBAL, SharedLimiterFactory.LIMITER_CLASS_KEY), "CountBasedLimiter", JOINER.join(BrokerConstants.GOBBLIN_BROKER_CONFIG_PREFIX, SharedLimiterFactory.NAME, SimpleScopeType.GLOBAL, CountBasedLimiter.Factory.COUNT_KEY), "10", JOINER.join(BrokerConstants.GOBBLIN_BROKER_CONFIG_PREFIX, SharedLimiterFactory.NAME, SimpleScopeType.LOCAL, SharedLimiterFactory.LIMITER_CLASS_KEY), "CountBasedLimiter", JOINER.join(BrokerConstants.GOBBLIN_BROKER_CONFIG_PREFIX, SharedLimiterFactory.NAME, SimpleScopeType.LOCAL, CountBasedLimiter.Factory.COUNT_KEY), "5"));
SharedResourcesBroker<SimpleScopeType> localBroker1 = broker.newSubscopedBuilder(new SimpleScope<>(SimpleScopeType.LOCAL, "local1")).build();
SharedResourcesBroker<SimpleScopeType> localBroker2 = broker.newSubscopedBuilder(new SimpleScope<>(SimpleScopeType.LOCAL, "local2")).build();
SharedLimiterFactory<SimpleScopeType> factory = new SharedLimiterFactory<>();
Limiter limiter1 = ((ResourceInstance<Limiter>) factory.createResource(localBroker1, broker.getConfigView(SimpleScopeType.LOCAL, new SharedLimiterKey("resource"), factory.getName()))).getResource();
Limiter limiter2 = ((ResourceInstance<Limiter>) factory.createResource(localBroker2, broker.getConfigView(SimpleScopeType.LOCAL, new SharedLimiterKey("resource"), factory.getName()))).getResource();
Assert.assertTrue(limiter1 instanceof MultiLimiter);
Assert.assertTrue(limiter2 instanceof MultiLimiter);
Assert.assertEquals(((CountBasedLimiter) ((MultiLimiter) limiter1).getUnderlyingLimiters().get(0)).getCountLimit(), 5);
Assert.assertEquals(((CountBasedLimiter) ((MultiLimiter) limiter1).getUnderlyingLimiters().get(1)).getCountLimit(), 10);
Assert.assertEquals(((CountBasedLimiter) ((MultiLimiter) limiter2).getUnderlyingLimiters().get(0)).getCountLimit(), 5);
Assert.assertEquals(((CountBasedLimiter) ((MultiLimiter) limiter2).getUnderlyingLimiters().get(1)).getCountLimit(), 10);
Assert.assertNotEquals(((MultiLimiter) limiter1).getUnderlyingLimiters().get(0), ((MultiLimiter) limiter2).getUnderlyingLimiters().get(0));
Assert.assertEquals(((MultiLimiter) limiter1).getUnderlyingLimiters().get(1), ((MultiLimiter) limiter2).getUnderlyingLimiters().get(1));
}
use of org.apache.gobblin.util.limiter.MultiLimiter in project incubator-gobblin by apache.
the class SharedLimiterFactory method createResource.
@Override
public SharedResourceFactoryResponse<Limiter> createResource(SharedResourcesBroker<S> broker, ScopedConfigView<S, SharedLimiterKey> configView) throws NotConfiguredException {
Config config = configView.getConfig();
SharedLimiterKey.GlobalLimiterPolicy globalLimiterPolicy = configView.getKey().getGlobalLimiterPolicy();
if (ConfigUtils.getBoolean(config, SKIP_GLOBAL_LIMITER_KEY, false)) {
if (globalLimiterPolicy != SharedLimiterKey.GlobalLimiterPolicy.LOCAL_ONLY) {
SharedLimiterKey modifiedKey = new SharedLimiterKey(configView.getKey().getResourceLimitedPath(), SharedLimiterKey.GlobalLimiterPolicy.LOCAL_ONLY);
return new ResourceCoordinate<>(this, modifiedKey, (S) configView.getScope());
}
} else if (config.hasPath(FAIL_IF_NO_GLOBAL_LIMITER_KEY) && config.getBoolean(FAIL_IF_NO_GLOBAL_LIMITER_KEY) && globalLimiterPolicy != SharedLimiterKey.GlobalLimiterPolicy.USE_GLOBAL) {
// if user has specified FAIL_IF_NO_GLOBAL_LIMITER_KEY, promote the policy from USE_GLOBAL_IF_CONFIGURED to USE_GLOBAL
// e.g. fail if no GLOBAL configuration is present
SharedLimiterKey modifiedKey = new SharedLimiterKey(configView.getKey().getResourceLimitedPath(), SharedLimiterKey.GlobalLimiterPolicy.USE_GLOBAL);
return new ResourceCoordinate<>(this, modifiedKey, (S) configView.getScope());
}
Limiter limiter;
if (!configView.getScope().isLocal() && !globalLimiterPolicy.equals(SharedLimiterKey.GlobalLimiterPolicy.LOCAL_ONLY)) {
try {
Class<?> klazz = Class.forName("org.apache.gobblin.util.limiter.RestliLimiterFactory");
return new ResourceCoordinate<>((SharedResourceFactory<Limiter, SharedLimiterKey, S>) klazz.newInstance(), configView.getKey(), (S) configView.getScope());
} catch (ReflectiveOperationException roe) {
if (globalLimiterPolicy.equals(SharedLimiterKey.GlobalLimiterPolicy.USE_GLOBAL)) {
throw new RuntimeException("There is no Global limiter factory in the classpath.");
}
}
}
if (config.hasPath(LIMITER_CLASS_KEY)) {
try {
LimiterFactory factory = RESOLVER.resolveClass(config.getString(LIMITER_CLASS_KEY)).newInstance();
limiter = factory.buildLimiter(config);
} catch (ReflectiveOperationException roe) {
throw new RuntimeException(roe);
}
} else {
if (config.hasPath(FAIL_ON_UNKNOWN_RESOURCE_ID) && config.getBoolean(FAIL_ON_UNKNOWN_RESOURCE_ID)) {
throw new NotConfiguredException();
}
limiter = new NoopLimiter();
}
ScopeType<S> scope = configView.getScope();
Collection<S> parentScopes = scope.parentScopes();
if (parentScopes != null) {
try {
for (S parentScope : parentScopes) {
limiter = new MultiLimiter(limiter, broker.getSharedResourceAtScope(this, configView.getKey(), parentScope));
}
} catch (NoSuchScopeException nsse) {
throw new RuntimeException("Could not get higher scope limiter. This is an error in code.", nsse);
}
}
return new ResourceInstance<>(limiter);
}
Aggregations