Skip to content

Commit cad3cd9

Browse files
rjernstkcm
authored andcommitted
Scripting: Use ParameterMap for deprecated ctx var in update scripts (#34065)
This commit removes the sysprop controlling whether ctx is in params for update scripts and replaces it with use of the new ParameterMap, which outputs a deprecation warning whenever params.ctx is used.
1 parent da2a4d7 commit cad3cd9

File tree

5 files changed

+45
-45
lines changed

5 files changed

+45
-45
lines changed

modules/reindex/src/main/java/org/elasticsearch/index/reindex/AbstractAsyncBulkByScrollAction.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,6 @@ public abstract static class ScriptApplier implements BiFunction<RequestWrapper<
737737
private final Script script;
738738
private final Map<String, Object> params;
739739

740-
private UpdateScript executable;
741-
private Map<String, Object> context;
742-
743740
public ScriptApplier(WorkerBulkByScrollTaskState taskWorker,
744741
ScriptService scriptService,
745742
Script script,
@@ -756,16 +753,8 @@ public RequestWrapper<?> apply(RequestWrapper<?> request, ScrollableHitSource.Hi
756753
if (script == null) {
757754
return request;
758755
}
759-
if (executable == null) {
760-
UpdateScript.Factory factory = scriptService.compile(script, UpdateScript.CONTEXT);
761-
executable = factory.newInstance(params);
762-
}
763-
if (context == null) {
764-
context = new HashMap<>();
765-
} else {
766-
context.clear();
767-
}
768756

757+
Map<String, Object> context = new HashMap<>();
769758
context.put(IndexFieldMapper.NAME, doc.getIndex());
770759
context.put(TypeFieldMapper.NAME, doc.getType());
771760
context.put(IdFieldMapper.NAME, doc.getId());
@@ -778,7 +767,9 @@ public RequestWrapper<?> apply(RequestWrapper<?> request, ScrollableHitSource.Hi
778767
OpType oldOpType = OpType.INDEX;
779768
context.put("op", oldOpType.toString());
780769

781-
executable.execute(context);
770+
UpdateScript.Factory factory = scriptService.compile(script, UpdateScript.CONTEXT);
771+
UpdateScript updateScript = factory.newInstance(params, context);
772+
updateScript.execute();
782773

783774
String newOp = (String) context.remove("op");
784775
if (newOp == null) {

modules/reindex/src/test/java/org/elasticsearch/index/reindex/AbstractAsyncBulkByScrollActionScriptTestCase.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ public void setupScriptService() {
5656
protected <T extends ActionRequest> T applyScript(Consumer<Map<String, Object>> scriptBody) {
5757
IndexRequest index = new IndexRequest("index", "type", "1").source(singletonMap("foo", "bar"));
5858
ScrollableHitSource.Hit doc = new ScrollableHitSource.BasicHit("test", "type", "id", 0);
59-
UpdateScript updateScript = new UpdateScript(Collections.emptyMap()) {
59+
UpdateScript.Factory factory = (params, ctx) -> new UpdateScript(Collections.emptyMap(), ctx) {
6060
@Override
61-
public void execute(Map<String, Object> ctx) {
61+
public void execute() {
6262
scriptBody.accept(ctx);
6363
}
64-
};
65-
UpdateScript.Factory factory = params -> updateScript;
64+
};;
6665
ExecutableScript simpleExecutableScript = new SimpleExecutableScript(scriptBody);
6766
when(scriptService.compile(any(), eq(ExecutableScript.CONTEXT))).thenReturn(params -> simpleExecutableScript);
6867
when(scriptService.compile(any(), eq(UpdateScript.CONTEXT))).thenReturn(factory);

server/src/main/java/org/elasticsearch/action/update/UpdateHelper.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919

2020
package org.elasticsearch.action.update;
2121

22-
import java.io.IOException;
23-
import java.util.Collections;
24-
import java.util.HashMap;
25-
import java.util.Map;
26-
import java.util.function.LongSupplier;
2722
import org.apache.logging.log4j.Logger;
2823
import org.elasticsearch.ElasticsearchException;
2924
import org.elasticsearch.action.DocWriteResponse;
@@ -52,17 +47,17 @@
5247
import org.elasticsearch.script.UpdateScript;
5348
import org.elasticsearch.search.lookup.SourceLookup;
5449

55-
import static org.elasticsearch.common.Booleans.parseBoolean;
50+
import java.io.IOException;
51+
import java.util.Collections;
52+
import java.util.HashMap;
53+
import java.util.Map;
54+
import java.util.function.LongSupplier;
5655

5756
/**
5857
* Helper for translating an update request to an index, delete request or update response.
5958
*/
6059
public class UpdateHelper extends AbstractComponent {
6160

62-
/** Whether scripts should add the ctx variable to the params map. */
63-
private static final boolean CTX_IN_PARAMS =
64-
parseBoolean(System.getProperty("es.scripting.update.ctx_in_params"), true);
65-
6661
private final ScriptService scriptService;
6762

6863
public UpdateHelper(Settings settings, ScriptService scriptService) {
@@ -286,17 +281,8 @@ private Map<String, Object> executeScript(Script script, Map<String, Object> ctx
286281
try {
287282
if (scriptService != null) {
288283
UpdateScript.Factory factory = scriptService.compile(script, UpdateScript.CONTEXT);
289-
final Map<String, Object> params;
290-
if (CTX_IN_PARAMS) {
291-
params = new HashMap<>(script.getParams());
292-
params.put(ContextFields.CTX, ctx);
293-
deprecationLogger.deprecated("Using `ctx` via `params.ctx` is deprecated. " +
294-
"Use -Des.scripting.update.ctx_in_params=false to enforce non-deprecated usage.");
295-
} else {
296-
params = script.getParams();
297-
}
298-
UpdateScript executableScript = factory.newInstance(params);
299-
executableScript.execute(ctx);
284+
UpdateScript executableScript = factory.newInstance(script.getParams(), ctx);
285+
executableScript.execute();
300286
}
301287
} catch (Exception e) {
302288
throw new IllegalArgumentException("failed to execute script", e);

server/src/main/java/org/elasticsearch/script/UpdateScript.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,57 @@
2020

2121
package org.elasticsearch.script;
2222

23+
import java.util.Collections;
24+
import java.util.HashMap;
2325
import java.util.Map;
2426

2527
/**
2628
* An update script.
2729
*/
2830
public abstract class UpdateScript {
2931

30-
public static final String[] PARAMETERS = { "ctx" };
32+
public static final String[] PARAMETERS = { };
33+
34+
private static final Map<String, String> DEPRECATIONS;
35+
static {
36+
Map<String, String> deprecations = new HashMap<>();
37+
deprecations.put(
38+
"ctx",
39+
"Accessing variable [ctx] via [params.ctx] from within a update script " +
40+
"is deprecated in favor of directly accessing [ctx]."
41+
);
42+
DEPRECATIONS = Collections.unmodifiableMap(deprecations);
43+
}
3144

3245
/** The context used to compile {@link UpdateScript} factories. */
3346
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("update", Factory.class);
3447

3548
/** The generic runtime parameters for the script. */
3649
private final Map<String, Object> params;
3750

38-
public UpdateScript(Map<String, Object> params) {
39-
this.params = params;
51+
/** The update context for the script. */
52+
private final Map<String, Object> ctx;
53+
54+
public UpdateScript(Map<String, Object> params, Map<String, Object> ctx) {
55+
Map<String, Object> paramsWithCtx = new HashMap<>(params);
56+
paramsWithCtx.put("ctx", ctx);
57+
this.params = new ParameterMap(paramsWithCtx, DEPRECATIONS);
58+
this.ctx = ctx;
4059
}
4160

4261
/** Return the parameters for this script. */
4362
public Map<String, Object> getParams() {
4463
return params;
4564
}
4665

47-
public abstract void execute(Map<String, Object> ctx);
66+
/** Return the update context for this script. */
67+
public Map<String, Object> getCtx() {
68+
return ctx;
69+
}
70+
71+
public abstract void execute();
4872

4973
public interface Factory {
50-
UpdateScript newInstance(Map<String, Object> params);
74+
UpdateScript newInstance(Map<String, Object> params, Map<String, Object> ctx);
5175
}
5276
}

test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ public boolean execute(Map<String, Object> ctx) {
117117
};
118118
return context.factoryClazz.cast(factory);
119119
} else if (context.instanceClazz.equals(UpdateScript.class)) {
120-
UpdateScript.Factory factory = parameters -> new UpdateScript(parameters) {
120+
UpdateScript.Factory factory = (parameters, ctx) -> new UpdateScript(parameters, ctx) {
121121
@Override
122-
public void execute(Map<String, Object> ctx) {
122+
public void execute() {
123123
final Map<String, Object> vars = new HashMap<>();
124124
vars.put("ctx", ctx);
125125
vars.put("params", parameters);

0 commit comments

Comments
 (0)