Skip to content

Commit 005d8f0

Browse files
authored
Script: Deprecate script context cache (#79508)
* Script: Deprecate script context cache Deprecate the script context cache in favor of the general cache. Users should use the following settings: `script.max_compilations_rate` to set the max compilation rate for user scripts such as filter scripts. Certain script contexts that submit scripts outside of the control of the user are exempted from this rate limit. Examples include runtime fields, ingest and watcher. `script.cache.max_size` to set the max size of the cache. `script.cache.expire` to set the expiration time for entries in the cache. Whats deprecated? `script.max_compilations_rate: use-context`. This special setting value was used to turn on the script context-specific caches. `script.context.$CONTEXT.cache_max_size`, use `script.cache.max_size` instead. `script.context.$CONTEXT.cache_expire`, use `script.cache.expire` instead. `script.context.$CONTEXT.max_compilations_rate`, use `script.max_compilations_rate` instead. The default cache size was increased from `100` to `3000`, which was approximately the max cache size when using context-specific caches. The default compilation rate limit was increased from `75/5m` to `150/5m` to account for increasing uses of scripts. Refs: #62899
1 parent b4fb27c commit 005d8f0

File tree

9 files changed

+401
-62
lines changed

9 files changed

+401
-62
lines changed

docs/reference/migration/migrate_7_16.asciidoc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,30 @@ avoid deprecation warnings and opt into the future behaviour, include the query
321321
parameter `?return_200_for_cluster_health_timeout` in your request.
322322
====
323323

324+
[[script-context-cache-deprecated]]
325+
.The script context cache is deprecated
326+
[%collapsible]
327+
====
328+
*Details* +
329+
Setting `script.max_compilations_rate` to `use-context` and
330+
configuring context-specific caches is deprecated.
331+
332+
Context-specific caches are no longer needed to prevent system scripts
333+
from triggering rate limits.
334+
335+
Configure the general cache to control the max compilation rate, cache size, and
336+
cache expiration for your scripts.
337+
338+
*Impact* +
339+
Remove `script.max_compilations_rate: use-context` and the context-specific cache
340+
settings: `script.context.$CONTEXT.cache_max_size`,
341+
`script.context.$CONTEXT.max_compilations_rate`, `script.context.$CONTEXT.cache_expire`.
342+
343+
The general cache defaults to a max size of 3000 with a rate limit of 150/5m,
344+
which allows 150 compilations per 5 minute period. By default, the entries in the
345+
cache do not expire.
346+
347+
To override the defaults, configure the `script.cache.max_size`,
348+
`script.max_compilations_rate`, and `script.cache.expire` settings.
349+
====
324350
// end::notable-breaking-changes[]

docs/reference/modules/indices/circuit_breaker.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ within a period of time.
126126
See the "prefer-parameters" section of the <<modules-scripting-using,scripting>>
127127
documentation for more information.
128128

129-
`script.context.$CONTEXT.max_compilations_rate`::
129+
`script.max_compilations_rate`::
130130
(<<dynamic-cluster-setting,Dynamic>>)
131131
Limit for the number of unique dynamic scripts within a certain interval
132-
that are allowed to be compiled for a given context. Defaults to `75/5m`,
133-
meaning 75 every 5 minutes.
132+
that are allowed to be compiled. Defaults to `150/5m`,
133+
meaning 150 every 5 minutes.
134134

135135
[[regex-circuit-breaker]]
136136
[discrete]

docs/reference/scripting/using.asciidoc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,8 @@ the `multiplier` parameter without {es} recompiling the script.
120120
}
121121
----
122122

123-
For most contexts, you can compile up to 75 scripts per 5 minutes by default.
124-
For ingest contexts, the default script compilation rate is unlimited. You
125-
can change these settings dynamically by setting
126-
`script.context.$CONTEXT.max_compilations_rate`. For example, the following
127-
setting limits script compilation to 100 scripts every 10 minutes for the
128-
{painless}/painless-field-context.html[field context]:
123+
You can compile up to 150 scripts per 5 minutes by default.
124+
For ingest contexts, the default script compilation rate is unlimited.
129125

130126
[source,js]
131127
----
@@ -406,8 +402,8 @@ small.
406402

407403
All scripts are cached by default so that they only need to be recompiled
408404
when updates occur. By default, scripts do not have a time-based expiration.
409-
You can change this behavior by using the `script.context.$CONTEXT.cache_expire` setting.
410-
Use the `script.context.$CONTEXT.cache_max_size` setting to configure the size of the cache.
405+
You can change this behavior by using the `script.cache.expire` setting.
406+
Use the `script.cache.max_size` setting to configure the size of the cache.
411407

412408
NOTE: The size of scripts is limited to 65,535 bytes. Set the value of `script.max_size_in_bytes` to increase that soft limit. If your scripts are
413409
really large, then consider using a

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.elasticsearch.cluster.metadata.Metadata;
2424
import org.elasticsearch.cluster.service.ClusterService;
2525
import org.elasticsearch.common.Strings;
26+
import org.elasticsearch.common.logging.DeprecationCategory;
27+
import org.elasticsearch.common.logging.DeprecationLogger;
2628
import org.elasticsearch.common.settings.ClusterSettings;
2729
import org.elasticsearch.common.settings.Setting;
2830
import org.elasticsearch.common.settings.Setting.Property;
@@ -50,6 +52,7 @@
5052
public class ScriptService implements Closeable, ClusterStateApplier, ScriptCompiler {
5153

5254
private static final Logger logger = LogManager.getLogger(ScriptService.class);
55+
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(ScriptService.class);
5356

5457
static final String DISABLE_DYNAMIC_SCRIPTING_SETTING = "script.disable_dynamic";
5558

@@ -59,16 +62,22 @@ public class ScriptService implements Closeable, ClusterStateApplier, ScriptComp
5962
static final String USE_CONTEXT_RATE_KEY = "use-context";
6063

6164
public static final Setting<Integer> SCRIPT_GENERAL_CACHE_SIZE_SETTING =
62-
Setting.intSetting("script.cache.max_size", 100, 0, Property.NodeScope);
65+
Setting.intSetting("script.cache.max_size", 3000, 0, Property.Dynamic, Property.NodeScope);
6366
public static final Setting<TimeValue> SCRIPT_GENERAL_CACHE_EXPIRE_SETTING =
64-
Setting.positiveTimeSetting("script.cache.expire", TimeValue.timeValueMillis(0), Property.NodeScope);
67+
Setting.positiveTimeSetting("script.cache.expire", TimeValue.timeValueMillis(0), Property.Dynamic, Property.NodeScope);
6568
public static final Setting<Integer> SCRIPT_MAX_SIZE_IN_BYTES =
6669
Setting.intSetting("script.max_size_in_bytes", 65535, 0, Property.Dynamic, Property.NodeScope);
6770
public static final Setting<ScriptCache.CompilationRate> SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING =
68-
new Setting<>("script.max_compilations_rate", USE_CONTEXT_RATE_KEY,
71+
new Setting<>("script.max_compilations_rate", "150/5m",
6972
(String value) -> value.equals(USE_CONTEXT_RATE_KEY) ? USE_CONTEXT_RATE_VALUE: new ScriptCache.CompilationRate(value),
7073
Property.Dynamic, Property.NodeScope);
7174

75+
public static final String USE_CONTEXT_RATE_KEY_DEPRECATION_MESSAGE = "[" + USE_CONTEXT_RATE_KEY + "] is deprecated for the setting [" +
76+
SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING.getKey() + "] as system scripts are now exempt from the rate limit. " +
77+
"Set to a value such as [150/5m] (a rate of 150 compilations per five minutes) to rate limit user scripts in case the " +
78+
"script cache [" + SCRIPT_GENERAL_CACHE_SIZE_SETTING.getKey() + "] is undersized causing script compilation thrashing.";
79+
80+
7281
// Per-context settings
7382
static final String CONTEXT_PREFIX = "script.context.";
7483

@@ -77,13 +86,14 @@ public class ScriptService implements Closeable, ClusterStateApplier, ScriptComp
7786
public static final Setting.AffixSetting<Integer> SCRIPT_CACHE_SIZE_SETTING =
7887
Setting.affixKeySetting(CONTEXT_PREFIX,
7988
"cache_max_size",
80-
key -> Setting.intSetting(key, SCRIPT_GENERAL_CACHE_SIZE_SETTING, 0, Property.NodeScope, Property.Dynamic));
89+
key -> Setting.intSetting(key, SCRIPT_GENERAL_CACHE_SIZE_SETTING, 0,
90+
Property.NodeScope, Property.Dynamic, Property.Deprecated));
8191

8292
public static final Setting.AffixSetting<TimeValue> SCRIPT_CACHE_EXPIRE_SETTING =
8393
Setting.affixKeySetting(CONTEXT_PREFIX,
8494
"cache_expire",
8595
key -> Setting.positiveTimeSetting(key, SCRIPT_GENERAL_CACHE_EXPIRE_SETTING, TimeValue.timeValueMillis(0),
86-
Property.NodeScope, Property.Dynamic));
96+
Property.NodeScope, Property.Dynamic, Property.Deprecated));
8797

8898
// Unlimited compilation rate for context-specific script caches
8999
static final String UNLIMITED_COMPILATION_RATE_KEY = "unlimited";
@@ -94,7 +104,7 @@ public class ScriptService implements Closeable, ClusterStateApplier, ScriptComp
94104
key -> new Setting<ScriptCache.CompilationRate>(key, "75/5m",
95105
(String value) -> value.equals(UNLIMITED_COMPILATION_RATE_KEY) ? ScriptCache.UNLIMITED_COMPILATION_RATE:
96106
new ScriptCache.CompilationRate(value),
97-
Property.NodeScope, Property.Dynamic));
107+
Property.NodeScope, Property.Dynamic, Property.Deprecated));
98108

99109
private static final ScriptCache.CompilationRate SCRIPT_COMPILATION_RATE_ZERO = new ScriptCache.CompilationRate(0, TimeValue.ZERO);
100110

@@ -205,6 +215,10 @@ public ScriptService(Settings settings, Map<String, ScriptEngine> engines, Map<S
205215
this.setCacheHolder(settings);
206216
}
207217

218+
public static boolean isUseContextCacheSet(Settings settings) {
219+
return SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING.get(settings).equals(USE_CONTEXT_RATE_VALUE);
220+
}
221+
208222
/**
209223
* This is overridden in tests to disable compilation rate limiting.
210224
*/
@@ -249,6 +263,10 @@ void registerClusterSettingsListeners(ClusterSettings clusterSettings) {
249263
*/
250264
void validateCacheSettings(Settings settings) {
251265
boolean useContext = SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING.get(settings).equals(USE_CONTEXT_RATE_VALUE);
266+
if (useContext) {
267+
deprecationLogger.warn(DeprecationCategory.SCRIPTING, "scripting-context-cache",
268+
USE_CONTEXT_RATE_KEY_DEPRECATION_MESSAGE);
269+
}
252270
List<Setting.AffixSetting<?>> affixes = Arrays.asList(SCRIPT_MAX_COMPILATIONS_RATE_SETTING, SCRIPT_CACHE_EXPIRE_SETTING,
253271
SCRIPT_CACHE_SIZE_SETTING);
254272
List<String> customRates = new ArrayList<>();
@@ -277,7 +295,7 @@ void validateCacheSettings(Settings settings) {
277295
String.join(", ", customRates) + "] if compile rates disabled via [" +
278296
SCRIPT_DISABLE_MAX_COMPILATIONS_RATE_SETTING.getKey() + "]");
279297
}
280-
if (useContext == false) {
298+
if (useContext == false && SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING.exists(settings)) {
281299
throw new IllegalArgumentException("Cannot set custom general compilation rates [" +
282300
SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING.getKey() + "] to [" +
283301
SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING.get(settings) + "] if compile rates disabled via [" +
@@ -568,6 +586,7 @@ void setCacheHolder(Settings settings) {
568586
} else if (current.general.rate.equals(SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING.get(settings)) == false ||
569587
current.general.cacheExpire.equals(SCRIPT_GENERAL_CACHE_EXPIRE_SETTING.get(settings)) == false ||
570588
current.general.cacheSize != SCRIPT_GENERAL_CACHE_SIZE_SETTING.get(settings)) {
589+
// General compilation rate, cache expiration or cache size changed
571590
cacheHolder.set(generalCacheHolder(settings));
572591
}
573592
}

0 commit comments

Comments
 (0)