Search in sources :

Example 1 with DistributedIdGenerator

use of com.yahoo.pulsar.broker.service.DistributedIdGenerator in project pulsar by yahoo.

the class DistributedIdGeneratorTest method invalidZnode.

@Test
public void invalidZnode() throws Exception {
    zkc.create("/my/test/invalid", "invalid-number".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    DistributedIdGenerator gen = new DistributedIdGenerator(zkc, "/my/test/invalid", "p");
    // It should not get exception if content is there
    assertEquals(gen.getNextId(), "p-0-0");
}
Also used : DistributedIdGenerator(com.yahoo.pulsar.broker.service.DistributedIdGenerator) Test(org.testng.annotations.Test)

Example 2 with DistributedIdGenerator

use of com.yahoo.pulsar.broker.service.DistributedIdGenerator in project pulsar by yahoo.

the class DistributedIdGeneratorTest method concurrent.

/**
     * Use multiple threads to generate many Id. Ensure no holes and no dups in the sequence
     */
@Test
public void concurrent() throws Exception {
    int Threads = 10;
    int Iterations = 100;
    CyclicBarrier barrier = new CyclicBarrier(Threads);
    CountDownLatch counter = new CountDownLatch(Threads);
    ExecutorService executor = Executors.newCachedThreadPool();
    List<String> results = Collections.synchronizedList(Lists.newArrayList());
    for (int i = 0; i < Threads; i++) {
        executor.execute(() -> {
            try {
                DistributedIdGenerator gen = new DistributedIdGenerator(zkc, "/my/test/concurrent", "prefix");
                barrier.await();
                for (int j = 0; j < Iterations; j++) {
                    results.add(gen.getNextId());
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                counter.countDown();
            }
        });
    }
    counter.await();
    assertEquals(results.size(), Threads * Iterations);
    // Check the list contains no duplicates
    Set<String> set = Sets.newHashSet(results);
    assertEquals(set.size(), results.size());
    executor.shutdown();
}
Also used : DistributedIdGenerator(com.yahoo.pulsar.broker.service.DistributedIdGenerator) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.testng.annotations.Test)

Example 3 with DistributedIdGenerator

use of com.yahoo.pulsar.broker.service.DistributedIdGenerator in project pulsar by yahoo.

the class DistributedIdGeneratorTest method simple.

@Test
public void simple() throws Exception {
    DistributedIdGenerator gen1 = new DistributedIdGenerator(zkc, "/my/test/simple", "p");
    assertEquals(gen1.getNextId(), "p-0-0");
    assertEquals(gen1.getNextId(), "p-0-1");
    assertEquals(gen1.getNextId(), "p-0-2");
    assertEquals(gen1.getNextId(), "p-0-3");
    DistributedIdGenerator gen2 = new DistributedIdGenerator(zkc, "/my/test/simple", "p");
    assertEquals(gen2.getNextId(), "p-1-0");
    assertEquals(gen2.getNextId(), "p-1-1");
    assertEquals(gen1.getNextId(), "p-0-4");
    assertEquals(gen2.getNextId(), "p-1-2");
}
Also used : DistributedIdGenerator(com.yahoo.pulsar.broker.service.DistributedIdGenerator) Test(org.testng.annotations.Test)

Aggregations

DistributedIdGenerator (com.yahoo.pulsar.broker.service.DistributedIdGenerator)3 Test (org.testng.annotations.Test)3 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ExecutorService (java.util.concurrent.ExecutorService)1