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

Parameterizing Painless script #9821

Closed
1 task
ruflin opened this issue Dec 28, 2018 · 10 comments
Closed
1 task

Parameterizing Painless script #9821

ruflin opened this issue Dec 28, 2018 · 10 comments
Labels
Filebeat Filebeat module Stalled Team:Integrations Label for the Integrations team

Comments

@ruflin
Copy link
Contributor

ruflin commented Dec 28, 2018

Filebeat modules use ingest pipelines with scripts inside. Parameterised scripts are preferred and more efficent as they only have to be compiled ones (see https://www.elastic.co/guide/en/elasticsearch/reference/6.x/modules-scripting-using.html#prefer-params). To have efficient pipelines in all modules each pipeline should be reviewed to assure we only use parameterised scripts.

Related:

@jsoriano
Copy link
Member

jsoriano commented Dec 31, 2018

I don't fully understand the example in the docs:

If you need to pass variables into the script, you should pass them in as named params instead of hard-coding values into the script itself. For example, if you want to be able to multiply a field value by different multipliers, don’t hard-code the multiplier into the script:

 "source": "doc['my_field'] * 2"

Instead, pass it in as a named parameter:

  "source": "doc['my_field'] * multiplier",
  "params": {
    "multiplier": 2
  }

The first version has to be recompiled every time the multiplier changes. The second version is only compiled once.

In this case the multiplier is never going to change, it is always going to be 2, why would it be recompiled?

@kaiyan-sheng
Copy link
Contributor

@jsoriano Seems like scripts has params will by default be only compiled once. Found this here: https://qbox.io/blog/advanced-methods-painless-scripting-elasticsearch-syntax-params

@jsoriano
Copy link
Member

jsoriano commented Jan 4, 2019

@kaiyan-sheng yes, this is what I don't understand, why is params required even for values that don't change.

@ruflin
Copy link
Contributor Author

ruflin commented Jan 7, 2019

I think we need @jakelandis to comment on this one. I'm also curious why we need to parametrise constants.

@jakelandis
Copy link

jakelandis commented Jan 7, 2019

You don't need to parameterize constants for ingest node scripts (unless you are constantly changing the pipeline, which you don't want to do for other reasons).

You may want to parameterize if your testing uses randomized constants but not needed for production code. Parameterize the arguments is imo and arguably a best practice for consistency with update scripts.

I don't fully understand the example in the docs

The keyword in there is "different multipliers". The script's cache keys are basically by the script itself as a string, so if that multiplier keeps changing, the string changes and it is cache miss. This maybe a concern for update/upsert scripts but not really for ingest node scripts.

The recent increase in circuit breaker exceptions is likely due to adding support for more template'd fields (starting in '6.4') and the 'if' condition in '6.5'. Once elastic/elasticsearch#37120 is fixed, it should help considerably with the circuit breaker exceptions.

(Also, somewhere, can't find it now, I provided a less concrete answer to a similar question... sorry about that. I have looked at this abit more since and ^^ is the correct answer).

@jsoriano
Copy link
Member

jsoriano commented Jan 8, 2019

@jakelandis thanks for the explanation. Let's see if I understand it now...

So, parametrization is only needed if the script is used with different multipliers. Then, following with the example, recompilations are triggered if there are scripts like these ones:

 "source": "doc['my_field'] * 2"

And

 "source": "doc['my_field'] * 10"

But not if they are defined like:

  "source": "doc['my_field'] * multiplier",
  "params": {
    "multiplier": 2
  }

And

  "source": "doc['my_field'] * multiplier",
  "params": {
    "multiplier": 10
  }

This makes sense.

But if the script is always used as "source": "doc['my_field'] * 2", then recompilations are not triggered because the multiplier (and the script) is always the same. Is this correct?

But then I don't understand why we need to parametrize literals that are never different, like in these cases.

Or maybe we don't need to parametrize so many things? To check recompilations, is there some metric exposing the count of compilations done?

@webmat
Copy link
Contributor

webmat commented Jan 8, 2019

@jsoriano But the real issue here is that all options that can be templated currently trigger a compilation, even if there's no Mustache interpolation in the value. This is what elastic/elasticsearch#37120 will address.

The reason we're seeing this problem on and off is likely that we're hovering close to that limit. Sometimes we trip it up, sometimes we don't. Our attempts at parameterizing just mix things up, they don't really fix anything.

But if you think about all of the processor options that support templating where we don't actually do templating most of the time... I think ES issue will be the game changer that brings everything back to a sane level.

@botelastic
Copy link

botelastic bot commented Jul 8, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@botelastic botelastic bot added the Stalled label Jul 8, 2020
@jsoriano
Copy link
Member

jsoriano commented Jul 8, 2020

The fix for elastic/elasticsearch#37120 was already merged. I guess we can close this then?

@botelastic botelastic bot removed the Stalled label Jul 8, 2020
@botelastic
Copy link

botelastic bot commented Jun 8, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@botelastic botelastic bot added the Stalled label Jun 8, 2021
@botelastic botelastic bot closed this as completed Jul 8, 2021
@zube zube bot removed the [zube]: Done label Oct 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Filebeat Filebeat module Stalled Team:Integrations Label for the Integrations team
Projects
None yet
Development

No branches or pull requests

6 participants