|
@@ -9,46 +9,47 @@
|
|
package org.elasticsearch.script;
|
|
package org.elasticsearch.script;
|
|
|
|
|
|
import org.elasticsearch.TransportVersion;
|
|
import org.elasticsearch.TransportVersion;
|
|
|
|
+import org.elasticsearch.common.collect.Iterators;
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
import org.elasticsearch.common.io.stream.Writeable;
|
|
import org.elasticsearch.common.io.stream.Writeable;
|
|
import org.elasticsearch.common.util.Maps;
|
|
import org.elasticsearch.common.util.Maps;
|
|
-import org.elasticsearch.xcontent.ToXContentFragment;
|
|
|
|
-import org.elasticsearch.xcontent.XContentBuilder;
|
|
|
|
|
|
+import org.elasticsearch.common.xcontent.ChunkedToXContent;
|
|
|
|
+import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
|
|
|
|
+import org.elasticsearch.xcontent.ToXContent;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Collections;
|
|
|
|
|
|
+import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
-
|
|
|
|
-public class ScriptStats implements Writeable, ToXContentFragment {
|
|
|
|
- private final List<ScriptContextStats> contextStats;
|
|
|
|
- private final long compilations;
|
|
|
|
- private final long cacheEvictions;
|
|
|
|
- private final long compilationLimitTriggered;
|
|
|
|
- private final TimeSeries compilationsHistory;
|
|
|
|
- private final TimeSeries cacheEvictionsHistory;
|
|
|
|
-
|
|
|
|
- public ScriptStats(List<ScriptContextStats> contextStats) {
|
|
|
|
- ArrayList<ScriptContextStats> ctxStats = new ArrayList<>(contextStats.size());
|
|
|
|
- ctxStats.addAll(contextStats);
|
|
|
|
- ctxStats.sort(ScriptContextStats::compareTo);
|
|
|
|
- this.contextStats = Collections.unmodifiableList(ctxStats);
|
|
|
|
- long compilations = 0;
|
|
|
|
- long cacheEvictions = 0;
|
|
|
|
- long compilationLimitTriggered = 0;
|
|
|
|
- for (ScriptContextStats stats : contextStats) {
|
|
|
|
- compilations += stats.getCompilations();
|
|
|
|
- cacheEvictions += stats.getCacheEvictions();
|
|
|
|
- compilationLimitTriggered += stats.getCompilationLimitTriggered();
|
|
|
|
- }
|
|
|
|
- this.compilations = compilations;
|
|
|
|
- this.cacheEvictions = cacheEvictions;
|
|
|
|
- this.compilationLimitTriggered = compilationLimitTriggered;
|
|
|
|
- this.compilationsHistory = new TimeSeries(compilations);
|
|
|
|
- this.cacheEvictionsHistory = new TimeSeries(cacheEvictions);
|
|
|
|
- }
|
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
+
|
|
|
|
+import static org.elasticsearch.common.collect.Iterators.single;
|
|
|
|
+import static org.elasticsearch.script.ScriptContextStats.Fields.COMPILATIONS_HISTORY;
|
|
|
|
+import static org.elasticsearch.script.ScriptStats.Fields.CACHE_EVICTIONS;
|
|
|
|
+import static org.elasticsearch.script.ScriptStats.Fields.COMPILATIONS;
|
|
|
|
+import static org.elasticsearch.script.ScriptStats.Fields.COMPILATION_LIMIT_TRIGGERED;
|
|
|
|
+import static org.elasticsearch.script.ScriptStats.Fields.CONTEXTS;
|
|
|
|
+import static org.elasticsearch.script.ScriptStats.Fields.SCRIPT_STATS;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Record object that holds global statistics of the scripts in a node.
|
|
|
|
+ *
|
|
|
|
+ * @param contextStats A list of different {@link ScriptContextStats}
|
|
|
|
+ * @param compilations Total number of compilations.
|
|
|
|
+ * @param cacheEvictions Total number of evictions.
|
|
|
|
+ * @param compilationLimitTriggered Total number of times that the compilation time has been reached.
|
|
|
|
+ * @param compilationsHistory Historical information of the compilations in timeseries format.
|
|
|
|
+ * @param cacheEvictionsHistory Historical information of the evictions in timeseries format.
|
|
|
|
+ */
|
|
|
|
+public record ScriptStats(
|
|
|
|
+ List<ScriptContextStats> contextStats,
|
|
|
|
+ long compilations,
|
|
|
|
+ long cacheEvictions,
|
|
|
|
+ long compilationLimitTriggered,
|
|
|
|
+ TimeSeries compilationsHistory,
|
|
|
|
+ TimeSeries cacheEvictionsHistory
|
|
|
|
+) implements Writeable, ChunkedToXContent {
|
|
|
|
|
|
public ScriptStats(
|
|
public ScriptStats(
|
|
long compilations,
|
|
long compilations,
|
|
@@ -57,16 +58,37 @@ public class ScriptStats implements Writeable, ToXContentFragment {
|
|
TimeSeries compilationsHistory,
|
|
TimeSeries compilationsHistory,
|
|
TimeSeries cacheEvictionsHistory
|
|
TimeSeries cacheEvictionsHistory
|
|
) {
|
|
) {
|
|
- this.contextStats = Collections.emptyList();
|
|
|
|
- this.compilations = compilations;
|
|
|
|
- this.cacheEvictions = cacheEvictions;
|
|
|
|
- this.compilationLimitTriggered = compilationLimitTriggered;
|
|
|
|
- this.compilationsHistory = compilationsHistory == null ? new TimeSeries(compilations) : compilationsHistory;
|
|
|
|
- this.cacheEvictionsHistory = cacheEvictionsHistory == null ? new TimeSeries(cacheEvictions) : cacheEvictionsHistory;
|
|
|
|
|
|
+ this(
|
|
|
|
+ List.of(),
|
|
|
|
+ compilations,
|
|
|
|
+ cacheEvictions,
|
|
|
|
+ compilationLimitTriggered,
|
|
|
|
+ Objects.requireNonNullElseGet(compilationsHistory, () -> new TimeSeries(compilations)),
|
|
|
|
+ Objects.requireNonNullElseGet(cacheEvictionsHistory, () -> new TimeSeries(cacheEvictions))
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
- public ScriptStats(ScriptContextStats context) {
|
|
|
|
- this(
|
|
|
|
|
|
+ public static ScriptStats read(List<ScriptContextStats> contextStats) {
|
|
|
|
+ long compilations = 0;
|
|
|
|
+ long cacheEvictions = 0;
|
|
|
|
+ long compilationLimitTriggered = 0;
|
|
|
|
+ for (var stats : contextStats) {
|
|
|
|
+ compilations += stats.getCompilations();
|
|
|
|
+ cacheEvictions += stats.getCacheEvictions();
|
|
|
|
+ compilationLimitTriggered += stats.getCompilationLimitTriggered();
|
|
|
|
+ }
|
|
|
|
+ return new ScriptStats(
|
|
|
|
+ contextStats.stream().sorted(ScriptContextStats::compareTo).toList(),
|
|
|
|
+ compilations,
|
|
|
|
+ cacheEvictions,
|
|
|
|
+ compilationLimitTriggered,
|
|
|
|
+ new TimeSeries(compilations),
|
|
|
|
+ new TimeSeries(cacheEvictions)
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static ScriptStats read(ScriptContextStats context) {
|
|
|
|
+ return new ScriptStats(
|
|
context.getCompilations(),
|
|
context.getCompilations(),
|
|
context.getCacheEvictions(),
|
|
context.getCacheEvictions(),
|
|
context.getCompilationLimitTriggered(),
|
|
context.getCompilationLimitTriggered(),
|
|
@@ -75,7 +97,11 @@ public class ScriptStats implements Writeable, ToXContentFragment {
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
|
|
- public ScriptStats(StreamInput in) throws IOException {
|
|
|
|
|
|
+ public static ScriptStats read(StreamInput in) throws IOException {
|
|
|
|
+ TimeSeries compilationsHistory;
|
|
|
|
+ TimeSeries cacheEvictionsHistory;
|
|
|
|
+ long compilations;
|
|
|
|
+ long cacheEvictions;
|
|
if (in.getTransportVersion().onOrAfter(TransportVersion.V_8_1_0)) {
|
|
if (in.getTransportVersion().onOrAfter(TransportVersion.V_8_1_0)) {
|
|
compilationsHistory = new TimeSeries(in);
|
|
compilationsHistory = new TimeSeries(in);
|
|
cacheEvictionsHistory = new TimeSeries(in);
|
|
cacheEvictionsHistory = new TimeSeries(in);
|
|
@@ -87,8 +113,16 @@ public class ScriptStats implements Writeable, ToXContentFragment {
|
|
compilationsHistory = new TimeSeries(compilations);
|
|
compilationsHistory = new TimeSeries(compilations);
|
|
cacheEvictionsHistory = new TimeSeries(cacheEvictions);
|
|
cacheEvictionsHistory = new TimeSeries(cacheEvictions);
|
|
}
|
|
}
|
|
- compilationLimitTriggered = in.readVLong();
|
|
|
|
- contextStats = in.readList(ScriptContextStats::new);
|
|
|
|
|
|
+ var compilationLimitTriggered = in.readVLong();
|
|
|
|
+ var contextStats = in.readList(ScriptContextStats::read);
|
|
|
|
+ return new ScriptStats(
|
|
|
|
+ contextStats,
|
|
|
|
+ compilations,
|
|
|
|
+ cacheEvictions,
|
|
|
|
+ compilationLimitTriggered,
|
|
|
|
+ compilationsHistory,
|
|
|
|
+ cacheEvictionsHistory
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -126,34 +160,34 @@ public class ScriptStats implements Writeable, ToXContentFragment {
|
|
}
|
|
}
|
|
Map<String, ScriptStats> contexts = Maps.newMapWithExpectedSize(contextStats.size());
|
|
Map<String, ScriptStats> contexts = Maps.newMapWithExpectedSize(contextStats.size());
|
|
for (ScriptContextStats contextStats : contextStats) {
|
|
for (ScriptContextStats contextStats : contextStats) {
|
|
- contexts.put(contextStats.getContext(), new ScriptStats(contextStats));
|
|
|
|
|
|
+ contexts.put(contextStats.getContext(), ScriptStats.read(contextStats));
|
|
}
|
|
}
|
|
return new ScriptCacheStats(contexts);
|
|
return new ScriptCacheStats(contexts);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
|
- builder.startObject(Fields.SCRIPT_STATS);
|
|
|
|
- builder.field(Fields.COMPILATIONS, compilations);
|
|
|
|
- builder.field(Fields.CACHE_EVICTIONS, cacheEvictions);
|
|
|
|
- builder.field(Fields.COMPILATION_LIMIT_TRIGGERED, compilationLimitTriggered);
|
|
|
|
- if (compilationsHistory != null && compilationsHistory.areTimingsEmpty() == false) {
|
|
|
|
- builder.startObject(ScriptContextStats.Fields.COMPILATIONS_HISTORY);
|
|
|
|
- compilationsHistory.toXContent(builder, params);
|
|
|
|
- builder.endObject();
|
|
|
|
- }
|
|
|
|
- if (cacheEvictionsHistory != null && cacheEvictionsHistory.areTimingsEmpty() == false) {
|
|
|
|
- builder.startObject(ScriptContextStats.Fields.COMPILATIONS_HISTORY);
|
|
|
|
- cacheEvictionsHistory.toXContent(builder, params);
|
|
|
|
- builder.endObject();
|
|
|
|
- }
|
|
|
|
- builder.startArray(Fields.CONTEXTS);
|
|
|
|
- for (ScriptContextStats contextStats : contextStats) {
|
|
|
|
- contextStats.toXContent(builder, params);
|
|
|
|
- }
|
|
|
|
- builder.endArray();
|
|
|
|
- builder.endObject();
|
|
|
|
- return builder;
|
|
|
|
|
|
+ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerParams) {
|
|
|
|
+ return Iterators.concat(
|
|
|
|
+ ChunkedToXContentHelper.startObject(SCRIPT_STATS),
|
|
|
|
+ ChunkedToXContentHelper.field(COMPILATIONS, compilations),
|
|
|
|
+ ChunkedToXContentHelper.field(CACHE_EVICTIONS, cacheEvictions),
|
|
|
|
+ ChunkedToXContentHelper.field(COMPILATION_LIMIT_TRIGGERED, compilationLimitTriggered),
|
|
|
|
+ single((builder, params) -> {
|
|
|
|
+ if (compilationsHistory != null && compilationsHistory.areTimingsEmpty() == false) {
|
|
|
|
+ builder.startObject(COMPILATIONS_HISTORY);
|
|
|
|
+ compilationsHistory.toXContent(builder, params);
|
|
|
|
+ builder.endObject();
|
|
|
|
+ }
|
|
|
|
+ if (cacheEvictionsHistory != null && cacheEvictionsHistory.areTimingsEmpty() == false) {
|
|
|
|
+ builder.startObject(COMPILATIONS_HISTORY);
|
|
|
|
+ cacheEvictionsHistory.toXContent(builder, params);
|
|
|
|
+ builder.endObject();
|
|
|
|
+ }
|
|
|
|
+ return builder;
|
|
|
|
+ }),
|
|
|
|
+ ChunkedToXContentHelper.array(CONTEXTS, contextStats.iterator()),
|
|
|
|
+ ChunkedToXContentHelper.endObject()
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
static final class Fields {
|
|
static final class Fields {
|