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


##########
streams/src/test/java/org/apache/kafka/streams/state/internals/MeteredTimestampedKeyValueStoreWithHeadersTest.java:
##########


Review Comment:
   This PR branched before 
KAFKA-20704([#22770](https://github.com/apache/kafka/pull/22770)) merged. That 
change already fixed the with-headers store and added tests to 
`MeteredTimestampedKeyValueStoreWithHeadersTest.java` with the same method 
names you're adding here (`shouldTrackOpenIteratorsMetricForRangeQuery`, 
`shouldTrackOpenIteratorsMetricForTimestampedRangeQuery`). Could you rebase on 
latest trunk?



##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredIteratorTracker.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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 = new LongAdder();
+
+    private final NavigableSet<MeteredIterator> openIterators =
+        new ConcurrentSkipListSet<>(
+            Comparator.comparingLong(MeteredIterator::startTimestamp)
+        );
+
+    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) {
+            return 0L;
+        }
+    }

Review Comment:
   Nit: file is missing a trailing newline.



##########
streams/src/test/java/org/apache/kafka/streams/state/internals/MeteredTimestampedKeyValueStoreTest.java:
##########


Review Comment:
   Suggestion: mirror the with-headers edge-case coverage for the plain store. 
Consider adding `shouldDecrementOpenIteratorsTwiceWhenClosedTwiceFor...` and 
`shouldLeaveIteratorOpenWhenNextThrowsAndNotClosedFor...`, pairing its sibling 
`MeteredTimestampedKeyValueStoreWithHeadersTest.java`



##########
streams/src/test/java/org/apache/kafka/streams/state/internals/MeteredTimestampedKeyValueStoreTest.java:
##########
@@ -451,6 +457,56 @@ public void shouldTrackOpenIteratorsMetric() {
         assertThat((Long) openIteratorsMetric.metricValue(), equalTo(0L));
     }
 
+    @Test
+    public void shouldTrackOpenIteratorsMetricForRangeQuery() {
+        assertTracksOpenIteratorsMetricForQuery(
+            RangeQuery.<String, String>withNoBounds()
+        );
+    }
+
+    @Test
+    public void shouldTrackOpenIteratorsMetricForTimestampedRangeQuery() {
+        assertTracksOpenIteratorsMetricForQuery(
+            TimestampedRangeQuery.<String, String>withNoBounds()
+        );
+    }
+
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    private void assertTracksOpenIteratorsMetricForQuery(final Query<?> query) 
{
+        setUp();
+
+        final QueryResult<KeyValueIterator<Bytes, byte[]>> rawResult =
+            QueryResult.forResult(KeyValueIterators.emptyIterator());
+
+        when(inner.query(
+            any(),
+            any(PositionBound.class),
+            any(QueryConfig.class)
+        )).thenReturn((QueryResult) rawResult);
+
+        init();
+
+        final KafkaMetric openIteratorsMetric = metric("num-open-iterators");
+        assertThat(openIteratorsMetric, not(nullValue()));
+        assertThat((Long) openIteratorsMetric.metricValue(), equalTo(0L));
+
+        final QueryResult<?> result = metered.query(
+            query,
+            PositionBound.unbounded(),
+            new QueryConfig(false)
+        );
+
+        assertTrue(result.isSuccess());
+
+        try (final KeyValueIterator<?, ?> unused =
+                (KeyValueIterator<?, ?>) result.getResult()) {
+            assertThat((Long) openIteratorsMetric.metricValue(), equalTo(1L));
+        }
+
+        assertThat((Long) openIteratorsMetric.metricValue(), equalTo(0L));
+    }
+    
+

Review Comment:
   Nit: drop the extra blank line after the new test method



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