You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "stevenschlansker (via GitHub)" <gi...@apache.org> on 2025/01/31 21:17:20 UTC

[PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

stevenschlansker opened a new pull request, #18771:
URL: https://github.com/apache/kafka/pull/18771

   I opened KAFKA-18689 because we see in our application:
   
   ```
   java.util.NoSuchElementException: null
   	at java.base/java.util.concurrent.ConcurrentSkipListMap.firstKey(ConcurrentSkipListMap.java:1863)
   	at java.base/java.util.concurrent.ConcurrentSkipListSet.first(ConcurrentSkipListSet.java:398)
   	at org.apache.kafka.streams.state.internals.MeteredKeyValueStore.lambda$registerMetrics$5(MeteredKeyValueStore.java:176)
   	at org.apache.kafka.common.metrics.KafkaMetric.metricValue(KafkaMetric.java:81)
   	at com.paywholesail.service.search.core.streams.KafkaStreamsMetricsManager$1.getValue(KafkaStreamsMetricsManager.java:53)
   	at io.dropwizard.metrics5.graphite.GraphiteReporter.reportGauge(GraphiteReporter.java:480)
   	at io.dropwizard.metrics5.graphite.GraphiteReporter.report(GraphiteReporter.java:378) 
   ```
   
   When looking at the code to fix it though, it seems there is already a fix applied. So no action is strictly necessary.
   However, I think the previous fix can be improved - instead of allowing the race to happen and catch the thrown exception, if we just use an iterator instead, we should get a (weakly) consistent view of the set despite concurrent modification.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938113387


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   Can you explain how this avoids the race condition that you actually hit in 3.9.0 and the code in `trunk` already fixes? It actually seems to re-introduce the race condition?
   
   Even if `openIterators` is a `ConcurrentSkipListSet`, there is still a race between `hasNext()` and `next()` and even after `hasNext()` returns `true`, the last iterator could be close and remove from `openIterators` concurrently, and `next()` would still throw an `NoSuchElementException`?
   
   We had some discussion on the original PR about this question, too, and concluded that we do not want to introduce heavy-weight locking, but just catch and swallow `NoSuchElementException` in case it happens: https://github.com/apache/kafka/pull/17713/files#r1833310356
   
   I am not a fan of the try-catch-swallow approach, and if we did miss anything, I would be very happy to change the code. But it seems your PR does not do the trick.
   
   Thoughts?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938345281


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   https://www.javaspecialists.eu/archive/Issue288-Weakly-Consistent-Iteration.html
   
   Refers to Java 16 (we are using 11 for Kafka Streams since AK 4.0).
   
   This one seems to be more conclusive:
   
   > Vector's Enumeration was neither fail-fast nor weakly-consistent. We could create an Enumeration, but even if hasMoreElements() was true, nextElement() could throw a NoSuchElementException. Also, with Vector if we removed an element before our current position, then we would skip over the next element. Similarly, adding an element before our current position would make us see duplicate elements. These effects do not happen with weakly-consistent iterators.
   
   `These effects do not happen with weakly-consistent iterators.` seems to refer to `NoSuchElementException`



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938344950


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   https://stackoverflow.com/questions/28915215/iterator-type-in-java-weakly-consistent
   
   Somewhat old, and refers to Java 8 only.
   
   Not sure how to read this one:
   
   > What this doesn't say (and what may be implicit in the use of the word "consistent") is that iteration won't result in an error such as IndexOutOfBoundsException or NoSuchElementException [..] The third bullet in particular explicitly doesn't make any guarantees about what elements are seen by the iteration if modifications occur during iteration.
   
   Also:
   
   > Weakly consistent iteration provides guarantees against repeated elements and against a variety of errors or infinite loops that can occur. The "weakness" is that they provide few guarantees about exactly which elements are observed during iteration.
   
   In the end, I can only infer that we don't know what elements we get back. It seems not to be conclusive about `hasNext/next` guarantees / safety.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938343500


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   > I think the only way to be sure would be to check in the JDK implementation and hope they don't change it in the future.
   
   I would not do want to rely on the implementation, but only if it's a public API contract.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938345281


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   https://www.javaspecialists.eu/archive/Issue288-Weakly-Consistent-Iteration.html



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938344950


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   https://stackoverflow.com/questions/28915215/iterator-type-in-java-weakly-consistent
   
   Somewhat old, and refers to Java 8 only.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "nicktelford (via GitHub)" <gi...@apache.org>.
nicktelford commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938365936


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   > Also talked to @ijuma about it, and he made the good point that `hasNext/next` must provide the guarantee to not throw `NoSuchElementException`, otherwise every multi-threaded "foreach" would be broken on a concurrent collection.
   
   Yeah, that's what I've been thinking over the last few hours too. This is why I think checking the implementation for clarification wouldn't be terrible, because it would likely clarify the guarantees they have documented.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on PR #18771:
URL: https://github.com/apache/kafka/pull/18771#issuecomment-2635579959

   @stevenschlansker -- can you update this PR so we can move forward with it?


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "stevenschlansker (via GitHub)" <gi...@apache.org>.
stevenschlansker commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938120110


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   Thanks for considering this PR @mjsax . I admit I do not have too much experience with `ConcurrentSkipListSet` (and the `ConcurrentSkipListMap` underlying it), but they promise:
   
   > Iterators and spliterators are weakly consistent.
   
   where weakly consistent is defined:
   
   > Most concurrent Collection implementations (including most Queues) also differ from the usual java.util conventions in that their Iterators and Spliterators provide weakly consistent rather than fast-fail traversal:
   >    they may proceed concurrently with other operations
   >    they will never throw ConcurrentModificationException
   >    they are guaranteed to traverse elements as they existed upon construction exactly once, and may (but are not guaranteed to) reflect any modifications subsequent to construction.
   
   I take this to mean that by taking a single `openIterators.iterator()` we then get a weakly consistent handle on the structure, and will either observe an element or not, but such an iterator should never run into the case where `hasNext()` returns `true` but then `next()` throws (which would not be "consistent" to me).
   
   I admit the docs stating "they may proceed concurrently with other operations" "won't throw CME" isn't 100% clear on this point about the slightly different NoSuchElementException.
   
   Assuming my analysis is correct, this is an improvement because the old code `isEmpty() + first()` is taking two "weak snapshots" of the data structure that are not consistent, but `iterator() { hasNext() + next() }` takes a single weak snapshot.
   
   If this isn't sufficiently convincing, I'm fine to drop this PR and move on with life :) 



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938344950


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   https://stackoverflow.com/questions/28915215/iterator-type-in-java-weakly-consistent
   
   Somewhat old, and refers to Java 8 only.
   
   Not sure how to read this one:
   
   > What this doesn't say (and what may be implicit in the use of the word "consistent") is that iteration won't result in an error such as IndexOutOfBoundsException or NoSuchElementException [..] The third bullet in particular explicitly doesn't make any guarantees about what elements are seen by the iteration if modifications occur during iteration.
   
   Also:
   
   > Weakly consistent iteration provides guarantees against repeated elements and against a variety of errors or infinite loops that can occur. The "weakness" is that they provide few guarantees about exactly which elements are observed during iteration.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938345391


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   Seems we cannot rely on not getting an `NoSuchElementException`...



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938344950


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   https://stackoverflow.com/questions/28915215/iterator-type-in-java-weakly-consistent
   
   Somewhat old, and refers to Java 8 only.
   
   > What this doesn't say (and what may be implicit in the use of the word "consistent") is that iteration won't result in an error such as IndexOutOfBoundsException or NoSuchElementException



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938113387


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   Can you explain how this avoids the race condition that you actually hit in 3.9.0 and the code in `trunk` already fixes? It actually seems to re-introduce the race condition?
   
   Even if `openIterators` is a `ConcurrentSkipListSet`, there is still a race between `hasNext()` and `next()` and even after `hasNext()` returns `true`, the last iterator could be close and remove from `openIterators` concurrently, and `next()` would still throw an `NoSuchElementException`?
   
   We had some discussion on the original PR about this question, too, and concluded that we do not want to introduce heavy-weight locking, but just catch and swallow `NoSuchElementException` in case it happens: https://github.com/apache/kafka/pull/17713/files#r1833310356
   
   I am not a fan of the try-catch-swallow approach, and if we did miss anything, I would be very happy to change the code. But it seems you PR does not do the trick.
   
   Thoughts?



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938568837


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   It can't hurt to check, but as I said, the _implementation_ is no what I care about, but the API contract. Ismael was absolutely sure about it, and it makes sense to me, so IMHO we can go ahead with PR even w/o checking further.
   
   The PR will just need an update for the other two metered stores.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax merged PR #18771:
URL: https://github.com/apache/kafka/pull/18771


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938336028


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   Thanks for digging JavaDocs. Did not have a close looker earlier. I you are right, it seems that
   
   > they are guaranteed to traverse elements as they existed upon construction exactly once, and may (but are not guaranteed to) reflect any modifications subsequent to construction.
   
   Is the key statement. I was never worried about `ConcurrentModificationException`, but only about `NoSuchElementException`.
   
   Maybe @nicktelford, who did the original PR can help out on this question, just to double check.
   
   Btw: if we go forward with this PR, we need to also apply this change to `MeteredWindowedStore` and `MeteredSessionStore`.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "nicktelford (via GitHub)" <gi...@apache.org>.
nicktelford commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938340249


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   As far as I understand, "weakly consistent" means that two different threads, constructing two different iterators over the structure would see a view of the structure that is consistent with _each other_ even in the face of additions or removals. 
   
   I'm not certain of the behaviour of an Iterator between `hasNext` and `next` calls. I'm on my phone right now so can't check, but it's possible that `hasNext` actually takes a reference to the next object that `next` then returns. This is how `KeyValueIterator` works IIRC.
   
   I think the only way to be sure would be to check in the JDK implementation and hope they don't change it in the future.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938344950


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   https://stackoverflow.com/questions/28915215/iterator-type-in-java-weakly-consistent



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "stevenschlansker (via GitHub)" <gi...@apache.org>.
stevenschlansker commented on PR #18771:
URL: https://github.com/apache/kafka/pull/18771#issuecomment-2628414500

   If you do not like this improvement, it's totally OK by me to close the issue and PR as duplicate.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "stevenschlansker (via GitHub)" <gi...@apache.org>.
stevenschlansker commented on PR #18771:
URL: https://github.com/apache/kafka/pull/18771#issuecomment-2637799132

   Thank you for the comprehensive discussion here. I've updated the PR to cover the other two cases as well.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938344950


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   https://stackoverflow.com/questions/28915215/iterator-type-in-java-weakly-consistent
   
   Somewhat old, and refers to Java 8 only.
   
   Not sure how to read this one:
   
   > What this doesn't say (and what may be implicit in the use of the word "consistent") is that iteration won't result in an error such as IndexOutOfBoundsException or NoSuchElementException [..] The third bullet in particular explicitly doesn't make any guarantees about what elements are seen by the iteration if modifications occur during iteration.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938343500


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   Thanks Nick!
   
   > I think the only way to be sure would be to check in the JDK implementation and hope they don't change it in the future.
   
   I would not want to rely on the implementation, but only on a public API contract.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938345281


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   https://www.javaspecialists.eu/archive/Issue288-Weakly-Consistent-Iteration.html
   
   Refers to Java 16 (we are using 11 for Kafka Streams since AK 4.0).



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938345391


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   Seems we cannot rely on not getting an `NoSuchElementException`...



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938336028


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   Thanks for digging JavaDocs. Did not have a close looker earlier. I you are right, it seems that
   
   > they are guaranteed to traverse elements as they existed upon construction exactly once, and may (but are not guaranteed to) reflect any modifications subsequent to construction.
   
   Is the key statement. I was never worried about `ConcurrentModificationException`, but only about `NoSuchElementException`.
   
   Maybe @nicktelford, who did the original PR can help out on this question, just to double check.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938344950


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   https://stackoverflow.com/questions/28915215/iterator-type-in-java-weakly-consistent
   
   Somewhat old, and refers to Java 8 only.
   
   Not sure how to read this one:
   
   > What this doesn't say (and what may be implicit in the use of the word "consistent") is that iteration won't result in an error such as IndexOutOfBoundsException or NoSuchElementException [..] The third bullet in particular explicitly doesn't make any guarantees about what elements are seen by the iteration if modifications occur during iteration.
   
   Also:
   
   > Weakly consistent iteration provides guarantees against repeated elements and against a variety of errors or infinite loops that can occur. The "weakness" is that they provide few guarantees about exactly which elements are observed during iteration.
   
   The last sentence seem to indicate that it's safe to use `hasNext/next` -- it's just not guaranteed which elements from the collection are returned. What aligns to the last sentence of the first paragraph (even if the first one is less conclusive about `hasNext/next` safety IMHO).



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938336028


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   Thanks for digging JavaDocs. Did not have a close looker earlier. If you are right, it seems that
   
   > they are guaranteed to traverse elements as they existed upon construction exactly once, and may (but are not guaranteed to) reflect any modifications subsequent to construction.
   
   is the key statement. I was never worried about `ConcurrentModificationException`, but only about `NoSuchElementException`.
   
   Maybe @nicktelford, who did the original PR can help out on this question, just to double check.
   
   Btw: if we go forward with this PR, we need to also apply this change to `MeteredWindowedStore` and `MeteredSessionStore`.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on code in PR #18771:
URL: https://github.com/apache/kafka/pull/18771#discussion_r1938347958


##########
streams/src/main/java/org/apache/kafka/streams/state/internals/MeteredKeyValueStore.java:
##########
@@ -156,11 +156,8 @@ private void registerMetrics() {
                 (config, now) -> numOpenIterators.sum());
         StateStoreMetrics.addOldestOpenIteratorGauge(taskId.toString(), metricsScope, name(), streamsMetrics,
                 (config, now) -> {
-                    try {
-                        return openIterators.isEmpty() ? null : openIterators.first().startTimestamp();
-                    } catch (final NoSuchElementException ignored) {
-                        return null;
-                    }
+                    final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
+                    return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;

Review Comment:
   Also talked to @ijuma about it, and he made the good point that `hasNext/next` must provide the guarantee to not throw `NoSuchElementException`, otherwise every multi-threaded "foreach" would be broken on a concurrent collection.



-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [KAFKA-18689] improve metric calculation to avoid NoSuchElementException [kafka]

Posted by "mjsax (via GitHub)" <gi...@apache.org>.
mjsax commented on PR #18771:
URL: https://github.com/apache/kafka/pull/18771#issuecomment-2638884128

   Thanks for the PR. Merged to `trunk`.


-- 
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-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org