Skip to content
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

HDDS-10614. Avoid decreasing cached space usage below zero #6508

Merged
merged 13 commits into from
Apr 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,17 @@ public void incrementUsedSpace(long usedSpace) {
}

public void decrementUsedSpace(long reclaimedSpace) {
cachedValue.addAndGet(-1 * reclaimedSpace);
cachedValue.updateAndGet(current -> {
long newValue = current - reclaimedSpace;
if (newValue < 0) {
LOG.warn(
"Attempted to decrement used space to a negative value. Current: {}, Decrement: {}",
current, reclaimedSpace);
ArafatKhan2198 marked this conversation as resolved.
Show resolved Hide resolved
return 0; // Reset to zero
} 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