-
Notifications
You must be signed in to change notification settings - Fork 14.7k
KAFKA-19679: Fix NoSuchElementException in oldest open iterator metric #20512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
(config, now) -> openIterators.first().startTimestamp() | ||
(config, now) -> { | ||
final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator(); | ||
return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we we ever want to return null
-- that why we added dynamic register/deregister behavior.
I believe, we would need to somehow cache the "last result" before we remove the last iterator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good suggestion. I've updated the PR. please let me know if the changes make sense to you.
…to avoid NoSuchElementException
a2da6c3
to
045efd4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, with two minor suggestion. Will merge both of them, and merge this PR to get it into AK 4.0.1 release.
streams/src/test/java/org/apache/kafka/streams/internals/metrics/OpenIteratorsTest.java
Outdated
Show resolved
Hide resolved
streams/src/test/java/org/apache/kafka/streams/internals/metrics/OpenIteratorsTest.java
Show resolved
Hide resolved
…cs/OpenIteratorsTest.java
…cs/OpenIteratorsTest.java
streams/src/test/java/org/apache/kafka/streams/internals/metrics/OpenIteratorsTest.java
Show resolved
Hide resolved
…cs/OpenIteratorsTest.java Fix spotless error
#20512) Querying the oldest-open-iterator metric can result in a NoSuchElementException when the last open iterator gets removed, due to a race condition between the query and the metric update. To avoid this race condition, this PR caches the metric result, to avoid accessing the list of open iterator directly. We don't need to clear this cache, because the entire metric is removed when the last iterator gets removed. Reviewers: Matthias J. Sax <matthias@confluent.io>
Thanks for the PR @Nikita-Shupletsov! Merged to Congrats to your first contribution! |
Oh. While trying to cherry-pick to So if we would want to fix in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for taking a look at this issue! I agree this PR fixes the issue as submitted - one comment regarding a possible related issue. We will test this out this week and report any issues we find.
|
||
public void add(final MeteredIterator iterator) { | ||
openIterators.add(iterator); | ||
updateOldestStartTimestamp(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible nano-optimization: the oldest start timestamp will only change if this is the first open iterator - any other open iterator will definitionally be older - so we could possibly only update the oldest timestamp inside the if (openIterators.size() == 1)
block below?
(assuming timestamps are all coming from the same time source)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not 100% sure I understand the idea.
If we add 3 iterators with timestamps: 1, 2, 3
the result will be 1.
if we delete the first one, the result will be 2. the result changed, even though we still have more than 1 open iterator.
if I misunderstood the idea, please let me know
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am only referring to the add()
flow - which will add 1, then add 2, then add 3 - and only the first operation may change the oldest iterator.
I agree in the remove()
case we must check every time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I was reviewing the PR, I also though about this, but though it's not worth doing, as creating iterators should not happen with very high frequency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In practice, I doubt it matters much, which is why I called it a "nano" optimization. I leave it in your capable hands to decide if it is worth implementing :)
That said, we actually are having scalability issues regarding creating iterators at relatively high frequency - see https://issues.apache.org/jira/browse/KAFKA-19678
One of our data stores is used almost exclusively for range lookups which use iterators.
openIterators.add(iterator); | ||
updateOldestStartTimestamp(); | ||
|
||
if (openIterators.size() == 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this more, I wonder if there is another possible race here that would not cause an exception but could cause missing or incorrectly persisting metrics. The OpenIterators tracker itself is not synchronized (good to avoid contention) but that means that we are mutating add()
or remove()
and calling size()
expecting the results in program order.
But, I don't see any guarantee this is the case - my (limited) understanding is that OpenIterators is a property of a KeyValueStore, which may have partitions shared between threads.
Imagine:
T1: open iterator on store S1 (partition 0)
T2: open iterator on store S1 (partition 1)
T1: openIterators.add(it1)
T2: openIterators.add(it2)
T1: if (openIterators.size() == 1) { (2 == 1? nope)
T2: if (openIterators.size() == 1) { (2 == 1? nope)
End: 2 iterators open, no metric registered
Same race is possible with remove.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. there is a potential race condition here. I will take a closer look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that's a problem. While stores are sharded, each shard is its own StateStore
and thus has an own OpenIterator
instance. Hence, OpenIterator
should be used by a single StreamsThreads
and there should not be any concurrent usage.
The concurrently issue we fixed in only for the metric, because addOldestOpenIteratorGauge
registers the Gauge (config, now) -> openIterators.first().startTimestamp()
(well, now (config, now) -> oldestStartTimestamp.get()
) which could be used concurrently, when the metric is read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, thanks for clarifying!
Querying the oldest-open-iterator metric can result in a
NoSuchElementException when the last open iterator gets removed, due to
a race condition between the query and the metric update.
To avoid this race condition, this PR caches the metric result, to avoid
accessing the list of open iterator directly. We don't need to clear
this cache, because the entire metric is removed when the last iterator
gets removed.
Reviewers: Matthias J. Sax matthias@confluent.io