Refine search
@Override protected RateLimiter createRateLimiter(String key, double permits, TimeUnit timeUnit) { long seconds = timeUnit.toSeconds(1); double permitsPerSecond = permits; if (seconds > 0) { permitsPerSecond = permits / timeUnit.toSeconds(1); } else { if (timeUnit == TimeUnit.MILLISECONDS) { permitsPerSecond = permits / 1000D; } } com.google.common.util.concurrent.RateLimiter rateLimiter = com.google.common.util.concurrent.RateLimiter.create(permitsPerSecond); return rateLimiter::tryAcquire; } }
private long measureTotalTimeMillis(RateLimiter rateLimiter, int permits, Random random) { long startTime = stopwatch.instant; while (permits > 0) { int nextPermitsToAcquire = Math.max(1, random.nextInt(permits)); permits -= nextPermitsToAcquire; rateLimiter.acquire(nextPermitsToAcquire); } rateLimiter.acquire(1); // to repay for any pending debt return NANOSECONDS.toMillis(stopwatch.instant - startTime); }
public void testImmediateTryAcquire() { RateLimiter r = RateLimiter.create(1); assertTrue("Unable to acquire initial permit", r.tryAcquire()); assertFalse("Capable of acquiring secondary permit", r.tryAcquire()); }
@Override public void setThrottleMarkDelete(double throttleMarkDelete) { if (throttleMarkDelete > 0.0) { if (markDeleteLimiter == null) { markDeleteLimiter = RateLimiter.create(throttleMarkDelete); } else { this.markDeleteLimiter.setRate(throttleMarkDelete); } } else { // Disable mark-delete rate limiter markDeleteLimiter = null; } }
public void testBurstyAndUpdate() { RateLimiter rateLimiter = RateLimiter.create(1.0, stopwatch); rateLimiter.acquire(1); // no wait rateLimiter.acquire(1); // R1.00, to repay previous rateLimiter.setRate(2.0); // update the rate! rateLimiter.acquire(1); // R1.00, to repay previous (the previous was under the old rate!) rateLimiter.acquire(2); // R0.50, to repay previous (now the rate takes effect) rateLimiter.acquire(4); // R1.00, to repay previous rateLimiter.acquire(1); // R2.00, to repay previous assertEvents("R0.00", "R1.00", "R1.00", "R0.50", "R1.00", "R2.00"); }
public void testSimpleWeights() { RateLimiter rateLimiter = RateLimiter.create(1.0, stopwatch); rateLimiter.acquire(1); // no wait rateLimiter.acquire(1); // R1.00, to repay previous rateLimiter.acquire(2); // R1.00, to repay previous rateLimiter.acquire(4); // R2.00, to repay previous rateLimiter.acquire(8); // R4.00, to repay previous rateLimiter.acquire(1); // R8.00, to repay previous assertEvents("R0.00", "R1.00", "R1.00", "R2.00", "R4.00", "R8.00"); }
private void processLog(long cursor) { long iterate = iterateFrom.longValue(); if (cursor < iterate) { if (LOG_LIMITER.tryAcquire()) { LOGGER.info("replay message log failed,cursor < iterate,cursor:{},iterate:{}", cursor, iterate); TimeUnit.MILLISECONDS.sleep(5); } catch (InterruptedException e) { LOGGER.warn("message log iterate sleep interrupted");
public int replay(RateLimiter rateLimiter, Set<InetAddress> hintedNodes) throws IOException { logger.trace("Replaying batch {}", id); if (mutations.isEmpty()) return 0; int gcgs = gcgs(mutations); if (TimeUnit.MILLISECONDS.toSeconds(writtenAt) + gcgs <= FBUtilities.nowInSeconds()) return 0; replayHandlers = sendReplays(mutations, writtenAt, hintedNodes); rateLimiter.acquire(replayedBytes); // acquire afterwards, to not mess up ttl calculation. return replayHandlers.size(); }
() -> Maps.immutableEntry(potentialLeader.getUUID(), potentialLeader))); } catch (RejectedExecutionException e) { if (logRateLimiter.tryAcquire()) { log.info("Leader ping executor rejected task. " + "This may signal a problem with cluster if happens frequently"); try { long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(leaderPingResponseWaitMs); for (;;) { try {
@Test(timeout = 60000) public void testMultiReaders() throws Exception { String name = "distrlog-multireaders"; final RateLimiter limiter = RateLimiter.create(1000); TimeUnit.SECONDS.sleep(5); new Object[] { writeCount.get(), readerThreads[0].getReadCount(), reader0.getReadAheadPosition() }); TimeUnit.MILLISECONDS.sleep(100);
public void testAcquireParameterValidation() { RateLimiter limiter = RateLimiter.create(999); try { limiter.acquire(0); fail(); } catch (IllegalArgumentException expected) { limiter.acquire(-1); fail(); } catch (IllegalArgumentException expected) { limiter.tryAcquire(0); fail(); } catch (IllegalArgumentException expected) { limiter.tryAcquire(-1); fail(); } catch (IllegalArgumentException expected) { limiter.tryAcquire(0, 1, SECONDS); fail(); } catch (IllegalArgumentException expected) { limiter.tryAcquire(-1, 1, SECONDS); fail(); } catch (IllegalArgumentException expected) {
/** * Acquires a permit from this {@link RateLimiter} if it can be acquired immediately without * delay. * * <p>This method is equivalent to {@code tryAcquire(1)}. * * @return {@code true} if the permit was acquired, {@code false} otherwise * @since 14.0 */ public boolean tryAcquire() { return tryAcquire(1, 0, MICROSECONDS); }
// Allow one request per second private RateLimiter throttle = RateLimiter.create(1.0); private void someMethod() { throttle.acquire(); // Do something }
public void testCreateWarmupParameterValidation() { RateLimiter unused; unused = RateLimiter.create(1.0, 1, NANOSECONDS); unused = RateLimiter.create(1.0, 0, NANOSECONDS); try { RateLimiter.create(0.0, 1, NANOSECONDS); fail(); } catch (IllegalArgumentException expected) { } try { RateLimiter.create(1.0, -1, NANOSECONDS); fail(); } catch (IllegalArgumentException expected) { } }
public void testSimpleRateUpdate() { RateLimiter limiter = RateLimiter.create(5.0, 5, SECONDS); assertEquals(5.0, limiter.getRate()); limiter.setRate(10.0); assertEquals(10.0, limiter.getRate()); try { limiter.setRate(0.0); fail(); } catch (IllegalArgumentException expected) { } try { limiter.setRate(-10.0); fail(); } catch (IllegalArgumentException expected) { } }
@VisibleForTesting static RateLimiter create( double permitsPerSecond, long warmupPeriod, TimeUnit unit, double coldFactor, SleepingStopwatch stopwatch) { RateLimiter rateLimiter = new SmoothWarmingUp(stopwatch, warmupPeriod, unit, coldFactor); rateLimiter.setRate(permitsPerSecond); return rateLimiter; }
double previousRate = queryQuotaConfig.getRateLimiter().getRate(); if (Math.abs(latestRate - previousRate) > 0.001) { queryQuotaConfig.getRateLimiter().setRate(latestRate); LOGGER.info( "Rate limiter for table: {} has been updated. Overall rate: {}. Previous per-broker rate: {}. New per-broker rate: {}. Number of online broker instances: {}",
/** * Try to acquire token from rate limiter. Emit the utilization of the qps quota if broker metric isn't null. * @param tableNameWithType table name with type. * @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. */ private boolean tryAcquireToken(String tableNameWithType, QueryQuotaConfig queryQuotaConfig) { // Use hit counter to count the number of hits. queryQuotaConfig.getHitCounter().hit(); RateLimiter rateLimiter = queryQuotaConfig.getRateLimiter(); double perBrokerRate = rateLimiter.getRate(); // Emit the qps capacity utilization rate. int numHits = queryQuotaConfig.getHitCounter().getHitCount(); if (_brokerMetrics != null) { int percentageOfCapacityUtilization = (int) (numHits * 100 / perBrokerRate); LOGGER.debug("The percentage of rate limit capacity utilization is {}", percentageOfCapacityUtilization); _brokerMetrics.setValueOfTableGauge(tableNameWithType, BrokerGauge.QUERY_QUOTA_CAPACITY_UTILIZATION_RATE, percentageOfCapacityUtilization); } if (!rateLimiter.tryAcquire()) { LOGGER.info("Quota is exceeded for table: {}. Per-broker rate: {}. Current qps: {}", tableNameWithType, perBrokerRate, numHits); return false; } // Token is successfully acquired. return true; }
@Override public String toString() { return String.format(Locale.ROOT, "RateLimiter[stableRate=%3.1fqps]", getRate()); }
private ApolloConfig loadApolloConfig() { if (!m_loadConfigRateLimiter.tryAcquire(5, TimeUnit.SECONDS)) { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(onErrorSleepTime); } catch (InterruptedException e) {