Search in sources :

Example 1 with FilterConfiguration

use of org.wikidata.query.rdf.blazegraph.filters.FilterConfiguration in project wikidata-query-rdf by wikimedia.

the class SystemOverloadFilter method init.

@Override
public void init(FilterConfig filterConfig) throws ServletException {
    super.init(filterConfig);
    try {
        operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
    } catch (IllegalArgumentException e) {
        operatingSystemMXBean = null;
        logger.error("Could not load {}.", OperatingSystemMXBean.class.getSimpleName(), e);
    }
    FilterConfiguration config = new FilterConfiguration(filterConfig, WDQS_CONFIG_PREFIX);
    systemLoadLowLimit = config.loadDoubleParam("system-load-low-limit", -1.0);
    systemLoadHighLimit = config.loadDoubleParam("system-load-high-limit", -1.0);
    if (systemLoadLowLimit > systemLoadHighLimit) {
        throw new ServletException("system-load-low-limit should be lower than system-load-high-limit");
    }
    enableFilterIfHeader = config.loadStringParam("enable-if-header");
}
Also used : ServletException(javax.servlet.ServletException) FilterConfiguration(org.wikidata.query.rdf.blazegraph.filters.FilterConfiguration)

Example 2 with FilterConfiguration

use of org.wikidata.query.rdf.blazegraph.filters.FilterConfiguration in project wikidata-query-rdf by wikimedia.

the class ThrottlingFilter method init.

/**
 * Initialise the filter.
 *
 * The following parameters are available (see
 * {@link org.isomorphism.util.TokenBucket} for the details on bucket
 * configuration, see implementation for the default values):
 * <ul>
 *     <li>{@code request-duration-threshold-in-millis}: requests longer
 *     than this threshold will start the tracking for this user</li>
 *
 *     <li>{@code time-bucket-capacity-in-seconds},
 *     {@code time-bucket-refill-amount-in-seconds},
 *     {@code time-bucket-refill-period-in-minutes}: configuration of the
 *     bucket tracking request durations</li>
 *
 *     <li>{@code error-bucket-capacity},
 *     {@code error-bucket-refill-amount},
 *     {@code error-bucket-refill-period-in-minutes}: configuration of the
 *     bucket tracking errors</li>
 *
 *     <li>{@code throttle-bucket-capacity},
 *     {@code throttle-bucket-refill-amount},
 *     {@code throttle-bucket-refill-period-in-minutes}: configuration of
 *     the bucket tracking throttling</li>
 *
 *     <li>{@code ban-duration-in-minutes}: how long should a user be
 *     banned when a ban is triggered</li>
 *
 *     <li>{@code max-state-size}: how many users to track</li>
 *     <li>{@code state-expiration-in-minutes}: tracking of a user expires
 *     after this duration</li>
 *
 *     <li>{@code enable-throttling-if-header}: enable the throttling on
 *     the requests which have this header set</li>
 *     <li>{@code enable-ban-if-header}: enable the banning on the requests
 *     which have this header set</li>
 *     <li>{@code always-throttle-param}: always throttle requests where
 *     this parameter is set (useful for testing)</li>
 *     <li>{@code always-ban-param}: always ban requests where this
 *     parameter is set (useful for testing)</li>
 *
 *     <li>{@code enabled}: entirely disable this filter if set to
 *     false</li>
 * </ul>
 *
 * See {@link FilterConfiguration#loadStringParam(String)} for
 * the details of where the configuration is loaded from.
 *
 * @param filterConfig {@inheritDoc}
 */
@Override
public void init(FilterConfig filterConfig) throws ServletException {
    super.init(filterConfig);
    ThrottlingFilterConfig config = new ThrottlingFilterConfig(new FilterConfiguration(filterConfig, FilterConfiguration.WDQS_CONFIG_PREFIX));
    this.enabled = config.isFilterEnabled();
    this.userAgentIpBucketing = new UserAgentIpAddressBucketing();
    this.regexBucketing = new RegexpBucketing(loadRegexPatterns(config.getRegexPatternsFile()), r -> r.getParameter("query"));
    this.agentBucketing = new RegexpBucketing(loadRegexPatterns(config.getAgentPatternsFile()), r -> r.getHeader("User-Agent"));
    stateStore = CacheBuilder.newBuilder().maximumSize(config.getMaxStateSize()).expireAfterAccess(config.getStateExpiration().toMillis(), MILLISECONDS).build();
    Callable<ThrottlingState> stateInitializer = createThrottlingState(config.getTimeBucketCapacity(), config.getTimeBucketRefillAmount(), config.getTimeBucketRefillPeriod(), config.getErrorBucketCapacity(), config.getErrorBucketRefillAmount(), config.getErrorBucketRefillPeriod(), config.getThrottleBucketCapacity(), config.getThrottleBucketRefillAmount(), config.getThrottleBucketRefillPeriod(), config.getBanDuration());
    timeAndErrorsThrottler = new TimeAndErrorsThrottler<>(config.getRequestDurationThreshold(), stateInitializer, stateStore, config.getEnableThrottlingIfHeader(), config.getAlwaysThrottleParam(), Clock.systemUTC());
    banThrottler = new BanThrottler<>(stateInitializer, stateStore, config.getEnableBanIfHeader(), config.getAlwaysBanParam(), Clock.systemUTC());
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) FilterChain(javax.servlet.FilterChain) Instant.now(java.time.Instant.now) ServletException(javax.servlet.ServletException) Stopwatch(com.google.common.base.Stopwatch) LoggerFactory(org.slf4j.LoggerFactory) Callable(java.util.concurrent.Callable) HttpServletRequest(javax.servlet.http.HttpServletRequest) ImmutableList(com.google.common.collect.ImmutableList) MonitoredFilter(org.wikidata.query.rdf.blazegraph.filters.MonitoredFilter) Duration(java.time.Duration) Filter(javax.servlet.Filter) ENGLISH(java.util.Locale.ENGLISH) Path(java.nio.file.Path) ServletRequest(javax.servlet.ServletRequest) PatternSyntaxException(java.util.regex.PatternSyntaxException) Logger(org.slf4j.Logger) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) ISO_INSTANT(java.time.format.DateTimeFormatter.ISO_INSTANT) IOException(java.io.IOException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Instant(java.time.Instant) String.format(java.lang.String.format) Objects(java.util.Objects) FilterConfiguration(org.wikidata.query.rdf.blazegraph.filters.FilterConfiguration) Stream(java.util.stream.Stream) TokenBuckets(org.isomorphism.util.TokenBuckets) Paths(java.nio.file.Paths) ServletResponse(javax.servlet.ServletResponse) FilterConfig(javax.servlet.FilterConfig) Clock(java.time.Clock) Pattern(java.util.regex.Pattern) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) FilterConfiguration(org.wikidata.query.rdf.blazegraph.filters.FilterConfiguration)

Aggregations

ServletException (javax.servlet.ServletException)2 FilterConfiguration (org.wikidata.query.rdf.blazegraph.filters.FilterConfiguration)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Stopwatch (com.google.common.base.Stopwatch)1 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Clock (java.time.Clock)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 Instant.now (java.time.Instant.now)1 ISO_INSTANT (java.time.format.DateTimeFormatter.ISO_INSTANT)1 Collection (java.util.Collection)1