adoroszlai commented on code in PR #6383:
URL: https://github.com/apache/ozone/pull/6383#discussion_r1605787680


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/fs/CachingSpaceUsageSource.java:
##########
@@ -76,42 +81,65 @@ public CachingSpaceUsageSource(SpaceUsageCheckParams 
params) {
 
   @Override
   public long getCapacity() {
-    return source.getCapacity();
+    try (AutoCloseableLock ignored = lock.readLock(null, null)) {
+      return cachedCapacity;
+    }
   }
 
   @Override
   public long getAvailable() {
-    return source.getAvailable();
+    try (AutoCloseableLock ignored = lock.readLock(null, null)) {
+      return cachedAvailable;
+    }
   }
 
   @Override
   public long getUsedSpace() {
-    return cachedValue.get();
+    try (AutoCloseableLock ignored = lock.readLock(null, null)) {
+      return cachedUsedSpace;
+    }
+  }
+
+  @Override
+  public SpaceUsageSource snapshot() {
+    try (AutoCloseableLock ignored = lock.readLock(null, null)) {
+      return new Fixed(cachedCapacity, cachedAvailable, cachedUsedSpace);
+    }
   }
 
   public void incrementUsedSpace(long usedSpace) {
-    cachedValue.addAndGet(usedSpace);
+    final long current, change;
+    try (AutoCloseableLock ignored = lock.writeLock(null, null)) {
+      current = cachedAvailable;
+      change = Math.min(current, usedSpace);
+      cachedAvailable -= change;
+      cachedUsedSpace += change;
+    }
+
+    if (change != usedSpace) {
+      LOG.warn("Attempted to decrement available space to a negative value. 
Current: {}, Decrement: {}, Source: {}",
+          current, usedSpace, source);
+    }
   }
 
   public void decrementUsedSpace(long 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;
-      }
-    });
+    final long current, change;
+    try (AutoCloseableLock ignored = lock.writeLock(null, null)) {
+      current = cachedUsedSpace;
+      change = Math.min(current, reclaimedSpace);
+      cachedUsedSpace -= change;
+      cachedAvailable += change;
+    }
+
+    if (change != reclaimedSpace) {
+      LOG.warn("Attempted to decrement used space to a negative value. 
Current: {}, Decrement: {}, Source: {}",
+          current, reclaimedSpace, source);
+    }
   }
 
   public void start() {
     if (executor != null) {
-      long initialDelay = cachedValue.get() > 0 ? refresh.toMillis() : 0;
+      long initialDelay = getUsedSpace() > 0 ? refresh.toMillis() : 0;

Review Comment:
   > `available` may change for other reasons
   
   Added more frequent update (at most 1 minute delay) for `available` and 
`capacity`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to