Skip to content

Commit

Permalink
Script: no compile rate limit for ingest templates (elastic#69841)
Browse files Browse the repository at this point in the history
Remove the compilation rate limit for ingest templates.

Creates a new context, `ingest_template`, with an unlimited
compilation rate limit.

The `template` context is used in many places so it cannot be
exempted from rate limits.

Fixes: elastic#64595
Backport: 9370b1c
  • Loading branch information
stu-elastic committed Mar 4, 2021
1 parent 02cc01d commit eb9cc00
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 @@ -53,6 +53,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 eb9cc00

Please sign in to comment.