forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script: Restore the scripting general cache
Restore the scripting general cache in preparation for deprecation of the context cache, then removal of the context cache in master. Brings back the general cache settings: * `script.max_compilations_rate` * `script.cache.expire` * `script.cache.max_size` `script.cache.max_size` and `script.cache.expire` are now dynamic settings. The context cache is still default, the switch to general cache as default will happen on context cache deprecation. System script contexts can now opt-out of compilation rate limiting using a flag rather than a sentinel rate limit value. Duplicated defaults for system contexts have been coalesced. Other than that, this code is the same as what was in 7.x to make a `master`-first commit and backport strategy easy. Refs: elastic#62899
- Loading branch information
1 parent
6192cb2
commit e709412
Showing
16 changed files
with
644 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
server/src/main/java/org/elasticsearch/script/ScriptCacheStats.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.script; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.xcontent.ToXContentFragment; | ||
import org.elasticsearch.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
// This class is deprecated in favor of ScriptStats and ScriptContextStats | ||
public class ScriptCacheStats implements Writeable, ToXContentFragment { | ||
private final Map<String, ScriptStats> context; | ||
private final ScriptStats general; | ||
|
||
public ScriptCacheStats(Map<String, ScriptStats> context) { | ||
this.context = Collections.unmodifiableMap(context); | ||
this.general = null; | ||
} | ||
|
||
public ScriptCacheStats(ScriptStats general) { | ||
this.general = Objects.requireNonNull(general); | ||
this.context = null; | ||
} | ||
|
||
public ScriptCacheStats(StreamInput in) throws IOException { | ||
boolean isContext = in.readBoolean(); | ||
if (isContext == false) { | ||
general = new ScriptStats(in); | ||
context = null; | ||
return; | ||
} | ||
|
||
general = null; | ||
int size = in.readInt(); | ||
Map<String,ScriptStats> context = new HashMap<>(size); | ||
for (int i=0; i < size; i++) { | ||
String name = in.readString(); | ||
context.put(name, new ScriptStats(in)); | ||
} | ||
this.context = Collections.unmodifiableMap(context); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
if (general != null) { | ||
out.writeBoolean(false); | ||
general.writeTo(out); | ||
return; | ||
} | ||
|
||
out.writeBoolean(true); | ||
out.writeInt(context.size()); | ||
for (String name: context.keySet().stream().sorted().collect(Collectors.toList())) { | ||
out.writeString(name); | ||
context.get(name).writeTo(out); | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(Fields.SCRIPT_CACHE_STATS); | ||
builder.startObject(Fields.SUM); | ||
if (general != null) { | ||
builder.field(ScriptStats.Fields.COMPILATIONS, general.getCompilations()); | ||
builder.field(ScriptStats.Fields.CACHE_EVICTIONS, general.getCacheEvictions()); | ||
builder.field(ScriptStats.Fields.COMPILATION_LIMIT_TRIGGERED, general.getCompilationLimitTriggered()); | ||
builder.endObject().endObject(); | ||
return builder; | ||
} | ||
|
||
ScriptStats sum = sum(); | ||
builder.field(ScriptStats.Fields.COMPILATIONS, sum.getCompilations()); | ||
builder.field(ScriptStats.Fields.CACHE_EVICTIONS, sum.getCacheEvictions()); | ||
builder.field(ScriptStats.Fields.COMPILATION_LIMIT_TRIGGERED, sum.getCompilationLimitTriggered()); | ||
builder.endObject(); | ||
|
||
builder.startArray(Fields.CONTEXTS); | ||
for (String name: context.keySet().stream().sorted().collect(Collectors.toList())) { | ||
ScriptStats stats = context.get(name); | ||
builder.startObject(); | ||
builder.field(Fields.CONTEXT, name); | ||
builder.field(ScriptStats.Fields.COMPILATIONS, stats.getCompilations()); | ||
builder.field(ScriptStats.Fields.CACHE_EVICTIONS, stats.getCacheEvictions()); | ||
builder.field(ScriptStats.Fields.COMPILATION_LIMIT_TRIGGERED, stats.getCompilationLimitTriggered()); | ||
builder.endObject(); | ||
} | ||
builder.endArray(); | ||
builder.endObject(); | ||
|
||
return builder; | ||
} | ||
|
||
/** | ||
* Get the context specific stats, null if using general cache | ||
*/ | ||
public Map<String, ScriptStats> getContextStats() { | ||
return context; | ||
} | ||
|
||
/** | ||
* Get the general stats, null if using context cache | ||
*/ | ||
public ScriptStats getGeneralStats() { | ||
return general; | ||
} | ||
|
||
/** | ||
* The sum of all script stats, either the general stats or the sum of all stats of the context stats. | ||
*/ | ||
public ScriptStats sum() { | ||
if (general != null) { | ||
return general; | ||
} | ||
long compilations = 0; | ||
long cacheEvictions = 0; | ||
long compilationLimitTriggered = 0; | ||
for (ScriptStats stat: context.values()) { | ||
compilations += stat.getCompilations(); | ||
cacheEvictions += stat.getCacheEvictions(); | ||
compilationLimitTriggered += stat.getCompilationLimitTriggered(); | ||
} | ||
return new ScriptStats( | ||
compilations, | ||
cacheEvictions, | ||
compilationLimitTriggered | ||
); | ||
} | ||
|
||
static final class Fields { | ||
static final String SCRIPT_CACHE_STATS = "script_cache"; | ||
static final String CONTEXT = "context"; | ||
static final String SUM = "sum"; | ||
static final String CONTEXTS = "contexts"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.