Skip to content

Commit cdbee32

Browse files
authored
Scripting: Per-context script cache, default off (#52855) (#53756)
* Adds per context settings: `script.context.${CONTEXT}.cache_max_size` ~ `script.cache.max_size` `script.context.${CONTEXT}.cache_expire` ~ `script.cache.expire` `script.context.${CONTEXT}.max_compilations_rate` ~ `script.max_compilations_rate` * Context cache is used if: `script.max_compilations_rate=use-context`. This value is dynamically updatable, so users can switch back to the general cache if desired. * Settings for context caches take the first value that applies: 1) Context specific settings if set, eg `script.context.ingest.cache_max_size` 2) Correlated general setting is set to the non-default value, eg `script.cache.max_size` 3) Context default The reason for 2's inclusion is to allow an easy transition for users who've customized their general cache settings. Using the general cache settings for the context caches results in higher effective settings, since they are multiplied across the number of contexts. So a general cache max size of 200 will become 200 * # of contexts. However, this behavior it will avoid users snapping to a value that is too low for them. Backport of: #52855 Refs: #50152
1 parent 5693239 commit cdbee32

File tree

12 files changed

+546
-84
lines changed

12 files changed

+546
-84
lines changed

server/src/main/java/org/elasticsearch/common/settings/AbstractScopedSettings.java

+12
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ public synchronized void addSettingsUpdateConsumer(Consumer<Settings> consumer,
224224
addSettingsUpdater(Setting.groupedSettingsUpdater(consumer, settings));
225225
}
226226

227+
/**
228+
* Adds a settings consumer that is only executed if any setting in the supplied list of settings is changed. In that case all the
229+
* settings are specified in the argument are returned. The validator is run across all specified settings before the settings are
230+
* applied.
231+
*
232+
* Also automatically adds empty consumers for all settings in order to activate logging
233+
*/
234+
public synchronized void addSettingsUpdateConsumer(Consumer<Settings> consumer, List<? extends Setting<?>> settings,
235+
Consumer<Settings> validator) {
236+
addSettingsUpdater(Setting.groupedSettingsUpdater(consumer, settings, validator));
237+
}
238+
227239
/**
228240
* Adds a settings consumer for affix settings. Affix settings have a namespace associated to it that needs to be available to the
229241
* consumer in order to be processed correctly.

server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,13 @@ public void apply(Settings value, Settings current, Settings previous) {
405405
NetworkService.TCP_CONNECT_TIMEOUT,
406406
IndexSettings.QUERY_STRING_ANALYZE_WILDCARD,
407407
IndexSettings.QUERY_STRING_ALLOW_LEADING_WILDCARD,
408+
ScriptService.SCRIPT_GENERAL_CACHE_SIZE_SETTING,
409+
ScriptService.SCRIPT_GENERAL_CACHE_EXPIRE_SETTING,
410+
ScriptService.SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING,
408411
ScriptService.SCRIPT_CACHE_SIZE_SETTING,
409412
ScriptService.SCRIPT_CACHE_EXPIRE_SETTING,
413+
ScriptService.SCRIPT_MAX_COMPILATIONS_RATE_SETTING,
410414
ScriptService.SCRIPT_MAX_SIZE_IN_BYTES,
411-
ScriptService.SCRIPT_MAX_COMPILATIONS_RATE,
412415
ScriptService.TYPES_ALLOWED_SETTING,
413416
ScriptService.CONTEXTS_ALLOWED_SETTING,
414417
IndicesService.INDICES_CACHE_CLEAN_INTERVAL_SETTING,

server/src/main/java/org/elasticsearch/common/settings/Setting.java

+6
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,12 @@ public String toString() {
667667

668668
static AbstractScopedSettings.SettingUpdater<Settings> groupedSettingsUpdater(Consumer<Settings> consumer,
669669
final List<? extends Setting<?>> configuredSettings) {
670+
return groupedSettingsUpdater(consumer, configuredSettings, (v) -> {});
671+
}
670672

673+
static AbstractScopedSettings.SettingUpdater<Settings> groupedSettingsUpdater(Consumer<Settings> consumer,
674+
final List<? extends Setting<?>> configuredSettings,
675+
Consumer<Settings> validator) {
671676
return new AbstractScopedSettings.SettingUpdater<Settings>() {
672677

673678
private Settings get(Settings settings) {
@@ -690,6 +695,7 @@ public boolean hasChanged(Settings current, Settings previous) {
690695

691696
@Override
692697
public Settings getValue(Settings current, Settings previous) {
698+
validator.accept(current);
693699
return get(current);
694700
}
695701

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

+22-44
Original file line numberDiff line numberDiff line change
@@ -40,53 +40,47 @@ public class ScriptCache {
4040

4141
private static final Logger logger = LogManager.getLogger(ScriptService.class);
4242

43-
private Cache<CacheKey, Object> cache;
44-
private final ScriptMetrics scriptMetrics = new ScriptMetrics();
43+
private final Cache<CacheKey, Object> cache;
44+
private final ScriptMetrics scriptMetrics;
4545

4646
private final Object lock = new Object();
4747

48-
private Tuple<Integer, TimeValue> rate;
48+
// Mutable fields
4949
private long lastInlineCompileTime;
5050
private double scriptsPerTimeWindow;
51-
private double compilesAllowedPerNano;
5251

53-
// Cache settings
54-
private int cacheSize;
55-
private TimeValue cacheExpire;
52+
// Cache settings or derived from settings
53+
final int cacheSize;
54+
final TimeValue cacheExpire;
55+
final Tuple<Integer, TimeValue> rate;
56+
private final double compilesAllowedPerNano;
5657

57-
public ScriptCache(
58+
ScriptCache(
5859
int cacheMaxSize,
5960
TimeValue cacheExpire,
6061
Tuple<Integer, TimeValue> maxCompilationRate
6162
) {
63+
this.cacheSize = cacheMaxSize;
64+
this.cacheExpire = cacheExpire;
65+
6266
CacheBuilder<CacheKey, Object> cacheBuilder = CacheBuilder.builder();
63-
if (cacheMaxSize >= 0) {
64-
cacheBuilder.setMaximumWeight(cacheMaxSize);
67+
if (this.cacheSize >= 0) {
68+
cacheBuilder.setMaximumWeight(this.cacheSize);
6569
}
6670

67-
if (cacheExpire.getNanos() != 0) {
68-
cacheBuilder.setExpireAfterAccess(cacheExpire);
71+
if (this.cacheExpire.getNanos() != 0) {
72+
cacheBuilder.setExpireAfterAccess(this.cacheExpire);
6973
}
7074

71-
logger.debug("using script cache with max_size [{}], expire [{}]", cacheMaxSize, cacheExpire);
75+
logger.debug("using script cache with max_size [{}], expire [{}]", this.cacheSize, this.cacheExpire);
7276
this.cache = cacheBuilder.removalListener(new ScriptCacheRemovalListener()).build();
7377

74-
this.lastInlineCompileTime = System.nanoTime();
75-
76-
this.cacheSize = cacheMaxSize;
77-
this.cacheExpire = cacheExpire;
78-
this.setMaxCompilationRate(maxCompilationRate);
79-
}
78+
this.rate = maxCompilationRate;
79+
this.scriptsPerTimeWindow = this.rate.v1();
80+
this.compilesAllowedPerNano = ((double) rate.v1()) / rate.v2().nanos();
8081

81-
private Cache<CacheKey,Object> buildCache() {
82-
CacheBuilder<CacheKey, Object> cacheBuilder = CacheBuilder.builder();
83-
if (cacheSize >= 0) {
84-
cacheBuilder.setMaximumWeight(cacheSize);
85-
}
86-
if (cacheExpire.getNanos() != 0) {
87-
cacheBuilder.setExpireAfterAccess(cacheExpire);
88-
}
89-
return cacheBuilder.removalListener(new ScriptCacheRemovalListener()).build();
82+
this.lastInlineCompileTime = System.nanoTime();
83+
this.scriptMetrics = new ScriptMetrics();
9084
}
9185

9286
<FactoryType> FactoryType compile(
@@ -185,22 +179,6 @@ void checkCompilationLimit() {
185179
}
186180
}
187181

188-
/**
189-
* This configures the maximum script compilations per five minute window.
190-
*
191-
* @param newRate the new expected maximum number of compilations per five minute window
192-
*/
193-
void setMaxCompilationRate(Tuple<Integer, TimeValue> newRate) {
194-
synchronized (lock) {
195-
this.rate = newRate;
196-
// Reset the counter to allow new compilations
197-
this.scriptsPerTimeWindow = rate.v1();
198-
this.compilesAllowedPerNano = ((double) rate.v1()) / newRate.v2().nanos();
199-
200-
this.cache = buildCache();
201-
}
202-
}
203-
204182
/**
205183
* A small listener for the script cache that calls each
206184
* {@code ScriptEngine}'s {@code scriptRemoved} method when the

0 commit comments

Comments
 (0)