use of com.google.common.util.concurrent.UncheckedExecutionException 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;
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by google.
the class LocalCache method load.
// timestamps as numeric primitives
@SuppressWarnings("GoodTime")
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;
}
use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by google.
the class CacheLoadingTest method testLoadUncheckedException.
public void testLoadUncheckedException() throws ExecutionException {
Exception e = new RuntimeException();
CacheLoader<Object, Object> loader = exceptionLoader(e);
LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().recordStats().build(loader);
CacheStats stats = cache.stats();
assertEquals(0, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(0, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.get(new Object());
fail();
} catch (UncheckedExecutionException expected) {
assertThat(expected).hasCauseThat().isSameInstanceAs(e);
}
stats = cache.stats();
assertEquals(1, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(1, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.getUnchecked(new Object());
fail();
} catch (UncheckedExecutionException expected) {
assertThat(expected).hasCauseThat().isSameInstanceAs(e);
}
stats = cache.stats();
assertEquals(2, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(2, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
cache.refresh(new Object());
checkLoggedCause(e);
stats = cache.stats();
assertEquals(2, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(3, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
Exception callableException = new RuntimeException();
try {
cache.get(new Object(), throwing(callableException));
fail();
} catch (UncheckedExecutionException expected) {
assertThat(expected).hasCauseThat().isSameInstanceAs(callableException);
}
stats = cache.stats();
assertEquals(3, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(4, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
try {
cache.getAll(asList(new Object()));
fail();
} catch (UncheckedExecutionException expected) {
assertThat(expected).hasCauseThat().isSameInstanceAs(e);
}
stats = cache.stats();
assertEquals(4, stats.missCount());
assertEquals(0, stats.loadSuccessCount());
assertEquals(5, stats.loadExceptionCount());
assertEquals(0, stats.hitCount());
}
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);
assertThat((ExecutionException) result.get(i)).hasCauseThat().isSameInstanceAs(e);
} else {
assertThat(result.get(i)).isInstanceOf(UncheckedExecutionException.class);
assertThat((UncheckedExecutionException) result.get(i)).hasCauseThat().isSameInstanceAs(e);
}
}
// subsequent calls should call the loader again, not get the old exception
try {
cache.getUnchecked("bar");
fail();
} catch (UncheckedExecutionException expected) {
}
assertEquals(2, callCount.get());
}
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);
assertThat(((UncheckedExecutionException) result.get(i))).hasCauseThat().isSameInstanceAs(e);
}
// subsequent calls should call the loader again, not get the old exception
try {
cache.getUnchecked("bar");
fail();
} catch (UncheckedExecutionException expected) {
}
assertEquals(2, callCount.get());
}
Aggregations