46
46
import java .io .IOException ;
47
47
import java .nio .charset .StandardCharsets ;
48
48
import java .util .ArrayList ;
49
+ import java .util .Collection ;
49
50
import java .util .Collections ;
50
51
import java .util .Comparator ;
51
52
import java .util .HashMap ;
@@ -241,7 +242,7 @@ public ScriptService(Settings settings, Map<String, ScriptEngine> engines, Map<S
241
242
242
243
// Validation requires knowing which contexts exist.
243
244
this .validateCacheSettings (settings );
244
- cacheHolder = new AtomicReference <>(new CacheHolder (settings , contexts .keySet (), compilationLimitsEnabled ()));
245
+ cacheHolder = new AtomicReference <>(new CacheHolder (settings , contexts .values (), compilationLimitsEnabled ()));
245
246
}
246
247
247
248
/**
@@ -255,12 +256,12 @@ void registerClusterSettingsListeners(ClusterSettings clusterSettings) {
255
256
clusterSettings .addSettingsUpdateConsumer (SCRIPT_MAX_SIZE_IN_BYTES , this ::setMaxSizeInBytes );
256
257
257
258
// Handle all updatable per-context settings at once for each context.
258
- for (String context : contexts .keySet ()) {
259
+ for (ScriptContext <?> context : contexts .values ()) {
259
260
clusterSettings .addSettingsUpdateConsumer (
260
261
(settings ) -> cacheHolder .get ().updateContextSettings (settings , context ),
261
- List .of (SCRIPT_CACHE_SIZE_SETTING .getConcreteSettingForNamespace (context ),
262
- SCRIPT_CACHE_EXPIRE_SETTING .getConcreteSettingForNamespace (context ),
263
- SCRIPT_MAX_COMPILATIONS_RATE_SETTING .getConcreteSettingForNamespace (context ),
262
+ List .of (SCRIPT_CACHE_SIZE_SETTING .getConcreteSettingForNamespace (context . name ),
263
+ SCRIPT_CACHE_EXPIRE_SETTING .getConcreteSettingForNamespace (context . name ),
264
+ SCRIPT_MAX_COMPILATIONS_RATE_SETTING .getConcreteSettingForNamespace (context . name ),
264
265
SCRIPT_GENERAL_CACHE_EXPIRE_SETTING , // general settings used for fallbacks
265
266
SCRIPT_GENERAL_CACHE_SIZE_SETTING
266
267
)
@@ -580,17 +581,18 @@ static class CacheHolder {
580
581
final ScriptCache general ;
581
582
final Map <String , AtomicReference <ScriptCache >> contextCache ;
582
583
583
- final Set <String > contexts ;
584
+ final Set <ScriptContext <?> > contexts ;
584
585
final boolean compilationLimitsEnabled ;
585
586
586
- CacheHolder (Settings settings , Set < String > contexts , boolean compilationLimitsEnabled ) {
587
+ CacheHolder (Settings settings , Collection < ScriptContext <?> > contexts , boolean compilationLimitsEnabled ) {
587
588
this .compilationLimitsEnabled = compilationLimitsEnabled ;
588
589
this .contexts = Set .copyOf (contexts );
589
590
if (SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings ).equals (USE_CONTEXT_RATE_VALUE )) {
590
591
this .general = null ;
591
592
Map <String , AtomicReference <ScriptCache >> contextCache = new HashMap <>(this .contexts .size ());
592
- for (String context : this .contexts ) {
593
- contextCache .put (context , new AtomicReference <>(contextFromSettings (settings , context , this .compilationLimitsEnabled )));
593
+ for (ScriptContext <?> context : this .contexts ) {
594
+ contextCache .put (context .name ,
595
+ new AtomicReference <>(contextFromSettings (settings , context , this .compilationLimitsEnabled )));
594
596
}
595
597
this .contextCache = Collections .unmodifiableMap (contextCache );
596
598
} else {
@@ -607,12 +609,24 @@ static class CacheHolder {
607
609
/**
608
610
* Create a ScriptCache for the given context.
609
611
*/
610
- private static ScriptCache contextFromSettings (Settings settings , String context , boolean compilationLimitsEnabled ) {
611
- return new ScriptCache (SCRIPT_CACHE_SIZE_SETTING .getConcreteSettingForNamespace (context ).get (settings ),
612
- SCRIPT_CACHE_EXPIRE_SETTING .getConcreteSettingForNamespace (context ).get (settings ),
613
- compilationLimitsEnabled ?
614
- SCRIPT_MAX_COMPILATIONS_RATE_SETTING .getConcreteSettingForNamespace (context ).get (settings ) :
615
- SCRIPT_COMPILATION_RATE_ZERO );
612
+ private static ScriptCache contextFromSettings (Settings settings , ScriptContext <?> context , boolean compilationLimitsEnabled ) {
613
+ String name = context .name ;
614
+ Tuple <Integer , TimeValue > compileRate ;
615
+ Setting <Tuple <Integer , TimeValue >> rateSetting = SCRIPT_MAX_COMPILATIONS_RATE_SETTING .getConcreteSettingForNamespace (name );
616
+ if (compilationLimitsEnabled == false ) {
617
+ compileRate = SCRIPT_COMPILATION_RATE_ZERO ;
618
+ } else if (rateSetting .existsOrFallbackExists (settings )) {
619
+ compileRate = rateSetting .get (settings );
620
+ } else {
621
+ compileRate = context .maxCompilationRateDefault ;
622
+ }
623
+
624
+ Setting <TimeValue > cacheExpire = SCRIPT_CACHE_EXPIRE_SETTING .getConcreteSettingForNamespace (name );
625
+ Setting <Integer > cacheSize = SCRIPT_CACHE_SIZE_SETTING .getConcreteSettingForNamespace (name );
626
+
627
+ return new ScriptCache (cacheSize .existsOrFallbackExists (settings ) ? cacheSize .get (settings ) : context .cacheSizeDefault ,
628
+ cacheExpire .existsOrFallbackExists (settings ) ? cacheExpire .get (settings ) : context .cacheExpireDefault ,
629
+ compileRate );
616
630
}
617
631
618
632
/**
@@ -666,16 +680,16 @@ ScriptStats stats() {
666
680
/**
667
681
* Update settings for the context cache, if we're in the context cache mode otherwise no-op.
668
682
*/
669
- void updateContextSettings (Settings settings , String context ) {
683
+ void updateContextSettings (Settings settings , ScriptContext <?> context ) {
670
684
if (general != null ) {
671
685
return ;
672
686
}
673
- AtomicReference <ScriptCache > ref = contextCache .get (context );
674
- assert ref != null : "expected script cache to exist for context [" + context + "]" ;
687
+ AtomicReference <ScriptCache > ref = contextCache .get (context . name );
688
+ assert ref != null : "expected script cache to exist for context [" + context . name + "]" ;
675
689
ScriptCache cache = ref .get ();
676
- assert cache != null : "expected script cache to be non-null for context [" + context + "]" ;
690
+ assert cache != null : "expected script cache to be non-null for context [" + context . name + "]" ;
677
691
ref .set (contextFromSettings (settings , context , compilationLimitsEnabled ));
678
- logger .debug ("Replaced context [" + context + "] with new settings" );
692
+ logger .debug ("Replaced context [" + context . name + "] with new settings" );
679
693
}
680
694
}
681
695
}
0 commit comments