Skip to content
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 4 commits into from
Sep 15, 2021
Merged

Conversation

brenuart
Copy link
Collaborator

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.

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 brenuart force-pushed the gh630-refactor-jsongenerator-pooling branch from 5434f24 to 6241695 Compare September 14, 2021 18:58
Copy link
Collaborator

@philsttr philsttr left a 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.

… 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).
@brenuart brenuart merged commit 4a6ccbf into main Sep 15, 2021
@brenuart brenuart deleted the gh630-refactor-jsongenerator-pooling branch September 15, 2021 17:15
@philsttr philsttr added this to the 7.0 milestone Sep 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants