jeffkbkim commented on code in PR #16949: URL: https://github.com/apache/kafka/pull/16949#discussion_r1751156337
########## group-coordinator/src/main/java/org/apache/kafka/coordinator/group/metrics/HdrHistogram.java: ########## @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.coordinator.group.metrics; + +import org.HdrHistogram.Histogram; +import org.HdrHistogram.Recorder; +import org.HdrHistogram.ValueRecorder; + +import java.util.concurrent.atomic.AtomicReference; + +/** + * <p>A wrapper on top of the HdrHistogram API. It handles writing to the histogram by delegating + * to an internal {@link ValueRecorder} implementation, and reading from the histogram by + * efficiently implementing the retrieval of up-to-date histogram data. + * + * <p>Note that all APIs expect a timestamp which is used by the histogram to discard decaying data + * and determine when the snapshot from which the histogram metrics are calculated should be + * refreshed. + */ +public final class HdrHistogram { + + private static final long DEFAULT_MAX_SNAPSHOT_AGE_MS = 1000L; + + /** + * The duration (in millis) after which the latest histogram snapshot is considered outdated and + * subsequent calls to {@link #latestHistogram(long)} will result in the snapshot being recreated. + */ + private final long maxSnapshotAgeMs; + + /** + * The internal HdrHistogram data structure used to: + * <ul> + * <li>ingest data;</li> + * <li>get a {@link Histogram} of the latest ingested data;</li> + * </ul> + */ + private final Recorder recorder; + + /** + * The latest snapshot of the internal HdrHistogram. Automatically updated by + * {@link #latestHistogram(long)} if older than {@link #maxSnapshotAgeMs}. + */ + private final AtomicReference<Timestamped<Histogram>> timestampedHistogramSnapshot; + + public HdrHistogram( + long highestTrackableValue, + int numberOfSignificantValueDigits + ) { + this(DEFAULT_MAX_SNAPSHOT_AGE_MS, highestTrackableValue, numberOfSignificantValueDigits); + } + + HdrHistogram( + long maxSnapshotAgeMs, + long highestTrackableValue, + int numberOfSignificantValueDigits + ) { + this.maxSnapshotAgeMs = maxSnapshotAgeMs; + recorder = new Recorder(highestTrackableValue, numberOfSignificantValueDigits); + this.timestampedHistogramSnapshot = new AtomicReference<>(new Timestamped<>(0, null)); + } + + private Histogram latestHistogram(long now) { + Timestamped<Histogram> latest = timestampedHistogramSnapshot.get(); + while (now - latest.timestamp > maxSnapshotAgeMs) { + Histogram currentSnapshot = recorder.getIntervalHistogram(); + boolean updatedLatest = timestampedHistogramSnapshot.compareAndSet( + latest, new Timestamped<>(now, currentSnapshot)); + + latest = timestampedHistogramSnapshot.get(); + if (updatedLatest) { + break; + } + } + return latest.value; + } Review Comment: That makes sense, please let me know and I will take a look. Thanks! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org