smiklosovic commented on code in PR #4514:
URL: https://github.com/apache/cassandra/pull/4514#discussion_r2590414988


##########
src/java/org/apache/cassandra/db/compression/ZstdCompressionDictionary.java:
##########
@@ -181,30 +195,48 @@ public Ref<ZstdCompressionDictionary> ref()
         return selfRef.ref();
     }
 
-    @Override
-    public void close()
+    private void ensureNotReleased()
     {
-        if (closed.compareAndSet(false, true))
-        {
-            selfRef.release();
-        }
+        if (selfRef.globalCount() <= 0)
+            throw new IllegalStateException("Dictionary has been released: " + 
dictId);
     }
 
+    /**
+     * Tidy implementation for cleaning up native Zstd resources.
+     *
+     * This class holds direct references to the resources that need cleanup,
+     * avoiding a circular reference pattern where Tidy would hold a reference
+     * to the parent dictionary object.
+     */
     private static class Tidy implements RefCounted.Tidy
     {
         private final ConcurrentHashMap<Integer, ZstdDictCompress> 
zstdDictCompressPerLevel;
-        private volatile ZstdDictDecompress dictDecompress;
+        private final AtomicReference<ZstdDictDecompress> dictDecompress;
 
-        Tidy(ConcurrentHashMap<Integer, ZstdDictCompress> 
zstdDictCompressPerLevel, ZstdDictDecompress dictDecompress)
+        Tidy(ConcurrentHashMap<Integer, ZstdDictCompress> 
zstdDictCompressPerLevel,
+             AtomicReference<ZstdDictDecompress> dictDecompress)
         {
             this.zstdDictCompressPerLevel = zstdDictCompressPerLevel;
             this.dictDecompress = dictDecompress;
         }
 
+        /**
+         * Clean up native resources when reference count reaches zero.
+         *
+         * IMPORTANT: This method is called exactly once when the last 
reference is released.
+         * Reference counting guarantees that no other thread can be executing
+         * dictionaryForCompression/Decompression when this runs, because:
+         * 1. Those methods require holding a valid reference
+         * 2. This only runs when refcount goes from 0 to -1

Review Comment:
   aha I guess it can



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