Class AbstractEviction
- java.lang.Object
-
- org.cache2k.core.eviction.AbstractEviction
-
- All Implemented Interfaces:
NeedsClose,Eviction
- Direct Known Subclasses:
ClockProPlusEviction
public abstract class AbstractEviction extends Object implements Eviction
Base class for different eviction algorithms, implementing statistics counting and chunking.Eviction happens in chunks, to do more work in one thread. Eviction may happen in different threads in parallel, in case lots of concurrent inserts triggers eviction. Theoretically it could be more effective to assign only one thread for eviction and exploit locality and make other threads carrying out insert wait to avoid overflowing the system. Since eviction listeners may be called as well, it is more general useful to run eviction in multiple threads in parallel if needed.
An eviction is basically: finding an eviction candidate based on the eviction algorithm
findEvictionCandidate(), mark entry for processing and call the eviction listener,- Author:
- Jens Wilke
-
-
Field Summary
Fields Modifier and Type Field Description protected HeapCacheForEvictionheapCacheprotected longidleNonEvictDrainCountprotected intidleScanRoundIncremented at start of new idle scan round to account for removals.protected Objectlockstatic intMAXIMAL_CHUNK_SIZEprotected longmaxSizeSize limit in number of entries.protected longmaxWeightMaximum allowed weight.static intMINIMAL_CHUNK_SIZEstatic longMINIMUM_CAPACITY_FOR_CHUNKING
-
Constructor Summary
Constructors Constructor Description AbstractEviction(HeapCacheForEviction heapCache, InternalEvictionListener listener, long maxSize, org.cache2k.operation.Weigher weigher, long maxWeight, boolean noChunking)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidchangeCapacity(long entryCountOrWeight)Update the limits and run eviction loop with chunks to get rid of entries fast.voidclose(InternalCacheCloseContext closeContext)voidevictEventually()Evict if needed, checks all segments.voidevictEventuallyBeforeInsert()Evict if needed, checks all segments.voidevictEventuallyBeforeInsertOnSegment(int hashCodeHint)Evict if needed, focused on the segment addressed by the hash code.longevictIdleEntries(int maxScan)Scan for idle (no access count since last scan) entries and evict them.protected abstract EntryfindEvictionCandidate()Find a candidate for eviction.protected abstract EntryfindIdleCandidate(int maxScan)EvictionMetricsgetMetrics()Get metrics related to the eviction.protected abstract longgetScanCount()protected abstract longgetSize()protected abstract voidinsertIntoReplacementList(Entry e)Place the entry as a new entry into the eviction data structures.booleanisWeigherPresent()longremoveAll()Remove all entries from the eviction data structure.protected abstract longremoveAllFromReplacementList()Remove entries from the replacement list without locking the entry itself.protected abstract voidremoveFromReplacementList(Entry e)Remove entry from the eviction data structures, because it was evicted or deleted.protected abstract voidremoveFromReplacementListOnEvict(Entry e)Identical toremoveFromReplacementList(Entry)by default but allows the eviction algorithm to do additional bookkeeping of eviction history.<T> TrunLocked(Supplier<T> j)Runs job making sure concurrent evictions operations pause.longstartNewIdleScanRound()Start a new idle scan round.booleansubmitWithoutTriggeringEviction(Entry e)Submit to eviction for inserting or removing from the replacement list.StringtoString()protected voidupdateAccumulatedWeightInLock(Entry e)Update total weight in this eviction segment.protected abstract voidupdateHotMax()Gets called when eviction is needed.protected voidupdateTotalWeightForRemove(Entry e)Called upon eviction or deletionbooleanupdateWeight(Entry e)Updates the weight on the entry and recalculates the total weight if needed.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.cache2k.core.eviction.Eviction
checkIntegrity
-
-
-
-
Field Detail
-
MINIMAL_CHUNK_SIZE
public static final int MINIMAL_CHUNK_SIZE
- See Also:
- Constant Field Values
-
MAXIMAL_CHUNK_SIZE
public static final int MAXIMAL_CHUNK_SIZE
- See Also:
- Constant Field Values
-
MINIMUM_CAPACITY_FOR_CHUNKING
public static final long MINIMUM_CAPACITY_FOR_CHUNKING
- See Also:
- Constant Field Values
-
heapCache
protected final HeapCacheForEviction heapCache
-
lock
protected final Object lock
-
maxSize
protected long maxSize
Size limit in number of entries. Derived fromCache2kConfig.getEntryCapacity()May be changed during runtime. Guarded by lock.
-
maxWeight
protected long maxWeight
Maximum allowed weight. May be changed during runtime. Guarded by lock.
-
idleScanRound
protected int idleScanRound
Incremented at start of new idle scan round to account for removals. Only a few bits used, max value isEntry.SCAN_ROUND_MASK.
-
idleNonEvictDrainCount
protected long idleNonEvictDrainCount
-
-
Constructor Detail
-
AbstractEviction
public AbstractEviction(HeapCacheForEviction heapCache, InternalEvictionListener listener, long maxSize, org.cache2k.operation.Weigher weigher, long maxWeight, boolean noChunking)
-
-
Method Detail
-
startNewIdleScanRound
public long startNewIdleScanRound()
Start a new idle scan round. At the moment this resets the drain count and increments the scan round counter.Future: init chunk size for more efficient eviction, update/adapt hot max
- Specified by:
startNewIdleScanRoundin interfaceEviction- See Also:
IdleScan
-
submitWithoutTriggeringEviction
public boolean submitWithoutTriggeringEviction(Entry e)
Description copied from interface:EvictionSubmit to eviction for inserting or removing from the replacement list. However, eviction should not be triggered (which in turn triggers a hash table update) since the hash segment lock is hold at the moment.- Specified by:
submitWithoutTriggeringEvictionin interfaceEviction
-
isWeigherPresent
public boolean isWeigherPresent()
- Specified by:
isWeigherPresentin interfaceEviction
-
updateTotalWeightForRemove
protected void updateTotalWeightForRemove(Entry e)
Called upon eviction or deletion
-
updateWeight
public boolean updateWeight(Entry e)
Description copied from interface:EvictionUpdates the weight on the entry and recalculates the total weight if needed.Expected not to hold the entry lock, which means, that this does not run in sync with the actual update. That is okay as long as it runs after every update.
Since we need to lock the eviction structure, this can happen in a separate thread.
- Specified by:
updateWeightin interfaceEviction- Returns:
- hint whether eviction should be run. for a bulk operation we want to do eviction once, so not do it within this method
-
updateAccumulatedWeightInLock
protected void updateAccumulatedWeightInLock(Entry e)
Update total weight in this eviction segment. The total weight differs from the accumulated entry weight, since it is based on the stored decompressed, compressed weight. We calculate based on the stored weight, because we don't want to call the weigher for deletion again, which may cause wrong counts.
-
evictEventuallyBeforeInsert
public void evictEventuallyBeforeInsert()
Description copied from interface:EvictionEvict if needed, checks all segments. Called before a new entry is inserted (changed from after in v1.4)- Specified by:
evictEventuallyBeforeInsertin interfaceEviction
-
evictEventuallyBeforeInsertOnSegment
public void evictEventuallyBeforeInsertOnSegment(int hashCodeHint)
Description copied from interface:EvictionEvict if needed, focused on the segment addressed by the hash code. Called before a new entry is inserted (changed from after in v1.4)- Specified by:
evictEventuallyBeforeInsertOnSegmentin interfaceEviction
-
evictEventually
public void evictEventually()
Description copied from interface:EvictionEvict if needed, checks all segments.- Specified by:
evictEventuallyin interfaceEviction
-
evictIdleEntries
public long evictIdleEntries(int maxScan)
Description copied from interface:EvictionScan for idle (no access count since last scan) entries and evict them.- Specified by:
evictIdleEntriesin interfaceEviction- Returns:
- number of evicted entries
- See Also:
IdleScan
-
getMetrics
public EvictionMetrics getMetrics()
Description copied from interface:EvictionGet metrics related to the eviction. No lock is needed before calling this method. Best efforts are made to extract consistent statistics and keep interruption short.- Specified by:
getMetricsin interfaceEviction
-
close
public void close(InternalCacheCloseContext closeContext)
- Specified by:
closein interfaceNeedsClose
-
runLocked
public <T> T runLocked(Supplier<T> j)
Description copied from interface:EvictionRuns job making sure concurrent evictions operations pause.
-
changeCapacity
public void changeCapacity(long entryCountOrWeight)
Update the limits and run eviction loop with chunks to get rid of entries fast. Gives up the lock to send events and allow other cache operations while adaption happens.- Specified by:
changeCapacityin interfaceEviction
-
removeAll
public final long removeAll()
Description copied from interface:EvictionRemove all entries from the eviction data structure.
-
getSize
protected abstract long getSize()
-
removeAllFromReplacementList
protected abstract long removeAllFromReplacementList()
Remove entries from the replacement list without locking the entry itself.
-
insertIntoReplacementList
protected abstract void insertIntoReplacementList(Entry e)
Place the entry as a new entry into the eviction data structures.
-
findEvictionCandidate
protected abstract Entry findEvictionCandidate()
Find a candidate for eviction. The method may return the identical if called many times but not sufficient more candidates are available. In any situation, subsequent calls must iterate all entries.
-
findIdleCandidate
protected abstract Entry findIdleCandidate(int maxScan)
-
removeFromReplacementListOnEvict
protected abstract void removeFromReplacementListOnEvict(Entry e)
Identical toremoveFromReplacementList(Entry)by default but allows the eviction algorithm to do additional bookkeeping of eviction history.
-
removeFromReplacementList
protected abstract void removeFromReplacementList(Entry e)
Remove entry from the eviction data structures, because it was evicted or deleted.
-
updateHotMax
protected abstract void updateHotMax()
Gets called when eviction is needed. Used by the eviction algorithm to update the clock sizes.
-
getScanCount
protected abstract long getScanCount()
-
-