|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.protocol.xpack.ml.job.config; |
| 20 | + |
| 21 | +import org.elasticsearch.common.Nullable; |
| 22 | +import org.elasticsearch.common.ParseField; |
| 23 | +import org.elasticsearch.common.unit.ByteSizeValue; |
| 24 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +/** |
| 34 | + * Analysis limits for autodetect. In particular, |
| 35 | + * this is a collection of parameters that allow limiting |
| 36 | + * the resources used by the job. |
| 37 | + */ |
| 38 | +public class AnalysisLimits implements ToXContentObject { |
| 39 | + |
| 40 | + /** |
| 41 | + * Serialisation field names |
| 42 | + */ |
| 43 | + public static final ParseField MODEL_MEMORY_LIMIT = new ParseField("model_memory_limit"); |
| 44 | + public static final ParseField CATEGORIZATION_EXAMPLES_LIMIT = new ParseField("categorization_examples_limit"); |
| 45 | + |
| 46 | + public static final ConstructingObjectParser<AnalysisLimits, Void> PARSER = |
| 47 | + new ConstructingObjectParser<>("analysis_limits", true, a -> new AnalysisLimits((Long) a[0], (Long) a[1])); |
| 48 | + |
| 49 | + static { |
| 50 | + PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(), p -> { |
| 51 | + if (p.currentToken() == XContentParser.Token.VALUE_STRING) { |
| 52 | + return ByteSizeValue.parseBytesSizeValue(p.text(), MODEL_MEMORY_LIMIT.getPreferredName()).getMb(); |
| 53 | + } else if (p.currentToken() == XContentParser.Token.VALUE_NUMBER) { |
| 54 | + return p.longValue(); |
| 55 | + } |
| 56 | + throw new IllegalArgumentException("Unsupported token [" + p.currentToken() + "]"); |
| 57 | + }, MODEL_MEMORY_LIMIT, ObjectParser.ValueType.VALUE); |
| 58 | + PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), CATEGORIZATION_EXAMPLES_LIMIT); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * The model memory limit in MiBs. |
| 63 | + * It is initialised to <code>null</code>, which implies that the server-side default will be used. |
| 64 | + */ |
| 65 | + private final Long modelMemoryLimit; |
| 66 | + |
| 67 | + /** |
| 68 | + * It is initialised to <code>null</code>. |
| 69 | + * A value of <code>null</code> will result in the server-side default being used. |
| 70 | + */ |
| 71 | + private final Long categorizationExamplesLimit; |
| 72 | + |
| 73 | + public AnalysisLimits(Long categorizationExamplesLimit) { |
| 74 | + this(null, categorizationExamplesLimit); |
| 75 | + } |
| 76 | + |
| 77 | + public AnalysisLimits(Long modelMemoryLimit, Long categorizationExamplesLimit) { |
| 78 | + this.modelMemoryLimit = modelMemoryLimit; |
| 79 | + this.categorizationExamplesLimit = categorizationExamplesLimit; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Maximum size of the model in MB before the anomaly detector |
| 84 | + * will drop new samples to prevent the model using any more |
| 85 | + * memory. |
| 86 | + * |
| 87 | + * @return The set memory limit or <code>null</code> if not set |
| 88 | + */ |
| 89 | + @Nullable |
| 90 | + public Long getModelMemoryLimit() { |
| 91 | + return modelMemoryLimit; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Gets the limit to the number of examples that are stored per category |
| 96 | + * |
| 97 | + * @return the limit or <code>null</code> if not set |
| 98 | + */ |
| 99 | + @Nullable |
| 100 | + public Long getCategorizationExamplesLimit() { |
| 101 | + return categorizationExamplesLimit; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 106 | + builder.startObject(); |
| 107 | + if (modelMemoryLimit != null) { |
| 108 | + builder.field(MODEL_MEMORY_LIMIT.getPreferredName(), modelMemoryLimit + "mb"); |
| 109 | + } |
| 110 | + if (categorizationExamplesLimit != null) { |
| 111 | + builder.field(CATEGORIZATION_EXAMPLES_LIMIT.getPreferredName(), categorizationExamplesLimit); |
| 112 | + } |
| 113 | + builder.endObject(); |
| 114 | + return builder; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Overridden equality test |
| 119 | + */ |
| 120 | + @Override |
| 121 | + public boolean equals(Object other) { |
| 122 | + if (this == other) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + if (other instanceof AnalysisLimits == false) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + AnalysisLimits that = (AnalysisLimits) other; |
| 131 | + return Objects.equals(this.modelMemoryLimit, that.modelMemoryLimit) && |
| 132 | + Objects.equals(this.categorizationExamplesLimit, that.categorizationExamplesLimit); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public int hashCode() { |
| 137 | + return Objects.hash(modelMemoryLimit, categorizationExamplesLimit); |
| 138 | + } |
| 139 | +} |
0 commit comments