Skip to content

LoggingProducerListener.onError() causes OutOfMemory exceptions for large messages #1898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
krocodl opened this issue Aug 2, 2021 · 2 comments · Fixed by #1904
Closed

LoggingProducerListener.onError() causes OutOfMemory exceptions for large messages #1898

krocodl opened this issue Aug 2, 2021 · 2 comments · Fixed by #1904

Comments

@krocodl
Copy link

krocodl commented Aug 2, 2021

For spring-kafka 2.5.2.RELEASE

The problem code fragment:

.append(" and payload='") .append(toDisplayString(ObjectUtils.nullSafeToString(record.value()), this.maxContentLogged))

For example, it the service tries to handle 20Mb message and the parameter "spring.cloud.stream.kafka.binder.configuration.max.request.size" is not increased accordingly, we expect to receive an exception about the "max.request.size" value.

But instead of this, ObjectUtils.nullSafeToString(record.value()) attempts to transform byte[] inside the String, where each byte is represented by about 5 bytes - something like ", 123".
As a result, we receive OOM, where the StringJoiner object occupies about half of the memory.

So, it is necessary to pass the "this.maxContentLogged" parameter inside the function "ObjectUtils.nullSafeToString" too, in order to prevent dumping the full byte array.

@krocodl
Copy link
Author

krocodl commented Aug 2, 2021

BTW, this pattern is widely copied in Kafka-related projects. For example, we can see completely the same in the KafkaMessageChannelBinder.DlqSender.sendToDlq() and have completely the same OOM error with large messages.

@krocodl
Copy link
Author

krocodl commented Aug 2, 2021

A temporary workaround:
@Bean public ProducerListener<Object, Object> fixedKafkaProducerListener() { return new FixedLoggingProducerListener<>(); }

This listener will be already used by createProducerMessageHandler:387, KafkaMessageChannelBinder
but NOT by
getErrorMessageHandler:1058, KafkaMessageChannelBinder

garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Aug 9, 2021
Resolves spring-projects#1898 (comment)

Large `byte[]` payloads could cause an OOM.

**cherry-pick to 2.7.x, 2.6.x, 2.5.x**
artembilan pushed a commit that referenced this issue Aug 9, 2021
Resolves #1898 (comment)

Large `byte[]` payloads could cause an OOM.

**cherry-pick to 2.7.x, 2.6.x, 2.5.x**
artembilan pushed a commit that referenced this issue Aug 9, 2021
Resolves #1898 (comment)

Large `byte[]` payloads could cause an OOM.

**cherry-pick to 2.7.x, 2.6.x, 2.5.x**
artembilan pushed a commit that referenced this issue Aug 9, 2021
Resolves #1898 (comment)

Large `byte[]` payloads could cause an OOM.

**cherry-pick to 2.7.x, 2.6.x, 2.5.x**
artembilan pushed a commit that referenced this issue Aug 9, 2021
Resolves #1898 (comment)

Large `byte[]` payloads could cause an OOM.

**cherry-pick to 2.7.x, 2.6.x, 2.5.x**

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/support/LoggingProducerListener.java
garyrussell added a commit that referenced this issue Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant