@Override public void doRun(Limiter limiter) throws InterruptedException { int ops = 0; while (ops < this.opsTarget) { limiter.acquirePermits(1); ops++; } } }
@Test public void testThrottling() throws InterruptedException { Limiter limiter = new CountBasedLimiter(10); limiter.start(); for (int i = 0; i < 10; i++) { Assert.assertTrue(limiter.acquirePermits(1) != null); } Assert.assertTrue(limiter.acquirePermits(1) == null); limiter.stop(); } }
@Override public void stop() { for (Limiter limiter : this.underlyingLimiters) { limiter.stop(); } } }
@Override public void start() { for (Limiter limiter : this.underlyingLimiters) { limiter.start(); } }
@Override public void close() throws IOException { try { this.extractor.close(); } finally { this.limiter.stop(); } } }
@Override public void start() { this.underlying.start(); RateComputingLimiterContainer.this.subLimiterPermitCounts.add(this.localPermitCount); }
@Override protected void map(Text key, NullWritable value, Context context) throws IOException, InterruptedException { try { Configuration configuration = context.getConfiguration(); Stressor stressor = context.getConfiguration().getClass(StressTestUtils.STRESSOR_CLASS, StressTestUtils.DEFAULT_STRESSOR_CLASS, Stressor.class).newInstance(); stressor.configure(context.getConfiguration()); RateComputingLimiterContainer limiterContainer = new RateComputingLimiterContainer(); Limiter limiter = limiterContainer.decorateLimiter(createLimiter(configuration, this.broker)); ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); ScheduledFuture<?> future = executor.scheduleAtFixedRate(new Recorder(limiterContainer, context, true), 0, 15, TimeUnit.SECONDS); limiter.start(); stressor.run(limiter); limiter.stop(); future.cancel(false); ExecutorsUtils.shutdownExecutorService(executor, Optional.<Logger>absent(), 10, TimeUnit.SECONDS); } catch (ReflectiveOperationException roe) { throw new IOException(roe); } } }
@Override public Closeable acquirePermits(long permits) throws InterruptedException { Closeable closeable = this.underlying.acquirePermits(permits); this.localPermitCount.addAndGet(permits); return closeable; }
@Override public void stop() { this.underlying.stop(); if (this.underlying instanceof RestliServiceBasedLimiter) { RestliServiceBasedLimiter restliLimiter = (RestliServiceBasedLimiter) this.underlying; RateComputingLimiterContainer.this.unusedPermitsCounts.add(restliLimiter.getUnusedPermits()); log.info("Unused permits: " + restliLimiter.getUnusedPermits()); } RateComputingLimiterContainer.this.subLimiterPermitCounts.remove(this.localPermitCount); } }
public void startRateControl() throws ExecutionException { getRateLimiter().start(); }
@Override public void doRun(Limiter limiter) throws InterruptedException { long runForSeconds = new Random().nextInt(180) + 1; long endTime = System.currentTimeMillis() + runForSeconds * 1000; while (System.currentTimeMillis() < endTime) { limiter.acquirePermits(1); } } }
@AfterClass public void tearDown() { this.limiter.stop(); } }
@BeforeClass public void setUp() { this.limiter = new RateBasedLimiter(20, TimeUnit.SECONDS); this.limiter.start(); }
@Override public Closeable acquirePermits(long permits) throws InterruptedException { Closer closer = Closer.create(); for (Limiter limiter : this.underlyingLimiters) { Closeable permit = limiter.acquirePermits(permits); if (permit == null) { try { closer.close(); } catch (IOException ioe) { throw new RuntimeException("Could not return intermediate permits."); } return null; } closer.register(permit); } return closer; }
@AfterClass public void tearDown() { this.limiter.stop(); } }
@BeforeClass public void setUp() { this.limiter = new TimeBasedLimiter(3l, TimeUnit.SECONDS); this.limiter.start(); }
/** * Acquire permit along with emitting metrics if enabled. * @param permits * @throws InterruptedException */ private void acquirePermits(long permits) throws InterruptedException { long startMs = System.currentTimeMillis(); //Measure in milliseconds. (Nanoseconds are more precise but expensive and not worth for this case) limiter.acquirePermits(permits); long permitAcquisitionTime = System.currentTimeMillis() - startMs; if (throttledTimer.isPresent()) { // Metrics enabled Instrumented.updateTimer(throttledTimer, permitAcquisitionTime, TimeUnit.MILLISECONDS); } this.throttledTime += permitAcquisitionTime; }
@Override public void close() throws IOException { getRateLimiter().stop(); super.close(); }
public LimitingExtractorDecorator(Extractor<S, D> extractor, Limiter limiter, TaskState state) { this.extractor = extractor; this.limiter = limiter; this.taskState = state; this.limiter.start(); this.eventSubmitter = new EventSubmitter.Builder(TaskMetrics.get(taskState).getMetricContext(), "gobblin.runtime.task").build(); }