Skip to content

Conversation

Nikita-Shupletsov
Copy link
Contributor

@Nikita-Shupletsov Nikita-Shupletsov commented Sep 9, 2025

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

@github-actions github-actions bot added triage PRs from the community streams small Small PRs and removed triage PRs from the community labels Sep 9, 2025
(config, now) -> openIterators.first().startTimestamp()
(config, now) -> {
final Iterator<MeteredIterator> openIteratorsIterator = openIterators.iterator();
return openIteratorsIterator.hasNext() ? openIteratorsIterator.next().startTimestamp() : null;
Copy link
Member

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.

Copy link
Contributor Author

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.

@Nikita-Shupletsov Nikita-Shupletsov changed the title KAFKA-19679: Fixed the regression in the metric calculation to avoid NoSuchElementException KAFKA-19679: Changed the calculation logic of oldest start timestamp to avoid NoSuchElementException Sep 9, 2025
Copy link
Member

@mjsax mjsax left a 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.

@mjsax mjsax changed the title KAFKA-19679: Changed the calculation logic of oldest start timestamp to avoid NoSuchElementException KAFKA-19679: Fix NoSuchElementException in oldest open iterator metric Sep 10, 2025
…cs/OpenIteratorsTest.java


Fix spotless error
@mjsax mjsax merged commit 8015c87 into apache:trunk Sep 10, 2025
20 checks passed
mjsax pushed a commit that referenced this pull request Sep 10, 2025
#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>
@mjsax
Copy link
Member

mjsax commented Sep 10, 2025

Thanks for the PR @Nikita-Shupletsov! Merged to trunk and cherry-picked to 4.1 branch.

Congrats to your first contribution!

@mjsax
Copy link
Member

mjsax commented Sep 10, 2025

Oh. While trying to cherry-pick to 4.0 branch, I realize that 4.0.0 is not affected. We never cherry-picked to fix the "return null" issue (https://issues.apache.org/jira/browse/KAFKA-19398) to 4.0.

So if we would want to fix in 4.0.1, we would need to cherry-pick both commits. Might not be worth the trouble, given that a next RC is already lines up.

Copy link
Contributor

@stevenschlansker stevenschlansker left a 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();
Copy link
Contributor

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)

Copy link
Contributor Author

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

Copy link
Contributor

@stevenschlansker stevenschlansker Sep 10, 2025

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.

Copy link
Member

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?

Copy link
Contributor

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) {
Copy link
Contributor

@stevenschlansker stevenschlansker Sep 10, 2025

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.

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for clarifying!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants