Search in sources :

Example 61 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestInterProcessSemaphore method testMaxPerSession.

@Test
public void testMaxPerSession() throws Exception {
    final int CLIENT_QTY = 10;
    final int LOOP_QTY = 100;
    final Random random = new Random();
    final int SESSION_MAX = random.nextInt(75) + 25;
    final Timing timing = new Timing();
    List<Future<Object>> futures = Lists.newArrayList();
    ExecutorService service = Executors.newCachedThreadPool();
    final Counter counter = new Counter();
    final AtomicInteger available = new AtomicInteger(SESSION_MAX);
    for (int i = 0; i < CLIENT_QTY; ++i) {
        futures.add(service.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
                client.start();
                try {
                    InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", SESSION_MAX);
                    for (int i = 0; i < LOOP_QTY; ++i) {
                        long start = System.currentTimeMillis();
                        int thisQty;
                        synchronized (available) {
                            if ((System.currentTimeMillis() - start) > 10000) {
                                throw new TimeoutException();
                            }
                            while (available.get() == 0) {
                                available.wait(timing.forWaiting().milliseconds());
                            }
                            thisQty = (available.get() > 1) ? (random.nextInt(available.get()) + 1) : 1;
                            available.addAndGet(-1 * thisQty);
                            Assert.assertTrue(available.get() >= 0);
                        }
                        Collection<Lease> leases = semaphore.acquire(thisQty, timing.forWaiting().seconds(), TimeUnit.SECONDS);
                        Assert.assertNotNull(leases);
                        try {
                            synchronized (counter) {
                                counter.currentCount += thisQty;
                                if (counter.currentCount > counter.maxCount) {
                                    counter.maxCount = counter.currentCount;
                                }
                            }
                            Thread.sleep(random.nextInt(25));
                        } finally {
                            synchronized (counter) {
                                counter.currentCount -= thisQty;
                            }
                            semaphore.returnAll(leases);
                            synchronized (available) {
                                available.addAndGet(thisQty);
                                available.notifyAll();
                            }
                        }
                    }
                } finally {
                    client.close();
                }
                return null;
            }
        }));
    }
    for (Future<Object> f : futures) {
        f.get();
    }
    synchronized (counter) {
        Assert.assertTrue(counter.currentCount == 0);
        Assert.assertTrue(counter.maxCount > 0);
        Assert.assertTrue(counter.maxCount <= SESSION_MAX);
        System.out.println(counter.maxCount);
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) Callable(java.util.concurrent.Callable) CuratorFramework(org.apache.curator.framework.CuratorFramework) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) Timing(org.apache.curator.test.Timing) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 62 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestInterProcessSemaphore method testSimple2.

@Test
public void testSimple2() throws Exception {
    final int MAX_LEASES = 3;
    Timing timing = new Timing();
    List<Lease> leases = Lists.newArrayList();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        for (int i = 0; i < MAX_LEASES; ++i) {
            InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX_LEASES);
            Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
            Assert.assertNotNull(lease);
            leases.add(lease);
        }
        InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX_LEASES);
        Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
        Assert.assertNull(lease);
        leases.remove(0).close();
        Assert.assertNotNull(semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS));
    } finally {
        for (Lease l : leases) {
            CloseableUtils.closeQuietly(l);
        }
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 63 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestInterProcessSemaphore method testReleaseInChunks.

@Test
public void testReleaseInChunks() throws Exception {
    final Timing timing = new Timing();
    final int MAX_LEASES = 11;
    final int THREADS = 100;
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();
    try {
        final Stepper latch = new Stepper();
        final Random random = new Random();
        final Counter counter = new Counter();
        ExecutorService service = Executors.newCachedThreadPool();
        ExecutorCompletionService<Object> completionService = new ExecutorCompletionService<Object>(service);
        for (int i = 0; i < THREADS; ++i) {
            completionService.submit(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX_LEASES);
                    Lease lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
                    if (lease == null) {
                        throw new Exception("timed out");
                    }
                    try {
                        synchronized (counter) {
                            ++counter.currentCount;
                            if (counter.currentCount > counter.maxCount) {
                                counter.maxCount = counter.currentCount;
                            }
                            counter.notifyAll();
                        }
                        latch.await();
                    } finally {
                        synchronized (counter) {
                            --counter.currentCount;
                        }
                        semaphore.returnLease(lease);
                    }
                    return null;
                }
            });
        }
        int remaining = THREADS;
        while (remaining > 0) {
            int times = Math.min(random.nextInt(5) + 1, remaining);
            latch.countDown(times);
            remaining -= times;
            Thread.sleep(random.nextInt(100) + 1);
        }
        for (int i = 0; i < THREADS; ++i) {
            completionService.take();
        }
        timing.sleepABit();
        synchronized (counter) {
            Assert.assertTrue(counter.currentCount == 0);
            Assert.assertTrue(counter.maxCount > 0);
            Assert.assertTrue(counter.maxCount <= MAX_LEASES);
            System.out.println(counter.maxCount);
        }
    } finally {
        client.close();
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) CuratorFramework(org.apache.curator.framework.CuratorFramework) Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 64 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestInterProcessSemaphore method testGetParticipantNodes.

@Test
public void testGetParticipantNodes() throws Exception {
    final int LEASES = 3;
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    List<Lease> leases = Lists.newArrayList();
    client.start();
    try {
        InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", LEASES);
        for (int i = 0; i < LEASES; ++i) {
            leases.add(semaphore.acquire());
        }
        Assert.assertEquals(semaphore.getParticipantNodes().size(), LEASES);
    } finally {
        for (Lease l : leases) {
            CloseableUtils.closeQuietly(l);
        }
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 65 with Timing

use of org.apache.curator.test.Timing in project xian by happyyangyuan.

the class TestInterProcessSemaphoreCluster method testKilledServerWithEnsembleProvider.

@Test
public void testKilledServerWithEnsembleProvider() throws Exception {
    final int CLIENT_QTY = 10;
    final Timing timing = new Timing();
    final String PATH = "/foo/bar/lock";
    ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY);
    ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
    TestingCluster cluster = new TestingCluster(3);
    try {
        cluster.start();
        final AtomicReference<String> connectionString = new AtomicReference<String>(cluster.getConnectString());
        final EnsembleProvider provider = new EnsembleProvider() {

            @Override
            public void start() throws Exception {
            }

            @Override
            public String getConnectionString() {
                return connectionString.get();
            }

            @Override
            public void close() throws IOException {
            }
        };
        final Semaphore acquiredSemaphore = new Semaphore(0);
        final AtomicInteger acquireCount = new AtomicInteger(0);
        final CountDownLatch suspendedLatch = new CountDownLatch(CLIENT_QTY);
        for (int i = 0; i < CLIENT_QTY; ++i) {
            completionService.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    CuratorFramework client = CuratorFrameworkFactory.builder().ensembleProvider(provider).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
                    try {
                        final Semaphore suspendedSemaphore = new Semaphore(0);
                        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

                            @Override
                            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                                if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
                                    suspendedLatch.countDown();
                                    suspendedSemaphore.release();
                                }
                            }
                        });
                        client.start();
                        InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 1);
                        while (!Thread.currentThread().isInterrupted()) {
                            Lease lease = null;
                            try {
                                lease = semaphore.acquire();
                                acquiredSemaphore.release();
                                acquireCount.incrementAndGet();
                                suspendedSemaphore.acquire();
                            } catch (Exception e) {
                            // just retry
                            } finally {
                                if (lease != null) {
                                    acquireCount.decrementAndGet();
                                    CloseableUtils.closeQuietly(lease);
                                }
                            }
                        }
                    } finally {
                        CloseableUtils.closeQuietly(client);
                    }
                    return null;
                }
            });
        }
        Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
        Assert.assertEquals(1, acquireCount.get());
        cluster.close();
        timing.awaitLatch(suspendedLatch);
        timing.forWaiting().sleepABit();
        Assert.assertEquals(0, acquireCount.get());
        cluster = new TestingCluster(3);
        cluster.start();
        connectionString.set(cluster.getConnectString());
        timing.forWaiting().sleepABit();
        Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
        timing.forWaiting().sleepABit();
        Assert.assertEquals(1, acquireCount.get());
    } finally {
        executorService.shutdown();
        executorService.awaitTermination(10, TimeUnit.SECONDS);
        executorService.shutdownNow();
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) EnsembleProvider(org.apache.curator.ensemble.EnsembleProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Aggregations

Timing (org.apache.curator.test.Timing)166 CuratorFramework (org.apache.curator.framework.CuratorFramework)147 Test (org.testng.annotations.Test)138 RetryOneTime (org.apache.curator.retry.RetryOneTime)129 CountDownLatch (java.util.concurrent.CountDownLatch)56 ConnectionState (org.apache.curator.framework.state.ConnectionState)39 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)29 KeeperException (org.apache.zookeeper.KeeperException)28 ExecutorService (java.util.concurrent.ExecutorService)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 Semaphore (java.util.concurrent.Semaphore)18 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)16 TestingServer (org.apache.curator.test.TestingServer)15 Stat (org.apache.zookeeper.data.Stat)12 TestingCluster (org.apache.curator.test.TestingCluster)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 InstanceSpec (org.apache.curator.test.InstanceSpec)9 Test (org.junit.Test)9