Skip to content

Commit

Permalink
Misc minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
henningandersen committed Jan 24, 2024
1 parent 5d406c4 commit 2e7ac84
Showing 1 changed file with 13 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -750,9 +750,9 @@ private static void throwAlreadyEvicted() {
* @return true if successful, i.e., not evicted and data available, false if evicted
*/
boolean tryRead(ByteBuffer buf, long offset) throws IOException {
SharedBytes.IO io = this.io;
if (io != null) {
int readBytes = io.read(buf, getRegionRelativePosition(offset));
SharedBytes.IO ioRef = this.io;
if (ioRef != null) {
int readBytes = ioRef.read(buf, getRegionRelativePosition(offset));
if (isEvicted()) {
buf.position(buf.position() - readBytes);
return false;
Expand Down Expand Up @@ -1086,6 +1086,8 @@ private RangeAvailableHandler readerWithOffset(RangeAvailableHandler reader, Cac
}

private boolean assertValidRegionAndLength(CacheFileRegion fileRegion, int channelPos, int len) {
assert fileRegion.io != null;
assert fileRegion.hasReferences();
assert regionOwners.get(fileRegion.io) == fileRegion;
assert channelPos >= 0 && channelPos + len <= regionSize;
return true;
Expand Down Expand Up @@ -1388,25 +1390,12 @@ private void unlink(final LFUCacheEntry entry) {

/**
* Cycles through the {@link LFUCacheEntry} from 0 to max frequency and
* tries to evict a chunk if no one is holding onto its resources anymore
* tries to evict a chunk if no one is holding onto its resources anymore.
*
* Also regularly polls for free regions and thus might steal one in case any become available.
*
* @return the frequency of the evicted entry as integer or -1 if no entry was evicted from cache
* @return a now free IO region or null if none available.
*/
private int maybeEvict() {
assert Thread.holdsLock(SharedBlobCacheService.this);
for (int currentFreq = 0; currentFreq < maxFreq; currentFreq++) {
for (LFUCacheEntry entry = freqs[currentFreq]; entry != null; entry = entry.next) {
boolean evicted = entry.chunk.tryEvict();
if (evicted && entry.chunk.io != null) {
unlink(entry);
keyMapping.remove(entry.chunk.regionKey, entry);
return currentFreq;
}
}
}
return -1;
}

private SharedBytes.IO maybeEvictAndTake(Runnable evictedNotification) {
assert Thread.holdsLock(SharedBlobCacheService.this);
for (int currentFreq = 0; currentFreq < maxFreq; currentFreq++) {
Expand All @@ -1419,14 +1408,14 @@ private SharedBytes.IO maybeEvictAndTake(Runnable evictedNotification) {
boolean evicted = entry.chunk.tryEvictNoDecRef();
if (evicted) {
try {
if (entry.chunk.io != null) {
SharedBytes.IO ioRef = entry.chunk.io;
if (ioRef != null) {
try {
if (entry.chunk.refCount() == 1) {
// grab io, rely on incref'ers also checking evicted field.
final SharedBytes.IO result = entry.chunk.io;
entry.chunk.io = null;
assert regionOwners.remove(result) == entry.chunk;
return result;
assert regionOwners.remove(ioRef) == entry.chunk;
return ioRef;
}
} finally {
unlink(entry);
Expand Down

0 comments on commit 2e7ac84

Please sign in to comment.