Search in sources :

Example 1 with ResourceWatcherService

use of org.elasticsearch.watcher.ResourceWatcherService in project elasticsearch by elastic.

the class ScriptServiceTests method setup.

@Before
public void setup() throws IOException {
    Path genericConfigFolder = createTempDir();
    baseSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(Environment.PATH_CONF_SETTING.getKey(), genericConfigFolder).put(ScriptService.SCRIPT_MAX_COMPILATIONS_PER_MINUTE.getKey(), 10000).build();
    resourceWatcherService = new ResourceWatcherService(baseSettings, null);
    scriptEngineService = new TestEngineService();
    dangerousScriptEngineService = new TestDangerousEngineService();
    TestEngineService defaultScriptServiceEngine = new TestEngineService(Script.DEFAULT_SCRIPT_LANG) {
    };
    scriptEnginesByLangMap = ScriptModesTests.buildScriptEnginesByLangMap(new HashSet<>(Arrays.asList(scriptEngineService, defaultScriptServiceEngine)));
    //randomly register custom script contexts
    int randomInt = randomIntBetween(0, 3);
    //prevent duplicates using map
    Map<String, ScriptContext.Plugin> contexts = new HashMap<>();
    for (int i = 0; i < randomInt; i++) {
        String plugin;
        do {
            plugin = randomAsciiOfLength(randomIntBetween(1, 10));
        } while (ScriptContextRegistry.RESERVED_SCRIPT_CONTEXTS.contains(plugin));
        String operation;
        do {
            operation = randomAsciiOfLength(randomIntBetween(1, 30));
        } while (ScriptContextRegistry.RESERVED_SCRIPT_CONTEXTS.contains(operation));
        String context = plugin + "_" + operation;
        contexts.put(context, new ScriptContext.Plugin(plugin, operation));
    }
    scriptEngineRegistry = new ScriptEngineRegistry(Arrays.asList(scriptEngineService, dangerousScriptEngineService, defaultScriptServiceEngine));
    scriptContextRegistry = new ScriptContextRegistry(contexts.values());
    scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    scriptContexts = scriptContextRegistry.scriptContexts().toArray(new ScriptContext[scriptContextRegistry.scriptContexts().size()]);
    logger.info("--> setup script service");
    scriptsFilePath = genericConfigFolder.resolve("scripts");
    Files.createDirectories(scriptsFilePath);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 2 with ResourceWatcherService

use of org.elasticsearch.watcher.ResourceWatcherService in project elasticsearch by elastic.

the class AbstractSortTestCase method init.

@BeforeClass
public static void init() throws IOException {
    Path genericConfigFolder = createTempDir();
    Settings baseSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(Environment.PATH_CONF_SETTING.getKey(), genericConfigFolder).build();
    Environment environment = new Environment(baseSettings);
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(new TestEngineService()));
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    scriptService = new ScriptService(baseSettings, environment, new ResourceWatcherService(baseSettings, null), scriptEngineRegistry, scriptContextRegistry, scriptSettings) {

        @Override
        public CompiledScript compile(Script script, ScriptContext scriptContext) {
            return new CompiledScript(ScriptType.INLINE, "mockName", "test", script);
        }
    };
    SearchModule searchModule = new SearchModule(Settings.EMPTY, false, emptyList());
    namedWriteableRegistry = new NamedWriteableRegistry(searchModule.getNamedWriteables());
    xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents());
}
Also used : Path(java.nio.file.Path) ContentPath(org.elasticsearch.index.mapper.ContentPath) CompiledScript(org.elasticsearch.script.CompiledScript) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ScriptContext(org.elasticsearch.script.ScriptContext) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry) ScriptService(org.elasticsearch.script.ScriptService) ScriptSettings(org.elasticsearch.script.ScriptSettings) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) Environment(org.elasticsearch.env.Environment) SearchModule(org.elasticsearch.search.SearchModule) TestEngineService(org.elasticsearch.script.ScriptServiceTests.TestEngineService) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Settings(org.elasticsearch.common.settings.Settings) ScriptSettings(org.elasticsearch.script.ScriptSettings) IndexSettings(org.elasticsearch.index.IndexSettings) BeforeClass(org.junit.BeforeClass)

Example 3 with ResourceWatcherService

use of org.elasticsearch.watcher.ResourceWatcherService in project elasticsearch by elastic.

the class IndexModuleTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
    indicesQueryCache = new IndicesQueryCache(settings);
    indexSettings = IndexSettingsModule.newIndexSettings("foo", settings);
    index = indexSettings.getIndex();
    environment = new Environment(settings);
    threadPool = new TestThreadPool("test");
    circuitBreakerService = new NoneCircuitBreakerService();
    bigArrays = new BigArrays(settings, circuitBreakerService);
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(emptyList());
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    scriptService = new ScriptService(settings, environment, new ResourceWatcherService(settings, threadPool), scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    clusterService = ClusterServiceUtils.createClusterService(threadPool);
    nodeEnvironment = new NodeEnvironment(settings, environment);
    mapperRegistry = new IndicesModule(Collections.emptyList()).getMapperRegistry();
}
Also used : IndicesQueryCache(org.elasticsearch.indices.IndicesQueryCache) ScriptService(org.elasticsearch.script.ScriptService) BigArrays(org.elasticsearch.common.util.BigArrays) IndicesModule(org.elasticsearch.indices.IndicesModule) ScriptSettings(org.elasticsearch.script.ScriptSettings) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) Environment(org.elasticsearch.env.Environment) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService)

Example 4 with ResourceWatcherService

use of org.elasticsearch.watcher.ResourceWatcherService in project elasticsearch by elastic.

the class NativeScriptTests method testFineGrainedSettingsDontAffectNativeScripts.

public void testFineGrainedSettingsDontAffectNativeScripts() throws IOException {
    Settings.Builder builder = Settings.builder();
    if (randomBoolean()) {
        ScriptType scriptType = randomFrom(ScriptType.values());
        builder.put("script" + "." + scriptType.getName(), randomBoolean());
    } else {
        ScriptContext scriptContext = randomFrom(ScriptContext.Standard.values());
        builder.put("script" + "." + scriptContext.getKey(), randomBoolean());
    }
    Settings settings = builder.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
    Environment environment = new Environment(settings);
    ResourceWatcherService resourceWatcherService = new ResourceWatcherService(settings, null);
    Map<String, NativeScriptFactory> nativeScriptFactoryMap = new HashMap<>();
    nativeScriptFactoryMap.put("my", new MyNativeScriptFactory());
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singleton(new NativeScriptEngineService(settings, nativeScriptFactoryMap)));
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(new ArrayList<>());
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    ScriptService scriptService = new ScriptService(settings, environment, resourceWatcherService, scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    for (ScriptContext scriptContext : scriptContextRegistry.scriptContexts()) {
        assertThat(scriptService.compile(new Script(ScriptType.INLINE, NativeScriptEngineService.NAME, "my", Collections.emptyMap()), scriptContext), notNullValue());
    }
}
Also used : HashMap(java.util.HashMap) Environment(org.elasticsearch.env.Environment) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService) Settings(org.elasticsearch.common.settings.Settings)

Example 5 with ResourceWatcherService

use of org.elasticsearch.watcher.ResourceWatcherService in project elasticsearch by elastic.

the class UpdateRequestTests method testNowInScript.

public void testNowInScript() throws IOException {
    Path genericConfigFolder = createTempDir();
    Settings baseSettings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(Environment.PATH_CONF_SETTING.getKey(), genericConfigFolder).build();
    Environment environment = new Environment(baseSettings);
    Map<String, Function<Map<String, Object>, Object>> scripts = new HashMap<>();
    scripts.put("ctx._source.update_timestamp = ctx._now", (vars) -> {
        Map<String, Object> vars2 = vars;
        @SuppressWarnings("unchecked") Map<String, Object> ctx = (Map<String, Object>) vars2.get("ctx");
        @SuppressWarnings("unchecked") Map<String, Object> source = (Map<String, Object>) ctx.get("_source");
        source.put("update_timestamp", ctx.get("_now"));
        return null;
    });
    scripts.put("ctx._timestamp = ctx._now", (vars) -> {
        @SuppressWarnings("unchecked") Map<String, Object> ctx = (Map<String, Object>) vars.get("ctx");
        ctx.put("_timestamp", ctx.get("_now"));
        return null;
    });
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(new MockScriptEngine("mock", scripts)));
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    ScriptService scriptService = new ScriptService(baseSettings, environment, new ResourceWatcherService(baseSettings, null), scriptEngineRegistry, scriptContextRegistry, scriptSettings);
    Settings settings = settings(Version.CURRENT).build();
    UpdateHelper updateHelper = new UpdateHelper(settings, scriptService);
    // We just upsert one document with now() using a script
    IndexRequest indexRequest = new IndexRequest("test", "type1", "2").source(jsonBuilder().startObject().field("foo", "bar").endObject());
    {
        UpdateRequest updateRequest = new UpdateRequest("test", "type1", "2").upsert(indexRequest).script(new Script(ScriptType.INLINE, "mock", "ctx._source.update_timestamp = ctx._now", Collections.emptyMap())).scriptedUpsert(true);
        long nowInMillis = randomNonNegativeLong();
        // We simulate that the document is not existing yet
        GetResult getResult = new GetResult("test", "type1", "2", 0, false, null, null);
        UpdateHelper.Result result = updateHelper.prepare(new ShardId("test", "_na_", 0), updateRequest, getResult, () -> nowInMillis);
        Streamable action = result.action();
        assertThat(action, instanceOf(IndexRequest.class));
        IndexRequest indexAction = (IndexRequest) action;
        assertEquals(indexAction.sourceAsMap().get("update_timestamp"), nowInMillis);
    }
    {
        UpdateRequest updateRequest = new UpdateRequest("test", "type1", "2").upsert(indexRequest).script(new Script(ScriptType.INLINE, "mock", "ctx._timestamp = ctx._now", Collections.emptyMap())).scriptedUpsert(true);
        // We simulate that the document is not existing yet
        GetResult getResult = new GetResult("test", "type1", "2", 0, true, new BytesArray("{}"), null);
        UpdateHelper.Result result = updateHelper.prepare(new ShardId("test", "_na_", 0), updateRequest, getResult, () -> 42L);
        Streamable action = result.action();
        assertThat(action, instanceOf(IndexRequest.class));
    }
}
Also used : HashMap(java.util.HashMap) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetResult(org.elasticsearch.index.get.GetResult) ScriptService(org.elasticsearch.script.ScriptService) ShardId(org.elasticsearch.index.shard.ShardId) Function(java.util.function.Function) ScriptSettings(org.elasticsearch.script.ScriptSettings) MockScriptEngine(org.elasticsearch.script.MockScriptEngine) Streamable(org.elasticsearch.common.io.stream.Streamable) ScriptSettings(org.elasticsearch.script.ScriptSettings) Settings(org.elasticsearch.common.settings.Settings) Path(java.nio.file.Path) Script(org.elasticsearch.script.Script) BytesArray(org.elasticsearch.common.bytes.BytesArray) GetResult(org.elasticsearch.index.get.GetResult) ScriptContextRegistry(org.elasticsearch.script.ScriptContextRegistry) ScriptEngineRegistry(org.elasticsearch.script.ScriptEngineRegistry) Environment(org.elasticsearch.env.Environment) HashMap(java.util.HashMap) Map(java.util.Map) ResourceWatcherService(org.elasticsearch.watcher.ResourceWatcherService)

Aggregations

ResourceWatcherService (org.elasticsearch.watcher.ResourceWatcherService)5 Environment (org.elasticsearch.env.Environment)4 Path (java.nio.file.Path)3 HashMap (java.util.HashMap)3 Settings (org.elasticsearch.common.settings.Settings)3 ScriptContextRegistry (org.elasticsearch.script.ScriptContextRegistry)3 ScriptEngineRegistry (org.elasticsearch.script.ScriptEngineRegistry)3 ScriptService (org.elasticsearch.script.ScriptService)3 ScriptSettings (org.elasticsearch.script.ScriptSettings)3 Script (org.elasticsearch.script.Script)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Function (java.util.function.Function)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)1 BytesArray (org.elasticsearch.common.bytes.BytesArray)1 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)1 Streamable (org.elasticsearch.common.io.stream.Streamable)1 BigArrays (org.elasticsearch.common.util.BigArrays)1 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)1 NodeEnvironment (org.elasticsearch.env.NodeEnvironment)1