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

Scripting: Replace Update Context #32096

Merged

Conversation

original-brownbear
Copy link
Member

Same as #32068 and #32003 (fully resolves the todo together with #32068 ).

One notable point would be the change in behaviour this introduces and I'm not 100% sure about:

Previously the params included ctx under params.ctx because of the way org.elasticsearch.painless.ScriptImpl worked (using the params to provide the ctx indirectly).
With this change, making ctx and actual parameter this wasn't necessary anymore and I didn't add any logic that puts ctx into params here (seemed wrong to me since it being there was just a side effect?).

This required a change to this IT:
See https://github.com/elastic/elasticsearch/compare/master...original-brownbear:replace-update-context?expand=1#diff-585f6613de46bec6547c7b2aed89db8bR135

The test forced a circular dependency in a kind of complicated way because the for loop would hit the key ctx and then put ctx to ctx.ctx. This doesn't happen anymore with the change here so I just directly forced that circular dependency.

@original-brownbear original-brownbear added the :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache label Jul 16, 2018
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

@original-brownbear
Copy link
Member Author

@rjernst can you take a look here when you have some time?

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

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

Overall this looks ok. We need to maintain backcompat, though, in 6.x, with having ctx available in params. The backcompat should be controlled by a sysprop and have a deprecation warning, as we have done in other similar PRs (look at scripted metric agg as an example). I would also like us to look at possibly having something other than Map, and a different name than ctx long term. The latter can happen as followups, but I want to bring it up as I think it is part of the overall work for making all script uses context aware.

@@ -132,7 +132,7 @@
body:
script:
lang: painless
source: "for (def key : params.keySet()) { ctx._source[key] = params[key]}"
source: "ctx._source.ctx = ctx"
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this creating a cycle in _source?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Ah, thanks, sorry I missed that.

@original-brownbear
Copy link
Member Author

@rjernst sorry missed your other comment last night. So in general are we good with this for master/7.0 and do the property + backwards compatibility things in the back port to 6.x only or do we add the backwards compatibility here as well?

@rjernst
Copy link
Member

rjernst commented Aug 3, 2018

@original-brownbear Typically we would add the backcompat/property to the first PR which would go into master and be backported to 6.x, then remove the backcompat in master in a followup.

@original-brownbear
Copy link
Member Author

@rjernst added a property that enables the old behaviour now. Wasn't quite sure how to exactly set it in the initial PR.
Defaulted it to the old behaviour (which would also cause deprecation logging by default). Is that correct?

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

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

This looks good. A few final notes.

@@ -40,6 +40,7 @@ integTestCluster {

// TODO: remove this for 7.0, this exists to allow the doc examples in 6.x to continue using the defaults
systemProperty 'es.scripting.use_java_time', 'false'
systemProperty 'es.scripting.update.ctx_in_params', 'false'
Copy link
Member

Choose a reason for hiding this comment

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

Since this is set in BuildPlugin, it shouldn't be necessary here or anywhere else?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right my bad that was just a random leftover from other experiments. Removing

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah no I needed this because it's in the integration test definition not the unit test definition that is inherited.

Copy link
Member

Choose a reason for hiding this comment

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

Hrm, are you sure? BuildPlugin.commonTestConfig should be shared by both

Copy link
Member Author

Choose a reason for hiding this comment

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

@rjernst yea def. I even debugged this locally and it failed like on Jenkins with the deprecation line.

ExecutableScript.Factory factory = params -> executableScript;
when(scriptService.compile(any(), eq(ExecutableScript.CONTEXT))).thenReturn(factory);
when(scriptService.compile(any(), eq(ExecutableScript.UPDATE_CONTEXT))).thenReturn(factory);
UpdateScript executableScript = new UpdateScript(Collections.emptyMap()) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: please update the variable names, as these are no longer ExecutableScript

final Map<String, Object> params;
if (CTX_IN_PARAMS) {
params = new HashMap<>();
params.putAll(script.getParams());
Copy link
Member

Choose a reason for hiding this comment

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

You could use the ctor that takes an existing Map to copy.

params = new HashMap<>();
params.putAll(script.getParams());
params.put(ContextFields.CTX, ctx);
deprecationLogger.deprecated("Exposing `ctx` under `params.ctx` is deprecated. " +
Copy link
Member

Choose a reason for hiding this comment

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

nit: "Exposing ctx under" -> "Using ctx via"

Also, I would use "to enforce the non-deprecated usage" instead of "to disable this behavior"

@original-brownbear
Copy link
Member Author

@rjernst thanks!, fixed all the points with the requested changes -> merging :)

@original-brownbear original-brownbear merged commit 79375d3 into elastic:master Aug 9, 2018
@original-brownbear original-brownbear deleted the replace-update-context branch August 9, 2018 12:32
original-brownbear added a commit to original-brownbear/elasticsearch that referenced this pull request Aug 9, 2018
* SCRIPTING: Move Update Scripts to their own context
* Added system property for backwards compatibility of change to `ctx.params`
original-brownbear added a commit that referenced this pull request Aug 12, 2018
* SCRIPTING: Move Update Scripts to their own context
* Added system property for backwards compatibility of change to `ctx.params`
lipsill added a commit to lipsill/elasticsearch that referenced this pull request Sep 24, 2018
nik9000 pushed a commit that referenced this pull request Oct 16, 2018
* Replace deprecated field `code` with `source` for stored scripts (#25127)
* Replace examples using the deprecated endpoint `{index}/{type}/_search`
with `{index}/_search` (#29468)
* Use a system property to avoid deprecation warnings after the Update
Scripts have been moved to their own context (#32096)
nik9000 pushed a commit that referenced this pull request Oct 16, 2018
* Replace deprecated field `code` with `source` for stored scripts (#25127)
* Replace examples using the deprecated endpoint `{index}/{type}/_search`
with `{index}/_search` (#29468)
* Use a system property to avoid deprecation warnings after the Update
Scripts have been moved to their own context (#32096)
kcm pushed a commit that referenced this pull request Oct 30, 2018
* Replace deprecated field `code` with `source` for stored scripts (#25127)
* Replace examples using the deprecated endpoint `{index}/{type}/_search`
with `{index}/_search` (#29468)
* Use a system property to avoid deprecation warnings after the Update
Scripts have been moved to their own context (#32096)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >refactoring v6.5.0 v7.0.0-beta1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants