Skip to content

Commit 6b3b519

Browse files
benwtrentelasticsearchmachine
andauthored
[9.0] Bypass MMap arena grouping as this has caused issues with too many regions being mapped (#135012) (#135131)
* Bypass MMap arena grouping as this has caused issues with too many regions being mapped (#135012) There is a JDK issue where closing sharedArenas from many threads can significantly harm performance. This ref-counting of shared arenas was designed as a way to get around this performance issue. However, we have noticed a significant increase in leaks and issues with mmap regions since this change. https://bugs.openjdk.org/browse/JDK-8335480 should have helped the performance impact of closing shared arenas (though possibly not fully mitigated it). I am proposing we turn off the grouping as it appears (at least to me), not worth it. I am willing to backdown if we thing other fixes should be done. I also suggest this gets backported to 9.1, 8.19, and is merged into 9.2 (cherry picked from commit 2672cd0) * [CI] Auto commit changes from spotless * fixing format * fixing compilation --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent 55305b3 commit 6b3b519

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

distribution/src/config/jvm.options

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
# Lucene 10: apply MADV_NORMAL advice to enable more aggressive readahead
6666
-Dorg.apache.lucene.store.defaultReadAdvice=normal
6767

68+
# Lucene provides a mechanism for shared mmapped arenas to be referenced between multiple threads
69+
# this is to get around potential performance issues when closing shared arenas on many threads
70+
# default to 1 to disable this feature
71+
-Dorg.apache.lucene.store.MMapDirectory.sharedArenaMaxPermits=1
72+
6873
## heap dumps
6974

7075
# generate a heap dump when an allocation from the Java heap fails; heap dumps

docs/changelog/135012.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 135012
2+
summary: Bypass MMap arena grouping as this has caused issues with too many regions
3+
being mapped
4+
area: "Engine"
5+
type: bug
6+
issues: []

plugins/store-smb/src/main/java/org/elasticsearch/index/store/smb/SmbMmapFsDirectoryFactory.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ public final class SmbMmapFsDirectoryFactory extends FsDirectoryFactory {
2424

2525
@Override
2626
protected Directory newFSDirectory(Path location, LockFactory lockFactory, IndexSettings indexSettings) throws IOException {
27+
MMapDirectory mMapDirectory = adjustSharedArenaGrouping(new MMapDirectory(location, lockFactory));
2728
return new SmbDirectoryWrapper(
28-
setPreload(
29-
new MMapDirectory(location, lockFactory),
30-
new HashSet<>(indexSettings.getValue(IndexModule.INDEX_STORE_PRE_LOAD_SETTING))
31-
)
29+
setPreload(mMapDirectory, new HashSet<>(indexSettings.getValue(IndexModule.INDEX_STORE_PRE_LOAD_SETTING)))
3230
);
3331
}
3432
}

server/src/main/java/org/elasticsearch/index/store/FsDirectoryFactory.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.elasticsearch.index.IndexModule;
2929
import org.elasticsearch.index.IndexSettings;
3030
import org.elasticsearch.index.shard.ShardPath;
31+
import org.elasticsearch.logging.LogManager;
32+
import org.elasticsearch.logging.Logger;
3133
import org.elasticsearch.plugins.IndexStorePlugin;
3234

3335
import java.io.IOException;
@@ -37,8 +39,25 @@
3739
import java.util.Set;
3840
import java.util.function.BiPredicate;
3941

42+
import static org.apache.lucene.store.MMapDirectory.SHARED_ARENA_MAX_PERMITS_SYSPROP;
43+
4044
public class FsDirectoryFactory implements IndexStorePlugin.DirectoryFactory {
4145

46+
private static final Logger Log = LogManager.getLogger(FsDirectoryFactory.class);
47+
private static final int sharedArenaMaxPermits;
48+
static {
49+
String prop = System.getProperty(SHARED_ARENA_MAX_PERMITS_SYSPROP);
50+
int value = 1;
51+
if (prop != null) {
52+
try {
53+
value = Integer.parseInt(prop); // ensure it's a valid integer
54+
} catch (NumberFormatException e) {
55+
Log.warn(() -> "unable to parse system property [" + SHARED_ARENA_MAX_PERMITS_SYSPROP + "] with value [" + prop + "]", e);
56+
}
57+
}
58+
sharedArenaMaxPermits = value; // default to 1
59+
}
60+
4261
private static final FeatureFlag MADV_RANDOM_FEATURE_FLAG = new FeatureFlag("madv_random");
4362

4463
public static final Setting<LockFactory> INDEX_LOCK_FACTOR_SETTING = new Setting<>("index.store.fs.fs_lock", "native", (s) -> {
@@ -72,6 +91,7 @@ protected Directory newFSDirectory(Path location, LockFactory lockFactory, Index
7291
// Use Lucene defaults
7392
final FSDirectory primaryDirectory = FSDirectory.open(location, lockFactory);
7493
if (primaryDirectory instanceof MMapDirectory mMapDirectory) {
94+
mMapDirectory = adjustSharedArenaGrouping(mMapDirectory);
7595
Directory dir = new HybridDirectory(lockFactory, setPreload(mMapDirectory, preLoadExtensions));
7696
if (MADV_RANDOM_FEATURE_FLAG.isEnabled() == false) {
7797
dir = disableRandomAdvice(dir);
@@ -81,7 +101,8 @@ protected Directory newFSDirectory(Path location, LockFactory lockFactory, Index
81101
return primaryDirectory;
82102
}
83103
case MMAPFS:
84-
Directory dir = setPreload(new MMapDirectory(location, lockFactory), preLoadExtensions);
104+
MMapDirectory mMapDirectory = adjustSharedArenaGrouping(new MMapDirectory(location, lockFactory));
105+
Directory dir = setPreload(mMapDirectory, preLoadExtensions);
85106
if (MADV_RANDOM_FEATURE_FLAG.isEnabled() == false) {
86107
dir = disableRandomAdvice(dir);
87108
}
@@ -101,6 +122,13 @@ public MMapDirectory setPreload(MMapDirectory mMapDirectory, Set<String> preLoad
101122
return mMapDirectory;
102123
}
103124

125+
public MMapDirectory adjustSharedArenaGrouping(MMapDirectory mMapDirectory) {
126+
if (sharedArenaMaxPermits <= 1) {
127+
mMapDirectory.setGroupingFunction(MMapDirectory.NO_GROUPING);
128+
}
129+
return mMapDirectory;
130+
}
131+
104132
/** Gets a preload function based on the given preLoadExtensions. */
105133
static BiPredicate<String, IOContext> getPreloadFunc(Set<String> preLoadExtensions) {
106134
if (preLoadExtensions.isEmpty() == false) {

0 commit comments

Comments
 (0)