Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class CachingSpaceUsageSource implements SpaceUsageSource {
private long cachedUsedSpace;
private long cachedAvailable;
private long cachedCapacity;
private SpaceUsageSource cachedUsage;
private final Duration refresh;
private final SpaceUsageSource source;
private final SpaceUsagePersistence persistence;
Expand Down Expand Up @@ -104,7 +105,15 @@ public long getUsedSpace() {
@Override
public SpaceUsageSource snapshot() {
try (AutoCloseableLock ignored = lock.readLock(null, null)) {
return new Fixed(cachedCapacity, cachedAvailable, cachedUsedSpace);
if (cachedUsage != null) {
return cachedUsage;
}
}
try (AutoCloseableLock ignored = lock.writeLock(null, null)) {
if (cachedUsage == null) {
cachedUsage = new Fixed(cachedCapacity, cachedAvailable, cachedUsedSpace);
}
return cachedUsage;
}
}

Expand All @@ -119,6 +128,7 @@ public void incrementUsedSpace(long usedSpace) {
change = Math.min(current, usedSpace);
cachedAvailable -= change;
cachedUsedSpace += change;
cachedUsage = null;
}

if (change != usedSpace) {
Expand All @@ -138,6 +148,7 @@ public void decrementUsedSpace(long reclaimedSpace) {
change = Math.min(current, reclaimedSpace);
cachedUsedSpace -= change;
cachedAvailable += change;
cachedUsage = null;
}

if (change != reclaimedSpace) {
Expand Down Expand Up @@ -202,6 +213,7 @@ private void updateAvailable() {
try (AutoCloseableLock ignored = lock.writeLock(null, null)) {
cachedAvailable = available;
cachedCapacity = capacity;
cachedUsage = null;
}
}

Expand All @@ -215,6 +227,7 @@ private void updateCachedValues(long used) {
cachedAvailable = available;
cachedCapacity = capacity;
cachedUsedSpace = used;
cachedUsage = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ void decrementUsedSpaceMoreThanCurrent() {
assertEquals(0, subject.getUsedSpace());
// available and used change by same amount (in opposite directions)
assertEquals(original.getAvailable() + original.getUsedSpace(), subject.getAvailable());
assertSnapshotIsUpToDate(subject);
}

@Test
Expand All @@ -179,6 +180,14 @@ void decrementAvailableSpaceMoreThanCurrent() {
assertEquals(0, subject.getAvailable());
// available and used change by same amount (in opposite directions)
assertEquals(original.getUsedSpace() + original.getAvailable(), subject.getUsedSpace());
assertSnapshotIsUpToDate(subject);
}

private static void assertSnapshotIsUpToDate(SpaceUsageSource subject) {
SpaceUsageSource snapshot = subject.snapshot();
assertEquals(subject.getCapacity(), snapshot.getCapacity());
assertEquals(subject.getAvailable(), snapshot.getAvailable());
assertEquals(subject.getUsedSpace(), snapshot.getUsedSpace());
}

private static long missingInitialValue() {
Expand Down Expand Up @@ -238,6 +247,7 @@ private static void assertAvailableWasUpdated(SpaceUsageSource source,

assertEquals(source.getCapacity(), subject.getCapacity());
assertEquals(source.getAvailable(), subject.getAvailable());
assertSnapshotIsUpToDate(subject);
}

private static void assertSubjectWasRefreshed(SpaceUsageSource source,
Expand Down
Loading