Script: Convert uses of CompiledTemplate to TemplateScript (elastic/x-pack-elasticsearch#1630)

This is the xpack side of elastic/elasticsearch#25032

Original commit: elastic/x-pack-elasticsearch@ba7df4f6ce
This commit is contained in:
Ryan Ernst 2017-06-02 13:41:33 -07:00 committed by GitHub
parent f90cfa9afb
commit 7ee8eccf95
12 changed files with 33 additions and 34 deletions

View File

@ -336,7 +336,7 @@ public class XPackPlugin extends Plugin implements ScriptPlugin, ActionPlugin, I
@Override
public List<ScriptContext> getContexts() {
return Arrays.asList(Watcher.SCRIPT_SEARCH_CONTEXT, Watcher.SCRIPT_EXECUTABLE_CONTEXT);
return Arrays.asList(Watcher.SCRIPT_SEARCH_CONTEXT, Watcher.SCRIPT_EXECUTABLE_CONTEXT, Watcher.SCRIPT_TEMPLATE_CONTEXT);
}
@Override

View File

@ -11,7 +11,7 @@ import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.template.CompiledTemplate;
import org.elasticsearch.script.TemplateScript;
import org.elasticsearch.xpack.watcher.Watcher;
import java.util.HashMap;
@ -52,8 +52,8 @@ public class TextTemplateEngine extends AbstractComponent {
options.put(Script.CONTENT_TYPE_OPTION, mediaType);
}
Script script = new Script(textTemplate.getType(), "mustache", template, options, mergedModel);
CompiledTemplate compiledTemplate = service.compileTemplate(script, Watcher.SCRIPT_EXECUTABLE_CONTEXT);
return compiledTemplate.run(model);
TemplateScript.Factory compiledTemplate = service.compile(script, Watcher.SCRIPT_TEMPLATE_CONTEXT);
return compiledTemplate.newInstance(model).execute();
}
private String trimContentType(TextTemplate textTemplate) {

View File

@ -56,11 +56,9 @@ import org.elasticsearch.index.shard.IndexSearcherWrapper;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.shard.ShardUtils;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.template.CompiledTemplate;
import org.elasticsearch.script.TemplateScript;
import org.elasticsearch.xpack.security.authc.Authentication;
import org.elasticsearch.xpack.security.authz.AuthorizationService;
import org.elasticsearch.xpack.security.authz.accesscontrol.DocumentSubsetReader.DocumentSubsetDirectoryReader;
@ -275,8 +273,8 @@ public class SecurityIndexSearcherWrapper extends IndexSearcherWrapper {
params.put("_user", userModel);
// Always enforce mustache script lang:
script = new Script(script.getType(), "mustache", script.getIdOrCode(), script.getOptions(), params);
CompiledTemplate compiledTemplate = scriptService.compileTemplate(script, ExecutableScript.CONTEXT);
return compiledTemplate.run(script.getParams());
TemplateScript compiledTemplate = scriptService.compile(script, TemplateScript.CONTEXT).newInstance(script.getParams());
return compiledTemplate.execute();
} else {
return querySource;
}

View File

@ -40,6 +40,7 @@ import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.SearchScript;
import org.elasticsearch.script.TemplateScript;
import org.elasticsearch.threadpool.ExecutorBuilder;
import org.elasticsearch.threadpool.FixedExecutorBuilder;
import org.elasticsearch.threadpool.ThreadPool;
@ -185,6 +186,8 @@ public class Watcher implements ActionPlugin {
// TODO: remove this context when each xpack script use case has their own contexts
public static final ScriptContext<ExecutableScript.Factory> SCRIPT_EXECUTABLE_CONTEXT
= new ScriptContext<>("xpack_executable", ExecutableScript.Factory.class);
public static final ScriptContext<TemplateScript.Factory> SCRIPT_TEMPLATE_CONTEXT
= new ScriptContext<>("xpack_template", TemplateScript.Factory.class);
private static final Logger logger = Loggers.getLogger(Watcher.class);
private WatcherIndexingListener listener;

View File

@ -15,8 +15,8 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.TemplateScript;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.template.CompiledTemplate;
import org.elasticsearch.xpack.watcher.Watcher;
import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext;
import org.elasticsearch.xpack.watcher.support.Variables;
@ -52,8 +52,8 @@ public class WatcherSearchTemplateService extends AbstractComponent {
}
// Templates are always of lang mustache:
Script template = new Script(source.getType(), "mustache", source.getIdOrCode(), source.getOptions(), watcherContextParams);
CompiledTemplate compiledTemplate = scriptService.compileTemplate(template, Watcher.SCRIPT_EXECUTABLE_CONTEXT);
return compiledTemplate.run(template.getParams());
TemplateScript.Factory compiledTemplate = scriptService.compile(template, Watcher.SCRIPT_TEMPLATE_CONTEXT);
return compiledTemplate.newInstance(template.getParams()).execute();
}
public SearchRequest toSearchRequest(WatcherSearchTemplateRequest request) throws IOException {

View File

@ -44,9 +44,9 @@ public class MockMustacheScriptEngine extends MockScriptEngine {
if (script.contains("{{") && script.contains("}}")) {
throw new IllegalArgumentException("Fix your test to not rely on mustache");
}
if (context.instanceClazz.equals(ExecutableScript.class) == false) {
if (context.instanceClazz.equals(TemplateScript.class) == false) {
throw new IllegalArgumentException("mock mustache only understands template scripts, not [" + context.name + "]");
}
return context.factoryClazz.cast((ExecutableScript.Factory) vars -> new MockExecutableScript(vars, p -> script));
return context.factoryClazz.cast((TemplateScript.Factory) vars -> () -> script);
}
}

View File

@ -13,7 +13,7 @@ import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.template.CompiledTemplate;
import org.elasticsearch.script.TemplateScript;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.watcher.Watcher;
import org.junit.Before;
@ -54,10 +54,10 @@ public class TextTemplateTests extends ESTestCase {
merged = unmodifiableMap(merged);
ScriptType type = randomFrom(ScriptType.values());
CompiledTemplate compiledTemplate = templateParams -> "rendered_text";
when(service.compileTemplate(new Script(type, lang, templateText,
TemplateScript.Factory compiledTemplate = templateParams -> () -> "rendered_text";
when(service.compile(new Script(type, lang, templateText,
type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null,
merged), Watcher.SCRIPT_EXECUTABLE_CONTEXT)).thenReturn(compiledTemplate);
merged), Watcher.SCRIPT_TEMPLATE_CONTEXT)).thenReturn(compiledTemplate);
TextTemplate template = templateBuilder(type, templateText, params);
assertThat(engine.render(template, model), is("rendered_text"));
@ -69,10 +69,10 @@ public class TextTemplateTests extends ESTestCase {
Map<String, Object> model = singletonMap("key", "model_val");
ScriptType type = randomFrom(ScriptType.values());
CompiledTemplate compiledTemplate = templateParams -> "rendered_text";
when(service.compileTemplate(new Script(type, lang, templateText,
TemplateScript.Factory compiledTemplate = templateParams -> () -> "rendered_text";
when(service.compile(new Script(type, lang, templateText,
type == ScriptType.INLINE ? Collections.singletonMap("content_type", "text/plain") : null,
model), Watcher.SCRIPT_EXECUTABLE_CONTEXT)).thenReturn(compiledTemplate);
model), Watcher.SCRIPT_TEMPLATE_CONTEXT)).thenReturn(compiledTemplate);
TextTemplate template = templateBuilder(type, templateText, params);
assertThat(engine.render(template, model), is("rendered_text"));
@ -82,9 +82,9 @@ public class TextTemplateTests extends ESTestCase {
String templateText = "_template";
Map<String, Object> model = singletonMap("key", "model_val");
CompiledTemplate compiledTemplate = templateParams -> "rendered_text";
when(service.compileTemplate(new Script(ScriptType.INLINE, lang, templateText,
Collections.singletonMap("content_type", "text/plain"), model), Watcher.SCRIPT_EXECUTABLE_CONTEXT))
TemplateScript.Factory compiledTemplate = templateParams -> () -> "rendered_text";
when(service.compile(new Script(ScriptType.INLINE, lang, templateText,
Collections.singletonMap("content_type", "text/plain"), model), Watcher.SCRIPT_TEMPLATE_CONTEXT))
.thenReturn(compiledTemplate);
TextTemplate template = new TextTemplate(templateText);

View File

@ -37,7 +37,6 @@ import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.SparseFixedBitSet;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
@ -68,13 +67,11 @@ import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.indices.TermsLookup;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.TemplateScript;
import org.elasticsearch.search.aggregations.LeafBucketCollector;
import org.elasticsearch.template.CompiledTemplate;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.IndexSettingsModule;
import org.elasticsearch.xpack.security.authz.accesscontrol.DocumentSubsetReader.DocumentSubsetDirectoryReader;
@ -458,8 +455,8 @@ public class SecurityIndexSearcherWrapperUnitTests extends ESTestCase {
}
};
CompiledTemplate compiledScript = mock(CompiledTemplate.class);
when(scriptService.compileTemplate(any(Script.class), eq(ExecutableScript.CONTEXT))).thenReturn(compiledScript);
TemplateScript.Factory compiledTemplate = params -> () -> "rendered_text";
when(scriptService.compile(any(Script.class), eq(TemplateScript.CONTEXT))).thenReturn(compiledTemplate);
XContentBuilder builder = jsonBuilder();
String query = new TermQueryBuilder("field", "{{_user.username}}").toXContent(builder, ToXContent.EMPTY_PARAMS).string();
@ -470,7 +467,7 @@ public class SecurityIndexSearcherWrapperUnitTests extends ESTestCase {
securityIndexSearcherWrapper.evaluateTemplate(querySource);
ArgumentCaptor<Script> argument = ArgumentCaptor.forClass(Script.class);
verify(scriptService).compileTemplate(argument.capture(), eq(ExecutableScript.CONTEXT));
verify(scriptService).compile(argument.capture(), eq(TemplateScript.CONTEXT));
Script usedScript = argument.getValue();
assertThat(usedScript.getIdOrCode(), equalTo(script.getIdOrCode()));
assertThat(usedScript.getType(), equalTo(script.getType()));

View File

@ -237,6 +237,7 @@ public final class WatcherTestUtils {
.build();
Map<String, ScriptContext> contexts = new HashMap<>(ScriptModule.CORE_CONTEXTS);
contexts.put(Watcher.SCRIPT_EXECUTABLE_CONTEXT.name, Watcher.SCRIPT_EXECUTABLE_CONTEXT);
contexts.put(Watcher.SCRIPT_TEMPLATE_CONTEXT.name, Watcher.SCRIPT_TEMPLATE_CONTEXT);
return new ScriptService(settings, Collections.emptyMap(), Collections.emptyMap());
}

View File

@ -173,7 +173,7 @@ public class SearchInputTests extends ESIntegTestCase {
@Override
public List<ScriptContext> getContexts() {
return Collections.singletonList(Watcher.SCRIPT_EXECUTABLE_CONTEXT);
return Collections.singletonList(Watcher.SCRIPT_TEMPLATE_CONTEXT);
}
}
}

View File

@ -299,7 +299,7 @@ public class SearchTransformTests extends ESIntegTestCase {
@Override
public List<ScriptContext> getContexts() {
return Collections.singletonList(Watcher.SCRIPT_EXECUTABLE_CONTEXT);
return Collections.singletonList(Watcher.SCRIPT_TEMPLATE_CONTEXT);
}
}
}

View File

@ -40,7 +40,7 @@ public class WatcherTemplateIT extends ESTestCase {
MustacheScriptEngine engine = new MustacheScriptEngine();
Map<String, ScriptEngine> engines = Collections.singletonMap(engine.getType(), engine);
Map<String, ScriptContext<?>> contexts =
Collections.singletonMap(Watcher.SCRIPT_EXECUTABLE_CONTEXT.name, Watcher.SCRIPT_EXECUTABLE_CONTEXT);
Collections.singletonMap(Watcher.SCRIPT_TEMPLATE_CONTEXT.name, Watcher.SCRIPT_TEMPLATE_CONTEXT);
ScriptService scriptService = new ScriptService(Settings.EMPTY, engines, contexts);
textTemplateEngine = new TextTemplateEngine(Settings.EMPTY, scriptService);
}