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 @@ -25,6 +25,7 @@
import org.slf4j.LoggerFactory;

import jakarta.annotation.Nullable;

import java.time.Duration;
import java.util.OptionalLong;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -94,7 +95,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 @@ -31,6 +31,7 @@
import java.util.concurrent.atomic.AtomicLong;

import static org.apache.hadoop.hdds.fs.MockSpaceUsageCheckParams.newBuilder;
import static org.apache.ratis.util.Preconditions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyLong;
Expand Down Expand Up @@ -142,6 +143,24 @@ public void savesValueOnShutdown() {
verify(executor).shutdown();
}

@Test
public void testDecrementDoesNotGoNegative() {
CachingSpaceUsageSource subject;
SpaceUsageCheckParams params;
AtomicLong cachedValue;
ArafatKhan2198 marked this conversation as resolved.
Show resolved Hide resolved
cachedValue = new AtomicLong(50);
params = paramsBuilder(cachedValue)
.withRefresh(Duration.ZERO)
.build();
subject = new CachingSpaceUsageSource(params);
// Try to decrement more than the current value
subject.decrementUsedSpace(100);

// Check that the value has not gone negative and has been reset to 0
assertTrue(subject.getUsedSpace() >= 0,
ArafatKhan2198 marked this conversation as resolved.
Show resolved Hide resolved
"Cached used space should not be negative");
}

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