Search in sources :

Example 1 with ExponentialBackOffRateLimiter

use of com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter in project rest.li by linkedin.

the class TestRateLimiter method testSimple.

@Test
public void testSimple() throws Exception {
    final int total = 10;
    // NB on Solaris x86 there seems to be an extra 10ms that gets added to the period; need
    // to figure this out.  For now set the period high enough that period + 10 will be within
    // the tolerance.
    final int period = 100;
    final CountDownLatch latch = new CountDownLatch(total);
    final Task incr = new Task() {

        @Override
        public void run(SimpleCallback doneCallback) {
            latch.countDown();
            doneCallback.onDone();
        }
    };
    RateLimiter limiter = new ExponentialBackOffRateLimiter(period, period, period, _executor);
    limiter.setPeriod(period);
    long start = System.currentTimeMillis();
    long lowTolerance = (total * period) * 4 / 5;
    long highTolerance = (total * period) * 5 / 4;
    for (int i = 0; i < total * period; i++) {
        limiter.submit(incr);
    }
    Assert.assertTrue(latch.await(highTolerance, TimeUnit.MILLISECONDS), "Should have finished within " + highTolerance + "ms");
    long t = System.currentTimeMillis() - start;
    Assert.assertTrue(t > lowTolerance, "Should have finished after " + lowTolerance + "ms (took " + t + ")");
}
Also used : Task(com.linkedin.r2.transport.http.client.RateLimiter.Task) CountDownLatch(java.util.concurrent.CountDownLatch) ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) SimpleCallback(com.linkedin.common.callback.SimpleCallback) ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) RateLimiter(com.linkedin.r2.transport.http.client.RateLimiter) Test(org.testng.annotations.Test)

Example 2 with ExponentialBackOffRateLimiter

use of com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter in project rest.li by linkedin.

the class TestHttpNettyClient method testFailBackoff.

@Test
public void testFailBackoff() throws Exception {
    final int WARM_UP = 10;
    final int N = 5;
    final int MAX_RATE_LIMITING_PERIOD = 500;
    final CountDownLatch warmUpLatch = new CountDownLatch(WARM_UP);
    final CountDownLatch latch = new CountDownLatch(N);
    final AtomicReference<Boolean> isShutdown = new AtomicReference<>(false);
    AsyncPool<Channel> testPool = new AsyncPoolImpl<>("test pool", new AsyncPool.Lifecycle<Channel>() {

        @Override
        public void create(Callback<Channel> callback) {
            if (warmUpLatch.getCount() > 0) {
                warmUpLatch.countDown();
            } else {
                latch.countDown();
            }
            callback.onError(new Throwable("Oops..."));
        }

        @Override
        public boolean validateGet(Channel obj) {
            return false;
        }

        @Override
        public boolean validatePut(Channel obj) {
            return false;
        }

        @Override
        public void destroy(Channel obj, boolean error, Callback<Channel> callback) {
        }

        @Override
        public PoolStats.LifecycleStats getStats() {
            return null;
        }
    }, 200, 30000, _scheduler, Integer.MAX_VALUE, AsyncPoolImpl.Strategy.MRU, 0, new ExponentialBackOffRateLimiter(0, MAX_RATE_LIMITING_PERIOD, Math.max(10, MAX_RATE_LIMITING_PERIOD / 32), _scheduler));
    HttpNettyClient client = new HttpNettyClient(address -> testPool, _scheduler, 500, 500, 1024 * 1024 * 2);
    final RestRequest r = new RestRequestBuilder(URI.create("http://localhost:8080/")).setMethod("GET").build();
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(() -> {
        while (!isShutdown.get()) {
            try {
                FutureCallback<RestResponse> callback = new FutureCallback<>();
                client.restRequest(r, new RequestContext(), new HashMap<>(), new TransportCallbackAdapter<RestResponse>(callback));
                callback.get();
            } catch (Exception e) {
            // ignore
            }
        }
    });
    // First ensure a bunch fail to get the rate limiting going
    warmUpLatch.await(120, TimeUnit.SECONDS);
    // Now we should be rate limited
    long start = System.currentTimeMillis();
    System.err.println("Starting at " + start);
    long lowTolerance = N * MAX_RATE_LIMITING_PERIOD * 4 / 5;
    long highTolerance = N * MAX_RATE_LIMITING_PERIOD * 5 / 4;
    Assert.assertTrue(latch.await(highTolerance, TimeUnit.MILLISECONDS), "Should have finished within " + highTolerance + "ms");
    long elapsed = System.currentTimeMillis() - start;
    Assert.assertTrue(elapsed > lowTolerance, "Should have finished after " + lowTolerance + "ms (took " + elapsed + ")");
    // shutdown everything
    isShutdown.set(true);
    executor.shutdown();
}
Also used : RequestContext(com.linkedin.r2.message.RequestContext) FutureCallback(com.linkedin.common.callback.FutureCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) Channel(io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) EncoderException(io.netty.handler.codec.EncoderException) TimeoutException(java.util.concurrent.TimeoutException) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RestRequest(com.linkedin.r2.message.rest.RestRequest) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 3 with ExponentialBackOffRateLimiter

use of com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter in project rest.li by linkedin.

the class TestRateLimiter method testSetPeriod.

@Test
public void testSetPeriod() throws Exception {
    final RateLimiter rl = new ExponentialBackOffRateLimiter(0, MAXPERIOD, 20, _executor);
    final ScheduledFuture<?> future = _executor.scheduleAtFixedRate(new Runnable() {

        private int _period = 300;

        @Override
        public void run() {
            rl.setPeriod(_period);
            _period += 50;
        }
    }, 0, 100, TimeUnit.MILLISECONDS);
    RateLimiterRunner runner = new RateLimiterRunner(System.currentTimeMillis());
    rl.submit(runner);
    Assert.assertTrue(runner.getElapsedTime() <= MAXPERIOD, "Elapsed Time exceed MAX Period");
    future.cancel(false);
}
Also used : ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) RateLimiter(com.linkedin.r2.transport.http.client.RateLimiter) Test(org.testng.annotations.Test)

Example 4 with ExponentialBackOffRateLimiter

use of com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter in project rest.li by linkedin.

the class TestRateLimiter method testMaxRunningTasks.

@Test
public void testMaxRunningTasks() throws Exception {
    final int total = 20;
    final int maxRunning = 5;
    final int period = 100;
    final Random rand = new Random();
    final CountDownLatch latch = new CountDownLatch(total);
    final AtomicInteger totalStarted = new AtomicInteger();
    final AtomicInteger totalFinished = new AtomicInteger();
    final Task r = new Task() {

        @Override
        public void run(final SimpleCallback callback) {
            totalStarted.incrementAndGet();
            int delay = period + rand.nextInt(period);
            _executor.schedule(new Runnable() {

                @Override
                public void run() {
                    totalFinished.incrementAndGet();
                    callback.onDone();
                }
            }, delay, TimeUnit.MILLISECONDS);
            latch.countDown();
        }
    };
    RateLimiter limiter = new ExponentialBackOffRateLimiter(period, period, period, _executor, maxRunning);
    limiter.setPeriod(period);
    for (int i = 0; i < total; ++i) {
        limiter.submit(r);
    }
    // check the current number of concurrent tasks every 100ms.
    for (int i = 0; i < total * 2; ++i) {
        int currentRunning = totalStarted.get() - totalFinished.get();
        Assert.assertTrue(currentRunning <= maxRunning, "Should have less than " + maxRunning + " concurrent tasks");
        Thread.sleep(period);
    }
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
    Assert.assertEquals(total, totalStarted.get());
}
Also used : Task(com.linkedin.r2.transport.http.client.RateLimiter.Task) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) SimpleCallback(com.linkedin.common.callback.SimpleCallback) ExponentialBackOffRateLimiter(com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter) RateLimiter(com.linkedin.r2.transport.http.client.RateLimiter) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)4 ExponentialBackOffRateLimiter (com.linkedin.r2.transport.http.client.ExponentialBackOffRateLimiter)3 RateLimiter (com.linkedin.r2.transport.http.client.RateLimiter)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 SimpleCallback (com.linkedin.common.callback.SimpleCallback)2 Task (com.linkedin.r2.transport.http.client.RateLimiter.Task)2 FutureCallback (com.linkedin.common.callback.FutureCallback)1 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)1 RequestContext (com.linkedin.r2.message.RequestContext)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 Channel (io.netty.channel.Channel)1 EncoderException (io.netty.handler.codec.EncoderException)1 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Random (java.util.Random)1 ExecutionException (java.util.concurrent.ExecutionException)1