chihsuan commented on code in PR #11376: URL: https://github.com/apache/ozone/pull/11376#discussion_r4155004547
########## hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOmSnapshotLeakDetection.java: ########## @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.UUID; +import org.apache.hadoop.hdds.utils.db.DBStore; +import org.apache.hadoop.ozone.security.acl.IAccessAuthorizer; +import org.apache.ozone.test.GenericTestUtils.LogCapturer; +import org.junit.jupiter.api.Test; + +/** + * Test {@link OmSnapshot}'s leak detection: the snapshot registers with a shared LeakDetector + * and warns if it is garbage collected while its underlying store is still open. + */ +class TestOmSnapshotLeakDetection { + + /** The reporter warns only when the store was not closed. Exercised directly with a mocked store. */ + @Test + void reporterWarnsWhenStoreNotClosed() { + DBStore store = mock(DBStore.class); + when(store.isClosed()).thenReturn(false); + try (LogCapturer logs = LogCapturer.captureLogs(OmSnapshot.class)) { + OmSnapshot.newLeakReporter(store, "snap-1").run(); + assertThat(logs.getOutput()).contains("is not closed properly. snapshotName: snap-1"); + } + } + + @Test + void reporterSilentWhenStoreClosed() { + DBStore store = mock(DBStore.class); + when(store.isClosed()).thenReturn(true); + try (LogCapturer logs = LogCapturer.captureLogs(OmSnapshot.class)) { + OmSnapshot.newLeakReporter(store, "snap-1").run(); + assertThat(logs.getOutput()).doesNotContain("is not closed properly"); + } + } + + /** + * Drive an actual {@link OmSnapshot} instance through GC without closing it and verify the leak + * is detected. Collaborators (including the {@link DBStore}) are mocked, so no metadata store is + * opened; this exercises the constructor's LeakDetector registration and the GC-triggered report. + */ + @Test + void leakDetectedForUnclosedSnapshot() throws Exception { Review Comment: Should we also add a test for the closed case? Right now the tests still pass even without `leakTracker.close()`. ########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OmSnapshot.java: ########## @@ -345,19 +353,21 @@ public UUID getSnapshotID() { public void close() throws IOException { // Close DB omMetadataManager.getStore().close(); + // Closed properly: stop tracking so the leak reporter does not fire at GC. + leakTracker.close(); Review Comment: nit: Could we use `try`/`finally` here, like [`OzoneClient#close`](https://github.com/apache/ozone/blob/4a832912feae49664e05479d0ea9ba2c182e905e/hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/OzoneClient.java#L121-L127)? That way the tracker is still released if the store close throws. -- 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]
