chia7712 commented on code in PR #22809:
URL: https://github.com/apache/kafka/pull/22809#discussion_r4222075394


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -104,9 +98,7 @@ public class MeteredKeyValueStore<K, V>
     private TaskId taskId;
     private Sensor restoreSensor;
 
-    protected LongAdder numOpenIterators = new LongAdder();
-    protected NavigableSet<MeteredIterator> openIterators = new 
ConcurrentSkipListSet<>(Comparator.comparingLong(MeteredIterator::startTimestamp));
-
+    protected final MeteredIteratorTracker iteratorTracker = new 
MeteredIteratorTracker();

Review Comment:
   package-private is more suitable for this variable



##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredIteratorTracker.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.streams.state.internals;
+
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.NavigableSet;
+import java.util.NoSuchElementException;
+import java.util.concurrent.ConcurrentSkipListSet;
+import java.util.concurrent.atomic.LongAdder;
+
+final class MeteredIteratorTracker {
+
+    private final LongAdder numOpenIterators;
+    private final NavigableSet<MeteredIterator> openIterators;
+
+    MeteredIteratorTracker() {
+        this(
+            new LongAdder(),
+            new 
ConcurrentSkipListSet<>(Comparator.comparingLong(MeteredIterator::startTimestamp))
+        );
+    }
+
+    MeteredIteratorTracker(final LongAdder numOpenIterators,
+                           final NavigableSet<MeteredIterator> openIterators) {
+        this.numOpenIterators = numOpenIterators;
+        this.openIterators = openIterators;
+    }
+
+    void add(final MeteredIterator iterator) {
+        numOpenIterators.increment();
+        openIterators.add(iterator);
+    }
+
+    void remove(final MeteredIterator iterator) {
+        numOpenIterators.decrement();
+        openIterators.remove(iterator);
+    }
+
+    long numOpenIterators() {
+        return numOpenIterators.sum();
+    }
+
+    long oldestIteratorStartTimestamp() {
+        try {
+            final Iterator<MeteredIterator> iterator = 
openIterators.iterator();
+            return iterator.hasNext() ? iterator.next().startTimestamp() : 0L;
+        } catch (final NoSuchElementException e) {

Review Comment:
   I’m confused why we need this catch. If the set is thread-safe, the iterator 
prefetches the next item when hasNext is called, IIRC, so next can’t throw 
`NoSuchElementException`. If it is not thread-safe, it throws 
`ConcurrentModificationException` instead, which this catch doesn’t cover 
anyway.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to