GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — [refactor][ml] Replace cache eviction algorithm with centralized removal queue and job by lhotari · Pull Request #24363 · apache/pulsar Conversation
|
| @Override | |
| public void doEviction(List<EntryCache> caches, long sizeToFree) { | |
| checkArgument(sizeToFree > 0); | |
| checkArgument(!caches.isEmpty()); | |
| caches.sort(reverseOrder()); | |
| long totalSize = 0; | |
| for (EntryCache cache : caches) { | |
| totalSize += cache.getSize(); | |
| } | |
| // This algorithm apply the eviction only the group of caches whose combined size reaches the | |
| // PercentOfSizeToConsiderForEviction | |
| List<EntryCache> cachesToEvict = new ArrayList(); | |
| long cachesToEvictTotalSize = 0; | |
| long sizeToConsiderForEviction = (long) (totalSize * PercentOfSizeToConsiderForEviction); | |
| log.debug("Need to gather at least {} from caches", sizeToConsiderForEviction); | |
| int cacheIdx = 0; | |
| while (cachesToEvictTotalSize < sizeToConsiderForEviction) { | |
| // This condition should always be true, considering that we cannot free more size that what we have in | |
| // cache | |
| checkArgument(cacheIdx < caches.size()); | |
| EntryCache entryCache = caches.get(cacheIdx++); | |
| cachesToEvictTotalSize += entryCache.getSize(); | |
| cachesToEvict.add(entryCache); | |
| log.debug("Added cache {} with size {}", entryCache.getName(), entryCache.getSize()); | |
| } | |
| int evictedEntries = 0; | |
| long evictedSize = 0; | |
| for (EntryCache entryCache : cachesToEvict) { | |
| // To each entryCache chosen to for eviction, we'll ask to evict a proportional amount of data | |
| long singleCacheSizeToFree = (long) (sizeToFree * (entryCache.getSize() / (double) cachesToEvictTotalSize)); | |
| if (singleCacheSizeToFree == 0) { | |
| // If the size of this cache went to 0, it probably means that its entries has been removed from the | |
| // cache since the time we've computed the ranking | |
| continue; | |
| } | |
| Pair<Integer, Long> evicted = entryCache.evictEntries(singleCacheSizeToFree); | |
| evictedEntries += evicted.getLeft(); | |
| evictedSize += evicted.getRight(); | |
| } | |
| log.info("Completed cache eviction. Removed {} entries from {} caches. ({} Mb)", evictedEntries, | |
| cachesToEvict.size(), evictedSize / RangeEntryCacheManagerImpl.MB); | |
| } |
Timestamp eviction
managedLedgerCacheEvictionIntervalMs=10)pulsar/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java
Lines 306 to 317 in eccc6b6
| private synchronized void doCacheEviction() { | |
| long maxTimestamp = System.nanoTime() - cacheEvictionTimeThresholdNanos; | |
| ledgers.values().forEach(mlfuture -> { | |
| if (mlfuture.isDone() && !mlfuture.isCompletedExceptionally()) { | |
| ManagedLedgerImpl ml = mlfuture.getNow(null); | |
| if (ml != null) { | |
| ml.doCacheEviction(maxTimestamp); | |
| } | |
| } | |
| }); | |
| } |
Note: There's a single shared removal queue for all ManagedLedgerImpl instances instead of having to do the check in multiple instances.
This change is already covered by existing tests:
RangeCacheTest validates the new removal queue functionalityEntryCacheManagerTest verifies eviction behavior remains correctdoc doc-required doc-not-needed doc-complete
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@ ## master #24363 +/- ## ============================================ + Coverage 73.57% 74.36% +0.78% - Complexity 32624 32651 +27 ============================================ Files 1877 1878 +1 Lines 139502 146301 +6799 Branches 15299 16772 +1473 ============================================ + Hits 102638 108796 +6158 + Misses 28908 28883 -25 - Partials 7956 8622 +666
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
b72bc4f to
9dd5726
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
My only question is about the eviction from the stash queue: since the insertion might not be in order, when there is memory pressure, how is the eviction going to happen?
Or, is the size of this stash already controlled in size such that it would only require time-based expiration?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
The merge conflicts will be resolved by first merging #24552 to master branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
For the implementation in this PR all entries are added in order to the removal queue and the timestamp is set when the entry gets added to the cache and the removal queue. Therefore, the timestamps are in order. The eviction will become more complicated later. In PIP-430, there's a need to put entries aside and keep them in the cache for a longer period of time. Currently that is addressed in PIP-430 by having 2 separate settings for expiration and that will simplify the solution. The maximum TTL for cache entries will continue to be in order in the removal queue, but another datastructure is needed when size based eviction would remove the entry, but it's prioritized to be skipped and kept until the entry expires. Handling that challenge is out-of-scope for this PR. After this current PR has been merged, it will be possible to build upon this and expand the solution further to implement PIP-430 (I have a WIP PR in my own fork, but it's not matching the PIP high-level design). One more detail about eviction handling that is present in this PR. The central queue is used for eviction by size (to keep all cached items under the limit) or by TTL. When entries are evicted directly from the RangeCache (for a complete ledger or for up to the markdelete position), the entry wrapper held in the range cache will be cleared and the same instance will remain in the queue until it gets processed. Empty entry wrappers will be held in the queue at most for the TTL in worst case. The entry wrapper gets recycled when it gets processed from the I hope this answer covers your question @merlimat |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
… and replace with ReferenceCountedEntry - ReferenceCountedEntry is already implemented by EntryImpl - Separate interface to avoid changing and breaking existing Entry interface
…ction-optimization
|
PIP-430 vote has passed. I'll merge this PR to master. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…val queue and job (apache#24363)
…val queue and job (apache#24363)
…val queue and job (apache#24363)
…val queue and job (apache#24363) (cherry picked from commit 8ce67b9) (cherry picked from commit 0128390)
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.