forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure one-shot wrappers release their delegates
We recently adjusted `RunOnce`, `Releasables#releaseOnce` and `ActionListener#notifyOnce` so that once they have fired they drop the now-unnecessary reference to the delegate. This commit introduces tests to verify that this reference does genuinely become unreachable (i.e. available for garbage collection) as expected. It also fixes a bug in `ActionListener#notifyOnce` which caused us to unexpectedly retain a reference to the delegate 🤦 Relates elastic#92452 Relates elastic#92507 Relates elastic#92537
- Loading branch information
1 parent
2cdaabe
commit a75dd6c
Showing
8 changed files
with
229 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
126 changes: 126 additions & 0 deletions
126
test/framework/src/main/java/org/elasticsearch/test/ReachabilityChecker.java
This file contains 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,126 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.test; | ||
|
||
import org.elasticsearch.common.Randomness; | ||
import org.elasticsearch.common.util.concurrent.ConcurrentCollections; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.MemoryMXBean; | ||
import java.lang.ref.PhantomReference; | ||
import java.lang.ref.ReferenceQueue; | ||
import java.util.Objects; | ||
import java.util.Queue; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Utility class for checking that objects become unreachable when expected. | ||
*/ | ||
public class ReachabilityChecker { | ||
|
||
private final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); | ||
private final Queue<Registered> references = ConcurrentCollections.newQueue(); | ||
|
||
/** | ||
* Register the given target object for reachability checks. | ||
* | ||
* @return the given target object. | ||
*/ | ||
public <T> T register(T target) { | ||
var referenceQueue = new ReferenceQueue<>(); | ||
references.add( | ||
new Registered(target.toString(), new PhantomReference<>(Objects.requireNonNull(target), referenceQueue), referenceQueue) | ||
); | ||
return target; | ||
} | ||
|
||
/** | ||
* Ensure that all registered objects have become unreachable. | ||
*/ | ||
public void ensureUnreachable() { | ||
ensureUnreachable(TimeUnit.SECONDS.toMillis(10)); | ||
} | ||
|
||
void ensureUnreachable(long timeoutMillis) { | ||
Registered registered; | ||
while ((registered = references.poll()) != null) { | ||
registered.assertReferenceEnqueuedForCollection(memoryMXBean, timeoutMillis); | ||
} | ||
} | ||
|
||
/** | ||
* From the objects registered since the most recent call to {@link #ensureUnreachable()} (or since the construction of this {@link | ||
* ReachabilityChecker} if {@link #ensureUnreachable()} has not been called) this method chooses one at random and verifies that it has | ||
* not yet become unreachable. | ||
*/ | ||
public void checkReachable() { | ||
if (references.peek() == null) { | ||
throw new AssertionError("no references registered"); | ||
} | ||
|
||
var target = Randomness.get().nextInt(references.size()); | ||
var iterator = references.iterator(); | ||
for (int i = 0; i < target; i++) { | ||
assertTrue(iterator.hasNext()); | ||
assertNotNull(iterator.next()); | ||
} | ||
|
||
assertTrue(iterator.hasNext()); | ||
iterator.next().assertReferenceNotEnqueuedForCollection(memoryMXBean); | ||
} | ||
|
||
private static final class Registered { | ||
private final String description; | ||
private final PhantomReference<?> phantomReference; | ||
private final ReferenceQueue<?> referenceQueue; | ||
|
||
Registered(String description, PhantomReference<?> phantomReference, ReferenceQueue<?> referenceQueue) { | ||
this.description = description; | ||
this.phantomReference = phantomReference; | ||
this.referenceQueue = referenceQueue; | ||
} | ||
|
||
/** | ||
* Attempts to trigger the GC repeatedly until the {@link ReferenceQueue} yields a reference. | ||
*/ | ||
public void assertReferenceEnqueuedForCollection(MemoryMXBean memoryMXBean, long timeoutMillis) { | ||
try { | ||
final var timeoutAt = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis); | ||
while (true) { | ||
memoryMXBean.gc(); | ||
final var ref = referenceQueue.remove(500); | ||
if (ref != null) { | ||
ref.clear(); | ||
return; | ||
} | ||
assertTrue("still reachable: " + description, System.nanoTime() < timeoutAt); | ||
assertNull(phantomReference.get()); // always succeeds, we're just doing this to use the phantomReference for something | ||
} | ||
} catch (Exception e) { | ||
throw new AssertionError("unexpected", e); | ||
} | ||
} | ||
|
||
/** | ||
* Attempts to trigger the GC and verifies that the {@link ReferenceQueue} does not yield a reference. | ||
*/ | ||
public void assertReferenceNotEnqueuedForCollection(MemoryMXBean memoryMXBean) { | ||
try { | ||
memoryMXBean.gc(); | ||
assertNull("became unreachable: " + description, referenceQueue.remove(100)); | ||
} catch (Exception e) { | ||
throw new AssertionError("unexpected", e); | ||
} | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
test/framework/src/test/java/org/elasticsearch/test/ReachabilityCheckerTests.java
This file contains 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,55 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.test; | ||
|
||
import org.hamcrest.Matchers; | ||
|
||
public class ReachabilityCheckerTests extends ESTestCase { | ||
|
||
public void testSuccess() { | ||
final var reachabilityChecker = new ReachabilityChecker(); | ||
var target = reachabilityChecker.register(createTarget()); | ||
reachabilityChecker.checkReachable(); | ||
target = null; | ||
reachabilityChecker.ensureUnreachable(); | ||
assertNull(target); | ||
} | ||
|
||
public void testBecomesUnreachable() { | ||
final var reachabilityChecker = new ReachabilityChecker(); | ||
var target = reachabilityChecker.register(createTarget()); | ||
reachabilityChecker.checkReachable(); | ||
target = null; | ||
assertThat( | ||
expectThrows(AssertionError.class, reachabilityChecker::checkReachable).getMessage(), | ||
Matchers.startsWith("became unreachable: test object") | ||
); | ||
assertNull(target); | ||
} | ||
|
||
public void testStaysReachable() { | ||
final var reachabilityChecker = new ReachabilityChecker(); | ||
var target = reachabilityChecker.register(createTarget()); | ||
reachabilityChecker.checkReachable(); | ||
assertThat( | ||
expectThrows(AssertionError.class, () -> reachabilityChecker.ensureUnreachable(500)).getMessage(), | ||
Matchers.startsWith("still reachable: test object") | ||
); | ||
assertNotNull(target); | ||
} | ||
|
||
private static Object createTarget() { | ||
return new Object() { | ||
@Override | ||
public String toString() { | ||
return "test object"; | ||
} | ||
}; | ||
} | ||
} |