Notify span listeners even if index store fails in SimpleCache.removeSpan

This fixes infinite loop in LeastRecentlyUsedCacheEvictor.evictCache when index store fails.

Also made CachedContentIndex not final so it can be mocked and added a package protected SimpleCache
constructor so mock index can be injected.

Issue: #3260

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=169249517
This commit is contained in:
eguven 2017-09-19 09:10:35 -07:00 committed by Oliver Woodman
parent 8b43d896f1
commit c127a577d7
2 changed files with 22 additions and 7 deletions

View file

@ -49,7 +49,7 @@ import javax.crypto.spec.SecretKeySpec;
/**
* This class maintains the index of cached content.
*/
/*package*/ final class CachedContentIndex {
/*package*/ class CachedContentIndex {
public static final String FILE_NAME = "cached_content_index.exi";

View file

@ -48,7 +48,7 @@ public final class SimpleCache implements Cache {
* @param evictor The evictor to be used.
*/
public SimpleCache(File cacheDir, CacheEvictor evictor) {
this(cacheDir, evictor, null);
this(cacheDir, evictor, null, false);
}
/**
@ -75,10 +75,22 @@ public final class SimpleCache implements Cache {
* @param encrypt When false, a plaintext index will be written.
*/
public SimpleCache(File cacheDir, CacheEvictor evictor, byte[] secretKey, boolean encrypt) {
this(cacheDir, evictor, new CachedContentIndex(cacheDir, secretKey, encrypt));
}
/**
* Constructs the cache. The cache will delete any unrecognized files from the directory. Hence
* the directory cannot be used to store other files.
*
* @param cacheDir A dedicated cache directory.
* @param evictor The evictor to be used.
* @param index The CachedContentIndex to be used.
*/
/*package*/ SimpleCache(File cacheDir, CacheEvictor evictor, CachedContentIndex index) {
this.cacheDir = cacheDir;
this.evictor = evictor;
this.lockedSpans = new HashMap<>();
this.index = new CachedContentIndex(cacheDir, secretKey, encrypt);
this.index = index;
this.listeners = new HashMap<>();
// Start cache initialization.
final ConditionVariable conditionVariable = new ConditionVariable();
@ -305,11 +317,14 @@ public final class SimpleCache implements Cache {
return;
}
totalSpace -= span.length;
if (removeEmptyCachedContent && cachedContent.isEmpty()) {
index.removeEmpty(cachedContent.key);
index.store();
try {
if (removeEmptyCachedContent && cachedContent.isEmpty()) {
index.removeEmpty(cachedContent.key);
index.store();
}
} finally {
notifySpanRemoved(span);
}
notifySpanRemoved(span);
}
@Override