Make script service a cluster state listener
Original commit: elastic/x-pack-elasticsearch@dc763f3d66
This commit is contained in:
parent
7e0e9d858d
commit
2d0055de35
|
@ -39,11 +39,8 @@ public final class MessyTestUtils {
|
||||||
ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singleton(groovyScriptEngineService));
|
ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singleton(groovyScriptEngineService));
|
||||||
ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Arrays.asList(ScriptServiceProxy.INSTANCE));
|
ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Arrays.asList(ScriptServiceProxy.INSTANCE));
|
||||||
|
|
||||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
|
||||||
Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build());
|
|
||||||
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
|
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
|
||||||
return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings),
|
return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings),
|
||||||
new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings),
|
new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings));
|
||||||
clusterService);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -362,7 +362,7 @@ public class SearchInputIT extends ESIntegTestCase {
|
||||||
protected WatcherSearchTemplateService watcherSearchTemplateService() {
|
protected WatcherSearchTemplateService watcherSearchTemplateService() {
|
||||||
String master = internalCluster().getMasterName();
|
String master = internalCluster().getMasterName();
|
||||||
return new WatcherSearchTemplateService(internalCluster().clusterService(master).getSettings(),
|
return new WatcherSearchTemplateService(internalCluster().clusterService(master).getSettings(),
|
||||||
ScriptServiceProxy.of(internalCluster().getInstance(ScriptService.class, master), internalCluster().clusterService(master)),
|
ScriptServiceProxy.of(internalCluster().getInstance(ScriptService.class, master)),
|
||||||
internalCluster().getInstance(IndicesQueriesRegistry.class, master),
|
internalCluster().getInstance(IndicesQueriesRegistry.class, master),
|
||||||
internalCluster().getInstance(AggregatorParsers.class, master),
|
internalCluster().getInstance(AggregatorParsers.class, master),
|
||||||
internalCluster().getInstance(Suggesters.class, master)
|
internalCluster().getInstance(Suggesters.class, master)
|
||||||
|
@ -370,7 +370,7 @@ public class SearchInputIT extends ESIntegTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ScriptServiceProxy scriptService() {
|
protected ScriptServiceProxy scriptService() {
|
||||||
return ScriptServiceProxy.of(internalCluster().getInstance(ScriptService.class), internalCluster().clusterService());
|
return ScriptServiceProxy.of(internalCluster().getInstance(ScriptService.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
private XContentSource toXContentSource(SearchInput.Result result) throws IOException {
|
private XContentSource toXContentSource(SearchInput.Result result) throws IOException {
|
||||||
|
|
|
@ -519,7 +519,7 @@ public class SearchTransformIT extends ESIntegTestCase {
|
||||||
protected WatcherSearchTemplateService watcherSearchTemplateService() {
|
protected WatcherSearchTemplateService watcherSearchTemplateService() {
|
||||||
String master = internalCluster().getMasterName();
|
String master = internalCluster().getMasterName();
|
||||||
return new WatcherSearchTemplateService(internalCluster().clusterService(master).getSettings(),
|
return new WatcherSearchTemplateService(internalCluster().clusterService(master).getSettings(),
|
||||||
ScriptServiceProxy.of(internalCluster().getInstance(ScriptService.class, master), internalCluster().clusterService(master)),
|
ScriptServiceProxy.of(internalCluster().getInstance(ScriptService.class, master)),
|
||||||
internalCluster().getInstance(IndicesQueriesRegistry.class, master),
|
internalCluster().getInstance(IndicesQueriesRegistry.class, master),
|
||||||
internalCluster().getInstance(AggregatorParsers.class, master),
|
internalCluster().getInstance(AggregatorParsers.class, master),
|
||||||
internalCluster().getInstance(Suggesters.class, master)
|
internalCluster().getInstance(Suggesters.class, master)
|
||||||
|
@ -527,7 +527,7 @@ public class SearchTransformIT extends ESIntegTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ScriptServiceProxy scriptService() {
|
protected ScriptServiceProxy scriptService() {
|
||||||
return ScriptServiceProxy.of(internalCluster().getInstance(ScriptService.class), internalCluster().clusterService());
|
return ScriptServiceProxy.of(internalCluster().getInstance(ScriptService.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<String, Object> doc(String date, String value) {
|
private static Map<String, Object> doc(String date, String value) {
|
||||||
|
|
|
@ -58,9 +58,7 @@ public class WatcherTemplateTests extends ESTestCase {
|
||||||
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, registry);
|
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, registry);
|
||||||
ScriptService scriptService = new ScriptService(setting, environment, resourceWatcherService, scriptEngineRegistry,
|
ScriptService scriptService = new ScriptService(setting, environment, resourceWatcherService, scriptEngineRegistry,
|
||||||
registry, scriptSettings);
|
registry, scriptSettings);
|
||||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
engine = new DefaultTextTemplateEngine(Settings.EMPTY, ScriptServiceProxy.of(scriptService));
|
||||||
Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build());
|
|
||||||
engine = new DefaultTextTemplateEngine(Settings.EMPTY, ScriptServiceProxy.of(scriptService, clusterService));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEscaping() throws Exception {
|
public void testEscaping() throws Exception {
|
||||||
|
|
|
@ -26,13 +26,11 @@ public class ScriptServiceProxy {
|
||||||
|
|
||||||
private final ScriptService service;
|
private final ScriptService service;
|
||||||
private final SecurityContext securityContext;
|
private final SecurityContext securityContext;
|
||||||
private final ClusterService clusterService;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ScriptServiceProxy(ScriptService service, SecurityContext securityContext, ClusterService clusterService) {
|
public ScriptServiceProxy(ScriptService service, SecurityContext securityContext) {
|
||||||
this.service = service;
|
this.service = service;
|
||||||
this.securityContext = securityContext;
|
this.securityContext = securityContext;
|
||||||
this.clusterService = clusterService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public CompiledScript compile(Script script) {
|
public CompiledScript compile(Script script) {
|
||||||
|
@ -41,7 +39,7 @@ public class ScriptServiceProxy {
|
||||||
|
|
||||||
public CompiledScript compile(org.elasticsearch.script.Script script, Map<String, String> compileParams) {
|
public CompiledScript compile(org.elasticsearch.script.Script script, Map<String, String> compileParams) {
|
||||||
return securityContext.executeAs(XPackUser.INSTANCE, () ->
|
return securityContext.executeAs(XPackUser.INSTANCE, () ->
|
||||||
service.compile(script, WatcherScriptContext.CTX, compileParams, clusterService.state()));
|
service.compile(script, WatcherScriptContext.CTX, compileParams));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> vars) {
|
public ExecutableScript executable(CompiledScript compiledScript, Map<String, Object> vars) {
|
||||||
|
@ -64,7 +62,7 @@ public class ScriptServiceProxy {
|
||||||
/**
|
/**
|
||||||
* Factory helper method for testing.
|
* Factory helper method for testing.
|
||||||
*/
|
*/
|
||||||
public static ScriptServiceProxy of(ScriptService service, ClusterService clusterService) {
|
public static ScriptServiceProxy of(ScriptService service) {
|
||||||
return new ScriptServiceProxy(service, SecurityContext.Insecure.INSTANCE, clusterService);
|
return new ScriptServiceProxy(service, SecurityContext.Insecure.INSTANCE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -256,11 +256,8 @@ public final class WatcherTestUtils {
|
||||||
ScriptEngineRegistry scriptEngineRegistry =
|
ScriptEngineRegistry scriptEngineRegistry =
|
||||||
new ScriptEngineRegistry(Collections.emptyList());
|
new ScriptEngineRegistry(Collections.emptyList());
|
||||||
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
|
ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
|
||||||
ClusterService clusterService = Mockito.mock(ClusterService.class);
|
|
||||||
Mockito.when(clusterService.state()).thenReturn(ClusterState.builder(new ClusterName("_name")).build());
|
|
||||||
return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings),
|
return ScriptServiceProxy.of(new ScriptService(settings, new Environment(settings),
|
||||||
new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings),
|
new ResourceWatcherService(settings, tp), scriptEngineRegistry, scriptContextRegistry, scriptSettings));
|
||||||
clusterService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SearchType getRandomSupportedSearchType() {
|
public static SearchType getRandomSupportedSearchType() {
|
||||||
|
@ -270,12 +267,4 @@ public final class WatcherTestUtils {
|
||||||
SearchType.DFS_QUERY_THEN_FETCH,
|
SearchType.DFS_QUERY_THEN_FETCH,
|
||||||
SearchType.DFS_QUERY_AND_FETCH);
|
SearchType.DFS_QUERY_AND_FETCH);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean areJsonEquivalent(String json1, String json2) throws IOException {
|
|
||||||
XContentParser parser1 = XContentHelper.createParser(new BytesArray(json1.getBytes(StandardCharsets.UTF_8)));
|
|
||||||
XContentParser parser2 = XContentHelper.createParser(new BytesArray(json2.getBytes(StandardCharsets.UTF_8)));
|
|
||||||
Map<String, Object> map1 = parser1.map();
|
|
||||||
Map<String, Object> map2 = parser2.map();
|
|
||||||
return map1.equals(map2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue