Search in sources :

Example 1 with LedgerRangeIterator

use of org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator in project bookkeeper by apache.

the class LedgerManagerIteratorTest method checkConcurrentModifications.

@Test
public void checkConcurrentModifications() throws Throwable {
    final int numWriters = 10;
    final int numCheckers = 10;
    final int numLedgers = 100;
    final long runtime = TimeUnit.NANOSECONDS.convert(2, TimeUnit.SECONDS);
    final boolean longRange = baseConf.getLedgerManagerFactoryClass() == LongHierarchicalLedgerManagerFactory.class;
    final Set<Long> mustExist = new TreeSet<>();
    LedgerManager lm = getLedgerManager();
    Random rng = new Random();
    for (int i = 0; i < numLedgers; ++i) {
        long lid = Math.abs(rng.nextLong());
        if (!longRange) {
            lid %= 1000000;
        }
        createLedger(lm, lid, Optional.of(BKException.Code.OK));
        mustExist.add(lid);
    }
    final long start = MathUtils.nowInNano();
    final CountDownLatch latch = new CountDownLatch(1);
    ArrayList<Thread> threads = new ArrayList<>();
    for (int i = 0; i < numWriters; ++i) {
        Thread thread = new Thread(safeWrapper(() -> {
            LedgerManager writerLM = getIndependentLedgerManager();
            Random writerRNG = new Random(rng.nextLong());
            try {
                latch.await();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                fail("Checker interrupted");
            }
            while (MathUtils.elapsedNanos(start) < runtime) {
                long candidate = 0;
                do {
                    candidate = Math.abs(writerRNG.nextLong());
                    if (!longRange) {
                        candidate %= 1000000;
                    }
                } while (mustExist.contains(candidate));
                try {
                    createLedger(writerLM, candidate, Optional.empty());
                    removeLedger(writerLM, candidate, Optional.empty());
                } catch (Throwable e) {
                    fail("Got exception thrashing store: " + e.toString());
                }
            }
        }));
        thread.start();
        threads.add(thread);
    }
    for (int i = 0; i < numCheckers; ++i) {
        Thread thread = new Thread(safeWrapper(() -> {
            LedgerManager checkerLM = getIndependentLedgerManager();
            try {
                latch.await();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                fail("Checker interrupted");
                e.printStackTrace();
            }
            while (MathUtils.elapsedNanos(start) < runtime) {
                try {
                    LedgerRangeIterator lri = checkerLM.getLedgerRanges();
                    Set<Long> returnedIds = ledgerRangeToSet(lri);
                    for (long id : mustExist) {
                        assertTrue(returnedIds.contains(id));
                    }
                    Set<Long> ledgersReadAsync = getLedgerIdsByUsingAsyncProcessLedgers(checkerLM);
                    for (long id : mustExist) {
                        assertTrue(ledgersReadAsync.contains(id));
                    }
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                    fail("Got exception scanning ledgers: " + e.toString());
                }
            }
        }));
        thread.start();
        threads.add(thread);
    }
    latch.countDown();
    for (Thread thread : threads) {
        thread.join();
    }
}
Also used : TreeSet(java.util.TreeSet) Set(java.util.Set) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) LedgerRangeIterator(org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator) Random(java.util.Random) TreeSet(java.util.TreeSet) Test(org.junit.Test)

Example 2 with LedgerRangeIterator

use of org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator in project bookkeeper by apache.

the class LedgerManagerIteratorTest method testWithSeveralIncompletePaths.

@Test
public void testWithSeveralIncompletePaths() throws Throwable {
    if (baseConf.getLedgerManagerFactoryClass() != LongHierarchicalLedgerManagerFactory.class) {
        return;
    }
    LedgerManager lm = getLedgerManager();
    Set<Long> ids = new TreeSet<>(Arrays.asList(2345678901234567890L, 3394498498348983841L, 6334994393848474732L, 7349370101927398483L));
    for (Long id : ids) {
        createLedger(lm, id, Optional.of(BKException.Code.OK));
    }
    String[] paths = { // top level, W-4292762
    "/ledgers/000/0000/0000", // shares two path segments with the first one, comes after
    "/ledgers/234/5678/9999", // shares one path segment with the second one, comes first
    "/ledgers/339/0000/0000", // shares three path segments with the third one, comes first
    "/ledgers/633/4994/3938/0000", // close to max long, at end
    "/ledgers/922/3372/0000/0000" };
    for (String path : paths) {
        ZkUtils.createFullPathOptimistic(zkc, path, "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    LedgerRangeIterator lri = lm.getLedgerRanges();
    assertNotNull(lri);
    Set<Long> returnedIds = ledgerRangeToSet(lri);
    assertEquals(ids, returnedIds);
    Set<Long> ledgersReadAsync = getLedgerIdsByUsingAsyncProcessLedgers(lm);
    assertEquals("Comparing LedgersIds read asynchronously", ids, ledgersReadAsync);
}
Also used : LedgerRangeIterator(org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator) TreeSet(java.util.TreeSet) Test(org.junit.Test)

Example 3 with LedgerRangeIterator

use of org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator in project bookkeeper by apache.

the class LedgerManagerIteratorTest method hierarchicalLedgerManagerAsyncProcessLedgersTest.

@Test
public void hierarchicalLedgerManagerAsyncProcessLedgersTest() throws Throwable {
    Assume.assumeTrue(baseConf.getLedgerManagerFactoryClass().equals(HierarchicalLedgerManagerFactory.class));
    LedgerManager lm = getLedgerManager();
    LedgerRangeIterator lri = lm.getLedgerRanges();
    Set<Long> ledgerIds = new TreeSet<>(Arrays.asList(1234L, 123456789123456789L));
    for (Long ledgerId : ledgerIds) {
        createLedger(lm, ledgerId, Optional.of(BKException.Code.OK));
    }
    Set<Long> ledgersReadThroughIterator = ledgerRangeToSet(lri);
    assertEquals("Comparing LedgersIds read through Iterator", ledgerIds, ledgersReadThroughIterator);
    Set<Long> ledgersReadAsync = getLedgerIdsByUsingAsyncProcessLedgers(lm);
    assertEquals("Comparing LedgersIds read asynchronously", ledgerIds, ledgersReadAsync);
}
Also used : LedgerRangeIterator(org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator) TreeSet(java.util.TreeSet) Test(org.junit.Test)

Example 4 with LedgerRangeIterator

use of org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator in project bookkeeper by apache.

the class LedgerManagerIteratorTest method testSingleLedger.

@Test
public void testSingleLedger() throws Throwable {
    LedgerManager lm = getLedgerManager();
    long id = 2020202;
    createLedger(lm, id, Optional.of(BKException.Code.OK));
    LedgerRangeIterator lri = lm.getLedgerRanges();
    assertNotNull(lri);
    Set<Long> lids = ledgerRangeToSet(lri);
    assertEquals(lids.size(), 1);
    assertEquals(lids.iterator().next().longValue(), id);
    Set<Long> ledgersReadAsync = getLedgerIdsByUsingAsyncProcessLedgers(lm);
    assertEquals("Comparing LedgersIds read asynchronously", lids, ledgersReadAsync);
}
Also used : LedgerRangeIterator(org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator) Test(org.junit.Test)

Example 5 with LedgerRangeIterator

use of org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator in project bookkeeper by apache.

the class LedgerManagerIteratorTest method testTwoLedgers.

@Test
public void testTwoLedgers() throws Throwable {
    LedgerManager lm = getLedgerManager();
    Set<Long> ids = new TreeSet<>(Arrays.asList(101010101L, 2020340302L));
    for (Long id : ids) {
        createLedger(lm, id, Optional.of(BKException.Code.OK));
    }
    LedgerRangeIterator lri = lm.getLedgerRanges();
    assertNotNull(lri);
    Set<Long> returnedIds = ledgerRangeToSet(lri);
    assertEquals(ids, returnedIds);
    Set<Long> ledgersReadAsync = getLedgerIdsByUsingAsyncProcessLedgers(lm);
    assertEquals("Comparing LedgersIds read asynchronously", ids, ledgersReadAsync);
}
Also used : LedgerRangeIterator(org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator) TreeSet(java.util.TreeSet) Test(org.junit.Test)

Aggregations

LedgerRangeIterator (org.apache.bookkeeper.meta.LedgerManager.LedgerRangeIterator)14 Test (org.junit.Test)11 TreeSet (java.util.TreeSet)9 LedgerRange (org.apache.bookkeeper.meta.LedgerManager.LedgerRange)3 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Checkpoint (org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint)2 GarbageCollector (org.apache.bookkeeper.bookie.GarbageCollector)2 ScanAndCompareGarbageCollector (org.apache.bookkeeper.bookie.ScanAndCompareGarbageCollector)2 Random (java.util.Random)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 LedgerMetadata (org.apache.bookkeeper.client.LedgerMetadata)1