mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
#2677 - Add ReentrantLock for DefaultServerCache.runEviction() and trigger runEviction() via mutationCounter
If we get a lot of cache mutation (puts) between background trims then also trigger runEviction() in foreground using mutationCounter.
This commit is contained in:
1 parent
1cd319c495
commit
5c332286b6
2 files changed
+75
-62
No files matched your search
+72
-59
@@ -13,6 +13,8 @@ import java.io.Serializable;
|
|||||||
import java.lang.ref.SoftReference;
|
import java.lang.ref.SoftReference;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The default cache implementation.
|
* The default cache implementation.
|
||||||
@@ -43,10 +45,12 @@ public class DefaultServerCache implements ServerCache {
|
|||||||
|
|
||||||
protected final String name;
|
protected final String name;
|
||||||
protected final String shortName;
|
protected final String shortName;
|
||||||
private final int maxSize;
|
protected final int maxSize;
|
||||||
private final int trimFrequency;
|
protected final int trimFrequency;
|
||||||
private final int maxIdleSecs;
|
protected final int maxIdleSecs;
|
||||||
private final int maxSecsToLive;
|
protected final int maxSecsToLive;
|
||||||
|
protected final ReentrantLock lock = new ReentrantLock();
|
||||||
|
protected final AtomicLong mutationCounter = new AtomicLong();
|
||||||
|
|
||||||
public DefaultServerCache(DefaultServerCacheConfig config) {
|
public DefaultServerCache(DefaultServerCacheConfig config) {
|
||||||
this.name = config.getName();
|
this.name = config.getName();
|
||||||
@@ -187,6 +191,9 @@ public class DefaultServerCache implements ServerCache {
|
|||||||
public void put(Object key, Object value) {
|
public void put(Object key, Object value) {
|
||||||
map.put(key, new SoftReference<>(new CacheEntry(key, value)));
|
map.put(key, new SoftReference<>(new CacheEntry(key, value)));
|
||||||
putCount.increment();
|
putCount.increment();
|
||||||
|
if (mutationCounter.incrementAndGet() > 1000) {
|
||||||
|
runEviction();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -222,66 +229,72 @@ public class DefaultServerCache implements ServerCache {
|
|||||||
* Run the eviction based on Idle time, Time to live and LRU last access.
|
* Run the eviction based on Idle time, Time to live and LRU last access.
|
||||||
*/
|
*/
|
||||||
public void runEviction() {
|
public void runEviction() {
|
||||||
long trimForMaxSize;
|
lock.lock();
|
||||||
if (maxSize == 0) {
|
|
||||||
trimForMaxSize = 0;
|
|
||||||
} else {
|
|
||||||
trimForMaxSize = size() - maxSize;
|
|
||||||
}
|
|
||||||
if (maxIdleSecs == 0 && maxSecsToLive == 0 && trimForMaxSize < 0) {
|
|
||||||
// nothing to trim on this cache
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
long startNanos = System.nanoTime();
|
|
||||||
long trimmedByIdle = 0;
|
|
||||||
long trimmedByGC = 0;
|
|
||||||
long trimmedByTTL = 0;
|
|
||||||
long trimmedByLRU = 0;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
List<CacheEntry> activeList = new ArrayList<>(map.size());
|
long trimForMaxSize;
|
||||||
long idleExpireNano = startNanos - TimeUnit.SECONDS.toNanos(maxIdleSecs);
|
if (maxSize == 0) {
|
||||||
long ttlExpireNano = startNanos - TimeUnit.SECONDS.toNanos(maxSecsToLive);
|
trimForMaxSize = 0;
|
||||||
Iterator<SoftReference<CacheEntry>> it = map.values().iterator();
|
} else {
|
||||||
while (it.hasNext()) {
|
trimForMaxSize = size() - maxSize;
|
||||||
SoftReference<CacheEntry> ref = it.next();
|
|
||||||
final CacheEntry cacheEntry = ref.get();
|
|
||||||
if (cacheEntry == null) {
|
|
||||||
it.remove();
|
|
||||||
trimmedByGC++;
|
|
||||||
} else if (maxIdleSecs > 0 && idleExpireNano > cacheEntry.getLastAccessTime()) {
|
|
||||||
it.remove();
|
|
||||||
trimmedByIdle++;
|
|
||||||
} else if (maxSecsToLive > 0 && ttlExpireNano > cacheEntry.getCreateTime()) {
|
|
||||||
it.remove();
|
|
||||||
trimmedByTTL++;
|
|
||||||
} else if (trimForMaxSize > 0) {
|
|
||||||
activeList.add(cacheEntry.forSort());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (trimForMaxSize > 0 && activeList.size() > maxSize) {
|
if (maxIdleSecs == 0 && maxSecsToLive == 0 && trimForMaxSize < 0) {
|
||||||
// sort into last access time ascending
|
// nothing to trim on this cache
|
||||||
activeList.sort(BY_LAST_ACCESS);
|
mutationCounter.set(0);
|
||||||
int trimSize = getTrimSize();
|
return;
|
||||||
for (int i = trimSize; i < activeList.size(); i++) {
|
}
|
||||||
// remove if still in the cache
|
long startNanos = System.nanoTime();
|
||||||
if (map.remove(activeList.get(i).getKey()) != null) {
|
long trimmedByIdle = 0;
|
||||||
trimmedByLRU++;
|
long trimmedByGC = 0;
|
||||||
|
long trimmedByTTL = 0;
|
||||||
|
long trimmedByLRU = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
List<CacheEntry> activeList = new ArrayList<>(map.size());
|
||||||
|
long idleExpireNano = startNanos - TimeUnit.SECONDS.toNanos(maxIdleSecs);
|
||||||
|
long ttlExpireNano = startNanos - TimeUnit.SECONDS.toNanos(maxSecsToLive);
|
||||||
|
Iterator<SoftReference<CacheEntry>> it = map.values().iterator();
|
||||||
|
while (it.hasNext()) {
|
||||||
|
SoftReference<CacheEntry> ref = it.next();
|
||||||
|
final CacheEntry cacheEntry = ref.get();
|
||||||
|
if (cacheEntry == null) {
|
||||||
|
it.remove();
|
||||||
|
trimmedByGC++;
|
||||||
|
} else if (maxIdleSecs > 0 && idleExpireNano > cacheEntry.getLastAccessTime()) {
|
||||||
|
it.remove();
|
||||||
|
trimmedByIdle++;
|
||||||
|
} else if (maxSecsToLive > 0 && ttlExpireNano > cacheEntry.getCreateTime()) {
|
||||||
|
it.remove();
|
||||||
|
trimmedByTTL++;
|
||||||
|
} else if (trimForMaxSize > 0) {
|
||||||
|
activeList.add(cacheEntry.forSort());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (trimForMaxSize > 0 && activeList.size() > maxSize) {
|
||||||
|
// sort into last access time ascending
|
||||||
|
activeList.sort(BY_LAST_ACCESS);
|
||||||
|
int trimSize = getTrimSize();
|
||||||
|
for (int i = trimSize; i < activeList.size(); i++) {
|
||||||
|
// remove if still in the cache
|
||||||
|
if (map.remove(activeList.get(i).getKey()) != null) {
|
||||||
|
trimmedByLRU++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mutationCounter.set(0);
|
||||||
|
evictCount.add(trimmedByIdle);
|
||||||
|
evictCount.add(trimmedByGC);
|
||||||
|
evictCount.add(trimmedByTTL);
|
||||||
|
evictCount.add(trimmedByLRU);
|
||||||
|
if (logger.isTraceEnabled()) {
|
||||||
|
long exeMicros = TimeUnit.MICROSECONDS.convert(System.nanoTime() - startNanos, TimeUnit.NANOSECONDS);
|
||||||
|
logger.trace("Executed trim of cache {} in [{}]millis idle[{}] timeToLive[{}] accessTime[{}] gc[{}]",
|
||||||
|
name, exeMicros, trimmedByIdle, trimmedByTTL, trimmedByLRU, trimmedByGC);
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.warn("Error during trim of DefaultServerCache [" + name + "]. Cache might be bigger than desired.", e);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
evictCount.add(trimmedByIdle);
|
lock.unlock();
|
||||||
evictCount.add(trimmedByGC);
|
|
||||||
evictCount.add(trimmedByTTL);
|
|
||||||
evictCount.add(trimmedByLRU);
|
|
||||||
if (logger.isTraceEnabled()) {
|
|
||||||
long exeMicros = TimeUnit.MICROSECONDS.convert(System.nanoTime() - startNanos, TimeUnit.NANOSECONDS);
|
|
||||||
logger.trace("Executed trim of cache {} in [{}]millis idle[{}] timeToLive[{}] accessTime[{}] gc[{}]",
|
|
||||||
name, exeMicros, trimmedByIdle, trimmedByTTL, trimmedByLRU, trimmedByGC);
|
|
||||||
}
|
|
||||||
} catch (Throwable e) {
|
|
||||||
logger.warn("Error during trim of DefaultServerCache [" + name + "]. Cache might be bigger than desired.", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+3
-3
@@ -17,7 +17,7 @@ class DefaultServerCache_RunEvictionTest {
|
|||||||
cacheOptions.setMaxSize(300);
|
cacheOptions.setMaxSize(300);
|
||||||
cacheOptions.setMaxIdleSecs(1);
|
cacheOptions.setMaxIdleSecs(1);
|
||||||
cacheOptions.setMaxSecsToLive(2);
|
cacheOptions.setMaxSecsToLive(2);
|
||||||
cacheOptions.setTrimFrequency(1);
|
cacheOptions.setTrimFrequency(5);
|
||||||
|
|
||||||
ServerCacheConfig con = new ServerCacheConfig(ServerCacheType.BEAN, "foo", "foo", cacheOptions, null, null);
|
ServerCacheConfig con = new ServerCacheConfig(ServerCacheType.BEAN, "foo", "foo", cacheOptions, null, null);
|
||||||
return new DefaultServerCache(new DefaultServerCacheConfig(con));
|
return new DefaultServerCache(new DefaultServerCacheConfig(con));
|
||||||
@@ -44,10 +44,10 @@ class DefaultServerCache_RunEvictionTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void doStuff() {
|
private void doStuff() {
|
||||||
for (int i = 0; i < 500; i++) {
|
for (int i = 0; i < 5000; i++) {
|
||||||
String key = "" + random.nextInt(20000);
|
String key = "" + random.nextInt(20000);
|
||||||
int mode = random.nextInt(10);
|
int mode = random.nextInt(10);
|
||||||
if (mode < 8) {
|
if (mode < 7) {
|
||||||
cache.get(key);
|
cache.get(key);
|
||||||
} else {
|
} else {
|
||||||
cache.put(key, key + "-" + System.currentTimeMillis());
|
cache.put(key, key + "-" + System.currentTimeMillis());
|
||||||
|
|||||||
Reference in new issue
Block a user