-
Notifications
You must be signed in to change notification settings - Fork 593
HDDS-9528. Managed objects should not override finalize() #5853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
78994d9
HDDS-9528. Managed objects should not override finalize()
duongkame a5bace9
Fix test.
duongkame ba3c85b
Correct approach.
duongkame 6e6ca6c
Testing leak detection.
duongkame 3045642
Cleanup.
duongkame 65477d5
Cleanup.
duongkame e4c2500
Checksylte.
duongkame 96f1e2d
Use GC from CodecTestUtil.
duongkame d87f173
Unit test for LeakDetector.
duongkame ceaf506
checkstyle.
duongkame 0eb4031
Correct docs.
duongkame 79b0ea6
Fix typo and dangling javadoc
adoroszlai 333e6a1
Reduce code duplications.
duongkame 69bf9d5
Update startIndex.
duongkame d082bee
Merge remote-tracking branch 'origin/master' into HDDS-9528
adoroszlai 8e19927
Move LeakDetector to utils package.
duongkame 7c9ddf8
Move LeakDetector to utils package.
duongkame 263fb65
Avoid calling getSimpleName for each ManagedObject creation.
duongkame 63304d0
Remove package-info.
duongkame 5a98898
Merge remote-tracking branch 'origin/master' into HDDS-9528
adoroszlai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/resource/LeakDetector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /* | ||
| * 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.hdds.resource; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.lang.ref.ReferenceQueue; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| /** | ||
| * Simple general resource leak detector using {@link ReferenceQueue} and {@link java.lang.ref.WeakReference} to | ||
| * observe resource object life-cycle and assert proper resource closure before they are GCed. | ||
| * | ||
| * <p> | ||
| * Example usage: | ||
| * | ||
| * <pre> {@code | ||
| * class MyResource implements AutoClosable { | ||
| * static final LeakDetector LEAK_DETECTOR = new LeakDetector("MyResource"); | ||
| * | ||
| * private final LeakTracker leakTracker = LEAK_DETECTOR.track(this, () -> { | ||
| * // report leaks, don't refer to the original object (MyResource) here. | ||
| * System.out.println("MyResource is not closed before being discarded."); | ||
| * }); | ||
| * | ||
| * @Override | ||
| * public void close() { | ||
| * // proper resources cleanup... | ||
| * // inform tracker that this object is closed properly. | ||
| * leakTracker.close(); | ||
| * } | ||
| * } | ||
| * | ||
| * }</pre> | ||
| */ | ||
| public class LeakDetector { | ||
| public static final Logger LOG = LoggerFactory.getLogger(LeakDetector.class); | ||
| private final ReferenceQueue<Object> queue = new ReferenceQueue<>(); | ||
| private final Set<LeakTracker> allLeaks = Collections.newSetFromMap(new ConcurrentHashMap<>()); | ||
szetszwo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private final String name; | ||
|
|
||
| public LeakDetector(String name) { | ||
| this.name = name; | ||
| start(); | ||
| } | ||
|
|
||
| private void start() { | ||
| Thread t = new Thread(this::run); | ||
| t.setName(LeakDetector.class.getSimpleName() + "-" + name); | ||
| t.setDaemon(true); | ||
| LOG.info("Starting leak detector thread {}.", name); | ||
| t.start(); | ||
| } | ||
|
|
||
| private void run() { | ||
| while (true) { | ||
| try { | ||
| LeakTracker tracker = (LeakTracker) queue.remove(); | ||
| // Original resource already been GCed, if tracker is not closed yet, | ||
| // report a leak. | ||
| if (allLeaks.remove(tracker)) { | ||
| tracker.reportLeak(); | ||
| } | ||
szetszwo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (InterruptedException e) { | ||
| LOG.warn("Thread interrupted, exiting.", e); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| LOG.warn("Exiting leak detector {}.", name); | ||
| } | ||
|
|
||
| public LeakTracker track(Object leakable, Runnable reportLeak) { | ||
| // A rate filter can be put here to only track a subset of all objects, e.g. 5%, 10%, | ||
| // if we have proofs that leak tracking impacts performance, or a single LeakDetector | ||
| // thread can't keep up with the pace of object allocation. | ||
| // For now, it looks effective enough and let keep it simple. | ||
| LeakTracker tracker = new LeakTracker(leakable, queue, allLeaks, reportLeak); | ||
| allLeaks.add(tracker); | ||
| return tracker; | ||
| } | ||
| } | ||
50 changes: 50 additions & 0 deletions
50
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/resource/LeakTracker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * 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.hdds.resource; | ||
|
|
||
| import java.lang.ref.ReferenceQueue; | ||
| import java.lang.ref.WeakReference; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * A token to track resource closure. | ||
| * | ||
| * @see LeakDetector | ||
| */ | ||
| public class LeakTracker extends WeakReference<Object> { | ||
| private final Set<LeakTracker> allLeaks; | ||
szetszwo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private final Runnable leakReporter; | ||
| LeakTracker(Object referent, ReferenceQueue<Object> referenceQueue, | ||
| Set<LeakTracker> allLeaks, Runnable leakReporter) { | ||
| super(referent, referenceQueue); | ||
| this.allLeaks = allLeaks; | ||
| this.leakReporter = leakReporter; | ||
| } | ||
|
|
||
| /** | ||
| * Called by the tracked resource when closing. | ||
| */ | ||
| public void close() { | ||
| allLeaks.remove(this); | ||
| } | ||
|
|
||
| void reportLeak() { | ||
| leakReporter.run(); | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/resource/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /** | ||
| * Contains utilities for resource management. | ||
| */ | ||
| package org.apache.hadoop.hdds.resource; | ||
|
||
67 changes: 67 additions & 0 deletions
67
hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/resource/TestLeakDetector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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.hdds.resource; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * Test LeakDetector. | ||
| */ | ||
| public class TestLeakDetector { | ||
| private static final LeakDetector LEAK_DETECTOR = new LeakDetector("test"); | ||
| private AtomicInteger leaks = new AtomicInteger(0); | ||
|
|
||
| @Test | ||
| public void testLeakDetector() throws Exception { | ||
| // create and close resource => no leaks. | ||
| createResource(true); | ||
| System.gc(); | ||
| Thread.sleep(100); | ||
| assertEquals(0, leaks.get()); | ||
|
|
||
| // create and not close => leaks. | ||
| createResource(false); | ||
| System.gc(); | ||
| Thread.sleep(100); | ||
| assertEquals(1, leaks.get()); | ||
| } | ||
|
|
||
| private void createResource(boolean close) throws Exception { | ||
| MyResource resource = new MyResource(leaks); | ||
| if (close) { | ||
| resource.close(); | ||
| } | ||
| } | ||
|
|
||
| private static final class MyResource implements AutoCloseable { | ||
| private final LeakTracker leakTracker; | ||
|
|
||
| private MyResource(final AtomicInteger leaks) { | ||
| leakTracker = LEAK_DETECTOR.track(this, () -> leaks.incrementAndGet()); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws Exception { | ||
| leakTracker.close(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should use the
org.apache.hadoop.hdds.utilspackage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@duongkame , any comments on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved.