-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Reduce overhead in blob cache service get #96399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
henningandersen
merged 4 commits into
elastic:main
from
henningandersen:fix_blob_cache_get_locking
May 31, 2023
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 96399 | ||
summary: Reduce overhead in blob cache service get | ||
area: Snapshot/Restore | ||
type: enhancement | ||
issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,6 @@ | |
import org.elasticsearch.common.unit.RelativeByteSizeValue; | ||
import org.elasticsearch.common.util.concurrent.AbstractAsyncTask; | ||
import org.elasticsearch.common.util.concurrent.AbstractRunnable; | ||
import org.elasticsearch.common.util.concurrent.KeyedLock; | ||
import org.elasticsearch.core.AbstractRefCounted; | ||
import org.elasticsearch.core.Assertions; | ||
import org.elasticsearch.core.Releasable; | ||
|
@@ -242,8 +241,6 @@ public void validate(ByteSizeValue value, Map<Setting<?>, Object> settings, bool | |
private final ConcurrentHashMap<RegionKey<KeyType>, Entry<CacheFileRegion>> keyMapping; | ||
private final ThreadPool threadPool; | ||
|
||
private final KeyedLock<KeyType> keyedLock = new KeyedLock<>(); | ||
|
||
private final SharedBytes sharedBytes; | ||
private final long cacheSize; | ||
private final long regionSize; | ||
|
@@ -380,57 +377,76 @@ private long getRegionSize(long fileLength, int region) { | |
|
||
public CacheFileRegion get(KeyType cacheKey, long fileLength, int region) { | ||
final long effectiveRegionSize = getRegionSize(fileLength, region); | ||
try (Releasable ignore = keyedLock.acquire(cacheKey)) { | ||
final RegionKey<KeyType> regionKey = new RegionKey<>(cacheKey, region); | ||
final long now = threadPool.relativeTimeInMillis(); | ||
final Entry<CacheFileRegion> entry = keyMapping.computeIfAbsent( | ||
regionKey, | ||
key -> new Entry<>(new CacheFileRegion(key, effectiveRegionSize), now) | ||
); | ||
if (entry.chunk.sharedBytesPos == -1) { | ||
// new item | ||
assert entry.freq == 0; | ||
assert entry.prev == null; | ||
assert entry.next == null; | ||
final Integer freeSlot = freeRegions.poll(); | ||
if (freeSlot != null) { | ||
// no need to evict an item, just add | ||
entry.chunk.sharedBytesPos = freeSlot; | ||
assert regionOwners[freeSlot].compareAndSet(null, entry.chunk); | ||
synchronized (this) { | ||
pushEntryToBack(entry); | ||
} | ||
} else { | ||
// need to evict something | ||
synchronized (this) { | ||
maybeEvict(); | ||
final RegionKey<KeyType> regionKey = new RegionKey<>(cacheKey, region); | ||
final long now = threadPool.relativeTimeInMillis(); | ||
final Entry<CacheFileRegion> entry = keyMapping.computeIfAbsent( | ||
regionKey, | ||
key -> new Entry<>(new CacheFileRegion(key, effectiveRegionSize), now) | ||
); | ||
// sharedBytesPos is volatile, double locking is fine, as long as we assign it last. | ||
if (entry.chunk.sharedBytesPos == -1) { | ||
synchronized (entry.chunk) { | ||
if (entry.chunk.sharedBytesPos == -1) { | ||
if (keyMapping.get(regionKey) != entry) { | ||
throw new AlreadyClosedException("no free region found (contender)"); | ||
} | ||
final Integer freeSlotRetry = freeRegions.poll(); | ||
if (freeSlotRetry != null) { | ||
entry.chunk.sharedBytesPos = freeSlotRetry; | ||
assert regionOwners[freeSlotRetry].compareAndSet(null, entry.chunk); | ||
// new item | ||
assert entry.freq == 0; | ||
assert entry.prev == null; | ||
assert entry.next == null; | ||
final Integer freeSlot = freeRegions.poll(); | ||
if (freeSlot != null) { | ||
// no need to evict an item, just add | ||
assert regionOwners[freeSlot].compareAndSet(null, entry.chunk); | ||
synchronized (this) { | ||
pushEntryToBack(entry); | ||
// assign sharedBytesPos only when chunk is ready for use. Under lock to avoid concurrent tryEvict. | ||
entry.chunk.sharedBytesPos = freeSlot; | ||
} | ||
} else { | ||
boolean removed = keyMapping.remove(regionKey, entry); | ||
assert removed; | ||
throw new AlreadyClosedException("no free region found"); | ||
} | ||
} | ||
} else { | ||
// check if we need to promote item | ||
synchronized (this) { | ||
if (now - entry.lastAccessed >= minTimeDelta && entry.freq + 1 < maxFreq) { | ||
unlink(entry); | ||
entry.freq++; | ||
entry.lastAccessed = now; | ||
pushEntryToBack(entry); | ||
// need to evict something | ||
synchronized (this) { | ||
maybeEvict(); | ||
} | ||
final Integer freeSlotRetry = freeRegions.poll(); | ||
if (freeSlotRetry != null) { | ||
assert regionOwners[freeSlotRetry].compareAndSet(null, entry.chunk); | ||
synchronized (this) { | ||
pushEntryToBack(entry); | ||
// assign sharedBytesPos only when chunk is ready for use. Under lock to avoid concurrent tryEvict. | ||
entry.chunk.sharedBytesPos = freeSlotRetry; | ||
} | ||
} else { | ||
boolean removed = keyMapping.remove(regionKey, entry); | ||
assert removed; | ||
throw new AlreadyClosedException("no free region found"); | ||
} | ||
} | ||
|
||
return entry.chunk; | ||
} | ||
} | ||
return entry.chunk; | ||
} | ||
if (Assertions.ENABLED) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: maybe move this kind of assertion to a separate method (that can just include the next assertion below) to save a little on method size here? (probably not too relevant but also makes the method easier to read IMO) |
||
synchronized (this) { | ||
// assert linked (or evicted) | ||
assert entry.prev != null || entry.chunk.isEvicted(); | ||
|
||
} | ||
} | ||
assert regionOwners[entry.chunk.sharedBytesPos].get() == entry.chunk || entry.chunk.isEvicted(); | ||
|
||
// existing item, check if we need to promote item | ||
synchronized (this) { | ||
if (now - entry.lastAccessed >= minTimeDelta && entry.freq + 1 < maxFreq) { | ||
unlink(entry); | ||
entry.freq++; | ||
entry.lastAccessed = now; | ||
pushEntryToBack(entry); | ||
} | ||
} | ||
|
||
return entry.chunk; | ||
volodk85 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public void onClose(CacheFileRegion chunk) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also assign -2 to
sharedBytesPos
in this case instead. I think it does not really matter, we expect near no contention here.