Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Public Interfaces

This KIP proposes two ways a way to implement this enhancement:

...

If the `Producer` detects that the `flush` method is invoked within the callback of the `close` method, throw a `KafkaException`. This approach would require modifying org.apache.kafka.clients.producer.Producer#flush  to include KafkaException .

Proposed Changes

If we choose to follow point 2, we will need to make the following changesThe following code is a demonstration of how we will add to the flush method:

Code Block
languagejava
titleProducer#flush
    /**
	 *                Omitting the unchange section ......
     * <p>
     * <b>Important:</b> This method must not be called from within the callback provided to
     * {@link #send(ProducerRecord, Callback)}.Invoking <code>flush()</code> in this context will result in a
     * {@link KafkaException} being thrown, as it will cause a deadlock.
     * </p>
     *
     * @throws InterruptException If the thread is interrupted while blocked
     * @throws KafkaException If the method is invoked inside a {@link #send(ProducerRecord, Callback)} callback
     */
    @Override
    public void flush() {
        if (Thread.currentThread() == this.ioThread) {
            log.error("KafkaProducer.flush() invocation inside a callback is not permitted because it may lead to deadlock.");
            throw new KafkaException("KafkaProducer.flush() invocation inside a callback is not permitted because it may lead to deadlock.");
        }
		
		// Omitting the unchange section ......

    }

...

Add a unit test to ensure that the deadlock protection throws a `KafkaException` with the corresponding error message to inform the user.

Rejected Alternatives

Adding an error log:

If the `Producer` detects that the `flush` method is invoked within the callback of the `close` method, log an error message.

Reason for Rejection

Although adding an error log message could help users understand what happened, it does not provide sufficient protection.

This KIP aims to offer fast-fail protection, which would save users time by allowing the `send` method to return immediately.N/A