jojochuang commented on code in PR #8473:
URL: https://github.com/apache/ozone/pull/8473#discussion_r2099007180


##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotCache.java:
##########
@@ -181,7 +183,23 @@ public ReferenceCounted<OmSnapshot> get(UUID key) throws 
IOException {
       throw new OMException("SnapshotId: '" + key + "' not found, or the 
snapshot is no longer active.",
           OMException.ResultCodes.FILE_NOT_FOUND);
     }
-    return rcOmSnapshot;
+    return new UncheckedAutoCloseableSupplier<OmSnapshot>() {

Review Comment:
   The primary change in the SnapshotCache class is that it no longer exposes 
the internal reference count mechanism to its callers. Here's what that means 
and why it's beneficial:
   
   Encapsulation of Resource Management
   Previously, the SnapshotCache relied on a ReferenceCounted class that 
allowed callers to see and, potentially, manipulate the reference count. This 
design exposed internal lifecycle details such as how many users were currently 
holding a reference to a snapshot. If a caller misused this information (for 
example, by incrementing or decrementing the count inappropriately), it could 
lead to serious errors such as premature resource cleanup or resource leaks.
   With the refactor, SnapshotCache now wraps its resource with an 
AutoCloseSupplier. This abstraction hides the reference count management 
completely so that callers only interact with a simple, high-level API for 
obtaining and closing the snapshot.
   
   Simplified and Safer API
   By returning an AutoCloseSupplier instead of a ReferenceCounted instance, 
the API now:
   
   Prevents the caller from accidentally interfering with the resource 
lifecycle.
   Automates resource cleanup under an explicit close mechanism (such as in a 
try-with-resources block), reducing the risk of human error.
   Promotes clarity by letting the SnapshotCache manage its internal resource 
state transparently.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotCache.java:
##########
@@ -181,7 +183,23 @@ public ReferenceCounted<OmSnapshot> get(UUID key) throws 
IOException {
       throw new OMException("SnapshotId: '" + key + "' not found, or the 
snapshot is no longer active.",
           OMException.ResultCodes.FILE_NOT_FOUND);
     }
-    return rcOmSnapshot;
+    return new UncheckedAutoCloseableSupplier<OmSnapshot>() {
+      private AtomicReference<Boolean> closed = new AtomicReference<>(false);
+      @Override
+      public OmSnapshot get() {
+        return rcOmSnapshot.get();
+      }
+
+      @Override
+      public void close() {

Review Comment:
   The close() method safely decrements the reference count of the cached 
OmSnapshot, ensuring this happens at most once, even if close() is called 
multiple times. This helps manage resources and prevents accidental double 
release.



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