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


##########
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:
   Yes, the `capacity` usually not changes, However, `available` may change for 
other reasons, such as multiple volumes sharing a physical disk, or other data 
being written to the disk. In this case, the actual `available` may differ from 
the value calculated by `increment/decrementUsedSpace`.
   
   However, if there is a lot of disk space left, I think this “difference” is 
acceptable, but when there is less disk space left, is there something that can 
be done to reduce the possible "difference"? Like an update thread or a 
fallback to get the `filesystem.available` every time instead of getting it 
from the `cache`.
   
   But it's a more delicate operation, not a must, what do you think?



-- 
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