Skip to content

Commit

Permalink
deprecate fallback prefix in Affix settings
Browse files Browse the repository at this point in the history
  • Loading branch information
stu-elastic committed Jan 17, 2024
1 parent 71c23f2 commit 36cb3d4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -441,12 +442,16 @@ public boolean hasIndexScope() {
/**
* Returns <code>true</code> if this setting is deprecated, otherwise <code>false</code>
*/
private boolean isDeprecated() {
protected boolean isDeprecated() {
return properties.contains(Property.Deprecated)
|| properties.contains(Property.DeprecatedWarning)
|| properties.contains(Property.IndexSettingDeprecatedInV7AndRemovedInV8);
}

protected Stream<Tuple<String, String>> deprecatedKeyStream(Settings settings) {
return Stream.empty();
}

private boolean isDeprecatedWarningOnly() {
return properties.contains(Property.DeprecatedWarning);
}
Expand Down Expand Up @@ -649,6 +654,10 @@ void checkDeprecation(Settings settings) {
Settings.DeprecationLoggerHolder.deprecationLogger.critical(DeprecationCategory.SETTINGS, key, message, key);
}
}
String message = "[{}] setting was deprecated in Elasticsearch and will be removed in a future release, use [{}] instead";
deprecatedKeyStream(settings).forEach(deprecatedReplacement -> {
Settings.DeprecationLoggerHolder.deprecationLogger.warn(DeprecationCategory.SETTINGS, deprecatedReplacement.v1(), message, deprecatedReplacement.v1(), deprecatedReplacement.v2());
});
}

/**
Expand Down Expand Up @@ -1077,6 +1086,17 @@ public Map<String, T> getAsMap(Settings settings) {
});
return Collections.unmodifiableMap(map);
}

@Override
protected Stream<Tuple<String, String>> deprecatedKeyStream(Settings settings) {
if (key.hasFallback() == false) {
return super.deprecatedKeyStream(settings);
}
return Stream.concat(
super.deprecatedKeyStream(settings),
settings.keySet().stream().map(key::maybeFallback).flatMap(Optional::stream)
);
}
}

/**
Expand Down Expand Up @@ -2168,8 +2188,8 @@ public boolean match(String toTest) {
*/
public static final class AffixKey implements Key {
private final Pattern pattern;
private final Pattern fallbackPattern;
private final String prefix;
private final String fallbackPrefix;
private final String suffix;

private final String keyString;
Expand All @@ -2182,14 +2202,16 @@ public static final class AffixKey implements Key {
if (prefix.endsWith(".") == false) {
throw new IllegalArgumentException("prefix must end with a '.'");
}
this.fallbackPrefix = fallbackPrefix;

String prefixPattern;
if (fallbackPrefix != null) {
if (fallbackPrefix.endsWith(".") == false) {
throw new IllegalArgumentException("prefix must end with a '.'");
}
fallbackPattern = Pattern.compile("(" + Pattern.quote(fallbackPrefix) + ")" + "((?:[-\\w]+[.])*[-\\w]+$)");
prefixPattern = "(" + Pattern.quote(prefix) + "|" + Pattern.quote(fallbackPrefix) + ")";
} else {
fallbackPattern = null;
prefixPattern = "(" + Pattern.quote(prefix) + ")";
}
this.suffix = suffix;
Expand All @@ -2214,6 +2236,19 @@ public boolean match(String key) {
return pattern.matcher(key).matches();
}

// package private for testing
Optional<Tuple<String, String>> maybeFallback(String key) {
Matcher m = fallbackPattern.matcher(key);
if (m.matches() == false) {
return Optional.empty();
}
return Optional.of(new Tuple<>(key, m.replaceFirst(prefix + "$2")));
}

private boolean hasFallback() {
return fallbackPattern != null;
}

/**
* Returns a string representation of the concrete setting key
*/
Expand All @@ -2226,12 +2261,12 @@ String getConcreteString(String key) {
}

/**
* Returns a string representation of the concrete setting key
* Returns a string representation of the namespace, without prefix and suffix, of the affix key
*/
String getNamespace(String key) {
Matcher matcher = pattern.matcher(key);
if (matcher.matches() == false) {
throw new IllegalStateException("can't get concrete string for key " + key + " key doesn't match");
throw new IllegalStateException("can't get namespace for key " + key + " key doesn't match");
}
return Settings.internKeyOrValue(matcher.group(3));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -1423,6 +1424,29 @@ public void testCheckForDeprecation() {
assertSettingDeprecationsAndWarnings(new Setting<?>[] { deprecatedSettingWarningOnly });
}

public void testAffixSettingFallback() {
Setting.AffixKey key = new Setting.AffixKey("foo.bar.baz.", null, "qux.quux.");
Optional<Tuple<String, String>> replacement = key.maybeFallback("qux.quux.abc.123.doremi");
assertTrue(replacement.isPresent());
assertThat(replacement.get(), equalTo(new Tuple<>("qux.quux.abc.123.doremi", "foo.bar.baz.abc.123.doremi")));
assertFalse(key.maybeFallback("foo.bar.baz.123.doremi").isPresent());
}

public void testCheckForDeprecatedAffixSettings() {
Setting.AffixSetting<Boolean> setting = Setting.prefixKeySetting(
"foo.",
"bar.",
(key) -> Setting.boolSetting(key, false, Property.NodeScope)
);
setting.checkDeprecation(Settings.builder().put("bar.a.1", true).put("bar.b.q.do", false).build());
assertWarnings(
"[bar.a.1] setting was deprecated in Elasticsearch and will be removed in a future release, use [foo.a.1] instead",
"[bar.b.q.do] setting was deprecated in Elasticsearch and will be removed in a future release, use [foo.b.q.do] instead"
);
setting.checkDeprecation(Settings.builder().put("foo.a.1", true).put("foo.b.q.d", false).build());
ensureNoWarnings();
}

public void testCheckForDeprecationWithSkipSetting() {
final String settingName = "foo.bar.hide.this";
final String settingValue = "blat";
Expand Down

0 comments on commit 36cb3d4

Please sign in to comment.