Open qijiale76 opened 1 year ago
Nice feature!
@qijiale76 Any progress here?
@qijiale76 Any progress here?
@rickyma I have attempted to address this issue. It's fairly straightforward to add tests related to memory leaks, and using the older version of the code that hasn't fixed the bug, these leaks can be detected. However, I haven't found a method to cause the entire unit test to fail when a memory leak occurs. If you have any effective solutions, please let me know. Thank you for your attention.
I think you can use a custom ResourceLeakDetector
:
import io.netty.util.ResourceLeakDetector;
import io.netty.util.ResourceLeak;
import io.netty.util.internal.StringUtil;
public class CustomResourceLeakDetector<T> extends ResourceLeakDetector<T> {
public CustomResourceLeakDetector(Class<?> resourceType, int samplingInterval) {
super(resourceType, samplingInterval);
}
@Override
protected void reportTracedLeak(String records, String additionalString) {
throw new AssertionError("Resource leak detected: " + records + StringUtil.NEWLINE + additionalString);
}
@Override
protected void reportUntracedLeak(String records) {
throw new AssertionError("Resource leak detected: " + records);
}
}
And the demo test code could be:
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.util.ResourceLeakDetectorFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class NettyLeakTest {
private CustomResourceLeakDetector<ByteBuf> leakDetector;
@Before
public void setUp() {
// Create a custom ResourceLeakDetector
leakDetector = new CustomResourceLeakDetector<>(ByteBuf.class, 1);
// Set ResourceLeakDetectorFactory to the custom ResourceLeakDetector
ResourceLeakDetectorFactory.setResourceLeakDetectorFactory(() -> leakDetector);
}
@Test
public void testLeak() {
// Write your business code here
ByteBufAllocator allocator = PooledByteBufAllocator.DEFAULT;
ByteBuf buffer = allocator.buffer();
// Intentionally do not release the buffer to trigger leak detection
// buffer.release();
}
@After
public void tearDown() {
// Check for leaks after the test is finished
// If a leak is detected, the custom ResourceLeakDetector will throw an AssertionError, causing the JUnit test to fail
leakDetector.reportLeak();
}
}
The code might not be workable. You can dig into it. Help Uniffle to gain the ability to find out potential leaks during tests.
@qijiale76
Code of Conduct
Search before asking
What would you like to be improved?
As mentioned in https://github.com/apache/incubator-uniffle/issues/1152, by adding
-Dio.netty.leakDetection.level=advanced
, we discovered the problem of buffer leak in Netty. It is necessary to include this leak detection feature in integration testing to better identify such problems.Ref: https://netty.io/wiki/reference-counted-objects.html
How should we improve?
No response
Are you willing to submit PR?