/** * Obliterate a list of keys. * * @param keys keys to obliterate * @throws StoreAccessException in case of store failure */ default void obliterate(Iterable<? extends K> keys) throws StoreAccessException { for (K key : keys) { obliterate(key); } }
/** * Helper method for handling runtime exceptions. * <p> * Returns most as {@link StoreAccessException} except for {@code StorePassThroughException}. * In which case if its cause is a {@link RuntimeException} it is thrown and if not it is returned * wrapped in a {@code StoreAccessException}. * * @param re the exception to handler * @return StoreAccessException to be thrown * @throws RuntimeException if {@code re} is a {@code StorePassThroughException} containing a {@code RuntimeException} */ public static StoreAccessException handleException(Exception re) { if(re instanceof StorePassThroughException) { Throwable cause = re.getCause(); if(cause instanceof RuntimeException) { throw (RuntimeException) cause; } else { return new StoreAccessException(cause); } } else { return new StoreAccessException(re); } } }
private ValueHolder<V> handleStoreAccessException(StoreAccessException ce) throws StoreAccessException { Throwable cause = ce.getCause(); if (cause instanceof StorePassThroughException) { throw (StoreAccessException) cause.getCause(); } if (cause instanceof Error) { throw (Error) cause; } if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } throw new RuntimeException("Unexpected checked exception wrapped in StoreAccessException", cause); }
/** * Tests {@link Ehcache#containsKey(Object) Ehcache.containsKey} over an empty cache * where {@link Store#containsKey(Object) Store.containsKey} throws a * {@link StoreAccessException StoreAccessException}. */ @Test public void testContainsKeyEmptyStoreAccessException() throws Exception { final FakeStore realStore = new FakeStore(Collections.<String, String>emptyMap()); this.store = spy(realStore); doThrow(new StoreAccessException("")).when(this.store).containsKey("key"); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.containsKey("key"); verify(this.resilienceStrategy).containsKeyFailure(eq("key"), any(StoreAccessException.class)); }
/** * Tests {@link Ehcache#clear()} over an empty cache where * {@link Store#clear() Store.clear} throws a * {@link StoreAccessException StoreAccessException}. */ @Test public void testClearEmptyStoreAccessException() throws Exception { final FakeStore realStore = new FakeStore(Collections.<String, String>emptyMap()); this.store = spy(realStore); doThrow(new StoreAccessException("")).when(this.store).clear(); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.clear(); verify(this.resilienceStrategy).clearFailure(any(StoreAccessException.class)); }
/** * {@inheritDoc} */ @Override public boolean containsKey(final K key) { statusTransitioner.checkAvailable(); checkNonNull(key); try { return store.containsKey(key); } catch (StoreAccessException e) { return resilienceStrategy.containsKeyFailure(key, e); } }
/** * Tests {@link Ehcache#containsKey(Object) Ehcache.containsKey} over a cache holding * the target key where {@link Store#containsKey(Object) Store.containsKey} * throws a {@link StoreAccessException StoreAccessException}. */ @Test public void testContainsKeyContainsStoreAccessException() throws Exception { final FakeStore realStore = new FakeStore(this.getTestStoreEntries()); this.store = spy(realStore); doThrow(new StoreAccessException("")).when(this.store).containsKey("keyA"); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.containsKey("keyA"); verify(this.resilienceStrategy).containsKeyFailure(eq("keyA"), any(StoreAccessException.class)); }
/** * Clear all entries from the store. * * @param from original failure causing the cleanup */ protected void cleanup(StoreAccessException from) { try { store.obliterate(); } catch (StoreAccessException e) { inconsistent(from, e); return; } recovered(from); }
@Override public Map<K, ValueHolder<V>> bulkComputeIfAbsent(Set<? extends K> keys, Function<Iterable<? extends K>, Iterable<? extends Map.Entry<? extends K, ? extends V>>> mappingFunction) throws StoreAccessException { throw new StoreAccessException("Problem trying to bulk compute"); } };
@Override public void putFailure(K key, V value, StoreAccessException e) { if (e.getCause() instanceof ReconnectInProgressException) { throw new RuntimeException(e); } }
/** * Tests {@link Ehcache#containsKey(Object) Ehcache.containsKey} over a non-empty cache * not holding the target key where {@link Store#containsKey(Object) Store.containsKey} * throws a {@link StoreAccessException StoreAccessException}. */ @Test public void testContainsKeyMissingStoreAccessException() throws Exception { final FakeStore realStore = new FakeStore(this.getTestStoreEntries()); this.store = spy(realStore); doThrow(new StoreAccessException("")).when(this.store).containsKey("missingKey"); final Ehcache<String, String> ehcache = this.getEhcache(); ehcache.containsKey("missingKey"); verify(this.resilienceStrategy).containsKeyFailure(eq("missingKey"), any(StoreAccessException.class)); }
/** * Clean the key from the store. * * @param key key to clean * @param from original failure causing the cleanup */ protected void cleanup(K key, StoreAccessException from) { try { store.obliterate(key); } catch (StoreAccessException e) { inconsistent(key, from, e); return; } recovered(key, from); }
@Override public Map<K, ValueHolder<V>> bulkCompute(Set<? extends K> keys, Function<Iterable<? extends Map.Entry<? extends K, ? extends V>>, Iterable<? extends Map.Entry<? extends K, ? extends V>>> remappingFunction) throws StoreAccessException { throw new StoreAccessException("Problem trying to bulk compute"); }
/** * Clean all keys from the store. * * @param keys keys to clean * @param from original failure causing the cleanup */ protected void cleanup(Iterable<? extends K> keys, StoreAccessException from) { try { store.obliterate(keys); } catch (StoreAccessException e) { inconsistent(keys, from, e); return; } recovered(keys, from); }
private void checkFailingKey(final String key) throws StoreAccessException { if (this.failingKeys.contains(key)) { throw new StoreAccessException(String.format("Accessing failing key: %s", key)); } }
@Test public void clearFailure() throws StoreAccessException { strategy.clearFailure(accessException); verify(store).obliterate(); }
@Test public void clearFailure() throws Exception { strategy.clearFailure(accessException); verify(store).obliterate(); }
@Test public void putFailure() throws StoreAccessException { strategy.putFailure(1, 1L, accessException); verify(store).obliterate(1); }
@Test public void removeFailure() throws Exception { strategy.removeFailure(1, accessException); verify(store).obliterate(1); verify(loaderWriter).delete(1); }
@Test public void putFailure() throws Exception { strategy.putFailure(1, 1L, accessException); verify(store).obliterate(1); verify(loaderWriter).write(1, 1L); }