-
Notifications
You must be signed in to change notification settings - Fork 407
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
Refactor JsonGenerator pooling #646
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Motivations: (1) For the pooling to be efficient, the feature `USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING` must be disabled when creating JsonGenerator. If not, JsonGenerator creates additional buffers and re-use them per-thread. This pattern is not applicable in our case: there is no relationship between the JsonGenerator to use and the current thread. Pooling Jsongenerator instances and the creation/configuration of these instances (disabling the feature) are therefore related and should ideally be implemented close together, in the same class. (2) Pooling is required only because CompositeJsonFormatter uses Jackson under the cover (JsonGenerator must be given an OutputStream when created). The pooling logic should therefore be isolated and hidden inside the CompositeJsonFormatter itself and considered an implementation detail. This would also lead to a cleaner interface with a single `write(Event event, OutputStream out)` method. This method can be used to write an event to whatever output stream without having to care about pooling at all... (3) The current implementation creates a ReusableByteBuffer and connects the JsonGenerator to it at creation time. They are both pooled at the same time. Content is first generated in the byte buffer before it can be copied in the output stream passed as argument to the write method. This intermediate buffer somehow limits the streaming capability of the implementation. This commit now connects the JsonGenerator to a "DisconnectedOutputStream" when it is created. When the `write(event, out)` method is called, the output stream of the JsonGenerator is connected to the one passed as argument before the generator is invoked. Content produced by the generator is therefore written directly in the target output stream without requiring an intermediate buffer. It is now up to the caller to decide if it needs an intermediate buffer or not... Pooling and buffering are two separate concerns that are now handled separately. (4) The pooling logic is now handled by the `ObjectPool` class and is reused by both the ReusableByteBufferPool and the CompositeJsonFormatter.
brenuart
force-pushed
the
gh630-refactor-jsongenerator-pooling
branch
from
September 14, 2021 18:58
5434f24
to
6241695
Compare
philsttr
reviewed
Sep 15, 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Just a couple minor comments.
src/main/java/net/logstash/logback/composite/CompositeJsonFormatter.java
Outdated
Show resolved
Hide resolved
src/main/java/net/logstash/logback/composite/CompositeJsonFormatter.java
Show resolved
Hide resolved
philsttr
reviewed
Sep 15, 2021
src/main/java/net/logstash/logback/composite/CompositeJsonFormatter.java
Outdated
Show resolved
Hide resolved
… used by StreamingEncoder Deciding if an intermediate buffer is required or not in the constructor was not a good idea: the encoder is not yet known at this point (still null).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is an extension of the work already done for issue #630.
Motivations:
(1)
For the pooling to be efficient, the feature
USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING
must be disabled when creating JsonGenerator. If not, JsonGenerator creates additional buffers and re-use them per-thread. This pattern is not applicable in our case: there is no relationship between the JsonGenerator to use and the current thread.Pooling Jsongenerator instances and the creation/configuration of these instances (disabling the feature) are therefore related and should ideally be implemented close together, in the same class.
(2)
Pooling is required only because CompositeJsonFormatter uses Jackson under the cover (JsonGenerator must be given an OutputStream when created). The pooling logic should therefore be isolated and hidden inside the CompositeJsonFormatter itself and considered an implementation detail. This would also lead to a cleaner interface with a single
write(Event event, OutputStream out)
method. This method can be used to write an event to whatever output stream without having to care about pooling at all...(3)
The current implementation creates a ReusableByteBuffer and connects the JsonGenerator to it at creation time. They are both pooled at the same time. Content is first generated in the byte buffer before it can be copied in the output stream passed as argument to the write method. This intermediate buffer somehow limits the streaming capability of the implementation.
This commit now connects the JsonGenerator to a "DisconnectedOutputStream" when it is created. When the
write(event, out)
method is called, the output stream of the JsonGenerator is connected to the one passed as argument before the generator is invoked. Content produced by the generator is therefore written directly in the target output stream without requiring an intermediate buffer. It is now up to the caller to decide if it needs an intermediate buffer or not... Pooling and buffering are two separate concerns that are now handled separately.(4)
The pooling logic is now handled by the
ObjectPool
class and is reused by both the ReusableByteBufferPool and the CompositeJsonFormatter.