Skip to content

Commit

Permalink
HDDS-10614. Avoid decreasing cached space usage below zero (#6508)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArafatKhan2198 authored Apr 18, 2024
1 parent 99a5703 commit cc023e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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

0 comments on commit cc023e7

Please sign in to comment.