Search in sources :

Example 31 with ExecutorCloser

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

the class CompositeDataStoreCacheTest method concurrentGetFromStagedAndCached.

/**
     * Concurrently retrieves 2 different files from cache.
     * One is staged and other in the download cache.
     * @throws Exception
     */
@Test
public void concurrentGetFromStagedAndCached() throws Exception {
    LOG.info("Starting concurrentGetFromStagedAndCached");
    // Add 1 to backend
    // Add 2 to upload area
    // Stop upload execution
    // Concurrently get 1 & 2
    // continue upload execution
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
    closer.register(new ExecutorCloser(executorService, 5, TimeUnit.MILLISECONDS));
    // Add file to backend
    File f2 = copyToFile(randomStream(1, 4 * 1024), folder.newFile());
    loader.write(ID_PREFIX + 1, f2);
    assertTrue(f2.exists());
    // stage for upload
    File f = copyToFile(randomStream(0, 4 * 1024), folder.newFile());
    boolean accepted = cache.stage(ID_PREFIX + 0, f);
    assertTrue(accepted);
    // Would hit the staging cache
    CountDownLatch thread1Start = new CountDownLatch(1);
    SettableFuture<File> future1 = retrieveThread(executorService, ID_PREFIX + 0, cache, thread1Start);
    // Would hit the download cache and load
    CountDownLatch thread2Start = new CountDownLatch(1);
    SettableFuture<File> future2 = retrieveThread(executorService, ID_PREFIX + 1, cache, thread2Start);
    thread1Start.countDown();
    thread2Start.countDown();
    File cached = future1.get();
    File cached2 = future2.get();
    LOG.info("Async tasks finished");
    assertFile(cached, 0, folder);
    assertTrue(Files.equal(f2, cached2));
    //start the original upload
    taskLatch.countDown();
    callbackLatch.countDown();
    waitFinish();
    assertCacheStats(cache.getStagingCacheStats(), 0, 0, 1, 1);
    assertEquals(2, cache.getStagingCacheStats().getLoadCount());
    assertEquals(1, cache.getStagingCacheStats().getLoadSuccessCount());
    assertCacheStats(cache.getCacheStats(), 2, 8 * 1024, 0, 2);
    assertEquals(1, cache.getCacheStats().getLoadCount());
    assertEquals(1, cache.getCacheStats().getLoadSuccessCount());
    LOG.info("Finished concurrentGetFromStagedAndCached");
}
Also used : ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) File(java.io.File) Test(org.junit.Test)

Example 32 with ExecutorCloser

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

the class CompositeDataStoreCacheTest method concurrentAddGet.

/**
     * Concurrently stage and get a file and then upload.
     * Use the file retrieve to read contents.
     * @throws Exception
     */
@Test
public void concurrentAddGet() throws Exception {
    LOG.info("Starting concurrentAddGet");
    // Add to the upload area
    // stop upload execution
    // Same as above but concurrently
    // Get
    // Continue upload execution
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
    closer.register(new ExecutorCloser(executorService, 5, TimeUnit.MILLISECONDS));
    // stage for upload
    File f = copyToFile(randomStream(0, 4 * 1024), folder.newFile());
    boolean accepted = cache.stage(ID_PREFIX + 0, f);
    assertTrue(accepted);
    // Would hit the staging cache
    CountDownLatch thread1Start = new CountDownLatch(1);
    SettableFuture<File> future1 = retrieveThread(executorService, ID_PREFIX + 0, cache, thread1Start);
    // Get a handle to the file and open stream
    File fileOnUpload = cache.getIfPresent(ID_PREFIX + 0);
    assertNotNull(fileOnUpload);
    final InputStream fStream = Files.asByteSource(fileOnUpload).openStream();
    thread1Start.countDown();
    //start the original upload
    taskLatch.countDown();
    callbackLatch.countDown();
    future1.get();
    waitFinish();
    LOG.info("Async tasks finished");
    File gold = copyToFile(randomStream(0, 4 * 1024), folder.newFile());
    File fromUploadStream = copyToFile(fStream, folder.newFile());
    assertTrue(Files.equal(gold, fromUploadStream));
    assertEquals(2, cache.getStagingCacheStats().getLoadCount());
    assertEquals(0, cache.getCacheStats().getLoadCount());
    assertEquals(0, cache.getCacheStats().getLoadSuccessCount());
    LOG.info("Finished concurrentAddGet");
}
Also used : InputStream(java.io.InputStream) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) File(java.io.File) Test(org.junit.Test)

Example 33 with ExecutorCloser

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

the class CompositeDataStoreCacheTest method setup.

@Before
public void setup() throws Exception {
    LOG.info("Starting setup");
    root = folder.newFolder();
    loader = new TestCacheLoader<String, InputStream>(folder.newFolder());
    uploader = new TestStagingUploader(folder.newFolder());
    // create executor
    taskLatch = new CountDownLatch(1);
    callbackLatch = new CountDownLatch(1);
    afterExecuteLatch = new CountDownLatch(1);
    executor = new TestExecutor(1, taskLatch, callbackLatch, afterExecuteLatch);
    // stats
    ScheduledExecutorService statsExecutor = Executors.newSingleThreadScheduledExecutor();
    closer.register(new ExecutorCloser(statsExecutor, 500, TimeUnit.MILLISECONDS));
    statsProvider = new DefaultStatisticsProvider(statsExecutor);
    scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
    closer.register(new ExecutorCloser(scheduledExecutor, 500, TimeUnit.MILLISECONDS));
    fileCacheExecutor = sameThreadExecutor();
    //cache instance
    cache = new CompositeDataStoreCache(root.getAbsolutePath(), null, 80 * 1024, /* bytes */
    10, 1, /*threads*/
    loader, uploader, statsProvider, executor, scheduledExecutor, fileCacheExecutor, 3000, 6000);
    closer.register(cache);
    LOG.info("Finished setup");
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InputStream(java.io.InputStream) DefaultStatisticsProvider(org.apache.jackrabbit.oak.stats.DefaultStatisticsProvider) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) Before(org.junit.Before)

Example 34 with ExecutorCloser

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

the class DocumentNodeStoreStatsTest method shutDown.

@After
public void shutDown() {
    statsProvider.close();
    new ExecutorCloser(executor).close();
}
Also used : ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) After(org.junit.After)

Example 35 with ExecutorCloser

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

the class AsyncIndexUpdateClusterTestIT method missingCheckpointDueToEventualConsistency.

@Test
public void missingCheckpointDueToEventualConsistency() throws Exception {
    IndexStatusListener l = new IndexStatusListener();
    AsyncIndexUpdate async1 = createAsync(ns1, l);
    closer.register(async1);
    AsyncIndexUpdate async2 = createAsync(ns2, l);
    closer.register(async2);
    // Phase 1 - Base setup - Index definition creation and
    // performing initial indexing
    // Create index definition on NS1
    NodeBuilder b1 = ns1.getRoot().builder();
    createIndexDefinition(b1);
    ns1.merge(b1, EmptyHook.INSTANCE, CommitInfo.EMPTY);
    // Trigger indexing on NS1
    async1.run();
    // make sure initial index is visible on both cluster nodes
    ns1.runBackgroundOperations();
    ns2.runBackgroundOperations();
    l.initDone();
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5);
    closer.register(new ExecutorCloser(executorService));
    executorService.scheduleWithFixedDelay(async1, 1, 3, TimeUnit.SECONDS);
    executorService.scheduleWithFixedDelay(async2, 1, 2, TimeUnit.SECONDS);
    executorService.scheduleWithFixedDelay(new PropertyMutator(ns1, "node1"), 500, 500, TimeUnit.MILLISECONDS);
    executorService.scheduleWithFixedDelay(new PropertyMutator(ns2, "node2"), 500, 500, TimeUnit.MILLISECONDS);
    for (int i = 0; i < 4 && !illegalReindex.get(); i++) {
        TimeUnit.SECONDS.sleep(5);
    }
    shutdown();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) Test(org.junit.Test)

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