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

Script: Opt-out system contexts from script compilation rate limit #79459

Merged
merged 3 commits into from
Oct 19, 2021

Conversation

stu-elastic
Copy link
Contributor

@stu-elastic stu-elastic commented Oct 19, 2021

System script contexts can now opt-out of compilation rate limiting using a flag rather than a sentinel rate limit value.

Duplicated defaults for system contexts have been coalesced.

Refs: #62899

Restore the scripting general cache in preparation for deprecation of
the context cache, then removal of the context cache in master.

Brings back the general cache settings:
* `script.max_compilations_rate`
* `script.cache.expire`
* `script.cache.max_size`

`script.cache.max_size` and `script.cache.expire` are now dynamic settings.

The context cache is still default, the switch to general cache as default will happen on context cache deprecation.

System script contexts can now opt-out of compilation rate limiting using a flag rather than a sentinel rate limit value.  Duplicated defaults for system contexts have been coalesced.

Other than that, this code is the same as what was in 7.x to make a `master`-first commit and backport strategy easy.

Refs: elastic#62899
Backport: elastic#79414
@stu-elastic stu-elastic changed the title Script: Restore the scripting general cache Script: Restore the scripting general cache (backport) Oct 19, 2021
@stu-elastic stu-elastic changed the title Script: Restore the scripting general cache (backport) Script: Opt-out system contexts from script compilation rate limit Oct 19, 2021
@stu-elastic stu-elastic added :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache v7.16.0 labels Oct 19, 2021
@stu-elastic stu-elastic marked this pull request as ready for review October 19, 2021 16:11
@elasticmachine elasticmachine added the Team:Core/Infra Meta label for core/infra team label Oct 19, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@stu-elastic stu-elastic requested a review from rjernst October 19, 2021 16:11
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.

It seems like all but runtime fields have the flag backwards, it should be false to not use rate limited, right?

@@ -20,8 +18,7 @@
public static final String[] PARAMETERS = { "ctx" };

/** The context used to compile {@link IngestConditionalScript} factories. */
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("processor_conditional", Factory.class,
200, TimeValue.timeValueMillis(0), ScriptCache.UNLIMITED_COMPILATION_RATE.asTuple(), true);
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("processor_conditional", Factory.class, true);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be false to maintain not using compilation limits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's the flag for stored, there is no flag for not using compilation limits in that constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to full constructor, value is explicitly false.

@@ -21,8 +19,7 @@
public static final String[] PARAMETERS = { "ctx" };

/** The context used to compile {@link IngestScript} factories. */
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("ingest", Factory.class,
200, TimeValue.timeValueMillis(0), ScriptCache.UNLIMITED_COMPILATION_RATE.asTuple(), true);
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("ingest", Factory.class, true);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be false to maintain not using compilation limits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's calling the following constructor and allowing stored scripts.

https://github.com/elastic/elasticsearch/pull/79459/files#diff-b39b841c6481b6822b5d879ccf88d4b5b7d17c91448c2ffc27f82f0bfea7d138R120

public ScriptContext(String name, Class<FactoryType> factoryClazz, boolean allowStoredScript) {
    this(name, factoryClazz, 200, TimeValue.timeValueMillis(0), false, allowStoredScript);
    }
}

https://github.com/elastic/elasticsearch/pull/79459/files#diff-b39b841c6481b6822b5d879ccf88d4b5b7d17c91448c2ffc27f82f0bfea7d138R109

public ScriptContext(String name, Class<FactoryType> factoryClazz, int cacheSizeDefault, TimeValue cacheExpireDefault,
ScriptContext(String name, Class<FactoryType> factoryClazz, int cacheSizeDefault, TimeValue cacheExpireDefault,
              Tuple<Integer, TimeValue> maxCompilationRateDefault, boolean allowStoredScript) {
              boolean compilationRateLimited, boolean allowStoredScript)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to full constructor, value is explicitly false.

@@ -109,6 +109,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.field(Fields.COMPILATIONS, compilations);
builder.field(Fields.CACHE_EVICTIONS, cacheEvictions);
builder.field(Fields.COMPILATION_LIMIT_TRIGGERED, compilationLimitTriggered);
/* TODO(stu): master only
Copy link
Member

Choose a reason for hiding this comment

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

remove?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

@@ -41,6 +39,5 @@ public TemplateScript(Map<String, Object> params) {
// Remove compilation rate limit for ingest. Ingest pipelines may use many mustache templates, triggering compilation
// rate limiting. MustacheScriptEngine explicitly checks for TemplateScript. Rather than complicating the implementation there by
// creating a new Script class (as would be customary), this context is used to avoid the default rate limit.
public static final ScriptContext<Factory> INGEST_CONTEXT = new ScriptContext<>("ingest_template", Factory.class,
200, TimeValue.timeValueMillis(0), ScriptCache.UNLIMITED_COMPILATION_RATE.asTuple(), true);
public static final ScriptContext<Factory> INGEST_CONTEXT = new ScriptContext<>("ingest_template", Factory.class, true);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be false to maintain not using compilation limits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's for allowing it to be stored.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to full constructor, value is explicitly false.

@@ -40,6 +40,15 @@ public void testXContent() throws IOException {
stats.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();

/* TODO(stu): master only
Copy link
Member

Choose a reason for hiding this comment

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

remove?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

@@ -236,8 +235,7 @@
new ByteSizeValue(1, ByteSizeUnit.MB), new ByteSizeValue(10, ByteSizeUnit.MB), NodeScope);

public static final ScriptContext<TemplateScript.Factory> SCRIPT_TEMPLATE_CONTEXT
= new ScriptContext<>("xpack_template", TemplateScript.Factory.class,
200, TimeValue.timeValueMillis(0), ScriptCache.UNLIMITED_COMPILATION_RATE.asTuple(), true);
= new ScriptContext<>("xpack_template", TemplateScript.Factory.class, true);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be false to maintain not using compilation limits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's for allowing it to be stored.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to full constructor, value is explicitly false.

@@ -44,6 +42,5 @@ public WatcherConditionScript(Map<String, Object> params, WatchExecutionContext
WatcherConditionScript newInstance(Map<String, Object> params, WatchExecutionContext watcherContext);
}

public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("watcher_condition", Factory.class,
200, TimeValue.timeValueMillis(0), ScriptCache.UNLIMITED_COMPILATION_RATE.asTuple(), true);
public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("watcher_condition", Factory.class, true);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be false to maintain not using compilation limits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's for allowing it to be stored.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to full constructor, value is explicitly false.

@@ -45,6 +43,5 @@ public WatcherTransformScript(Map<String, Object> params, WatchExecutionContext
WatcherTransformScript newInstance(Map<String, Object> params, WatchExecutionContext watcherContext, Payload payload);
}

public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("watcher_transform", Factory.class,
200, TimeValue.timeValueMillis(0), ScriptCache.UNLIMITED_COMPILATION_RATE.asTuple(), true);
public static ScriptContext<Factory> CONTEXT = new ScriptContext<>("watcher_transform", Factory.class, true);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be false to maintain not using compilation limits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's for allowing it to be stored.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to full constructor, value is explicitly false.

* - default cache expiration: no expiration
* - unlimited compilation rate
*/
public ScriptContext(String name, Class<FactoryType> factoryClazz, boolean allowStoredScript) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need another constructor? Let's just update the few places you already have modified in this PR to be explicit about the settings for each context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this constructor to avoid having two boolean flags in a row, which (despite intellij) is a bit hard to read.

Will update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

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.

LGTM

@stu-elastic stu-elastic merged commit 8601c9b into elastic:7.x Oct 19, 2021
stu-elastic added a commit to stu-elastic/elasticsearch that referenced this pull request Oct 20, 2021
Deprecate the script context cache in favor of the general cache.

Users should use the following settings:
`script.max_compilations_rate` to set the max compilation rate
  for user scripts such as filter scripts.  Certain script contexts
  that submit scripts outside of the control of the user are
  exempted from this rate limit.  Examples include runtime fields,
  ingest and watcher.

`script.cache.max_size` to set the max size of the cache.

`script.cache.expire` to set the expiration time for entries in
the cache.

Whats deprecated?
`script.max_compilations_rate: use-context`.  This special
setting value was used to turn on the script context-specific caches.

`script.context.$CONTEXT.cache_max_size`, use `script.cache.max_size`
instead.

`script.context.$CONTEXT.cache_expire`, use `script.cache.expire`
instead.

`script.context.$CONTEXT.max_compilations_rate`, use
`script.max_compilations_rate` instead.

The default cache size was increased from `100` to `3000`, which
was approximately the max cache size when using context-specific caches.

The default compilation rate limit was increased from `75/5m` to
`150/5m` to account for increasing uses of scripts.

System script contexts can now opt-out of compilation rate limiting
using a flag rather than a sentinel rate limit value.

7.16: Script: Deprecate script context cache elastic#79508
Refs: elastic#62899

7.16: Script: Opt-out system contexts from script compilation rate limit elastic#79459
Refs: elastic#62899
stu-elastic added a commit to stu-elastic/elasticsearch that referenced this pull request Oct 20, 2021
Deprecate the script context cache in favor of the general cache.

Users should use the following settings:
`script.max_compilations_rate` to set the max compilation rate
  for user scripts such as filter scripts.  Certain script contexts
  that submit scripts outside of the control of the user are
  exempted from this rate limit.  Examples include runtime fields,
  ingest and watcher.

`script.cache.max_size` to set the max size of the cache.

`script.cache.expire` to set the expiration time for entries in
the cache.

Whats deprecated?
`script.max_compilations_rate: use-context`.  This special
setting value was used to turn on the script context-specific caches.

`script.context.$CONTEXT.cache_max_size`, use `script.cache.max_size`
instead.

`script.context.$CONTEXT.cache_expire`, use `script.cache.expire`
instead.

`script.context.$CONTEXT.max_compilations_rate`, use
`script.max_compilations_rate` instead.

The default cache size was increased from `100` to `3000`, which
was approximately the max cache size when using context-specific caches.

The default compilation rate limit was increased from `75/5m` to
`150/5m` to account for increasing uses of scripts.

System script contexts can now opt-out of compilation rate limiting
using a flag rather than a sentinel rate limit value.

7.16: Script: Deprecate script context cache elastic#79508
Refs: elastic#62899

7.16: Script: Opt-out system contexts from script compilation rate limit elastic#79459
Refs: elastic#62899
stu-elastic added a commit that referenced this pull request Oct 21, 2021
Deprecate the script context cache in favor of the general cache.

Users should use the following settings:
`script.max_compilations_rate` to set the max compilation rate
  for user scripts such as filter scripts.  Certain script contexts
  that submit scripts outside of the control of the user are
  exempted from this rate limit.  Examples include runtime fields,
  ingest and watcher.

`script.cache.max_size` to set the max size of the cache.

`script.cache.expire` to set the expiration time for entries in
the cache.

Whats deprecated?
`script.max_compilations_rate: use-context`.  This special
setting value was used to turn on the script context-specific caches.

`script.context.$CONTEXT.cache_max_size`, use `script.cache.max_size`
instead.

`script.context.$CONTEXT.cache_expire`, use `script.cache.expire`
instead.

`script.context.$CONTEXT.max_compilations_rate`, use
`script.max_compilations_rate` instead.

The default cache size was increased from `100` to `3000`, which
was approximately the max cache size when using context-specific caches.

The default compilation rate limit was increased from `75/5m` to
`150/5m` to account for increasing uses of scripts.

System script contexts can now opt-out of compilation rate limiting
using a flag rather than a sentinel rate limit value.

7.16: Script: Deprecate script context cache #79508
Refs: #62899

7.16: Script: Opt-out system contexts from script compilation rate limit #79459
Refs: #62899
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 >non-issue Team:Core/Infra Meta label for core/infra team v7.16.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants