Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Type removal] Remove _type deprecation from script and conditional processor #3239

Merged
merged 2 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import org.opensearch.common.Nullable;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.logging.DeprecationLogger;
import org.opensearch.common.util.CollectionUtils;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.NamedXContentRegistry;
Expand All @@ -45,7 +44,6 @@
import org.opensearch.ingest.AbstractProcessor;
import org.opensearch.ingest.IngestDocument;
import org.opensearch.ingest.Processor;
import org.opensearch.script.DynamicMap;
import org.opensearch.script.IngestScript;
import org.opensearch.script.Script;
import org.opensearch.script.ScriptException;
Expand All @@ -55,7 +53,6 @@
import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;

import static org.opensearch.ingest.ConfigurationUtils.newConfigurationException;

Expand All @@ -64,12 +61,6 @@
*/
public final class ScriptProcessor extends AbstractProcessor {

private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(DynamicMap.class);
private static final Map<String, Function<Object, Object>> PARAMS_FUNCTIONS = org.opensearch.common.collect.Map.of("_type", value -> {
deprecationLogger.deprecate("script_processor", "[types removal] Looking up doc types [_type] in scripts is deprecated.");
return value;
});

public static final String TYPE = "script";

private final Script script;
Expand Down Expand Up @@ -111,7 +102,7 @@ public IngestDocument execute(IngestDocument document) {
} else {
ingestScript = precompiledIngestScript;
}
ingestScript.execute(new DynamicMap(document.getSourceAndMetadata(), PARAMS_FUNCTIONS));
ingestScript.execute(document.getSourceAndMetadata());
CollectionUtils.ensureNoSelfReferences(document.getSourceAndMetadata(), "ingest script");
return document;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,4 @@ private void assertIngestDocument(IngestDocument ingestDocument) {
int bytesTotal = ingestDocument.getFieldValue("bytes_in", Integer.class) + ingestDocument.getFieldValue("bytes_out", Integer.class);
assertThat(ingestDocument.getSourceAndMetadata().get("bytes_total"), is(bytesTotal));
}

public void testTypeDeprecation() throws Exception {
String scriptName = "script";
ScriptService scriptService = new ScriptService(
Settings.builder().build(),
Collections.singletonMap(
Script.DEFAULT_SCRIPT_LANG,
new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> {
ctx.get("_type");
return null;
}), Collections.emptyMap())
),
new HashMap<>(ScriptModule.CORE_CONTEXTS)
);
Script script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap());
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
ScriptProcessor processor = new ScriptProcessor(randomAlphaOfLength(10), null, script, null, scriptService);
processor.execute(ingestDocument);
assertWarnings("[types removal] Looking up doc types [_type] in scripts is deprecated.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

package org.opensearch.ingest;

import org.opensearch.common.logging.DeprecationLogger;
import org.opensearch.script.DynamicMap;
import org.opensearch.script.IngestConditionalScript;
import org.opensearch.script.Script;
import org.opensearch.script.ScriptException;
Expand All @@ -51,7 +49,6 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.LongSupplier;
import java.util.stream.Collectors;

Expand All @@ -64,15 +61,6 @@
*/
public class ConditionalProcessor extends AbstractProcessor implements WrappingProcessor {

private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(DynamicMap.class);
private static final Map<String, Function<Object, Object>> FUNCTIONS = org.opensearch.common.collect.Map.of("_type", value -> {
deprecationLogger.deprecate(
"conditional-processor__type",
"[types removal] Looking up doc types [_type] in scripts is deprecated."
);
return value;
});

static final String TYPE = "conditional";

private final Script condition;
Expand Down Expand Up @@ -153,7 +141,7 @@ boolean evaluate(IngestDocument ingestDocument) {
IngestConditionalScript.Factory factory = scriptService.compile(condition, IngestConditionalScript.CONTEXT);
script = factory.newInstance(condition.getParams());
}
return script.execute(new UnmodifiableIngestData(new DynamicMap(ingestDocument.getSourceAndMetadata(), FUNCTIONS)));
return script.execute(new UnmodifiableIngestData(ingestDocument.getSourceAndMetadata()));
}

public Processor getInnerProcessor() {
Expand Down
11 changes: 1 addition & 10 deletions server/src/main/java/org/opensearch/script/UpdateScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@

package org.opensearch.script;

import org.opensearch.common.logging.DeprecationLogger;

import java.util.Map;
import java.util.function.Function;

/**
* An update script.
Expand All @@ -44,12 +41,6 @@
*/
public abstract class UpdateScript {

private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(DynamicMap.class);
private static final Map<String, Function<Object, Object>> PARAMS_FUNCTIONS = org.opensearch.common.collect.Map.of("_type", value -> {
deprecationLogger.deprecate("update-script", "[types removal] Looking up doc types [_type] in scripts is deprecated.");
return value;
});

public static final String[] PARAMETERS = {};

/** The context used to compile {@link UpdateScript} factories. */
Expand All @@ -63,7 +54,7 @@ public abstract class UpdateScript {

public UpdateScript(Map<String, Object> params, Map<String, Object> ctx) {
this.params = params;
this.ctx = new DynamicMap(ctx, PARAMS_FUNCTIONS);
this.ctx = ctx;
}

/** Return the parameters for this script. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,56 +163,6 @@ public void testActsOnImmutableData() throws Exception {
assertMutatingCtxThrows(ctx -> ((List<Object>) ctx.get("listField")).remove("bar"));
}

public void testTypeDeprecation() throws Exception {

ScriptService scriptService = new ScriptService(
Settings.builder().build(),
Collections.singletonMap(
Script.DEFAULT_SCRIPT_LANG,
new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> {
ctx.get("_type");
return true;
}), Collections.emptyMap())
),
new HashMap<>(ScriptModule.CORE_CONTEXTS)
);

LongSupplier relativeTimeProvider = mock(LongSupplier.class);
when(relativeTimeProvider.getAsLong()).thenReturn(0L, TimeUnit.MILLISECONDS.toNanos(1), 0L, TimeUnit.MILLISECONDS.toNanos(2));
ConditionalProcessor processor = new ConditionalProcessor(
randomAlphaOfLength(10),
"description",
new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()),
scriptService,
new Processor() {
@Override
public IngestDocument execute(final IngestDocument ingestDocument) {
return ingestDocument;
}

@Override
public String getType() {
return null;
}

@Override
public String getTag() {
return null;
}

@Override
public String getDescription() {
return null;
}
},
relativeTimeProvider
);

IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
processor.execute(ingestDocument, (result, e) -> {});
assertWarnings("[types removal] Looking up doc types [_type] in scripts is deprecated.");
}

public void testPrecompiledError() {
ScriptService scriptService = MockScriptService.singleContext(
IngestConditionalScript.CONTEXT,
Expand Down