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

Convert commitAsync callback handling to ZIO sooner #1404

Merged
merged 77 commits into from
Dec 7, 2024

Conversation

svroonland
Copy link
Collaborator

@svroonland svroonland commented Nov 24, 2024

KafkaConsumer's commitAsync takes a callback, which we program against with complicated followup code. This PR attempts to convert everything to ZIO's earlier on, making chaining followup effects easier to reason about.

As this changes some functionality around locking and same / single threads, here's a summary of what do we need to ensure:

  • We have an exclusive lock on the consumer when calling commitAsync. In Runloop.run this is done using ConsumerAccess. In the rebalance coordinator (while rebalancing) we already have the lock as we're calling poll() so no need for extra locking.
  • The consumer is not used from more than one thread at the same time. For use in Runloop.run we get this for free by guaranteeing exclusive access. In the rebalance coordinator a poll() call is in the middle of being executed and we need to call commitAsync on the same thread as the rebalance listener is invoked.

Anything that is not calling commitAsync is free to run on any thread as executed by the default ZIO runtime.

svroonland and others added 30 commits November 3, 2024 10:39
…cala

Co-authored-by: Erik van Oosten <e.vanoosten@grons.nl>
…cala

Co-authored-by: Erik van Oosten <e.vanoosten@grons.nl>
…cala

Co-authored-by: Erik van Oosten <e.vanoosten@grons.nl>
With rebalanceSafeCommits, we expect all pulled records to be processed and committed.
Helps understanding the logic here
@svroonland svroonland changed the base branch from separate-rebalance-listener-file to master November 26, 2024 10:52
@svroonland svroonland closed this Nov 26, 2024
@svroonland svroonland reopened this Nov 26, 2024
@svroonland svroonland marked this pull request as ready for review November 26, 2024 19:45
Copy link
Collaborator

@erikvanoosten erikvanoosten left a comment

Choose a reason for hiding this comment

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

I am not done but I have to go. Will look further another time.

Please be aware that this PR is consistently 10ms slower than main (see the benchmarks).

svroonland and others added 3 commits November 27, 2024 17:31
@svroonland svroonland changed the title Convert commitAsync to ZIO sooner Convert commitAsync callabck handling to ZIO sooner Dec 1, 2024
@svroonland svroonland changed the title Convert commitAsync callabck handling to ZIO sooner Convert commitAsync callback handling to ZIO sooner Dec 3, 2024
Copy link
Collaborator

@erikvanoosten erikvanoosten left a comment

Choose a reason for hiding this comment

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

Looks good to me!

private def commitAsyncZIO(
consumer: ByteArrayKafkaConsumer,
offsets: Map[TopicPartition, OffsetAndMetadata],
doOnComplete: Either[Exception, Map[TopicPartition, OffsetAndMetadata]] => UIO[Unit]
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this PR we now always provide a callback to commitAsync, even when the given list of offsets is empty (so a callback is not neccesary). During a rebalance, this method is called a lot (every 100ms) and it is likely that most of these calls do not need a callback.

WDYT of making doOnComplete an Option, and then when it's None we pass null to commitAsync like we did before?

Copy link
Collaborator

Choose a reason for hiding this comment

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

On the other hand, doing this removes the ability to react to errors... 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah yeah I totally missed this. But I don't think it would make a large performance difference if it's only once every 100 ms.

@svroonland svroonland merged commit 4a0176f into master Dec 7, 2024
13 checks passed
@svroonland svroonland deleted the async-commit-zio branch December 7, 2024 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants