Do not lock on settings keyset if keys initialized (#52435)

Every time a setting#exist call is made we lock on the keyset to ensure
that it has been initialized. This a heavyweight operation that only
should be done once. This commit moves to a volatile read instead to
prevent unnecessary locking.
This commit is contained in:
Tim Brooks 2020-02-18 08:08:39 -07:00
parent b5e191fa57
commit 7fcd997b39
No known key found for this signature in database
GPG Key ID: C2AA3BB91A889E77
1 changed files with 11 additions and 8 deletions

View File

@ -716,14 +716,17 @@ public final class Settings implements ToXContentFragment {
/** Returns the fully qualified setting names contained in this settings object. */
public Set<String> keySet() {
synchronized (keys) {
if (keys.get() == null) {
if (secureSettings == null) {
keys.set(settings.keySet());
} else {
Stream<String> stream = Stream.concat(settings.keySet().stream(), secureSettings.getSettingNames().stream());
// uniquify, since for legacy reasons the same setting name may exist in both
keys.set(Collections.unmodifiableSet(stream.collect(Collectors.toSet())));
if (keys.get() == null) {
synchronized (keys) {
// Check that the keys are still null now that we have acquired the lock
if (keys.get() == null) {
if (secureSettings == null) {
keys.set(settings.keySet());
} else {
Stream<String> stream = Stream.concat(settings.keySet().stream(), secureSettings.getSettingNames().stream());
// uniquify, since for legacy reasons the same setting name may exist in both
keys.set(Collections.unmodifiableSet(stream.collect(Collectors.toSet())));
}
}
}
}