Table of Contents |
---|
Status
Current state: Under Discussion
...
Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).
Motivation
The JVM based KafkaConsumer contains a check that rejects nested invocations from different threads (in method acquire). For programs that use an async runtime, this is an almost impossible requirement. Also, the check is more strict than is required; we only need to validate that there is no concurrent access to the consumer.
Examples of affected async runtimes are Kotlin co-routines (see KAFKA-7143) and Zio.
Public Interfaces
Two new method will be added to org.apache.kafka.clients.consumer.KafkaConsumer
: getThreadAccessKey
and setThreadAccessKey
.
One class is added: org.apache.kafka.clients.consumer.ThreadAccessKey
.
Proposed Changes
In this PR we replace the thread-id check with an access-key that allows a callback to pass on access to the Kafka consumer.
...
When release
is invoked, we first validate that the top of the stack is equal to the thread-local variable. If it is not equal, it means that a callback didn't wait for the other thread to complete invoking the consumer. After the check we pop the top value of the access-key stack, and restore the thread-local variable to its previous value. The thread-local variable is restored by copying the new top of the stack into it, or if the stack is now empty we clear the thread-local variable.
Details
We use object identity to compare access keys. For this purpose the class ThreadAccessKey
is introduced. This has the advantages that it is not possible to guess keys and it gives an efficient implementation.
When one of the described checks in acquire
or release
fail, we throw a ConcurrentModificationException
similar to current behavior of acquire
and release
.
Compatibility, Deprecation, and Migration Plan
For existing users nothing changes, only the exception message for using the consumer from the wrong thread changes.
There is no need to deprecate anything. No migration is needed.
Test Plan
Background Color | ||
---|---|---|
| ||
INPUT NEEDED |
...
As far as the author is aware, there are currently no (integration) tests that test the thread-id check. If there would be, these should continue to pass. In addition, they could be extended to support the additional behavior.
Rejected Alternatives
Alternative A: add a configuration to disable the thread-id check
Disabling the thread-id check based on configuration would be a very easy change. However, without the check it will become very easy to use the consumer wrong, especially from multi-threaded asynchronous runtimes.
Alternative B: disallow concurrent invocations, but allow them from any thread
This is a stronger approach than alternative A, but still a lot weaker than the proposed change. For example, with this alternative, when a callback is running, a completely unrelated thread may use the consumer. Since that thread is unrelated there is no coordination between when the callback ends and the other thread causing the consumer to be running on multiple threads after all. This can lead to very hard to track bugs.