Skip to content
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

KAFKA-17100: GlobalStreamThread#start should not busy-wait #16914

Merged
merged 5 commits into from
Aug 25, 2024

Conversation

raminqaf
Copy link
Contributor

@raminqaf raminqaf commented Aug 19, 2024

More detailed description
I have replaced the sleep method with a CountDownLatch. It will await until the latch count reaches zero. The countdown happens in the initialize method in a finally block.

Summary of testing strategy
All the tests in GlobalStreamThreadTest pass, indicating that the class's behavior has remained unchanged.

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

Comment on lines 179 to 183
// Question: Is this a good idea? Or should we spread the latch countdown to finally blocks
// Count down the latch if transitioning from CREATED to any other state
if (oldState == State.CREATED && newState != State.CREATED) {
initializationLatch.countDown();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gharris1727 This works (all the tests pass), but I want to double-check it with you. The other alternative would be to count down in finally blocks across the class.

Copy link
Contributor

Choose a reason for hiding this comment

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

The other alternative would be to count down in finally blocks across the class.

It could make sense to have a finally block in the initialize method and call countDown in shutdown, but the current implementation appears to have reasonable semantics.

I have a weak feeling that counting down this latch is too much for the setState method to be doing, but at the same time pushing the latch management out into the calling code risks missing a (possibly future) call-site which transitions away from CREATED.

I think i would leave this to see what the Streams folks think about it.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @gharris1727 -- this is not the right place for this code. I am not 100% sure right now what the best place would be, but both run() or initialize() look like good candidates (maybe initialize() is better).

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 moved the countDown logic to the finally block of initialize. It sounds reasonable to me.

@raminqaf raminqaf changed the title GlobalStreamThread#start should not busy-wait [KAFKA-17100] GlobalStreamThread#start should not busy-wait Aug 19, 2024
@raminqaf raminqaf changed the title [KAFKA-17100] GlobalStreamThread#start should not busy-wait KAFKA-17100: GlobalStreamThread#start should not busy-wait Aug 19, 2024
Copy link
Contributor

@gharris1727 gharris1727 left a comment

Choose a reason for hiding this comment

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

This seems reasonable to me, and captures the intent of my ticket.

@mjsax @ableegoldman Could one of you PTAL, thanks!

Comment on lines 179 to 183
// Question: Is this a good idea? Or should we spread the latch countdown to finally blocks
// Count down the latch if transitioning from CREATED to any other state
if (oldState == State.CREATED && newState != State.CREATED) {
initializationLatch.countDown();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The other alternative would be to count down in finally blocks across the class.

It could make sense to have a finally block in the initialize method and call countDown in shutdown, but the current implementation appears to have reasonable semantics.

I have a weak feeling that counting down this latch is too much for the setState method to be doing, but at the same time pushing the latch management out into the calling code risks missing a (possibly future) call-site which transitions away from CREATED.

I think i would leave this to see what the Streams folks think about it.

initializationLatch.await();
} catch (final InterruptedException e) {
currentThread().interrupt();
throw new IllegalStateException("Thread was interrupted during initialization", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a "new" exception, as the previous code would have just spun calling Utils.sleep and continuously throwing InterruptedExceptions. It's more informative than the other IllegalStateException thrown here, so this seems reasonable.

@mjsax mjsax added the streams label Aug 22, 2024
initializationLatch.await();
} catch (final InterruptedException e) {
currentThread().interrupt();
throw new IllegalStateException("Thread was interrupted during initialization", e);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new IllegalStateException("Thread was interrupted during initialization", e);
throw new IllegalStateException("GlobalStreamThread was interrupted during initialization", e);

@raminqaf raminqaf requested review from gharris1727 and mjsax August 23, 2024 07:55
@@ -436,6 +430,8 @@ private StateConsumer initialize() {
} catch (final Exception fatalException) {
closeStateConsumer(stateConsumer, false);
startupException = new StreamsException("Exception caught during initialization of GlobalStreamThread", fatalException);
} finally {
initializationLatch.countDown();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think to keep the existing semantics, another one of these is needed in shutdown.

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 we need to right now, given that start() blocks and thus shutdown() cannot be triggered as long as init() did not finish (cf https://issues.apache.org/jira/browse/KAFKA-7380)

But maybe good to add to be future proof...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting... 💭 I have added the countdown to the shutdown().

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.

@gharris1727 are you happy with the PR? Can we merge it?

Copy link
Contributor

@gharris1727 gharris1727 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @raminqaf!

@mjsax mjsax merged commit 9eb7e1a into apache:trunk Aug 25, 2024
4 of 7 checks passed
@mjsax
Copy link
Member

mjsax commented Aug 25, 2024

Thanks for the PR @raminqaf -- merged to trunk.

LoganZhuZzz pushed a commit to LoganZhuZzz/kafka that referenced this pull request Aug 28, 2024
)

This PR replaces a busy-wait sleep with a CountDownLatch.

Reviewers: Greg Harris <[email protected]>, Matthias J. Sax <[email protected]>
@raminqaf raminqaf deleted the KAFKA-17100 branch August 28, 2024 07:37
bboyleonp666 pushed a commit to bboyleonp666/kafka that referenced this pull request Sep 4, 2024
)

This PR replaces a busy-wait sleep with a CountDownLatch.

Reviewers: Greg Harris <[email protected]>, Matthias J. Sax <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants