Skip to content

Scripting: Per-context script cache, default off #52855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6cc359c
Scripting: Per-context script cache, default off
stu-elastic Feb 26, 2020
cc55177
Setting SCRIPT_*_DEPRECATED -> SCRIPT_GENERAL_*; contextPrefix -> CON…
stu-elastic Feb 27, 2020
3e0cbb9
Settings: AffixSettings as validator dependencies
stu-elastic Feb 28, 2020
c516495
Use validators for TimeSettings, use atomic ref to change context set…
stu-elastic Mar 2, 2020
5762bd5
Get affix match any
stu-elastic Mar 2, 2020
ab9ad44
WIP flag
stu-elastic Mar 3, 2020
06f0bd8
Add script.context.cache_enabled setting, use single cache updater to…
stu-elastic Mar 3, 2020
bfa97f9
Allow "use-context" setting to flip back and forth between context an…
stu-elastic Mar 4, 2020
c7b6ef8
Merge branch 'master' into fix/50152-painless-limit-per-context__02__…
stu-elastic Mar 4, 2020
5ce234a
Remove commented out tests, unnecessary settings updates, simpile cac…
stu-elastic Mar 4, 2020
9c59b35
Add some cache object tests
stu-elastic Mar 9, 2020
f8ae8bb
Test per context nodes
stu-elastic Mar 9, 2020
6310977
Merge branch 'master' of github.com:elastic/elasticsearch into fix/50…
stu-elastic Mar 9, 2020
020cc27
Merge branch 'master' of github.com:elastic/elasticsearch into fix/50…
stu-elastic Mar 10, 2020
893f187
ZERO -> SCRIPT_COMPILATION_RATE_ZERO
stu-elastic Mar 10, 2020
b9b626a
SCRIPT_MAX_COMPILATIONS_RATE += _SETTING, CacheUpdater:contextExists …
stu-elastic Mar 11, 2020
6672e64
MAX_COMPILATION_RATE_FUNCTION check of USE_CONTEXT_RATE_KEY moved to …
stu-elastic Mar 11, 2020
bfb77ed
flip assert
stu-elastic Mar 11, 2020
6deb708
script.context.$CONTEXT.{cache_max_size,cache_expire} fallback to gen…
stu-elastic Mar 11, 2020
821cc54
use validator in group settings updater, validate all settings together
stu-elastic Mar 13, 2020
76ec2ab
Update dependencies on context cache updaters
stu-elastic Mar 13, 2020
b588673
Add conflict, fallback, and validator tests
stu-elastic Mar 13, 2020
63ffa65
CacheHolder settings tests
stu-elastic Mar 13, 2020
557c856
Merge branch 'master' of github.com:elastic/elasticsearch into fix/50…
stu-elastic Mar 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ public synchronized void addSettingsUpdateConsumer(Consumer<Settings> consumer,
addSettingsUpdater(Setting.groupedSettingsUpdater(consumer, settings));
}

/**
* Adds a settings consumer that is only executed if any setting in the supplied list of settings is changed. In that case all the
* settings are specified in the argument are returned. The validator is run across all specified settings before the settings are
* applied.
*
* Also automatically adds empty consumers for all settings in order to activate logging
*/
public synchronized void addSettingsUpdateConsumer(Consumer<Settings> consumer, List<? extends Setting<?>> settings,
Consumer<Settings> validator) {
addSettingsUpdater(Setting.groupedSettingsUpdater(consumer, settings, validator));
}

/**
* Adds a settings consumer for affix settings. Affix settings have a namespace associated to it that needs to be available to the
* consumer in order to be processed correctly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,13 @@ public void apply(Settings value, Settings current, Settings previous) {
NetworkService.TCP_RECEIVE_BUFFER_SIZE,
IndexSettings.QUERY_STRING_ANALYZE_WILDCARD,
IndexSettings.QUERY_STRING_ALLOW_LEADING_WILDCARD,
ScriptService.SCRIPT_GENERAL_CACHE_SIZE_SETTING,
ScriptService.SCRIPT_GENERAL_CACHE_EXPIRE_SETTING,
ScriptService.SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING,
ScriptService.SCRIPT_CACHE_SIZE_SETTING,
ScriptService.SCRIPT_CACHE_EXPIRE_SETTING,
ScriptService.SCRIPT_MAX_COMPILATIONS_RATE_SETTING,
ScriptService.SCRIPT_MAX_SIZE_IN_BYTES,
ScriptService.SCRIPT_MAX_COMPILATIONS_RATE,
ScriptService.TYPES_ALLOWED_SETTING,
ScriptService.CONTEXTS_ALLOWED_SETTING,
IndicesService.INDICES_CACHE_CLEAN_INTERVAL_SETTING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,12 @@ public String toString() {

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

static AbstractScopedSettings.SettingUpdater<Settings> groupedSettingsUpdater(Consumer<Settings> consumer,
final List<? extends Setting<?>> configuredSettings,
Consumer<Settings> validator) {
return new AbstractScopedSettings.SettingUpdater<Settings>() {

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

@Override
public Settings getValue(Settings current, Settings previous) {
validator.accept(current);
return get(current);
}

Expand Down
66 changes: 22 additions & 44 deletions server/src/main/java/org/elasticsearch/script/ScriptCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,53 +40,47 @@ public class ScriptCache {

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

private Cache<CacheKey, Object> cache;
private final ScriptMetrics scriptMetrics = new ScriptMetrics();
private final Cache<CacheKey, Object> cache;
private final ScriptMetrics scriptMetrics;

private final Object lock = new Object();

private Tuple<Integer, TimeValue> rate;
// Mutable fields
private long lastInlineCompileTime;
private double scriptsPerTimeWindow;
private double compilesAllowedPerNano;

// Cache settings
private int cacheSize;
private TimeValue cacheExpire;
// Cache settings or derived from settings
final int cacheSize;
final TimeValue cacheExpire;
final Tuple<Integer, TimeValue> rate;
private final double compilesAllowedPerNano;

public ScriptCache(
ScriptCache(
int cacheMaxSize,
TimeValue cacheExpire,
Tuple<Integer, TimeValue> maxCompilationRate
) {
this.cacheSize = cacheMaxSize;
this.cacheExpire = cacheExpire;

CacheBuilder<CacheKey, Object> cacheBuilder = CacheBuilder.builder();
if (cacheMaxSize >= 0) {
cacheBuilder.setMaximumWeight(cacheMaxSize);
if (this.cacheSize >= 0) {
cacheBuilder.setMaximumWeight(this.cacheSize);
}

if (cacheExpire.getNanos() != 0) {
cacheBuilder.setExpireAfterAccess(cacheExpire);
if (this.cacheExpire.getNanos() != 0) {
cacheBuilder.setExpireAfterAccess(this.cacheExpire);
}

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

this.lastInlineCompileTime = System.nanoTime();

this.cacheSize = cacheMaxSize;
this.cacheExpire = cacheExpire;
this.setMaxCompilationRate(maxCompilationRate);
}
this.rate = maxCompilationRate;
this.scriptsPerTimeWindow = this.rate.v1();
this.compilesAllowedPerNano = ((double) rate.v1()) / rate.v2().nanos();

private Cache<CacheKey,Object> buildCache() {
CacheBuilder<CacheKey, Object> cacheBuilder = CacheBuilder.builder();
if (cacheSize >= 0) {
cacheBuilder.setMaximumWeight(cacheSize);
}
if (cacheExpire.getNanos() != 0) {
cacheBuilder.setExpireAfterAccess(cacheExpire);
}
return cacheBuilder.removalListener(new ScriptCacheRemovalListener()).build();
this.lastInlineCompileTime = System.nanoTime();
this.scriptMetrics = new ScriptMetrics();
}

<FactoryType> FactoryType compile(
Expand Down Expand Up @@ -185,22 +179,6 @@ void checkCompilationLimit() {
}
}

/**
* This configures the maximum script compilations per five minute window.
*
* @param newRate the new expected maximum number of compilations per five minute window
*/
void setMaxCompilationRate(Tuple<Integer, TimeValue> newRate) {
synchronized (lock) {
this.rate = newRate;
// Reset the counter to allow new compilations
this.scriptsPerTimeWindow = rate.v1();
this.compilesAllowedPerNano = ((double) rate.v1()) / newRate.v2().nanos();

this.cache = buildCache();
}
}

/**
* A small listener for the script cache that calls each
* {@code ScriptEngine}'s {@code scriptRemoved} method when the
Expand Down
Loading