Search in sources :

Example 26 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by google.

the class LocalCache method load.

private V load(Object key) throws ExecutionException {
    long startTime = ticker.read();
    V calculatedValue;
    try {
        /*
       * This cast isn't safe, but we can rely on the fact that K is almost always passed to
       * Map.get(), and tools like IDEs and Findbugs can catch situations where this isn't the
       * case.
       *
       * The alternative is to add an overloaded method, but the chances of a user calling get()
       * instead of the new API and the risks inherent in adding a new API outweigh this little
       * hole.
       */
        K castKey = (K) key;
        calculatedValue = loader.load(castKey);
        put(castKey, calculatedValue);
    } catch (RuntimeException e) {
        statsCounter.recordLoadException(ticker.read() - startTime);
        throw new UncheckedExecutionException(e);
    } catch (Exception e) {
        statsCounter.recordLoadException(ticker.read() - startTime);
        throw new ExecutionException(e);
    } catch (Error e) {
        statsCounter.recordLoadException(ticker.read() - startTime);
        throw new ExecutionError(e);
    }
    if (calculatedValue == null) {
        String message = loader + " returned null for key " + key + ".";
        throw new CacheLoader.InvalidCacheLoadException(message);
    }
    statsCounter.recordLoadSuccess(ticker.read() - startTime);
    return calculatedValue;
}
Also used : ExecutionError(com.google.common.util.concurrent.ExecutionError) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionError(com.google.common.util.concurrent.ExecutionError) ExecutionException(java.util.concurrent.ExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NoSuchElementException(java.util.NoSuchElementException)

Example 27 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by google.

the class CacheLoadingTest method testConcurrentLoadingCheckedException.

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

        @Override
        public String load(String key) throws IOException, InterruptedException {
            callCount.incrementAndGet();
            startSignal.await();
            throw e;
        }
    });
    List<Object> result = doConcurrentGet(cache, "bar", count, startSignal);
    assertEquals(1, callCount.get());
    for (int i = 0; i < count; i++) {
        // doConcurrentGet alternates between calling getUnchecked and calling get. If we call get(),
        // we should get an ExecutionException; if we call getUnchecked(), we should get an
        // UncheckedExecutionException.
        int mod = i % 3;
        if (mod == 0 || mod == 2) {
            assertThat(result.get(i)).isInstanceOf(ExecutionException.class);
            assertSame(e, ((ExecutionException) result.get(i)).getCause());
        } else {
            assertThat(result.get(i)).isInstanceOf(UncheckedExecutionException.class);
            assertSame(e, ((UncheckedExecutionException) result.get(i)).getCause());
        }
    }
    // subsequent calls should call the loader again, not get the old exception
    try {
        cache.getUnchecked("bar");
        fail();
    } catch (UncheckedExecutionException expected) {
    }
    assertEquals(2, callCount.get());
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 28 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by google.

the class CacheLoadingTest method testConcurrentLoadingUncheckedException.

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

        @Override
        public String load(String key) throws InterruptedException {
            callCount.incrementAndGet();
            startSignal.await();
            throw e;
        }
    });
    List<Object> result = doConcurrentGet(cache, "bar", count, startSignal);
    assertEquals(1, callCount.get());
    for (int i = 0; i < count; i++) {
        // doConcurrentGet alternates between calling getUnchecked and calling get, but an unchecked
        // exception thrown by the loader is always wrapped as an UncheckedExecutionException.
        assertThat(result.get(i)).isInstanceOf(UncheckedExecutionException.class);
        assertSame(e, ((UncheckedExecutionException) result.get(i)).getCause());
    }
    // subsequent calls should call the loader again, not get the old exception
    try {
        cache.getUnchecked("bar");
        fail();
    } catch (UncheckedExecutionException expected) {
    }
    assertEquals(2, callCount.get());
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 29 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by google.

the class CacheLoadingTest method testReloadAfterFailure.

public void testReloadAfterFailure() throws ExecutionException {
    final AtomicInteger count = new AtomicInteger();
    final Exception e = new IllegalStateException("exception to trigger failure on first load()");
    CacheLoader<Integer, String> failOnceFunction = new CacheLoader<Integer, String>() {

        @Override
        public String load(Integer key) throws Exception {
            if (count.getAndIncrement() == 0) {
                throw e;
            }
            return key.toString();
        }
    };
    CountingRemovalListener<Integer, String> removalListener = countingRemovalListener();
    LoadingCache<Integer, String> cache = CacheBuilder.newBuilder().removalListener(removalListener).build(failOnceFunction);
    try {
        cache.getUnchecked(1);
        fail();
    } catch (UncheckedExecutionException ue) {
        assertSame(e, ue.getCause());
    }
    assertEquals("1", cache.getUnchecked(1));
    assertEquals(0, removalListener.getCount());
    count.set(0);
    cache.refresh(2);
    checkLoggedCause(e);
    assertEquals("2", cache.getUnchecked(2));
    assertEquals(0, removalListener.getCount());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) IOException(java.io.IOException) InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) ExecutionException(java.util.concurrent.ExecutionException)

Example 30 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by google.

the class CacheLoadingTest method testBulkLoadUncheckedException.

public void testBulkLoadUncheckedException() throws ExecutionException {
    Exception e = new RuntimeException();
    CacheLoader<Object, Object> loader = exceptionLoader(e);
    LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().recordStats().build(bulkLoader(loader));
    CacheStats stats = cache.stats();
    assertEquals(0, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(0, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
    try {
        cache.getAll(asList(new Object()));
        fail();
    } catch (UncheckedExecutionException expected) {
        assertSame(e, expected.getCause());
    }
    stats = cache.stats();
    assertEquals(1, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(1, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) IOException(java.io.IOException) InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)45 ExecutionException (java.util.concurrent.ExecutionException)33 InvalidCacheLoadException (com.google.common.cache.CacheLoader.InvalidCacheLoadException)22 IOException (java.io.IOException)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 CountDownLatch (java.util.concurrent.CountDownLatch)6 GenericPageProcessor (com.facebook.presto.operator.GenericPageProcessor)3 PageProcessor (com.facebook.presto.operator.PageProcessor)3 Type (com.facebook.presto.spi.type.Type)3 SymbolToInputRewriter (com.facebook.presto.sql.planner.SymbolToInputRewriter)3 CanonicalizeExpressions.canonicalizeExpression (com.facebook.presto.sql.planner.optimizations.CanonicalizeExpressions.canonicalizeExpression)3 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)3 RowExpression (com.facebook.presto.sql.relational.RowExpression)3 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)3 Expression (com.facebook.presto.sql.tree.Expression)3 ExecutionError (com.google.common.util.concurrent.ExecutionError)3 NoSuchElementException (java.util.NoSuchElementException)3 Test (org.junit.Test)3 Stopwatch (com.google.common.base.Stopwatch)2 UnsupportedLoadingOperationException (com.google.common.cache.CacheLoader.UnsupportedLoadingOperationException)2