Class 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 Detail

      • MINIMUM_CAPACITY_FOR_CHUNKING

        public static final long MINIMUM_CAPACITY_FOR_CHUNKING
        See Also:
        Constant Field Values
      • lock

        protected final Object lock
      • maxSize

        protected long maxSize
        Size limit in number of entries. Derived from Cache2kConfig.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 is Entry.SCAN_ROUND_MASK.
    • 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:
        startNewIdleScanRound in interface Eviction
        See Also:
        IdleScan
      • submitWithoutTriggeringEviction

        public boolean submitWithoutTriggeringEviction​(Entry e)
        Description copied from interface: Eviction
        Submit 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:
        submitWithoutTriggeringEviction in interface Eviction
      • updateTotalWeightForRemove

        protected void updateTotalWeightForRemove​(Entry e)
        Called upon eviction or deletion
      • updateWeight

        public boolean updateWeight​(Entry e)
        Description copied from interface: Eviction
        Updates 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:
        updateWeight in interface Eviction
        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: Eviction
        Evict if needed, checks all segments. Called before a new entry is inserted (changed from after in v1.4)
        Specified by:
        evictEventuallyBeforeInsert in interface Eviction
      • evictEventuallyBeforeInsertOnSegment

        public void evictEventuallyBeforeInsertOnSegment​(int hashCodeHint)
        Description copied from interface: Eviction
        Evict 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:
        evictEventuallyBeforeInsertOnSegment in interface Eviction
      • evictEventually

        public void evictEventually()
        Description copied from interface: Eviction
        Evict if needed, checks all segments.
        Specified by:
        evictEventually in interface Eviction
      • evictIdleEntries

        public long evictIdleEntries​(int maxScan)
        Description copied from interface: Eviction
        Scan for idle (no access count since last scan) entries and evict them.
        Specified by:
        evictIdleEntries in interface Eviction
        Returns:
        number of evicted entries
        See Also:
        IdleScan
      • getMetrics

        public EvictionMetrics getMetrics()
        Description copied from interface: Eviction
        Get 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:
        getMetrics in interface Eviction
      • runLocked

        public <T> T runLocked​(Supplier<T> j)
        Description copied from interface: Eviction
        Runs job making sure concurrent evictions operations pause.
        Specified by:
        runLocked in interface Eviction
      • 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:
        changeCapacity in interface Eviction
      • removeAll

        public final long removeAll()
        Description copied from interface: Eviction
        Remove all entries from the eviction data structure.
        Specified by:
        removeAll in interface Eviction
        Returns:
        entry count
      • 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 to removeFromReplacementList(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()