Skip to content

Commit d1e7941

Browse files
committed
Reenable LiveVersionMapTests.testRamBytesUsed on Java 9.
I also had to make the test more lenient. This is due to the fact that Lucene's RamUsageTester was changed in order not to reflect `java.*` classes and the way that it estimates ram usage of maps is by assuming it has similar memory usage to an `Object[]` array that stores all keys and values. The implementation in `LiveVersionMap` tries to be slightly more realistic by taking the load factor and linked lists into account, so it usually gives a higher estimate which happens to be closer to reality. Closes #22548
1 parent 3cf599b commit d1e7941

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

server/src/test/java/org/elasticsearch/index/engine/LiveVersionMapTests.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121

2222
import org.apache.lucene.util.BytesRef;
2323
import org.apache.lucene.util.BytesRefBuilder;
24+
import org.apache.lucene.util.Constants;
2425
import org.apache.lucene.util.RamUsageTester;
2526
import org.apache.lucene.util.TestUtil;
26-
import org.elasticsearch.Assertions;
27-
import org.elasticsearch.bootstrap.JavaVersion;
2827
import org.elasticsearch.common.lease.Releasable;
2928
import org.elasticsearch.test.ESTestCase;
3029

@@ -43,7 +42,6 @@
4342
public class LiveVersionMapTests extends ESTestCase {
4443

4544
public void testRamBytesUsed() throws Exception {
46-
assumeTrue("Test disabled for JDK 9", JavaVersion.current().compareTo(JavaVersion.parse("9")) < 0);
4745
LiveVersionMap map = new LiveVersionMap();
4846
for (int i = 0; i < 100000; ++i) {
4947
BytesRefBuilder uid = new BytesRefBuilder();
@@ -72,8 +70,23 @@ public void testRamBytesUsed() throws Exception {
7270
}
7371
actualRamBytesUsed = RamUsageTester.sizeOf(map);
7472
estimatedRamBytesUsed = map.ramBytesUsed();
75-
// less than 25% off
76-
assertEquals(actualRamBytesUsed, estimatedRamBytesUsed, actualRamBytesUsed / 4);
73+
long tolerance;
74+
if (Constants.JRE_IS_MINIMUM_JAVA9) {
75+
// With Java 9, RamUsageTester computes the memory usage of maps as
76+
// the memory usage of an array that would contain exactly all keys
77+
// and values. This is an under-estimation of the actual memory
78+
// usage since it ignores the impact of the load factor and of the
79+
// linked list/tree that is used to store collisions. Se we use a
80+
// bigger tolerance.
81+
// less than 50% off
82+
tolerance = actualRamBytesUsed / 2;
83+
} else {
84+
// Java 8 is more accurate by doing reflection into the actual JDK classes
85+
// so we give it a lower error bound.
86+
// less than 25% off
87+
tolerance = actualRamBytesUsed / 4;
88+
}
89+
assertEquals(actualRamBytesUsed, estimatedRamBytesUsed, tolerance);
7790
}
7891

7992
private BytesRef uid(String string) {

0 commit comments

Comments
 (0)