|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V., and/or licensed to Elasticsearch B.V. |
| 3 | + * under one or more license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. 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 | + * This file is based on a modification of https://github.com/open-telemetry/opentelemetry-java which is licensed under the Apache 2.0 License. |
| 20 | + */ |
| 21 | + |
| 22 | +package org.elasticsearch.exponentialhistogram; |
| 23 | + |
| 24 | +import org.elasticsearch.core.Releasables; |
| 25 | + |
| 26 | +import java.util.TreeMap; |
| 27 | + |
| 28 | +/** |
| 29 | + * A builder for building a {@link ReleasableExponentialHistogram} directly from buckets. |
| 30 | + * Note that this class is not optimized regarding memory allocations, so it is not intended for high-throughput usage. |
| 31 | + */ |
| 32 | +public class ExponentialHistogramBuilder { |
| 33 | + |
| 34 | + private final ExponentialHistogramCircuitBreaker breaker; |
| 35 | + |
| 36 | + private final int scale; |
| 37 | + private ZeroBucket zeroBucket = ZeroBucket.minimalEmpty(); |
| 38 | + private Double sum; |
| 39 | + private Double min; |
| 40 | + private Double max; |
| 41 | + |
| 42 | + private final TreeMap<Long, Long> negativeBuckets = new TreeMap<>(); |
| 43 | + private final TreeMap<Long, Long> positiveBuckets = new TreeMap<>(); |
| 44 | + |
| 45 | + ExponentialHistogramBuilder(int scale, ExponentialHistogramCircuitBreaker breaker) { |
| 46 | + this.breaker = breaker; |
| 47 | + this.scale = scale; |
| 48 | + } |
| 49 | + |
| 50 | + public ExponentialHistogramBuilder zeroBucket(ZeroBucket zeroBucket) { |
| 51 | + this.zeroBucket = zeroBucket; |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Sets the sum of the histogram values. If not set, the sum will be estimated from the buckets. |
| 57 | + * @param sum the sum value |
| 58 | + * @return the builder |
| 59 | + */ |
| 60 | + public ExponentialHistogramBuilder sum(double sum) { |
| 61 | + this.sum = sum; |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Sets the min value of the histogram values. If not set, the min will be estimated from the buckets. |
| 67 | + * @param min the min value |
| 68 | + * @return the builder |
| 69 | + */ |
| 70 | + public ExponentialHistogramBuilder min(double min) { |
| 71 | + this.min = min; |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Sets the max value of the histogram values. If not set, the max will be estimated from the buckets. |
| 77 | + * @param max the max value |
| 78 | + * @return the builder |
| 79 | + */ |
| 80 | + public ExponentialHistogramBuilder max(double max) { |
| 81 | + this.max = max; |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Adds the given bucket to the positive buckets. |
| 87 | + * Buckets may be added in arbitrary order, but each bucket can only be added once. |
| 88 | + * |
| 89 | + * @param index the index of the bucket |
| 90 | + * @param count the count of the bucket, must be at least 1 |
| 91 | + * @return the builder |
| 92 | + */ |
| 93 | + public ExponentialHistogramBuilder addPositiveBucket(long index, long count) { |
| 94 | + if (count < 1) { |
| 95 | + throw new IllegalArgumentException("Bucket count must be at least 1"); |
| 96 | + } |
| 97 | + if (positiveBuckets.containsKey(index)) { |
| 98 | + throw new IllegalArgumentException("Positive bucket already exists: " + index); |
| 99 | + } |
| 100 | + positiveBuckets.put(index, count); |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Adds the given bucket to the negative buckets. |
| 106 | + * Buckets may be added in arbitrary order, but each bucket can only be added once. |
| 107 | + * |
| 108 | + * @param index the index of the bucket |
| 109 | + * @param count the count of the bucket, must be at least 1 |
| 110 | + * @return the builder |
| 111 | + */ |
| 112 | + public ExponentialHistogramBuilder addNegativeBucket(long index, long count) { |
| 113 | + if (count < 1) { |
| 114 | + throw new IllegalArgumentException("Bucket count must be at least 1"); |
| 115 | + } |
| 116 | + if (negativeBuckets.containsKey(index)) { |
| 117 | + throw new IllegalArgumentException("Negative bucket already exists: " + index); |
| 118 | + } |
| 119 | + negativeBuckets.put(index, count); |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + public ReleasableExponentialHistogram build() { |
| 124 | + FixedCapacityExponentialHistogram result = FixedCapacityExponentialHistogram.create( |
| 125 | + negativeBuckets.size() + positiveBuckets.size(), |
| 126 | + breaker |
| 127 | + ); |
| 128 | + boolean success = false; |
| 129 | + try { |
| 130 | + result.resetBuckets(scale); |
| 131 | + result.setZeroBucket(zeroBucket); |
| 132 | + negativeBuckets.forEach((index, count) -> result.tryAddBucket(index, count, false)); |
| 133 | + positiveBuckets.forEach((index, count) -> result.tryAddBucket(index, count, true)); |
| 134 | + |
| 135 | + double sumVal = (sum != null) |
| 136 | + ? sum |
| 137 | + : ExponentialHistogramUtils.estimateSum(result.negativeBuckets().iterator(), result.positiveBuckets().iterator()); |
| 138 | + double minVal = (min != null) |
| 139 | + ? min |
| 140 | + : ExponentialHistogramUtils.estimateMin(zeroBucket, result.negativeBuckets(), result.positiveBuckets()).orElse(Double.NaN); |
| 141 | + double maxVal = (max != null) |
| 142 | + ? max |
| 143 | + : ExponentialHistogramUtils.estimateMax(zeroBucket, result.negativeBuckets(), result.positiveBuckets()).orElse(Double.NaN); |
| 144 | + |
| 145 | + result.setMin(minVal); |
| 146 | + result.setMax(maxVal); |
| 147 | + result.setSum(sumVal); |
| 148 | + |
| 149 | + success = true; |
| 150 | + } finally { |
| 151 | + if (success == false) { |
| 152 | + Releasables.close(result); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // Create histogram |
| 157 | + return result; |
| 158 | + } |
| 159 | +} |
0 commit comments