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

Provide a way to customize a transactionIdSuffix #2852

Closed
stillya opened this issue Oct 17, 2023 · 8 comments · Fixed by #2930
Closed

Provide a way to customize a transactionIdSuffix #2852

stillya opened this issue Oct 17, 2023 · 8 comments · Fixed by #2930

Comments

@stillya
Copy link
Contributor

stillya commented Oct 17, 2023

Expected Behavior

Provide strategy to customize a transactionIdSuffix.

Current Behavior

Currently, spring-kafka use AtomicInteger for transactionIdSuffix.

Context

In my environment, I work with restricted transactionId within a specific range(e.g. topic-{1-5}).
Currently, spring-kafka use AtomicInteger for transactionIdSuffix, it would be beneficial to have the flexibility to implement a custom strategy for transactionIdSuffix.
This could involve actions such as resetting the counter after reaching a limit or a storage for managing transactionsIds.

I'm happy to contribute a PR once I know what your opinion on this feature is.

@garyrussell
Copy link
Contributor

This makes sense, although you can't reset it if there is still a producer with that prefix; the reset would have to eject the existing producer from the cache.

A PR would certainly be welcome; thanks!

@Wzy19930507
Copy link
Contributor

Wzy19930507 commented Nov 20, 2023

Restricted transactionId within a specific range(e.g. topic-{1-5})

If all transactionId are in use, DefaultKafkaProducerFactory.createTransactionalProducer's behavior needs to change.
such as return null, wait some time or threw exception.

Is it reasonable to implement a transaction connection pool?

@stillya
Copy link
Contributor Author

stillya commented Nov 20, 2023

Is it reasonable to implement a transaction connection pool?

I don't think it's feasible because a transaction is a shared resource among many instances rather than a cross-one, so we can't suggest which transactionId is being used. In my opinion, the default strategy could be a round-robin approach. When we reach the limit, the counter resets. After each FencedException we got on producing, we increment the counter and eject existing producer from the cache.

@Wzy19930507

This comment was marked as outdated.

@garyrussell
Copy link
Contributor

Probably the simplest solution would be a new property maxCache. When non-zero, we pre-populate a blocking queue with the "available" suffixes.

When a producer is not returned to the cache (either because it was fenced, or because the maxAge expired in expire()) we add that suffix back to the queue for reuse.

When a producer is requested and there are no cached producers, and there are no "available" suffixes, we throw a NoProducerAvailableException.

The user can then use a RetryTemplate configured to retry that exception, with a suitably configured back off.

@Wzy19930507
Copy link
Contributor

Hi, @stillya @garyrussell , If you don't mind, may i pick it up?
I think maxCache is feasible.

@garyrussell
Copy link
Contributor

Sure; thanks!

Wzy19930507 pushed a commit to Wzy19930507/spring-kafka that referenced this issue Nov 23, 2023
add properties `maxCache` at `DefaultKafkaProducerFactory`, setting `maxCache` greater than zero can reuse `transactional.id`.

Resolves spring-projects#2852
Wzy19930507 pushed a commit to Wzy19930507/spring-kafka that referenced this issue Nov 23, 2023
add properties `maxCache` at `DefaultKafkaProducerFactory`, setting `maxCache` greater than zero can reuse `transactional.id`.

Resolves spring-projects#2852
Wzy19930507 pushed a commit to Wzy19930507/spring-kafka that referenced this issue Nov 27, 2023
Introducing a new interface for generating transactionIdSuffix with a default increment implementation.

Introducing a new interface for return the current transactionIdSuffix.

Resolves spring-projects#2852
@stillya
Copy link
Contributor Author

stillya commented Dec 10, 2023

Hi, @Wzy19930507 and @sobychacko.
I've decided to lend a hand to already an existing pull request and created own with a new interface to enable the management of transactionIdSuffix. Could you please take a look?

stillya added a commit to stillya/spring-kafka that referenced this issue Dec 10, 2023
@sobychacko sobychacko modified the milestones: Backlog, 3.2.0-M1 Dec 13, 2023
stillya pushed a commit to stillya/spring-kafka that referenced this issue Jan 18, 2024
stillya pushed a commit to stillya/spring-kafka that referenced this issue Jan 18, 2024
sobychacko pushed a commit that referenced this issue Jan 19, 2024
Fixes: #2852 

* Provide a way to customize the transactionIdSuffix by providing a new strategy API - `TransactionIdSuffixStrategy`
* PR adds a default implementation for this new API
* Related changes in classes that deal with transactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants