swamirishi commented on code in PR #8474: URL: https://github.com/apache/ozone/pull/8474#discussion_r2126315463
########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotCache.java: ########## @@ -215,14 +229,53 @@ public void release(UUID key) { val.decrementRefCount(); } + /** + * Acquires a write lock on the snapshot database and returns an auto-closeable supplier + * for lock details. The lock ensures that the operations accessing the snapshot database + * are performed in a thread-safe manner. The returned supplier automatically releases the + * lock when closed, preventing potential resource contention or deadlocks. + */ + public UncheckedAutoCloseableSupplier<OMLockDetails> lock() { + return lock(() -> lock.acquireResourceWriteLock(SNAPSHOT_DB_LOCK), + () -> lock.releaseResourceWriteLock(SNAPSHOT_DB_LOCK)); + } + + private UncheckedAutoCloseableSupplier<OMLockDetails> lock( + Supplier<OMLockDetails> lockFunction, Supplier<OMLockDetails> unlockFunction) { + AtomicReference<OMLockDetails> lockDetails = new AtomicReference<>(lockFunction.get()); + if (lockDetails.get().isLockAcquired()) { + cleanup(true); + if (!dbMap.isEmpty()) { + lockDetails.set(unlockFunction.get()); + } + } + + return new UncheckedAutoCloseableSupplier<OMLockDetails>() { + + @Override + public void close() { + lockDetails.updateAndGet((prevLock) -> { + if (prevLock != null && prevLock.isLockAcquired()) { + return unlockFunction.get(); + } + return prevLock; + }); + } + + @Override + public OMLockDetails get() { + return lockDetails.get(); + } + }; + } /** * If cache size exceeds soft limit, attempt to clean up and close the instances that has zero reference count. */ @VisibleForTesting Review Comment: There are already testcases for testing snapshot eviction -- 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: issues-unsubscr...@ozone.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org For additional commands, e-mail: issues-h...@ozone.apache.org