Skip to content

Commit 0cf1b70

Browse files
Hold static resources in static fields in PluginsService (elastic#96263)
This doesn't have much production impact other than highlighting what things are actually static in nature, but speeds up tests by a couple percent when looping internal cluster tests that start many nodes, making reproducing tricky flakiness in distrib tests slightly less painful.
1 parent 38e6bbc commit 0cf1b70

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

server/src/main/java/org/elasticsearch/plugins/PluginsService.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.elasticsearch.plugins.spi.SPIClassIterator;
3434

3535
import java.io.IOException;
36-
import java.io.UncheckedIOException;
3736
import java.lang.ModuleLayer.Controller;
3837
import java.lang.module.Configuration;
3938
import java.lang.module.ModuleFinder;
@@ -99,6 +98,17 @@ record LoadedPlugin(PluginDescriptor descriptor, Plugin instance, ClassLoader lo
9998
private static final Logger logger = LogManager.getLogger(PluginsService.class);
10099
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(PluginsService.class);
101100

101+
private static final Map<String, List<ModuleQualifiedExportsService>> exportsServices;
102+
103+
static {
104+
Map<String, List<ModuleQualifiedExportsService>> qualifiedExports = new HashMap<>();
105+
var loader = ServiceLoader.load(ModuleQualifiedExportsService.class, PluginsService.class.getClassLoader());
106+
for (var exportsService : loader) {
107+
addExportsService(qualifiedExports, exportsService, exportsService.getClass().getModule().getName());
108+
}
109+
exportsServices = Map.copyOf(qualifiedExports);
110+
}
111+
102112
private final Settings settings;
103113
private final Path configPath;
104114

@@ -123,8 +133,7 @@ public PluginsService(Settings settings, Path configPath, Path modulesDirectory,
123133
this.settings = settings;
124134
this.configPath = configPath;
125135

126-
Map<String, List<ModuleQualifiedExportsService>> qualifiedExports = new HashMap<>();
127-
loadExportsServices(qualifiedExports, PluginsService.class.getClassLoader());
136+
Map<String, List<ModuleQualifiedExportsService>> qualifiedExports = new HashMap<>(exportsServices);
128137
addServerExportsService(qualifiedExports);
129138

130139
Set<PluginBundle> seenBundles = new LinkedHashSet<>();
@@ -202,12 +211,21 @@ static void checkMandatoryPlugins(Set<String> existingPlugins, Set<String> manda
202211
}
203212
}
204213

214+
private static final Set<String> officialPlugins;
215+
216+
static {
217+
try (var stream = PluginsService.class.getResourceAsStream("/plugins.txt")) {
218+
officialPlugins = Streams.readAllLines(stream).stream().map(String::trim).collect(Collectors.toUnmodifiableSet());
219+
} catch (final IOException e) {
220+
throw new AssertionError(e);
221+
}
222+
}
223+
205224
private static List<PluginRuntimeInfo> getRuntimeInfos(
206225
PluginIntrospector inspector,
207226
List<PluginDescriptor> pluginDescriptors,
208227
Map<String, LoadedPlugin> plugins
209228
) {
210-
var officialPlugins = getOfficialPlugins();
211229
List<PluginRuntimeInfo> runtimeInfos = new ArrayList<>();
212230
for (PluginDescriptor descriptor : pluginDescriptors) {
213231
LoadedPlugin plugin = plugins.get(descriptor.getName());
@@ -223,14 +241,6 @@ private static List<PluginRuntimeInfo> getRuntimeInfos(
223241
return runtimeInfos;
224242
}
225243

226-
private static Set<String> getOfficialPlugins() {
227-
try (var stream = PluginsService.class.getResourceAsStream("/plugins.txt")) {
228-
return Streams.readAllLines(stream).stream().map(String::trim).collect(Sets.toUnmodifiableSortedSet());
229-
} catch (final IOException e) {
230-
throw new UncheckedIOException(e);
231-
}
232-
}
233-
234244
/**
235245
* Map a function over all plugins
236246
* @param function a function that takes a plugin and returns a result
@@ -788,13 +798,6 @@ private static void exposeQualifiedExportsAndOpens(Module target, Map<String, Li
788798
qualifiedExports.getOrDefault(target.getName(), List.of()).forEach(exportService -> exportService.addExportsAndOpens(target));
789799
}
790800

791-
private static void loadExportsServices(Map<String, List<ModuleQualifiedExportsService>> qualifiedExports, ClassLoader classLoader) {
792-
var loader = ServiceLoader.load(ModuleQualifiedExportsService.class, classLoader);
793-
for (var exportsService : loader) {
794-
addExportsService(qualifiedExports, exportsService, exportsService.getClass().getModule().getName());
795-
}
796-
}
797-
798801
private static void addExportsService(
799802
Map<String, List<ModuleQualifiedExportsService>> qualifiedExports,
800803
ModuleQualifiedExportsService exportsService,

0 commit comments

Comments
 (0)