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 @@ -94,7 +94,19 @@ public void incrementUsedSpace(long usedSpace) {
}

public void decrementUsedSpace(long reclaimedSpace) {
cachedValue.addAndGet(-1 * reclaimedSpace);
cachedValue.updateAndGet(current -> {
long newValue = current - reclaimedSpace;
if (newValue < 0) {
if (current > 0) {
LOG.warn("Attempted to decrement used space to a negative value. " +
"Current: {}, Decrement: {}, Source: {}",
current, reclaimedSpace, source);
}
return 0;
} else {
return newValue;
}
});
}

public void start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ public void savesValueOnShutdown() {
verify(executor).shutdown();
}

@Test
public void testDecrementDoesNotGoNegative() {
SpaceUsageCheckParams params = paramsBuilder(new AtomicLong(50))
.withRefresh(Duration.ZERO)
.build();
CachingSpaceUsageSource subject = new CachingSpaceUsageSource(params);

// Try to decrement more than the current value
subject.decrementUsedSpace(100);

// Check that the value has been set to 0
assertEquals(0, subject.getUsedSpace());
}

private static long missingInitialValue() {
return 0L;
}
Expand Down