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

Threadsafe transactions #888

Merged
merged 7 commits into from
Mar 9, 2022

Conversation

janstenpickle
Copy link
Contributor

Addresses a bug with concurrent transactional producer access discovered in #883.

Introduces a semaphore around the transactional produce WithProducer implementation. This is because only one transaction may be started per producer at any one time, so produce requests must be queued.

Introduces a semaphore around the transactional produce
`WithProducer` implementation. This is because only one transaction
may be started per producer at any one time, so produce requests
must be queued.
Adds separate `WithProducer` implementation that uses a semaphore for
guaranteeing exclusive access to the producer for concurrent
transactional operations.
Comment on lines 42 to 45
val close = withProducer.exclusiveAccess {
case (producer, blocking) =>
blocking(producer.close(settings.producerSettings.closeTimeout.asJava))
}
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 realised that closing the producer should probably have exclusive access too!

Copy link
Member

Choose a reason for hiding this comment

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

Not sure whether it's necessary but definitely shouldn't do any harm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think you're right it might not be necessary, because the producer will wait for any in flight records to be flushed or the provided timeout to expire. From the perspective of not letting any other records get added to the producer during shutdown this is quite nice, but this shouldn't happen thanks to Resource, unless someone has done something silly like leak the producer out of the resource.

It also changes the behaviour of the closeTimeout setting for the transactional producer; we're using the semaphore to sequence transactions where we wait for the client buffer to flush and transaction comitted to the broker, the closeTimeout effectively becomes redundant when we use the semaphore with close because the buffer will always be empty. Given this do you think it's better to remove the exclusiveAccess call here?

@janstenpickle janstenpickle requested a review from bplommer March 8, 2022 09:42
bplommer and others added 3 commits March 8, 2022 10:35
Add comment explaining why exclusive access there is bad
@janstenpickle
Copy link
Contributor Author

janstenpickle commented Mar 8, 2022

@bplommer it's probably not a problem because no one has reported any issues with concurrent access, but this change will make concurrent access to a single producer effectively single-threaded. I wonder if it's worth updating the documentation noting this, maybe even providing guidance on pooling producers?

@bplommer
Copy link
Member

bplommer commented Mar 8, 2022

@janstenpickle that does sound worthwhile - I'm happy to merge this PR without it and am unlikely to get to it myself in the near future, but if you want to add that change here or in a follow-up PR (or just open an issue) you're more than welcome to 😅

@janstenpickle
Copy link
Contributor Author

Yes, of course. Happy to add the warning, I haven't experimented with pooled transactional producers myself, so probably won't add anything other than a vague recommendation for now. As you said elsewhere pooling functionality something that could be added to 3.x.

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

Successfully merging this pull request may close these issues.

2 participants