Versions Compared

Key

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

...

Lazy initialization for RecordHeader was introduced in KAFKA-10438, improving performance but also creating unexpected side effects.
Since the Consumer is not thread-safe, the same assumption naturally extends to ConsumerRecord. However, users often assume that read-only access across threads is safe.
With lazy initialization, this assumption no longer holds, which can lead to unexpected behavior.
So far, three concurrency-related issues (KAFKA-12999, KAFKA-17725, KAFKA-18470) have been reported in connection with RecordHeader data access.
Making We can make RecordHeader thread-safe would help avoid user confusion and prevent similar issues in the future.to ensure that users accessing it in a read-only manner do not encounter a NullPointerException. 
(Note that because the Consumer is not thread-safe, if other threads can modify the RecordHeader through the Consumer, its thread-safety can no longer be guaranteed.)

Public Interfaces

org.apache.kafka.common.header.internals.RecordHeader class will be updated to be thread-safe.

...

Code Block
languagejava
titleRecordHeader Change
linenumberstrue
public class RecordHeader implements Header {
    private ByteBuffer keyBuffer;
    private volatile String key;
    private ByteBuffer valueBuffer;
    private volatile byte[] value;

    public String key() {
        if (key == null) {
            synchronized (this) {
                if (key == null) {
                    key = Utils.utf8(keyBuffer, keyBuffer.remaining());
                    keyBuffer = null;
                }
            }
        }
        return key;
    }

    public byte[] value() {
        if (value == null && valueBuffer != null) {
            synchronized (this) {
                if (value == null && valueBuffer != null) {
                    value = Utils.toArray(valueBuffer);
                    valueBuffer = null;
                }
            }
        }
        return value;
    }
}

...

Full-Method Synchronization means that the entire key()/ value() method is synchronized.

Benchmark code

...