Skip to content

Commit

Permalink
Script: unthrottle ingest templates #70272
Browse files Browse the repository at this point in the history
Scripts in ingest pipelines may be throttled on startup
or when beats starts up. This causes pipelines to be
stuck in a broken state.

Pipelines with many mustache templates are particularly impacted.

Removes the compilation rate limit for ingest templates by
creating a new context, `ingest_template`, with an unlimited
compilation rate limit.

Fixes: #64595
Backport: 9370b1c
  • Loading branch information
stu-elastic authored Mar 10, 2021
1 parent e869257 commit 58db0a3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public <T> T compile(

@Override
public Set<ScriptContext<?>> getSupportedContexts() {
return Collections.singleton(TemplateScript.CONTEXT);
return org.elasticsearch.common.collect.Set.of(TemplateScript.CONTEXT, TemplateScript.INGEST_CONTEXT);
}

private CustomMustacheFactory createMustacheFactory(Map<String, String> options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static ValueSource wrap(Object value, ScriptService scriptService, Map<String, S
Script script = new Script(
ScriptType.INLINE, DEFAULT_TEMPLATE_LANG, (String) value, scriptOptions, org.elasticsearch.common.collect.Map.of()
);
return new TemplatedValue(scriptService.compile(script, TemplateScript.CONTEXT));
return new TemplatedValue(scriptService.compile(script, TemplateScript.INGEST_CONTEXT));
} else {
return new ObjectValue(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ScriptModule {
SimilarityScript.CONTEXT,
SimilarityWeightScript.CONTEXT,
TemplateScript.CONTEXT,
TemplateScript.INGEST_CONTEXT,
MovingFunctionScript.CONTEXT,
ScriptedMetricAggContexts.InitScript.CONTEXT,
ScriptedMetricAggContexts.MapScript.CONTEXT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package org.elasticsearch.script;

import org.elasticsearch.common.unit.TimeValue;

import java.util.Map;

/**
Expand Down Expand Up @@ -35,4 +37,10 @@ public interface Factory {
}

public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("template", Factory.class);

// 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());
}

0 comments on commit 58db0a3

Please sign in to comment.