|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.core.ml.inference.trainedmodel; |
| 7 | + |
| 8 | +import org.elasticsearch.common.Nullable; |
| 9 | +import org.elasticsearch.common.ParseField; |
| 10 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 11 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 12 | +import org.elasticsearch.common.io.stream.Writeable; |
| 13 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 14 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 15 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 16 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 17 | +import org.elasticsearch.xpack.core.common.time.TimeUtils; |
| 18 | +import org.elasticsearch.xpack.core.ml.utils.ToXContentParams; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.time.Instant; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.concurrent.atomic.LongAdder; |
| 24 | + |
| 25 | +public class InferenceStats implements ToXContentObject, Writeable { |
| 26 | + |
| 27 | + public static final String NAME = "inference_stats"; |
| 28 | + public static final ParseField MISSING_ALL_FIELDS_COUNT = new ParseField("missing_all_fields_count"); |
| 29 | + public static final ParseField INFERENCE_COUNT = new ParseField("inference_count"); |
| 30 | + public static final ParseField MODEL_ID = new ParseField("model_id"); |
| 31 | + public static final ParseField NODE_ID = new ParseField("node_id"); |
| 32 | + public static final ParseField FAILURE_COUNT = new ParseField("failure_count"); |
| 33 | + public static final ParseField TYPE = new ParseField("type"); |
| 34 | + public static final ParseField TIMESTAMP = new ParseField("time_stamp"); |
| 35 | + |
| 36 | + public static final ConstructingObjectParser<InferenceStats, Void> PARSER = new ConstructingObjectParser<>( |
| 37 | + NAME, |
| 38 | + true, |
| 39 | + a -> new InferenceStats((Long)a[0], (Long)a[1], (Long)a[2], (String)a[3], (String)a[4], (Instant)a[5]) |
| 40 | + ); |
| 41 | + static { |
| 42 | + PARSER.declareLong(ConstructingObjectParser.constructorArg(), MISSING_ALL_FIELDS_COUNT); |
| 43 | + PARSER.declareLong(ConstructingObjectParser.constructorArg(), INFERENCE_COUNT); |
| 44 | + PARSER.declareLong(ConstructingObjectParser.constructorArg(), FAILURE_COUNT); |
| 45 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), MODEL_ID); |
| 46 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), NODE_ID); |
| 47 | + PARSER.declareField(ConstructingObjectParser.constructorArg(), |
| 48 | + p -> TimeUtils.parseTimeFieldToInstant(p, TIMESTAMP.getPreferredName()), |
| 49 | + TIMESTAMP, |
| 50 | + ObjectParser.ValueType.VALUE); |
| 51 | + } |
| 52 | + public static InferenceStats emptyStats(String modelId, String nodeId) { |
| 53 | + return new InferenceStats(0L, 0L, 0L, modelId, nodeId, Instant.now()); |
| 54 | + } |
| 55 | + |
| 56 | + public static String docId(String modelId, String nodeId) { |
| 57 | + return NAME + "-" + modelId + "-" + nodeId; |
| 58 | + } |
| 59 | + |
| 60 | + private final long missingAllFieldsCount; |
| 61 | + private final long inferenceCount; |
| 62 | + private final long failureCount; |
| 63 | + private final String modelId; |
| 64 | + private final String nodeId; |
| 65 | + private final Instant timeStamp; |
| 66 | + |
| 67 | + private InferenceStats(Long missingAllFieldsCount, |
| 68 | + Long inferenceCount, |
| 69 | + Long failureCount, |
| 70 | + String modelId, |
| 71 | + String nodeId, |
| 72 | + Instant instant) { |
| 73 | + this(unbox(missingAllFieldsCount), |
| 74 | + unbox(inferenceCount), |
| 75 | + unbox(failureCount), |
| 76 | + modelId, |
| 77 | + nodeId, |
| 78 | + instant); |
| 79 | + } |
| 80 | + |
| 81 | + public InferenceStats(long missingAllFieldsCount, |
| 82 | + long inferenceCount, |
| 83 | + long failureCount, |
| 84 | + String modelId, |
| 85 | + String nodeId, |
| 86 | + Instant timeStamp) { |
| 87 | + this.missingAllFieldsCount = missingAllFieldsCount; |
| 88 | + this.inferenceCount = inferenceCount; |
| 89 | + this.failureCount = failureCount; |
| 90 | + this.modelId = modelId; |
| 91 | + this.nodeId = nodeId; |
| 92 | + this.timeStamp = timeStamp == null ? |
| 93 | + Instant.ofEpochMilli(Instant.now().toEpochMilli()) : |
| 94 | + Instant.ofEpochMilli(timeStamp.toEpochMilli()); |
| 95 | + } |
| 96 | + |
| 97 | + public InferenceStats(StreamInput in) throws IOException { |
| 98 | + this.missingAllFieldsCount = in.readVLong(); |
| 99 | + this.inferenceCount = in.readVLong(); |
| 100 | + this.failureCount = in.readVLong(); |
| 101 | + this.modelId = in.readOptionalString(); |
| 102 | + this.nodeId = in.readOptionalString(); |
| 103 | + this.timeStamp = in.readInstant(); |
| 104 | + } |
| 105 | + |
| 106 | + public long getMissingAllFieldsCount() { |
| 107 | + return missingAllFieldsCount; |
| 108 | + } |
| 109 | + |
| 110 | + public long getInferenceCount() { |
| 111 | + return inferenceCount; |
| 112 | + } |
| 113 | + |
| 114 | + public long getFailureCount() { |
| 115 | + return failureCount; |
| 116 | + } |
| 117 | + |
| 118 | + public String getModelId() { |
| 119 | + return modelId; |
| 120 | + } |
| 121 | + |
| 122 | + public String getNodeId() { |
| 123 | + return nodeId; |
| 124 | + } |
| 125 | + |
| 126 | + public Instant getTimeStamp() { |
| 127 | + return timeStamp; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 132 | + builder.startObject(); |
| 133 | + if (params.paramAsBoolean(ToXContentParams.FOR_INTERNAL_STORAGE, false)) { |
| 134 | + assert modelId != null : "model_id cannot be null when storing inference stats"; |
| 135 | + assert nodeId != null : "node_id cannot be null when storing inference stats"; |
| 136 | + builder.field(TYPE.getPreferredName(), NAME); |
| 137 | + builder.field(MODEL_ID.getPreferredName(), modelId); |
| 138 | + builder.field(NODE_ID.getPreferredName(), nodeId); |
| 139 | + } |
| 140 | + builder.field(FAILURE_COUNT.getPreferredName(), failureCount); |
| 141 | + builder.field(INFERENCE_COUNT.getPreferredName(), inferenceCount); |
| 142 | + builder.field(MISSING_ALL_FIELDS_COUNT.getPreferredName(), missingAllFieldsCount); |
| 143 | + builder.timeField(TIMESTAMP.getPreferredName(), TIMESTAMP.getPreferredName() + "_string", timeStamp.toEpochMilli()); |
| 144 | + builder.endObject(); |
| 145 | + return builder; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public boolean equals(Object o) { |
| 150 | + if (this == o) return true; |
| 151 | + if (o == null || getClass() != o.getClass()) return false; |
| 152 | + InferenceStats that = (InferenceStats) o; |
| 153 | + return missingAllFieldsCount == that.missingAllFieldsCount |
| 154 | + && inferenceCount == that.inferenceCount |
| 155 | + && failureCount == that.failureCount |
| 156 | + && Objects.equals(modelId, that.modelId) |
| 157 | + && Objects.equals(nodeId, that.nodeId) |
| 158 | + && Objects.equals(timeStamp, that.timeStamp); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public int hashCode() { |
| 163 | + return Objects.hash(missingAllFieldsCount, inferenceCount, failureCount, modelId, nodeId, timeStamp); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public String toString() { |
| 168 | + return "InferenceStats{" + |
| 169 | + "missingAllFieldsCount=" + missingAllFieldsCount + |
| 170 | + ", inferenceCount=" + inferenceCount + |
| 171 | + ", failureCount=" + failureCount + |
| 172 | + ", modelId='" + modelId + '\'' + |
| 173 | + ", nodeId='" + nodeId + '\'' + |
| 174 | + ", timeStamp=" + timeStamp + |
| 175 | + '}'; |
| 176 | + } |
| 177 | + |
| 178 | + private static long unbox(@Nullable Long value) { |
| 179 | + return value == null ? 0L : value; |
| 180 | + } |
| 181 | + |
| 182 | + public static Accumulator accumulator(InferenceStats stats) { |
| 183 | + return new Accumulator(stats); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void writeTo(StreamOutput out) throws IOException { |
| 188 | + out.writeVLong(this.missingAllFieldsCount); |
| 189 | + out.writeVLong(this.inferenceCount); |
| 190 | + out.writeVLong(this.failureCount); |
| 191 | + out.writeOptionalString(this.modelId); |
| 192 | + out.writeOptionalString(this.nodeId); |
| 193 | + out.writeInstant(timeStamp); |
| 194 | + } |
| 195 | + |
| 196 | + public static class Accumulator { |
| 197 | + |
| 198 | + private final LongAdder missingFieldsAccumulator = new LongAdder(); |
| 199 | + private final LongAdder inferenceAccumulator = new LongAdder(); |
| 200 | + private final LongAdder failureCountAccumulator = new LongAdder(); |
| 201 | + private final String modelId; |
| 202 | + private final String nodeId; |
| 203 | + |
| 204 | + public Accumulator(String modelId, String nodeId) { |
| 205 | + this.modelId = modelId; |
| 206 | + this.nodeId = nodeId; |
| 207 | + } |
| 208 | + |
| 209 | + public Accumulator(InferenceStats previousStats) { |
| 210 | + this.modelId = previousStats.modelId; |
| 211 | + this.nodeId = previousStats.nodeId; |
| 212 | + this.missingFieldsAccumulator.add(previousStats.missingAllFieldsCount); |
| 213 | + this.inferenceAccumulator.add(previousStats.inferenceCount); |
| 214 | + this.failureCountAccumulator.add(previousStats.failureCount); |
| 215 | + } |
| 216 | + |
| 217 | + public Accumulator merge(InferenceStats otherStats) { |
| 218 | + this.missingFieldsAccumulator.add(otherStats.missingAllFieldsCount); |
| 219 | + this.inferenceAccumulator.add(otherStats.inferenceCount); |
| 220 | + this.failureCountAccumulator.add(otherStats.failureCount); |
| 221 | + return this; |
| 222 | + } |
| 223 | + |
| 224 | + public void incMissingFields() { |
| 225 | + this.missingFieldsAccumulator.increment(); |
| 226 | + } |
| 227 | + |
| 228 | + public void incInference() { |
| 229 | + this.inferenceAccumulator.increment(); |
| 230 | + } |
| 231 | + |
| 232 | + public void incFailure() { |
| 233 | + this.failureCountAccumulator.increment(); |
| 234 | + } |
| 235 | + |
| 236 | + public InferenceStats currentStats() { |
| 237 | + return currentStats(Instant.now()); |
| 238 | + } |
| 239 | + |
| 240 | + public InferenceStats currentStats(Instant timeStamp) { |
| 241 | + return new InferenceStats(missingFieldsAccumulator.longValue(), |
| 242 | + inferenceAccumulator.longValue(), |
| 243 | + failureCountAccumulator.longValue(), |
| 244 | + modelId, |
| 245 | + nodeId, |
| 246 | + timeStamp); |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments