Skip to content

Commit 4cbde0c

Browse files
committed
Caching script factory instead of script instance
1 parent b509846 commit 4cbde0c

File tree

5 files changed

+42
-71
lines changed

5 files changed

+42
-71
lines changed

server/src/main/java/org/elasticsearch/ingest/ConditionalProcessor.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import org.elasticsearch.common.logging.DeprecationCategory;
1313
import org.elasticsearch.common.logging.DeprecationLogger;
14-
import org.elasticsearch.script.CtxMapWrapper;
1514
import org.elasticsearch.script.DynamicMap;
1615
import org.elasticsearch.script.IngestConditionalScript;
1716
import org.elasticsearch.script.Script;
@@ -57,8 +56,7 @@ public class ConditionalProcessor extends AbstractProcessor implements WrappingP
5756
private final Processor processor;
5857
private final IngestMetric metric;
5958
private final LongSupplier relativeTimeProvider;
60-
private final IngestConditionalScript precompiledConditionScript;
61-
private final CtxMapWrapper ctxMapWrapper;
59+
private final IngestConditionalScript.Factory precompiledConditionalScriptFactory;
6260

6361
ConditionalProcessor(String tag, String description, Script script, ScriptService scriptService, Processor processor) {
6462
this(tag, description, script, scriptService, processor, System::nanoTime);
@@ -78,15 +76,13 @@ public class ConditionalProcessor extends AbstractProcessor implements WrappingP
7876
this.processor = processor;
7977
this.metric = new IngestMetric();
8078
this.relativeTimeProvider = relativeTimeProvider;
81-
this.ctxMapWrapper = new CtxMapWrapper();
8279

8380
try {
84-
final IngestConditionalScript.Factory factory = scriptService.compile(script, IngestConditionalScript.CONTEXT);
8581
if (ScriptType.INLINE.equals(script.getType())) {
86-
precompiledConditionScript = factory.newInstance(script.getParams(), ctxMapWrapper);
82+
precompiledConditionalScriptFactory = scriptService.compile(script, IngestConditionalScript.CONTEXT);
8783
} else {
8884
// stored script, so will have to compile at runtime
89-
precompiledConditionScript = null;
85+
precompiledConditionalScriptFactory = null;
9086
}
9187
} catch (ScriptException e) {
9288
throw newConfigurationException(TYPE, tag, null, e);
@@ -144,17 +140,14 @@ public void execute(IngestDocument ingestDocument, BiConsumer<IngestDocument, Ex
144140
}
145141

146142
boolean evaluate(IngestDocument ingestDocument) {
147-
IngestConditionalScript script = precompiledConditionScript;
148-
if (script == null) {
149-
IngestConditionalScript.Factory factory = scriptService.compile(condition, IngestConditionalScript.CONTEXT);
150-
script = factory.newInstance(condition.getParams(), ctxMapWrapper);
151-
}
152-
ctxMapWrapper.setCtxMap(new UnmodifiableIngestData(new DynamicMap(ingestDocument.getSourceAndMetadata(), FUNCTIONS)));
153-
try {
154-
return script.execute();
155-
} finally {
156-
ctxMapWrapper.clearCtxMap();
157-
}
143+
IngestConditionalScript.Factory factory = precompiledConditionalScriptFactory;
144+
if (factory == null) {
145+
factory = scriptService.compile(condition, IngestConditionalScript.CONTEXT);
146+
}
147+
return factory.newInstance(
148+
condition.getParams(),
149+
new UnmodifiableIngestData(new DynamicMap(ingestDocument.getSourceAndMetadata(), FUNCTIONS))
150+
).execute();
158151
}
159152

160153
public Processor getInnerProcessor() {

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515

1616
/**
1717
* A script used by {@link org.elasticsearch.ingest.ConditionalProcessor}.
18-
* To properly expose the {@link SourceMapFieldScript#field(String)} API, make sure to provide a valid {@link CtxMap} before execution
19-
* through the {@link CtxMapWrapper} passed to the constructor and make sure to clear it after use to avoid leaks.
2018
*/
2119
public abstract class IngestConditionalScript extends SourceMapFieldScript {
2220

2321
public static final String[] PARAMETERS = {};
2422

25-
/** The context used to compile {@link IngestConditionalScript} factories. */
23+
/**
24+
* The context used to compile {@link IngestConditionalScript} factories.
25+
* */
2626
public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>(
2727
"processor_conditional",
2828
Factory.class,
@@ -32,22 +32,27 @@ public abstract class IngestConditionalScript extends SourceMapFieldScript {
3232
true
3333
);
3434

35-
/** The generic runtime parameters for the script. */
35+
/**
36+
* The generic runtime parameters for the script.
37+
*/
3638
private final Map<String, Object> params;
3739

38-
public IngestConditionalScript(Map<String, Object> params, CtxMapWrapper ctxMapWrapper) {
39-
super(ctxMapWrapper);
40+
public IngestConditionalScript(Map<String, Object> params, Map<String, Object> ctxMap) {
41+
super(ctxMap);
4042
this.params = params;
4143
}
4244

43-
/** Return the parameters for this script. */
45+
/**
46+
* Return the parameters for this script.
47+
* @return a map of parameters
48+
*/
4449
public Map<String, Object> getParams() {
4550
return params;
4651
}
4752

4853
public abstract boolean execute();
4954

5055
public interface Factory {
51-
IngestConditionalScript newInstance(Map<String, Object> params, CtxMapWrapper ctxMapWrapper);
56+
IngestConditionalScript newInstance(Map<String, Object> params, Map<String, Object> ctxMap);
5257
}
5358
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,27 @@
1818
* These scripts provide {@code ctx} for backwards compatibility and expose {@link Metadata}.
1919
*/
2020
public abstract class SourceMapFieldScript {
21-
protected final CtxMapWrapper ctxMapWrapper;
21+
protected final Map<String, Object> ctxMap;
2222

23-
public SourceMapFieldScript(CtxMapWrapper ctxMapWrapper) {
24-
this.ctxMapWrapper = ctxMapWrapper;
23+
public SourceMapFieldScript(Map<String, Object> ctxMap) {
24+
this.ctxMap = ctxMap;
2525
}
2626

27-
/** Provides backwards compatibility access to ctx */
27+
/**
28+
* Provides backwards compatibility access to ctx
29+
* @return the context map containing the source data
30+
*/
2831
public Map<String, Object> getCtx() {
29-
return ctxMapWrapper.getCtxMap();
32+
return ctxMap;
3033
}
3134

35+
/**
36+
* Expose the {@link SourceMapField field} API
37+
*
38+
* @param path the path to the field in the source map
39+
* @return a new {@link SourceMapField} instance for the specified path
40+
*/
3241
public SourceMapField field(String path) {
33-
return new SourceMapField(path, ctxMapWrapper::getCtxMap);
42+
return new SourceMapField(path, () -> ctxMap);
3443
}
3544
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ public void execute() {
169169
} else if (context.instanceClazz.equals(AggregationScript.class)) {
170170
return context.factoryClazz.cast(new MockAggregationScript(script));
171171
} else if (context.instanceClazz.equals(IngestConditionalScript.class)) {
172-
IngestConditionalScript.Factory factory = (parameters, ctxMapWrapper) -> new IngestConditionalScript(
172+
IngestConditionalScript.Factory factory = (parameters, ctxMap) -> new IngestConditionalScript(
173173
parameters,
174-
ctxMapWrapper
174+
ctxMap
175175
) {
176176
@Override
177177
public boolean execute() {
178-
return (boolean) script.apply(ctxMapWrapper.getCtxMap());
178+
return (boolean) script.apply(ctxMap);
179179
}
180180
};
181181
return context.factoryClazz.cast(factory);

0 commit comments

Comments
 (0)