Repository navigation
SOLR-17864: Provide backwards support for "<str name="hideStackTrace">${solr.hideStackTrace:false}</str>" #4893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| title: > | ||
| A solr.xml or solrconfig.xml property-substitution token (${...}) that still references a | ||
| legacy/deprecated system property name now falls back to the current property's value if that's | ||
| been set, instead of silently ignoring it. Previously, upgrading and setting only the new | ||
| property name had no effect unless the config file's own ${...} token was also updated by hand. | ||
| type: fixed | ||
| authors: | ||
| - name: Eric Pugh | ||
| links: | ||
| - name: SOLR-17864 | ||
| url: https://issues.apache.org/jira/browse/SOLR-17864 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,11 +15,12 @@ | |
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.solr.logging; | ||
| package org.apache.solr.common.util; | ||
|
|
||
| import java.lang.invoke.MethodHandles; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.function.Supplier; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
|
|
@@ -41,11 +42,25 @@ public class DeprecationLog { | |
| * @return true if logged | ||
| */ | ||
| public static boolean log(String featureId, String message) { | ||
| if (alreadyLogged.putIfAbsent(featureId, message) != null) { | ||
| return log(featureId, () -> message); | ||
| } | ||
|
|
||
| /** | ||
| * Like {@link #log(String, String)}, but only builds the message if this is the first time {@code | ||
| * featureId} is logged. Use this when building the message isn't free and the call site runs | ||
| * often (e.g. on every property lookup). | ||
| * | ||
| * @return true if logged | ||
| */ | ||
| public static boolean log(String featureId, Supplier<String> message) { | ||
| if (alreadyLogged.containsKey(featureId)) { | ||
| return false; | ||
| } | ||
|
Comment on lines
+56
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This first part is needless; surely putIfAbsent will handle this, right? |
||
| if (alreadyLogged.putIfAbsent(featureId, featureId) != null) { | ||
| return false; | ||
| } | ||
| Logger log = LoggerFactory.getLogger(LOG_PREFIX + featureId); | ||
| log.warn(message); | ||
| log.warn(message.get()); | ||
| return true; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,8 +47,14 @@ public class EnvUtils { | |
| /** Maps ENV keys to sys prop keys for special/custom mappings */ | ||
| private static final Map<String, String> CUSTOM_MAPPINGS = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Maps a legacy/deprecated sys prop key (dot-separated form) to the current property it was | ||
| * replaced by, and whether the replacement is boolean-inverted relative to the legacy property. | ||
| */ | ||
| private record DeprecatedMapping(String currentName, boolean inverted) {} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay/fine but merely for a boolean you could simply have a parallel set of inverted names. That's what I'd do. |
||
|
|
||
| /** Maps deprecated sys prop keys to current sys prop keys with special/custom mappings */ | ||
| private static final Map<String, String> DEPRECATED_MAPPINGS = new HashMap<>(); | ||
| private static final Map<String, DeprecatedMapping> DEPRECATED_MAPPINGS = new HashMap<>(); | ||
|
|
||
| private static final Map<String, String> camelCaseToDotsMap = new ConcurrentHashMap<>(); | ||
|
|
||
|
|
@@ -74,8 +80,14 @@ public class EnvUtils { | |
| for (String key : props.stringPropertyNames()) { | ||
| CUSTOM_MAPPINGS.put(key, props.getProperty(key)); | ||
| } | ||
| for (String key : deprecatedProps.stringPropertyNames()) { | ||
| DEPRECATED_MAPPINGS.put(camelCaseToDotSeparated(deprecatedProps.getProperty(key)), key); | ||
| for (String currentName : deprecatedProps.stringPropertyNames()) { | ||
| String legacyName = deprecatedProps.getProperty(currentName); | ||
| boolean inverted = legacyName.startsWith("!"); | ||
| if (inverted) { | ||
| legacyName = legacyName.substring(1); | ||
| } | ||
| DEPRECATED_MAPPINGS.put( | ||
| camelCaseToDotSeparated(legacyName), new DeprecatedMapping(currentName, inverted)); | ||
| } | ||
| init(false, System.getenv(), System.getProperties()); | ||
| } | ||
|
|
@@ -108,24 +120,68 @@ public static String getProperty(String key) { | |
| * @param defaultValue fallback value if property is not found | ||
| */ | ||
| public static String getProperty(String key, String defaultValue) { | ||
| String value = getPropertyWithCamelCaseFallback(key); | ||
| String value = resolvePropertyValue(key); | ||
| return value != null ? value : defaultValue; | ||
| } | ||
|
|
||
| /** | ||
| * Get a property from given key or an alias key converted from CamelCase to dot separated. | ||
| * Resolves {@code key} by trying, in order: the key as given; the key converted from CamelCase to | ||
| * dot-separated; and -- if {@code key} is itself a legacy/deprecated property name -- the current | ||
| * property it was replaced by. See {@link #resolveViaDeprecatedMapping} for why that last | ||
| * fallback is needed. | ||
| * | ||
| * @return property value or value of dot-separated alias key or null if not found | ||
| * @return the resolved value, or null if none of the above are found | ||
| */ | ||
| private static String getPropertyWithCamelCaseFallback(String key) { | ||
| private static String resolvePropertyValue(String key) { | ||
| String value = System.getProperty(key); | ||
| if (value != null) { | ||
| return value; | ||
| } else { | ||
| // Figure out if string is CamelCase and convert to dot separated | ||
| String altKey = camelCaseToDotSeparated(key); | ||
| return System.getProperty(altKey); | ||
| } | ||
| // Figure out if string is CamelCase and convert to dot separated | ||
| String altKey = camelCaseToDotSeparated(key); | ||
| value = System.getProperty(altKey); | ||
| if (value != null) { | ||
| return value; | ||
| } | ||
| return resolveViaDeprecatedMapping(key, altKey); | ||
| } | ||
|
|
||
| /** | ||
| * If {@code dotKey} is a known legacy/deprecated property name, returns the value of the current | ||
| * property it was replaced by (inverted, if the mapping is boolean-inverted), or null if that | ||
| * current property hasn't been set either. | ||
| * | ||
| * <p>This matters for config files (e.g. {@code solr.xml}) that still contain a {@code | ||
| * ${legacyName:default}} substitution token from before a property was renamed: without this, | ||
| * setting only the new property name would silently have no effect on that token, since nothing | ||
| * else ever rewrites the token's text. See SOLR-17864. | ||
| * | ||
| * @param key the property key as originally looked up (used only for the deprecation message) | ||
| * @param dotKey {@code key} converted to dot-separated form; this is what {@code | ||
| * DEPRECATED_MAPPINGS} is keyed by | ||
| */ | ||
| private static String resolveViaDeprecatedMapping(String key, String dotKey) { | ||
| DeprecatedMapping mapping = DEPRECATED_MAPPINGS.get(dotKey); | ||
| if (mapping == null) { | ||
| return null; | ||
| } | ||
| String newValue = System.getProperty(mapping.currentName()); | ||
|
epugh marked this conversation as resolved.
Resolved
|
||
| if (newValue == null) { | ||
| return null; | ||
| } | ||
| DeprecationLog.log( | ||
| dotKey, | ||
| () -> | ||
| "A config file still references the deprecated system property " | ||
| + key | ||
| + "; it was replaced by " | ||
| + mapping.currentName() | ||
| + ". Support for the old property will be removed in a future version of Solr."); | ||
| return mapping.inverted() ? invertBooleanString(newValue) : newValue; | ||
| } | ||
|
|
||
| private static String invertBooleanString(String value) { | ||
| return String.valueOf(!Boolean.parseBoolean(value)); | ||
| } | ||
|
|
||
| private static String camelCaseToDotSeparated(String key) { | ||
|
|
@@ -218,26 +274,24 @@ static synchronized void init( | |
|
|
||
| for (String deprecatedKey : sysProperties.stringPropertyNames()) { | ||
| var dotKey = camelCaseToDotSeparated(deprecatedKey); | ||
| if (DEPRECATED_MAPPINGS.containsKey(dotKey) | ||
| || DEPRECATED_MAPPINGS.containsKey("!" + dotKey)) { | ||
| applyDeprecatedPropertyMapping(deprecatedKey, dotKey, sysProperties); | ||
| DeprecatedMapping mapping = DEPRECATED_MAPPINGS.get(dotKey); | ||
| if (mapping != null) { | ||
| applyDeprecatedPropertyMapping(deprecatedKey, mapping, sysProperties); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void applyDeprecatedPropertyMapping( | ||
| String deprecatedKey, String lookupKey, Properties sysProperties) { | ||
| var newPropName = | ||
| DEPRECATED_MAPPINGS.getOrDefault(lookupKey, DEPRECATED_MAPPINGS.get("!" + lookupKey)); | ||
| String deprecatedKey, DeprecatedMapping mapping, Properties sysProperties) { | ||
| var newValue = | ||
| DEPRECATED_MAPPINGS.containsKey(lookupKey) | ||
| ? sysProperties.getProperty(deprecatedKey) | ||
| : String.valueOf(!Boolean.parseBoolean(sysProperties.getProperty(deprecatedKey))); | ||
| mapping.inverted() | ||
| ? invertBooleanString(sysProperties.getProperty(deprecatedKey)) | ||
| : sysProperties.getProperty(deprecatedKey); | ||
| log.warn( | ||
| "Deprecated system property {} has been replaced by {}. Support for the old property will be removed in a future version of Solr.", | ||
| deprecatedKey, | ||
| newPropName); | ||
| setProperty(newPropName, newValue); | ||
| mapping.currentName()); | ||
| setProperty(mapping.currentName(), newValue); | ||
| } | ||
|
|
||
| protected static String envNameToSyspropName(String envName) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 This PR adds solr.xml referencing a deprecated system property logs a deprecation warning to
test_start_solr.bats. BATS is going away: CATS (SOLR-18642, #5323) has replaced it onmain, and SOLR-18645 (#5369) removes the.batsfiles. Please port it toStartSolrTestinsolr/packaging/src/test/org/apache/solr/shell/, next to the existingdisable.config.editdeprecation-log test. See dev-docs/cats.adoc for porting help.