@@ -86,7 +86,7 @@ public class ScriptService implements Closeable, ClusterStateApplier {
86
86
if (rate < 0 ) {
87
87
throw new IllegalArgumentException ("rate [" + rate + "] must be positive" );
88
88
}
89
- TimeValue timeValue = TimeValue .parseTimeValue (time , "script.max_compilations_rate" );
89
+ TimeValue timeValue = TimeValue .parseTimeValue (time , "script.context.$CONTEXT. max_compilations_rate" );
90
90
if (timeValue .nanos () <= 0 ) {
91
91
throw new IllegalArgumentException ("time value [" + time + "] must be positive" );
92
92
}
@@ -101,16 +101,8 @@ public class ScriptService implements Closeable, ClusterStateApplier {
101
101
}
102
102
};
103
103
104
- public static final Setting <Integer > SCRIPT_GENERAL_CACHE_SIZE_SETTING =
105
- Setting .intSetting ("script.cache.max_size" , 100 , 0 , Property .NodeScope , Property .Deprecated );
106
- public static final Setting <TimeValue > SCRIPT_GENERAL_CACHE_EXPIRE_SETTING =
107
- Setting .positiveTimeSetting ("script.cache.expire" , TimeValue .timeValueMillis (0 ), Property .NodeScope , Property .Deprecated );
108
104
public static final Setting <Integer > SCRIPT_MAX_SIZE_IN_BYTES =
109
105
Setting .intSetting ("script.max_size_in_bytes" , 65535 , 0 , Property .Dynamic , Property .NodeScope );
110
- public static final Setting <Tuple <Integer , TimeValue >> SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING =
111
- new Setting <>("script.max_compilations_rate" , USE_CONTEXT_RATE_KEY ,
112
- (String value ) -> value .equals (USE_CONTEXT_RATE_KEY ) ? USE_CONTEXT_RATE_VALUE : MAX_COMPILATION_RATE_FUNCTION .apply (value ),
113
- Property .Dynamic , Property .NodeScope , Property .Deprecated );
114
106
115
107
// Per-context settings
116
108
static final String CONTEXT_PREFIX = "script.context." ;
@@ -242,7 +234,7 @@ public ScriptService(Settings settings, Map<String, ScriptEngine> engines, Map<S
242
234
243
235
// Validation requires knowing which contexts exist.
244
236
this .validateCacheSettings (settings );
245
- this .setCacheHolder ( settings );
237
+ this .cacheHolder . set ( contextCacheHolder ( settings ) );
246
238
}
247
239
248
240
/**
@@ -261,33 +253,17 @@ void registerClusterSettingsListeners(ClusterSettings clusterSettings) {
261
253
(settings ) -> cacheHolder .get ().set (context .name , contextCache (settings , context )),
262
254
List .of (SCRIPT_CACHE_SIZE_SETTING .getConcreteSettingForNamespace (context .name ),
263
255
SCRIPT_CACHE_EXPIRE_SETTING .getConcreteSettingForNamespace (context .name ),
264
- SCRIPT_MAX_COMPILATIONS_RATE_SETTING .getConcreteSettingForNamespace (context .name ),
265
- SCRIPT_GENERAL_CACHE_EXPIRE_SETTING , // general settings used for fallbacks
266
- SCRIPT_GENERAL_CACHE_SIZE_SETTING
256
+ SCRIPT_MAX_COMPILATIONS_RATE_SETTING .getConcreteSettingForNamespace (context .name )
267
257
)
268
258
);
269
259
}
270
-
271
- // Handle all settings for context and general caches, this flips between general and context caches.
272
- clusterSettings .addSettingsUpdateConsumer (
273
- (settings ) -> setCacheHolder (settings ),
274
- List .of (SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING ,
275
- SCRIPT_GENERAL_CACHE_EXPIRE_SETTING ,
276
- SCRIPT_GENERAL_CACHE_SIZE_SETTING ,
277
- SCRIPT_MAX_COMPILATIONS_RATE_SETTING ,
278
- SCRIPT_DISABLE_MAX_COMPILATIONS_RATE_SETTING ,
279
- SCRIPT_CACHE_EXPIRE_SETTING ,
280
- SCRIPT_CACHE_SIZE_SETTING ),
281
- this ::validateCacheSettings
282
- );
283
260
}
284
261
285
262
/**
286
263
* Throw an IllegalArgumentException if any per-context setting does not match a context or if per-context settings are configured
287
264
* when using the general cache.
288
265
*/
289
266
void validateCacheSettings (Settings settings ) {
290
- boolean useContext = SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings ).equals (USE_CONTEXT_RATE_VALUE );
291
267
List <Setting .AffixSetting <?>> affixes = List .of (SCRIPT_MAX_COMPILATIONS_RATE_SETTING , SCRIPT_CACHE_EXPIRE_SETTING ,
292
268
SCRIPT_CACHE_SIZE_SETTING );
293
269
List <String > customRates = new ArrayList <>();
@@ -304,24 +280,13 @@ void validateCacheSettings(Settings settings) {
304
280
}
305
281
}
306
282
}
307
- if (useContext == false && keys .isEmpty () == false ) {
308
- keys .sort (Comparator .naturalOrder ());
309
- throw new IllegalArgumentException ("Context cache settings [" + String .join (", " , keys ) + "] requires [" +
310
- SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .getKey () + "] to be [" + USE_CONTEXT_RATE_KEY + "]" );
311
- }
312
283
if (SCRIPT_DISABLE_MAX_COMPILATIONS_RATE_SETTING .get (settings )) {
313
284
if (customRates .size () > 0 ) {
314
285
customRates .sort (Comparator .naturalOrder ());
315
286
throw new IllegalArgumentException ("Cannot set custom context compilation rates [" +
316
287
String .join (", " , customRates ) + "] if compile rates disabled via [" +
317
288
SCRIPT_DISABLE_MAX_COMPILATIONS_RATE_SETTING .getKey () + "]" );
318
289
}
319
- if (useContext == false ) {
320
- throw new IllegalArgumentException ("Cannot set custom general compilation rates [" +
321
- SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .getKey () + "] to [" +
322
- SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings ) + "] if compile rates disabled via [" +
323
- SCRIPT_DISABLE_MAX_COMPILATIONS_RATE_SETTING .getKey () + "]" );
324
- }
325
290
}
326
291
}
327
292
@@ -585,39 +550,6 @@ public void applyClusterState(ClusterChangedEvent event) {
585
550
clusterState = event .state ();
586
551
}
587
552
588
- void setCacheHolder (Settings settings ) {
589
- CacheHolder current = cacheHolder .get ();
590
- boolean useContext = SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings ).equals (USE_CONTEXT_RATE_VALUE );
591
-
592
- if (current == null ) {
593
- if (useContext ) {
594
- cacheHolder .set (contextCacheHolder (settings ));
595
- } else {
596
- cacheHolder .set (generalCacheHolder (settings ));
597
- }
598
- return ;
599
- }
600
-
601
- // Update
602
- if (useContext ) {
603
- if (current .general != null ) {
604
- // Flipping to context specific
605
- cacheHolder .set (contextCacheHolder (settings ));
606
- }
607
- } else if (current .general == null ) {
608
- // Flipping to general
609
- cacheHolder .set (generalCacheHolder (settings ));
610
- } else if (current .general .rate .equals (SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings )) == false ) {
611
- // General compilation rate changed, that setting is the only dynamically updated general setting
612
- cacheHolder .set (generalCacheHolder (settings ));
613
- }
614
- }
615
-
616
- CacheHolder generalCacheHolder (Settings settings ) {
617
- return new CacheHolder (SCRIPT_GENERAL_CACHE_SIZE_SETTING .get (settings ), SCRIPT_GENERAL_CACHE_EXPIRE_SETTING .get (settings ),
618
- SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .get (settings ), SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING .getKey ());
619
- }
620
-
621
553
CacheHolder contextCacheHolder (Settings settings ) {
622
554
Map <String , ScriptCache > contextCache = new HashMap <>(contexts .size ());
623
555
contexts .forEach ((k , v ) -> contextCache .put (k , contextCache (settings , v )));
@@ -651,29 +583,19 @@ ScriptCache contextCache(Settings settings, ScriptContext<?> context) {
651
583
* 2) context mode, if the context script cache is configured. There is no general cache in this case.
652
584
*/
653
585
static class CacheHolder {
654
- final ScriptCache general ;
655
586
final Map <String , AtomicReference <ScriptCache >> contextCache ;
656
587
657
- CacheHolder (int cacheMaxSize , TimeValue cacheExpire , Tuple <Integer , TimeValue > maxCompilationRate , String contextRateSetting ) {
658
- contextCache = null ;
659
- general = new ScriptCache (cacheMaxSize , cacheExpire , maxCompilationRate , contextRateSetting );
660
- }
661
-
662
588
CacheHolder (Map <String , ScriptCache > context ) {
663
589
Map <String , AtomicReference <ScriptCache >> refs = new HashMap <>(context .size ());
664
590
context .forEach ((k , v ) -> refs .put (k , new AtomicReference <>(v )));
665
591
contextCache = Collections .unmodifiableMap (refs );
666
- general = null ;
667
592
}
668
593
669
594
/**
670
595
* get the cache appropriate for the context. If in general mode, return the general cache. Otherwise return the ScriptCache for
671
596
* the given context. Returns null in context mode if the requested context does not exist.
672
597
*/
673
598
ScriptCache get (String context ) {
674
- if (general != null ) {
675
- return general ;
676
- }
677
599
AtomicReference <ScriptCache > ref = contextCache .get (context );
678
600
if (ref == null ) {
679
601
return null ;
@@ -682,16 +604,10 @@ ScriptCache get(String context) {
682
604
}
683
605
684
606
ScriptStats stats () {
685
- if (general != null ) {
686
- return general .stats ();
687
- }
688
607
return ScriptStats .sum (contextCache .values ().stream ().map (AtomicReference ::get ).map (ScriptCache ::stats )::iterator );
689
608
}
690
609
691
610
ScriptCacheStats cacheStats () {
692
- if (general != null ) {
693
- return new ScriptCacheStats (general .stats ());
694
- }
695
611
Map <String , ScriptStats > context = new HashMap <>(contextCache .size ());
696
612
for (String name : contextCache .keySet ()) {
697
613
context .put (name , contextCache .get (name ).get ().stats ());
@@ -703,9 +619,6 @@ ScriptCacheStats cacheStats() {
703
619
* Update a single context cache if we're in the context cache mode otherwise no-op.
704
620
*/
705
621
void set (String name , ScriptCache cache ) {
706
- if (general != null ) {
707
- return ;
708
- }
709
622
AtomicReference <ScriptCache > ref = contextCache .get (name );
710
623
assert ref != null : "expected script cache to exist for context [" + name + "]" ;
711
624
ScriptCache oldCache = ref .get ();
0 commit comments