-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ML] Start gathering and storing inference stats #53429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
benwtrent
merged 23 commits into
elastic:master
from
benwtrent:feature/ml-inference-stats-collection
Apr 3, 2020
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ee48686
[ML] Gathering inference stats in localModel and loading service
benwtrent 0112705
Merge branch 'master' into feature/ml-inference-stats-collection
benwtrent 6b45143
making stats loading failure fail the listeners
benwtrent 3afa69e
Merge branch 'master' into feature/ml-inference-stats-collection
benwtrent c406e8e
allowing missing stats index
benwtrent b7bda48
addressing pr comments
benwtrent 6bf2263
Merge branch 'master' into feature/ml-inference-stats-collection
benwtrent bf4051a
Merge branch 'master' into feature/ml-inference-stats-collection
benwtrent 0d6ba92
addressing pr comments
benwtrent d2aecfa
Merge branch 'master' into feature/ml-inference-stats-collection
elasticmachine 9faca62
fixing style checks
benwtrent 5b0cf39
Merge branch 'feature/ml-inference-stats-collection' of github.com:be…
benwtrent f5f2821
Merge branch 'master' into feature/ml-inference-stats-collection
elasticmachine fe44488
Update InferenceIngestIT.java
benwtrent 650947b
Merge branch 'master' into feature/ml-inference-stats-collection
elasticmachine c2c1522
Merge branch 'master' into feature/ml-inference-stats-collection
benwtrent 5f86f95
addressing PR comments
benwtrent 0360c3b
incrementally updating stats instead of overwriting
benwtrent 8802fc4
Merge remote-tracking branch 'upstream/master' into feature/ml-infere…
benwtrent 0cca932
fixing bwc serialization versions
benwtrent ff15e97
minor fixes
benwtrent 74b95d5
handling situation where aggs are null
benwtrent 1d7abd5
fixing stats queueing
benwtrent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
249 changes: 249 additions & 0 deletions
249
.../src/main/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/InferenceStats.java
This file contains hidden or 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,249 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
| package org.elasticsearch.xpack.core.ml.inference.trainedmodel; | ||
|
|
||
| import org.elasticsearch.common.Nullable; | ||
| import org.elasticsearch.common.ParseField; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.io.stream.Writeable; | ||
| 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.xpack.core.common.time.TimeUtils; | ||
| import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.Instant; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.atomic.LongAdder; | ||
|
|
||
| public class InferenceStats implements ToXContentObject, Writeable { | ||
|
|
||
| public static final String NAME = "inference_stats"; | ||
| public static final ParseField MISSING_ALL_FIELDS_COUNT = new ParseField("missing_all_fields_count"); | ||
| public static final ParseField INFERENCE_COUNT = new ParseField("inference_count"); | ||
| public static final ParseField MODEL_ID = new ParseField("model_id"); | ||
| public static final ParseField NODE_ID = new ParseField("node_id"); | ||
| public static final ParseField FAILURE_COUNT = new ParseField("failure_count"); | ||
| public static final ParseField TYPE = new ParseField("type"); | ||
| public static final ParseField TIMESTAMP = new ParseField("time_stamp"); | ||
|
|
||
| public static final ConstructingObjectParser<InferenceStats, Void> PARSER = new ConstructingObjectParser<>( | ||
| NAME, | ||
| true, | ||
| a -> new InferenceStats((Long)a[0], (Long)a[1], (Long)a[2], (String)a[3], (String)a[4], (Instant)a[5]) | ||
| ); | ||
| static { | ||
| PARSER.declareLong(ConstructingObjectParser.constructorArg(), MISSING_ALL_FIELDS_COUNT); | ||
| PARSER.declareLong(ConstructingObjectParser.constructorArg(), INFERENCE_COUNT); | ||
| PARSER.declareLong(ConstructingObjectParser.constructorArg(), FAILURE_COUNT); | ||
| PARSER.declareString(ConstructingObjectParser.constructorArg(), MODEL_ID); | ||
| PARSER.declareString(ConstructingObjectParser.constructorArg(), NODE_ID); | ||
| PARSER.declareField(ConstructingObjectParser.constructorArg(), | ||
| p -> TimeUtils.parseTimeFieldToInstant(p, TIMESTAMP.getPreferredName()), | ||
| TIMESTAMP, | ||
| ObjectParser.ValueType.VALUE); | ||
| } | ||
| public static InferenceStats emptyStats(String modelId, String nodeId) { | ||
| return new InferenceStats(0L, 0L, 0L, modelId, nodeId, Instant.now()); | ||
| } | ||
|
|
||
| public static String docId(String modelId, String nodeId) { | ||
| return NAME + "-" + modelId + "-" + nodeId; | ||
| } | ||
|
|
||
| private final long missingAllFieldsCount; | ||
| private final long inferenceCount; | ||
| private final long failureCount; | ||
| private final String modelId; | ||
| private final String nodeId; | ||
| private final Instant timeStamp; | ||
|
|
||
| private InferenceStats(Long missingAllFieldsCount, | ||
| Long inferenceCount, | ||
| Long failureCount, | ||
| String modelId, | ||
| String nodeId, | ||
| Instant instant) { | ||
| this(unbox(missingAllFieldsCount), | ||
| unbox(inferenceCount), | ||
| unbox(failureCount), | ||
| modelId, | ||
| nodeId, | ||
| instant); | ||
| } | ||
|
|
||
| public InferenceStats(long missingAllFieldsCount, | ||
| long inferenceCount, | ||
| long failureCount, | ||
| String modelId, | ||
| String nodeId, | ||
| Instant timeStamp) { | ||
| this.missingAllFieldsCount = missingAllFieldsCount; | ||
| this.inferenceCount = inferenceCount; | ||
| this.failureCount = failureCount; | ||
| this.modelId = modelId; | ||
| this.nodeId = nodeId; | ||
| this.timeStamp = timeStamp == null ? | ||
| Instant.ofEpochMilli(Instant.now().toEpochMilli()) : | ||
| Instant.ofEpochMilli(timeStamp.toEpochMilli()); | ||
| } | ||
|
|
||
| public InferenceStats(StreamInput in) throws IOException { | ||
| this.missingAllFieldsCount = in.readVLong(); | ||
| this.inferenceCount = in.readVLong(); | ||
| this.failureCount = in.readVLong(); | ||
| this.modelId = in.readOptionalString(); | ||
| this.nodeId = in.readOptionalString(); | ||
| this.timeStamp = in.readInstant(); | ||
| } | ||
|
|
||
| public long getMissingAllFieldsCount() { | ||
| return missingAllFieldsCount; | ||
| } | ||
|
|
||
| public long getInferenceCount() { | ||
| return inferenceCount; | ||
| } | ||
|
|
||
| public long getFailureCount() { | ||
| return failureCount; | ||
| } | ||
|
|
||
| public String getModelId() { | ||
| return modelId; | ||
| } | ||
|
|
||
| public String getNodeId() { | ||
| return nodeId; | ||
| } | ||
|
|
||
| public Instant getTimeStamp() { | ||
| return timeStamp; | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| builder.startObject(); | ||
| if (params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false)) { | ||
| assert modelId != null : "model_id cannot be null when storing inference stats"; | ||
| assert nodeId != null : "node_id cannot be null when storing inference stats"; | ||
| builder.field(TYPE.getPreferredName(), NAME); | ||
| builder.field(MODEL_ID.getPreferredName(), modelId); | ||
| builder.field(NODE_ID.getPreferredName(), nodeId); | ||
benwtrent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| builder.field(FAILURE_COUNT.getPreferredName(), failureCount); | ||
| builder.field(INFERENCE_COUNT.getPreferredName(), inferenceCount); | ||
| builder.field(MISSING_ALL_FIELDS_COUNT.getPreferredName(), missingAllFieldsCount); | ||
| builder.timeField(TIMESTAMP.getPreferredName(), TIMESTAMP.getPreferredName() + "_string", timeStamp.toEpochMilli()); | ||
| builder.endObject(); | ||
| return builder; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| InferenceStats that = (InferenceStats) o; | ||
| return missingAllFieldsCount == that.missingAllFieldsCount | ||
| && inferenceCount == that.inferenceCount | ||
| && failureCount == that.failureCount | ||
| && Objects.equals(modelId, that.modelId) | ||
| && Objects.equals(nodeId, that.nodeId) | ||
| && Objects.equals(timeStamp, that.timeStamp); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(missingAllFieldsCount, inferenceCount, failureCount, modelId, nodeId, timeStamp); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "InferenceStats{" + | ||
| "missingAllFieldsCount=" + missingAllFieldsCount + | ||
| ", inferenceCount=" + inferenceCount + | ||
| ", failureCount=" + failureCount + | ||
| ", modelId='" + modelId + '\'' + | ||
| ", nodeId='" + nodeId + '\'' + | ||
| ", timeStamp=" + timeStamp + | ||
| '}'; | ||
| } | ||
|
|
||
| private static long unbox(@Nullable Long value) { | ||
| return value == null ? 0L : value; | ||
| } | ||
|
|
||
| public static Accumulator accumulator(InferenceStats stats) { | ||
| return new Accumulator(stats); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| out.writeVLong(this.missingAllFieldsCount); | ||
| out.writeVLong(this.inferenceCount); | ||
| out.writeVLong(this.failureCount); | ||
| out.writeOptionalString(this.modelId); | ||
| out.writeOptionalString(this.nodeId); | ||
| out.writeInstant(timeStamp); | ||
| } | ||
|
|
||
| public static class Accumulator { | ||
|
|
||
| private final LongAdder missingFieldsAccumulator = new LongAdder(); | ||
| private final LongAdder inferenceAccumulator = new LongAdder(); | ||
| private final LongAdder failureCountAccumulator = new LongAdder(); | ||
| private final String modelId; | ||
| private final String nodeId; | ||
|
|
||
| public Accumulator(String modelId, String nodeId) { | ||
| this.modelId = modelId; | ||
| this.nodeId = nodeId; | ||
| } | ||
|
|
||
| public Accumulator(InferenceStats previousStats) { | ||
| this.modelId = previousStats.modelId; | ||
| this.nodeId = previousStats.nodeId; | ||
| this.missingFieldsAccumulator.add(previousStats.missingAllFieldsCount); | ||
| this.inferenceAccumulator.add(previousStats.inferenceCount); | ||
| this.failureCountAccumulator.add(previousStats.failureCount); | ||
| } | ||
|
|
||
| public Accumulator merge(InferenceStats otherStats) { | ||
| this.missingFieldsAccumulator.add(otherStats.missingAllFieldsCount); | ||
| this.inferenceAccumulator.add(otherStats.inferenceCount); | ||
| this.failureCountAccumulator.add(otherStats.failureCount); | ||
| return this; | ||
| } | ||
|
|
||
| public void incMissingFields() { | ||
| this.missingFieldsAccumulator.increment(); | ||
| } | ||
|
|
||
| public void incInference() { | ||
| this.inferenceAccumulator.increment(); | ||
| } | ||
|
|
||
| public void incFailure() { | ||
| this.failureCountAccumulator.increment(); | ||
| } | ||
|
|
||
| public InferenceStats currentStats() { | ||
| return currentStats(Instant.now()); | ||
| } | ||
|
|
||
| public InferenceStats currentStats(Instant timeStamp) { | ||
| return new InferenceStats(missingFieldsAccumulator.longValue(), | ||
| inferenceAccumulator.longValue(), | ||
| failureCountAccumulator.longValue(), | ||
| modelId, | ||
| nodeId, | ||
| timeStamp); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.