4747import java .nio .charset .StandardCharsets ;
4848import java .util .ArrayList ;
4949import java .util .Arrays ;
50+ import java .util .Collection ;
5051import java .util .Collections ;
5152import java .util .Comparator ;
5253import java .util .HashMap ;
@@ -242,7 +243,7 @@ public ScriptService(Settings settings, Map<String, ScriptEngine> engines, Map<S
242243
243244 // Validation requires knowing which contexts exist.
244245 this .validateCacheSettings (settings );
245- cacheHolder = new AtomicReference <>(new CacheHolder (settings , contexts .keySet (), compilationLimitsEnabled ()));
246+ cacheHolder = new AtomicReference <>(new CacheHolder (settings , contexts .values (), compilationLimitsEnabled ()));
246247 }
247248
248249 /**
@@ -256,12 +257,12 @@ void registerClusterSettingsListeners(ClusterSettings clusterSettings) {
256257 clusterSettings .addSettingsUpdateConsumer (SCRIPT_MAX_SIZE_IN_BYTES , this ::setMaxSizeInBytes );
257258
258259 // Handle all updatable per-context settings at once for each context.
259- for (String context : contexts .keySet ()) {
260+ for (ScriptContext <?> context : contexts .values ()) {
260261 clusterSettings .addSettingsUpdateConsumer (
261262 (settings ) -> cacheHolder .get ().updateContextSettings (settings , context ),
262- Arrays .asList (SCRIPT_CACHE_SIZE_SETTING .getConcreteSettingForNamespace (context ),
263- SCRIPT_CACHE_EXPIRE_SETTING .getConcreteSettingForNamespace (context ),
264- SCRIPT_MAX_COMPILATIONS_RATE_SETTING .getConcreteSettingForNamespace (context ),
263+ Arrays .asList (SCRIPT_CACHE_SIZE_SETTING .getConcreteSettingForNamespace (context . name ),
264+ SCRIPT_CACHE_EXPIRE_SETTING .getConcreteSettingForNamespace (context . name ),
265+ SCRIPT_MAX_COMPILATIONS_RATE_SETTING .getConcreteSettingForNamespace (context . name ),
265266 SCRIPT_GENERAL_CACHE_EXPIRE_SETTING ,
266267 // general settings used for fallbacks
267268 SCRIPT_GENERAL_CACHE_SIZE_SETTING )
@@ -581,17 +582,18 @@ static class CacheHolder {
581582 final ScriptCache general ;
582583 final Map <String , AtomicReference <ScriptCache >> contextCache ;
583584
584- final Set <String > contexts ;
585+ final Set <ScriptContext <?> > contexts ;
585586 final boolean compilationLimitsEnabled ;
586587
587- CacheHolder (Settings settings , Set < String > contexts , boolean compilationLimitsEnabled ) {
588+ CacheHolder (Settings settings , Collection < ScriptContext <?> > contexts , boolean compilationLimitsEnabled ) {
588589 this .compilationLimitsEnabled = compilationLimitsEnabled ;
589- this .contexts = Collections .unmodifiableSet (contexts );
590+ this .contexts = Collections .unmodifiableSet (new HashSet <>( contexts ) );
590591 if (SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings ).equals (USE_CONTEXT_RATE_VALUE )) {
591592 this .general = null ;
592593 Map <String , AtomicReference <ScriptCache >> contextCache = new HashMap <>(this .contexts .size ());
593- for (String context : this .contexts ) {
594- contextCache .put (context , new AtomicReference <>(contextFromSettings (settings , context , this .compilationLimitsEnabled )));
594+ for (ScriptContext <?> context : this .contexts ) {
595+ contextCache .put (context .name ,
596+ new AtomicReference <>(contextFromSettings (settings , context , this .compilationLimitsEnabled )));
595597 }
596598 this .contextCache = Collections .unmodifiableMap (contextCache );
597599 } else {
@@ -608,12 +610,24 @@ static class CacheHolder {
608610 /**
609611 * Create a ScriptCache for the given context.
610612 */
611- private static ScriptCache contextFromSettings (Settings settings , String context , boolean compilationLimitsEnabled ) {
612- return new ScriptCache (SCRIPT_CACHE_SIZE_SETTING .getConcreteSettingForNamespace (context ).get (settings ),
613- SCRIPT_CACHE_EXPIRE_SETTING .getConcreteSettingForNamespace (context ).get (settings ),
614- compilationLimitsEnabled ?
615- SCRIPT_MAX_COMPILATIONS_RATE_SETTING .getConcreteSettingForNamespace (context ).get (settings ) :
616- SCRIPT_COMPILATION_RATE_ZERO );
613+ private static ScriptCache contextFromSettings (Settings settings , ScriptContext <?> context , boolean compilationLimitsEnabled ) {
614+ String name = context .name ;
615+ Tuple <Integer , TimeValue > compileRate ;
616+ Setting <Tuple <Integer , TimeValue >> rateSetting = SCRIPT_MAX_COMPILATIONS_RATE_SETTING .getConcreteSettingForNamespace (name );
617+ if (compilationLimitsEnabled == false ) {
618+ compileRate = SCRIPT_COMPILATION_RATE_ZERO ;
619+ } else if (rateSetting .existsOrFallbackExists (settings )) {
620+ compileRate = rateSetting .get (settings );
621+ } else {
622+ compileRate = context .maxCompilationRateDefault ;
623+ }
624+
625+ Setting <TimeValue > cacheExpire = SCRIPT_CACHE_EXPIRE_SETTING .getConcreteSettingForNamespace (name );
626+ Setting <Integer > cacheSize = SCRIPT_CACHE_SIZE_SETTING .getConcreteSettingForNamespace (name );
627+
628+ return new ScriptCache (cacheSize .existsOrFallbackExists (settings ) ? cacheSize .get (settings ) : context .cacheSizeDefault ,
629+ cacheExpire .existsOrFallbackExists (settings ) ? cacheExpire .get (settings ) : context .cacheExpireDefault ,
630+ compileRate );
617631 }
618632
619633 /**
@@ -667,16 +681,16 @@ ScriptStats stats() {
667681 /**
668682 * Update settings for the context cache, if we're in the context cache mode otherwise no-op.
669683 */
670- void updateContextSettings (Settings settings , String context ) {
684+ void updateContextSettings (Settings settings , ScriptContext <?> context ) {
671685 if (general != null ) {
672686 return ;
673687 }
674- AtomicReference <ScriptCache > ref = contextCache .get (context );
675- assert ref != null : "expected script cache to exist for context [" + context + "]" ;
688+ AtomicReference <ScriptCache > ref = contextCache .get (context . name );
689+ assert ref != null : "expected script cache to exist for context [" + context . name + "]" ;
676690 ScriptCache cache = ref .get ();
677- assert cache != null : "expected script cache to be non-null for context [" + context + "]" ;
691+ assert cache != null : "expected script cache to be non-null for context [" + context . name + "]" ;
678692 ref .set (contextFromSettings (settings , context , compilationLimitsEnabled ));
679- logger .debug ("Replaced context [" + context + "] with new settings" );
693+ logger .debug ("Replaced context [" + context . name + "] with new settings" );
680694 }
681695 }
682696}
0 commit comments