Search in sources :

Example 1 with InvalidCacheLoadException

use of com.google.common.cache.CacheLoader.InvalidCacheLoadException in project caffeine by ben-manes.

the class CacheLoadingTest method testConcurrentLoadingNull.

/**
   * On a concurrent computation that returns null, all threads should get an
   * InvalidCacheLoadException, with the loader only called once. The result should not be cached
   * (a later request should call the loader again).
   */
private static void testConcurrentLoadingNull(Caffeine<Object, Object> builder) throws InterruptedException {
    int count = 10;
    final AtomicInteger callCount = new AtomicInteger();
    final CountDownLatch startSignal = new CountDownLatch(count + 1);
    LoadingCache<String, String> cache = CaffeinatedGuava.build(builder, new CacheLoader<String, String>() {

        @Override
        public String load(String key) {
            callCount.incrementAndGet();
            Uninterruptibles.awaitUninterruptibly(startSignal);
            return null;
        }
    });
    List<Object> result = doConcurrentGet(cache, "bar", count, startSignal);
    assertEquals(count, callCount.get());
    for (int i = 0; i < count; i++) {
        assertTrue(result.get(i) instanceof InvalidCacheLoadException);
    }
    // subsequent calls should call the loader again, not get the old exception
    try {
        cache.getUnchecked("bar");
        fail();
    } catch (InvalidCacheLoadException expected) {
    }
    assertEquals(count + 1, callCount.get());
}
Also used : InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with InvalidCacheLoadException

use of com.google.common.cache.CacheLoader.InvalidCacheLoadException in project guava by google.

the class LocalCache method loadAll.

/**
 * Returns the result of calling {@link CacheLoader#loadAll}, or null if {@code loader} doesn't
 * implement {@code loadAll}.
 */
@Nullable
Map<K, V> loadAll(Set<? extends K> keys, CacheLoader<? super K, V> loader) throws ExecutionException {
    checkNotNull(loader);
    checkNotNull(keys);
    Stopwatch stopwatch = Stopwatch.createStarted();
    Map<K, V> result;
    boolean success = false;
    try {
        // safe since all keys extend K
        @SuppressWarnings("unchecked") Map<K, V> map = (Map<K, V>) loader.loadAll(keys);
        result = map;
        success = true;
    } catch (UnsupportedLoadingOperationException e) {
        success = true;
        throw e;
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new ExecutionException(e);
    } catch (RuntimeException e) {
        throw new UncheckedExecutionException(e);
    } catch (Exception e) {
        throw new ExecutionException(e);
    } catch (Error e) {
        throw new ExecutionError(e);
    } finally {
        if (!success) {
            globalStatsCounter.recordLoadException(stopwatch.elapsed(NANOSECONDS));
        }
    }
    if (result == null) {
        globalStatsCounter.recordLoadException(stopwatch.elapsed(NANOSECONDS));
        throw new InvalidCacheLoadException(loader + " returned null map from loadAll");
    }
    stopwatch.stop();
    // TODO(fry): batch by segment
    boolean nullsPresent = false;
    for (Entry<K, V> entry : result.entrySet()) {
        K key = entry.getKey();
        V value = entry.getValue();
        if (key == null || value == null) {
            // delay failure until non-null entries are stored
            nullsPresent = true;
        } else {
            put(key, value);
        }
    }
    if (nullsPresent) {
        globalStatsCounter.recordLoadException(stopwatch.elapsed(NANOSECONDS));
        throw new InvalidCacheLoadException(loader + " returned null keys or values from loadAll");
    }
    // TODO(fry): record count of loaded entries
    globalStatsCounter.recordLoadSuccess(stopwatch.elapsed(NANOSECONDS));
    return result;
}
Also used : ExecutionError(com.google.common.util.concurrent.ExecutionError) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Stopwatch(com.google.common.base.Stopwatch) ExecutionError(com.google.common.util.concurrent.ExecutionError) UnsupportedLoadingOperationException(com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException) InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) AbstractMap(java.util.AbstractMap) UnsupportedLoadingOperationException(com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 3 with InvalidCacheLoadException

use of com.google.common.cache.CacheLoader.InvalidCacheLoadException in project guava by google.

the class CacheLoadingTest method testConcurrentLoadingNull.

/**
 * On a concurrent computation that returns null, all threads should get an
 * InvalidCacheLoadException, with the loader only called once. The result should not be cached (a
 * later request should call the loader again).
 */
private static void testConcurrentLoadingNull(CacheBuilder<Object, Object> builder) throws InterruptedException {
    int count = 10;
    final AtomicInteger callCount = new AtomicInteger();
    final CountDownLatch startSignal = new CountDownLatch(count + 1);
    LoadingCache<String, String> cache = builder.build(new CacheLoader<String, String>() {

        @Override
        public String load(String key) throws InterruptedException {
            callCount.incrementAndGet();
            startSignal.await();
            return null;
        }
    });
    List<Object> result = doConcurrentGet(cache, "bar", count, startSignal);
    assertEquals(1, callCount.get());
    for (int i = 0; i < count; i++) {
        assertThat(result.get(i)).isInstanceOf(InvalidCacheLoadException.class);
    }
    // subsequent calls should call the loader again, not get the old exception
    try {
        cache.getUnchecked("bar");
        fail();
    } catch (InvalidCacheLoadException expected) {
    }
    assertEquals(2, callCount.get());
}
Also used : InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with InvalidCacheLoadException

use of com.google.common.cache.CacheLoader.InvalidCacheLoadException in project guava by hceylan.

the class LocalCache method loadAll.

/**
 * Returns the result of calling {@link CacheLoader#loadAll}, or null if {@code loader} doesn't
 * implement {@code loadAll}.
 */
@Nullable
Map<K, V> loadAll(Set<? extends K> keys, CacheLoader<? super K, V> loader) throws ExecutionException {
    Stopwatch stopwatch = new Stopwatch().start();
    Map<K, V> result;
    boolean success = false;
    try {
        // safe since all keys extend K
        @SuppressWarnings("unchecked") Map<K, V> map = (Map<K, V>) loader.loadAll(keys);
        result = map;
        success = true;
    } catch (UnsupportedLoadingOperationException e) {
        success = true;
        throw e;
    } catch (RuntimeException e) {
        throw new UncheckedExecutionException(e);
    } catch (Exception e) {
        throw new ExecutionException(e);
    } catch (Error e) {
        throw new ExecutionError(e);
    } finally {
        if (!success) {
            globalStatsCounter.recordLoadException(stopwatch.elapsedTime(NANOSECONDS));
        }
    }
    if (result == null) {
        globalStatsCounter.recordLoadException(stopwatch.elapsedTime(NANOSECONDS));
        throw new InvalidCacheLoadException(loader + " returned null map from loadAll");
    }
    stopwatch.stop();
    // TODO(fry): batch by segment
    boolean nullsPresent = false;
    for (Map.Entry<K, V> entry : result.entrySet()) {
        K key = entry.getKey();
        V value = entry.getValue();
        if (key == null || value == null) {
            // delay failure until non-null entries are stored
            nullsPresent = true;
        } else {
            put(key, value);
        }
    }
    if (nullsPresent) {
        globalStatsCounter.recordLoadException(stopwatch.elapsedTime(NANOSECONDS));
        throw new InvalidCacheLoadException(loader + " returned null keys or values from loadAll");
    }
    // TODO(fry): record count of loaded entries
    globalStatsCounter.recordLoadSuccess(stopwatch.elapsedTime(NANOSECONDS));
    return result;
}
Also used : ExecutionError(com.google.common.util.concurrent.ExecutionError) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Stopwatch(com.google.common.base.Stopwatch) ExecutionError(com.google.common.util.concurrent.ExecutionError) UnsupportedLoadingOperationException(com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException) InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) AbstractMap(java.util.AbstractMap) UnsupportedLoadingOperationException(com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException) Nullable(javax.annotation.Nullable)

Example 5 with InvalidCacheLoadException

use of com.google.common.cache.CacheLoader.InvalidCacheLoadException in project guava by hceylan.

the class CacheLoadingTest method testConcurrentLoadingNull.

/**
 * On a concurrent computation that returns null, all threads should get an
 * InvalidCacheLoadException, with the loader only called once. The result should not be cached
 * (a later request should call the loader again).
 */
private static void testConcurrentLoadingNull(CacheBuilder<Object, Object> builder) throws InterruptedException {
    int count = 10;
    final AtomicInteger callCount = new AtomicInteger();
    final CountDownLatch startSignal = new CountDownLatch(count + 1);
    LoadingCache<String, String> cache = builder.build(new CacheLoader<String, String>() {

        @Override
        public String load(String key) throws InterruptedException {
            callCount.incrementAndGet();
            startSignal.await();
            return null;
        }
    });
    List<Object> result = doConcurrentGet(cache, "bar", count, startSignal);
    assertEquals(1, callCount.get());
    for (int i = 0; i < count; i++) {
        assertTrue(result.get(i) instanceof InvalidCacheLoadException);
    }
    // subsequent calls should call the loader again, not get the old exception
    try {
        cache.getUnchecked("bar");
        fail();
    } catch (InvalidCacheLoadException expected) {
    }
    assertEquals(2, callCount.get());
}
Also used : InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

InvalidCacheLoadException (com.google.common.cache.CacheLoader.InvalidCacheLoadException)5 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Stopwatch (com.google.common.base.Stopwatch)2 UnsupportedLoadingOperationException (com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ExecutionError (com.google.common.util.concurrent.ExecutionError)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)2 IOException (java.io.IOException)2 AbstractMap (java.util.AbstractMap)2 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 Nullable (javax.annotation.Nullable)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1