Skip to content

Commit 070ea7e

Browse files
authored
Scripting: Per-context script cache, default off (#52855)
* 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. Refs: #50152
1 parent c46e0f5 commit 070ea7e

File tree

12 files changed

+544
-84
lines changed

12 files changed

+544
-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
@@ -364,10 +364,13 @@ public void apply(Settings value, Settings current, Settings previous) {
364364
NetworkService.TCP_RECEIVE_BUFFER_SIZE,
365365
IndexSettings.QUERY_STRING_ANALYZE_WILDCARD,
366366
IndexSettings.QUERY_STRING_ALLOW_LEADING_WILDCARD,
367+
ScriptService.SCRIPT_GENERAL_CACHE_SIZE_SETTING,
368+
ScriptService.SCRIPT_GENERAL_CACHE_EXPIRE_SETTING,
369+
ScriptService.SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING,
367370
ScriptService.SCRIPT_CACHE_SIZE_SETTING,
368371
ScriptService.SCRIPT_CACHE_EXPIRE_SETTING,
372+
ScriptService.SCRIPT_MAX_COMPILATIONS_RATE_SETTING,
369373
ScriptService.SCRIPT_MAX_SIZE_IN_BYTES,
370-
ScriptService.SCRIPT_MAX_COMPILATIONS_RATE,
371374
ScriptService.TYPES_ALLOWED_SETTING,
372375
ScriptService.CONTEXTS_ALLOWED_SETTING,
373376
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
@@ -668,7 +668,12 @@ public String toString() {
668668

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

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

674679
private Settings get(Settings settings) {
@@ -691,6 +696,7 @@ public boolean hasChanged(Settings current, Settings previous) {
691696

692697
@Override
693698
public Settings getValue(Settings current, Settings previous) {
699+
validator.accept(current);
694700
return get(current);
695701
}
696702

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)