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 @@ -120,7 +120,11 @@ public void incrementUsedSpace(long usedSpace) {
if (usedSpace == 0) {
return;
}
Preconditions.assertTrue(usedSpace > 0, () -> usedSpace + " < 0");
if (usedSpace < 0) {
Comment thread
errose28 marked this conversation as resolved.
LOG.warn("Ignoring negative incrementUsedSpace({}) for {}", usedSpace, source);
return;
}

final long current, change;
try (AutoCloseableLock ignored = lock.writeLock(null, null)) {
current = cachedAvailable;
Expand All @@ -140,7 +144,11 @@ public void decrementUsedSpace(long reclaimedSpace) {
if (reclaimedSpace == 0) {
return;
}
Preconditions.assertTrue(reclaimedSpace > 0, () -> reclaimedSpace + " < 0");
if (reclaimedSpace < 0) {
LOG.warn("Ignoring negative reclaimedSpace({}) for {}", reclaimedSpace, source);
return;
}

final long current, change;
try (AutoCloseableLock ignored = lock.writeLock(null, null)) {
current = cachedUsedSpace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,41 @@ void decrementAvailableSpaceMoreThanCurrent() {
assertSnapshotIsUpToDate(subject);
}

// If negative values are passed, check that the state is unchanged
@Test
void incrementUsedSpaceIgnoresNegativeValue() {
SpaceUsageCheckParams params = paramsBuilder(new AtomicLong(50))
.withRefresh(Duration.ZERO)
.build();
CachingSpaceUsageSource subject = new CachingSpaceUsageSource(params);
SpaceUsageSource original = subject.snapshot();

subject.incrementUsedSpace(-1L);

// negative value should be ignored, leaving cached state unchanged
assertEquals(original.getUsedSpace(), subject.getUsedSpace());
assertEquals(original.getAvailable(), subject.getAvailable());
assertEquals(original.getCapacity(), subject.getCapacity());
assertSnapshotIsUpToDate(subject);
}

@Test
void decrementUsedSpaceIgnoresNegativeValue() {
SpaceUsageCheckParams params = paramsBuilder(new AtomicLong(50))
.withRefresh(Duration.ZERO)
.build();
CachingSpaceUsageSource subject = new CachingSpaceUsageSource(params);
SpaceUsageSource original = subject.snapshot();

subject.decrementUsedSpace(-1L);

// negative value should be ignored, leaving cached state unchanged
assertEquals(original.getUsedSpace(), subject.getUsedSpace());
assertEquals(original.getAvailable(), subject.getAvailable());
assertEquals(original.getCapacity(), subject.getCapacity());
assertSnapshotIsUpToDate(subject);
}

private static void assertSnapshotIsUpToDate(SpaceUsageSource subject) {
SpaceUsageSource snapshot = subject.snapshot();
assertEquals(subject.getCapacity(), snapshot.getCapacity());
Expand Down