Search in sources :

Example 1 with SegmentNodeStoreStats

use of org.apache.jackrabbit.oak.segment.SegmentNodeStoreStats in project jackrabbit-oak by apache.

the class LockBasedSchedulerTest method testSimulatedRaceOnRevisions.

/**
 * OAK-7162
 *
 * This test guards against race conditions which may happen when the head
 * state in {@link Revisions} is changed from outside the scheduler. If a
 * race condition happens at that point, data from a single commit will be
 * lost.
 */
@Test
public void testSimulatedRaceOnRevisions() throws Exception {
    final MemoryStore ms = new MemoryStore();
    StatisticsProvider statsProvider = StatisticsProvider.NOOP;
    SegmentNodeStoreStats stats = new SegmentNodeStoreStats(statsProvider);
    final LockBasedScheduler scheduler = LockBasedScheduler.builder(ms.getRevisions(), ms.getReader(), stats).build();
    final RecordId initialHead = ms.getRevisions().getHead();
    ExecutorService executorService = newFixedThreadPool(10);
    final AtomicInteger count = new AtomicInteger();
    final Random rand = new Random();
    try {
        Callable<PropertyState> commitTask = new Callable<PropertyState>() {

            @Override
            public PropertyState call() throws Exception {
                String property = "prop" + count.incrementAndGet();
                Commit commit = createCommit(scheduler, property, "value");
                SegmentNodeState result = (SegmentNodeState) scheduler.schedule(commit);
                return result.getProperty(property);
            }
        };
        Callable<Void> parallelTask = new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                Thread.sleep(rand.nextInt(10));
                ms.getRevisions().setHead(ms.getRevisions().getHead(), initialHead);
                return null;
            }
        };
        List<Future<?>> results = newArrayList();
        for (int i = 0; i < 100; i++) {
            results.add(executorService.submit(commitTask));
            executorService.submit(parallelTask);
        }
        for (Future<?> result : results) {
            assertNotNull("PropertyState must not be null! The corresponding commit got lost because of a race condition.", result.get());
        }
    } finally {
        new ExecutorCloser(executorService).close();
    }
}
Also used : SegmentNodeStoreStats(org.apache.jackrabbit.oak.segment.SegmentNodeStoreStats) SegmentNodeState(org.apache.jackrabbit.oak.segment.SegmentNodeState) StatisticsProvider(org.apache.jackrabbit.oak.stats.StatisticsProvider) Callable(java.util.concurrent.Callable) PropertyState(org.apache.jackrabbit.oak.api.PropertyState) MemoryStore(org.apache.jackrabbit.oak.segment.memory.MemoryStore) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) RecordId(org.apache.jackrabbit.oak.segment.RecordId) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) Test(org.junit.Test)

Example 2 with SegmentNodeStoreStats

use of org.apache.jackrabbit.oak.segment.SegmentNodeStoreStats in project jackrabbit-oak by apache.

the class LockBasedSchedulerCheckpointTest method testShortWait.

/**
 * OAK-3587 test simulates a timeout while trying to create a checkpoint,
 * then releases the lock and tries again
 */
@Test
public void testShortWait() throws Exception {
    MemoryStore ms = new MemoryStore();
    System.setProperty("oak.checkpoints.lockWaitTime", "1");
    StatisticsProvider statsProvider = StatisticsProvider.NOOP;
    SegmentNodeStoreStats stats = new SegmentNodeStoreStats(statsProvider);
    final LockBasedScheduler scheduler = LockBasedScheduler.builder(ms.getRevisions(), ms.getReader(), stats).build();
    final Semaphore semaphore = new Semaphore(0);
    final AtomicBoolean blocking = new AtomicBoolean(true);
    final Callable<Boolean> block = new Callable<Boolean>() {

        @Override
        public Boolean call() {
            while (blocking.get()) {
                if (semaphore.availablePermits() == 0) {
                    semaphore.release();
                }
            }
            return true;
        }
    };
    Thread background = new Thread() {

        @Override
        public void run() {
            try {
                Commit commit = createBlockingCommit(scheduler, "foo", "bar", block);
                scheduler.schedule(commit);
            } catch (Exception e) {
            // 
            }
        }
    };
    background.start();
    semaphore.acquire();
    String cp0 = scheduler.checkpoint(10, Collections.<String, String>emptyMap());
    assertNull(retrieveCheckpoint(scheduler, cp0));
    blocking.set(false);
    String cp1 = scheduler.checkpoint(10, Collections.<String, String>emptyMap());
    assertNotNull(retrieveCheckpoint(scheduler, cp1));
}
Also used : SegmentNodeStoreStats(org.apache.jackrabbit.oak.segment.SegmentNodeStoreStats) Semaphore(java.util.concurrent.Semaphore) StatisticsProvider(org.apache.jackrabbit.oak.stats.StatisticsProvider) Callable(java.util.concurrent.Callable) MemoryStore(org.apache.jackrabbit.oak.segment.memory.MemoryStore) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Example 3 with SegmentNodeStoreStats

use of org.apache.jackrabbit.oak.segment.SegmentNodeStoreStats in project jackrabbit-oak by apache.

the class LockBasedSchedulerCheckpointTest method testLongWait.

/**
 * OAK-3587 test simulates a wait less than configured
 * {@code SegmentNodeStore#setCheckpointsLockWaitTime(int)} value so the
 * checkpoint call must return a valid value
 */
@Test
public void testLongWait() throws Exception {
    final int blockTime = 1;
    MemoryStore ms = new MemoryStore();
    System.setProperty("oak.checkpoints.lockWaitTime", "2");
    StatisticsProvider statsProvider = StatisticsProvider.NOOP;
    SegmentNodeStoreStats stats = new SegmentNodeStoreStats(statsProvider);
    final LockBasedScheduler scheduler = LockBasedScheduler.builder(ms.getRevisions(), ms.getReader(), stats).build();
    final Semaphore semaphore = new Semaphore(0);
    final Callable<Boolean> block = new Callable<Boolean>() {

        @Override
        public Boolean call() {
            try {
                semaphore.release();
                SECONDS.sleep(blockTime);
            } catch (InterruptedException e) {
            // 
            }
            return true;
        }
    };
    Thread background = new Thread() {

        @Override
        public void run() {
            try {
                Commit commit = createBlockingCommit(scheduler, "foo", "bar", block);
                scheduler.schedule(commit);
            } catch (Exception e) {
            // 
            }
        }
    };
    background.start();
    semaphore.acquire();
    String cp0 = scheduler.checkpoint(10, Collections.<String, String>emptyMap());
    assertNotNull(retrieveCheckpoint(scheduler, cp0));
}
Also used : SegmentNodeStoreStats(org.apache.jackrabbit.oak.segment.SegmentNodeStoreStats) Semaphore(java.util.concurrent.Semaphore) StatisticsProvider(org.apache.jackrabbit.oak.stats.StatisticsProvider) Callable(java.util.concurrent.Callable) MemoryStore(org.apache.jackrabbit.oak.segment.memory.MemoryStore) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Aggregations

Callable (java.util.concurrent.Callable)3 SegmentNodeStoreStats (org.apache.jackrabbit.oak.segment.SegmentNodeStoreStats)3 MemoryStore (org.apache.jackrabbit.oak.segment.memory.MemoryStore)3 StatisticsProvider (org.apache.jackrabbit.oak.stats.StatisticsProvider)3 Test (org.junit.Test)3 Semaphore (java.util.concurrent.Semaphore)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Random (java.util.Random)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)1 ExecutorCloser (org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser)1 RecordId (org.apache.jackrabbit.oak.segment.RecordId)1 SegmentNodeState (org.apache.jackrabbit.oak.segment.SegmentNodeState)1