|
@@ -5,6 +5,8 @@
|
|
|
*/
|
|
|
package org.elasticsearch.xpack.core.ml.dataframe.stats.common;
|
|
|
|
|
|
+import org.elasticsearch.Version;
|
|
|
+import org.elasticsearch.common.Nullable;
|
|
|
import org.elasticsearch.common.ParseField;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
@@ -14,6 +16,7 @@ import org.elasticsearch.common.xcontent.ConstructingObjectParser;
|
|
|
import org.elasticsearch.common.xcontent.ObjectParser;
|
|
|
import org.elasticsearch.common.xcontent.ToXContentObject;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
+import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
import org.elasticsearch.xpack.core.common.time.TimeUtils;
|
|
|
import org.elasticsearch.xpack.core.ml.dataframe.stats.Fields;
|
|
|
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
|
|
@@ -21,6 +24,7 @@ import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.time.Instant;
|
|
|
+import java.util.Locale;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
public class MemoryUsage implements Writeable, ToXContentObject {
|
|
@@ -28,13 +32,15 @@ public class MemoryUsage implements Writeable, ToXContentObject {
|
|
|
public static final String TYPE_VALUE = "analytics_memory_usage";
|
|
|
|
|
|
public static final ParseField PEAK_USAGE_BYTES = new ParseField("peak_usage_bytes");
|
|
|
+ public static final ParseField STATUS = new ParseField("status");
|
|
|
+ public static final ParseField INCREASED_MEMORY_ESTIMATE_BYTES = new ParseField("increased_memory_estimate_bytes");
|
|
|
|
|
|
public static final ConstructingObjectParser<MemoryUsage, Void> STRICT_PARSER = createParser(false);
|
|
|
public static final ConstructingObjectParser<MemoryUsage, Void> LENIENT_PARSER = createParser(true);
|
|
|
|
|
|
private static ConstructingObjectParser<MemoryUsage, Void> createParser(boolean ignoreUnknownFields) {
|
|
|
ConstructingObjectParser<MemoryUsage, Void> parser = new ConstructingObjectParser<>(TYPE_VALUE,
|
|
|
- ignoreUnknownFields, a -> new MemoryUsage((String) a[0], (Instant) a[1], (long) a[2]));
|
|
|
+ ignoreUnknownFields, a -> new MemoryUsage((String) a[0], (Instant) a[1], (long) a[2], (Status) a[3], (Long) a[4]));
|
|
|
|
|
|
parser.declareString((bucket, s) -> {}, Fields.TYPE);
|
|
|
parser.declareString(ConstructingObjectParser.constructorArg(), Fields.JOB_ID);
|
|
@@ -43,6 +49,13 @@ public class MemoryUsage implements Writeable, ToXContentObject {
|
|
|
Fields.TIMESTAMP,
|
|
|
ObjectParser.ValueType.VALUE);
|
|
|
parser.declareLong(ConstructingObjectParser.constructorArg(), PEAK_USAGE_BYTES);
|
|
|
+ parser.declareField(ConstructingObjectParser.optionalConstructorArg(), p -> {
|
|
|
+ if (p.currentToken() == XContentParser.Token.VALUE_STRING) {
|
|
|
+ return Status.fromString(p.text());
|
|
|
+ }
|
|
|
+ throw new IllegalArgumentException("Unsupported token [" + p.currentToken() + "]");
|
|
|
+ }, STATUS, ObjectParser.ValueType.STRING);
|
|
|
+ parser.declareLong(ConstructingObjectParser.optionalConstructorArg(), INCREASED_MEMORY_ESTIMATE_BYTES);
|
|
|
return parser;
|
|
|
}
|
|
|
|
|
@@ -52,27 +65,43 @@ public class MemoryUsage implements Writeable, ToXContentObject {
|
|
|
*/
|
|
|
private final Instant timestamp;
|
|
|
private final long peakUsageBytes;
|
|
|
+ private final Status status;
|
|
|
+ @Nullable private final Long increasedMemoryEstimateBytes;
|
|
|
|
|
|
/**
|
|
|
* Creates a zero usage object
|
|
|
*/
|
|
|
public MemoryUsage(String jobId) {
|
|
|
- this(jobId, null, 0);
|
|
|
+ this(jobId, null, 0, null, null);
|
|
|
}
|
|
|
|
|
|
- public MemoryUsage(String jobId, Instant timestamp, long peakUsageBytes) {
|
|
|
+ public MemoryUsage(String jobId, Instant timestamp, long peakUsageBytes, @Nullable Status status,
|
|
|
+ @Nullable Long increasedMemoryEstimateBytes) {
|
|
|
this.jobId = Objects.requireNonNull(jobId);
|
|
|
// We intend to store this timestamp in millis granularity. Thus we're rounding here to ensure
|
|
|
// internal representation matches toXContent
|
|
|
this.timestamp = timestamp == null ? null : Instant.ofEpochMilli(
|
|
|
ExceptionsHelper.requireNonNull(timestamp, Fields.TIMESTAMP).toEpochMilli());
|
|
|
this.peakUsageBytes = peakUsageBytes;
|
|
|
+ this.status = status == null ? Status.OK : status;
|
|
|
+ this.increasedMemoryEstimateBytes = increasedMemoryEstimateBytes;
|
|
|
}
|
|
|
|
|
|
public MemoryUsage(StreamInput in) throws IOException {
|
|
|
jobId = in.readString();
|
|
|
timestamp = in.readOptionalInstant();
|
|
|
peakUsageBytes = in.readVLong();
|
|
|
+ if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
|
|
|
+ status = Status.readFromStream(in);
|
|
|
+ increasedMemoryEstimateBytes = in.readOptionalVLong();
|
|
|
+ } else {
|
|
|
+ status = Status.OK;
|
|
|
+ increasedMemoryEstimateBytes = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Status getStatus() {
|
|
|
+ return status;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -80,6 +109,10 @@ public class MemoryUsage implements Writeable, ToXContentObject {
|
|
|
out.writeString(jobId);
|
|
|
out.writeOptionalInstant(timestamp);
|
|
|
out.writeVLong(peakUsageBytes);
|
|
|
+ if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
|
|
|
+ status.writeTo(out);
|
|
|
+ out.writeOptionalVLong(increasedMemoryEstimateBytes);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -94,6 +127,10 @@ public class MemoryUsage implements Writeable, ToXContentObject {
|
|
|
timestamp.toEpochMilli());
|
|
|
}
|
|
|
builder.field(PEAK_USAGE_BYTES.getPreferredName(), peakUsageBytes);
|
|
|
+ builder.field(STATUS.getPreferredName(), status);
|
|
|
+ if (increasedMemoryEstimateBytes != null) {
|
|
|
+ builder.field(INCREASED_MEMORY_ESTIMATE_BYTES.getPreferredName(), increasedMemoryEstimateBytes);
|
|
|
+ }
|
|
|
builder.endObject();
|
|
|
return builder;
|
|
|
}
|
|
@@ -106,12 +143,14 @@ public class MemoryUsage implements Writeable, ToXContentObject {
|
|
|
MemoryUsage other = (MemoryUsage) o;
|
|
|
return Objects.equals(jobId, other.jobId)
|
|
|
&& Objects.equals(timestamp, other.timestamp)
|
|
|
- && peakUsageBytes == other.peakUsageBytes;
|
|
|
+ && peakUsageBytes == other.peakUsageBytes
|
|
|
+ && Objects.equals(status, other.status)
|
|
|
+ && Objects.equals(increasedMemoryEstimateBytes, other.increasedMemoryEstimateBytes);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int hashCode() {
|
|
|
- return Objects.hash(jobId, timestamp, peakUsageBytes);
|
|
|
+ return Objects.hash(jobId, timestamp, peakUsageBytes, status, increasedMemoryEstimateBytes);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -127,4 +166,27 @@ public class MemoryUsage implements Writeable, ToXContentObject {
|
|
|
public static String documentIdPrefix(String jobId) {
|
|
|
return TYPE_VALUE + "_" + jobId + "_";
|
|
|
}
|
|
|
+
|
|
|
+ public enum Status implements Writeable {
|
|
|
+ OK,
|
|
|
+ HARD_LIMIT;
|
|
|
+
|
|
|
+ public static Status fromString(String value) {
|
|
|
+ return valueOf(value.toUpperCase(Locale.ROOT));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Status readFromStream(StreamInput in) throws IOException {
|
|
|
+ return in.readEnum(Status.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void writeTo(StreamOutput out) throws IOException {
|
|
|
+ out.writeEnum(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return name().toLowerCase(Locale.ROOT);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|