diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotCache.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotCache.java index 790150e218a..40a92b1ae0e 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotCache.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/SnapshotCache.java @@ -227,6 +227,7 @@ public UncheckedAutoCloseableSupplier get(UUID key) throws IOExcepti LOG.info("Loading SnapshotId: '{}'", k); try { v = new ReferenceCounted<>(cacheLoader.load(key), false, this); + omMetrics.incNumSnapshotCacheSize(); } catch (OMException omEx) { // Return null if the snapshot is no longer active if (!omEx.getResult().equals(OMException.ResultCodes.FILE_NOT_FOUND)) { @@ -239,7 +240,6 @@ public UncheckedAutoCloseableSupplier get(UUID key) throws IOExcepti // Unexpected and unknown exception thrown from CacheLoader#load throw new IllegalStateException(ex); } - omMetrics.incNumSnapshotCacheSize(); } if (v != null) { // When RC OmSnapshot is successfully loaded diff --git a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotCache.java b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotCache.java index 2c789729c06..cd0f45324d9 100644 --- a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotCache.java +++ b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestSnapshotCache.java @@ -170,6 +170,18 @@ public void testGetReleasesReadLockOnLoadFailure() throws Exception { verify(lock, times(1)).releaseReadLock(eq(SNAPSHOT_DB_LOCK), eq(dbKey.toString())); } + @Test + @DisplayName("get() of an inactive snapshot does not count towards cache size") + void testGetInactiveSnapshotDoesNotIncrementCacheSize() throws Exception { + final UUID dbKey = UUID.randomUUID(); + when(cacheLoader.load(eq(dbKey))) + .thenThrow(new OMException("Snapshot is no longer active", OMException.ResultCodes.FILE_NOT_FOUND)); + OMException ex = assertThrows(OMException.class, () -> snapshotCache.get(dbKey)); + assertEquals(OMException.ResultCodes.FILE_NOT_FOUND, ex.getResult()); + assertEquals(0, snapshotCache.size()); + assertEquals(0, omMetrics.getNumSnapshotCacheSize()); + } + @ParameterizedTest @ValueSource(ints = {0, 1, 5, 10}) @DisplayName("Tests get() holds a read lock")