2323import org .elasticsearch .cluster .metadata .Metadata ;
2424import org .elasticsearch .cluster .service .ClusterService ;
2525import org .elasticsearch .common .Strings ;
26+ import org .elasticsearch .common .logging .DeprecationCategory ;
27+ import org .elasticsearch .common .logging .DeprecationLogger ;
2628import org .elasticsearch .common .settings .ClusterSettings ;
2729import org .elasticsearch .common .settings .Setting ;
2830import org .elasticsearch .common .settings .Setting .Property ;
5052public class ScriptService implements Closeable , ClusterStateApplier , ScriptCompiler {
5153
5254 private static final Logger logger = LogManager .getLogger (ScriptService .class );
55+ private static final DeprecationLogger deprecationLogger = DeprecationLogger .getLogger (ScriptService .class );
5356
5457 static final String DISABLE_DYNAMIC_SCRIPTING_SETTING = "script.disable_dynamic" ;
5558
@@ -59,16 +62,22 @@ public class ScriptService implements Closeable, ClusterStateApplier, ScriptComp
5962 static final String USE_CONTEXT_RATE_KEY = "use-context" ;
6063
6164 public static final Setting <Integer > SCRIPT_GENERAL_CACHE_SIZE_SETTING =
62- Setting .intSetting ("script.cache.max_size" , 100 , 0 , Property .NodeScope );
65+ Setting .intSetting ("script.cache.max_size" , 3000 , 0 , Property . Dynamic , Property .NodeScope );
6366 public static final Setting <TimeValue > SCRIPT_GENERAL_CACHE_EXPIRE_SETTING =
64- Setting .positiveTimeSetting ("script.cache.expire" , TimeValue .timeValueMillis (0 ), Property .NodeScope );
67+ Setting .positiveTimeSetting ("script.cache.expire" , TimeValue .timeValueMillis (0 ), Property .Dynamic , Property . NodeScope );
6568 public static final Setting <Integer > SCRIPT_MAX_SIZE_IN_BYTES =
6669 Setting .intSetting ("script.max_size_in_bytes" , 65535 , 0 , Property .Dynamic , Property .NodeScope );
6770 public static final Setting <ScriptCache .CompilationRate > SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING =
68- new Setting <>("script.max_compilations_rate" , USE_CONTEXT_RATE_KEY ,
71+ new Setting <>("script.max_compilations_rate" , "150/5m" ,
6972 (String value ) -> value .equals (USE_CONTEXT_RATE_KEY ) ? USE_CONTEXT_RATE_VALUE : new ScriptCache .CompilationRate (value ),
7073 Property .Dynamic , Property .NodeScope );
7174
75+ public static final String USE_CONTEXT_RATE_KEY_DEPRECATION_MESSAGE = "[" + USE_CONTEXT_RATE_KEY + "] is deprecated for the setting [" +
76+ SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .getKey () + "] as system scripts are now exempt from the rate limit. " +
77+ "Set to a value such as [150/5m] (a rate of 150 compilations per five minutes) to rate limit user scripts in case the " +
78+ "script cache [" + SCRIPT_GENERAL_CACHE_SIZE_SETTING .getKey () + "] is undersized causing script compilation thrashing." ;
79+
80+
7281 // Per-context settings
7382 static final String CONTEXT_PREFIX = "script.context." ;
7483
@@ -77,13 +86,14 @@ public class ScriptService implements Closeable, ClusterStateApplier, ScriptComp
7786 public static final Setting .AffixSetting <Integer > SCRIPT_CACHE_SIZE_SETTING =
7887 Setting .affixKeySetting (CONTEXT_PREFIX ,
7988 "cache_max_size" ,
80- key -> Setting .intSetting (key , SCRIPT_GENERAL_CACHE_SIZE_SETTING , 0 , Property .NodeScope , Property .Dynamic ));
89+ key -> Setting .intSetting (key , SCRIPT_GENERAL_CACHE_SIZE_SETTING , 0 ,
90+ Property .NodeScope , Property .Dynamic , Property .Deprecated ));
8191
8292 public static final Setting .AffixSetting <TimeValue > SCRIPT_CACHE_EXPIRE_SETTING =
8393 Setting .affixKeySetting (CONTEXT_PREFIX ,
8494 "cache_expire" ,
8595 key -> Setting .positiveTimeSetting (key , SCRIPT_GENERAL_CACHE_EXPIRE_SETTING , TimeValue .timeValueMillis (0 ),
86- Property .NodeScope , Property .Dynamic ));
96+ Property .NodeScope , Property .Dynamic , Property . Deprecated ));
8797
8898 // Unlimited compilation rate for context-specific script caches
8999 static final String UNLIMITED_COMPILATION_RATE_KEY = "unlimited" ;
@@ -94,7 +104,7 @@ public class ScriptService implements Closeable, ClusterStateApplier, ScriptComp
94104 key -> new Setting <ScriptCache .CompilationRate >(key , "75/5m" ,
95105 (String value ) -> value .equals (UNLIMITED_COMPILATION_RATE_KEY ) ? ScriptCache .UNLIMITED_COMPILATION_RATE :
96106 new ScriptCache .CompilationRate (value ),
97- Property .NodeScope , Property .Dynamic ));
107+ Property .NodeScope , Property .Dynamic , Property . Deprecated ));
98108
99109 private static final ScriptCache .CompilationRate SCRIPT_COMPILATION_RATE_ZERO = new ScriptCache .CompilationRate (0 , TimeValue .ZERO );
100110
@@ -205,6 +215,10 @@ public ScriptService(Settings settings, Map<String, ScriptEngine> engines, Map<S
205215 this .setCacheHolder (settings );
206216 }
207217
218+ public static boolean isUseContextCacheSet (Settings settings ) {
219+ return SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings ).equals (USE_CONTEXT_RATE_VALUE );
220+ }
221+
208222 /**
209223 * This is overridden in tests to disable compilation rate limiting.
210224 */
@@ -249,6 +263,10 @@ void registerClusterSettingsListeners(ClusterSettings clusterSettings) {
249263 */
250264 void validateCacheSettings (Settings settings ) {
251265 boolean useContext = SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings ).equals (USE_CONTEXT_RATE_VALUE );
266+ if (useContext ) {
267+ deprecationLogger .warn (DeprecationCategory .SCRIPTING , "scripting-context-cache" ,
268+ USE_CONTEXT_RATE_KEY_DEPRECATION_MESSAGE );
269+ }
252270 List <Setting .AffixSetting <?>> affixes = Arrays .asList (SCRIPT_MAX_COMPILATIONS_RATE_SETTING , SCRIPT_CACHE_EXPIRE_SETTING ,
253271 SCRIPT_CACHE_SIZE_SETTING );
254272 List <String > customRates = new ArrayList <>();
@@ -277,7 +295,7 @@ void validateCacheSettings(Settings settings) {
277295 String .join (", " , customRates ) + "] if compile rates disabled via [" +
278296 SCRIPT_DISABLE_MAX_COMPILATIONS_RATE_SETTING .getKey () + "]" );
279297 }
280- if (useContext == false ) {
298+ if (useContext == false && SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING . exists ( settings ) ) {
281299 throw new IllegalArgumentException ("Cannot set custom general compilation rates [" +
282300 SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .getKey () + "] to [" +
283301 SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings ) + "] if compile rates disabled via [" +
@@ -568,6 +586,7 @@ void setCacheHolder(Settings settings) {
568586 } else if (current .general .rate .equals (SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings )) == false ||
569587 current .general .cacheExpire .equals (SCRIPT_GENERAL_CACHE_EXPIRE_SETTING .get (settings )) == false ||
570588 current .general .cacheSize != SCRIPT_GENERAL_CACHE_SIZE_SETTING .get (settings )) {
589+ // General compilation rate, cache expiration or cache size changed
571590 cacheHolder .set (generalCacheHolder (settings ));
572591 }
573592 }
0 commit comments