Search in sources :

Example 16 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project azure-sdk-for-java by Azure.

the class CachingKeyResolverTest method KeyVault_CachingKeyResolverThrows.

/* 
     * Tests the behavior of CachingKeyResolver when resolving key throws 
     * and validate that the failed entity is not added to the cache. 
     */
@Test
public void KeyVault_CachingKeyResolverThrows() {
    IKeyResolver mockedKeyResolver = mock(IKeyResolver.class);
    CachingKeyResolver resolver = new CachingKeyResolver(10, mockedKeyResolver);
    // First throw exception and for the second call return a value
    when(mockedKeyResolver.resolveKeyAsync(keyId)).thenThrow(new RuntimeException("test")).thenReturn(ikeyAsync);
    try {
        resolver.resolveKeyAsync(keyId);
        assertFalse("Should have thrown an exception.", true);
    } catch (UncheckedExecutionException e) {
        assertTrue("RuntimeException is expected.", e.getCause() instanceof RuntimeException);
    }
    resolver.resolveKeyAsync(keyId);
    resolver.resolveKeyAsync(keyId);
    verify(mockedKeyResolver, times(2)).resolveKeyAsync(keyId);
}
Also used : IKeyResolver(com.microsoft.azure.keyvault.core.IKeyResolver) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) CachingKeyResolver(com.microsoft.azure.keyvault.extensions.CachingKeyResolver) Test(org.junit.Test)

Example 17 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project jackrabbit-oak by apache.

the class MongoDocumentStore method find.

@SuppressWarnings("unchecked")
private <T extends Document> T find(final Collection<T> collection, final String key, boolean preferCached, final int maxCacheAge) {
    if (collection != Collection.NODES) {
        return findUncachedWithRetry(collection, key, DocumentReadPreference.PRIMARY, 2);
    }
    NodeDocument doc;
    if (maxCacheAge > 0 || preferCached) {
        // first try without lock
        doc = nodesCache.getIfPresent(key);
        if (doc != null) {
            if (preferCached || getTime() - doc.getCreated() < maxCacheAge) {
                stats.doneFindCached(collection, key);
                if (doc == NodeDocument.NULL) {
                    return null;
                }
                return (T) doc;
            }
        }
    }
    Throwable t;
    try {
        Lock lock = nodeLocks.acquire(key);
        try {
            if (maxCacheAge > 0 || preferCached) {
                // try again some other thread may have populated
                // the cache by now
                doc = nodesCache.getIfPresent(key);
                if (doc != null) {
                    if (preferCached || getTime() - doc.getCreated() < maxCacheAge) {
                        stats.doneFindCached(collection, key);
                        if (doc == NodeDocument.NULL) {
                            return null;
                        }
                        return (T) doc;
                    }
                }
            }
            final NodeDocument d = (NodeDocument) findUncachedWithRetry(collection, key, getReadPreference(maxCacheAge), 2);
            invalidateCache(collection, key);
            doc = nodesCache.get(key, new Callable<NodeDocument>() {

                @Override
                public NodeDocument call() throws Exception {
                    return d == null ? NodeDocument.NULL : d;
                }
            });
        } finally {
            lock.unlock();
        }
        if (doc == NodeDocument.NULL) {
            return null;
        } else {
            return (T) doc;
        }
    } catch (UncheckedExecutionException e) {
        t = e.getCause();
    } catch (ExecutionException e) {
        t = e.getCause();
    } catch (RuntimeException e) {
        t = e;
    }
    throw new DocumentStoreException("Failed to load document with " + key, t);
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable) Lock(java.util.concurrent.locks.Lock)

Example 18 with UncheckedExecutionException

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

the class MergeabilityCacheImpl method get.

@Override
public boolean get(ObjectId commit, Ref intoRef, SubmitType submitType, String mergeStrategy, Branch.NameKey dest, Repository repo) {
    ObjectId into = intoRef != null ? intoRef.getObjectId() : ObjectId.zeroId();
    EntryKey key = new EntryKey(commit, into, submitType, mergeStrategy);
    try {
        return cache.get(key, () -> {
            if (key.into.equals(ObjectId.zeroId())) {
                // Assume yes on new branch.
                return true;
            }
            try (CodeReviewRevWalk rw = CodeReviewCommit.newRevWalk(repo)) {
                Set<RevCommit> accepted = SubmitDryRun.getAlreadyAccepted(repo, rw);
                accepted.add(rw.parseCommit(key.into));
                accepted.addAll(Arrays.asList(rw.parseCommit(key.commit).getParents()));
                return submitDryRun.run(key.submitType, repo, rw, dest, key.into, key.commit, accepted);
            }
        });
    } catch (ExecutionException | UncheckedExecutionException e) {
        log.error(String.format("Error checking mergeability of %s into %s (%s)", key.commit.name(), key.into.name(), key.submitType.name()), e.getCause());
        return false;
    }
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ObjectId(org.eclipse.jgit.lib.ObjectId) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 19 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project google-cloud-java by GoogleCloudPlatform.

the class BaseEmulatorHelper method waitForProcess.

private static int waitForProcess(final Process process, Duration timeout) throws InterruptedException, TimeoutException {
    if (process == null) {
        return 0;
    }
    final SettableFuture<Integer> exitValue = SettableFuture.create();
    Thread waiter = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                exitValue.set(process.waitFor());
            } catch (InterruptedException e) {
                exitValue.setException(e);
            }
        }
    });
    waiter.start();
    try {
        return exitValue.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        if (e.getCause() instanceof InterruptedException) {
            throw (InterruptedException) e.getCause();
        }
        throw new UncheckedExecutionException(e);
    } finally {
        waiter.interrupt();
    }
}
Also used : BigInteger(java.math.BigInteger) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 20 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project caffeine by ben-manes.

the class CacheLoadingTest method testLoadUncheckedException.

public void testLoadUncheckedException() throws ExecutionException {
    Exception e = new RuntimeException();
    CacheLoader<Object, Object> loader = exceptionLoader(e);
    LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder().recordStats().executor(MoreExecutors.directExecutor()), 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) {
        assertSame(e, expected.getCause());
    }
    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) {
        assertSame(e, expected.getCause());
    }
    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) {
        assertSame(callableException, expected.getCause());
    }
    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) {
        assertSame(e, expected.getCause());
    }
    stats = cache.stats();
    assertEquals(4, stats.missCount());
    assertEquals(0, stats.loadSuccessCount());
    assertEquals(5, stats.loadExceptionCount());
    assertEquals(0, stats.hitCount());
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) 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