Skip to content
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

Add guard against null-valued settings #17310

Merged
merged 4 commits into from
Mar 24, 2016
Merged

Add guard against null-valued settings #17310

merged 4 commits into from
Mar 24, 2016

Conversation

jasontedor
Copy link
Member

This commit adds a guard against null-valued settings that are loaded
from yaml or json settings files, and also adds a test that ensures
the same remains true for settings loaded from properties files.

Relates #17292

@jasontedor jasontedor added review :Core/Infra/Settings Settings infrastructure and APIs v5.0.0-alpha1 labels Mar 24, 2016
@jasontedor
Copy link
Member Author

This gives more user-friendly error messages like:

Exception in thread "main" SettingsException[Failed to load settings from [elasticsearch.yml]]; nested: ElasticsearchParseException[null-valued setting found for key [cluster.name] found at line number [1], column number [14]];
Likely root cause: ElasticsearchParseException[null-valued setting found for key [cluster.name] found at line number [1], column number [14]]

instead of

Exception in thread "main" java.lang.NullPointerException: value can not be null for [cluster.name]

Note that prior to #17292, the error message here was even worse:

Exception in thread "main" java.lang.NullPointerException: Argument 'value' must not be null.

This commit adds a guard against null-valued settings that are loaded
from yaml or json settings files, and also adds a test that ensures
the same remains true for settings loaded from properties files.
@rjernst
Copy link
Member

rjernst commented Mar 24, 2016

LGTM. I would just call the boolean "allowNullValues" though.

@@ -27,6 +27,10 @@
*/
public class JsonSettingsLoader extends XContentSettingsLoader {

public JsonSettingsLoader(boolean guardAgainstNullValuedSettings) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any place we want this to be false? just wondering if we just make it default behavior...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, we do ... never mind :(

@bleskes
Copy link
Contributor

bleskes commented Mar 24, 2016

nice. Happy we found a better name for the exception than the dreaded NPE

The sole constructor of XContentSettingsLoader accepts a boolean flag
that indicates whether or not null values parsed from the source should
be rejected or not. Previously a true value for this flag meant that
null values should be rejected. With this commit, the meaning of this
flag is reversed so that a true value means that null values should be
accepted (note that this is needed due to the way that settings are
unset via the cluster update settings API). The name of this flag has
been changed from guardAgainstNullValuedSettings to allowNullValues, for
clarity.
@jasontedor jasontedor merged commit bb364cc into elastic:master Mar 24, 2016
@jasontedor jasontedor deleted the null-valued-settings branch March 24, 2016 11:53
* accept null-valued keys.
*
* @param source The underlying settings content.
* @return A settings loader.
*/
public static SettingsLoader loaderFromSource(String source) {
if (source.indexOf('{') != -1 && source.indexOf('}') != -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This detection feels pretty similar to XContentFactory#xContentType(CharSequence). Maybe we can use that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nik9000 I have a code cleanup PR in the works related to this; I'll investigate that change in the context of that PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we should do this; the YAML detection there is different and I'm hesitant to change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with that. It'd help me the next time I look at this code if you add a comment explaining why you don't think this should share the detection logic. Or else I'm going to think the same thing all over again and waste time coming to the same conclusions you have. Or worse, I won't see why you didn't want to merge them and I'll break something.

@jasontedor
Copy link
Member Author

LGTM. I would just call the boolean "allowNullValues" though.

I agree that that is better and I pushed 4d27328.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants