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

Compare with Current View Page History

« Previous Version 3 Next »

Status

Current state: Under Discussion

Discussion thread: https://lists.apache.org/thread/tbhmkf44jhjf8lqmo7w2whynbgttf1o6

PR: https://github.com/apache/kafka/pull/12545

Motivation

Reduce Fetcher#parseRecord() memory copy: we can direct use ByteBuffer instead of byte[] to deserialize

Public Interfaces

We propose adding default method Deserializer#deserialize(String, Headers, ByteBuffer).

ClassMethod

Deserializer

default T deserialize(String topic, Headers headers, ByteBuffer data) {
return deserialize(topic, headers, Utils.toArray(data));
}

ByteBufferDeserializer

@Override
public ByteBuffer deserialize(String topic, Headers headers, ByteBuffer data) {
return data;
}

StringDeserializer

@Override
public String deserialize(String topic, Headers headers, ByteBuffer data) {
if (data == null) {
return null;
}

try {
if (data.hasArray()) {
return new String(data.array(), data.position() + data.arrayOffset(), data.remaining(), encoding);
} else {
return new String(Utils.toArray(data), encoding);
}
} catch (UnsupportedEncodingException e) {
throw new SerializationException("Error when deserializing ByteBuffer to string due to unsupported encoding " + encoding);
}
}

Proposed Changes

  • Deserializer add default method deserialize(String, Headers, ByteBuffer);
  • Invoke Deserializer#deserialize(String, Headers, ByteBuffer) instead of Deserializer#deserialize(String, Headers, byte[]) in Fetcher#parseRecord(TopicPartition, RecordBatch, Record).

Compatibility, Deprecation, and Migration Plan

  • This proposal has no compatibility issues, we just add default method deserialize(String, Headers, ByteBuffer) which is compatible with the existing Deserializers.

Rejected Alternatives

Another solution I thought of is PoolArea, just like netty, but this solution is more complicated.

  • No labels