xichen01 commented on code in PR #6383:
URL: https://github.com/apache/ozone/pull/6383#discussion_r1601349923
##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/fs/CachingSpaceUsageSource.java:
##########
@@ -64,6 +68,7 @@ public CachingSpaceUsageSource(SpaceUsageCheckParams params) {
refresh = params.getRefresh();
source = params.getSource();
+ lock = new AutoCloseableReadWriteLock(source.toString());
Review Comment:
`source.getPath()` seems to be a better choice, `toString` is easy to
modify, but some subclass do not Implement `getPath`
##########
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:
Is the default value `1h` too long after we cache the `capacity` and
`available`. In some scenarios, such as near-full capacity or sharing disk for
multi volume. May make a relatively large difference with real `capacity` and
cached `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]