You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

Status

Current state: Under Discussion Work in progress

Discussion thread: here [Change the link from the KIP proposal email archive to your own email thread]

JIRA: here

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.

Proposed Changes

In this PR we replace the thread-id check with an access-key that is stored on a thread-local variable and is also put on a stack of access keys stored in the consumer. Developers that work in an async runtime can get the access-key and then use it on the thread-local variable in a thread of their choosing.

The consumer maintains a stack of access keys so that invocations from a callback within a callback are possible. The top of the stack corresponds to the most recent invocation. An empty stack means that the consumer was not yet invoked.

When acquire  is invoked, we first check if access is restricted. It is restricted when the access-key stack is not empty. If it is not empty, the thread-local variable must be equal to the value on the top of the stack. If it is empty, any thread may continue. After this check, we generate a new access-key that can be used inside callbacks. This new access key is pushed on the stack and also stored in the thread-local variable.

When after this, the consumer calls a callback, the callback must be able to invoke the consumer again. This is allowed because the thread-local variable corresponds to the top of the stack. Therefore, code that is not aware of this KIP (all programs in existence till now) will continue to work as before.
The callback may now chose to access the thread-local variable, and store the access key on the local-variable of another thread, thereby allowing that thread to access the consumer. Because acquire  immediately and atomically stores a new access key, it is not possible for multiple threads to use a valid access key concurrently.
When a callback passes on the access-key to another thread, it must wait for that other thread to complete before returning from the callback.

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. After the check we pop the top value of the access-key stack, and then copy the new top of the stack to the thread-local variable, or if the stack is now empty we clear the thread-local variable.

Details

The access keys must be sufficiently random so that they cannot be guessed.

When a check fails, we throw a ConcurrentModificationException similarly to current behavior of the consumer.

Compatibility, Deprecation, and Migration Plan

For existing-users (almost) nothing changes. The only change is when the program calls the consumer from the wrong thread: the message of the ConcurrentModificationException no longer contains the expected thread id.

There is no need to deprecate anything. No migration is needed.

Test Plan

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 for us. 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 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 threads after all. This can lead to very hard to track bugs.

  • No labels