mjsax commented on code in PR #14620:
URL: https://github.com/apache/kafka/pull/14620#discussion_r1379594061


##########
clients/src/main/java/org/apache/kafka/common/telemetry/internals/LastValueTracker.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.common.telemetry.internals;
+
+import java.time.Instant;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * A LastValueTracker uses a ConcurrentMap to maintain historic values for a 
given key, and return
+ * a previous value and an Instant for that value.
+ *
+ * @param <T> The type of the value.
+ */
+public class LastValueTracker<T> {
+    private final ConcurrentMap<MetricKey, 
AtomicReference<InstantAndValue<T>>> counters = new ConcurrentHashMap<>();
+
+    /**
+     * Return the last instant/value for the given MetricKey, or 
Optional.empty if there isn't one.
+     *
+     * @param metricKey the key for which to calculate a getAndSet.
+     * @param now the timestamp for the new value.
+     * @param value the current value.
+     * @return the timestamp of the previous entry and its value. If there
+     *     isn't a previous entry, then this method returns {@link 
Optional#empty()}
+     */
+    public Optional<InstantAndValue<T>> getAndSet(MetricKey metricKey, Instant 
now, T value) {
+        InstantAndValue<T> instantAndValue = new InstantAndValue<>(now, value);
+        AtomicReference<InstantAndValue<T>> valueOrNull = counters

Review Comment:
   Still not sure if I can follow -- I don't see any code that would return the 
value of the `counters` map (only the `remove` but given that the map is thread 
safe, it's not possible that we would return it to two threads -- a second 
thread would get a `null` back)? And the put is protected by the concurrent 
map. 



-- 
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

Reply via email to