Skip to content

Commit 75116a2

Browse files
authored
Adds test name to MockPageCacheRecycler exception (#28359)
This change adds the test name to the exceptions thrown by the MockPageCacheRecycler and MockBigArrays. Also, if there is more than one page/array which are not released it will add the first one as the cause of the thrown exception and the others as suppressed exceptions. Relates to #21315
1 parent 65184d0 commit 75116a2

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

test/framework/src/main/java/org/elasticsearch/common/util/MockBigArrays.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121

2222
import com.carrotsearch.randomizedtesting.RandomizedContext;
2323
import com.carrotsearch.randomizedtesting.SeedUtils;
24+
2425
import org.apache.lucene.util.Accountable;
2526
import org.apache.lucene.util.Accountables;
2627
import org.apache.lucene.util.BytesRef;
27-
import org.elasticsearch.common.settings.Settings;
28+
import org.apache.lucene.util.LuceneTestCase;
2829
import org.elasticsearch.common.util.set.Sets;
2930
import org.elasticsearch.indices.breaker.CircuitBreakerService;
3031
import org.elasticsearch.test.ESTestCase;
3132

3233
import java.util.Collection;
3334
import java.util.Collections;
3435
import java.util.HashMap;
36+
import java.util.Iterator;
3537
import java.util.Map;
3638
import java.util.Random;
3739
import java.util.concurrent.ConcurrentHashMap;
@@ -59,8 +61,17 @@ public static void ensureAllArraysAreReleased() throws Exception {
5961
masterCopy.keySet().retainAll(ACQUIRED_ARRAYS.keySet());
6062
ACQUIRED_ARRAYS.keySet().removeAll(masterCopy.keySet()); // remove all existing master copy we will report on
6163
if (!masterCopy.isEmpty()) {
62-
final Object cause = masterCopy.entrySet().iterator().next().getValue();
63-
throw new RuntimeException(masterCopy.size() + " arrays have not been released", cause instanceof Throwable ? (Throwable) cause : null);
64+
Iterator<Object> causes = masterCopy.values().iterator();
65+
Object firstCause = causes.next();
66+
RuntimeException exception = new RuntimeException(masterCopy.size() + " arrays have not been released",
67+
firstCause instanceof Throwable ? (Throwable) firstCause : null);
68+
while (causes.hasNext()) {
69+
Object cause = causes.next();
70+
if (cause instanceof Throwable) {
71+
exception.addSuppressed((Throwable) cause);
72+
}
73+
}
74+
throw exception;
6475
}
6576
}
6677
}
@@ -249,7 +260,9 @@ private abstract static class AbstractArrayWrapper {
249260
AbstractArrayWrapper(boolean clearOnResize) {
250261
this.clearOnResize = clearOnResize;
251262
this.originalRelease = new AtomicReference<>();
252-
ACQUIRED_ARRAYS.put(this, TRACK_ALLOCATIONS ? new RuntimeException() : Boolean.TRUE);
263+
ACQUIRED_ARRAYS.put(this,
264+
TRACK_ALLOCATIONS ? new RuntimeException("Unreleased array from test: " + LuceneTestCase.getTestClass().getName())
265+
: Boolean.TRUE);
253266
}
254267

255268
protected abstract BigArray getDelegate();

test/framework/src/main/java/org/elasticsearch/common/util/MockPageCacheRecycler.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.common.util;
2121

22+
import org.apache.lucene.util.LuceneTestCase;
2223
import org.elasticsearch.common.recycler.Recycler.V;
2324
import org.elasticsearch.common.settings.Settings;
2425
import org.elasticsearch.common.util.set.Sets;
@@ -27,6 +28,7 @@
2728
import java.lang.reflect.Array;
2829
import java.util.Arrays;
2930
import java.util.HashMap;
31+
import java.util.Iterator;
3032
import java.util.Map;
3133
import java.util.Random;
3234
import java.util.concurrent.ConcurrentHashMap;
@@ -48,8 +50,13 @@ public static void ensureAllPagesAreReleased() throws Exception {
4850
masterCopy.keySet().retainAll(ACQUIRED_PAGES.keySet());
4951
ACQUIRED_PAGES.keySet().removeAll(masterCopy.keySet()); // remove all existing master copy we will report on
5052
if (!masterCopy.isEmpty()) {
51-
final Throwable t = masterCopy.entrySet().iterator().next().getValue();
52-
throw new RuntimeException(masterCopy.size() + " pages have not been released", t);
53+
Iterator<Throwable> causes = masterCopy.values().iterator();
54+
Throwable firstCause = causes.next();
55+
RuntimeException exception = new RuntimeException(masterCopy.size() + " pages have not been released", firstCause);
56+
while (causes.hasNext()) {
57+
exception.addSuppressed(causes.next());
58+
}
59+
throw exception;
5360
}
5461
}
5562
}
@@ -66,7 +73,7 @@ public MockPageCacheRecycler(Settings settings) {
6673
}
6774

6875
private <T> V<T> wrap(final V<T> v) {
69-
ACQUIRED_PAGES.put(v, new Throwable());
76+
ACQUIRED_PAGES.put(v, new Throwable("Unreleased Page from test: " + LuceneTestCase.getTestClass().getName()));
7077
return new V<T>() {
7178

7279
@Override

0 commit comments

Comments
 (0)