Example usage for com.google.common.util.concurrent RateLimiter getRate

List of usage examples for com.google.common.util.concurrent RateLimiter getRate

Introduction

In this page you can find the example usage for com.google.common.util.concurrent RateLimiter getRate.

Prototype

public final double getRate() 

Source Link

Document

Returns the stable rate (as permits per seconds ) with which this RateLimiter is configured with.

Usage

From source file:com.magnet.mmx.server.plugin.mmxmgmt.monitoring.RateLimiterService.java

public static boolean isAllowed(RateLimiterDescriptor descriptor) {
    if (descriptor.getPermitsPerSecond() <= 0) {
        LOGGER.trace("isAllowed : rate limiting disabled");
        return true;
    }//from   w  w w  .  ja v  a  2s . c  om
    try {
        LOGGER.trace("isAllowed : getting ratelimiter for descriptor={}", descriptor);
        RateLimiter r = getOrCreate(descriptor);
        LOGGER.trace("isAllowed : retireved ratelimiter={}, rate={}", r, r.getRate());
        return r.tryAcquire();
    } catch (ExecutionException e) {
        LOGGER.error("isAllowed : Caught exception getting RateLimiter for descriptor : {}", descriptor, e);
        return false;
    }
}

From source file:com.pinterest.pinlater.QueueRateLimiter.java

public static IFace create(double maxRequestsPerSecond) {
    if (maxRequestsPerSecond <= 0.0) {
        return ALLOW_NONE;
    }//from  www  .j av  a  2  s. c o m

    final RateLimiter rateLimiter = RateLimiter.create(maxRequestsPerSecond);
    return new IFace() {
        @Override
        public boolean allowDequeue(int numJobs) {
            return rateLimiter.tryAcquire(numJobs);
        }

        @Override
        public double getRate() {
            return rateLimiter.getRate();
        }
    };
}

From source file:com.spotify.styx.StyxScheduler.java

private static void updateRuntimeConfig(Storage storage, RateLimiter rateLimiter) {
    try {/*w  ww  .j  a  v a2 s  . co  m*/
        double currentRate = rateLimiter.getRate();
        Double updatedRate = storage.submissionRateLimit()
                .orElse(StyxScheduler.DEFAULT_SUBMISSION_RATE_PER_SEC);
        if (Double.compare(updatedRate, currentRate) != 0) {
            LOG.info("Updating submission rate limit: {} -> {}", currentRate, updatedRate);
            rateLimiter.setRate(updatedRate);
        }
    } catch (IOException e) {
        LOG.warn("Failed to fetch the submission rate config from storage, " + "skipping RateLimiter update");
    }
}

From source file:com.ibm.og.scheduling.ConcurrentRequestScheduler.java

/**
 * Constructs an instance with the provided concurrency
 * //from w ww. j  a va 2  s. c om
 * @param concurrentRequests the number of concurrent requests allowed
 * @throws IllegalArgumentException if concurrentRequests is negative or zero
 */
public ConcurrentRequestScheduler(final int concurrentRequests, final double rampup,
        final TimeUnit rampupUnit) {
    checkArgument(concurrentRequests > 0, "concurrentRequests must be > 0");
    checkArgument(rampup >= 0.0, "rampup must be >= 0.0 [%s]", rampup);
    checkNotNull(rampupUnit);
    this.concurrentRequests = concurrentRequests;
    this.rampup = rampup;
    this.rampupUnit = rampupUnit;
    this.started = new CountDownLatch(1);

    if (DoubleMath.fuzzyEquals(rampup, 0.0, Math.pow(0.1, 6))) {
        this.permits = new Semaphore(concurrentRequests);
    } else {
        this.permits = new Semaphore(0);
        final Thread rampupThread = new Thread(new Runnable() {
            @Override
            public void run() {
                final double rampSeconds = (rampup * rampupUnit.toNanos(1)) / TimeUnit.SECONDS.toNanos(1);
                _logger.debug("Ramp seconds [{}]", rampSeconds);

                final RateLimiter ramp = RateLimiter.create(concurrentRequests / rampSeconds);
                _logger.debug("Ramp rate [{}]", ramp.getRate());

                _logger.debug("Awaiting start latch");
                Uninterruptibles.awaitUninterruptibly(ConcurrentRequestScheduler.this.started);

                _logger.info("Starting ramp");
                for (int i = 0; i < concurrentRequests; i++) {
                    _logger.debug("Acquiring RateLimiter permit");
                    ramp.acquire();
                    _logger.debug("Releasing semaphore permit");
                    ConcurrentRequestScheduler.this.permits.release();
                }
                _logger.info("Finished ramp");
            }
        }, "concurrent-scheduler-ramp");
        rampupThread.setDaemon(true);
        rampupThread.start();
        _logger.debug("Starting permits [{}]", this.permits.availablePermits());
    }
}

From source file:com.linkedin.pinot.broker.queryquota.TableQueryQuotaManager.java

/**
 * Try to acquire token from rate limiter. Emit the utilization of the qps quota if broker metric isn't null.
 * @param tableName origin table name, which could be raw.
 * @param queryQuotaConfig query quota config for type-specific table.
 * @return true if there's no qps quota for that table, or a token is acquired successfully.
 *//*from   w  ww  .ja v a 2  s  .c  o m*/
private boolean tryAcquireToken(String tableName, QueryQuotaConfig queryQuotaConfig) {
    // Use hit counter to count the number of hits.
    queryQuotaConfig.getHitCounter().hit();

    RateLimiter rateLimiter = queryQuotaConfig.getRateLimiter();
    double perBrokerRate = rateLimiter.getRate();
    if (!rateLimiter.tryAcquire()) {
        LOGGER.info("Quota is exceeded for table: {}. Per-broker rate: {}", tableName, perBrokerRate);
        return false;
    }

    // Emit the qps capacity utilization rate.
    if (_brokerMetrics != null) {
        int numHits = queryQuotaConfig.getHitCounter().getHitCount();
        int percentageOfCapacityUtilization = (int) (numHits * 100 / perBrokerRate);
        LOGGER.debug("The percentage of rate limit capacity utilization is {}",
                percentageOfCapacityUtilization);
        _brokerMetrics.setValueOfTableGauge(tableName, BrokerGauge.QUERY_QUOTA_CAPACITY_UTILIZATION_RATE,
                percentageOfCapacityUtilization);
    }

    // Token is successfully acquired.
    return true;
}

From source file:io.warp10.continuum.ThrottlingManager.java

public static Map<String, Object> getLimits(String producer, String app) {
    Map<String, Object> limits = new HashMap<String, Object>();

    RateLimiter producerLimiter = producerRateLimiters.get(producer);
    RateLimiter applicationLimiter = applicationRateLimiters.get(app);

    Long oProducerLimit = producerMADSLimits.get(producer);
    Long oApplicationLimit = applicationMADSLimits.get(app);

    long producerLimit = Long.MAX_VALUE;
    long applicationLimit = Long.MAX_VALUE;

    HyperLogLogPlus prodHLLP = producerHLLPEstimators.get(producer);
    HyperLogLogPlus appHLLP = applicationHLLPEstimators.get(app);

    if (null != producerLimiter) {
        limits.put(LIMITS_PRODUCER_RATE_CURRENT, producerLimiter.getRate());
    }//from ww  w.j  a va 2  s .co  m

    if (null != applicationLimiter) {
        limits.put(LIMITS_APPLICATION_RATE_CURRENT, applicationLimiter.getRate());
    }

    if (null != oProducerLimit) {
        limits.put(LIMITS_PRODUCER_MADS_LIMIT, oProducerLimit);
        producerLimit = (long) oProducerLimit;
    }

    if (null != oApplicationLimit) {
        limits.put(LIMITS_APPLICATION_MADS_LIMIT, oApplicationLimit);
        applicationLimit = (long) oApplicationLimit;
    }

    if (null != prodHLLP) {
        try {
            long cardinality = prodHLLP.cardinality();

            // Change cardinality so it is capped by 'producerLimit', we don't want to expose the
            // toleranceRatio

            if (cardinality > producerLimit) {
                cardinality = producerLimit;
            }

            limits.put(LIMITS_PRODUCER_MADS_CURRENT, cardinality);
        } catch (IOException ioe) {
        }
    }

    if (null != appHLLP) {
        try {
            long cardinality = appHLLP.cardinality();

            // Change cardinality so it is capped by 'producerLimit', we don't want to expose the
            // toleranceRatio

            if (cardinality > applicationLimit) {
                cardinality = applicationLimit;
            }

            limits.put(LIMITS_APPLICATION_MADS_CURRENT, cardinality);
        } catch (IOException ioe) {
        }
    }

    return limits;
}

From source file:com.netflix.ndbench.core.NdBenchDriver.java

private void checkAndInitRateLimit(AtomicReference<RateLimiter> rateLimiter, int property, String prop) {
    RateLimiter oldLimiter = rateLimiter.get();
    if (oldLimiter == null) {
        Logger.info("Setting rate Limit for: " + prop + " to: " + property);
        rateLimiter.set(RateLimiter.create(property));
        return;/*from  w  ww .  j a va2  s.  c om*/
    }

    int oldLimit = Double.valueOf(oldLimiter.getRate()).intValue();
    int newLimit = property;
    if (oldLimit != newLimit) {
        Logger.info("Updating rate Limit for: " + prop + " to: " + newLimit);
        rateLimiter.set(RateLimiter.create(newLimit));
    }
}

From source file:io.warp10.continuum.ThrottlingManager.java

/**
 * Validate the ingestion of datapoints against the DDP limit
 * //w w  w .  j a  v  a2  s  .  com
 * We tolerate inputs only if they wouldn't incur a wait greater than 2 seconds
 * 
 * @param producer
 * @param owner
 * @param application
 * @param count
 * @param maxwait Max wait per datapoint
 */
public static void checkDDP(Metadata metadata, String producer, String owner, String application, int count,
        long maxwait) throws WarpException {
    if (!loaded) {
        return;
    }

    //
    // Extract RateLimiter
    //

    RateLimiter producerLimiter = producerRateLimiters.get(producer);
    RateLimiter applicationLimiter = applicationRateLimiters.get(application);

    //
    // TODO(hbs): store per producer/per app maxwait values? Extract them from the throttling file?
    //

    long appMaxWait = maxwait;
    long producerMaxWait = maxwait;

    // -1.0 as the default rate means do not enforce DDP limit
    if (null == producerLimiter && null == applicationLimiter && -1.0D == DEFAULT_RATE_PRODUCER) {
        return;
    } else if (null == producerLimiter) {
        // Create a rate limiter with the default rate      
        producerLimiter = RateLimiter.create(Math.max(MINIMUM_RATE_LIMIT, DEFAULT_RATE_PRODUCER));
        producerRateLimiters.put(producer, producerLimiter);
    }

    // Check per application limiter
    if (null != applicationLimiter) {
        synchronized (applicationLimiter) {
            if (!applicationLimiter.tryAcquire(count, appMaxWait * count, TimeUnit.MILLISECONDS)) {
                StringBuilder sb = new StringBuilder();
                sb.append("Storing data for ");
                if (null != metadata) {
                    GTSHelper.metadataToString(sb, metadata.getName(), metadata.getLabels());
                }
                sb.append(" would incur a wait greater than ");
                sb.append(appMaxWait);
                sb.append(
                        " ms per datapoint due to your Daily Data Points limit being already exceeded for application '"
                                + application + "'. Current max rate is " + applicationLimiter.getRate()
                                + " datapoints/s.");

                Map<String, String> labels = new HashMap<String, String>();
                labels.put(SensisionConstants.SENSISION_LABEL_APPLICATION, application);
                Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_THROTTLING_RATE_PER_APP, labels,
                        1);
                Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_THROTTLING_RATE_PER_APP_GLOBAL,
                        Sensision.EMPTY_LABELS, 1);

                throw new WarpException(sb.toString());
            }
        }
    }

    synchronized (producerLimiter) {
        if (!producerLimiter.tryAcquire(count, producerMaxWait * count, TimeUnit.MILLISECONDS)) {
            StringBuilder sb = new StringBuilder();
            sb.append("Storing data for ");
            if (null != metadata) {
                GTSHelper.metadataToString(sb, metadata.getName(), metadata.getLabels());
            }
            sb.append(" would incur a wait greater than ");
            sb.append(producerMaxWait);
            sb.append(
                    " ms per datapoint due to your Daily Data Points limit being already exceeded. Current maximum rate is "
                            + producerLimiter.getRate() + " datapoints/s.");

            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_PRODUCER, producer);
            Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_THROTTLING_RATE, labels, 1);
            Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_THROTTLING_RATE_GLOBAL,
                    Sensision.EMPTY_LABELS, 1);

            throw new WarpException(sb.toString());
        }
    }
}