Search in sources :

Example 1 with ConcurrentLFUCache

use of org.apache.solr.util.ConcurrentLFUCache in project lucene-solr by apache.

the class TestLFUCache method testConcurrentAccess.

@Test
public void testConcurrentAccess() throws InterruptedException {
    /* Set up a thread pool with twice as many threads as there are CPUs. */
    final ConcurrentLFUCache<Integer, Long> cache = new ConcurrentLFUCache<>(10, 9);
    ExecutorService executorService = ExecutorUtil.newMDCAwareFixedThreadPool(10, new DefaultSolrThreadFactory("testConcurrentAccess"));
    final AtomicReference<Throwable> error = new AtomicReference<>();
    /*
     * Use the thread pool to execute at least two million puts into the cache.
     * Without the fix on SOLR-7585, NoSuchElementException is thrown.
     * Simultaneous calls to markAndSweep are protected from each other by a
     * lock, so they run sequentially, and due to a problem in the previous
     * design, the cache eviction doesn't work right.
     */
    for (int i = 0; i < atLeast(2_000_000); ++i) {
        executorService.submit(() -> {
            try {
                cache.put(random().nextInt(100), random().nextLong());
            } catch (Throwable t) {
                error.compareAndSet(null, t);
            }
        });
    }
    executorService.shutdown();
    executorService.awaitTermination(1, TimeUnit.MINUTES);
    // then:
    assertNull("Exception during concurrent access: " + error.get(), error.get());
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) DefaultSolrThreadFactory(org.apache.solr.util.DefaultSolrThreadFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConcurrentLFUCache(org.apache.solr.util.ConcurrentLFUCache) Test(org.junit.Test)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ConcurrentLFUCache (org.apache.solr.util.ConcurrentLFUCache)1 DefaultSolrThreadFactory (org.apache.solr.util.DefaultSolrThreadFactory)1 Test (org.junit.Test)1