Skip to content

Commit

Permalink
Script: compile/cache eviction history metric placeholders
Browse files Browse the repository at this point in the history
Adds 5m/15m/24h metrics to _nodes/stats when those metrics
are non-zero.  Those metrics are not yet populated.

BWC: history metrics are only sent between v8.0.0+ nodes,
v7.16.0 nodes will not send those metrics until they are populated.

Refs: elastic#62899
  • Loading branch information
stu-elastic committed Sep 23, 2021
1 parent 86b8063 commit 35c9264
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.elasticsearch.script;

import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
Expand All @@ -20,21 +21,33 @@
public class ScriptContextStats implements Writeable, ToXContentFragment, Comparable<ScriptContextStats> {
private final String context;
private final long compilations;
private final TimeSeries compilationsHistory;
private final long cacheEvictions;
private final TimeSeries cacheEvictionsHistory;
private final long compilationLimitTriggered;

public ScriptContextStats(String context, long compilations, long cacheEvictions, long compilationLimitTriggered) {
public ScriptContextStats(String context, long compilations, long cacheEvictions, long compilationLimitTriggered,
TimeSeries compilationsHistory, TimeSeries cacheEvictionsHistory) {
this.context = Objects.requireNonNull(context);
this.compilations = compilations;
this.cacheEvictions = cacheEvictions;
this.compilationLimitTriggered = compilationLimitTriggered;
this.compilationsHistory = compilationsHistory;
this.cacheEvictionsHistory = cacheEvictionsHistory;
}

public ScriptContextStats(StreamInput in) throws IOException {
context = in.readString();
compilations = in.readVLong();
cacheEvictions = in.readVLong();
compilationLimitTriggered = in.readVLong();
if (in.getVersion().before(Version.V_8_0_0)) {
compilationsHistory = null;
cacheEvictionsHistory = null;
} else {
compilationsHistory = in.readOptionalWriteable(TimeSeries::new);
cacheEvictionsHistory = in.readOptionalWriteable(TimeSeries::new);
}
}

@Override
Expand All @@ -43,6 +56,53 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(compilations);
out.writeVLong(cacheEvictions);
out.writeVLong(compilationLimitTriggered);
if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
out.writeOptionalWriteable(compilationsHistory);
out.writeOptionalWriteable(cacheEvictionsHistory);
}
}

public static class TimeSeries implements Writeable, ToXContentFragment {
public final long five;
public final long fifteen;
public final long day;

public TimeSeries() {
this.five = 0;
this.fifteen = 0;
this.day = 0;
}

public TimeSeries(long five, long fifteen, long day) {
this.five = five;
this.fifteen = fifteen;
this.day = day;
}

public TimeSeries(StreamInput in) throws IOException {
five = in.readVLong();
fifteen = in.readVLong();
day = in.readVLong();
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(Fields.FIVE_MINUTES, five);
builder.field(Fields.FIFTEEN_MINUTES, fifteen);
builder.field(Fields.TWENTY_FOUR_HOURS, day);
return builder;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(five);
out.writeVLong(fifteen);
out.writeVLong(day);
}

public boolean isEmpty() {
return five == 0 && fifteen == 0 && day == 0;
}
}

public String getContext() {
Expand All @@ -53,10 +113,18 @@ public long getCompilations() {
return compilations;
}

public TimeSeries getCompilationsHistory() {
return compilationsHistory;
}

public long getCacheEvictions() {
return cacheEvictions;
}

public TimeSeries getCacheEvictionsHistory() {
return cacheEvictionsHistory;
}

public long getCompilationLimitTriggered() {
return compilationLimitTriggered;
}
Expand All @@ -66,7 +134,22 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.startObject();
builder.field(Fields.CONTEXT, getContext());
builder.field(Fields.COMPILATIONS, getCompilations());

TimeSeries series = getCompilationsHistory();
if (series != null && series.isEmpty() == false) {
builder.startObject(Fields.COMPILATIONS_HISTORY);
series.toXContent(builder, params);
builder.endObject();
}

builder.field(Fields.CACHE_EVICTIONS, getCacheEvictions());
series = getCacheEvictionsHistory();
if (series != null && series.isEmpty() == false) {
builder.startObject(Fields.CACHE_EVICTIONS_HISTORY);
series.toXContent(builder, params);
builder.endObject();
}

builder.field(Fields.COMPILATION_LIMIT_TRIGGERED, getCompilationLimitTriggered());
builder.endObject();
return builder;
Expand All @@ -80,7 +163,12 @@ public int compareTo(ScriptContextStats o) {
static final class Fields {
static final String CONTEXT = "context";
static final String COMPILATIONS = "compilations";
static final String COMPILATIONS_HISTORY = "compilations_history";
static final String CACHE_EVICTIONS = "cache_evictions";
static final String CACHE_EVICTIONS_HISTORY = "cache_evictions_history";
static final String COMPILATION_LIMIT_TRIGGERED = "compilation_limit_triggered";
static final String FIVE_MINUTES = "5m";
static final String FIFTEEN_MINUTES = "15m";
static final String TWENTY_FOUR_HOURS = "24h";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public ScriptContextStats stats(String context) {
context,
compilationsMetric.count(),
cacheEvictionsMetric.count(),
compilationLimitTriggered.count()
compilationLimitTriggered.count(),
new ScriptContextStats.TimeSeries(),
new ScriptContextStats.TimeSeries()
);
}}
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,31 @@ public static NodeStats createNodeStats() {
List<ScriptContextStats> stats = new ArrayList<>(numContents);
HashSet<String> contexts = new HashSet<>();
for (int i = 0; i < numContents; i++) {
long compile = randomLongBetween(0, 1024);
ScriptContextStats.TimeSeries compileSeries = null;
if (randomBoolean()) {
compileSeries = new ScriptContextStats.TimeSeries(
randomLongBetween(0, 1024),
randomLongBetween(0, 1024),
randomLongBetween(0, 1024)
);
}
long eviction = randomLongBetween(0, 1024);
ScriptContextStats.TimeSeries evictionSeries = null;
if (randomBoolean()) {
evictionSeries = new ScriptContextStats.TimeSeries(
randomLongBetween(0, 1024),
randomLongBetween(0, 1024),
randomLongBetween(0, 1024)
);
}
stats.add(new ScriptContextStats(
randomValueOtherThanMany(contexts::contains, () -> randomAlphaOfLength(12)),
compile,
eviction,
randomLongBetween(0, 1024),
randomLongBetween(0, 1024),
randomLongBetween(0, 1024))
compileSeries,
evictionSeries)
);
}
scriptStats = new ScriptStats(stats);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
public class ScriptStatsTests extends ESTestCase {
public void testXContent() throws IOException {
List<ScriptContextStats> contextStats = List.of(
new ScriptContextStats("contextB", 100, 201, 302),
new ScriptContextStats("contextA", 1000, 2010, 3020)
new ScriptContextStats("contextB", 100, 201, 302,
new ScriptContextStats.TimeSeries(1000, 1001, 1002),
new ScriptContextStats.TimeSeries(2000, 2001, 2002)
),
new ScriptContextStats("contextA", 1000, 2010, 3020, null, new ScriptContextStats.TimeSeries(0, 0, 0))
);
ScriptStats stats = new ScriptStats(contextStats);
final XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
Expand All @@ -46,7 +49,17 @@ public void testXContent() throws IOException {
" {\n" +
" \"context\" : \"contextB\",\n" +
" \"compilations\" : 100,\n" +
" \"compilations_history\" : {\n" +
" \"5m\" : 1000,\n" +
" \"15m\" : 1001,\n" +
" \"24h\" : 1002\n" +
" },\n" +
" \"cache_evictions\" : 201,\n" +
" \"cache_evictions_history\" : {\n" +
" \"5m\" : 2000,\n" +
" \"15m\" : 2001,\n" +
" \"24h\" : 2002\n" +
" },\n" +
" \"compilation_limit_triggered\" : 302\n" +
" }\n" +
" ]\n" +
Expand Down

0 comments on commit 35c9264

Please sign in to comment.