Search in sources :

Example 16 with ExecutorCloser

use of org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser in project jackrabbit-oak by apache.

the class CompactionAndCleanupIT method concurrentCleanup.

/**
     * Test asserting OAK-4700: Concurrent cleanup must not remove segments that are still reachable
     */
@Test
public void concurrentCleanup() throws Exception {
    File fileStoreFolder = getFileStoreFolder();
    final FileStore fileStore = fileStoreBuilder(fileStoreFolder).withMaxFileSize(1).build();
    final SegmentNodeStore nodeStore = SegmentNodeStoreBuilders.builder(fileStore).build();
    ExecutorService executorService = newFixedThreadPool(50);
    final AtomicInteger counter = new AtomicInteger();
    try {
        Callable<Void> concurrentWriteTask = new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                NodeBuilder builder = nodeStore.getRoot().builder();
                builder.setProperty("blob-" + counter.getAndIncrement(), createBlob(nodeStore, 512 * 512));
                nodeStore.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
                fileStore.flush();
                return null;
            }
        };
        final Callable<Void> concurrentCleanTask = new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                fileStore.cleanup();
                return null;
            }
        };
        List<Future<?>> results = newArrayList();
        for (int i = 0; i < 50; i++) {
            if (i % 2 == 0) {
                results.add(executorService.submit(concurrentWriteTask));
            } else {
                results.add(executorService.submit(concurrentCleanTask));
            }
        }
        for (Future<?> result : results) {
            assertNull(result.get());
        }
        for (String fileName : fileStoreFolder.list()) {
            if (fileName.endsWith(".tar")) {
                int pos = fileName.length() - "a.tar".length();
                char generation = fileName.charAt(pos);
                assertEquals("Expected nothing to be cleaned but generation '" + generation + "' for file " + fileName + " indicates otherwise.", "a", valueOf(generation));
            }
        }
    } finally {
        new ExecutorCloser(executorService).close();
        fileStore.close();
    }
}
Also used : NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) Callable(java.util.concurrent.Callable) FileStore(org.apache.jackrabbit.oak.segment.file.FileStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) File(java.io.File) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) Test(org.junit.Test)

Example 17 with ExecutorCloser

use of org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser in project jackrabbit-oak by apache.

the class CompactionAndCleanupIT method concurrentWritesCleanupNoNewGen.

/**
     * Test asserting OAK-4669: No new generation of tar should be created when the segments are the same 
     * and when various indices are created. 
     */
@Test
public void concurrentWritesCleanupNoNewGen() throws Exception {
    ScheduledExecutorService scheduler = newSingleThreadScheduledExecutor();
    StatisticsProvider statsProvider = new DefaultStatisticsProvider(scheduler);
    final FileStoreGCMonitor fileStoreGCMonitor = new FileStoreGCMonitor(Clock.SIMPLE);
    File fileStoreFolder = getFileStoreFolder();
    final FileStore fileStore = fileStoreBuilder(fileStoreFolder).withGCOptions(defaultGCOptions().setRetainedGenerations(2)).withGCMonitor(fileStoreGCMonitor).withStatisticsProvider(statsProvider).withMaxFileSize(1).withMemoryMapping(false).build();
    final SegmentNodeStore nodeStore = SegmentNodeStoreBuilders.builder(fileStore).build();
    ExecutorService executorService = newFixedThreadPool(5);
    final AtomicInteger counter = new AtomicInteger();
    try {
        Callable<Void> concurrentWriteTask = new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                NodeBuilder builder = nodeStore.getRoot().builder();
                builder.setProperty("blob-" + counter.getAndIncrement(), createBlob(nodeStore, 512 * 512));
                nodeStore.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY);
                fileStore.flush();
                return null;
            }
        };
        List<Future<?>> results = newArrayList();
        for (int i = 0; i < 5; i++) {
            results.add(executorService.submit(concurrentWriteTask));
        }
        for (Future<?> result : results) {
            assertNull(result.get());
        }
        fileStore.cleanup();
        for (String fileName : fileStoreFolder.list()) {
            if (fileName.endsWith(".tar")) {
                int pos = fileName.length() - "a.tar".length();
                char generation = fileName.charAt(pos);
                assertTrue("Expected generation is 'a', but instead was: '" + generation + "' for file " + fileName, generation == 'a');
            }
        }
    } finally {
        new ExecutorCloser(executorService).close();
        fileStore.close();
        new ExecutorCloser(scheduler).close();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) FileStoreGCMonitor(org.apache.jackrabbit.oak.segment.file.FileStoreGCMonitor) DefaultStatisticsProvider(org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) DefaultStatisticsProvider(org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider) StatisticsProvider(org.apache.jackrabbit.oak.stats.StatisticsProvider) Callable(java.util.concurrent.Callable) FileStore(org.apache.jackrabbit.oak.segment.file.FileStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) File(java.io.File) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) Test(org.junit.Test)

Example 18 with ExecutorCloser

use of org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser in project jackrabbit-oak by apache.

the class ActiveDeletedBlobCollectionIT method after.

@After
public void after() {
    new ExecutorCloser(executorService).close();
    executorService.shutdown();
    IndexDefinition.setDisableStoredIndexDefinition(false);
}
Also used : ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) After(org.junit.After)

Example 19 with ExecutorCloser

use of org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser in project jackrabbit-oak by apache.

the class LucenePropertyIndexTest method after.

@After
public void after() {
    new ExecutorCloser(executorService).close();
    IndexDefinition.setDisableStoredIndexDefinition(false);
}
Also used : ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) After(org.junit.After)

Example 20 with ExecutorCloser

use of org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser in project jackrabbit-oak by apache.

the class UploadStagingCacheTest method testConcurrentPutDelete.

private void testConcurrentPutDelete(int diff) throws Exception {
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
    closer.register(new ExecutorCloser(executorService));
    //start immediately
    taskLatch.countDown();
    // Add immediately
    List<ListenableFuture<Integer>> futures = put(folder);
    // New task to put another file
    File f2 = copyToFile(randomStream(diff, 4 * 1024), folder.newFile());
    CountDownLatch putThreadLatch = new CountDownLatch(1);
    CountDownLatch triggerLatch = new CountDownLatch(1);
    SettableFuture<Optional<SettableFuture<Integer>>> future1 = putThread(executorService, diff, f2, stagingCache, putThreadLatch, triggerLatch);
    putThreadLatch.countDown();
    // wait for put thread to go ahead
    callbackLatch.countDown();
    ScheduledFuture<?> scheduledFuture = removeExecutor.schedule(stagingCache.new RemoveJob(), 0, TimeUnit.MILLISECONDS);
    triggerLatch.await();
    if (future1.get().isPresent()) {
        futures.add(future1.get().get());
    }
    ListenableFuture<List<Integer>> listListenableFuture = Futures.successfulAsList(futures);
    try {
        listListenableFuture.get();
        scheduledFuture.get();
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertTrue(Files.equal(copyToFile(randomStream(0, 4 * 1024), folder.newFile()), uploader.read(ID_PREFIX + 0)));
    assertTrue(Files.equal(copyToFile(randomStream(diff, 4 * 1024), folder.newFile()), uploader.read(ID_PREFIX + diff)));
}
Also used : Optional(com.google.common.base.Optional) CountDownLatch(java.util.concurrent.CountDownLatch) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) List(java.util.List) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) File(java.io.File)

Aggregations

ExecutorCloser (org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser)36 Test (org.junit.Test)15 File (java.io.File)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)11 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)9 ExecutorService (java.util.concurrent.ExecutorService)6 DefaultStatisticsProvider (org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)5 After (org.junit.After)5 Callable (java.util.concurrent.Callable)4 Future (java.util.concurrent.Future)4 FileStore (org.apache.jackrabbit.oak.segment.file.FileStore)4 InputStream (java.io.InputStream)3 StatisticsProvider (org.apache.jackrabbit.oak.stats.StatisticsProvider)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 Closeable (java.io.Closeable)2 IOException (java.io.IOException)2 List (java.util.List)2