Skip to content

Commit b3f4a64

Browse files
authored
HDDS-15112. Handle negative space usage values gracefully in CachingSpaceUsageSource (#10130)
1 parent 659fc31 commit b3f4a64

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/fs/CachingSpaceUsageSource.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ public void incrementUsedSpace(long usedSpace) {
120120
if (usedSpace == 0) {
121121
return;
122122
}
123-
Preconditions.assertTrue(usedSpace > 0, () -> usedSpace + " < 0");
123+
if (usedSpace < 0) {
124+
LOG.warn("Ignoring negative incrementUsedSpace({}) for {}", usedSpace, source);
125+
return;
126+
}
127+
124128
final long current, change;
125129
try (AutoCloseableLock ignored = lock.writeLock(null, null)) {
126130
current = cachedAvailable;
@@ -140,7 +144,11 @@ public void decrementUsedSpace(long reclaimedSpace) {
140144
if (reclaimedSpace == 0) {
141145
return;
142146
}
143-
Preconditions.assertTrue(reclaimedSpace > 0, () -> reclaimedSpace + " < 0");
147+
if (reclaimedSpace < 0) {
148+
LOG.warn("Ignoring negative reclaimedSpace({}) for {}", reclaimedSpace, source);
149+
return;
150+
}
151+
144152
final long current, change;
145153
try (AutoCloseableLock ignored = lock.writeLock(null, null)) {
146154
current = cachedUsedSpace;

hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/fs/TestCachingSpaceUsageSource.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,41 @@ void decrementAvailableSpaceMoreThanCurrent() {
182182
assertSnapshotIsUpToDate(subject);
183183
}
184184

185+
// If negative values are passed, check that the state is unchanged
186+
@Test
187+
void incrementUsedSpaceIgnoresNegativeValue() {
188+
SpaceUsageCheckParams params = paramsBuilder(new AtomicLong(50))
189+
.withRefresh(Duration.ZERO)
190+
.build();
191+
CachingSpaceUsageSource subject = new CachingSpaceUsageSource(params);
192+
SpaceUsageSource original = subject.snapshot();
193+
194+
subject.incrementUsedSpace(-1L);
195+
196+
// negative value should be ignored, leaving cached state unchanged
197+
assertEquals(original.getUsedSpace(), subject.getUsedSpace());
198+
assertEquals(original.getAvailable(), subject.getAvailable());
199+
assertEquals(original.getCapacity(), subject.getCapacity());
200+
assertSnapshotIsUpToDate(subject);
201+
}
202+
203+
@Test
204+
void decrementUsedSpaceIgnoresNegativeValue() {
205+
SpaceUsageCheckParams params = paramsBuilder(new AtomicLong(50))
206+
.withRefresh(Duration.ZERO)
207+
.build();
208+
CachingSpaceUsageSource subject = new CachingSpaceUsageSource(params);
209+
SpaceUsageSource original = subject.snapshot();
210+
211+
subject.decrementUsedSpace(-1L);
212+
213+
// negative value should be ignored, leaving cached state unchanged
214+
assertEquals(original.getUsedSpace(), subject.getUsedSpace());
215+
assertEquals(original.getAvailable(), subject.getAvailable());
216+
assertEquals(original.getCapacity(), subject.getCapacity());
217+
assertSnapshotIsUpToDate(subject);
218+
}
219+
185220
private static void assertSnapshotIsUpToDate(SpaceUsageSource subject) {
186221
SpaceUsageSource snapshot = subject.snapshot();
187222
assertEquals(subject.getCapacity(), snapshot.getCapacity());

0 commit comments

Comments
 (0)