Skip to content

Attempt to call close() on evicted cache values #3804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.function.Function;
import software.amazon.awssdk.annotations.SdkProtectedApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.utils.Logger;
import software.amazon.awssdk.utils.Validate;

/**
Expand All @@ -40,6 +41,8 @@
@ThreadSafe
public final class LruCache<K, V> {

private static final Logger log = Logger.loggerFor(LruCache.class);

private static final int DEFAULT_SIZE = 100;

private final Map<K, CacheEntry<K, V>> cache;
Expand Down Expand Up @@ -146,10 +149,21 @@ private void addToQueue(CacheEntry<K, V> entry) {
*/
private void evict() {
leastRecentlyUsed.isEvicted(true);
closeEvictedResourcesIfPossible(leastRecentlyUsed.value);
cache.remove(leastRecentlyUsed.key());
removeFromQueue(leastRecentlyUsed);
}

private void closeEvictedResourcesIfPossible(V value) {
if (value instanceof AutoCloseable) {
try {
((AutoCloseable) value).close();
} catch (Exception e) {
log.warn(() -> "Attempted to close instance that was evicted by cache, but got exception: " + e.getMessage());
}
}
}

public int size() {
return cache.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -113,6 +114,37 @@ void when_cacheFillsUp_ValuesAreEvictedFromCache() {
verify(simpleValueSupplier, times(1)).apply(simpleTestKeys.get(4));
}

@Test
void when_closeableValuesAreEvicted_CloseMethodIsCalled() {
int cacheSize = 3;
int evictNum = 2;
LruCache<Integer, CloseableClass> cache = LruCache.builder(CloseableClass::new)
.maxSize(cacheSize)
.build();
CloseableClass.reset();
for (int i = 0; i < cacheSize + evictNum; i++) {
cache.get(i);
}
assertThat(CloseableClass.evictedItems()).isNotEmpty();
assertThat(CloseableClass.evictedItems()).hasSize(evictNum);
assertThat(CloseableClass.evictedItems().get(0)).isEqualTo(0);
assertThat(CloseableClass.evictedItems().get(1)).isEqualTo(1);
}

@Test
void when_closeableValuesAreEvicted_NoExceptionsAreThrownIfCloseFails() {
int cacheSize = 3;
int evictNum = 2;
LruCache<Integer, FaultyCloseableClass> cache = LruCache.builder(FaultyCloseableClass::new)
.maxSize(cacheSize)
.build();
CloseableClass.reset();
for (int i = 0; i < cacheSize + evictNum; i++) {
cache.get(i);
}
assertThat(CloseableClass.evictedItems()).isEmpty();
}

@Test
void when_mostRecentValueIsHit_ValuesAreReorderedCorrectly() {
LruCache<Integer, String> cache = simpleCache.get();
Expand Down Expand Up @@ -257,4 +289,42 @@ public String apply(Integer key) {
return value;
}
}

private static class CloseableClass implements AutoCloseable {

private static List<Integer> evictedList = new ArrayList<>();

private final Integer key;
CloseableClass(Integer key) {
this.key = key;
}
public Integer get() throws Exception {
return key;
}

public static void reset() {
evictedList = new ArrayList<>();
}

public static List<Integer> evictedItems() {
return Collections.unmodifiableList(evictedList);
}

@Override
public void close() {
evictedList.add(key);
}
}

private static class FaultyCloseableClass extends CloseableClass {

FaultyCloseableClass(Integer key) {
super(key);
}

@Override
public void close() {
throw new RuntimeException("Could not close resources!");
}
}
}